You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
20KB

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