GDB
用GDB调试Python程序
环境是,Python程序调用了C语言的函数,用GDB调试Python程序,从里面进入C函数再进行详细查看。
# gdb python
> break Py_function_name # in python c wrapper file
> dir c_source_code_dir
> run python_script.py
> l
> n
> xxx
用GDB调试可执行程序
这里的可执行程序可以是用C语言写的简单的二进制程序。
# gdb executable-file
# gdb --args ./executable-file 1 2 3
(gdb) break function_name
以上第一条命令的意思是这个二进制可执行程序没有输入参数,第二条命令意思是这个二进制可执行程序接收三个参数分别是1 2 3。
删除断点
# gdb
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004566b3 in func1 at test.c:6
2 breakpoint keep y 0x00000000004d8e63 in func2 at test.c:15
(gdb) del 2
GDB 打印完整的字符串
默认长度是200。
> set print elements 0
> p long_string
用GDB调试后台进程
# gdb -- pid
(gdb) break function_name
用GDB调试core dump文件
# file core.12345 # find the binary execute file
输出如下:
core.12345: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from '/opt/com/binfile'
# gdb /opt/com/binfile core.12345
输出如下:
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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:
...
Reading symbols from /opt/com/binfile...done.
[New Thread 13755]
[New Thread 13768]
[New Thread 13773]
[New Thread 13774]
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libz.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libz.so.1
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.1
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libnss_files.so.2
Reading symbols from /lib64/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgcc_s.so.1
Core was generated by `/opt/com/binfile'.
Program terminated with signal 6, Aborted.
#0 0x0000003dd8c328a5 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install binfile.x86_64
(gdb) bt # you can also use `where`
输出如下:
(gdb) where
#0 0x0000003dd8c328a5 in raise () from /lib64/libc.so.6
#1 0x0000003dd8c34085 in abort () from /lib64/libc.so.6
#2 0x0000003dd8c707b7 in __libc_message () from /lib64/libc.so.6
#3 0x0000003dd8c760e6 in malloc_printerr () from /lib64/libc.so.6
#4 0x0000000000475ad4 in setResourcePlan (path=0x4868410 "/",
xmlstr=0x4868870 "xxxx"..., cluster=1, detail=0x7fffa7bf3768) at xxx.consumer.c:4354
#5 0x00000000004754a3 in setresplan (ch=0x3366e80, xdrs=0x3366fb8, hdr=0x7fffa7bf4870) at xxx.consumer.c:4173
#6 0x00000000004170d7 in switch_ (chan=0x3366e80, xdrs=0x3366fb8, hdr=0x7fffa7bf4870) at xxx.c:1856
#7 0x00000000004161e2 in processRequest (chan=0x3366e80) at xxxx.c:1367
#8 0x0000000000419242 in donio (active=0x263e320) at xxx.nio.c:242
#9 0x00000000004152c6 in main (argc=1, argv=0x7fffa7bf4a88) at xxxx.c:730
(gdb) f 4
这个会跳到以上出错的#4,然后dir source_file_dir
(gdb) dir /path/to/source_dir
(gdb) p xxx
C++ 添加断点
(gdb) break namespace1::ClassName1::function1
(gdb) break namespace2::ClassName1<:classname2 namespace3::classname3>::function2
GDB Issue
Attempt to dereference a generic pointer.
有时用p var会报以上信息,这时需要加上类型,比如p (char *) var。