root@ubuntu:~/cpptest# gdb gdb_example GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git 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-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 gdb_example...done.
调试示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(gdb) break add Breakpoint 1 at 0x674: file gdb_example.c, line 3. (gdb) run Starting program: /root/cpptest/gdb_example
Breakpoint 1, add (a=48, b=85) at gdb_example.c:3 3 return a + b; (gdb) next 4 } (gdb) next main () at gdb_example.c:12 12 for (i = 0; i < 10; i++) (gdb) next 14 sum[i] = add(array1[i], array2[i]); (gdb) print sum $1 = {133, 0, 0, 0, 0, 0, 0, 0, 0, 0}
GDB的启动
启动GDB的方法有以下几种: 1、gdb program program也就是你的执行文件,一般在当前目录下。 2、gdb program core 用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。 3、gdb program 1234 如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。
(gdb) set *(unsignedchar *)p='h'//令p是指向无符号字符的指针变量 (gdb) set *(unsignedchar *)(p+1)='e' (gdb) set *(unsignedchar *)(p+2)='l' (gdb) set *(unsignedchar *)(p+3)='l' (gdb) set *(unsignedchar *)(p+4)='o'
看看结果:
1 2
(gdb) x/s p 0x804b008: "hello"
也可以直接使用地址常数:
1 2 3 4 5 6 7 8 9
(gdb) p p //print命令(缩写为p) $2 = (void *) 0x804b008 (gdb) set *(unsignedchar *)0x804b008='w' (gdb) set *(unsignedchar *)0x804b009='o' (gdb) set *(unsignedchar *)0x804b00a='r' (gdb) set *(unsignedchar *)0x804b00b='l' (gdb) set *(unsignedchar *)0x804b00c='d' (gdb) x/s 0x804b008 0x804b008: "world"
run命令
在GDB中,运行程序使用run命令。在程序运行前,我们可以设置如下4方面的工作环境。
程序运行参数 set args可指定运行时参数,如
1
set args 1020304050;
show args命令可以查看设置好的运行参数。
运行环境 path<dir>可设定程序的运行路径; how paths可查看程序的运行路径; set environment varname[=value]可设置环境变量,如set env USER=baohua; show environment[varname]则可查看环境变量。
工作目录 cd<dir>相当于shell的cd命令,pwd可显示当前所在的目录。
程序的输入输出 info terminal用于显示程序用到的终端的模式; 在GDB中也可以使用重定向控制程序输出,如run>outfile; 用tty命令可以指定输入输出的终端设备,如tty /dev/ttyS1。
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000000674 in add at gdb_example.c:3 2 breakpoint keep y 0x0000000000000752 in main at gdb_example.c:12 3 breakpoint keep y 0x0000000000000689 in main at gdb_example.c:5 4 breakpoint keep y 0x0000000000000752 in main at gdb_example.c:12 5 breakpoint keep y 0x000000000000070c in main at gdb_example.c:10 6 breakpoint keep y 0x0000000000000698 in main at gdb_example.c:7 (gdb) info break5 Num Type Disp Enb Address What 5 breakpoint keep y 0x000000000000070c in main at gdb_example.c:10 (gdb) info breakpoint 5 Num Type Disp Enb Address What 5 breakpoint keep y 0x000000000000070c in main at gdb_example.c:10
Dump of assembler code for function func: 0x8048450 <func>: push %ebp 0x8048451 <func+1>: mov %esp,%ebp 0x8048453 <func+3>: sub $0x18,%esp 0x8048456 <func+6>: movl $0x0,0xfffffffc(%ebp) ... End of assembler dump.