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
<!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: Writing an Xmethod</title>
 
<meta name="description" content="Debugging with GDB: Writing an Xmethod">
<meta name="keywords" content="Debugging with GDB: Writing an Xmethod">
<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="Inferiors-In-Python.html#Inferiors-In-Python" rel="next" title="Inferiors In Python">
<link href="Xmethod-API.html#Xmethod-API" rel="previous" title="Xmethod API">
<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="Writing-an-Xmethod"></a>
<div class="header">
<p>
Next: <a href="Inferiors-In-Python.html#Inferiors-In-Python" accesskey="n" rel="next">Inferiors In Python</a>, Previous: <a href="Xmethod-API.html#Xmethod-API" accesskey="p" rel="previous">Xmethod API</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="Writing-an-Xmethod-1"></a>
<h4 class="subsubsection">23.2.2.15 Writing an Xmethod</h4>
<a name="index-writing-xmethods-in-Python"></a>
 
<p>Implementing xmethods in Python will require implementing xmethod
matchers and xmethod workers (see <a href="Xmethods-In-Python.html#Xmethods-In-Python">Xmethods In Python</a>).  Consider
the following C<tt>++</tt> class:
</p>
<div class="smallexample">
<pre class="smallexample">class MyClass
{
public:
  MyClass (int a) : a_(a) { }
 
  int geta (void) { return a_; }
  int operator+ (int b);
 
private:
  int a_;
};
 
int
MyClass::operator+ (int b)
{
  return a_ + b;
}
</pre></div>
 
<p>Let us define two xmethods for the class <code>MyClass</code>, one
replacing the method <code>geta</code>, and another adding an overloaded
flavor of <code>operator+</code> which takes a <code>MyClass</code> argument (the
C<tt>++</tt> code above already has an overloaded <code>operator+</code>
which takes an <code>int</code> argument).  The xmethod matcher can be
defined as follows:
</p>
<div class="smallexample">
<pre class="smallexample">class MyClass_geta(gdb.xmethod.XMethod):
    def __init__(self):
        gdb.xmethod.XMethod.__init__(self, 'geta')
 
    def get_worker(self, method_name):
        if method_name == 'geta':
            return MyClassWorker_geta()
 
 
class MyClass_sum(gdb.xmethod.XMethod):
    def __init__(self):
        gdb.xmethod.XMethod.__init__(self, 'sum')
 
    def get_worker(self, method_name):
        if method_name == 'operator+':
            return MyClassWorker_plus()
 
 
class MyClassMatcher(gdb.xmethod.XMethodMatcher):
    def __init__(self):
        gdb.xmethod.XMethodMatcher.__init__(self, 'MyClassMatcher')
        # List of methods 'managed' by this matcher
        self.methods = [MyClass_geta(), MyClass_sum()]
 
    def match(self, class_type, method_name):
        if class_type.tag != 'MyClass':
            return None
        workers = []
        for method in self.methods:
            if method.enabled:
                worker = method.get_worker(method_name)
                if worker:
                    workers.append(worker)
 
        return workers
</pre></div>
 
<p>Notice that the <code>match</code> method of <code>MyClassMatcher</code> returns
a worker object of type <code>MyClassWorker_geta</code> for the <code>geta</code>
method, and a worker object of type <code>MyClassWorker_plus</code> for the
<code>operator+</code> method.  This is done indirectly via helper classes
derived from <code>gdb.xmethod.XMethod</code>.  One does not need to use the
<code>methods</code> attribute in a matcher as it is optional.  However, if a
matcher manages more than one xmethod, it is a good practice to list the
xmethods in the <code>methods</code> attribute of the matcher.  This will then
facilitate enabling and disabling individual xmethods via the
<code>enable/disable</code> commands.  Notice also that a worker object is
returned only if the corresponding entry in the <code>methods</code> attribute
of the matcher is enabled.
</p>
<p>The implementation of the worker classes returned by the matcher setup
above is as follows:
</p>
<div class="smallexample">
<pre class="smallexample">class MyClassWorker_geta(gdb.xmethod.XMethodWorker):
    def get_arg_types(self):
        return None
 
    def get_result_type(self, obj):
        return gdb.lookup_type('int')
 
    def __call__(self, obj):
        return obj['a_']
 
 
class MyClassWorker_plus(gdb.xmethod.XMethodWorker):
    def get_arg_types(self):
        return gdb.lookup_type('MyClass')
 
    def get_result_type(self, obj):
        return gdb.lookup_type('int')
 
    def __call__(self, obj, other):
        return obj['a_'] + other['a_']
</pre></div>
 
<p>For <small>GDB</small> to actually lookup a xmethod, it has to be
registered with it.  The matcher defined above is registered with
<small>GDB</small> globally as follows:
</p>
<div class="smallexample">
<pre class="smallexample">gdb.xmethod.register_xmethod_matcher(None, MyClassMatcher())
</pre></div>
 
<p>If an object <code>obj</code> of type <code>MyClass</code> is initialized in C<tt>++</tt>
code as follows:
</p>
<div class="smallexample">
<pre class="smallexample">MyClass obj(5);
</pre></div>
 
<p>then, after loading the Python script defining the xmethod matchers
and workers into <code>GDBN</code>, invoking the method <code>geta</code> or using
the operator <code>+</code> on <code>obj</code> will invoke the xmethods
defined above:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) p obj.geta()
$1 = 5
 
(gdb) p obj + obj
$2 = 10
</pre></div>
 
<p>Consider another example with a C++ template class:
</p>
<div class="smallexample">
<pre class="smallexample">template &lt;class T&gt;
class MyTemplate
{
public:
  MyTemplate () : dsize_(10), data_ (new T [10]) { }
  ~MyTemplate () { delete [] data_; }
 
  int footprint (void)
  {
    return sizeof (T) * dsize_ + sizeof (MyTemplate&lt;T&gt;);
  }
 
private:
  int dsize_;
  T *data_;
};
</pre></div>
 
<p>Let us implement an xmethod for the above class which serves as a
replacement for the <code>footprint</code> method.  The full code listing
of the xmethod workers and xmethod matchers is as follows:
</p>
<div class="smallexample">
<pre class="smallexample">class MyTemplateWorker_footprint(gdb.xmethod.XMethodWorker):
    def __init__(self, class_type):
        self.class_type = class_type
 
    def get_arg_types(self):
        return None
 
    def get_result_type(self):
        return gdb.lookup_type('int')
 
    def __call__(self, obj):
        return (self.class_type.sizeof +
                obj['dsize_'] *
                self.class_type.template_argument(0).sizeof)
 
 
class MyTemplateMatcher_footprint(gdb.xmethod.XMethodMatcher):
    def __init__(self):
        gdb.xmethod.XMethodMatcher.__init__(self, 'MyTemplateMatcher')
 
    def match(self, class_type, method_name):
        if (re.match('MyTemplate&lt;[ \t\n]*[_a-zA-Z][ _a-zA-Z0-9]*&gt;',
                     class_type.tag) and
            method_name == 'footprint'):
            return MyTemplateWorker_footprint(class_type)
</pre></div>
 
<p>Notice that, in this example, we have not used the <code>methods</code>
attribute of the matcher as the matcher manages only one xmethod.  The
user can enable/disable this xmethod by enabling/disabling the matcher
itself.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Inferiors-In-Python.html#Inferiors-In-Python" accesskey="n" rel="next">Inferiors In Python</a>, Previous: <a href="Xmethod-API.html#Xmethod-API" accesskey="p" rel="previous">Xmethod API</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>