Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Commands-In-Python.html 17KB

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