编程基础-----c语言打印调用栈
来源:互联网 发布:守望先锋网络延迟查询 编辑:程序博客网 时间:2024/05/17 22:37
官方介绍:
SYNOPSIS top
#include <execinfo.h> int backtrace(void **buffer, int size); char **backtrace_symbols(void *const *buffer, int size); void backtrace_symbols_fd(void *const *buffer, int size, int fd);
DESCRIPTION top
backtrace() returns a backtrace for the calling program, in the array pointed to by buffer. A backtrace is the series of currently active function calls for the program. Each item in the array pointed to by buffer is of type void *, and is the return address from the corresponding stack frame. The size argument specifies the maximum number of addresses that can be stored in buffer. If the backtrace is larger than size, then the addresses corresponding to the size most recent function calls are returned; to obtain the complete backtrace, make sure that buffer and size are large enough. Given the set of addresses returned by backtrace() in buffer, backtrace_symbols() translates the addresses into an array of strings that describe the addresses symbolically. The size argument specifies the number of addresses in buffer. The symbolic representation of each address consists of the function name (if this can be determined), a hexadecimal offset into the function, and the actual return address (in hexadecimal). The address of the array of string pointers is returned as the function result of backtrace_symbols(). This array is malloc(3)ed by backtrace_symbols(), and must be freed by the caller. (The strings pointed to by the array of pointers need not and should not be freed.) backtrace_symbols_fd() takes the same buffer and size arguments as backtrace_symbols(), but instead of returning an array of strings to the caller, it writes the strings, one per line, to the file descriptor fd. backtrace_symbols_fd() does not call malloc(3), and so can be employed in situations where the latter function might fail.
RETURN VALUE top
backtrace() returns the number of addresses returned in buffer, which is not greater than size. If the return value is less than size, then the full backtrace was stored; if it is equal to size, then it may have been truncated, in which case the addresses of the oldest stack frames are not returned. On success, backtrace_symbols() returns a pointer to the array malloc(3)ed by the call; on error, NULL is returned.
VERSIONS top
backtrace(), backtrace_symbols(), and backtrace_symbols_fd() are provided in glibc since version 2.1.
CONFORMING TO top
These functions are GNU extensions.
NOTES top
These functions make some assumptions about how a function's return address is stored on the stack. Note the following: * Omission of the frame pointers (as implied by any of gcc(1)'s nonzero optimization levels) may cause these assumptions to be violated. * Inlined functions do not have stack frames. * Tail-call optimization causes one stack frame to replace another. The symbol names may be unavailable without the use of special linker options. For systems using the GNU linker, it is necessary to use the -rdynamic linker option. Note that names of "static" functions are not exposed, and won't be available in the backtrace.
EXAMPLE top
The program below demonstrates the use of backtrace() and backtrace_symbols(). The following shell session shows what we might see when running the program: $ cc -rdynamic prog.c -o prog $ ./prog 3 backtrace() returned 8 addresses ./prog(myfunc3+0x5c) [0x80487f0] ./prog [0x8048871] ./prog(myfunc+0x21) [0x8048894] ./prog(myfunc+0x1a) [0x804888d] ./prog(myfunc+0x1a) [0x804888d] ./prog(main+0x65) [0x80488fb] /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c] ./prog [0x8048711]
Program source
#include <execinfo.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void myfunc3(void) { int j, nptrs; #define SIZE 100 void *buffer[100]; char **strings; nptrs = backtrace(buffer, SIZE); printf("backtrace() returned %d addresses\n", nptrs); /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) would produce similar output to the following: */ strings = backtrace_symbols(buffer, nptrs); if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } for (j = 0; j < nptrs; j++) printf("%s\n", strings[j]); free(strings); } static void /* "static" means don't export the symbol... */ myfunc2(void) { myfunc3(); } void myfunc(int ncalls) { if (ncalls > 1) myfunc(ncalls - 1); else myfunc2(); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "%s num-calls\n", argv[0]); exit(EXIT_FAILURE); } myfunc(atoi(argv[1])); exit(EXIT_SUCCESS); }
自己示例:
#include <stdio.h>#include <execinfo.h>#include <stdlib.h>void fun1();void fun2();void fun3();void print_stack();int main(){fun3();return 0;}void fun1(){printf("print_stack begin\n");print_stack();}void fun2(){fun1();}void fun3(){fun2();}void print_stack(){int size = 16;int i;void *array[16];int stack_num = backtrace(array, size);char **stacktrace = NULL; stacktrace = (char**)backtrace_symbols(array, stack_num);for(i = 0; i < stack_num; i++){printf("%s\n", stacktrace[i]);}free(stacktrace);}
编译:
- 编程基础-----c语言打印调用栈
- 编程基础-----c语言打印调用栈
- c语言打印调用栈
- C语言下打印调用栈
- C语言打印树基础
- C语言基础-函数调用栈
- C语言编程基础
- c语言基础编程
- c语言基础--编程基础
- 各语言打印调用栈
- C语言 基础之printf打印输出
- C语言基础 打印九成九乘法表
- C语言基础---打印99乘法表
- C语言编程基础题库
- C语言网络编程基础
- C语言编程基础--(1)
- C语言编程基础考试
- C语言编程基础7.8
- 开源 免费 java CMS - FreeCMS1.5 标签 infoSearch
- python自然语言处理学习笔记第二章第二部分
- 关于hql中部分关联字段为空时,不报错而直接查不出字段为空的记录
- 【Kickstart+Dhcp+Nfs+Tftp+Pxe 最实用的批量linux服务器安装】
- 数据对齐
- 编程基础-----c语言打印调用栈
- 成为IT经理必备的十大软技能
- iOS应用程序的窗口元素及其运行原理
- 分享2014年热门网络新词整理集锦(扫盲)
- Windows下命令行下启动ORACLE服务
- 肥胖与高血压有什么关系?
- C++_友元函数
- 【Java TCP/IP Socket】TCP Socket通信中由read返回值造成的的死锁问题(含代码)
- 使用Nexus搭建Maven私服