#include"stdio.h" int add(int x,int y) { return x+y; }
int main() { int p = add(6,5); printf("%d\n",p); return 0; }
编译 gcc
1 2 3 4 5 6 7 8
[root@localhost cpp]# gcc add.cpp -o add.o [root@localhost cpp]# ll -al total 20 drwxr-xr-x. 2 root root 34 Oct 15 02:14 . drwxr-xr-x. 4 root root 39 Oct 15 00:41 .. -rw-r--r--. 1 root root 121 Oct 15 00:43 add.cpp -rwxr-xr-x. 1 root root 12776 Oct 15 02:14 add.o [root@localhost cpp]#
进入gdb调试
1. 方法一
1
gdb $file
如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@localhost cpp]# gdb add.o GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8 Copyright (C) 2018 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". 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 add.o...(no debugging symbols found)...done. (gdb)
[root@localhost cpp]# gdb GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8 Copyright (C) 2018 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". 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". (gdb) file add.o Reading symbols from add.o...(no debugging symbols found)...done. (gdb)
使用gdb打开文件后 ,使用 run 或者 r 开始程序的执行, 也可以使用 run parameter 将参数传递给该程序
GDB 命令列表
命令
缩写
命令说明
list
l
显示多行源代码 编译选项需要 gcc -g
break
b
设置断点 b main
info
i
描述程序的状态
run
r
开始运行程序
display
disp
跟踪查看某个变量,每次停下来都显示它的值
step
s
执行下一条语句,如果该语句为函数调用,则进入函数执行第一条语句s;相当于其它调试器中的“Step Into (单步跟踪进入)”
next
n
执行下一条语句,如果该语句为函数调用,不会进入函数内部执行(即不会一步步调试函数内部语句);相当于其它调试器中的“Step Over (单步跟踪)”
[root@localhost cpp]# gcc -o add.o add.cpp [root@localhost cpp]# gdb add.o GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8 Copyright (C) 2018 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". 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 add.o...(no debugging symbols found)...done. (gdb) list No symbol table is loaded. Use the "file" command. (gdb)
[root@localhost cpp]# gcc -g -o add.o add.cpp [root@localhost cpp]# gdb add.o GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8 Copyright (C) 2018 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". 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 add.o...done. (gdb) l 1 #include"stdio.h" 2 int add(int x,int y) 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); (gdb)
(gdb) list main 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); 11 return 0; 12 } (gdb) list add 1 #include"stdio.h" 2 int add(int x,int y) 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); (gdb)
list -
显示刚才打印过的源代码之前的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(gdb) list main 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); 11 return 0; 12 } (gdb) list - 1 #include"stdio.h" 2 int add(int x,int y) (gdb)
list
在刚才打印过的源代码之后打印10条代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(gdb) list - 1 #include"stdio.h" 2 int add(int x,int y) (gdb) list 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); 11 return 0; 12 } (gdb) list Line number 13 out of range; add.cpp has 12 lines. (gdb)
(gdb) list 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); 11 return 0; 12 } (gdb) b 10 Breakpoint 1 at 0x4005c4: file add.cpp, line 10. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
Breakpoint 1, main () at add.cpp:10 10 printf("%d\n",p); (gdb) c Continuing. 11 [Inferior 1 (process 1957) exited normally] (gdb)
使用 delete breakpoints 断点号 删除断点 这里的断点号表示的是第几个断点,刚才执行 break 10 返回 Breakpoint 1 at 0x4005c4: file add.cpp, line 10. 中的1表示该断点的标号,因此使用 delete breakpoints 1 表示删除第10行所定义的断点 clear n 表示清除第n行的断点,因此 clear 10 等同于 delete breakpoints 1 disable/enable n 表示使得编号为n的断点暂时失效或有效
info breakpoints 查看断点相关的信息
属性
含义
Num
断点编号
Disp
断点执行一次之后是否有效 kep:有效 dis:无效
Enb
当前断点是否有效 y:有效 n:无效
Address
内存地址
What
位置
如下:
1 2 3 4 5
(gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000004005c4 in main() at add.cpp:10 breakpoint already hit 1 time (gdb)
display /disp 查看参数值
1 2 3 4 5 6 7 8 9 10 11
(gdb) b 10 Breakpoint 1 at 0x4005c4: file add.cpp, line 10. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
Breakpoint 1, main () at add.cpp:10 10 printf("%d\n",p); (gdb) display p 1: p = 11 (gdb)
(gdb) b main Breakpoint 2 at 0x4005b2: file add.cpp, line 9. (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /root/projects/cpp/add.o
Breakpoint 2, main () at add.cpp:9 9 int p = add(6,5); 1: p = 0 (gdb) s add (x=6, y=5) at add.cpp:4 4 return x+y; (gdb) s 5 } (gdb) s
Breakpoint 1, main () at add.cpp:10 10 printf("%d\n",p); 1: p = 11 (gdb) s 11 11 return 0; 1: p = 11 (gdb) s 12 } 1: p = 11 (gdb) s 0x00007ffff7a356a3 in __libc_start_main () from /lib64/libc.so.6 (gdb)
(gdb) r Starting program: /root/projects/cpp/add.o
Breakpoint 2, main () at add.cpp:9 9 int p = add(6,5); 1: p = 0 (gdb) n
Breakpoint 1, main () at add.cpp:10 10 printf("%d\n",p); 1: p = 11 (gdb) n 11 11 return 0; 1: p = 11 (gdb) n 12 } 1: p = 11 (gdb) n 0x00007ffff7a356a3 in __libc_start_main () from /lib64/libc.so.6
(gdb) l 1 #include"stdio.h" 2 int add(int x,int y) 3 { 4 return x+y; 5 } 6 7 int main() 8 { 9 int p = add(6,5); 10 printf("%d\n",p); (gdb) l 11 return 0; 12 } (gdb) b main Breakpoint 1 at 0x4005b2: file add.cpp, line 9. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
Breakpoint 1, main () at add.cpp:9 9 int p = add(6,5); (gdb) watch p Hardware watchpoint 2: p (gdb) c Continuing.
Hardware watchpoint 2: p
Old value = 0 New value = 11 main () at add.cpp:10 10 printf("%d\n",p); (gdb) c Continuing. 11
Watchpoint 2 deleted because the program has left the block in which its expression is valid. 0x00007ffff7a356a3 in __libc_start_main () from /lib64/libc.so.6 (gdb)
print命令 whatis命令
print 表达式:简记为 p ,其中“表达式”可以是任何当前正在被测试程序的有效表达式,比如当前正在调试C语言的程序,那么“表达式”可以是任何C语言的有效表达式,包括数字,变量甚至是函数调用。 print a:将显示整数 a 的值 print ++a:将把 a 中的值加1,并显示出来 print name:将显示字符串 name 的值 print gdb_test(22):将以整数22作为参数调用 gdb_test() 函数 print gdb_test(a):将以变量 a 作为参数调用 gdb_test() 函数
Reading symbols from add.o...done. (gdb) b main Breakpoint 1 at 0x4005b2: file add.cpp, line 9. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
Breakpoint 1, main () at add.cpp:9 9 int p = add(6,5); (gdb) bt #0 main () at add.cpp:9 (gdb) s add (x=6, y=5) at add.cpp:4 4 return x+y; (gdb) bt #0 add (x=6, y=5) at add.cpp:4 #1 0x00000000004005c1 in main () at add.cpp:9 (gdb) s 5 } (gdb) s main () at add.cpp:10 10 printf("%d\n",p); (gdb) s 11 11 return 0; (gdb) s 12 } (gdb) s 0x00007ffff7a356a3 in __libc_start_main () from /lib64/libc.so.6 (gdb) bt #0 0x00007ffff7a356a3 in __libc_start_main () from /lib64/libc.so.6 #1 0x00000000004004de in _start () (gdb) s Single stepping until exit from function __libc_start_main, which has no line number information. [Inferior 1 (process 2029) exited normally] (gdb) bt No stack. (gdb)
Reading symbols from add.o...done. (gdb) b main Breakpoint 1 at 0x4005b2: file add.cpp, line 9. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
(gdb) disassemble /m main Dump of assembler code for function main(): 8 { 0x00000000004005aa <+0>: push %rbp 0x00000000004005ab <+1>: mov %rsp,%rbp 0x00000000004005ae <+4>: sub $0x10,%rsp
Reading symbols from add.o...done. (gdb) disass /m main Dump of assembler code for function main(): 8 { 0x00000000004005aa <+0>: push %rbp 0x00000000004005ab <+1>: mov %rsp,%rbp 0x00000000004005ae <+4>: sub $0x10,%rsp
End of assembler dump. (gdb) b *0x00000000004005ab Breakpoint 1 at 0x4005ab: file add.cpp, line 8. (gdb) r Starting program: /root/projects/cpp/add.o Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
Breakpoint 1, 0x00000000004005ab in main () at add.cpp:8 8 { (gdb) p $rbp $1 = (void *) 0x4005e0 <__libc_csu_init> (gdb) p $rsp $2 = (void *) 0x7fffffffe420 (gdb) si 0x00000000004005ae 8 { (gdb) disass main Dump of assembler code for function main(): 0x00000000004005aa <+0>: push %rbp 0x00000000004005ab <+1>: mov %rsp,%rbp => 0x00000000004005ae <+4>: sub $0x10,%rsp 0x00000000004005b2 <+8>: mov $0x5,%esi 0x00000000004005b7 <+13>: mov $0x6,%edi 0x00000000004005bc <+18>: callq 0x400596 <add(int, int)> 0x00000000004005c1 <+23>: mov %eax,-0x4(%rbp) 0x00000000004005c4 <+26>: mov -0x4(%rbp),%eax 0x00000000004005c7 <+29>: mov %eax,%esi 0x00000000004005c9 <+31>: mov $0x400678,%edi 0x00000000004005ce <+36>: mov $0x0,%eax 0x00000000004005d3 <+41>: callq 0x4004a0 <printf@plt> 0x00000000004005d8 <+46>: mov $0x0,%eax 0x00000000004005dd <+51>: leaveq 0x00000000004005de <+52>: retq End of assembler dump. (gdb) p $rbp $4 = (void *) 0x7fffffffe420 (gdb) si 9 int p = add(6,5); (gdb) p $rsp $5 = (void *) 0x7fffffffe410 (gdb)