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

值的历史
print命令显示过的值会被记录下来,这些值可以其他表达式中使用。
我们使用的源文件为:
/* 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在启动之前将其作为命令文件运行。
顺序如下:
  1. $HOME/.gdbinit
  2. 运行命令行选项
  3. ./.gdbinit
  4. 加载通过-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) 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值