目录
一,程序发布模式
二,调试器gdb
一,程序发布模式
程序发布方式有debug和release模式;
Linux gcc/g++生成的二进制执行文件,默认为release模式;
使用gbd调试,在gcc/g++编译时需加选项-g,以debug模式发布;
//默认无调试信息
[wz@VM-4-4-centos ~]$ gdb test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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 /home/wz/test...(no debugging symbols found)...done.
(gdb)
//debug版本会偏大
[wz@VM-4-4-centos ~]$ ll -h
total 28K
-rw-rw-r-- 1 wz wz 137 Mar 13 21:27 test.c
-rwxrwxr-x 1 wz wz 9.2K Mar 13 21:28 test_debug
-rwxrwxr-x 1 wz wz 8.2K Mar 13 21:27 test_release
//test_debug具有调试信息
[wz@VM-4-4-centos ~]$ readelf -S test_release | grep debug
[wz@VM-4-4-centos ~]$ readelf -S test_debug | grep debug
[27] .debug_aranges PROGBITS 0000000000000000 00001061
[28] .debug_info PROGBITS 0000000000000000 00001091
[29] .debug_abbrev PROGBITS 0000000000000000 0000113d
[30] .debug_line PROGBITS 0000000000000000 0000118e
[31] .debug_str PROGBITS 0000000000000000 000011de
二,调试器gdb
- gbd "binfile",使用gbd调试;
- ctrl+d、或quit/q,退出gdb;
- l 行号、或list 行号,显示源代码,每次10行;
- b 行号、或break 行号,在指定行设置断点(类似vs的F9);
- info b、或info break,查看断点;
- r、或run,开始调试(类似vs的F5),从头开始;
- c,或continue,从当前位置继续连续调试,跳到下一个断点;
- n、或next,单步逐条执行(类似vs的F10);
- s,或step,单步逐过程执行(类似vs的F11,去进入被调用的函数体);
- until 行号,跳转到指定行(如跳出循环);
- finish,执行完当前函数;
- p 变量,打印变量值;
- print(表达式),打印表达式值;
- display 变量,跟踪常显示变量,每次停下来都会显示;
- undisplay 变量序号,取消变量跟踪;
- d n、或delete breakpoints n,删除序号为n的断点;
- delete breakpoints,删除所有断点;
- disable n,或disable breakpoints n,禁用断点;
- enable n,或enable breakpoints n,启用断点;
- bt,或breaktrace,查看调用堆栈;
- set 变量,设置变量值;
//list 行号、或l 行号,显示源代码,每次10行;
(gdb) l
1 #include
2
3 int main()
4 {
5 int i=0;
6 int sum=0;
7 for(;i<100;i++)
8 {
9 sum += i;
10 }
(gdb) l
11 printf("sum=%d\n",sum);
12 return 0;
13 }
(gdb) l
Line number 14 out of range; test.c has 13 lines.
//break 行号、或b 行号,在指定行设置断点(类似vs的F9);
(gdb) b 7
Breakpoint 1 at 0x400543: file test.c, line 7.
(gdb) b 11
Breakpoint 2 at 0x400555: file test.c, line 11.
//info break、或info b,查看断点;
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400543 in main at test.c:7
2 breakpoint keep y 0x0000000000400555 in main at test.c:11
//run、或r,开始调试(类似vs的F5);
(gdb) r
Starting program: /home/wz/test_debug
Breakpoint 1, main () at test.c:7
7 for(;i<100;i++)
Missing separate debuginfos, use: debuginfo-install glibc-2.17-325.el7_9.x86_64
//next、或n,单部逐条执行;
(gdb) n
9 sum += i;
(gdb) n
7 for(;i<100;i++)
(gdb) n
9 sum += i;
(gdb) n
7 for(;i<100;i++)
(gdb)
//p 变量,打印变量值;
(gdb) p i
$1 = 1
(gdb) p sum
$2 = 1
(gdb) p &i
$6 = (int *) 0x7fffffffe42c
(gdb) p &sum
$7 = (int *) 0x7fffffffe428
//print(表达式),打印表达式值;
(gdb) print(i)
$3 = 1
(gdb) print(sum)
$4 = 1
//display 变量,跟踪常显示变量,每次停下来都会显示;
(gdb) display i
1: i = 0
(gdb) display sum
2: sum = 0
(gdb) n
7 for(;i<100;i++)
2: sum = 0
1: i = 0
(gdb) n
9 sum += i;
2: sum = 0
1: i = 1
//undisplay取消变量跟踪
(gdb) undisplay 1
(gdb) n
9 sum += i;
2: sum = 3
(gdb)
//continue,或c,从当前位置继续连续调试;
(gdb) c
Continuing.
Breakpoint 2, main () at test.c:11
11 printf("sum=%d\n",sum);
2: sum = 4950
//删除断点
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400543 in main at test.c:7
2 breakpoint keep y 0x0000000000400555 in main at test.c:11
(gdb) d 2
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400543 in main at test.c:7
(gdb) d
Delete all breakpoints? (y or n) y
(gdb) info b
No breakpoints or watchpoints.
//breaktrace,或bt,查看调用堆栈;
(gdb) bt
#0 fun (num=100) at test.c:8
#1 0x000000000040056d in main () at test.c:14
//step,或s,单步逐过程执行(类似vs的F11);
(gdb) s
fun (num=100) at test.c:4
4 int i=0;
(gdb) n
5 int sum=0;
(gdb) n
6 for(;i
//finish,执行完当前函数;
(gdb) finish
Run till exit from #0 fun (num=100) at test.c:6
0x000000000040056d in main () at test.c:14
14 int sum=fun(100);
Value returned is $1 = 4950
//disable n,或disable breakpoints n,禁用断点;
//enable n,或enable breakpoints n,启用断点;
(gdb) disable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep n 0x0000000000400563 in main at test.c:14
breakpoint already hit 1 time
(gdb) enable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400563 in main at test.c:14
breakpoint already hit 1 time
//set 变量,设置变量值;
(gdb) p sum
$2 = 0
(gdb) set sum=10
(gdb) p sum
$3 = 10