Linux c開発-常用Linux下c言語デバッグ方法

7852 ワード

本文は主にLinuxの下でc言語のプロセスをデバッグする常用方法を総括します.
1. dmesg
dmesgは主にプロセスがクラッシュしたときにカーネルに関する情報を表示するために用いられる.
dmesg | tail -f   #      
dmesg -c #   ,       
dmesg -s 1024 #       
[root@localhost togo]# dmesg | tail -f
[   11.859146] sr 1:0:0:0: Attached scsi generic sg1 type 5
[   12.291406] ip_tables: (C) 2000-2006 Netfilter Core Team
[   12.380055] nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
[   12.417066] ip6_tables: (C) 2000-2006 Netfilter Core Team
[   12.493635] Ebtables v2.0 registered
[   12.525655] Bridge firewalling registered
[   12.907898] e1000: eno16777736 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   12.910863] IPv6: ADDRCONF(NETDEV_UP): eno16777736: link is not ready
[   12.911492] IPv6: ADDRCONF(NETDEV_CHANGE): eno16777736: link becomes ready
[   16.225872] Ebtables v2.0 unregistered

2.ldd lddコマンドは、主にプロセス依存の共有リンクライブラリを表示します.
#Togo       libevent  

[root@localhost togo]# ldd ./togo
	linux-vdso.so.1 =>  (0x00007ffff374b000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa06c8d6000)
	libevent-2.0.so.5 => /lib64/libevent-2.0.so.5 (0x00007fa06c68e000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fa06c2cc000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fa06cafc000)

3.coreファイル
プログラムが偶発的にクラッシュすると,システムのcore dumpを用いてクラッシュ時のエラー情報を表示することができる.チェックセグメントエラーなどの異常情報は非常に強力です.
core dumpをオンにするには、ulimitを設定する必要があります.
[root@localhost togo]# ulimit -c unlimited

一般的に、core dump後のファイルは現在のディレクトリにあり、統一されたcore dumpディレクトリである可能性があります.統合ディレクトリを表示するには、次の手順に従います.
[root@localhost home]# cat  /proc/sys/kernel/core_pattern
/tmp/core-%e-%s-%u-%g-%p-%t

gdbコマンドを使用してデバッグするには、次の手順に従います.
[root@localhost home]# gdb ./main ./core-main-11-0-0-3424-1451986509 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-51.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/main...(no debugging symbols found)...done.
[New LWP 3424]
Core was generated by `./main'.
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000400500 in main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.17-55.el7_0.1.x86_64
(gdb) where
#0  0x0000000000400500 in main ()
(gdb) quit
whereを入力すると、異常の位置をすばやく表示できます.入力quitは迅速に終了します.
4. pstack
pstackは一般的にスレッドを表示します.マルチスレッドcプログラムでは、デッドロックが発生しやすく、pstackを使用してデッドロックがあるかどうかを確認します.
#Togo 9   ,      ,      ,8 libevent epoll wait

[root@localhost home]# ps -ef | grep togo
root      2999     1  8 16:14 ?        00:08:16 ./togo
root      3518  2851  0 17:51 pts/0    00:00:00 grep --color=auto togo
[root@localhost home]# pstack 2999
Thread 10 (Thread 0x7f6f75079700 (LWP 3000)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x0000000000401d22 in togo_mt_process (args=<optimized out>) at togo_server.c:183
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 9 (Thread 0x7f6f74878700 (LWP 3001)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 8 (Thread 0x7f6f6ffff700 (LWP 3002)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 7 (Thread 0x7f6f6f7fe700 (LWP 3003)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 6 (Thread 0x7f6f6effd700 (LWP 3004)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 5 (Thread 0x7f6f6e7fc700 (LWP 3005)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 4 (Thread 0x7f6f6dffb700 (LWP 3006)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f6f6d7fa700 (LWP 3007)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f6f6cff9700 (LWP 3008)):
#0  0x00007f6f79bba5f3 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f6f79ea9803 in epoll_dispatch () from /lib64/libevent-2.0.so.5
#2  0x00007f6f79e953ea in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x000000000040208f in togo_wt_cb (args=<optimized out>) at togo_server.c:338
#4  0x00007f6f7a0d4df3 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6f79bba01d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f6f7a4fb740 (LWP 2999)):
#0  0x00007f6f7a0d5f37 in pthread_join () from /lib64/libpthread.so.0
#1  0x00000000004026f5 in togo_server_init () at togo_server.c:103
#2  0x0000000000401b5a in togo_init (argc=1, argv=0x7fffae18a178) at togo_init.c:24
#3  0x00007f6f79ae5af5 in __libc_start_main () from /lib64/libc.so.6
#4  0x00000000004017e1 in _start ()