Parameters-In-Python.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: Parameters In Python</title>
  16. <meta name="description" content="Debugging with GDB: Parameters In Python">
  17. <meta name="keywords" content="Debugging with GDB: Parameters 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="Functions-In-Python.html#Functions-In-Python" rel="next" title="Functions In Python">
  27. <link href="Commands-In-Python.html#Commands-In-Python" rel="previous" title="Commands 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="Parameters-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Functions-In-Python.html#Functions-In-Python" accesskey="n" rel="next">Functions In Python</a>, Previous: <a href="Commands-In-Python.html#Commands-In-Python" accesskey="p" rel="previous">Commands 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="Parameters-In-Python-1"></a>
  65. <h4 class="subsubsection">23.2.2.21 Parameters In Python</h4>
  66. <a name="index-parameters-in-python"></a>
  67. <a name="index-python-parameters"></a>
  68. <a name="index-gdb_002eParameter"></a>
  69. <a name="index-Parameter"></a>
  70. <p>You can implement new <small>GDB</small> parameters using Python. A new
  71. parameter is implemented as an instance of the <code>gdb.Parameter</code>
  72. class.
  73. </p>
  74. <p>Parameters are exposed to the user via the <code>set</code> and
  75. <code>show</code> commands. See <a href="Help.html#Help">Help</a>.
  76. </p>
  77. <p>There are many parameters that already exist and can be set in
  78. <small>GDB</small>. Two examples are: <code>set follow fork</code> and
  79. <code>set charset</code>. Setting these parameters influences certain
  80. behavior in <small>GDB</small>. Similarly, you can define parameters that
  81. can be used to influence behavior in custom Python scripts and commands.
  82. </p>
  83. <dl>
  84. <dt><a name="index-Parameter_002e_005f_005finit_005f_005f"></a>Function: <strong>Parameter.__init__</strong> <em>(name, <var>command-class</var>, <var>parameter-class</var> <span class="roman">[</span>, <var>enum-sequence</var><span class="roman">]</span>)</em></dt>
  85. <dd><p>The object initializer for <code>Parameter</code> registers the new
  86. parameter with <small>GDB</small>. This initializer is normally invoked
  87. from the subclass&rsquo; own <code>__init__</code> method.
  88. </p>
  89. <p><var>name</var> is the name of the new parameter. If <var>name</var> consists
  90. of multiple words, then the initial words are looked for as prefix
  91. parameters. An example of this can be illustrated with the
  92. <code>set print</code> set of parameters. If <var>name</var> is
  93. <code>print foo</code>, then <code>print</code> will be searched as the prefix
  94. parameter. In this case the parameter can subsequently be accessed in
  95. <small>GDB</small> as <code>set print foo</code>.
  96. </p>
  97. <p>If <var>name</var> consists of multiple words, and no prefix parameter group
  98. can be found, an exception is raised.
  99. </p>
  100. <p><var>command-class</var> should be one of the &lsquo;<samp>COMMAND_</samp>&rsquo; constants
  101. (see <a href="Commands-In-Python.html#Commands-In-Python">Commands In Python</a>). This argument tells <small>GDB</small> how to
  102. categorize the new parameter in the help system.
  103. </p>
  104. <p><var>parameter-class</var> should be one of the &lsquo;<samp>PARAM_</samp>&rsquo; constants
  105. defined below. This argument tells <small>GDB</small> the type of the new
  106. parameter; this information is used for input validation and
  107. completion.
  108. </p>
  109. <p>If <var>parameter-class</var> is <code>PARAM_ENUM</code>, then
  110. <var>enum-sequence</var> must be a sequence of strings. These strings
  111. represent the possible values for the parameter.
  112. </p>
  113. <p>If <var>parameter-class</var> is not <code>PARAM_ENUM</code>, then the presence
  114. of a fourth argument will cause an exception to be thrown.
  115. </p>
  116. <p>The help text for the new parameter is taken from the Python
  117. documentation string for the parameter&rsquo;s class, if there is one. If
  118. there is no documentation string, a default value is used.
  119. </p></dd></dl>
  120. <dl>
  121. <dt><a name="index-Parameter_002eset_005fdoc"></a>Variable: <strong>Parameter.set_doc</strong></dt>
  122. <dd><p>If this attribute exists, and is a string, then its value is used as
  123. the help text for this parameter&rsquo;s <code>set</code> command. The value is
  124. examined when <code>Parameter.__init__</code> is invoked; subsequent changes
  125. have no effect.
  126. </p></dd></dl>
  127. <dl>
  128. <dt><a name="index-Parameter_002eshow_005fdoc"></a>Variable: <strong>Parameter.show_doc</strong></dt>
  129. <dd><p>If this attribute exists, and is a string, then its value is used as
  130. the help text for this parameter&rsquo;s <code>show</code> command. The value is
  131. examined when <code>Parameter.__init__</code> is invoked; subsequent changes
  132. have no effect.
  133. </p></dd></dl>
  134. <dl>
  135. <dt><a name="index-Parameter_002evalue"></a>Variable: <strong>Parameter.value</strong></dt>
  136. <dd><p>The <code>value</code> attribute holds the underlying value of the
  137. parameter. It can be read and assigned to just as any other
  138. attribute. <small>GDB</small> does validation when assignments are made.
  139. </p></dd></dl>
  140. <p>There are two methods that may be implemented in any <code>Parameter</code>
  141. class. These are:
  142. </p>
  143. <dl>
  144. <dt><a name="index-Parameter_002eget_005fset_005fstring"></a>Function: <strong>Parameter.get_set_string</strong> <em>(self)</em></dt>
  145. <dd><p>If this method exists, <small>GDB</small> will call it when a
  146. <var>parameter</var>&rsquo;s value has been changed via the <code>set</code> API (for
  147. example, <kbd>set foo off</kbd>). The <code>value</code> attribute has already
  148. been populated with the new value and may be used in output. This
  149. method must return a string. If the returned string is not empty,
  150. <small>GDB</small> will present it to the user.
  151. </p>
  152. <p>If this method raises the <code>gdb.GdbError</code> exception
  153. (see <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>), then <small>GDB</small> will print the
  154. exception&rsquo;s string and the <code>set</code> command will fail. Note,
  155. however, that the <code>value</code> attribute will not be reset in this
  156. case. So, if your parameter must validate values, it should store the
  157. old value internally and reset the exposed value, like so:
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">class ExampleParam (gdb.Parameter):
  161. def __init__ (self, name):
  162. super (ExampleParam, self).__init__ (name,
  163. gdb.COMMAND_DATA,
  164. gdb.PARAM_BOOLEAN)
  165. self.value = True
  166. self.saved_value = True
  167. def validate(self):
  168. return False
  169. def get_set_string (self):
  170. if not self.validate():
  171. self.value = self.saved_value
  172. raise gdb.GdbError('Failed to validate')
  173. self.saved_value = self.value
  174. </pre></div>
  175. </dd></dl>
  176. <dl>
  177. <dt><a name="index-Parameter_002eget_005fshow_005fstring"></a>Function: <strong>Parameter.get_show_string</strong> <em>(self, svalue)</em></dt>
  178. <dd><p><small>GDB</small> will call this method when a <var>parameter</var>&rsquo;s
  179. <code>show</code> API has been invoked (for example, <kbd>show foo</kbd>). The
  180. argument <code>svalue</code> receives the string representation of the
  181. current value. This method must return a string.
  182. </p></dd></dl>
  183. <p>When a new parameter is defined, its type must be specified. The
  184. available types are represented by constants defined in the <code>gdb</code>
  185. module:
  186. </p>
  187. <dl compact="compact">
  188. <dd><a name="index-PARAM_005fBOOLEAN"></a>
  189. <a name="index-gdb_002ePARAM_005fBOOLEAN"></a>
  190. </dd>
  191. <dt><code>gdb.PARAM_BOOLEAN</code></dt>
  192. <dd><p>The value is a plain boolean. The Python boolean values, <code>True</code>
  193. and <code>False</code> are the only valid values.
  194. </p>
  195. <a name="index-PARAM_005fAUTO_005fBOOLEAN"></a>
  196. <a name="index-gdb_002ePARAM_005fAUTO_005fBOOLEAN"></a>
  197. </dd>
  198. <dt><code>gdb.PARAM_AUTO_BOOLEAN</code></dt>
  199. <dd><p>The value has three possible states: true, false, and &lsquo;<samp>auto</samp>&rsquo;. In
  200. Python, true and false are represented using boolean constants, and
  201. &lsquo;<samp>auto</samp>&rsquo; is represented using <code>None</code>.
  202. </p>
  203. <a name="index-PARAM_005fUINTEGER"></a>
  204. <a name="index-gdb_002ePARAM_005fUINTEGER"></a>
  205. </dd>
  206. <dt><code>gdb.PARAM_UINTEGER</code></dt>
  207. <dd><p>The value is an unsigned integer. The value of 0 should be
  208. interpreted to mean &ldquo;unlimited&rdquo;.
  209. </p>
  210. <a name="index-PARAM_005fINTEGER"></a>
  211. <a name="index-gdb_002ePARAM_005fINTEGER"></a>
  212. </dd>
  213. <dt><code>gdb.PARAM_INTEGER</code></dt>
  214. <dd><p>The value is a signed integer. The value of 0 should be interpreted
  215. to mean &ldquo;unlimited&rdquo;.
  216. </p>
  217. <a name="index-PARAM_005fSTRING"></a>
  218. <a name="index-gdb_002ePARAM_005fSTRING"></a>
  219. </dd>
  220. <dt><code>gdb.PARAM_STRING</code></dt>
  221. <dd><p>The value is a string. When the user modifies the string, any escape
  222. sequences, such as &lsquo;<samp>\t</samp>&rsquo;, &lsquo;<samp>\f</samp>&rsquo;, and octal escapes, are
  223. translated into corresponding characters and encoded into the current
  224. host charset.
  225. </p>
  226. <a name="index-PARAM_005fSTRING_005fNOESCAPE"></a>
  227. <a name="index-gdb_002ePARAM_005fSTRING_005fNOESCAPE"></a>
  228. </dd>
  229. <dt><code>gdb.PARAM_STRING_NOESCAPE</code></dt>
  230. <dd><p>The value is a string. When the user modifies the string, escapes are
  231. passed through untranslated.
  232. </p>
  233. <a name="index-PARAM_005fOPTIONAL_005fFILENAME"></a>
  234. <a name="index-gdb_002ePARAM_005fOPTIONAL_005fFILENAME"></a>
  235. </dd>
  236. <dt><code>gdb.PARAM_OPTIONAL_FILENAME</code></dt>
  237. <dd><p>The value is a either a filename (a string), or <code>None</code>.
  238. </p>
  239. <a name="index-PARAM_005fFILENAME"></a>
  240. <a name="index-gdb_002ePARAM_005fFILENAME"></a>
  241. </dd>
  242. <dt><code>gdb.PARAM_FILENAME</code></dt>
  243. <dd><p>The value is a filename. This is just like
  244. <code>PARAM_STRING_NOESCAPE</code>, but uses file names for completion.
  245. </p>
  246. <a name="index-PARAM_005fZINTEGER"></a>
  247. <a name="index-gdb_002ePARAM_005fZINTEGER"></a>
  248. </dd>
  249. <dt><code>gdb.PARAM_ZINTEGER</code></dt>
  250. <dd><p>The value is an integer. This is like <code>PARAM_INTEGER</code>, except 0
  251. is interpreted as itself.
  252. </p>
  253. <a name="index-PARAM_005fZUINTEGER"></a>
  254. <a name="index-gdb_002ePARAM_005fZUINTEGER"></a>
  255. </dd>
  256. <dt><code>gdb.PARAM_ZUINTEGER</code></dt>
  257. <dd><p>The value is an unsigned integer. This is like <code>PARAM_INTEGER</code>,
  258. except 0 is interpreted as itself, and the value cannot be negative.
  259. </p>
  260. <a name="index-PARAM_005fZUINTEGER_005fUNLIMITED"></a>
  261. <a name="index-gdb_002ePARAM_005fZUINTEGER_005fUNLIMITED"></a>
  262. </dd>
  263. <dt><code>gdb.PARAM_ZUINTEGER_UNLIMITED</code></dt>
  264. <dd><p>The value is a signed integer. This is like <code>PARAM_ZUINTEGER</code>,
  265. except the special value -1 should be interpreted to mean
  266. &ldquo;unlimited&rdquo;. Other negative values are not allowed.
  267. </p>
  268. <a name="index-PARAM_005fENUM"></a>
  269. <a name="index-gdb_002ePARAM_005fENUM"></a>
  270. </dd>
  271. <dt><code>gdb.PARAM_ENUM</code></dt>
  272. <dd><p>The value is a string, which must be one of a collection string
  273. constants provided when the parameter is created.
  274. </p></dd>
  275. </dl>
  276. <hr>
  277. <div class="header">
  278. <p>
  279. Next: <a href="Functions-In-Python.html#Functions-In-Python" accesskey="n" rel="next">Functions In Python</a>, Previous: <a href="Commands-In-Python.html#Commands-In-Python" accesskey="p" rel="previous">Commands 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>
  280. </div>
  281. </body>
  282. </html>