利用backtrace和backtrace_symbols函数打印调用栈信息
在头文件"execinfo.h"中声明了三个函数用于获取当前线程的函数调用堆栈。
#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);
man 帮助:
DESCRIPTION
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.
int backtrace(void **buffer,int size)
该函数用与获取当前线程的调用堆栈,获取的信息将会被存放在buffer中,它是一个指针数组。参数 size 用来指定buffer中可以保存多少个void* 元素。函数返回值是实际获取的指针个数,最大不超过size大小在buffer中的指针实际是从堆栈中获取的返回地址,每一个堆栈框架有一个返回地址。
注意某些编译器的优化选项对获取正确的调用堆栈有干扰,另外内联函数没有堆栈框架;删除框架指针也会使无法正确解析堆栈内容
char ** backtrace_symbols (void *const *buffer, int size)
backtrace_symbols将从backtrace函数获取的信息转化为一个字符串数组. 参数buffer应该是从backtrace函数获取的数组指针,size是该数组中的元素个数(backtrace的返回值),函数返回值是一个指向字符串数组的指针,它的大小同buffer相同.每个字符串包含了一个相对于buffer中对应元素的可打印信息.它包括函数名,函数的偏移地址,和实际的返回地址
现在,只有使用ELF二进制格式的程序和苦衷才能获取函数名称和偏移地址.在其他系统,只有16进制的返回地址能被获取.另外,你可能需要传递相应的标志给链接器,以能支持函数名功能(比如,在使用GNU ld的系统中,你需要传递(-rdynamic))
backtrace_symbols生成的字符串都是malloc出来的,但是不要最后一个一个的free,因为backtrace_symbols是根据backtrace给出的call stack层数,一次性的malloc出来一块内存来存放结果字符串的,所以,像上面代码一样,只需要在最后,free backtrace_symbols的返回指针就OK了。这一点backtrace的manual中也是特别提到的。
注意:如果不能为字符串获取足够的空间函数的返回值将会为NULL
void backtrace_symbols_fd (void *const *buffer, int size, int fd)
backtrace_symbols_fd与backtrace_symbols 函数具有相同的功能,不同的是它不会给调用者返回字符串数组,而是将结果写入文件描述符为fd的文件中,每个函数对应一行.它不需要调用malloc函数,因此适用于有可能调用该函数会失败的情况。
man手册中示例:
#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);
}
结果:
总结:使用以下几个函数既可完成堆栈信息的打印
int backtrace (void **buffer, int size)
char ** backtrace_symbols (void *const *buffer, int size)
char* abi::__cxa_demangle
(
const char * mangled_name,
char * output_buffer,
size_t * length,
int * status
)
1. backtrace可以在程序运行的任何地方被调用,返回各个调用函数的返回地址,可以限制最大调用栈返回层数。
2. 在backtrace拿到函数返回地址之后,backtrace_symbols可以将其转换为编译符号,这些符号是编译期间就确定的
3. 根据backtrace_symbols返回的编译符号,abi::__cxa_demangle可以找到具体地函数方法
利用backtrace和backtrace_symbols函数打印调用栈信息的更多相关文章
- 在C/C++程序里打印调用栈信息
我们知道,GDB的backtrace命令可以查看堆栈信息.但很多时候,GDB根本用不上.比如说,在线上环境中可能没有GDB,即使有,也不太可能让我们直接在上面调试.如果能让程序自己输出调用栈,那是最好 ...
- 在c或c+程序里打印调用栈。转
在C/C++程序里打印调用栈信息 我们知道,GDB的backtrace命令可以查看堆栈信息.但很多时候,GDB根本用不上.比如说,在线上环境中可能没有GDB,即使有,也不太可能让我们直接在上面调试.如 ...
- android打印调用栈
在某些机器上,不能下断点,出现了某个诡异的问题,想到唯一的解决方式,就是打印调用栈了,google发现这个,记录下,以后备用 Log.d(",Log.getStackTraceString( ...
- kernel中,dump_stack打印调用栈,print_hex_dump打印一片内存,记录一下
kernel中,dump_stack打印调用栈,print_hex_dump打印一片内存,记录一下
- perf打印调用栈的过程
perf_prepare_sample-->perf_callchain-->get_perf_callchain 上面的调用栈会使用 perf_event_output--> 0x ...
- Android 中调试手段 打印函数调用栈信息
下面来简单介绍下 android 中的一种调试方法. 在 android 的 app 开发与调试中,经常需要用到打 Log 的方式来查看函数调用点. 这里介绍一种方法来打印当前栈中的函数调用关系 St ...
- PHP debug_backtrace() 函数打印调用处的调试信息
http://php.net/manual/zh/function.debug-backtrace.php debug_backtrace (PHP 4 >= 4.3.0, PHP 5, PHP ...
- python 打印调用栈
import traceback def BBQ(): traceback.print_stack() 引入 traceback 包,在某个函数中执行 traceback.print_stack().
- java 中打印调用栈
source-code: public class A { public A() {} private static void printStackTrace() { StackTra ...
随机推荐
- org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException
使用Intelij Idea时,报错如下: org.apache.catalina.core.StandardContext.listenerStart Error configuring appli ...
- ubuntu下安装.deb包的安装方法
ubuntu16.04的软件中心应该是有bug,安装不了第三方.deb文件,我们只有使用dpkg -i 或者gdebi的方式安装,我使用的是后者,因为后者功能更加强大.要使用gdebi命令先要安装它: ...
- Python print() 函数
Python print() 函数 Python 内置函数 描述 print() 方法用于打印输出,最常见的一个函数. print 在 Python3.x 是一个函数,但在 Python2.x 版本 ...
- html节点操作与事件
<div id='a' style="width: 500px;height: 500px;background-color: grey;"> </div> ...
- [leetcode]113. Path Sum II路径和(返回路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 使用springmvc从页面中获取数据,然后根据获得的参数信息进行修改,如果修改的数据中含有不是基本数据类型的参数。比如传的参数中有Date类型的数据时,需要我们进行参数类型转换。
1.1 需求 在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式. 1.2 需求分析 由于日期数据有很多格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑 ...
- UI设计,你为什么不能把标题做的更明显呢?
在设计中标题常常被重视,标题即是文案信息的精华提炼,那么如何能把标题在很多文案信息中脱颖而出就是设计师所要做的工作,前面的文章说过对比可以凸显主题,这期是在对比合理的前提下更进一步的处理方法,我们可以 ...
- gruop by报错this is incompatible with sql_mode=only_full_group_by
set @@GLOBAL.sql_mode=''; set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_ ...
- Laravel 处理 Options 请求的原理以及批处理方案
0. 背景 在前后端分离的应用中,需要使用CORS完成跨域访问.在CORS中发送非简单请求时,前端会发一个请求方式为OPTIONS的预请求,前端只有收到服务器对这个OPTIONS请求的正确响应,才会发 ...
- centos7 编译安装mysql
centos 7 安装mySql 1,准备mySql源码安装 #wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar ...