- 启动调试程序
如果需要调试程序,编译时需要加‘-g’参数
$ gcc -Wall -g crash.c -o crash
- 加载可执行文件
1.gdb 可执行文件名
2.先输入gdb 回车,再执行 file 可执行文件名
对应关系
1.$ gdb
GNU gdb (GDB) 7.8
...
2. (gdb) file crash
Reading symbols from crash...done.
- 保存断点功能
(gdb) b 10
Breakpoint 1 at 0x4011b6: file crash.c, line 10.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x004011b6 in main at crash.c:10
(gdb) save breakpoint crash.bp
Saved to file 'crash.bp'.
(gdb) q
Administrator@S8UUZHAMA4G7PPW ~/pro_test
$ ls
crash.bp crash.c crash.exe
- 加载断点信息
$ gdb crash.exe -x crash.bp
GNU gdb (GDB) 7.8
Copyright (C) 2014 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 "i686-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from crash.exe...done.
Breakpoint 1 at 0x4011b6: file crash.c, line 10.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x004011b6 in main at crash.c:10
(gdb)
- 调试带参数的程序
Administrator@S8UUZHAMA4G7PPW ~/pro_test
$ gcc -Wall -g -std=c99 test.c -o test
Administrator@S8UUZHAMA4G7PPW ~/pro_test
$ ./test a b c d
argc: 5
argv[0] : ./test
argv[1] : a
argv[2] : b
argv[3] : c
argv[4] : d
1.set args方式
$ gdb test
GNU gdb (GDB) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
...
Reading symbols from test...done.
(gdb) set args a b c d
(gdb) r
Starting program: /home/Administrator/pro_test/test a b c d
[New Thread 4948.0x1034]
[New Thread 4948.0x1bc]
argc: 5
argv[0] : /home/Administrator/pro_test/test
argv[1] : a
argv[2] : b
argv[3] : c
argv[4] : d
[Thread 4948.0x1bc exited with code 1]
[Inferior 1 (process 4948) exited with code 01]
(gdb)
2.带参数调试方法
$ gdb --args test a b c d
GNU gdb (GDB) 7.8
...
Reading symbols from test...done.
(gdb) r
Starting program: /home/Administrator/pro_test/test a b c d
[New Thread 5344.0xde0]
[New Thread 5344.0x1738]
argc: 5
argv[0] : /home/Administrator/pro_test/test
argv[1] : a
argv[2] : b
argv[3] : c
argv[4] : d
[Inferior 1 (process 5344) exited with code 01]
(gdb)
3.run时带参数执行
$ gdb test
GNU gdb (GDB) 7.8
...
Reading symbols from test...done.
(gdb) r a b c d
Starting program: /home/Administrator/pro_test/test a b c d
[New Thread 3248.0xcb4]
[New Thread 3248.0x1704]
argc: 5
argv[0] : /home/Administrator/pro_test/test
argv[1] : a
argv[2] : b
argv[3] : c
argv[4] : d
[Thread 3248.0x1704 exited with code 1]
[Inferior 1 (process 3248) exited with code 01]
(gdb)
- cygwin中如何产生core
设置error_start为D:\cygwin\bin\dumper.exe
$ export CYGWIN='error_start=D:\cygwin\bin\dumper.exe'
- Core文件
1. 查看系统是否允许生成core文件
$ ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 2026
cpu time (seconds, -t) unlimited
max user processes (-u) 256
virtual memory (kbytes, -v) unlimited
core文件大小限制为0,不能生成core文件。
core file size (blocks, -c) 0
2. 使用如下命令取消限制,使系统能生成core文件
在宿主目录下修改.bashrc配置文件中添加 ulimit -c unlimited
或者指定core文件大小,如1K
ulimit -c 1024
3.启动gdb core(不带-q也可以)
$ gdb -q test test.exe.core
4.通过where查找出错的地方
(gdb) where
#0 0x004011a0 in test () at test.c:5
#1 0x004011c7 in main () at test.c:11
5.发布版core
用发布时的原代码,在原有的编译选项上(不要改变任何编译参数,即使有"-O2"参数,也不要动),只加上"-g"选项,编译出对应的debug程序。
6.Core文件的命名和路径
默认情况下core文件名即为"core",文件路径:运行可执行文件时,环境所在的目录
两个控制文件
(1) /proc/sys/kernel/core_uses_pid
0: 默认值,生成的文件名不会带上PID后缀
1: 文件名带上PID后缀,如"core.2976"
修改方法: "echo 1 > /proc/sys/kernel/core_uses_pid"
(2) /proc/sys/kernel/core_pattern
用来控制文件名格式
默认值: core
假设将其值改变为:"/home/duanbei/corefile/core-%e-%p-%t", 那么所有的core文件将保存在"/home/duanbei/corefile"目录下,文件名格式为“core-程序名-pid-时间戳”
格式参数列表
%p - insert pid into filename
%u - insert current uid into filename
%g - insert current gid into filename
%s - insert signal that caused the coredump into the filename
%t - insert UNIX time that the coredump occurred into filename
%h - insert hostname where the coredump happened into filename
%e - insert coredumping executable name into filename
注:如果设置了“%p”,而“/proc/sys/kernel/core_uses_pid”值又为1,则不会添加PID后缀,因文件名中已含该信息。
如果需 要生成core文件,就不要在程序中用signal()捕获‘SIGSEGV’信号了,不然生成不了core文件。