打印出程序运行时的堆栈情况:
#include
#include
#include
/* Obtain a backtrace and print it to stdout.
*/void print_trace(void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace(array, 10);
strings = backtrace_symbols(array, size);
printf("Obtained %zd
stack frames.\n", size);
for (i = 0; i < size; i++)
printf("%s\n", strings[i]);
free(strings);
}
/* A dummy function to make the backtrace more interesting. */
void dummy_function(void)
{
print_trace();
}
int main(void)
{
dummy_function();
return 0;
}
gcc -rdynamic -o t t.c
注:-rdynamic (其实时传给ld的参数),否则可能不会打印出函数名称,而代之以偏移量。
没有评论:
发表评论