【问题标题】:using function system() in c/c++ to invoke gdb piped to where, to record stack trace - error Hangup detected on fd 0使用 c/c++ 中的函数 system() 调用通过管道传输到 where 的 gdb,以记录堆栈跟踪 - 在 fd 0 上检测到错误挂起
【发布时间】:2013-09-28 03:04:33
【问题描述】:

在 Linux 上工作我正在尝试构建一个崩溃处理程序函数 - 因此在崩溃之前我们会在日志文件中获得堆栈跟踪 - 做了一些研究并知道,它要么使用 -g/-rdynamic Flags 生成调试信息,然后使用 glibc backtrace / backtrace_symbols_fd 函数来获取堆栈跟踪......

它在我的应用程序中没有多大用处,不允许使用 -g/-rdynamic 标志.. 所以请进一步阅读reading

我已经构建了以下代码,但它没有按预期工作,并且在生成的日志文件中它以以下错误结束:

(gdb) Hangup detected on fd 0
error detected on stdin
A debugging session is active.

Inferior 1 [process 6625] will be detached.
Quit anyway? (y or n) [answered Y; input not from terminal]
Detaching from program: /u/vivek/demo/testprg, process 6625

代码: 在启动时注册一个 Crash Handler 函数为 -

struct sigaction act;
memset(&act, 0, sizeof (act));
act.sa_sigaction = ProcessCrash;
sigfillset (&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction (SIGSEGV, &act, NULL);

当我的程序接收到 SIGSEGV 信号时调用以下函数 -

void ProcessCrash( int signal, siginfo_t * siginfo, void *context)
{
    cerr <<"** ProcessCrash –Handler Function**" << endl;
       ucontext_t *uc = (ucontext_t *)context;


       if (signal == SIGSEGV)
              fprintf(stderr, "\nGot signal %d, faulty address is %p, "
                     "from %p\n\n", signal, siginfo->si_addr, 
                            uc->uc_mcontext.gregs[REG_RIP]);  // REG_EIP is changed to REG_RIP for –m64 arch.
       else
              fprintf(stderr, "Got signal %d#92;\n", signal);

      // [[ using GDB to pring the stack trace

       char gdb[516];

    // command 1
    //sprintf(gdb, "echo 'where\ndetach' | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());

    // command 2
    sprintf(gdb, "(echo \"source Trace.txt\";cat) | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());

    fprintf(stderr, "\n** GDB Command-> %s \n", gdb);
    // output of above is 
    //  ** GDB Command-> (echo "source Trace.txt";cat) | gdb -q /u/vivek/demo/testprg -pid 6625 > gdbTest.dump

    // Run Either command 1 or command 2  but we get the same result
        system(gdb);

      // GDB test ]]


       // Produce a core dump 
       abort();

       return; 
}

Trace.txt 内容:

set logging on
where
detach
quit

请让我知道有没有办法摆脱它......因为我没有办法克服它......

【问题讨论】:

  • 你有没有考虑过至少没有-ggdb 无论如何都不会给你有用的名字?
  • system 函数在信号处理程序中调用是安全的。参见例如signal(7) 手册页。
  • 是的,我已经在没有 -g 选项的情况下测试了 gdb,并且在接收核心时,它正确地指示了具有确切行号的功能和文件...
  • @JoachimPileborg 实际上意识到了这一点,而是出于好奇而这样做.. 没有 -g 和 backtrace .. 有没有其他方法可以做到这一点......通过帖子.. @987654323 @donno 如果它在没有 -g 的情况下也可以工作
  • @JoachimPileborg:担心这个有点晚了,程序已经崩溃了。

标签: c++ c linux gdb


【解决方案1】:

不确定您打算如何处理 (echo \"source Trace.txt\";cat) | 部分。 据我所知,只需使用gdb -batch -x Trace.txt -q %.256s -pid %d &gt; gdbTest.dump 就可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2012-04-30
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2018-07-13
    相关资源
    最近更新 更多