-4

i have several questions:

  1. int backtrace(void **buffer, int size); Here buffer is array of pointers. and the array elements on returns points to each frame of stack in return. So, backtrace() internally calls malloc() number of times as that of number of frames? why this is for?
  2. the above allocated memory is not freed() from backtrace(), So, who release this memory?
  3. is there any way, to avoid malloc() from inside backtrace()?
  4. where can i find backtrace() source code?
  5. how can i write code to backtrace in assembly?
thiton
  • 35,651
  • 4
  • 70
  • 100
sandeep
  • 513
  • 9
  • 17
  • 3
    -1: My 2008-06-14 manpage (and earlier versions too) answers your questions 1 and 2 explicitly. Description, paragraph 2: "and must be freed by the caller". – thiton Jan 04 '12 at 09:52
  • could you please let me know how and where to find "My 2008-06-14 manpage"? – sandeep Jan 04 '12 at 10:32
  • 2
    Run `man backtrace` in a shell (in a terminal) on any linux system. The manual pages are *the* authoritative source of information for all system and core C library functions. – thiton Jan 04 '12 at 12:14
  • this gives description of backtrace command and not function backtrace(). again online documentation talk about freeing memory of backtrace_symbol() not backtrace()... and no where it is writen that backtrace() allocates memory ... and free or dont free it.. – sandeep Jan 05 '12 at 08:51

2 Answers2

4

Please read the manual page for backtrace a bit more carefully. You seem to be mistaken about some of the fundamentals:

  1. No, array is your buffer of pointers, which are filled-in by backtrace() to point at the stack frames. No calls to malloc() are made.
  2. You, since it's your memory.
  3. Not applicable, backtrace() does not call malloc().
  4. It's part of GNU libc. Here is one part of the implementation.
  5. Probably by copying the stack pointer and manually walking the stack.

You seem to confuse backtrace() with backtrace_symbols(), the latter involves memory allocations.

To read out the current backtrace up to a maximum of 64 levels, just do:

void *stack[64];
const int depth = backtrace(stack, sizeof stack / sizeof *stack);
unwind
  • 391,730
  • 64
  • 469
  • 606
  • @backtrace() does not call malloc(). - i had added a hook to malloc() before backtrace call and when i called backtrace() the hook for malloc was called. and the backtrace was failed to retun stack. but when i called malloc specifically from my HOok function, and return the allocated memory from it, backtrace was Successfull.. again, when i added hook for free, it was not invoked .. it lead me to conclude above point. when i read the documentation, i was also assuming as you and now verified it myself.. – sandeep Jan 04 '12 at 10:24
0

Apparently, the GNU backtrace() function calls malloc() once -- and then never calls it again. If one examines the source, one sees that backtrace() invokes several "dl" functions to unwind the stack, and one of those must be calling malloc(). I agree that malloc() is being called. I get around the problem when I load my heapmanager hooks by calling backtrace() once during the initialization phase so I can avoid a recursive call to the hooked malloc() function as I attempt to develop the list of calling sequences in my software associated with the heap.

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33