Next: Python API, Up: Python
gdb provides two commands for accessing the Python interpreter, and one related setting:
python-interactive [command]pi [command]python-interactive command can be used
to start an interactive Python prompt. To return to gdb,
type the EOF character (e.g., Ctrl-D on an empty prompt).
Alternatively, a single-line Python command can be given as an argument and evaluated. If the command is an expression, the result will be printed; otherwise, nothing will be printed. For example:
(gdb) python-interactive 2 + 3
5
python [command]py [command]python command can be used to evaluate Python code.
If given an argument, the python command will evaluate the
argument as a Python command. For example:
(gdb) python print 23
23
If you do not provide an argument to python, it will act as a
multi-line command, like define. In this case, the Python
script is made up of subsequent command lines, given after the
python command. This command list is terminated using a line
containing end. For example:
(gdb) python
Type python script
End with a line saying just "end".
>print 23
>end
23
set python print-stackset python print-stack: if full, then
full Python stack printing is enabled; if none, then Python stack
and message printing is disabled; if message, the default, only
the message component of the error is printed.
It is also possible to execute a Python script from the gdb interpreter:
source script-namescript-extension setting. See Extending GDB.
python execfile ("script-name")execfile Python built-in function,
and thus is always available.