当我的c ++ Linux应用程序崩溃时,我需要转储堆栈跟踪。我使用backtrace()
和backtrace_symbols()
成功完成了此操作。现在,另外我想得到崩溃的行号。它是怎么做到的?
答案 0 :(得分:16)
我从
获得了帮助http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l2.html 并http://www.linuxjournal.com/article/6391?page=0,0提出示例代码,说明如何实现这一目标。
基本上它是关于将堆栈回溯放在信号处理程序中,让后者捕获程序可以接收的所有“坏”信号(SIGSEGV,SIGBUS,SIGILL,SIGFPE等)。这样,如果您的程序崩溃并且您没有使用调试器运行它,您可以获得堆栈跟踪并知道故障发生的位置。此技术还可用于了解程序循环的位置,以防它停止响应...
下面的代码为跟踪中的每个地址运行外部程序addr2line,将其转换为文件名和行号。
下面的源代码打印所有本地功能的行号。如果调用另一个库中的函数,您可能会看到几个??:0而不是文件名。
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
void bt_sighandler(int sig, struct sigcontext ctx) {
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
if (sig == SIGSEGV)
printf("Got signal %d, faulty address is %p, "
"from %p\n", sig, ctx.cr2, ctx.eip);
else
printf("Got signal %d\n", sig);
trace_size = backtrace(trace, 16);
/* overwrite sigaction with caller's address */
trace[1] = (void *)ctx.eip;
messages = backtrace_symbols(trace, trace_size);
/* skip first stack frame (points here) */
printf("[bt] Execution path:\n");
for (i=1; i<trace_size; ++i)
{
printf("[bt] #%d %s\n", i, messages[i]);
char syscom[256];
sprintf(syscom,"addr2line %p -e sighandler", trace[i]); //last parameter is the name of this app
system(syscom);
}
exit(0);
}
int func_a(int a, char b) {
char *p = (char *)0xdeadbeef;
a = a + b;
*p = 10; /* CRASH here!! */
return 2*a;
}
int func_b() {
int res, a = 5;
res = 5 + func_a(a, 't');
return res;
}
int main() {
/* Install our signal handler */
struct sigaction sa;
sa.sa_handler = (void *)bt_sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
/* ... add any other signal here */
/* Do something */
printf("%d\n", func_b());
}
此代码应编译为:gcc sighandler.c -o sighandler -rdynamic
程序输出:
Got signal 11, faulty address is 0xdeadbeef, from 0x8048975
[bt] Execution path:
[bt] #1 ./sighandler(func_a+0x1d) [0x8048975]
/home/karl/workspace/stacktrace/sighandler.c:44
[bt] #2 ./sighandler(func_b+0x20) [0x804899f]
/home/karl/workspace/stacktrace/sighandler.c:54
[bt] #3 ./sighandler(main+0x6c) [0x8048a16]
/home/karl/workspace/stacktrace/sighandler.c:74
[bt] #4 /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0x3fdbd6]
??:0
[bt] #5 ./sighandler() [0x8048781]
??:0
答案 1 :(得分:4)
只有使用调试信息(即gcc -Wall -g
或g++ -Wall -g
)编译程序时才可能。如果没有-g
,则可执行文件不包含任何源行信息。如果使用gcc
,您可以使用优化和优化进行编译调试信息(例如g++ -Wall -g -O2
)但有时行位置会“令人惊讶”。
-Wall
标志要求GCC显示所有警告。它非常有用(因此我建议使用它)但与-g
或调试信息无关。
至于如何提取行号,最简单的方法是分叉gdb
进程。或者,您可以使用DWARF中的libdwarf
获取调试信息(格式为ELF tool chain)并对其进行解析。我不确定这是值得的......
要获得回溯,您可能只需将gdb
作为gdb --args yourprogram itsarguments
运行您的程序......
您还可以使用最近GCC内部的libbacktrace(实际上是Ian Taylor's libbacktrace),它旨在解决您的问题(它正在“解释”当前可执行文件的DWARF格式,您将使用g++ -O -g
编译。
答案 2 :(得分:1)
答案 3 :(得分:0)
system()
需要#include <stdlib.h>
似乎没有其他东西。
$ g++-8 -g -o dump dump.cpp
dump.cpp: In function ‘void bt_sighandler(int, sigcontext)’:
dump.cpp:15:43: error: ‘struct sigcontext’ has no member named
‘eip’; did > you mean ‘rip’?
"from %p\n", sig, ctx.cr2, ctx.eip);
^~~
rip
dump.cpp:21:26: error: ‘struct sigcontext’ has no member
named ‘eip’;
did you mean ‘rip’?
trace[1] = (void *)ctx.eip;
^~~
rip
dump.cpp: In function ‘int main()’:
dump.cpp:64:19: error: invalid conversion from ‘void*’ to
‘__sighandler_t’ > {aka ‘void (*)(int)’} [-fpermissive]
sa.sa_handler = (void *)bt_sighandler;
^~~~~~~~~~~~~~~~~~~~~
答案 4 :(得分:0)
好的,所以当我绊倒这个漂亮的笔记时,我面前有一个Xavier(NVIDIA Jetpack 4.4 Ubuntu 18.04.5)打开。我很沮丧地发现该代码无法在Xavier上运行,Xavier基本上是aarch64架构,其中一些信号结构是不同的。
因此,我设法整理了一个在X86_64和aarch64上都可以使用的反例。
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
#include <stdlib.h>
#include <ucontext.h>
#include <string.h>
#ifdef __aarch64__
#define _PC pc
#define _SP sp
#elif defined(__x86_64__)
#define _PC gregs[REG_RSP]
#define _SP gregs[REG_RIP]
#else
#error architecture not supported
#endif
static void addr2line(void *traceP, void *messageP) {
char syscom[256];
#ifdef __aarch64__
char message[4096];
strcpy(message, (char *) messageP);
char *saveP = NULL, *token = strtok_r(message, "(", &saveP);
if(token) {
token = strtok_r(NULL, "+", &saveP);
if(token) {
char *term = strchr(token,')');
if(term)
*term = 0;
}
}
if(token==NULL) {
token = message;
}
sprintf(syscom,"addr2line %s -e sighandler", token);
#elif defined(__x86_64__)
sprintf(syscom,"addr2line %p -e sighandler", traceP); //last parameter is the name of this app
#endif
system(syscom);
}
void bt_sighandler(int sig, siginfo_t *psi, void *ctxarg) {
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
mcontext_t *ctxP = &((ucontext_t *) ctxarg)->uc_mcontext;
if (sig == SIGSEGV)
printf("Got signal %d, faulty address is %p, "
"from %p\n", sig, (void *) ctxP->_PC, (void *) ctxP->_SP);
else
printf("Got signal %d\n", sig);
trace_size = backtrace(trace, 16);
/* overwrite sigaction with caller's address */
trace[1] = (void *)ctxP->_SP;
messages = backtrace_symbols(trace, trace_size);
/* skip first stack frame (points here) */
printf("[bt] Execution path:\n");
for (i=1; i<trace_size; ++i)
{
printf("[bt] #%d %s\n", i, messages[i]);
addr2line(trace[i], messages[i]);
}
exit(0);
}
int func_a(int a, char b) {
char *p = (char *)0xdeadbeef;
a = a + b;
*p = 10; /* CRASH here!! */
return 2*a;
}
int func_b() {
int res, a = 5;
res = 5 + func_a(a, 't');
return res;
}
int main() {
/* Install our signal handler */
struct sigaction sa;
sa.sa_sigaction = bt_sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART|SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
/* ... add any other signal here */
/* Do something */
printf("%d\n", func_b());
}
基本上:
代码现在在x86_64上运行,并提供相似的可用结果。
xavier $ ./sighandler 3
Got signal 11, faulty address is 0x5563baadf4, from 0x7fd4439350
[bt] Execution path:
[bt] #1 [0x7fd4439350]
??:0
[bt] #2 ./sighandler(+0xdf4) [0x5563baadf4]
/home/jsaari/project/radar_ars/alfalfa/cuda/debug/sighandler.cpp:79
[bt] #3 ./sighandler(+0xe24) [0x5563baae24]
/home/jsaari/project/radar_ars/alfalfa/cuda/debug/sighandler.cpp:89
[bt] #4 ./sighandler(+0xea4) [0x5563baaea4]
/home/jsaari/project/radar_ars/alfalfa/cuda/debug/sighandler.cpp:109
[bt] #5 /lib/aarch64-linux-gnu/libc.so.6(__libc_start_main+0xe0) [0x7f9ae316e0]
??:0
[bt] #6 ./sighandler(+0xa94) [0x5563baaa94]
:?
x86_64 $ ./sighandler 3
Got signal 11, faulty address is 0x7ffe291d27d0, from 0x40095a
[bt] Execution path:
[bt] #1 ./sighandler() [0x40095a]
/home/jsaari/common/experiment/backtrace/sighandler.cpp:79
[bt] #2 ./sighandler() [0x40095a]
/home/jsaari/common/experiment/backtrace/sighandler.cpp:79
[bt] #3 ./sighandler() [0x400982]
/home/jsaari/common/experiment/backtrace/sighandler.cpp:89
[bt] #4 ./sighandler() [0x4009f4]
/home/jsaari/common/experiment/backtrace/sighandler.cpp:109
[bt] #5 /lib64/libc.so.6(__libc_start_main+0xf5) [0x7f294a36d495]
??:0
[bt] #6 ./sighandler() [0x4006d9]
??:?
恕我直言-既然NVIDIA已经收购了ARM,ARM将接管物联网(如果尚未收购),而英特尔很可能开始陷入困境。
很明显,这里显示的是地址混乱:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=860394
显然,这是由gcc仅构建与位置无关的代码引起的。如果您使用“ -no-pie”构建应用程序,则trace [i]条目可用于addr2line。
除了我编码的过程之外,我还没有找到如何管理它的方法。
来自此帖子:
How to find load relocation for a PIE binary?
重定位值可以从
获得#include <link.h>
. . .
uintptr_t relocation = _r_dump.r_map->l_addr;
可以从trace [i]中减去重定位值,以获得addr2line可以利用的地址(在我使用的xavier aarch64框上的结果)。
对于非重定位二进制文件,重定位的值为“ 0”(在我使用的x86_64框上的结果)。