Next: Threads In Python, Previous: Inferiors In Python, Up: Python API
gdb provides a general event facility so that Python code can be notified of various state changes, particularly changes that occur in the inferior.
An event is just an object that describes some state change. The type of the object and its attributes will vary depending on the details of the change. All the existing events are described below.
In order to be notified of an event, you must register an event handler
with an event registry. An event registry is an object in the
gdb.events module which dispatches particular events. A registry
provides methods to register and unregister event handlers:
Add the given callable object to the registry. This object will be called when an event corresponding to this registry occurs.
Remove the given object from the registry. Once removed, the object will no longer receive notifications of events.
Here is an example:
def exit_handler (event):
print ("event type: exit")
if hasattr (event, 'exit_code'):
print ("exit code: %d" % (event.exit_code))
else:
print ("exit code not available")
gdb.events.exited.connect (exit_handler)
In the above example we connect our handler exit_handler to the
registry events.exited. Once connected, exit_handler gets
called when the inferior exits. The argument event in this example is
of type gdb.ExitedEvent. As you can see in the example the
ExitedEvent object has an attribute which indicates the exit code of
the inferior.
Some events can be thread specific when gdb is running in
non-stop mode. When represented in Python, these events all extend
gdb.ThreadEvent. This event is a base class and is never
emitted directly; instead, events which are emitted by this or other
modules might extend this event. Examples of these events are
gdb.BreakpointEvent and gdb.ContinueEvent.
gdb.ThreadEvent holds the following attributes:
In non-stop mode this attribute will be set to the specific thread which was involved in the emitted event. Otherwise, it will be set to
None.
The following is a listing of the event registries that are available and details of the events they emit:
events.contgdb.ContinueEvent, which extends gdb.ThreadEvent.
This event indicates that the inferior has been continued after a
stop. For inherited attribute refer to gdb.ThreadEvent above.
events.exitedevents.ExitedEvent, which indicates that the inferior has
exited. events.ExitedEvent has two attributes:
An integer representing the exit code, if available, which the inferior has returned. (The exit code could be unavailable if, for example, gdb detaches from the inferior.) If the exit code is unavailable, the attribute does not exist.
events.stopgdb.StopEvent, which extends gdb.ThreadEvent.
Indicates that the inferior has stopped. All events emitted by this
registry extend gdb.StopEvent. As a child of
gdb.ThreadEvent, gdb.StopEvent will indicate the stopped
thread when gdb is running in non-stop mode. Refer to
gdb.ThreadEvent above for more details.
Emits gdb.SignalEvent, which extends gdb.StopEvent.
This event indicates that the inferior or one of its threads has
received a signal. gdb.SignalEvent has the following
attributes:
A string representing the signal received by the inferior. A list of possible signal values can be obtained by running the command
info signalsin the gdb command prompt.
Also emits gdb.BreakpointEvent, which extends
gdb.StopEvent.
gdb.BreakpointEvent event indicates that one or more breakpoints have
been hit, and has the following attributes:
A sequence containing references to all the breakpoints (type
gdb.Breakpoint) that were hit. See Breakpoints In Python, for details of thegdb.Breakpointobject.
A reference to the first breakpoint that was hit. This attribute is maintained for backward compatibility and is now deprecated in favor of the
gdb.BreakpointEvent.breakpointsattribute.
events.new_objfilegdb.NewObjFileEvent which indicates that a new object file has
been loaded by gdb. gdb.NewObjFileEvent has one attribute:
A reference to the object file (
gdb.Objfile) which has been loaded. See Objfiles In Python, for details of thegdb.Objfileobject.
events.free_objfilegdb.FreeObjFileEvent which indicates that an object file
is about to be removed from gdb. One reason this can happen
is when the inferior calls dlclose.
gdb.FreeObjFileEvent has one attribute:
A reference to the object file (
gdb.Objfile) which will be unloaded. See Objfiles In Python, for details of thegdb.Objfileobject.
events.clear_objfilesgdb.ClearObjFilesEvent which indicates that the list of object
files for a program space has been reset.
gdb.ClearObjFilesEvent has one attribute:
A reference to the program space (
gdb.Progspace) whose objfile list has been cleared. See Progspaces In Python.
events.inferior_callgdb.InferiorCallPreEvent, and after an inferior call,
this emits an event of type gdb.InferiorCallPostEvent.
gdb.InferiorCallPreEventgdb.InferiorCallPostEventevents.memory_changedgdb.MemoryChangedEvent which indicates that the memory of the
inferior has been modified by the gdb user, for instance via a
command like set *addr = value. The event has the following
attributes:
events.register_changedgdb.RegisterChangedEvent which indicates that a register in the
inferior has been modified by the gdb user.
A gdb.Frame object representing the frame in which the register was modified.
events.breakpoint_createdgdb.Breakpoint object.
events.breakpoint_modifiedgdb.Breakpoint object.
events.breakpoint_deletedgdb.Breakpoint object. When this event is
emitted, the gdb.Breakpoint object will already be in its
invalid state; that is, the is_valid method will return
False.
events.before_promptevents.new_inferiorThe event is of type gdb.NewInferiorEvent. This has a single
attribute:
events.inferior_deletedremove-inferiors.
The event is of type gdb.InferiorDeletedEvent. This has a single
attribute:
The inferior that is being removed, a
gdb.Inferiorobject.
events.new_threadgdb.NewThreadEvent, which extends gdb.ThreadEvent.
This has a single attribute:
events.gdb_exitinggdb.GdbExitingEvent,
which has a single attribute:
events.connection_removedgdb.ConnectionEvent. This has a single read-only attribute: