linuxのデバック方法

いつかまとめる-メモ

gdb

 コアファイルがある場合
 gdb -c core
 bt
 
 (gdb) attach 2471
Attaching to process 2471
Reading symbols from /usr/sbin/winbindd...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libcap.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/x86_64-linux-gnu/libcap.so.2
Reading symbols from /lib/x86_64-linux-gnu/libresolv.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/x86_64-linux-gnu/libresolv.so.2
Reading symbols from /lib/x86_64-linux-gnu/libnsl.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/x86_64-linux-gnu/libnsl.so.1
Reading symbols from /lib/x86_64-linux-gnu/libdl.so.2...(no debugging symbols found)...done.

(gdb) bt # 現在のバックトレースを表示する
#0  0x00007f4459c73df8 in poll () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007f445c95561e in sys_poll ()
#2  0x00007f445c98c8dc in ?? ()
#3  0x00007f445c98d460 in _tevent_loop_once ()
#4  0x00007f445c89fc3b in main ()

(gdb) info proc # プロセス情報を表示する
process 29027
cmdline = 'php-fpm: master process (/etc/php5/fpm/php-fpm.conf)'
cwd = '/'
exe = '/usr/sbin/php5-fpm'

(gdb) info threads # スレッドの一覧を表示する
  Id   Target Id         Frame
* 1    Thread 0x7f546190f720 (LWP 29027) "php5-fpm" 0x00007f545ecbd0d3 in epoll_wait () from /lib/x86_64-linux-gnu/libc.so.6

(gdb) info program # プログラムの実行状態を表示
        Using the running image of attached Thread 0x7fa5c6525780 (LWP 5776).
Program stopped at 0x7fa5c54cb2b3.

(gdb) info locals # ローカル変数の名前と値を表示
No locals.

(gdb) thread apply 1 bt # スレッドにコマンドを送信する

Thread 1 (Thread 0x7f546190f720 (LWP 29027)):
#0  0x00007f545ecbd0d3 in epoll_wait () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x0000000000757542 in ?? ()
#2  0x000000000074e299 in ?? ()
#3  0x0000000000748e57 in ?? ()
#4  0x0000000000431214 in ?? ()
#5  0x00007f545ec00ead in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#6  0x0000000000432501 in _start ()

(gdb) show # 各種設定を表示する
ada trust-PAD-over-XVS:  Whether an optimization trusting PAD types over XVS types is activated is on.
annotate:  Annotation_level is 0.
architecture:  The target architecture is set automatically (currently i386:x86-64)
args:  Argument list to give program being debugged when it is started is "".
auto-load-scripts:  The debugger's behaviour regarding auto-loaded scripts is on.
auto-solib-add:  Autoloading of shared library symbols is on.

(gdb) run /var/www/htdocs/a.php
Starting program: /usr/bin/php5 /var/www/htdocs/a.php
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff0f09700 (LWP 1497)]
[Thread 0x7ffff0f09700 (LWP 1497) exited]
array(1) {
  [0]=>
  string(21) "/var/www/htdocs/a.php"
}
[Inferior 1 (process 1494) exited normally]


(gdb) quit

(gdb) where
#0  0x00007f545ecbd0d3 in epoll_wait () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x0000000000757542 in ?? ()
#2  0x000000000074e299 in ?? ()
#3  0x0000000000748e57 in ?? ()
#4  0x0000000000431214 in ?? ()
#5  0x00007f545ec00ead in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#6  0x0000000000432501 in _start ()

https://www.ibm.com/developerworks/jp/linux/library/l-debug/

strace

straceはシステムコールトレーサで、すでに実行されているプログラムによって発せられたコールを追跡する

root@akat:/home/akat# strace -tt -s 1024 -p 29027
Process 29027 attached - interrupt to quit
00:01:46.929454 clock_gettime(CLOCK_MONOTONIC, {6643581, 965972053}) = 0
00:01:46.930045 clock_gettime(CLOCK_MONOTONIC, {6643581, 966349703}) = 0
00:01:46.930632 epoll_wait(7, {}, 1, 179) = 0
00:01:47.110395 clock_gettime(CLOCK_MONOTONIC, {6643582, 146960800}) = 0
00:01:47.111103 clock_gettime(CLOCK_MONOTONIC, {6643582, 147427003}) = 0
00:01:47.111410 getsockopt(6, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\340p\370\213\0\0\0\0\340p\370\213\340p\370\213\0\0\0\0\0\0\0\0\0\0\0\0\220\320\3\0\377\377\377\177\n\0\0\0\0\0\0\0\3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", [104]) = 0
00:01:47.111942 clock_gettime(CLOCK_MONOTONIC, {6643582, 148215308}) = 0
00:01:47.112281 epoll_wait(7, ^C <unfinished ...>
Process 29027 detached

mysql

file /usr/sbin/mysqld # dynamically linkedでなければ、nmコマンドの-Dオプションは不要
nm -n -D /usr/sbin/mysqld > mysqld.sym
lv mysqld.sym
resolve_stack_dump -s mysqld.sym -n /var/log/mysql/error.log > mysqld.stack

参考

http://techblog.yahoo.co.jp/tips/php/