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-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Parameters In Python</title>
18
19<meta name="description" content="Debugging with GDB: Parameters In Python">
20<meta name="keywords" content="Debugging with GDB: Parameters In Python">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Python-API.html#Python-API" rel="up" title="Python API">
29<link href="Functions-In-Python.html#Functions-In-Python" rel="next" title="Functions In Python">
30<link href="Commands-In-Python.html#Commands-In-Python" rel="previous" title="Commands In Python">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Parameters-In-Python"></a>
65<div class="header">
66<p>
67Next: <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>
68</div>
69<hr>
70<a name="Parameters-In-Python-1"></a>
71<h4 class="subsubsection">23.2.2.21 Parameters In Python</h4>
72
73<a name="index-parameters-in-python"></a>
74<a name="index-python-parameters"></a>
75<a name="index-gdb_002eParameter"></a>
76<a name="index-Parameter"></a>
77<p>You can implement new <small>GDB</small> parameters using Python.  A new
78parameter is implemented as an instance of the <code>gdb.Parameter</code>
79class.
80</p>
81<p>Parameters are exposed to the user via the <code>set</code> and
82<code>show</code> commands.  See <a href="Help.html#Help">Help</a>.
83</p>
84<p>There are many parameters that already exist and can be set in
85<small>GDB</small>.  Two examples are: <code>set follow fork</code> and
86<code>set charset</code>.  Setting these parameters influences certain
87behavior in <small>GDB</small>.  Similarly, you can define parameters that
88can be used to influence behavior in custom Python scripts and commands.
89</p>
90<dl>
91<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>
92<dd><p>The object initializer for <code>Parameter</code> registers the new
93parameter with <small>GDB</small>.  This initializer is normally invoked
94from the subclass&rsquo; own <code>__init__</code> method.
95</p>
96<p><var>name</var> is the name of the new parameter.  If <var>name</var> consists
97of multiple words, then the initial words are looked for as prefix
98parameters.  An example of this can be illustrated with the
99<code>set print</code> set of parameters.  If <var>name</var> is
100<code>print foo</code>, then <code>print</code> will be searched as the prefix
101parameter.  In this case the parameter can subsequently be accessed in
102<small>GDB</small> as <code>set print foo</code>.
103</p>
104<p>If <var>name</var> consists of multiple words, and no prefix parameter group
105can be found, an exception is raised.
106</p>
107<p><var>command-class</var> should be one of the &lsquo;<samp>COMMAND_</samp>&rsquo; constants
108(see <a href="Commands-In-Python.html#Commands-In-Python">Commands In Python</a>).  This argument tells <small>GDB</small> how to
109categorize the new parameter in the help system.
110</p>
111<p><var>parameter-class</var> should be one of the &lsquo;<samp>PARAM_</samp>&rsquo; constants
112defined below.  This argument tells <small>GDB</small> the type of the new
113parameter; this information is used for input validation and
114completion.
115</p>
116<p>If <var>parameter-class</var> is <code>PARAM_ENUM</code>, then
117<var>enum-sequence</var> must be a sequence of strings.  These strings
118represent the possible values for the parameter.
119</p>
120<p>If <var>parameter-class</var> is not <code>PARAM_ENUM</code>, then the presence
121of a fourth argument will cause an exception to be thrown.
122</p>
123<p>The help text for the new parameter is taken from the Python
124documentation string for the parameter&rsquo;s class, if there is one.  If
125there is no documentation string, a default value is used.
126</p></dd></dl>
127
128<dl>
129<dt><a name="index-Parameter_002eset_005fdoc"></a>Variable: <strong>Parameter.set_doc</strong></dt>
130<dd><p>If this attribute exists, and is a string, then its value is used as
131the help text for this parameter&rsquo;s <code>set</code> command.  The value is
132examined when <code>Parameter.__init__</code> is invoked; subsequent changes
133have no effect.
134</p></dd></dl>
135
136<dl>
137<dt><a name="index-Parameter_002eshow_005fdoc"></a>Variable: <strong>Parameter.show_doc</strong></dt>
138<dd><p>If this attribute exists, and is a string, then its value is used as
139the help text for this parameter&rsquo;s <code>show</code> command.  The value is
140examined when <code>Parameter.__init__</code> is invoked; subsequent changes
141have no effect.
142</p></dd></dl>
143
144<dl>
145<dt><a name="index-Parameter_002evalue"></a>Variable: <strong>Parameter.value</strong></dt>
146<dd><p>The <code>value</code> attribute holds the underlying value of the
147parameter.  It can be read and assigned to just as any other
148attribute.  <small>GDB</small> does validation when assignments are made.
149</p></dd></dl>
150
151<p>There are two methods that may be implemented in any <code>Parameter</code>
152class.  These are:
153</p>
154<dl>
155<dt><a name="index-Parameter_002eget_005fset_005fstring"></a>Function: <strong>Parameter.get_set_string</strong> <em>(self)</em></dt>
156<dd><p>If this method exists, <small>GDB</small> will call it when a
157<var>parameter</var>&rsquo;s value has been changed via the <code>set</code> API (for
158example, <kbd>set foo off</kbd>).  The <code>value</code> attribute has already
159been populated with the new value and may be used in output.  This
160method must return a string.  If the returned string is not empty,
161<small>GDB</small> will present it to the user.
162</p>
163<p>If this method raises the <code>gdb.GdbError</code> exception
164(see <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>), then <small>GDB</small> will print the
165exception&rsquo;s string and the <code>set</code> command will fail.  Note,
166however, that the <code>value</code> attribute will not be reset in this
167case.  So, if your parameter must validate values, it should store the
168old value internally and reset the exposed value, like so:
169</p>
170<div class="smallexample">
171<pre class="smallexample">class ExampleParam (gdb.Parameter):
172   def __init__ (self, name):
173      super (ExampleParam, self).__init__ (name,
174                   gdb.COMMAND_DATA,
175                   gdb.PARAM_BOOLEAN)
176      self.value = True
177      self.saved_value = True
178   def validate(self):
179      return False
180   def get_set_string (self):
181      if not self.validate():
182        self.value = self.saved_value
183        raise gdb.GdbError('Failed to validate')
184      self.saved_value = self.value
185</pre></div>
186</dd></dl>
187
188<dl>
189<dt><a name="index-Parameter_002eget_005fshow_005fstring"></a>Function: <strong>Parameter.get_show_string</strong> <em>(self, svalue)</em></dt>
190<dd><p><small>GDB</small> will call this method when a <var>parameter</var>&rsquo;s
191<code>show</code> API has been invoked (for example, <kbd>show foo</kbd>).  The
192argument <code>svalue</code> receives the string representation of the
193current value.  This method must return a string.
194</p></dd></dl>
195
196<p>When a new parameter is defined, its type must be specified.  The
197available types are represented by constants defined in the <code>gdb</code>
198module:
199</p>
200<dl compact="compact">
201<dd><a name="index-PARAM_005fBOOLEAN"></a>
202<a name="index-gdb_002ePARAM_005fBOOLEAN"></a>
203</dd>
204<dt><code>gdb.PARAM_BOOLEAN</code></dt>
205<dd><p>The value is a plain boolean.  The Python boolean values, <code>True</code>
206and <code>False</code> are the only valid values.
207</p>
208<a name="index-PARAM_005fAUTO_005fBOOLEAN"></a>
209<a name="index-gdb_002ePARAM_005fAUTO_005fBOOLEAN"></a>
210</dd>
211<dt><code>gdb.PARAM_AUTO_BOOLEAN</code></dt>
212<dd><p>The value has three possible states: true, false, and &lsquo;<samp>auto</samp>&rsquo;.  In
213Python, true and false are represented using boolean constants, and
214&lsquo;<samp>auto</samp>&rsquo; is represented using <code>None</code>.
215</p>
216<a name="index-PARAM_005fUINTEGER"></a>
217<a name="index-gdb_002ePARAM_005fUINTEGER"></a>
218</dd>
219<dt><code>gdb.PARAM_UINTEGER</code></dt>
220<dd><p>The value is an unsigned integer.  The value of 0 should be
221interpreted to mean &ldquo;unlimited&rdquo;.
222</p>
223<a name="index-PARAM_005fINTEGER"></a>
224<a name="index-gdb_002ePARAM_005fINTEGER"></a>
225</dd>
226<dt><code>gdb.PARAM_INTEGER</code></dt>
227<dd><p>The value is a signed integer.  The value of 0 should be interpreted
228to mean &ldquo;unlimited&rdquo;.
229</p>
230<a name="index-PARAM_005fSTRING"></a>
231<a name="index-gdb_002ePARAM_005fSTRING"></a>
232</dd>
233<dt><code>gdb.PARAM_STRING</code></dt>
234<dd><p>The value is a string.  When the user modifies the string, any escape
235sequences, such as &lsquo;<samp>\t</samp>&rsquo;, &lsquo;<samp>\f</samp>&rsquo;, and octal escapes, are
236translated into corresponding characters and encoded into the current
237host charset.
238</p>
239<a name="index-PARAM_005fSTRING_005fNOESCAPE"></a>
240<a name="index-gdb_002ePARAM_005fSTRING_005fNOESCAPE"></a>
241</dd>
242<dt><code>gdb.PARAM_STRING_NOESCAPE</code></dt>
243<dd><p>The value is a string.  When the user modifies the string, escapes are
244passed through untranslated.
245</p>
246<a name="index-PARAM_005fOPTIONAL_005fFILENAME"></a>
247<a name="index-gdb_002ePARAM_005fOPTIONAL_005fFILENAME"></a>
248</dd>
249<dt><code>gdb.PARAM_OPTIONAL_FILENAME</code></dt>
250<dd><p>The value is a either a filename (a string), or <code>None</code>.
251</p>
252<a name="index-PARAM_005fFILENAME"></a>
253<a name="index-gdb_002ePARAM_005fFILENAME"></a>
254</dd>
255<dt><code>gdb.PARAM_FILENAME</code></dt>
256<dd><p>The value is a filename.  This is just like
257<code>PARAM_STRING_NOESCAPE</code>, but uses file names for completion.
258</p>
259<a name="index-PARAM_005fZINTEGER"></a>
260<a name="index-gdb_002ePARAM_005fZINTEGER"></a>
261</dd>
262<dt><code>gdb.PARAM_ZINTEGER</code></dt>
263<dd><p>The value is an integer.  This is like <code>PARAM_INTEGER</code>, except 0
264is interpreted as itself.
265</p>
266<a name="index-PARAM_005fZUINTEGER"></a>
267<a name="index-gdb_002ePARAM_005fZUINTEGER"></a>
268</dd>
269<dt><code>gdb.PARAM_ZUINTEGER</code></dt>
270<dd><p>The value is an unsigned integer.  This is like <code>PARAM_INTEGER</code>,
271except 0 is interpreted as itself, and the value cannot be negative.
272</p>
273<a name="index-PARAM_005fZUINTEGER_005fUNLIMITED"></a>
274<a name="index-gdb_002ePARAM_005fZUINTEGER_005fUNLIMITED"></a>
275</dd>
276<dt><code>gdb.PARAM_ZUINTEGER_UNLIMITED</code></dt>
277<dd><p>The value is a signed integer.  This is like <code>PARAM_ZUINTEGER</code>,
278except the special value -1 should be interpreted to mean
279&ldquo;unlimited&rdquo;.  Other negative values are not allowed.
280</p>
281<a name="index-PARAM_005fENUM"></a>
282<a name="index-gdb_002ePARAM_005fENUM"></a>
283</dd>
284<dt><code>gdb.PARAM_ENUM</code></dt>
285<dd><p>The value is a string, which must be one of a collection string
286constants provided when the parameter is created.
287</p></dd>
288</dl>
289
290<hr>
291<div class="header">
292<p>
293Next: <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>
294</div>
295
296
297
298</body>
299</html>
300