【问题标题】:backtrace results did not reveal crash location回溯结果未显示崩溃位置
【发布时间】:2013-02-22 06:16:57
【问题描述】:

我正在尝试使用 backtrace 和 backtrace_symbols 结果来打印在捕获 SIGSEGV 信号后我的代码崩溃的位置。我明确地从一个工作线程触发了它。我用选项 -rdynamic 编译了我的静态链接多线程 C++ 程序,我期待 backtrace_symbols 会返回一个堆栈跟踪来告诉我崩溃发生在哪里,但下面的结果不是我所期望的,MyApplicationService 不是一种方法,但只是可执行文件名,我不明白为什么。

    2013-03-07 15:13:18.119986598 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZNK5boost4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS_6system10error_codeEiEclEPS3_S7_i+0x74) [0xcd69fa]
    2013-03-07 15:13:18.119988126 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost3_bi5list3INS0_5valueIPN18my_app__service_lib14service_runnerEEENS_3argILi1EEENS7_ILi2EEEEclINS_4_mfi3mf2IvS4_RKNS_6system10error_codeEiEENS0_5list2ISH_RKiEEEEvNS0_4typeIvEERT_RT0_i+0x7c) [0xcd6848]
    2013-03-07 15:13:18.119989192 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost3_bi6bind_tIvNS_4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS_6system10error_codeEiEENS0_5list3INS0_5valueIPS5_EENS_3argILi1EEENSF_ILi2EEEEEEclIS7_iEEvRKT_RKT0_+0x4c) [0xcd6690]
    2013-03-07 15:13:18.119990120 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio6detail7binder2INS_3_bi6bind_tIvNS_4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS_6system10error_codeEiEENS3_5list3INS3_5valueIPS8_EENS_3argILi1EEENSI_ILi2EEEEEEESA_iEclEv+0x2b) [0xcd6597]
    2013-03-07 15:13:18.119991031 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio19asio_handler_invokeINS0_6detail7binder2INS_3_bi6bind_tIvNS_4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS_6system10error_codeEiEENS4_5list3INS4_5valueIPS9_EENS_3argILi1EEENSJ_ILi2EEEEEEESB_iEEEEvT_z+0x62) [0xcd64b0]
    2013-03-07 15:13:18.119992105 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN33boost_asio_handler_invoke_helpers6invokeIN5boost4asio6detail7binder2INS1_3_bi6bind_tIvNS1_4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS1_6system10error_codeEiEENS5_5list3INS5_5valueIPSA_EENS1_3argILi1EEENSK_ILi2EEEEEEESC_iEESO_EEvRT_RT0_+0x64) [0xcd636c]
    2013-03-07 15:13:18.119993000 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio6detail14signal_handlerINS_3_bi6bind_tIvNS_4_mfi3mf2IvN18my_app__service_lib14service_runnerERKNS_6system10error_codeEiEENS3_5list3INS3_5valueIPS8_EENS_3argILi1EEENSI_ILi2EEEEEEEE11do_completeEPNS1_15task_io_serviceEPNS1_25task_io_service_operationESC_m+0xb9) [0xcd605d]
    2013-03-07 15:13:18.119993884 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio6detail25task_io_service_operation8completeERNS1_15task_io_serviceERKNS_6system10error_codeEm+0x32) [0xccceac]
    2013-03-07 15:13:18.119994819 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio6detail15task_io_service10do_run_oneERNS1_11scoped_lockINS1_11posix_mutexEEERNS2_11thread_infoERKNS_6system10error_codeE+0x1a1) [0xccf4a5]
    2013-03-07 15:13:18.119995795 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio6detail15task_io_service3runERNS_6system10error_codeE+0x137) [0xcceffd]
    2013-03-07 15:13:18.119996845 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN5boost4asio10io_service3runEv+0x2f) [0xccf823]
    2013-03-07 15:13:18.119997845 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(_ZN18my_app__service_lib14service_runner3runEv+0x97) [0xccbb65]
    2013-03-07 15:13:18.119998914 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService(main+0x4cc) [0xcc4608]
    2013-03-07 15:13:18.119999767 [7f2279bbe720] INFO  service_runner - /lib64/libc.so.6(__libc_start_main+0xfd) [0x3f0f01ecdd]
    2013-03-07 15:13:18.120000694 [7f2279bbe720] INFO  service_runner - ../Debug/MyApplicationService() [0xcc4059]

这是我用来打印上述结果的代码:

          void *trace[16];
          char **messages = (char **)NULL;
          int i, trace_size = 0;
          SERVICE_LOG_INFO << "Receive signal " << sig ;    
          trace_size = backtrace(trace, 16);
          messages = backtrace_symbols(trace, trace_size);
          LOG << "[bt] Execution path:" ;
          for (i=1; i<trace_size; ++i)
              LOG << messages[i];

附加信息:

    OS: RHEL 6.3
    compiler: gcc 4.4.6
    compile option: -I/opt/rh/devtoolset-1.0/root/usr/include -I/opt/rh/devtoolset-1.0/root/usr/include/c++/4.7.0 -I/opt/rh/devtoolset-1.0/root/usr/include/c++/4.7.0/backward -I/opt/rh/devtoolset-1.0/root/usr/include/c++/4.7.0/x86_64-redhat-linux -I/opt/rh/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/include -I/opt/boost_1_52_0 -I/opt/tbb41_20121003oss/include -I/opt/libev-4.11/include -I"Myapp/include" -O0 -g3 -rdynamic -Wall -c -fmessage-length=0 -std=c++11 -fno-strict-aliasing -MMD -MP -MF"src/service_runner.d" -MT"src/service_runner.d" -o "src/service_runner.o" "../src/service_runner.cpp"

【问题讨论】:

  • 您是否使用调试信息构建?为什么不尝试在调试器中运行它呢?
  • 是的,使用 -g3。我正在使用 Eclipse CDT
  • 我们需要更多信息,例如编译和链接应用程序时命令行的外观如何?您使用的是哪个编译器和哪个版本等。
  • 使用 valgrind - 修复段错误和内存泄漏的最佳工具
  • @BryanFok:不知道这些乱七八糟的 C++ 名称是否让您感到困惑,如果有,请阅读this

标签: c++ linux segmentation-fault


【解决方案1】:

终于把一切都搞定了。

解决步骤: 1) 在 linker 选项中添加 -rdynamic。

2) 使用此代码对损坏的字符串进行解码:(代码来自此thread

            // skip first stack frame (points here)
            for (int i = 0; i < trace_size && messages != NULL; ++i)
            {
                char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;

                // find parantheses and +address offset surrounding mangled name
                for (char *p = messages[i]; *p; ++p)
                {
                    if (*p == '(')
                    {
                        mangled_name = p;
                    }
                    else if (*p == '+')
                    {
                        offset_begin = p;
                    }
                    else if (*p == ')')
                    {
                        offset_end = p;
                        break;
                    }
                }

                // if the line could be processed, attempt to demangle the symbol
                if (mangled_name && offset_begin && offset_end &&
                    mangled_name < offset_begin)
                {
                    *mangled_name++ = '\0';
                    *offset_begin++ = '\0';
                    *offset_end++ = '\0';

                    int status;
                    char * real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);

                    // if demangling is successful, output the demangled function name
                    if (status == 0)
                    {
                        std::cerr << "[bt]: (" << i << ") " << messages[i] << " : "
                                  << real_name << "+" << offset_begin << offset_end
                                  << std::endl;

                    }
                    // otherwise, output the mangled function name
                    else
                    {
                        std::cerr << "[bt]: (" << i << ") " << messages[i] << " : "
                                  << mangled_name << "+" << offset_begin << offset_end
                                  << std::endl;
                    }
                    free(real_name);
                }
                // otherwise, print the whole line
                else
                {
                    std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl;
                }
            }
            std::cerr << std::endl;

【讨论】:

    【解决方案2】:

    编译选项显示-c,因此-rdynamic 被传递到编译步骤。您需要确保在链接代码时使用它。检查是否是这种情况。

    编辑: Bryan,您现在将损坏的符号添加到问题中,因此您现在想问一个不同的问题。请不要那样做。

    但是既然你这样做了:你需要从 backtrace_symbols 解析每一行以提取 mangled 符号并在其上调用 __cxa_demangle。策略是从字符串末尾搜索+,然后搜索左括号(,再次从+ 向后搜索。向后搜索的原因是,前面的文件名也可能包含这些字符(不太可能,但可能)。如果您尝试实施它并遇到问题,请打开一个新问题

    【讨论】:

    • 谢谢丹尼尔。情况有所好转。但是现在消息都堆积起来了..它仍然没有指向它崩溃的地方。 +1
    • 不客气。我假设这个问题会得到回答,如果你需要帮助,请打开一个只关注崩溃的新问题(当然,在你身边进行一些研究之后)。
    • 你有你的符号mangled(查找“C++ mangling”)。你需要demangle他们。您可以在程序中执行此操作(请参阅cxa_demangle())或使用外部工具(c++filt)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2020-01-31
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多