當前位置:
首頁 >
获取函数调用堆栈
發布時間:2023/12/15
39
豆豆
int backtrace(void **buffer,int size)
該函數用于獲取當前線程的調用堆棧,獲取的信息將會被存放在buffer中,它是一個指針列表。參數 size 用來指定buffer中可以保存多少個void* 元素。函數返回值是實際獲取的指針個數,最大不超過size大小。
在buffer中的指針實際是從堆棧中獲取的返回地址,每一個堆??蚣苡幸粋€返回地址。
注意:某些編譯器的優化選項對獲取正確的調用堆棧有干擾,另外內聯函數沒有堆??蚣?#xff1b;刪除框架指針也會導致無法正確解析堆棧內容。
?
?
1. 代碼:
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <execinfo.h> #include <signal.h>void dump(int signo) {void *buffer[30] = {0};size_t size;char **strings = NULL;size_t i = 0;size = backtrace(buffer, 30);fprintf(stdout, "Obtained %zd stack frames.nm\n", size);strings = backtrace_symbols(buffer, size);if (strings == NULL){perror("backtrace_symbols.");exit(EXIT_FAILURE);}for (i = 0; i < size; i++){fprintf(stdout, "%s\n", strings[i]);}free(strings);strings = NULL;exit(0); }void func_c() {*((volatile char *)0x0) = 0x9999; }void func_b() {func_c(); }void func_a() {func_b(); }int main(int argc, const char *argv[]) {if (signal(SIGSEGV, dump) == SIG_ERR)perror("can't catch SIGSEGV");func_a();return 0; }?
?
2. 編譯
gcc -g -rdynamic fun_trace.c -o test
?
3. 運行
[baoliw@AONTFN07 test]$ ./test Obtained 7 stack frames.nm ./test(dump+0x45) [0x80487a9] [0x6ff400] ./test(func_b+0x8) [0x804886c] ./test(func_a+0x8) [0x8048876] ./test(main+0x33) [0x80488ab] /lib/libc.so.6(__libc_start_main+0xe6) [0x126cc6] ./test() [0x80486d1]?
總結
- 上一篇: 桌面文件在c盘哪个文件夹(桌面在C盘里的
- 下一篇: local_irq_save和 loca