You can extend @value{GDBN} using the @uref{http://www.python.org/,
Python programming language}. This feature is available only if
@value{GDBN} was configured using @option{--with-python}.
+@value{GDBN} can be built against either Python 2 or Python 3; which
+one you have depends on this configure-time option.
@cindex python directory
Python scripts used by @value{GDBN} should be installed in
The script name must end with @samp{.py} and @value{GDBN} must be configured
to recognize the script language based on filename extension using
the @code{script-extension} setting. @xref{Extending GDB, ,Extending GDB}.
-
-@item python execfile ("script-name")
-This method is based on the @code{execfile} Python built-in function,
-and thus is always available.
@end table
@node Python API
@value{GDBN} automatically @code{import}s the @code{gdb} module for
use in all scripts evaluated by the @code{python} command.
+Some types of the @code{gdb} module come with a textual representation
+(accessible through the @code{repr} or @code{str} functions). These are
+offered for debugging purposes only, expect them to change over time.
+
@findex gdb.PYTHONDIR
@defvar gdb.PYTHONDIR
A string containing the python directory (@pxref{Python}).
@end defun
@findex gdb.decode_line
-@defun gdb.decode_line @r{[}expression@r{]}
+@defun gdb.decode_line (@r{[}expression@r{]})
Return locations of the line specified by @var{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
@end defvar
@defvar Value.dynamic_type
-The dynamic type of this @code{gdb.Value}. This uses C@t{++} run-time
-type information (@acronym{RTTI}) to determine the dynamic type of the
-value. If this value is of class type, it will return the class in
-which the value is embedded, if any. If this value is of pointer or
-reference to a class type, it will compute the dynamic type of the
-referenced object, and return a pointer or reference to that type,
-respectively. In all other cases, it will return the value's static
-type.
+The dynamic type of this @code{gdb.Value}. This uses the object's
+virtual table and the C@t{++} run-time type information
+(@acronym{RTTI}) to determine the dynamic type of the value. If this
+value is of class type, it will return the class in which the value is
+embedded, if any. If this value is of pointer or reference to a class
+type, it will compute the dynamic type of the referenced object, and
+return a pointer or reference to that type, respectively. In all
+other cases, it will return the value's static type.
Note that this feature will only work when debugging a C@t{++} program
that includes @acronym{RTTI} for the object in question. Otherwise,
@subsubsection Pretty Printing API
@cindex python pretty printing api
-An example output is provided (@pxref{Pretty Printing}).
-
A pretty-printer is just an object that holds a value and implements a
-specific interface, defined here.
+specific interface, defined here. An example output is provided
+(@pxref{Pretty Printing}).
@defun pretty_printer.children (self)
@value{GDBN} will call this method on a pretty-printer to compute the
unwinders in Python uses this object to return frame's ID and previous
frame registers when @value{GDBN} core asks for them.
+An unwinder should do as little work as possible. Some otherwise
+innocuous operations can cause problems (even crashes, as this code is
+not not well-hardened yet). For example, making an inferior call from
+an unwinder is unadvisable, as an inferior call will reset
+@value{GDBN}'s stack unwinding process, potentially causing re-entrant
+unwinding.
+
@subheading Unwinder Input
An object passed to an unwinder (a @code{gdb.PendingFrame} instance)
provides a method to read frame's registers:
@defun PendingFrame.read_register (reg)
-This method returns the contents of the register @var{regn} in the
+This method returns the contents of the register @var{reg} in the
frame as a @code{gdb.Value} object. @var{reg} can be either a
register number or a register name; the values are platform-specific.
They are usually found in the corresponding
-@file{@var{platform}-tdep.h} file in the @value{GDBN} source tree.
+@file{@var{platform}-tdep.h} file in the @value{GDBN} source tree. If
+@var{reg} does not name a register for the current architecture, this
+method will throw an exception.
+
+Note that this method will always return a @code{gdb.Value} for a
+valid register name. This does not mean that the value will be valid.
+For example, you may request a register that an earlier unwinder could
+not unwind---the value will be unavailable. Instead, the
+@code{gdb.Value} returned from this method will be lazy; that is, its
+underlying bits will not be fetched until it is first used. So,
+attempting to use such a value will cause an exception at the point of
+use.
+
+The type of the returned @code{gdb.Value} depends on the register and
+the architecture. It is common for registers to have a scalar type,
+like @code{long long}; but many other types are possible, such as
+pointer, pointer-to-function, floating point or vector types.
@end defun
It also provides a factory method to create a @code{gdb.UnwindInfo}
determine which function will be used, as follows:
@table @code
-@item sp, pc, special
-@code{frame_id_build_special (@var{frame_id}.sp, @var{frame_id}.pc, @var{frame_id}.special)}
-
@item sp, pc
-@code{frame_id_build (@var{frame_id}.sp, @var{frame_id}.pc)}
+The frame is identified by the given stack address and PC. The stack
+address must be chosen so that it is constant throughout the lifetime
+of the frame, so a typical choice is the value of the stack pointer at
+the start of the function---in the DWARF standard, this would be the
+``Call Frame Address''.
+
+This is the most common case by far. The other cases are documented
+for completeness but are only useful in specialized situations.
-This is the most common case.
+@item sp, pc, special
+The frame is identified by the stack address, the PC, and a
+``special'' address. The special address is used on architectures
+that can have frames that do not change the stack, but which are still
+distinct, for example the IA-64, which has a second stack for
+registers. Both @var{sp} and @var{special} must be constant
+throughout the lifetime of the frame.
@item sp
-@code{frame_id_build_wild (@var{frame_id}.sp)}
+The frame is identified by the stack address only. Any other stack
+frame with a matching @var{sp} will be considered to match this frame.
+Inside gdb, this is called a ``wild frame''. You will never need
+this.
@end table
-The attribute values should be @code{gdb.Value}
+
+Each attribute value should be an instance of @code{gdb.Value}.
@end defun
been cleared. @xref{Progspaces In Python}.
@end defvar
-@item events.inferior_call_pre
-Emits @code{gdb.InferiorCallPreEvent} which indicates that a function in
-the inferior is about to be called.
+@item events.inferior_call
+Emits events just before and after a function in the inferior is
+called by @value{GDBN}. Before an inferior call, this emits an event
+of type @code{gdb.InferiorCallPreEvent}, and after an inferior call,
+this emits an event of type @code{gdb.InferiorCallPostEvent}.
+
+@table @code
+@tindex gdb.InferiorCallPreEvent
+@item @code{gdb.InferiorCallPreEvent}
+Indicates that a function in the inferior is about to be called.
@defvar InferiorCallPreEvent.ptid
The thread in which the call will be run.
The location of the function to be called.
@end defvar
-@item events.inferior_call_post
-Emits @code{gdb.InferiorCallPostEvent} which indicates that a function in
-the inferior has returned.
+@tindex gdb.InferiorCallPostEvent
+@item @code{gdb.InferiorCallPostEvent}
+Indicates that a function in the inferior has just been called.
@defvar InferiorCallPostEvent.ptid
The thread in which the call was run.
@defvar InferiorCallPostEvent.address
The location of the function that was called.
@end defvar
+@end table
@item events.memory_changed
Emits @code{gdb.MemoryChangedEvent} which indicates that the memory of the
@end defun
@node Frames In Python
-@subsubsection Accessing inferior stack frames from Python.
+@subsubsection Accessing inferior stack frames from Python
@cindex frames in python
When the debugged program stops, @value{GDBN} is able to analyze its call
@end defun
@node Blocks In Python
-@subsubsection Accessing blocks from Python.
+@subsubsection Accessing blocks from Python
@cindex blocks in python
@tindex gdb.Block
@end defvar
@node Symbols In Python
-@subsubsection Python representation of Symbols.
+@subsubsection Python representation of Symbols
@cindex symbols in python
@tindex gdb.Symbol
@end vtable
@node Symbol Tables In Python
-@subsubsection Symbol table representation in Python.
+@subsubsection Symbol table representation in Python
@cindex symbol tables in python
@tindex gdb.Symtab
@end defvar
@node Lazy Strings In Python
-@subsubsection Python representation of lazy strings.
+@subsubsection Python representation of lazy strings
@cindex lazy strings in python
@tindex gdb.LazyString