调试__do_global_dtors_aux以查找代码位置

时间:2013-10-10 10:50:34

标签: c++ gdb valgrind fastcgi

我想找个bug。使用gdb我可以看到:

Program received signal SIGSEGV, Segmentation fault.
0x00007f4325cc286f in std::ostream::flush() () from /usr/lib/libstdc++.so.6
(gdb) bt
#0  0x00007f4325cc286f in std::ostream::flush() () from /usr/lib/libstdc++.so.6
#1  0x00007f4325c8d31c in std::ios_base::Init::~Init() () from /usr/lib/libstdc++.so.6
#2  0x00007f4325462995 in __cxa_finalize (d=0x7f432451c458) at cxa_finalize.c:56
#3  0x00007f432430ee86 in __do_global_dtors_aux () from /usr/lib/libsocketxx.so.1.2
#4  0x0000000000000000 in ?? ()

但是我不知道在代码中构造/破坏全局变量会导致问题。此外,gdb似乎无法使用调试库并显示行号:

$ gdb
GNU gdb (GDB) 7.3-debian
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/usr/lib/debug".
(gdb) attach 10681
[...]
Reading symbols from /usr/lib/libsocketxx.so.1.2...Reading symbols from /usr/lib/debug/usr/lib/libsocketxx.so.1.2.0...done.
done.
Loaded symbols for /usr/lib/libsocketxx.so.1.2

我似乎无法使用valgrind获取更多信息:

$ valgrind --tool=exp-sgcheck  --trace-children=yes spawn-fcgi -f /usr/lib/server/server.fcgi -p 9000  -n 
==14248== exp-sgcheck, a stack and global array overrun detector
==14248== NOTE: This is an Experimental-Class Valgrind Tool
==14248== Copyright (C) 2003-2012, and GNU GPL'd, by OpenWorks Ltd et al.
==14248== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==14248== Command: spawn-fcgi -f /usr/lib/server/server.fcgi -p 9000 -n
==14248== 
==14248== exp-sgcheck, a stack and global array overrun detector
==14248== NOTE: This is an Experimental-Class Valgrind Tool
==14248== Copyright (C) 2003-2012, and GNU GPL'd, by OpenWorks Ltd et al.
==14248== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==14248== Command: /bin/sh -c exec\ /usr/lib/server/server.fcgi
==14248== 
==14248== exp-sgcheck, a stack and global array overrun detector
==14248== NOTE: This is an Experimental-Class Valgrind Tool
==14248== Copyright (C) 2003-2012, and GNU GPL'd, by OpenWorks Ltd et al.
==14248== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==14248== Command: /usr/lib/server/server.fcgi
==14248== 
==14248== 
==14248== Process terminating with default action of signal 11 (SIGSEGV)
==14248==  Bad permissions for mapped region at address 0x7685B0E78C376F24
==14248==    at 0x7685B0E78C376F24: ???
==14248==    by 0x65060EC: std::ostream::flush() (ostream.tcc:219)
==14248==    by 0x64C72FE: std::ios_base::Init::~Init() (ios_init.cc:131)
==14248==    by 0x6C5F994: __cxa_finalize (cxa_finalize.c:56)
==14248==    by 0x7EA3E85: ??? (in /usr/lib/libsocketxx.so.1.2.0)
==14248==    by 0x7EAC760: ??? (in /usr/lib/libsocketxx.so.1.2.0)
==14248==    by 0x6C5F611: __run_exit_handlers (exit.c:78)
==14248==    by 0x6C5F664: exit (exit.c:100)
==14248==    by 0x6C47C93: (below main) (libc-start.c:260)
==14248== 
==14248== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 95 from 91)

你会如何追踪这种错误?

编辑:

我已经能够使用调试符号包,所以现在完全回溯:

Program received signal SIGSEGV, Segmentation fault.
Reading in symbols for ../../../../../src/libstdc++-v3/src/ostream-inst.cc...done.
0x00007f7711d98dc7 in std::basic_streambuf<char, std::char_traits<char> >::pubsync (this=0x7fff6f2e8250)
    at /build/buildd-gcc-4.4_4.4.5-8-amd64-za8kXx/gcc-4.4-4.4.5/build/x86_64-linux-gnu/libstdc++-v3/include/streambuf:248
248       pubsync() { return this->sync(); }
Current language:  auto
The current source language is "auto; currently c++".
(gdb) bt full
Reading in symbols for ../../../../../src/libstdc++-v3/src/ios_init.cc...done.
Reading in symbols for cxa_finalize.c...done.
#0  0x00007f7711d98dc7 in std::basic_streambuf<char, std::char_traits<char> >::pubsync (this=0x7fff6f2e8250)
    at /build/buildd-gcc-4.4_4.4.5-8-amd64-za8kXx/gcc-4.4-4.4.5/build/x86_64-linux-gnu/libstdc++-v3/include/streambuf:248
No locals.
#1  0x00007f7711d8e0ed in std::ostream::flush (this=0x6247e0 <_ZSt4cout@@GLIBCXX_3.4>)
    at /build/buildd-gcc-4.4_4.4.5-8-amd64-za8kXx/gcc-4.4-4.4.5/build/x86_64-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:219
        __err = std::_S_goodbit
#2  0x00007f7711d4f2ff in std::ios_base::Init::~Init (this=0x7f77105b16d0 <_ZStL8__ioinit>, __in_chrg=<optimized out>) at ../../../../../src/libstdc++-v3/src/ios_init.cc:131
No locals.
#3  0x00007f7711518995 in __cxa_finalize (d=0x7f77105b1620) at cxa_finalize.c:56
        check = <optimized out>
        cxafn = 0x6247e0 <_ZSt4cout@@GLIBCXX_3.4>
        cxaarg = 0x7fff6f2e8250
        f = 0x7f771183f350 <initial+80>
            funcs = 0x7f771183f300 <initial>
#4  0x00007f77103a2316 in __do_global_dtors_aux () from /tmp/inst/usr/lib/libsocketxx.so.1.2
No symbol table info available.
#5  0x0000000000000000 in ?? ()
No symbol table info available.

1 个答案:

答案 0 :(得分:1)

我没有找到调试此案例的方法,但是我找到了实际问题。使用fcgi时,你不应该做这样的事情:

int main(void) {
    // Backup the stdio streambufs
    streambuf * cin_streambuf  = cin.rdbuf();
    streambuf * cout_streambuf = cout.rdbuf();
    streambuf * cerr_streambuf = cerr.rdbuf();

    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);

    while (FCGX_Accept_r(&request) == 0) {
        fcgi_streambuf cin_fcgi_streambuf(request.in);
        fcgi_streambuf cout_fcgi_streambuf(request.out);
        fcgi_streambuf cerr_fcgi_streambuf(request.err);

        cin.rdbuf(&cin_fcgi_streambuf);
        cout.rdbuf(&cout_fcgi_streambuf);
        cerr.rdbuf(&cerr_fcgi_streambuf);

        // [YOUR CODE]
        if( condition) return 1; // NEVER DO THIS
    }

    // restore stdio streambufs
    cin.rdbuf(cin_streambuf);
    cout.rdbuf(cout_streambuf);
    cerr.rdbuf(cerr_streambuf);

    return 0;
}