質問

I am trying to script GDB with python. I have a GDB's native script file which sources a python script file. In the .gdb file I am declaring some breakpoints on different functions. I am able to perform next/step/continue over these breakpoints using the python script and print different variables. But I have a unique python function for each breakpoint with specific prints.I want to make it better and more general.

What I want is to have one function in python code and a way to identify which breakpoint is hit so that I can print different variables depending on the breakpoint. If I just print them anyway then I will get the out of scope errors.

I have checked and GDB also allows playing around with breakpoints in python by defining breakpoints in python code as mentioned here.

Is there another way to do this task (keeping breakpoint definitions out of python code) or is using gdb Breakpoint class the only way ? All I want is a check which can help identify which breakpoint it is.

Thanks

役に立ちましたか?

解決

Is there another way to do this task (keeping breakpoint definitions out of python code) All I want is a check which can help identify which breakpoint it is.

It is possible at least with gdb 7.6. Please, see the first example in my answer. You can also set all your breakpoints right in a python script and it is shown in the second example. But first, this is a test C++ program, that will be used in the examples.

>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;
}

First example - breakpoints in a native gdb script and python function in another script. I use events (Events-In-Python):

So this is a native script with some breakpoints:

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

And this is a python script 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:20\n")
        gdb.execute("info frame")
    elif event.breakpoint.location == "main.21":
        gdb.write("Breakpoint in main.cpp:21\n")
        gdb.write("some info")
    else:
      pass

    gdb.execute("c")

gdb.events.stop.connect(my_breakpoint_handler)

And this is test itself:

>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]

Second example - all breakpoints and python function in python script.

This is a python gdb script:

>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:20\n")
            gdb.execute("info frame")
          elif self.location == "main.21":
            gdb.write("Breakpoint in main.cpp:21\n")
          return False



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

And here it is used:

>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]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top