gdb给指定位置设置断点

  1. 说明

    • gdb断点

      • 有三种: 模糊行级匹配,精确行级匹配,指令级匹配.
    • 模糊行级匹配

      • 即断点是在某行,但是给的表达式可以匹配多个行.
      • 即设置了多个断点.
    • 精确行级匹配

      • 同上,去除上面的二义性.
    • 指令级匹配

      • 断点精确到具体的某个指令,虽然最终也会落在某行.
  2. 模糊行级匹配

    • 代码

      int a = 0;
      void show(int) {
         a += 2;
      }
      void show() {
         a ++;
      }
      int main() {
         show(1);
         show(2);
         return 0;
      }
      
    • 编译调试

      [root@localhost temp]# g++ test.cpp -g
      [root@localhost temp]# gdb ./a.out
      GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.el7
      Copyright (C) 2013 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 "x86_64-redhat-linux-gnu".a
      For bug reporting instructions, please see:
      <http://www.gnu.org/software/gdb/bugs/>...
      Reading symbols from /root/cfile/temp/a.out...done.
      (gdb) break show
      Breakpoint 1 at 0x400514: show. (2 locations)
      (gdb) info breakpoints
      Num     Type           Disp Enb Address            What
      1       breakpoint     keep y   <MULTIPLE>
      1.1                         y     0x0000000000400514 in show(int) at test.cpp:3
      1.2                         y     0x0000000000400529 in show() at test.cpp:6
      (gdb)
      
      
      • 可以看到break show设置断点添加了两个断点.
  3. 精确行级匹配

    • 说明

      • gdb需要升级才支持.与gcc4.8.5配对的不支持.
    • 升级

      • 下载 wget https://repo.huaweicloud.com/centos/7/sclo/x86_64/rh/Packages/d/devtoolset-7-gdb-8.0-17.el7.x86_64.rpm
      • 配置 yum install ./devtoolset-7-gdb-8.0-17.el7.x86_64.rpm
      • 安装 yum install devtoolset-7-gdb.x86_64 -y
    • 指定

      [root@localhost temp]# /opt/rh/devtoolset-7/root/usr/bin/gdb a.out
      GNU gdb (GDB) Red Hat Enterprise Linux 8.0.1-36.el7
      Copyright (C) 2017 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 "x86_64-redhat-linux-gnu".
      Type "show configuration" for configuration details.
      For bug reporting instructions, please see:
      <http://www.gnu.org/software/gdb/bugs/>.
      Find the GDB manual and other documentation resources online at:
      <http://www.gnu.org/software/gdb/documentation/>.
      For help, type "help".
      Type "apropos word" to search for commands related to "word"...
      Reading symbols from a.out...done.
      (gdb) break -source test.cpp -function show -line 3
      Breakpoint 1 at 0x400514: file test.cpp, line 3.
      (gdb) info breakpoints
      Num     Type           Disp Enb Address            What
      1       breakpoint     keep y   0x0000000000400514 in show(int) at test.cpp:3
      (gdb)
      
      
    • 说明

      • 支持指定source源文件,-function函数,-line行号,-label跳转位置.
  4. 指令级断点

    • 说明

      • 通过反汇编获取虚拟地址,设置对应位置的断点.
      • 注意指令前面加 * .
    • 案例

      [root@localhost temp]# gdb ./a.out
      GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.el7
      Copyright (C) 2013 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 "x86_64-redhat-linux-gnu".
      For bug reporting instructions, please see:
      <http://www.gnu.org/software/gdb/bugs/>...
      Reading symbols from /root/cfile/temp/a.out...done.
      (gdb) disassemble show
      Dump of assembler code for function show(int):
        0x000000000040050d <+0>:     push   %rbp
        0x000000000040050e <+1>:     mov    %rsp,%rbp
        0x0000000000400511 <+4>:     mov    %edi,-0x4(%rbp)
        0x0000000000400514 <+7>:     mov    0x200b16(%rip),%eax        # 0x601030 <a>
        0x000000000040051a <+13>:    add    $0x2,%eax
        0x000000000040051d <+16>:    mov    %eax,0x200b0d(%rip)        # 0x601030 <a>
        0x0000000000400523 <+22>:    pop    %rbp
        0x0000000000400524 <+23>:    retq
      End of assembler dump.
      (gdb) break *0x000000000040050e
      Breakpoint 1 at 0x40050e: file test.cpp, line 2.
      
      
  5. 总结

    • 转化为行显示断点位置.
    • 多种指定方式,模糊到精确,简单到复杂.
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值