Next: Exception Handling, Up: Python API
At startup, gdb overrides Python's sys.stdout
and
sys.stderr
to print using gdb's output-paging streams.
A Python program which outputs to one of these streams may have its
output interrupted by the user (see Screen Size). In this
situation, a Python KeyboardInterrupt
exception is thrown.
Some care must be taken when writing Python code to run in gdb. Two things worth noting in particular:
SIGCHLD
and SIGINT
.
Python code must not override these, or even change the options using
sigaction
. If your program changes the handling of these
signals, gdb will most likely stop working correctly. Note
that it is unfortunately common for GUI toolkits to install a
SIGCHLD
handler.
gdb introduces a new Python module, named gdb
. All
methods and classes added by gdb are placed in this module.
gdb automatically import
s the gdb
module for
use in all scripts evaluated by the python
command.
Evaluate command, a string, as a gdb CLI command. If a GDB exception happens while command runs, it is translated as described in Exception Handling.
The from_tty flag specifies whether gdb ought to consider this command as having originated from the user invoking it interactively. It must be a boolean value. If omitted, it defaults to
False
.By default, any output produced by command is sent to gdb's standard output (and to the log output if logging is turned on). If the to_string parameter is
True
, then output will be collected bygdb.execute
and returned as a string. The default isFalse
, in which case the return value isNone
. If to_string isTrue
, the gdb virtual terminal will be temporarily set to unlimited width and height, and its pagination will be disabled; see Screen Size.
Return a sequence holding all of gdb's breakpoints. See Breakpoints In Python, for more information.
Return the value of a gdb parameter given by its name, a string; the parameter name string may contain spaces if the parameter has a multi-part name. For example, ‘print object’ is a valid parameter name.
If the named parameter does not exist, this function throws a
gdb.error
(see Exception Handling). Otherwise, the parameter's value is converted to a Python value of the appropriate type, and returned.
Return a value from gdb's value history (see Value History). The number argument indicates which history element to return. If number is negative, then gdb will take its absolute value and count backward from the last element (i.e., the most recent element) to find the value to return. If number is zero, then gdb will return the most recent element. If the element specified by number doesn't exist in the value history, a
gdb.error
exception will be raised.If no exception is raised, the return value is always an instance of
gdb.Value
(see Values From Inferior).
Parse expression, which must be a string, as an expression in the current language, evaluate it, and return the result as a
gdb.Value
.This function can be useful when implementing a new command (see Commands In Python), as it provides a way to parse the command's argument as an expression. It is also useful simply to compute values, for example, it is the only way to get the value of a convenience variable (see Convenience Vars) as a
gdb.Value
.
Return the
gdb.Symtab_and_line
object corresponding to the pc value. See Symbol Tables In Python. If an invalid value of pc is passed as an argument, then thesymtab
andline
attributes of the returnedgdb.Symtab_and_line
object will beNone
and 0 respectively.
Put event, a callable object taking no arguments, into gdb's internal event queue. This callable will be invoked at some later point, during gdb's event processing. Events posted using
post_event
will be run in the order in which they were posted; however, there is no way to know when they will be processed relative to other events inside gdb.gdb is not thread-safe. If your Python program uses multiple threads, you must be careful to only call gdb-specific functions in the gdb thread.
post_event
ensures this. For example:(gdb) python >import threading > >class Writer(): > def __init__(self, message): > self.message = message; > def __call__(self): > gdb.write(self.message) > >class MyThread1 (threading.Thread): > def run (self): > gdb.post_event(Writer("Hello ")) > >class MyThread2 (threading.Thread): > def run (self): > gdb.post_event(Writer("World\n")) > >MyThread1().start() >MyThread2().start() >end (gdb) Hello World
Print a string to gdb's paginated output stream. The optional stream determines the stream to print to. The default stream is gdb's standard output stream. Possible stream values are:
gdb.STDOUT
- gdb's standard output stream.
gdb.STDERR
- gdb's standard error stream.
gdb.STDLOG
- gdb's log stream (see Logging Output).
Writing to
sys.stdout
orsys.stderr
will automatically call this function and will automatically direct the output to the relevant stream.
Flush the buffer of a gdb paginated stream so that the contents are displayed immediately. gdb will flush the contents of a stream automatically when it encounters a newline in the buffer. The optional stream determines the stream to flush. The default stream is gdb's standard output stream. Possible stream values are:
gdb.STDOUT
- gdb's standard output stream.
gdb.STDERR
- gdb's standard error stream.
gdb.STDLOG
- gdb's log stream (see Logging Output).
Flushing
sys.stdout
orsys.stderr
will automatically call this function for the relevant stream.
Return the name of the current target character set (see Character Sets). This differs from
gdb.parameter('target-charset')
in that ‘auto’ is never returned.
Return the name of the current target wide character set (see Character Sets). This differs from
gdb.parameter('target-wide-charset')
in that ‘auto’ is never returned.
Return the name of the shared library holding the given address as a string, or
None
.
Return locations of the line specified by expression, or of the current line if no argument was given. This function returns a Python tuple containing two elements. The first element contains a string holding any unparsed section of expression (or
None
if the expression has been fully parsed). The second element contains eitherNone
or another tuple that contains all the locations that match the expression represented asgdb.Symtab_and_line
objects (see Symbol Tables In Python). If expression is provided, it is decoded the way that gdb's inbuiltbreak
oredit
commands do (see Specify Location).
If prompt_hook is callable, gdb will call the method assigned to this operation before a prompt is displayed by gdb.
The parameter
current_prompt
contains the current gdb prompt. This method must return a Python string, orNone
. If a string is returned, the gdb prompt will be set to that string. IfNone
is returned, gdb will continue to use the current prompt.Some prompts cannot be substituted in gdb. Secondary prompts such as those used by readline for command input, and annotation related prompts are prohibited from being changed.