1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Basic Python</title>
18
19<meta name="description" content="Debugging with GDB: Basic Python">
20<meta name="keywords" content="Debugging with GDB: Basic Python">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Python-API.html#Python-API" rel="up" title="Python API">
29<link href="Exception-Handling.html#Exception-Handling" rel="next" title="Exception Handling">
30<link href="Python-API.html#Python-API" rel="previous" title="Python API">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Basic-Python"></a>
65<div class="header">
66<p>
67Next: <a href="Exception-Handling.html#Exception-Handling" accesskey="n" rel="next">Exception Handling</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
68</div>
69<hr>
70<a name="Basic-Python-1"></a>
71<h4 class="subsubsection">23.2.2.1 Basic Python</h4>
72
73<a name="index-python-stdout"></a>
74<a name="index-python-pagination"></a>
75<p>At startup, <small>GDB</small> overrides Python&rsquo;s <code>sys.stdout</code> and
76<code>sys.stderr</code> to print using <small>GDB</small>&rsquo;s output-paging streams.
77A Python program which outputs to one of these streams may have its
78output interrupted by the user (see <a href="Screen-Size.html#Screen-Size">Screen Size</a>).  In this
79situation, a Python <code>KeyboardInterrupt</code> exception is thrown.
80</p>
81<p>Some care must be taken when writing Python code to run in
82<small>GDB</small>.  Two things worth noting in particular:
83</p>
84<ul>
85<li> <small>GDB</small> install handlers for <code>SIGCHLD</code> and <code>SIGINT</code>.
86Python code must not override these, or even change the options using
87<code>sigaction</code>.  If your program changes the handling of these
88signals, <small>GDB</small> will most likely stop working correctly.  Note
89that it is unfortunately common for GUI toolkits to install a
90<code>SIGCHLD</code> handler.
91
92</li><li> <small>GDB</small> takes care to mark its internal file descriptors as
93close-on-exec.  However, this cannot be done in a thread-safe way on
94all platforms.  Your Python programs should be aware of this and
95should both create new file descriptors with the close-on-exec flag
96set and arrange to close unneeded file descriptors before starting a
97child process.
98</li></ul>
99
100<a name="index-python-functions"></a>
101<a name="index-python-module"></a>
102<a name="index-gdb-module"></a>
103<p><small>GDB</small> introduces a new Python module, named <code>gdb</code>.  All
104methods and classes added by <small>GDB</small> are placed in this module.
105<small>GDB</small> automatically <code>import</code>s the <code>gdb</code> module for
106use in all scripts evaluated by the <code>python</code> command.
107</p>
108<p>Some types of the <code>gdb</code> module come with a textual representation
109(accessible through the <code>repr</code> or <code>str</code> functions).  These are
110offered for debugging purposes only, expect them to change over time.
111</p>
112<a name="index-gdb_002ePYTHONDIR"></a>
113<dl>
114<dt><a name="index-gdb_002ePYTHONDIR-1"></a>Variable: <strong>gdb.PYTHONDIR</strong></dt>
115<dd><p>A string containing the python directory (see <a href="Python.html#Python">Python</a>).
116</p></dd></dl>
117
118<a name="index-gdb_002eexecute"></a>
119<dl>
120<dt><a name="index-gdb_002eexecute-1"></a>Function: <strong>gdb.execute</strong> <em>(command <span class="roman">[</span>, from_tty <span class="roman">[</span>, to_string<span class="roman">]]</span>)</em></dt>
121<dd><p>Evaluate <var>command</var>, a string, as a <small>GDB</small> CLI command.
122If a GDB exception happens while <var>command</var> runs, it is
123translated as described in <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>.
124</p>
125<p>The <var>from_tty</var> flag specifies whether <small>GDB</small> ought to consider this
126command as having originated from the user invoking it interactively.
127It must be a boolean value.  If omitted, it defaults to <code>False</code>.
128</p>
129<p>By default, any output produced by <var>command</var> is sent to
130<small>GDB</small>&rsquo;s standard output (and to the log output if logging is
131turned on).  If the <var>to_string</var> parameter is
132<code>True</code>, then output will be collected by <code>gdb.execute</code> and
133returned as a string.  The default is <code>False</code>, in which case the
134return value is <code>None</code>.  If <var>to_string</var> is <code>True</code>, the
135<small>GDB</small> virtual terminal will be temporarily set to unlimited width
136and height, and its pagination will be disabled; see <a href="Screen-Size.html#Screen-Size">Screen Size</a>.
137</p></dd></dl>
138
139<a name="index-gdb_002ebreakpoints"></a>
140<dl>
141<dt><a name="index-gdb_002ebreakpoints-1"></a>Function: <strong>gdb.breakpoints</strong> <em>()</em></dt>
142<dd><p>Return a sequence holding all of <small>GDB</small>&rsquo;s breakpoints.
143See <a href="Breakpoints-In-Python.html#Breakpoints-In-Python">Breakpoints In Python</a>, for more information.  In <small>GDB</small>
144version 7.11 and earlier, this function returned <code>None</code> if there
145were no breakpoints.  This peculiarity was subsequently fixed, and now
146<code>gdb.breakpoints</code> returns an empty sequence in this case.
147</p></dd></dl>
148
149<dl>
150<dt><a name="index-gdb_002erbreak"></a>Function: <strong>gdb.rbreak</strong> <em>(regex <span class="roman">[</span>, minsyms <span class="roman">[</span>, throttle, <span class="roman">[</span>, symtabs <span class="roman">]]]</span>)</em></dt>
151<dd><p>Return a Python list holding a collection of newly set
152<code>gdb.Breakpoint</code> objects matching function names defined by the
153<var>regex</var> pattern.  If the <var>minsyms</var> keyword is <code>True</code>, all
154system functions (those not explicitly defined in the inferior) will
155also be included in the match.  The <var>throttle</var> keyword takes an
156integer that defines the maximum number of pattern matches for
157functions matched by the <var>regex</var> pattern.  If the number of
158matches exceeds the integer value of <var>throttle</var>, a
159<code>RuntimeError</code> will be raised and no breakpoints will be created.
160If <var>throttle</var> is not defined then there is no imposed limit on the
161maximum number of matches and breakpoints to be created.  The
162<var>symtabs</var> keyword takes a Python iterable that yields a collection
163of <code>gdb.Symtab</code> objects and will restrict the search to those
164functions only contained within the <code>gdb.Symtab</code> objects.
165</p></dd></dl>
166
167<a name="index-gdb_002eparameter"></a>
168<dl>
169<dt><a name="index-gdb_002eparameter-1"></a>Function: <strong>gdb.parameter</strong> <em>(parameter)</em></dt>
170<dd><p>Return the value of a <small>GDB</small> <var>parameter</var> given by its name,
171a string; the parameter name string may contain spaces if the parameter has a
172multi-part name.  For example, &lsquo;<samp>print object</samp>&rsquo; is a valid
173parameter name.
174</p>
175<p>If the named parameter does not exist, this function throws a
176<code>gdb.error</code> (see <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>).  Otherwise, the
177parameter&rsquo;s value is converted to a Python value of the appropriate
178type, and returned.
179</p></dd></dl>
180
181<a name="index-gdb_002ehistory"></a>
182<dl>
183<dt><a name="index-gdb_002ehistory-1"></a>Function: <strong>gdb.history</strong> <em>(number)</em></dt>
184<dd><p>Return a value from <small>GDB</small>&rsquo;s value history (see <a href="Value-History.html#Value-History">Value History</a>).  The <var>number</var> argument indicates which history element to return.
185If <var>number</var> is negative, then <small>GDB</small> will take its absolute value
186and count backward from the last element (i.e., the most recent element) to
187find the value to return.  If <var>number</var> is zero, then <small>GDB</small> will
188return the most recent element.  If the element specified by <var>number</var>
189doesn&rsquo;t exist in the value history, a <code>gdb.error</code> exception will be
190raised.
191</p>
192<p>If no exception is raised, the return value is always an instance of
193<code>gdb.Value</code> (see <a href="Values-From-Inferior.html#Values-From-Inferior">Values From Inferior</a>).
194</p></dd></dl>
195
196<a name="index-gdb_002econvenience_005fvariable"></a>
197<dl>
198<dt><a name="index-gdb_002econvenience_005fvariable-1"></a>Function: <strong>gdb.convenience_variable</strong> <em>(name)</em></dt>
199<dd><p>Return the value of the convenience variable (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>) named <var>name</var>.  <var>name</var> must be a string.  The name
200should not include the &lsquo;<samp>$</samp>&rsquo; that is used to mark a convenience
201variable in an expression.  If the convenience variable does not
202exist, then <code>None</code> is returned.
203</p></dd></dl>
204
205<a name="index-gdb_002eset_005fconvenience_005fvariable"></a>
206<dl>
207<dt><a name="index-gdb_002eset_005fconvenience_005fvariable-1"></a>Function: <strong>gdb.set_convenience_variable</strong> <em>(name, value)</em></dt>
208<dd><p>Set the value of the convenience variable (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>)
209named <var>name</var>.  <var>name</var> must be a string.  The name should not
210include the &lsquo;<samp>$</samp>&rsquo; that is used to mark a convenience variable in an
211expression.  If <var>value</var> is <code>None</code>, then the convenience
212variable is removed.  Otherwise, if <var>value</var> is not a
213<code>gdb.Value</code> (see <a href="Values-From-Inferior.html#Values-From-Inferior">Values From Inferior</a>), it is is converted
214using the <code>gdb.Value</code> constructor.
215</p></dd></dl>
216
217<a name="index-gdb_002eparse_005fand_005feval"></a>
218<dl>
219<dt><a name="index-gdb_002eparse_005fand_005feval-1"></a>Function: <strong>gdb.parse_and_eval</strong> <em>(expression)</em></dt>
220<dd><p>Parse <var>expression</var>, which must be a string, as an expression in
221the current language, evaluate it, and return the result as a
222<code>gdb.Value</code>.
223</p>
224<p>This function can be useful when implementing a new command
225(see <a href="Commands-In-Python.html#Commands-In-Python">Commands In Python</a>), as it provides a way to parse the
226command&rsquo;s argument as an expression.  It is also useful simply to
227compute values.
228</p></dd></dl>
229
230<a name="index-gdb_002efind_005fpc_005fline"></a>
231<dl>
232<dt><a name="index-gdb_002efind_005fpc_005fline-1"></a>Function: <strong>gdb.find_pc_line</strong> <em>(pc)</em></dt>
233<dd><p>Return the <code>gdb.Symtab_and_line</code> object corresponding to the
234<var>pc</var> value.  See <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>.  If an invalid
235value of <var>pc</var> is passed as an argument, then the <code>symtab</code> and
236<code>line</code> attributes of the returned <code>gdb.Symtab_and_line</code> object
237will be <code>None</code> and 0 respectively.  This is identical to
238<code>gdb.current_progspace().find_pc_line(pc)</code> and is included for
239historical compatibility.
240</p></dd></dl>
241
242<a name="index-gdb_002epost_005fevent"></a>
243<dl>
244<dt><a name="index-gdb_002epost_005fevent-1"></a>Function: <strong>gdb.post_event</strong> <em>(event)</em></dt>
245<dd><p>Put <var>event</var>, a callable object taking no arguments, into
246<small>GDB</small>&rsquo;s internal event queue.  This callable will be invoked at
247some later point, during <small>GDB</small>&rsquo;s event processing.  Events
248posted using <code>post_event</code> will be run in the order in which they
249were posted; however, there is no way to know when they will be
250processed relative to other events inside <small>GDB</small>.
251</p>
252<p><small>GDB</small> is not thread-safe.  If your Python program uses multiple
253threads, you must be careful to only call <small>GDB</small>-specific
254functions in the <small>GDB</small> thread.  <code>post_event</code> ensures
255this.  For example:
256</p>
257<div class="smallexample">
258<pre class="smallexample">(gdb) python
259&gt;import threading
260&gt;
261&gt;class Writer():
262&gt; def __init__(self, message):
263&gt;        self.message = message;
264&gt; def __call__(self):
265&gt;        gdb.write(self.message)
266&gt;
267&gt;class MyThread1 (threading.Thread):
268&gt; def run (self):
269&gt;        gdb.post_event(Writer(&quot;Hello &quot;))
270&gt;
271&gt;class MyThread2 (threading.Thread):
272&gt; def run (self):
273&gt;        gdb.post_event(Writer(&quot;World\n&quot;))
274&gt;
275&gt;MyThread1().start()
276&gt;MyThread2().start()
277&gt;end
278(gdb) Hello World
279</pre></div>
280</dd></dl>
281
282<a name="index-gdb_002ewrite"></a>
283<dl>
284<dt><a name="index-gdb_002ewrite-1"></a>Function: <strong>gdb.write</strong> <em>(string <span class="roman">[</span>, stream])</em></dt>
285<dd><p>Print a string to <small>GDB</small>&rsquo;s paginated output stream.  The
286optional <var>stream</var> determines the stream to print to.  The default
287stream is <small>GDB</small>&rsquo;s standard output stream.  Possible stream
288values are:
289</p>
290<dl compact="compact">
291<dd><a name="index-STDOUT"></a>
292<a name="index-gdb_002eSTDOUT"></a>
293</dd>
294<dt><code>gdb.STDOUT</code></dt>
295<dd><p><small>GDB</small>&rsquo;s standard output stream.
296</p>
297<a name="index-STDERR"></a>
298<a name="index-gdb_002eSTDERR"></a>
299</dd>
300<dt><code>gdb.STDERR</code></dt>
301<dd><p><small>GDB</small>&rsquo;s standard error stream.
302</p>
303<a name="index-STDLOG"></a>
304<a name="index-gdb_002eSTDLOG"></a>
305</dd>
306<dt><code>gdb.STDLOG</code></dt>
307<dd><p><small>GDB</small>&rsquo;s log stream (see <a href="Logging-Output.html#Logging-Output">Logging Output</a>).
308</p></dd>
309</dl>
310
311<p>Writing to <code>sys.stdout</code> or <code>sys.stderr</code> will automatically
312call this function and will automatically direct the output to the
313relevant stream.
314</p></dd></dl>
315
316<a name="index-gdb_002eflush"></a>
317<dl>
318<dt><a name="index-gdb_002eflush-1"></a>Function: <strong>gdb.flush</strong> <em>()</em></dt>
319<dd><p>Flush the buffer of a <small>GDB</small> paginated stream so that the
320contents are displayed immediately.  <small>GDB</small> will flush the
321contents of a stream automatically when it encounters a newline in the
322buffer.  The optional <var>stream</var> determines the stream to flush.  The
323default stream is <small>GDB</small>&rsquo;s standard output stream.  Possible
324stream values are:
325</p>
326<dl compact="compact">
327<dd><a name="index-STDOUT-1"></a>
328<a name="index-gdb_002eSTDOUT-1"></a>
329</dd>
330<dt><code>gdb.STDOUT</code></dt>
331<dd><p><small>GDB</small>&rsquo;s standard output stream.
332</p>
333<a name="index-STDERR-1"></a>
334<a name="index-gdb_002eSTDERR-1"></a>
335</dd>
336<dt><code>gdb.STDERR</code></dt>
337<dd><p><small>GDB</small>&rsquo;s standard error stream.
338</p>
339<a name="index-STDLOG-1"></a>
340<a name="index-gdb_002eSTDLOG-1"></a>
341</dd>
342<dt><code>gdb.STDLOG</code></dt>
343<dd><p><small>GDB</small>&rsquo;s log stream (see <a href="Logging-Output.html#Logging-Output">Logging Output</a>).
344</p>
345</dd>
346</dl>
347
348<p>Flushing <code>sys.stdout</code> or <code>sys.stderr</code> will automatically
349call this function for the relevant stream.
350</p></dd></dl>
351
352<a name="index-gdb_002etarget_005fcharset"></a>
353<dl>
354<dt><a name="index-gdb_002etarget_005fcharset-1"></a>Function: <strong>gdb.target_charset</strong> <em>()</em></dt>
355<dd><p>Return the name of the current target character set (see <a href="Character-Sets.html#Character-Sets">Character Sets</a>).  This differs from <code>gdb.parameter('target-charset')</code> in
356that &lsquo;<samp>auto</samp>&rsquo; is never returned.
357</p></dd></dl>
358
359<a name="index-gdb_002etarget_005fwide_005fcharset"></a>
360<dl>
361<dt><a name="index-gdb_002etarget_005fwide_005fcharset-1"></a>Function: <strong>gdb.target_wide_charset</strong> <em>()</em></dt>
362<dd><p>Return the name of the current target wide character set
363(see <a href="Character-Sets.html#Character-Sets">Character Sets</a>).  This differs from
364<code>gdb.parameter('target-wide-charset')</code> in that &lsquo;<samp>auto</samp>&rsquo; is
365never returned.
366</p></dd></dl>
367
368<a name="index-gdb_002esolib_005fname"></a>
369<dl>
370<dt><a name="index-gdb_002esolib_005fname-1"></a>Function: <strong>gdb.solib_name</strong> <em>(address)</em></dt>
371<dd><p>Return the name of the shared library holding the given <var>address</var>
372as a string, or <code>None</code>.  This is identical to
373<code>gdb.current_progspace().solib_name(address)</code> and is included for
374historical compatibility.
375</p></dd></dl>
376
377<a name="index-gdb_002edecode_005fline"></a>
378<dl>
379<dt><a name="index-gdb_002edecode_005fline-1"></a>Function: <strong>gdb.decode_line</strong> <em>(<span class="roman">[</span>expression<span class="roman">]</span>)</em></dt>
380<dd><p>Return locations of the line specified by <var>expression</var>, or of the
381current line if no argument was given.  This function returns a Python
382tuple containing two elements.  The first element contains a string
383holding any unparsed section of <var>expression</var> (or <code>None</code> if
384the expression has been fully parsed).  The second element contains
385either <code>None</code> or another tuple that contains all the locations
386that match the expression represented as <code>gdb.Symtab_and_line</code>
387objects (see <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>).  If <var>expression</var> is
388provided, it is decoded the way that <small>GDB</small>&rsquo;s inbuilt
389<code>break</code> or <code>edit</code> commands do (see <a href="Specify-Location.html#Specify-Location">Specify Location</a>).
390</p></dd></dl>
391
392<dl>
393<dt><a name="index-gdb_002eprompt_005fhook"></a>Function: <strong>gdb.prompt_hook</strong> <em>(current_prompt)</em></dt>
394<dd><a name="prompt_005fhook"></a>
395<p>If <var>prompt_hook</var> is callable, <small>GDB</small> will call the method
396assigned to this operation before a prompt is displayed by
397<small>GDB</small>.
398</p>
399<p>The parameter <code>current_prompt</code> contains the current <small>GDB</small>
400prompt.  This method must return a Python string, or <code>None</code>.  If
401a string is returned, the <small>GDB</small> prompt will be set to that
402string.  If <code>None</code> is returned, <small>GDB</small> will continue to use
403the current prompt.
404</p>
405<p>Some prompts cannot be substituted in <small>GDB</small>.  Secondary prompts
406such as those used by readline for command input, and annotation
407related prompts are prohibited from being changed.
408</p></dd></dl>
409
410<hr>
411<div class="header">
412<p>
413Next: <a href="Exception-Handling.html#Exception-Handling" accesskey="n" rel="next">Exception Handling</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
414</div>
415
416
417
418</body>
419</html>
420