GDB配置(打印STL容器、VS code配置、远程调试debug)


0.前言

  Linux开发必须要把gdb玩的666呀,建议先阅读下《C++ vector STL实现详解》。gdb玩的666可以摆脱调试中各种使用std::cout打印输出变量的恶习,尤其对于大型项目,每次调试都需要增加std::cout,单单编译时长都能让你发狂,严重影响开发效率。

1.gdb定义打印函数

  gdb原生支持定义打印函数,了解类型后,可以自行定制打印函数。目前网上有提供针对stl标准容器的现成文件.gdbinit,该文件会定义string、vector、map、set、deque、priority_queue等等,需要可以直接下载这里, 把文件保存为.gdbinit, 放在用户目录下~/.gdbinit,每次执行gdb调试会自动加载.gdbinit初始文件。

文件中vector定义如下

define pvector
    if $argc == 0
        help pvector
    else
        set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start
        set $capacity = $arg0._M_impl._M_end_of_storage - $arg0._M_impl._M_start
        set $size_max = $size - 1
    end
    if $argc == 1
        set $i = 0
        while $i < $size
            printf "elem[%u]: ", $i
            p *($arg0._M_impl._M_start + $i)
            set $i++
        end
    end
    if $argc == 2
        set $idx = $arg1
        if $idx < 0 || $idx > $size_max
            printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
        else
            printf "elem[%u]: ", $idx
            p *($arg0._M_impl._M_start + $idx)
        end
    end
    if $argc == 3
      set $start_idx = $arg1
      set $stop_idx = $arg2
      if $start_idx > $stop_idx
        set $tmp_idx = $start_idx
        set $start_idx = $stop_idx
        set $stop_idx = $tmp_idx
      end
      if $start_idx < 
  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值