设为首页 加入收藏

TOP

调试器(GDB)的基本使用方法(之二)(一)
2014-11-23 21:34:10 来源: 作者: 【 】 浏览:9
Tags:调试器 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 
#include 

#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
2901 0.0 0.0 2000 284 pts/2 S 19:20 0:00 ./test_attach
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/'
exe = '/home//test_attach'
最后使用detach命令可以将gdb与进程分离。 (gdb) detach
Detaching from program: /home//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 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:
.
(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,
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇调试器(GDB)的基本使用方法(之.. 下一篇c语言中关于指针的一些乱七八糟的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: