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