6

In a gdb python script, how do I set or modify the commands list for a breakpoint?

I can do this trivially by hand. For example to change the commands list for breakpoint 2, at the prompt I can enter:

(gdb) commands 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>info reg rax
>set $rax=0x1234
>end

Yet, from a gdb python script I cannot seem to execute a multiline command.

gdb.execute("commands 2\ninfo reg rax\nset $rax=0x1234\nend\n")

Just gets me an output like

(gdb) source blah.py
>

and it is sitting there for more input. It won't move on until I type end and press enter. Then it just gives complaints making it clear it has not correctly parsed anything after the commands 2 of that string.

Trying to input each line separately doesn't help. For example the script:

gdb.execute("commands 2")
gdb.execute("info reg rax")
gdb.execute("set $rax=0x1234")
gdb.execute("end")

waits for more input from the user during the first execute, and so has similar problems. And while mostly wishful, the following doesn't work either:

gdb.execute(["commands 2","info reg rax","set $rax=0x1234","end"])

It is easy to programatically get the list of breakpoints with gdb.breakpoints(). And these objects have a property commands, which I can see any commands I set by hand or from a native gdb script. However if I try to modify this property, it fails. So once something is set by hand, or a gdb script, there appears to be no way for a python script to edit it. The API document is missing a lot of helpful information, but it does state https://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python Variable: Breakpoint.commands ... This attribute is not writable.

And no, I don't consider it a useful answer to "never use gdb scripts" or "never enter commands by hand" and "instead always write a python gdb.Breakpoint subclass with Breakpoint.stop() set to do something special, and rewrite all existing scripts to use such features". I can only seem to find information for that workaround. But I'm not willing to give up current methods of interaction just due to a gdb quirk.

Since I can easily run commands to do what I want by hand, there must be a way to modify or set the breakpoint commands from a gdb python script. How do I do this?

1
  • Hacky possibility: It occurs to me I could have python write out a native gdb script to disk and have it execute that. Please tell me this interface isn't so broken that I have to resort to that. Commented Jan 18, 2018 at 2:34

1 Answer 1

2

I don't think there is a way at present.

As you found, gdb.execute doesn't support multiple lines. This is just a limitation in the Python layer that nobody ever fixed. (I didn't see a bug for it so I filed bug 22730.

Also, a breakpoint's commands field is not assignable. I filed bug 22731 for this.

Your source idea, while horrible, would work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please update the answer to say "until GDB 8.2..." and then provide the two samples how to do this wth GDB 8.2 (once with the br.commands and once with the muliline one)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.