调试器(GDB)的基本使用方法(之三)
值的历史 print命令显示过的值会被记录下来,这些值可以其他表达式中使用。 我们使用的源文件为:
(gdb) b main Breakpoint 1 at 0x8048380: file contains3.c, line 35.
(gdb) b 14
Breakpoint 2 at 0x804845e: file contains3.c, line 14.
(gdb) b 31
Breakpoint 3 at 0x8048524: file contains3.c, line 31.
(gdb) r
Starting program: /home/shuaihua/CLan/contains3
Breakpoint 1, main (argc=1, argv=0xbffff324) at contains3.c:35
35 {
(gdb) p argc
$1 = 1
(gdb) p $
$2 = 1
(gdb) P $$
$3 = 1
(gdb) show value
$1 = 1
$2 = 1
$3 = 1
(gdb)
值的历史的访问变量和说明
变量 可以随意定义变量。变量以$开头,有英文和数字组成 (gdb) set $abc=100
(gdb) p $abc
$4 = 100
命令历史 默认历史命令存放在./.gdb_history中。 (gdb) show history
expansion: History expansion on command input is off.
filename: The filename in which to record the command history is "/home/<username>/CLan/.gdb_history".
save: Saving of the history record on exit is off.
size: The size of the command history is 256.
格式: set history expansion show history expansion (gdb) set history expansion
(gdb) show history expansion
History expansion on command input is on.
(gdb) show history
expansion: History expansion on command input is on.
filename: The filename in which to record the command history is "/home/<username>/CLan/.gdb_history".
save: Saving of the history record on exit is off.
size: The size of the command history is 256.
可将命令历史保存到文件中,可以通过环境变量GDBHISTFILE改变默认文件: 格式: set history filename 文件名 show history filename 启用命令历史保存到文件和恢复的功能: 格式: set history save show history save 设置保存到命令历史中的命令数量,默认为256。 格式: set history size 数字 show history size
(gdb) set history save
(gdb) show history save
Saving of the history record on exit is on.
(gdb) set history size 512
(gdb) show history size
The size of the command history is 512.
(gdb) show history
expansion: History expansion on command input is on.
filename: The filename in which to record the command history is "/home/shuaihua/CLan/.gdb_history".
save: Saving of the history record on exit is on.
size: The size of the command history is 512.
初始化文件(.gdbinit) Linux下gdb初始化文件为.gdbinit。如果存在.gdbinit文件,GDB在启动之前将其作为命令文件运行。 顺序如下: $HOME/.gdbinit运行命令行选项./.gdbinit加载通过-x选项给出的命令文件
命令定义 用define可以自定义命令,用document可以给自定义的命令加说明,利用help 命令名可以查看定义的命令。 define格式: define 命令名 命令 ………… end
document格式: document 命令名 说明 end
help格式: help 命令名
(gdb) define lsi
Type commands for definition of "lsi".
End with a line saying just "end".
>x/15i $pc
>end
(gdb) document lsi
Type documentation for "lsi".
End with a line saying just "end".
>list machine instructions
>15
>end
(gdb) lsi
=> 0x8048380 <main>: push %ebp
0x8048381 <main+1>: mov %esp,%ebp
0x8048383 <main+3>: and $0xfffffff0,%esp
0x8048386 <main+6>: call 0x8048450 <solve>
0x804838b <main+11>: xor %eax,%eax
0x804838d <main+13>: leave
0x804838e <main+14>: ret
0x804838f: nop
0x8048390 <_start>: xor %ebp,%ebp
0x8048392 <_start+2>: pop %esi
0x8048393 <_start+3>: mov %esp,%ecx
0x8048395 <_start+5>: and $0xfffffff0,%esp
0x8048398 <_start+8>: push %eax
0x8048399 <_start+9>: push %esp
0x804839a <_start+10>: push %edx
(gdb) help lsi
list machine instructions
15
(gdb)
/* Filename: contains3.c* Description: 用来计算从1~1000的数中有多少个含有3的数。* Author: Howard* Date : 2013-12-05* Modified Date:2013-12-06* Version: v1.1*/#include <stdio.h>void solve(){ int i = 0; /*1=<i<=1000*/ int j = 0; /*控制输出个数为10时换行*/ int count = 0; /*计数符合要求的数的个数*/ int one, two, three; /*one(百位) two(十位) three(个位)*/ for (i=1; i<=1000; i++){ one = i/100; two = i%100/10; three = i%10; if (3==one || 3==two || 3==three){ j ++; count ++; printf("%4d", i); } if (10==j){ j = 0; printf("/n"); } } printf("/n总数为:%4d/n", count); }int main(int argc, char *argv[]){ solve(); return 0;}
(gdb) b main Breakpoint 1 at 0x8048380: file contains3.c, line 35.
(gdb) b 14
Breakpoint 2 at 0x804845e: file contains3.c, line 14.
(gdb) b 31
Breakpoint 3 at 0x8048524: file contains3.c, line 31.
(gdb) r
Starting program: /home/shuaihua/CLan/contains3
Breakpoint 1, main (argc=1, argv=0xbffff324) at contains3.c:35
35 {
(gdb) p argc
$1 = 1
(gdb) p $
$2 = 1
(gdb) P $$
$3 = 1
(gdb) show value
$1 = 1
$2 = 1
$3 = 1
(gdb)
值的历史的访问变量和说明
变量 | 说明 |
$ | 值历史中的最后一个值 |
$n | 值历史的第n个值 |
$$ | 值历史的倒数第二个值 |
$$n | 值历史的倒数第n个值 |
$_ | x命令显示过的最后的地址 |
$__ | x命令显示过的最后的地址的值 |
$_exitcode | 调试中的程序的返回代码 |
$bpnum | 最后设置的断点的编号 |
变量 可以随意定义变量。变量以$开头,有英文和数字组成 (gdb) set $abc=100
(gdb) p $abc
$4 = 100
命令历史 默认历史命令存放在./.gdb_history中。 (gdb) show history
expansion: History expansion on command input is off.
filename: The filename in which to record the command history is "/home/<username>/CLan/.gdb_history".
save: Saving of the history record on exit is off.
size: The size of the command history is 256.
格式: set history expansion show history expansion (gdb) set history expansion
(gdb) show history expansion
History expansion on command input is on.
(gdb) show history
expansion: History expansion on command input is on.
filename: The filename in which to record the command history is "/home/<username>/CLan/.gdb_history".
save: Saving of the history record on exit is off.
size: The size of the command history is 256.
可将命令历史保存到文件中,可以通过环境变量GDBHISTFILE改变默认文件: 格式: set history filename 文件名 show history filename 启用命令历史保存到文件和恢复的功能: 格式: set history save show history save 设置保存到命令历史中的命令数量,默认为256。 格式: set history size 数字 show history size
(gdb) set history save
(gdb) show history save
Saving of the history record on exit is on.
(gdb) set history size 512
(gdb) show history size
The size of the command history is 512.
(gdb) show history
expansion: History expansion on command input is on.
filename: The filename in which to record the command history is "/home/shuaihua/CLan/.gdb_history".
save: Saving of the history record on exit is on.
size: The size of the command history is 512.
初始化文件(.gdbinit) Linux下gdb初始化文件为.gdbinit。如果存在.gdbinit文件,GDB在启动之前将其作为命令文件运行。 顺序如下: $HOME/.gdbinit运行命令行选项./.gdbinit加载通过-x选项给出的命令文件
命令定义 用define可以自定义命令,用document可以给自定义的命令加说明,利用help 命令名可以查看定义的命令。 define格式: define 命令名 命令 ………… end
document格式: document 命令名 说明 end
help格式: help 命令名
(gdb) define lsi
Type commands for definition of "lsi".
End with a line saying just "end".
>x/15i $pc
>end
(gdb) document lsi
Type documentation for "lsi".
End with a line saying just "end".
>list machine instructions
>15
>end
(gdb) lsi
=> 0x8048380 <main>: push %ebp
0x8048381 <main+1>: mov %esp,%ebp
0x8048383 <main+3>: and $0xfffffff0,%esp
0x8048386 <main+6>: call 0x8048450 <solve>
0x804838b <main+11>: xor %eax,%eax
0x804838d <main+13>: leave
0x804838e <main+14>: ret
0x804838f: nop
0x8048390 <_start>: xor %ebp,%ebp
0x8048392 <_start+2>: pop %esi
0x8048393 <_start+3>: mov %esp,%ecx
0x8048395 <_start+5>: and $0xfffffff0,%esp
0x8048398 <_start+8>: push %eax
0x8048399 <_start+9>: push %esp
0x804839a <_start+10>: push %edx
(gdb) help lsi
list machine instructions
15
(gdb)
>更多相关文章
- 09-29如何通过wrap malloc定位C/C++程序的内存泄漏
- 02-25打车软件大战升级,补贴还能维持多久?
- 12-23BMP文件右旋90度[c语言]
- 12-23寻找直方图中面积最大的矩形(C语言版)
- 12-23[ndk,2]ndk开发案例和错误处理
- 12-23[ndk,1]ndk开发,C语言入门讲解
- 12-23C语言连续存储实现队列机制
- 12-23Objective-c 数据类型
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 12-05亚马逊推出新一代基础模型 任意模态生成大模
- 12-05OpenAI拓展欧洲业务 将在苏黎世设立办公室
- 12-05微软质疑美国联邦贸易委员会泄露信息 督促其
- 12-05联交所取消宝宝树上市地位 宝宝树:不会对公
- 12-04企业微信致歉:文档打开异常已完成修复
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御