Reputation: 79487
I'm experimenting with python-interactive mode in gdb, and I can't figure out how to change a variable from inside it. I know how to do it without python - set variable a = 10
.
I'm using this test program:
#include <stdio.h>
int main(int argc, char *argv[]) {
int a;
printf("Enter a: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
}
I've placed a breakpoint after the scanf()
, and when it's hit I enter python interactive mode. Now I want to change the variable a
to some other value. I tried using a = 10
, but it wasn't changed, and the same value I entered in the scanf()
(in this case it's 5) was printed instead.
(gdb) b main.c:6 Breakpoint 1 at 0x8048503: file main.c, line 6. (gdb) r Starting program: /home/sashoalm/Desktop/test/a.out Enter a: 5 Breakpoint 1, main (argc=1, argv=0xbffff1d4 "\214\363\377\277") at main.c:6 6 printf("You entered: %d\n", a); (gdb) python-interactive >>> a = 10 >>> (gdb) c Continuing. You entered: 5 [Inferior 1 (process 26133) exited normally]
So what is the correct way to do it?
Upvotes: 2
Views: 1126
Reputation: 79487
After some searching through the Python API documentation I found the answer. I needed to use gdb.execute('set var a = 10')
, which allows python scripts to execute gdb commands, and the commands are evaluated as if the user has written them.
I used this code to read a
, add 5 to it and then set it:
symbol = gdb.lookup_symbol('a')[0] frame = gdb.selected_frame() value = symbol.value(frame) gdb.execute('set var a = %d' % (int(value)+5))
Upvotes: 3