如何在python代码中识别特定的GDB断点



我正在尝试用python编写GDB脚本。我有一个GDB的原生脚本文件,它源于一个python脚本文件。在.gdb文件中,我在不同的函数上声明了一些断点。我可以使用python脚本在这些断点上执行next/step/contence,并打印不同的变量。但我有一个独特的python函数,用于每个具有特定打印的断点。我想把它做得更好、更全面。

我想要的是在python代码中有一个函数,以及一种识别命中哪个断点的方法,这样我就可以根据断点打印不同的变量。如果我只是打印它们,那么我会得到范围外的错误。

我已经检查过了,GDB还允许通过在python代码中定义断点来绕过python中的断点。

有没有其他方法可以完成这项任务(将断点定义排除在python代码之外),或者使用gdbBreakpoint类是唯一的方法?我想要的只是一个检查,它可以帮助识别它是哪个断点

感谢

有没有其他方法可以完成这项任务(将断点定义排除在python代码之外)我想要的只是一个检查,它可以帮助识别它是哪个断点

至少在gdb 7.6中是可能的。请看我答案中的第一个例子。您还可以在python脚本中设置所有断点,如第二个示例所示。但首先,这是一个测试C++程序,将在示例中使用。

>cat main.cpp
int a()
{
  int p = 0;
  p = p +1;
  return  p;
}
int b()
{
  return a();
}
int c()
{
  return a();
}
int main()
{
  c();
  b();
  a();
  return 0;
}

第一个例子-本地gdb脚本中的断点和另一个脚本中的python函数。我使用事件(Python中的事件):

因此,这是一个带有一些断点的本地脚本:

>cat my_check.gdb
b main.cpp:20
b main.cpp:21
b b
source my_check.py
r
q

这是一个python脚本my_check.py:

def my_breakpoint_handler (event):
  if (isinstance(event, gdb.BreakpointEvent)):
    print event.breakpoint.location
    if event.breakpoint.location == "b":
        gdb.write("Breakpoint in b()n")
        gdb.execute("bt")
    elif event.breakpoint.location == "main.cpp:20":
        gdb.write("Breakpoint in main.cpp:20n")
        gdb.execute("info frame")
    elif event.breakpoint.location == "main.21":
        gdb.write("Breakpoint in main.cpp:21n")
        gdb.write("some info")
    else:
      pass
    gdb.execute("c")
gdb.events.stop.connect(my_breakpoint_handler)

这就是测试本身:

>gdb -q -x my_check.gdb a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.breakpoint/a.out...done.
Breakpoint 1 at 0x40056a: file main.cpp, line 20.
Breakpoint 2 at 0x40056f: file main.cpp, line 21.
Breakpoint 3 at 0x400554: file main.cpp, line 10.
Breakpoint 1, main () at main.cpp:20
20        c();
main.cpp:20
Breakpoint in main.cpp:20
Stack level 0, frame at 0x7fffffffe0d0:
 rip = 0x40056a in main (main.cpp:20); saved rip 0x3c4121ecdd
 source language c++.
 Arglist at 0x7fffffffe0c0, args:
 Locals at 0x7fffffffe0c0, Previous frame's sp is 0x7fffffffe0d0
 Saved registers:
  rbp at 0x7fffffffe0c0, rip at 0x7fffffffe0c8
Breakpoint 2, main () at main.cpp:21
21        b();
main.cpp:21
Breakpoint 3, b () at main.cpp:10
10        return a();
b
Breakpoint in b()
#0  b () at main.cpp:10
#1  0x0000000000400574 in main () at main.cpp:21
[Inferior 1 (process 20798) exited normally]

第二个例子-python脚本中的所有断点和python函数。

这是一个python gdb脚本:

>cat my_check2.py
class MyBreakpoint (gdb.Breakpoint):
        def stop (self):
          print self.location
          if self.location == "b":
            gdb.write("Breakpoint in b()n")
            gdb.execute("bt")
          elif self.location == "main.cpp:20":
            gdb.write("Breakpoint in main.cpp:20n")
            gdb.execute("info frame")
          elif self.location == "main.21":
            gdb.write("Breakpoint in main.cpp:21n")
          return False

MyBreakpoint("main.cpp:20")
MyBreakpoint("main.cpp:21")
MyBreakpoint("b")
gdb.execute("r")
gdb.execute("q")

这里使用的是:

>gdb -q -x my_check2.py a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.breakpoint/a.out...done.
Breakpoint 1 at 0x40056a: file main.cpp, line 20.
Breakpoint 2 at 0x40056f: file main.cpp, line 21.
Breakpoint 3 at 0x400554: file main.cpp, line 10.
main.cpp:20
Breakpoint in main.cpp:20
Stack level 0, frame at 0x7fffffffe0d0:
 rip = 0x40056a in main (main.cpp:20); saved rip 0x3c4121ecdd
 source language c++.
 Arglist at 0x7fffffffe0c0, args:
 Locals at 0x7fffffffe0c0, Previous frame's sp is 0x7fffffffe0d0
 Saved registers:
  rbp at 0x7fffffffe0c0, rip at 0x7fffffffe0c8
main.cpp:21
b
Breakpoint in b()
#0  b () at main.cpp:10
#1  0x0000000000400574 in main () at main.cpp:21
[Inferior 1 (process 27434) exited normally]

最新更新