【问题标题】:How can I detect breakpoints in GDB command files and stop the execution?如何检测 GDB 命令文件中的断点并停止执行?
【发布时间】:2020-01-11 23:06:42
【问题描述】:

我正在尝试使用 GDB 命令文件实现单步执行。我想在遇到任何断点时停止执行命令文件。

有没有办法检测断点被命中?以及之后如何停止执行命令文件。

我检查了文档,似乎我只能对某些断点使用中断命令。但不适用于随机断点。

我有这样的事情:

printf "single stepping\n"
set $steps_count = 0
     while ($steps_count < 5)
         set $steps_count = $steps_count + 1
          printf "program counter 0x%x\n", $pc
         printf "next #%d\n", $steps_count
         next
     end

这就是我想要实现的目标:

printf "single stepping\n"
set $steps_count = 0
     while (# breakpoint is not hit)
         set $steps_count = $steps_count + 1
          printf "program counter 0x%x\n", $pc
         printf "next #%d\n", $steps_count
         next
     end
# end the execution of the command file.

更新: 我尝试使用以下方法:

# set the end breakpoint
break *0x199e
commands
    stop 1 
    quit
end

break *0x1980
# set a certain once we encounter the first breakpoint
commands
    set $count = 0
    while $count < 5
        printf "#PC: 0x%x", $pc
        set $count = $count + 1
        stepi
    end
end

一旦命中第一个断点,这应该开始记录程序计数器。但我不知道为什么这个 break 命令不起作用。一旦断点被​​击中,程序就会停止并且不会进入while循环!

更新: 根据 gdb 文档:

命令列表中的任何其他命令,在恢复执行的命令之后,都将被忽略。这是因为任何时候你恢复执行(即使是简单的下一步或步骤),你都可能遇到另一个断点——它可能有自己的命令列表,导致执行哪个列表的歧义。

所以我认为在中断命令中放置一个具有 stepnext 的循环不是一个好主意。

【问题讨论】:

  • 对于更新,您能否分享您获得的 gdb 输出(可能还有版本)?
  • @ChristianB 我用我在文档中找到的内容更新了这个问题。谢谢
  • 似乎中断命令没有在我的命令文件中执行。我已经在另一个问题中问过这个问题。 stackoverflow.com/questions/57988776/…

标签: debugging gdb


【解决方案1】:

尝试类似:

commands 1-10 # for breakpoints number 1 through 10
set $breakpoint_hit = 1
end

然后在你的循环中检查它。

【讨论】:

  • 我更新了问题。你能发现任何错误吗?
猜你喜欢
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多