如何检查断点是否按特定顺序命中?

时间:2019-09-10 00:08:52

标签: gdb

我正在尝试建立一个GDB脚本,该脚本设置一些断点,并确保按一定顺序命中它们,如果没有抛出错误。

它看起来像:

break *0x400de0    # Should be hit first
break *0x40f650    # Should be hit second
break *0x40f662    # Should be hit third
run
# hits breakpoint #
if (??? == "0x400de0")
   continue
   # hits breakpoint #
   if (??? == "0x40f650")
      continue
      # hits breakpoint #
      if (??? == "0x40f662")
          print "The correct thing happened!"
          # Do some things here....
          quit
      else
          print "ERROR"
          quit
    else
      print "ERROR"
      quit
else
    print "ERROR"
    quit

但是,我有点想将断点处的地址放入变量中。我已经研究过使用frame来打印当前地址,但是我不知道如何将其放入变量进行比较。

我已经研究过使用Python-GDB脚本执行此操作,但是,对于我的应用程序来说似乎有点复杂。如果它是唯一的选择,我将使用它。

1 个答案:

答案 0 :(得分:2)

这可以使用状态机来完成。

set $state = 0
break *0x400de0
commands
  if $state == 0
    set $state = 1
  else
    printf "error, state is %d expected 0\n", $state
  end
end
break *0x40f650
commands
  if $state == 1
    set $state = 2
  else
    printf "error, state is %d expected 1\n", $state
  end
end
break *0x40f662
commands
  if $state == 2
    printf "done\n"
  else
    printf "error, state is %d expected 2\n", $state
  end
end

run