This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Some help required with gdb-python scripting
- From: Yogesh Mundada <yogesh dot mundada at gmail dot com>
- To: gdb at sourceware dot org
- Date: Sat, 27 Mar 2010 07:59:45 -0400
- Subject: Some help required with gdb-python scripting
Hi,
Sorry about this lengthy email.
I am writing a few python functions to collect some information about
breakpoints execution. I am putting two break points in the object
file and want my functions to get called as these breakpoints are
reached to collect some information. I am running the latest source
code(03/26) from archer-tromey-python branch.
My python functions are defined as follows:
-----------------
#!/home/yogesh/build/gdb/gdb -P
import gdb
import pdb
brk_340 = 0
brk_379 = 0
class call_profiler (gdb.Function):
"""Profiles calls"""
def __init__ (self):
super (call_profiler, self).__init__ ("call_profiler")
def invoke (self):
#pdb.set_trace()
global brk_340
global brk_379
fr = gdb.selected_frame()
fisa = fr.find_sal()
if '340' in str(fisa.line):
brk_340 += 1
elif '379' in str(fisa.line):
brk_379 += 1
else:
print "In some weird break point"
return True
class print_variables(gdb.Function):
"""Prints brk_340, brk_379"""
def __init__(self):
super (print_variables, self).__init__ ("print_variables")
def invoke(self):
global brk_340
global brk_379
print "Break at 340: %d, Break at 379: %d" % (brk_340, brk_379)
return True
call_profiler()
print_variables()
----------------
I am starting the debugging as follows:
#gdb
(gdb) file vmlinux
Reading symbols from /mnt/tmp/yogesh/linux-2.6.24-yhm-custom/vmlinux...done.
(gdb) source /home/yogesh/profile_call.py
(gdb) b arch/x86/kernel/entry_32.S:340
Breakpoint 1 at 0xc01043bb: file arch/x86/kernel/entry_32.S, line 340.
(gdb) b arch/x86/kernel/entry_32.S:379
Breakpoint 2 at 0xc010443b: file arch/x86/kernel/entry_32.S, line 379.
(gdb) commands 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>silent
>call $call_profiler()
>continue
>end
(gdb) commands 2
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
>silent
>call $call_profiler()
>continue
>end
(gdb) target remote localhost:7732
(gdb) c
Continuing.
-------------------------------
This works until it asks me to press enter because the screen is full
of output from 'return 1' statements in the call_profiler().
----------------------------
$2 = 1
$3 = 1
..
..
$45 = 1
$46 = 1
---Type <return> to continue, or q <return> to quit---
----------------------------
Thus, I have to press enter after the breakpoint is reached every 45
times or so. I tried to return nothing in the call_profiler(), but
then the python interpreter gives an error.
My questions are:
1. Is there any way to disable this 'more' type editor feature after
every few lines?
2. How can I examine value of eax register in the function call_profiler()?
I tried - fr.read_var('%eax'), fr.read_var('$eax') and
fr.read_var('eax') - but all of them give me an error saying that the
variable not found.
3. Is there any way to stop execution and give control back to the
user if some condition is true?
Something like:
if eax == 1:
gdb.cli()
I see that gdb has a cli built-in method. But if I call it, I get
following error:
(Pdb) gdb.cli()
*** RuntimeError: cannot invoke CLI recursively
4. Is there any way to make this process completely automated without
me ever requiring to set breakpoints or put commands for breakpoints?
I think I can import object file and set break points as follows in
the script:
gdb.execute("file
/mnt/tmp/yogesh/vmware-ubuntu/linux-2.6.24-yhm-custom/vmlinux")
gdb.execute("b arch/x86/kernel/entry_32.S:340")
gdb.execute("b arch/x86/kernel/entry_32.S:379")
gdb.execute("target remote localhost:7732")
But, after that, how do I set commands to be executed for breakpoints
1 and 2? If I just do gdb.execute("commands 1"), that starts some
scripting prompt.
-Yogesh