Commands-In-Python.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Commands In Python</title>
  16. <meta name="description" content="Debugging with GDB: Commands In Python">
  17. <meta name="keywords" content="Debugging with GDB: Commands In Python">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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="Parameters-In-Python.html#Parameters-In-Python" rel="next" title="Parameters In Python">
  27. <link href="Recordings-In-Python.html#Recordings-In-Python" rel="previous" title="Recordings In Python">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Commands-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Parameters-In-Python.html#Parameters-In-Python" accesskey="n" rel="next">Parameters In Python</a>, Previous: <a href="Recordings-In-Python.html#Recordings-In-Python" accesskey="p" rel="previous">Recordings In Python</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>
  62. </div>
  63. <hr>
  64. <a name="Commands-In-Python-1"></a>
  65. <h4 class="subsubsection">23.2.2.20 Commands In Python</h4>
  66. <a name="index-commands-in-python"></a>
  67. <a name="index-python-commands-1"></a>
  68. <p>You can implement new <small>GDB</small> CLI commands in Python. A CLI
  69. command is implemented using an instance of the <code>gdb.Command</code>
  70. class, most commonly using a subclass.
  71. </p>
  72. <dl>
  73. <dt><a name="index-Command_002e_005f_005finit_005f_005f"></a>Function: <strong>Command.__init__</strong> <em>(name, <var>command_class</var> <span class="roman">[</span>, <var>completer_class</var> <span class="roman">[</span>, <var>prefix</var><span class="roman">]]</span>)</em></dt>
  74. <dd><p>The object initializer for <code>Command</code> registers the new command
  75. with <small>GDB</small>. This initializer is normally invoked from the
  76. subclass&rsquo; own <code>__init__</code> method.
  77. </p>
  78. <p><var>name</var> is the name of the command. If <var>name</var> consists of
  79. multiple words, then the initial words are looked for as prefix
  80. commands. In this case, if one of the prefix commands does not exist,
  81. an exception is raised.
  82. </p>
  83. <p>There is no support for multi-line commands.
  84. </p>
  85. <p><var>command_class</var> should be one of the &lsquo;<samp>COMMAND_</samp>&rsquo; constants
  86. defined below. This argument tells <small>GDB</small> how to categorize the
  87. new command in the help system.
  88. </p>
  89. <p><var>completer_class</var> is an optional argument. If given, it should be
  90. one of the &lsquo;<samp>COMPLETE_</samp>&rsquo; constants defined below. This argument
  91. tells <small>GDB</small> how to perform completion for this command. If not
  92. given, <small>GDB</small> will attempt to complete using the object&rsquo;s
  93. <code>complete</code> method (see below); if no such method is found, an
  94. error will occur when completion is attempted.
  95. </p>
  96. <p><var>prefix</var> is an optional argument. If <code>True</code>, then the new
  97. command is a prefix command; sub-commands of this command may be
  98. registered.
  99. </p>
  100. <p>The help text for the new command is taken from the Python
  101. documentation string for the command&rsquo;s class, if there is one. If no
  102. documentation string is provided, the default value &ldquo;This command is
  103. not documented.&rdquo; is used.
  104. </p></dd></dl>
  105. <a name="index-don_0027t-repeat-Python-command"></a>
  106. <dl>
  107. <dt><a name="index-Command_002edont_005frepeat"></a>Function: <strong>Command.dont_repeat</strong> <em>()</em></dt>
  108. <dd><p>By default, a <small>GDB</small> command is repeated when the user enters a
  109. blank line at the command prompt. A command can suppress this
  110. behavior by invoking the <code>dont_repeat</code> method. This is similar
  111. to the user command <code>dont-repeat</code>, see <a href="Define.html#Define">dont-repeat</a>.
  112. </p></dd></dl>
  113. <dl>
  114. <dt><a name="index-Command_002einvoke"></a>Function: <strong>Command.invoke</strong> <em>(argument, from_tty)</em></dt>
  115. <dd><p>This method is called by <small>GDB</small> when this command is invoked.
  116. </p>
  117. <p><var>argument</var> is a string. It is the argument to the command, after
  118. leading and trailing whitespace has been stripped.
  119. </p>
  120. <p><var>from_tty</var> is a boolean argument. When true, this means that the
  121. command was entered by the user at the terminal; when false it means
  122. that the command came from elsewhere.
  123. </p>
  124. <p>If this method throws an exception, it is turned into a <small>GDB</small>
  125. <code>error</code> call. Otherwise, the return value is ignored.
  126. </p>
  127. <a name="index-gdb_002estring_005fto_005fargv"></a>
  128. <p>To break <var>argument</var> up into an argv-like string use
  129. <code>gdb.string_to_argv</code>. This function behaves identically to
  130. <small>GDB</small>&rsquo;s internal argument lexer <code>buildargv</code>.
  131. It is recommended to use this for consistency.
  132. Arguments are separated by spaces and may be quoted.
  133. Example:
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">print gdb.string_to_argv (&quot;1 2\ \\\&quot;3 '4 \&quot;5' \&quot;6 '7\&quot;&quot;)
  137. ['1', '2 &quot;3', '4 &quot;5', &quot;6 '7&quot;]
  138. </pre></div>
  139. </dd></dl>
  140. <a name="index-completion-of-Python-commands"></a>
  141. <dl>
  142. <dt><a name="index-Command_002ecomplete"></a>Function: <strong>Command.complete</strong> <em>(text, word)</em></dt>
  143. <dd><p>This method is called by <small>GDB</small> when the user attempts
  144. completion on this command. All forms of completion are handled by
  145. this method, that is, the <tt class="key">TAB</tt> and <tt class="key">M-?</tt> key bindings
  146. (see <a href="Completion.html#Completion">Completion</a>), and the <code>complete</code> command (see <a href="Help.html#Help">complete</a>).
  147. </p>
  148. <p>The arguments <var>text</var> and <var>word</var> are both strings; <var>text</var>
  149. holds the complete command line up to the cursor&rsquo;s location, while
  150. <var>word</var> holds the last word of the command line; this is computed
  151. using a word-breaking heuristic.
  152. </p>
  153. <p>The <code>complete</code> method can return several values:
  154. </p><ul>
  155. <li> If the return value is a sequence, the contents of the sequence are
  156. used as the completions. It is up to <code>complete</code> to ensure that the
  157. contents actually do complete the word. A zero-length sequence is
  158. allowed, it means that there were no completions available. Only
  159. string elements of the sequence are used; other elements in the
  160. sequence are ignored.
  161. </li><li> If the return value is one of the &lsquo;<samp>COMPLETE_</samp>&rsquo; constants defined
  162. below, then the corresponding <small>GDB</small>-internal completion
  163. function is invoked, and its result is used.
  164. </li><li> All other results are treated as though there were no available
  165. completions.
  166. </li></ul>
  167. </dd></dl>
  168. <p>When a new command is registered, it must be declared as a member of
  169. some general class of commands. This is used to classify top-level
  170. commands in the on-line help system; note that prefix commands are not
  171. listed under their own category but rather that of their top-level
  172. command. The available classifications are represented by constants
  173. defined in the <code>gdb</code> module:
  174. </p>
  175. <dl compact="compact">
  176. <dd><a name="index-COMMAND_005fNONE"></a>
  177. <a name="index-gdb_002eCOMMAND_005fNONE"></a>
  178. </dd>
  179. <dt><code>gdb.COMMAND_NONE</code></dt>
  180. <dd><p>The command does not belong to any particular class. A command in
  181. this category will not be displayed in any of the help categories.
  182. </p>
  183. <a name="index-COMMAND_005fRUNNING"></a>
  184. <a name="index-gdb_002eCOMMAND_005fRUNNING"></a>
  185. </dd>
  186. <dt><code>gdb.COMMAND_RUNNING</code></dt>
  187. <dd><p>The command is related to running the inferior. For example,
  188. <code>start</code>, <code>step</code>, and <code>continue</code> are in this category.
  189. Type <kbd>help running</kbd> at the <small>GDB</small> prompt to see a list of
  190. commands in this category.
  191. </p>
  192. <a name="index-COMMAND_005fDATA"></a>
  193. <a name="index-gdb_002eCOMMAND_005fDATA"></a>
  194. </dd>
  195. <dt><code>gdb.COMMAND_DATA</code></dt>
  196. <dd><p>The command is related to data or variables. For example,
  197. <code>call</code>, <code>find</code>, and <code>print</code> are in this category. Type
  198. <kbd>help data</kbd> at the <small>GDB</small> prompt to see a list of commands
  199. in this category.
  200. </p>
  201. <a name="index-COMMAND_005fSTACK"></a>
  202. <a name="index-gdb_002eCOMMAND_005fSTACK"></a>
  203. </dd>
  204. <dt><code>gdb.COMMAND_STACK</code></dt>
  205. <dd><p>The command has to do with manipulation of the stack. For example,
  206. <code>backtrace</code>, <code>frame</code>, and <code>return</code> are in this
  207. category. Type <kbd>help stack</kbd> at the <small>GDB</small> prompt to see a
  208. list of commands in this category.
  209. </p>
  210. <a name="index-COMMAND_005fFILES"></a>
  211. <a name="index-gdb_002eCOMMAND_005fFILES"></a>
  212. </dd>
  213. <dt><code>gdb.COMMAND_FILES</code></dt>
  214. <dd><p>This class is used for file-related commands. For example,
  215. <code>file</code>, <code>list</code> and <code>section</code> are in this category.
  216. Type <kbd>help files</kbd> at the <small>GDB</small> prompt to see a list of
  217. commands in this category.
  218. </p>
  219. <a name="index-COMMAND_005fSUPPORT"></a>
  220. <a name="index-gdb_002eCOMMAND_005fSUPPORT"></a>
  221. </dd>
  222. <dt><code>gdb.COMMAND_SUPPORT</code></dt>
  223. <dd><p>This should be used for &ldquo;support facilities&rdquo;, generally meaning
  224. things that are useful to the user when interacting with <small>GDB</small>,
  225. but not related to the state of the inferior. For example,
  226. <code>help</code>, <code>make</code>, and <code>shell</code> are in this category. Type
  227. <kbd>help support</kbd> at the <small>GDB</small> prompt to see a list of
  228. commands in this category.
  229. </p>
  230. <a name="index-COMMAND_005fSTATUS"></a>
  231. <a name="index-gdb_002eCOMMAND_005fSTATUS"></a>
  232. </dd>
  233. <dt><code>gdb.COMMAND_STATUS</code></dt>
  234. <dd><p>The command is an &lsquo;<samp>info</samp>&rsquo;-related command, that is, related to the
  235. state of <small>GDB</small> itself. For example, <code>info</code>, <code>macro</code>,
  236. and <code>show</code> are in this category. Type <kbd>help status</kbd> at the
  237. <small>GDB</small> prompt to see a list of commands in this category.
  238. </p>
  239. <a name="index-COMMAND_005fBREAKPOINTS"></a>
  240. <a name="index-gdb_002eCOMMAND_005fBREAKPOINTS"></a>
  241. </dd>
  242. <dt><code>gdb.COMMAND_BREAKPOINTS</code></dt>
  243. <dd><p>The command has to do with breakpoints. For example, <code>break</code>,
  244. <code>clear</code>, and <code>delete</code> are in this category. Type <kbd>help
  245. breakpoints</kbd> at the <small>GDB</small> prompt to see a list of commands in
  246. this category.
  247. </p>
  248. <a name="index-COMMAND_005fTRACEPOINTS"></a>
  249. <a name="index-gdb_002eCOMMAND_005fTRACEPOINTS"></a>
  250. </dd>
  251. <dt><code>gdb.COMMAND_TRACEPOINTS</code></dt>
  252. <dd><p>The command has to do with tracepoints. For example, <code>trace</code>,
  253. <code>actions</code>, and <code>tfind</code> are in this category. Type
  254. <kbd>help tracepoints</kbd> at the <small>GDB</small> prompt to see a list of
  255. commands in this category.
  256. </p>
  257. <a name="index-COMMAND_005fTUI"></a>
  258. <a name="index-gdb_002eCOMMAND_005fTUI"></a>
  259. </dd>
  260. <dt><code>gdb.COMMAND_TUI</code></dt>
  261. <dd><p>The command has to do with the text user interface (see <a href="TUI.html#TUI">TUI</a>).
  262. Type <kbd>help tui</kbd> at the <small>GDB</small> prompt to see a list of
  263. commands in this category.
  264. </p>
  265. <a name="index-COMMAND_005fUSER"></a>
  266. <a name="index-gdb_002eCOMMAND_005fUSER"></a>
  267. </dd>
  268. <dt><code>gdb.COMMAND_USER</code></dt>
  269. <dd><p>The command is a general purpose command for the user, and typically
  270. does not fit in one of the other categories.
  271. Type <kbd>help user-defined</kbd> at the <small>GDB</small> prompt to see
  272. a list of commands in this category, as well as the list of gdb macros
  273. (see <a href="Sequences.html#Sequences">Sequences</a>).
  274. </p>
  275. <a name="index-COMMAND_005fOBSCURE"></a>
  276. <a name="index-gdb_002eCOMMAND_005fOBSCURE"></a>
  277. </dd>
  278. <dt><code>gdb.COMMAND_OBSCURE</code></dt>
  279. <dd><p>The command is only used in unusual circumstances, or is not of
  280. general interest to users. For example, <code>checkpoint</code>,
  281. <code>fork</code>, and <code>stop</code> are in this category. Type <kbd>help
  282. obscure</kbd> at the <small>GDB</small> prompt to see a list of commands in this
  283. category.
  284. </p>
  285. <a name="index-COMMAND_005fMAINTENANCE"></a>
  286. <a name="index-gdb_002eCOMMAND_005fMAINTENANCE"></a>
  287. </dd>
  288. <dt><code>gdb.COMMAND_MAINTENANCE</code></dt>
  289. <dd><p>The command is only useful to <small>GDB</small> maintainers. The
  290. <code>maintenance</code> and <code>flushregs</code> commands are in this category.
  291. Type <kbd>help internals</kbd> at the <small>GDB</small> prompt to see a list of
  292. commands in this category.
  293. </p></dd>
  294. </dl>
  295. <p>A new command can use a predefined completion function, either by
  296. specifying it via an argument at initialization, or by returning it
  297. from the <code>complete</code> method. These predefined completion
  298. constants are all defined in the <code>gdb</code> module:
  299. </p>
  300. <dl compact="compact">
  301. <dd><a name="index-COMPLETE_005fNONE"></a>
  302. </dd>
  303. <dt><code>gdb.COMPLETE_NONE</code>
  304. <a name="index-gdb_002eCOMPLETE_005fNONE"></a>
  305. </dt>
  306. <dd><p>This constant means that no completion should be done.
  307. </p>
  308. <a name="index-COMPLETE_005fFILENAME"></a>
  309. </dd>
  310. <dt><code>gdb.COMPLETE_FILENAME</code>
  311. <a name="index-gdb_002eCOMPLETE_005fFILENAME"></a>
  312. </dt>
  313. <dd><p>This constant means that filename completion should be performed.
  314. </p>
  315. <a name="index-COMPLETE_005fLOCATION"></a>
  316. </dd>
  317. <dt><code>gdb.COMPLETE_LOCATION</code>
  318. <a name="index-gdb_002eCOMPLETE_005fLOCATION"></a>
  319. </dt>
  320. <dd><p>This constant means that location completion should be done.
  321. See <a href="Specify-Location.html#Specify-Location">Specify Location</a>.
  322. </p>
  323. <a name="index-COMPLETE_005fCOMMAND"></a>
  324. </dd>
  325. <dt><code>gdb.COMPLETE_COMMAND</code>
  326. <a name="index-gdb_002eCOMPLETE_005fCOMMAND"></a>
  327. </dt>
  328. <dd><p>This constant means that completion should examine <small>GDB</small>
  329. command names.
  330. </p>
  331. <a name="index-COMPLETE_005fSYMBOL"></a>
  332. </dd>
  333. <dt><code>gdb.COMPLETE_SYMBOL</code>
  334. <a name="index-gdb_002eCOMPLETE_005fSYMBOL"></a>
  335. </dt>
  336. <dd><p>This constant means that completion should be done using symbol names
  337. as the source.
  338. </p>
  339. <a name="index-COMPLETE_005fEXPRESSION"></a>
  340. </dd>
  341. <dt><code>gdb.COMPLETE_EXPRESSION</code>
  342. <a name="index-gdb_002eCOMPLETE_005fEXPRESSION"></a>
  343. </dt>
  344. <dd><p>This constant means that completion should be done on expressions.
  345. Often this means completing on symbol names, but some language
  346. parsers also have support for completing on field names.
  347. </p></dd>
  348. </dl>
  349. <p>The following code snippet shows how a trivial CLI command can be
  350. implemented in Python:
  351. </p>
  352. <div class="smallexample">
  353. <pre class="smallexample">class HelloWorld (gdb.Command):
  354. &quot;&quot;&quot;Greet the whole world.&quot;&quot;&quot;
  355. def __init__ (self):
  356. super (HelloWorld, self).__init__ (&quot;hello-world&quot;, gdb.COMMAND_USER)
  357. def invoke (self, arg, from_tty):
  358. print &quot;Hello, World!&quot;
  359. HelloWorld ()
  360. </pre></div>
  361. <p>The last line instantiates the class, and is necessary to trigger the
  362. registration of the command with <small>GDB</small>. Depending on how the
  363. Python code is read into <small>GDB</small>, you may need to import the
  364. <code>gdb</code> module explicitly.
  365. </p>
  366. <hr>
  367. <div class="header">
  368. <p>
  369. Next: <a href="Parameters-In-Python.html#Parameters-In-Python" accesskey="n" rel="next">Parameters In Python</a>, Previous: <a href="Recordings-In-Python.html#Recordings-In-Python" accesskey="p" rel="previous">Recordings In Python</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>
  370. </div>
  371. </body>
  372. </html>