使用的文件:
/*filename: test_attach.c
*description: This file used to test the attach command of gdb
*author: Howard
*date: 2013-11-25
*version: v1.0
*/
#ifndef ATTACHTEST
#define ATTACHTEST
#include <stdio.h>
#include <unistd.h>
#endif
int main(int argc, char *argv[])
{
while(1){
sleep(10);
}
return 0;
}
使用下面的方法编译并运行:
$ gcc -Wall -O2 -g -o test_attach test_attach.c
$ ./test_attach &
[1] 2901
$ ps aux | grep test_attach
<username> 2901 0.0 0.0 2000 284 pts/2 S 19:20 0:00 ./ test_attach
<username> 3021 0.0 0.1 6076 836 pts/2 S+ 19:21 0:00 grep --color=auto test_attach
$ ./test_attach &
[1] 2901
$ ps aux | grep test_attach
<username> 2901 0.0 0.0 2000 284 pts/2 S 19:20 0:00 ./ test_attach
<username> 3021 0.0 0.1 6076 836 pts/2 S+ 19:21 0:00 grep --color=auto test_attach
使用attach命令:
(gdb) attach 2901
Attaching to process 2901
Reading symbols from /home/shuaihua/test_attach...done.
Reading symbols from /lib/i386-linux-gnu/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00a1c416 in __kernel_vsyscall ()
Attaching to process 2901
Reading symbols from /home/shuaihua/test_attach...done.
Reading symbols from /lib/i386-linux-gnu/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00a1c416 in __kernel_vsyscall ()
注意:这个地方可能需要超级用户权限来执行,我使用的ubuntu12.04测试的。
使用显示栈帧可以看进程是怎样陷入等待状态的:
使用显示栈帧可以看进程是怎样陷入等待状态的:
(gdb) bt
#0 0x00a1c416 in __kernel_vsyscall ()
#1 0x004d9f00 in nanosleep () from /lib/i386-linux-gnu/libc.so.6
#2 0x004d9d1f in sleep () from /lib/i386-linux-gnu/libc.so.6
#3 0x0804834c in main (argc=1, argv=0xbfdab6c4) at test_attach.c:19
#0 0x00a1c416 in __kernel_vsyscall ()
#1 0x004d9f00 in nanosleep () from /lib/i386-linux-gnu/libc.so.6
#2 0x004d9d1f in sleep () from /lib/i386-linux-gnu/libc.so.6
#3 0x0804834c in main (argc=1, argv=0xbfdab6c4) at test_attach.c:19
可以看出程序调用了sleep()函数而键入等待装填的从源文件中可以找到此行代码。
使用info proc可以查看进程信息:
使用info proc可以查看进程信息:
(gdb) info proc
process 2901
cmdline = './test_attach'
cwd = '/home/<username>'
exe = '/home/<username>/test_attach'
最后使用detach命令可以将gdb与进程分离。
process 2901
cmdline = './test_attach'
cwd = '/home/<username>'
exe = '/home/<username>/test_attach'
最后使用detach命令可以将gdb与进程分离。
(gdb) detach
Detaching from program: /home/<username>/test_attach, process 2901
Detaching from program: /home/<username>/test_attach, process 2901
条件断点
条件断点仅在特定条件下中断。
格式:
break 断点 if 条件
condition 断点编号 条件——给指定的断点添加或删除触发条件
如果条件为真则暂停运行。
反复执行
格式:
ignore 断点编号 次数
在编号指定的断点、监视点(watchpoint)或捕获点(catchpoint)忽略指定的次数。
continue与ignore一样,有类似的功能。
执行指定次数的命令:
格式:
continue 次数
step 次数
stepi 次数
next 次数
nexti 次数
finish命令执行完当前函数后暂停,until命令执行完当前函数等代码块后暂停,如果是循环,则执行完尊换后暂停。
格式:
finish
until
until 地址
删除断点和禁用断点
clear可以删除已定义的断点,disable可以临时禁用断点。
格式:
clear
clear 函数名
clear 行号
clear 文件名:行号
clear 文件名:函数名
delete [breakpoints] 断点编号
格式:
disable [breakpoints]
disable [breakpoints] 断点编号
disable display 显示编号
disable mem 内存区域
如果不指定断点号,则表示禁用所有断点。
enable [breakpoints]
enable [breakpoints] 断点编号
enable [breakpoints] once 断点编号
enable [breakpoints] delete 断点编号
enable display 显示编号
enable mem 内存区域
once是指定的断点只启用一次,delete是在暂停后删除断点。
断点命令
在断点中断时自动执行的命令。
格式:
commands 断点编号
命令
......
end
$ gdb
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
(gdb) file staticvariable
Reading symbols from /home/shuaihua/CLan/staticvariable...done.
(gdb) info b
No breakpoints or watchpoints.
(gdb) b 28 if k==100
Breakpoint 1 at 0x80483a5: file staticvariable.c, line 28.
(gdb) command 1
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>p k
>end
(gdb) b main
Breakpoint 2 at 0x8048359: file staticvariable.c, line 19.
(gdb) command 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>p j
>end
(gdb) run
Starting program: /home/shuaihua/CLan/staticvariable
Breakpoint 2, main () at staticvariable.c:19
19 {
$1 = 0
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
19 {
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
19 {
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
i = :10
j = :1
Breakpoint 1, main () at staticvariable.c:32
32 }
$2 = 100
(gdb) n
0x0014c4d3 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 2950) exited normally]
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
(gdb) file staticvariable
Reading symbols from /home/shuaihua/CLan/staticvariable...done.
(gdb) info b
No breakpoints or watchpoints.
(gdb) b 28 if k==100
Breakpoint 1 at 0x80483a5: file staticvariable.c, line 28.
(gdb) command 1
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>p k
>end
(gdb) b main
Breakpoint 2 at 0x8048359: file staticvariable.c, line 19.
(gdb) command 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>p j
>end
(gdb) run
Starting program: /home/shuaihua/CLan/staticvariable
Breakpoint 2, main () at staticvariable.c:19
19 {
$1 = 0
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
19 {
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
19 {
(gdb) n
28 printf("i = :%d\n", i);
(gdb) n
i = :10
j = :1
Breakpoint 1, main () at staticvariable.c:32
32 }
$2 = 100
(gdb) n
0x0014c4d3 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 2950) exited normally]
/* FileName: staticvariable.c
* Description: 用来测试静态变量的输出结果。
* Author: Howard
* Date: 2013-12-05
* Version: v1.0
*/
#include <stdio.h>
static int j = 0;
int func1(void)
{
static int i = 0;
i ++;
return i;
}
void func2(void)
{
j = 0;
j ++;
}
int main(void)
{
int k;
int i;
for (k=0; k<10; k++){
i = func1();
func2();
}
k = 100;
printf("i = :%d\n", i);
printf("j = :%d\n", j);
return 0;
}
常用命令及省略形式
命令 | 简写形式 | 说明 |
backtrace | bt、where | 显示backtrace |
break | b | 设置断点 |
continue | c | 继续运行 |
delete | d | 删除断点 |
finish | 运行到函数结束处 | |
info breakpoints | info b | 显示断点信息 |
next | n | 执行下一行 |
p | 显示表达式 | |
run | r | 运行程序 |
step | s | 一次执行一行,但是可以进入到函数内部(这点与next不同) |
x | 显示内存内容 | |
until | u | 执行到指定的行 |
其他命令
命令 | 简写形式 | 说明 |
directory | dir | 插入目录 |
disable | dis | 禁用断点 |
down | do | 在当前调用栈中选择要显示的栈帧 |
edit | e | 编辑问价或函数 |
frame | f | 选择要显示的栈帧 |
forward-search | fo | 向前搜索 |
generate-core-file | gcore | 生成内核转储 |
help | h | 显示帮助一栏 |
info | i | 显示信息 |
list | l | 显示函数或行 |
nexti | ni | 执行下一行(以汇编代码为单位) |
print-object | po | 显示目标信息 |
sharedlibrary | share | 加载共享库的符号 |
stepi | si | 执行下一行 |