Backtrace 在 Linux x86_64 上如何工作?
我有几个问题:
- int backtrace(void **buffer, int size); 这里的缓冲区是指针数组。返回上的数组元素指向返回的堆栈的每个帧。那么,backtrace() 内部调用 malloc() 的次数与帧数相同吗?这是为什么?
- 上面分配的内存没有从backtrace()中freed(),那么,谁释放了这块内存呢?
- 有什么办法可以避免在 backtrace() 内部使用 malloc() 吗?
- 在哪里可以找到 backtrace() 源代码?
- 我如何编写代码来在汇编中进行回溯?
i have several questions:
- 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? - the above allocated memory is not freed() from backtrace(), So, who release this memory?
- is there any way, to avoid malloc() from inside backtrace()?
- where can i find backtrace() source code?
- how can i write code to backtrace in assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请仔细阅读手册页以了解回溯。您似乎误解了一些基本原理:
array
是您的指针缓冲区,由backtrace()
填充指向堆栈帧。不会调用malloc()
。backtrace()
不会调用malloc()
。您似乎将
backtrace()
与backtrace_symbols()
混淆了,后者涉及内存分配。要读出最多 64 级的当前回溯,只需执行以下操作:
Please read the manual page for backtrace a bit more carefully. You seem to be mistaken about some of the fundamentals:
array
is your buffer of pointers, which are filled-in bybacktrace()
to point at the stack frames. No calls tomalloc()
are made.backtrace()
does not callmalloc()
.You seem to confuse
backtrace()
withbacktrace_symbols()
, the latter involves memory allocations.To read out the current backtrace up to a maximum of 64 levels, just do:
显然,GNU
backtrace()
函数调用malloc()
一次,然后就不再调用它。如果检查源代码,就会发现 backtrace() 调用了几个“dl”函数来展开堆栈,其中之一必须调用 malloc()。我同意正在调用malloc()
。当我加载我的 heapmanager 挂钩时,我通过在初始化阶段调用一次 backtrace() 来解决这个问题,这样我就可以避免对挂钩的 malloc( 的递归调用) ) 当我尝试在我的软件中开发与堆相关的调用序列列表时,函数起作用。Apparently, the GNU
backtrace()
function callsmalloc()
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 thatmalloc()
is being called. I get around the problem when I load myheapmanager
hooks by callingbacktrace()
once during the initialization phase so I can avoid a recursive call to the hookedmalloc()
function as I attempt to develop the list of calling sequences in my software associated with the heap.