Stacktrace文件和行位置

时间:2018-02-13 23:50:25

标签: c++ c backtrace

所以对此已经存在很多问题了,感谢像this one这样的线程我的文件和行号有用。

问题是我在某些情况下没有得到我期望的东西,并试图弄清楚为什么以及是否有办法解决它。

实施例

typedef struct _sig_ucontext {
    unsigned long uc_flags;
    struct ucontext *uc_link;
    stack_t uc_stack;
    struct sigcontext uc_mcontext;
    sigset_t uc_sigmask;
} sig_ucontext_t;

void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext)
{
    void * array[50];
    void * caller_address;
    char ** messages;
    int size, i;
    sig_ucontext_t * uc;

    uc = (sig_ucontext_t *) ucontext;

    /* Get the address at the time the signal was raised */
#if defined(__i386__) // gcc specific
    caller_address = (void *) uc->uc_mcontext.eip; // EIP: x86 specific
#elif defined(__x86_64__) // gcc specific
    caller_address = (void *) uc->uc_mcontext.rip; // RIP: x86_64 specific
#endif

    fprintf(stderr, "signal %d (%s), address is %p from %p\n", sig_num, strsignal(sig_num), info->si_addr, (void *) caller_address);

    size = backtrace(array, 50);

    /* overwrite sigaction with caller's address */
    array[1] = caller_address;

    messages = backtrace_symbols(array, size);

    /* skip first stack frame (points here) */
    for (i = 1; i < size && messages != NULL; ++i)
    {
        fprintf(stderr, "[bt]: (%d) %s\n", i, messages[i]);

        char syscom[256];
        sprintf(syscom, "addr2line %p -e GEBMerge", array[i]);
        system(syscom);
    }

    free(messages);

    exit(EXIT_FAILURE);
}

我准备信号处理程序然后我有功能。

int main(int argc, char **argv)
{
    //     std::cerr << "Entering main function of GEBMerge.cxx...\n";

    struct sigaction sigact;

    sigact.sa_sigaction = crit_err_hdlr;
    sigact.sa_flags = SA_RESTART | SA_SIGINFO;

    if (sigaction(SIGSEGV, &sigact, (struct sigaction *) NULL) != 0)
    {
        fprintf(stderr, "error setting signal handler for %d (%s)\n",
        SIGSEGV, strsignal(SIGSEGV));

        exit(EXIT_FAILURE);
    }

    int* crash = 0;
    *crash = 666;

    return 0;
}

这将产生

signal 11 (Segmentation fault), address is (nil) from 0x402423
[bt]: (1) ./GEBMerge(main+0x43) [0x402423]
/mnt/hgfs/Dropbox/ORNL/software/goddess_daq/source/GEBMerge.cxx:245
[bt]: (2) ./GEBMerge(main+0x43) [0x402423]
/mnt/hgfs/Dropbox/ORNL/software/goddess_daq/source/GEBMerge.cxx:245
[bt]: (3) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd58e7c1830]
??:0
[bt]: (4) ./GEBMerge(_start+0x29) [0x402519]
??:?

这似乎很好(尽管我不确定为什么第一行会出现两次)。

但是如果我在 main 中使用它而不是

int main(int argc, char **argv)
{
    //     std::cerr << "Entering main function of GEBMerge.cxx...\n";

    struct sigaction sigact;

    sigact.sa_sigaction = crit_err_hdlr;
    sigact.sa_flags = SA_RESTART | SA_SIGINFO;

    if (sigaction(SIGSEGV, &sigact, (struct sigaction *) NULL) != 0)
    {
        fprintf(stderr, "error setting signal handler for %d (%s)\n",
        SIGSEGV, strsignal(SIGSEGV));

        exit(EXIT_FAILURE);
    }

    char* crash = 0;
    sprintf(crash, "crash it damit!");

    return 0;
}

我得到以下

signal 11 (Segmentation fault), address is (nil) from 0x403503
[bt]: (1) ./GEBMerge(main+0x73) [0x403503]
/usr/include/x86_64-linux-gnu/bits/stdio2.h:34
[bt]: (2) ./GEBMerge(main+0x73) [0x403503]
/usr/include/x86_64-linux-gnu/bits/stdio2.h:34
[bt]: (3) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) 
[0x7f5f74ba9830]
??:0
[bt]: (4) ./GEBMerge(_start+0x29) [0x406169]
??:?

我理解为什么行/usr/include/x86_64-linux-gnu/bits/stdio2.h:34出现(链接到我假设的sprintf函数)但我希望在此堆栈跟踪的某处看到对调用 sprintf 的行的引用我的程序的主要

我想有些东西我不知道但却无法弄清楚是什么。

感谢您的帮助

0 个答案:

没有答案