共享库中的Segfault。怎么调试?

时间:2012-05-07 08:29:33

标签: c debugging gdb segmentation-fault shared-libraries

我在程序中使用了库(libnids)。我从库中调用函数nids_run,它继续运行,直到我显式调用nids_exit。

我的程序处理SIGINT并调用nids_exit。中断处理程序正常返回,但有时在库将控件返回给我的程序之前,我会收到段错误。这是GDB给我的回溯:

#0  0x00007ffff6498b2a in ?? () from /usr/lib/libpcap.so.1
#1  0x00007ffff649bee1 in pcap_loop () from /usr/lib/libpcap.so.1
#2  0x00007ffff77bae66 in nids_run () from /usr/lib/libnids.so.1.24
#3  0x0000000000401e92 in main (argc=3, argv=0x7fffffffebf8) at eve.c:139

找到问题的最佳策略是什么?我应该以某种方式调试libpcap吗?

更新 正如ArjunShankar所说,我在Valgrind下运行我的程序。这是输出的一部分:

==7504== Invalid read of size 4
==7504==    at 0x654EDC1: ??? (in /usr/lib/libpcap.so.1.2.1)
==7504==    by 0x6551EE0: pcap_loop (in /usr/lib/libpcap.so.1.2.1)
==7504==    by 0x5250E65: nids_run (in /usr/lib/libnids.so.1.24)
==7504==    by 0x401E91: main (eve.c:139)
==7504==  Address 0x70eece8 is 40 bytes inside a block of size 768 free'd
==7504==    at 0x4C29A9E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7504==    by 0x5250DEB: nids_exit (in /usr/lib/libnids.so.1.24)
==7504==    by 0x4026D0: signal_handler (signalhandling.c:17)
==7504==    by 0x5B6313F: ??? (in /lib/libpthread-2.15.so)
==7504==    by 0x5B5FC60: pthread_cond_timedwait@@GLIBC_2.3.2 (in /lib/libpthread-2.15.so)
==7504==    by 0x58E37D4: g_cond_wait_until (in /usr/lib/libglib-2.0.so.0.3200.1)
==7504==    by 0x587E2C0: ??? (in /usr/lib/libglib-2.0.so.0.3200.1)
==7504==    by 0x587E909: g_async_queue_timeout_pop (in /usr/lib/libglib-2.0.so.0.3200.1)
==7504==    by 0x4022D2: analyzer_thread_func (analyzers.c:93)
==7504==    by 0x58CA0C4: ??? (in /usr/lib/libglib-2.0.so.0.3200.1)
==7504==    by 0x5B5BE0D: start_thread (in /lib/libpthread-2.15.so)

更多输出可在以下位置找到:http://pastebin.com/93gkSScS

1 个答案:

答案 0 :(得分:3)

Valgrind输出显示libpcap(来自nids_run内部)试图在nids_exit free之后读取内存位置:

e.g:

==7504== Invalid read of size 4
==7504==    at 0x654EDC1: ??? (in /usr/lib/libpcap.so.1.2.1)
==7504==    by 0x6551EE0: pcap_loop (in /usr/lib/libpcap.so.1.2.1)
==7504==    by 0x5250E65: nids_run (in /usr/lib/libnids.so.1.24)

Address 0x70eece8 is 40 bytes inside a block of size 768 free'd
==7504==    at 0x4C29A9E: free
==7504==    by 0x5250DEB: nids_exit (in /usr/lib/libnids.so.1.24)

这是一个768大小的块,free中的nids_exit d,随后在nids_run内读取(显然还没有停止)。

所有其他错误都相似(nids_exit free是一个块,nids_run继续尝试使用它。

这意味着:您要么没有正确使用libnids(nids_run / nids_exit),要么libnids中存在错误。