Commands-In-Guile.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 Guile</title>
  16. <meta name="description" content="Debugging with GDB: Commands In Guile">
  17. <meta name="keywords" content="Debugging with GDB: Commands In Guile">
  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="Guile-API.html#Guile-API" rel="up" title="Guile API">
  26. <link href="Parameters-In-Guile.html#Parameters-In-Guile" rel="next" title="Parameters In Guile">
  27. <link href="Writing-a-Guile-Pretty_002dPrinter.html#Writing-a-Guile-Pretty_002dPrinter" rel="previous" title="Writing a Guile Pretty-Printer">
  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-Guile"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Parameters-In-Guile.html#Parameters-In-Guile" accesskey="n" rel="next">Parameters In Guile</a>, Previous: <a href="Writing-a-Guile-Pretty_002dPrinter.html#Writing-a-Guile-Pretty_002dPrinter" accesskey="p" rel="previous">Writing a Guile Pretty-Printer</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile 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-Guile-1"></a>
  65. <h4 class="subsubsection">23.3.3.11 Commands In Guile</h4>
  66. <a name="index-commands-in-guile"></a>
  67. <a name="index-guile-commands-1"></a>
  68. <p>You can implement new <small>GDB</small> CLI commands in Guile. A CLI
  69. command object is created with the <code>make-command</code> Guile function,
  70. and added to <small>GDB</small> with the <code>register-command!</code> Guile function.
  71. This two-step approach is taken to separate out the side-effect of adding
  72. the command to <small>GDB</small> from <code>make-command</code>.
  73. </p>
  74. <p>There is no support for multi-line commands, that is commands that
  75. consist of multiple lines and are terminated with <code>end</code>.
  76. </p>
  77. <dl>
  78. <dt><a name="index-_0028make_002dcommand"></a>Scheme Procedure: <strong>(make-command</strong> <em>name <span class="roman">[</span>#:invoke invoke] <span class="roman">[</span>#:command-class command-class<span class="roman">]</span> <span class="roman">[</span>#:completer-class completer] <span class="roman">[</span>#:prefix? prefix<span class="roman">]</span> <span class="roman">[</span>#:doc doc-string])</em></dt>
  79. <dd>
  80. <p>The argument <var>name</var> is the name of the command. If <var>name</var> consists of
  81. multiple words, then the initial words are looked for as prefix
  82. commands. In this case, if one of the prefix commands does not exist,
  83. an exception is raised.
  84. </p>
  85. <p>The result is the <code>&lt;gdb:command&gt;</code> object representing the command.
  86. The command is not usable until it has been registered with <small>GDB</small>
  87. with <code>register-command!</code>.
  88. </p>
  89. <p>The rest of the arguments are optional.
  90. </p>
  91. <p>The argument <var>invoke</var> is a procedure of three arguments: <var>self</var>,
  92. <var>args</var> and <var>from-tty</var>. The argument <var>self</var> is the
  93. <code>&lt;gdb:command&gt;</code> object representing the command.
  94. The argument <var>args</var> is a string representing the arguments passed to
  95. the command, after leading and trailing whitespace has been stripped.
  96. The argument <var>from-tty</var> is a boolean flag and specifies whether the
  97. command should consider itself to have been originated from the user
  98. invoking it interactively. If this function throws an exception,
  99. it is turned into a <small>GDB</small> <code>error</code> call.
  100. Otherwise, the return value is ignored.
  101. </p>
  102. <p>The argument <var>command-class</var> is one of the &lsquo;<samp>COMMAND_</samp>&rsquo; constants
  103. defined below. This argument tells <small>GDB</small> how to categorize the
  104. new command in the help system. The default is <code>COMMAND_NONE</code>.
  105. </p>
  106. <p>The argument <var>completer</var> is either <code>#f</code>, one of the &lsquo;<samp>COMPLETE_</samp>&rsquo;
  107. constants defined below, or a procedure, also defined below.
  108. This argument tells <small>GDB</small> how to perform completion
  109. for this command. If not provided or if the value is <code>#f</code>,
  110. then no completion is performed on the command.
  111. </p>
  112. <p>The argument <var>prefix</var> is a boolean flag indicating whether the new
  113. command is a prefix command; sub-commands of this command may be
  114. registered.
  115. </p>
  116. <p>The argument <var>doc-string</var> is help text for the new command.
  117. If no documentation string is provided, the default value &ldquo;This command is
  118. not documented.&rdquo; is used.
  119. </p></dd></dl>
  120. <dl>
  121. <dt><a name="index-register_002dcommand_0021"></a>Scheme Procedure: <strong>register-command!</strong> <em>command</em></dt>
  122. <dd><p>Add <var>command</var>, a <code>&lt;gdb:command&gt;</code> object, to <small>GDB</small>&rsquo;s
  123. list of commands.
  124. It is an error to register a command more than once.
  125. The result is unspecified.
  126. </p></dd></dl>
  127. <dl>
  128. <dt><a name="index-command_003f"></a>Scheme Procedure: <strong>command?</strong> <em>object</em></dt>
  129. <dd><p>Return <code>#t</code> if <var>object</var> is a <code>&lt;gdb:command&gt;</code> object.
  130. Otherwise return <code>#f</code>.
  131. </p></dd></dl>
  132. <a name="index-don_0027t-repeat-Guile-command"></a>
  133. <dl>
  134. <dt><a name="index-dont_002drepeat"></a>Scheme Procedure: <strong>dont-repeat</strong></dt>
  135. <dd><p>By default, a <small>GDB</small> command is repeated when the user enters a
  136. blank line at the command prompt. A command can suppress this
  137. behavior by invoking the <code>dont-repeat</code> function. This is similar
  138. to the user command <code>dont-repeat</code>, see <a href="Define.html#Define">dont-repeat</a>.
  139. </p></dd></dl>
  140. <dl>
  141. <dt><a name="index-string_002d_003eargv"></a>Scheme Procedure: <strong>string-&gt;argv</strong> <em>string</em></dt>
  142. <dd><p>Convert a string to a list of strings split up according to
  143. <small>GDB</small>&rsquo;s argv parsing rules.
  144. It is recommended to use this for consistency.
  145. Arguments are separated by spaces and may be quoted.
  146. Example:
  147. </p>
  148. <div class="smallexample">
  149. <pre class="smallexample">scheme@(guile-user)&gt; (string-&gt;argv &quot;1 2\\ \\\&quot;3 '4 \&quot;5' \&quot;6 '7\&quot;&quot;)
  150. $1 = (&quot;1&quot; &quot;2 \&quot;3&quot; &quot;4 \&quot;5&quot; &quot;6 '7&quot;)
  151. </pre></div>
  152. </dd></dl>
  153. <dl>
  154. <dt><a name="index-throw_002duser_002derror"></a>Scheme Procedure: <strong>throw-user-error</strong> <em>message . args</em></dt>
  155. <dd><p>Throw a <code>gdb:user-error</code> exception.
  156. The argument <var>message</var> is the error message as a format string, like the
  157. <var>fmt</var> argument to the <code>format</code> Scheme function.
  158. See <a href="http://www.gnu.org/software/guile/manual/html_node/Formatted-Output.html#Formatted-Output">Formatted Output</a> in <cite>GNU Guile Reference Manual</cite>.
  159. The argument <var>args</var> is a list of the optional arguments of <var>message</var>.
  160. </p>
  161. <p>This is used when the command detects a user error of some kind,
  162. say a bad command argument.
  163. </p>
  164. <div class="smallexample">
  165. <pre class="smallexample">(gdb) guile (use-modules (gdb))
  166. (gdb) guile
  167. (register-command! (make-command &quot;test-user-error&quot;
  168. #:command-class COMMAND_OBSCURE
  169. #:invoke (lambda (self arg from-tty)
  170. (throw-user-error &quot;Bad argument ~a&quot; arg))))
  171. end
  172. (gdb) test-user-error ugh
  173. ERROR: Bad argument ugh
  174. </pre></div>
  175. </dd></dl>
  176. <a name="index-completion-of-Guile-commands"></a>
  177. <dl>
  178. <dt><a name="index-self"></a>completer: <strong>self</strong> <em>text word</em></dt>
  179. <dd><p>If the <var>completer</var> option to <code>make-command</code> is a procedure,
  180. it takes three arguments: <var>self</var> which is the <code>&lt;gdb:command&gt;</code>
  181. object, and <var>text</var> and <var>word</var> which are both strings.
  182. The argument <var>text</var> holds the complete command line up to the cursor&rsquo;s
  183. location. The argument <var>word</var> holds the last word of the command line;
  184. this is computed using a word-breaking heuristic.
  185. </p>
  186. <p>All forms of completion are handled by this function, that is,
  187. the <tt class="key">TAB</tt> and <tt class="key">M-?</tt> key bindings (see <a href="Completion.html#Completion">Completion</a>),
  188. and the <code>complete</code> command (see <a href="Help.html#Help">complete</a>).
  189. </p>
  190. <p>This procedure can return several kinds of values:
  191. </p>
  192. <ul>
  193. <li> If the return value is a list, the contents of the list are used as the
  194. completions. It is up to <var>completer</var> to ensure that the
  195. contents actually do complete the word. An empty list is
  196. allowed, it means that there were no completions available. Only
  197. string elements of the list are used; other elements in the
  198. list are ignored.
  199. </li><li> If the return value is a <code>&lt;gdb:iterator&gt;</code> object, it is iterated over to
  200. obtain the completions. It is up to <code>completer-procedure</code> to ensure
  201. that the results actually do complete the word. Only
  202. string elements of the result are used; other elements in the
  203. sequence are ignored.
  204. </li><li> All other results are treated as though there were no available
  205. completions.
  206. </li></ul>
  207. </dd></dl>
  208. <p>When a new command is registered, it will have been declared as a member of
  209. some general class of commands. This is used to classify top-level
  210. commands in the on-line help system; note that prefix commands are not
  211. listed under their own category but rather that of their top-level
  212. command. The available classifications are represented by constants
  213. defined in the <code>gdb</code> module:
  214. </p>
  215. <dl compact="compact">
  216. <dt><code>COMMAND_NONE</code>
  217. <a name="index-COMMAND_005fNONE-1"></a>
  218. </dt>
  219. <dd><p>The command does not belong to any particular class. A command in
  220. this category will not be displayed in any of the help categories.
  221. This is the default.
  222. </p>
  223. </dd>
  224. <dt><code>COMMAND_RUNNING</code>
  225. <a name="index-COMMAND_005fRUNNING-1"></a>
  226. </dt>
  227. <dd><p>The command is related to running the inferior. For example,
  228. <code>start</code>, <code>step</code>, and <code>continue</code> are in this category.
  229. Type <kbd>help running</kbd> at the <small>GDB</small> prompt to see a list of
  230. commands in this category.
  231. </p>
  232. </dd>
  233. <dt><code>COMMAND_DATA</code>
  234. <a name="index-COMMAND_005fDATA-1"></a>
  235. </dt>
  236. <dd><p>The command is related to data or variables. For example,
  237. <code>call</code>, <code>find</code>, and <code>print</code> are in this category. Type
  238. <kbd>help data</kbd> at the <small>GDB</small> prompt to see a list of commands
  239. in this category.
  240. </p>
  241. </dd>
  242. <dt><code>COMMAND_STACK</code>
  243. <a name="index-COMMAND_005fSTACK-1"></a>
  244. </dt>
  245. <dd><p>The command has to do with manipulation of the stack. For example,
  246. <code>backtrace</code>, <code>frame</code>, and <code>return</code> are in this
  247. category. Type <kbd>help stack</kbd> at the <small>GDB</small> prompt to see a
  248. list of commands in this category.
  249. </p>
  250. </dd>
  251. <dt><code>COMMAND_FILES</code>
  252. <a name="index-COMMAND_005fFILES-1"></a>
  253. </dt>
  254. <dd><p>This class is used for file-related commands. For example,
  255. <code>file</code>, <code>list</code> and <code>section</code> are in this category.
  256. Type <kbd>help files</kbd> at the <small>GDB</small> prompt to see a list of
  257. commands in this category.
  258. </p>
  259. </dd>
  260. <dt><code>COMMAND_SUPPORT</code>
  261. <a name="index-COMMAND_005fSUPPORT-1"></a>
  262. </dt>
  263. <dd><p>This should be used for &ldquo;support facilities&rdquo;, generally meaning
  264. things that are useful to the user when interacting with <small>GDB</small>,
  265. but not related to the state of the inferior. For example,
  266. <code>help</code>, <code>make</code>, and <code>shell</code> are in this category. Type
  267. <kbd>help support</kbd> at the <small>GDB</small> prompt to see a list of
  268. commands in this category.
  269. </p>
  270. </dd>
  271. <dt><code>COMMAND_STATUS</code>
  272. <a name="index-COMMAND_005fSTATUS-1"></a>
  273. </dt>
  274. <dd><p>The command is an &lsquo;<samp>info</samp>&rsquo;-related command, that is, related to the
  275. state of <small>GDB</small> itself. For example, <code>info</code>, <code>macro</code>,
  276. and <code>show</code> are in this category. Type <kbd>help status</kbd> at the
  277. <small>GDB</small> prompt to see a list of commands in this category.
  278. </p>
  279. </dd>
  280. <dt><code>COMMAND_BREAKPOINTS</code>
  281. <a name="index-COMMAND_005fBREAKPOINTS-1"></a>
  282. </dt>
  283. <dd><p>The command has to do with breakpoints. For example, <code>break</code>,
  284. <code>clear</code>, and <code>delete</code> are in this category. Type <kbd>help
  285. breakpoints</kbd> at the <small>GDB</small> prompt to see a list of commands in
  286. this category.
  287. </p>
  288. </dd>
  289. <dt><code>COMMAND_TRACEPOINTS</code>
  290. <a name="index-COMMAND_005fTRACEPOINTS-1"></a>
  291. </dt>
  292. <dd><p>The command has to do with tracepoints. For example, <code>trace</code>,
  293. <code>actions</code>, and <code>tfind</code> are in this category. Type
  294. <kbd>help tracepoints</kbd> at the <small>GDB</small> prompt to see a list of
  295. commands in this category.
  296. </p>
  297. </dd>
  298. <dt><code>COMMAND_USER</code>
  299. <a name="index-COMMAND_005fUSER-1"></a>
  300. </dt>
  301. <dd><p>The command is a general purpose command for the user, and typically
  302. does not fit in one of the other categories.
  303. Type <kbd>help user-defined</kbd> at the <small>GDB</small> prompt to see
  304. a list of commands in this category, as well as the list of gdb macros
  305. (see <a href="Sequences.html#Sequences">Sequences</a>).
  306. </p>
  307. </dd>
  308. <dt><code>COMMAND_OBSCURE</code>
  309. <a name="index-COMMAND_005fOBSCURE-1"></a>
  310. </dt>
  311. <dd><p>The command is only used in unusual circumstances, or is not of
  312. general interest to users. For example, <code>checkpoint</code>,
  313. <code>fork</code>, and <code>stop</code> are in this category. Type <kbd>help
  314. obscure</kbd> at the <small>GDB</small> prompt to see a list of commands in this
  315. category.
  316. </p>
  317. </dd>
  318. <dt><code>COMMAND_MAINTENANCE</code>
  319. <a name="index-COMMAND_005fMAINTENANCE-1"></a>
  320. </dt>
  321. <dd><p>The command is only useful to <small>GDB</small> maintainers. The
  322. <code>maintenance</code> and <code>flushregs</code> commands are in this category.
  323. Type <kbd>help internals</kbd> at the <small>GDB</small> prompt to see a list of
  324. commands in this category.
  325. </p></dd>
  326. </dl>
  327. <p>A new command can use a predefined completion function, either by
  328. specifying it via an argument at initialization, or by returning it
  329. from the <code>completer</code> procedure. These predefined completion
  330. constants are all defined in the <code>gdb</code> module:
  331. </p>
  332. <dl compact="compact">
  333. <dt><code>COMPLETE_NONE</code>
  334. <a name="index-COMPLETE_005fNONE-1"></a>
  335. </dt>
  336. <dd><p>This constant means that no completion should be done.
  337. </p>
  338. </dd>
  339. <dt><code>COMPLETE_FILENAME</code>
  340. <a name="index-COMPLETE_005fFILENAME-1"></a>
  341. </dt>
  342. <dd><p>This constant means that filename completion should be performed.
  343. </p>
  344. </dd>
  345. <dt><code>COMPLETE_LOCATION</code>
  346. <a name="index-COMPLETE_005fLOCATION-1"></a>
  347. </dt>
  348. <dd><p>This constant means that location completion should be done.
  349. See <a href="Specify-Location.html#Specify-Location">Specify Location</a>.
  350. </p>
  351. </dd>
  352. <dt><code>COMPLETE_COMMAND</code>
  353. <a name="index-COMPLETE_005fCOMMAND-1"></a>
  354. </dt>
  355. <dd><p>This constant means that completion should examine <small>GDB</small>
  356. command names.
  357. </p>
  358. </dd>
  359. <dt><code>COMPLETE_SYMBOL</code>
  360. <a name="index-COMPLETE_005fSYMBOL-1"></a>
  361. </dt>
  362. <dd><p>This constant means that completion should be done using symbol names
  363. as the source.
  364. </p>
  365. </dd>
  366. <dt><code>COMPLETE_EXPRESSION</code>
  367. <a name="index-COMPLETE_005fEXPRESSION-1"></a>
  368. </dt>
  369. <dd><p>This constant means that completion should be done on expressions.
  370. Often this means completing on symbol names, but some language
  371. parsers also have support for completing on field names.
  372. </p></dd>
  373. </dl>
  374. <p>The following code snippet shows how a trivial CLI command can be
  375. implemented in Guile:
  376. </p>
  377. <div class="smallexample">
  378. <pre class="smallexample">(gdb) guile
  379. (register-command! (make-command &quot;hello-world&quot;
  380. #:command-class COMMAND_USER
  381. #:doc &quot;Greet the whole world.&quot;
  382. #:invoke (lambda (self args from-tty) (display &quot;Hello, World!\n&quot;))))
  383. end
  384. (gdb) hello-world
  385. Hello, World!
  386. </pre></div>
  387. <hr>
  388. <div class="header">
  389. <p>
  390. Next: <a href="Parameters-In-Guile.html#Parameters-In-Guile" accesskey="n" rel="next">Parameters In Guile</a>, Previous: <a href="Writing-a-Guile-Pretty_002dPrinter.html#Writing-a-Guile-Pretty_002dPrinter" accesskey="p" rel="previous">Writing a Guile Pretty-Printer</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile 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>
  391. </div>
  392. </body>
  393. </html>