3

I want to use python script in gdb, but I have some questions, how can I let these commands result redirected to my python script?

I mean, when I use "info f", in gdb, it will print the infomation about the ebp, eip infomation... For now I want to let these infomation do not show on the screen, but to redirect to variable.

For example, in my python script, there is a string called "str1", so I want str1=gdb.command("info f"), I try some ways to do, but the infomation will show on the screen, how can I remove it from screen, just store it in a string ?

1
  • Does someone know the way to do it? Thanks!!!!! This is a assignment, I must use gdb, and python script. Commented Jan 7, 2012 at 23:18

2 Answers 2

2

Here is an example:

(gdb) info frame
Stack level 0, frame at 0x7fffffffd960:
 rip = 0x7ffff7dec680 in *__GI__dl_debug_state (dl-debug.c:77); saved rip 0x7ffff7de0731
 called by frame at 0x7fffffffdab0
 source language c.
 Arglist at 0x7fffffffd950, args: 
 Locals at 0x7fffffffd950, Previous frame's sp is 0x7fffffffd960
 Saved registers:
  rip at 0x7fffffffd958

(gdb) python str1 = gdb.execute("info frame", False, True)
(gdb) python print str1
Stack level 0, frame at 0x7fffffffd960:
 rip = 0x7ffff7dec680 in *__GI__dl_debug_state (dl-debug.c:77); saved rip 0x7ffff7de0731
 called by frame at 0x7fffffffdab0
 source language c.
 Arglist at 0x7fffffffd950, args: 
 Locals at 0x7fffffffd950, Previous frame's sp is 0x7fffffffd960
 Saved registers:
  rip at 0x7fffffffd958

Documentation here.

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

1 Comment

HOHO, that's what I want!!! So, how about the "Flase" and "True" in the function? Thanks!!
1

gdb.execute (command [, from_tty [, to_string]]) is what're you( we :D ) seeking for. You may look at a function decription. The first argument is quoted GDB command, the second I didn't really understood, I suppose because I don't need it, just keep it false. And the third determines where a command output will be streamed. If false(default), it's just going to be printed, if true, output will be wrapped as a string and return to you, so you may assign it to a variable.

An example:

(gdb) py MyVar = gdb.execute("info f",False,True)
(gdb) py print(MyVar)
Stack level 0, frame at 0x7fffffffdda0:
 rip = 0x4006a6 in open@plt; saved rip 0x4007b9
 called by frame at 0x7fffffffddc0
 Arglist at 0x7fffffffdd90, args: 
 Locals at 0x7fffffffdd90, Previous frame's sp is 0x7fffffffdda0
 Saved registers:
  rip at 0x7fffffffdd98

(gdb) 

You may also be interested in gdb.parse_and_eval (expression). It gives an easy way to use so called convinience variables of GDB:

(gdb) set $MyVar = "PinkyPie is the best!"
(gdb) pi
>>> MyStr = gdb.parse_and_eval("$MyVar")
>>> print(MyStr)
"PinkyPie is the best!"
>>> 
(gdb) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.