Re: Timer

Phil Muldoon
2013-05-07T13:28:14Z

On 06/05/13 10:52, Marc BrÃ¼nink wrote:
> I want to execute a piece of code at regular intervals. Actually I'm sampling $pc.
> (let's not go into detail why I use gdb)
>
> My current solution just starts another process that sends a SIGTRAP to the debugged application. Using a simple script I can print the $pc.
>
> However, I just realised that this approach does not work too well. If gdb is stopped due to a breakpoint it will interpret the received SIGTRAP as another hit of the very same breakpoint.
>
> Reproduce:
> 1. Attach to any program
> 2. Create any breakpoint
> 3. Wait until breakpoint is hit
> 5. continue

You can a little of this right now with the GDB Python API by writing a
breakpoint in Python that implements the "stop" callback. I know you
mentioned only within the confines of GDB, but the Python API is
provided as standard (of course, you have to have Python installed on
the system).

From:

http://sourceware.org/gdb/current/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python

class MyBreakpoint(gdb.Breakpoint):
def stop(self):
pc = gdb.parse_and_eval("(long) $pc")
print "0x"+long(pc).__format__("x")
return False

b = MyBreakpoint("foo.c:42")

In the "stop" callback, if the callback returns "False", the inferior
is continued (if "True", the inferior is stopped). If you wanted to
make the breakpoint depend on the state of the inferior, you can use
the Breakpoint.condition string to insert a condition.

> 4. Send SIGTRAP to debugged application

But I am not sure what this relates to a timer event, so I am probably
not fully understanding what you want. There are two other areas of
the Python API which may interest you:

Events in GDB/Python:

http://sourceware.org/gdb/current/onlinedocs/gdb/Events-In-Python.html#Events-In-Python

And inserting objects into GDB's event loop

gdb.post_event()

http://sourceware.org/gdb/current/onlinedocs/gdb/Basic-Python.html#Basic-Python

Unfortunately post_event() is not deterministic. The callable object
is called when GDB gets around to processing that event in the queue.
There is no way to arbitrarily run an event according to a timer, at
least in the Python API; it is event based only (right now).

Cheers,

Phil