调试器(GDB)的基本使用方法(之二)


attach到进程
使用的文件:
/*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
使用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 ()
注意:这个地方可能需要超级用户权限来执行,我使用的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
可以看出程序调用了sleep()函数而键入等待装填的从源文件中可以找到此行代码。
使用info proc可以查看进程信息:
(gdb) info proc
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

条件断点
条件断点仅在特定条件下中断。
格式:
     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]

/* 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;
}



常用命令及省略形式
命令简写形式说明
backtracebt、where显示backtrace
breakb设置断点
continuec继续运行
deleted删除断点
finish 运行到函数结束处
info breakpointsinfo b显示断点信息
nextn执行下一行
printp显示表达式
runr运行程序
steps一次执行一行,但是可以进入到函数内部(这点与next不同)
x 显示内存内容
untilu执行到指定的行
其他命令
命令简写形式说明
directorydir插入目录
disabledis禁用断点
downdo在当前调用栈中选择要显示的栈帧
edite编辑问价或函数
framef选择要显示的栈帧
forward-searchfo向前搜索
generate-core-filegcore生成内核转储
helph显示帮助一栏
infoi显示信息
listl显示函数或行
nextini执行下一行(以汇编代码为单位)
print-objectpo显示目标信息
sharedlibraryshare加载共享库的符号
stepisi执行下一行


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值