Writing-an-Xmethod.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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: Writing an Xmethod</title>
  16. <meta name="description" content="Debugging with GDB: Writing an Xmethod">
  17. <meta name="keywords" content="Debugging with GDB: Writing an Xmethod">
  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="Inferiors-In-Python.html#Inferiors-In-Python" rel="next" title="Inferiors In Python">
  27. <link href="Xmethod-API.html#Xmethod-API" rel="previous" title="Xmethod API">
  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="Writing-an-Xmethod"></a>
  59. <div class="header">
  60. <p>
  61. 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>
  62. </div>
  63. <hr>
  64. <a name="Writing-an-Xmethod-1"></a>
  65. <h4 class="subsubsection">23.2.2.15 Writing an Xmethod</h4>
  66. <a name="index-writing-xmethods-in-Python"></a>
  67. <p>Implementing xmethods in Python will require implementing xmethod
  68. matchers and xmethod workers (see <a href="Xmethods-In-Python.html#Xmethods-In-Python">Xmethods In Python</a>). Consider
  69. the following C<tt>++</tt> class:
  70. </p>
  71. <div class="smallexample">
  72. <pre class="smallexample">class MyClass
  73. {
  74. public:
  75. MyClass (int a) : a_(a) { }
  76. int geta (void) { return a_; }
  77. int operator+ (int b);
  78. private:
  79. int a_;
  80. };
  81. int
  82. MyClass::operator+ (int b)
  83. {
  84. return a_ + b;
  85. }
  86. </pre></div>
  87. <p>Let us define two xmethods for the class <code>MyClass</code>, one
  88. replacing the method <code>geta</code>, and another adding an overloaded
  89. flavor of <code>operator+</code> which takes a <code>MyClass</code> argument (the
  90. C<tt>++</tt> code above already has an overloaded <code>operator+</code>
  91. which takes an <code>int</code> argument). The xmethod matcher can be
  92. defined as follows:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">class MyClass_geta(gdb.xmethod.XMethod):
  96. def __init__(self):
  97. gdb.xmethod.XMethod.__init__(self, 'geta')
  98. def get_worker(self, method_name):
  99. if method_name == 'geta':
  100. return MyClassWorker_geta()
  101. class MyClass_sum(gdb.xmethod.XMethod):
  102. def __init__(self):
  103. gdb.xmethod.XMethod.__init__(self, 'sum')
  104. def get_worker(self, method_name):
  105. if method_name == 'operator+':
  106. return MyClassWorker_plus()
  107. class MyClassMatcher(gdb.xmethod.XMethodMatcher):
  108. def __init__(self):
  109. gdb.xmethod.XMethodMatcher.__init__(self, 'MyClassMatcher')
  110. # List of methods 'managed' by this matcher
  111. self.methods = [MyClass_geta(), MyClass_sum()]
  112. def match(self, class_type, method_name):
  113. if class_type.tag != 'MyClass':
  114. return None
  115. workers = []
  116. for method in self.methods:
  117. if method.enabled:
  118. worker = method.get_worker(method_name)
  119. if worker:
  120. workers.append(worker)
  121. return workers
  122. </pre></div>
  123. <p>Notice that the <code>match</code> method of <code>MyClassMatcher</code> returns
  124. a worker object of type <code>MyClassWorker_geta</code> for the <code>geta</code>
  125. method, and a worker object of type <code>MyClassWorker_plus</code> for the
  126. <code>operator+</code> method. This is done indirectly via helper classes
  127. derived from <code>gdb.xmethod.XMethod</code>. One does not need to use the
  128. <code>methods</code> attribute in a matcher as it is optional. However, if a
  129. matcher manages more than one xmethod, it is a good practice to list the
  130. xmethods in the <code>methods</code> attribute of the matcher. This will then
  131. facilitate enabling and disabling individual xmethods via the
  132. <code>enable/disable</code> commands. Notice also that a worker object is
  133. returned only if the corresponding entry in the <code>methods</code> attribute
  134. of the matcher is enabled.
  135. </p>
  136. <p>The implementation of the worker classes returned by the matcher setup
  137. above is as follows:
  138. </p>
  139. <div class="smallexample">
  140. <pre class="smallexample">class MyClassWorker_geta(gdb.xmethod.XMethodWorker):
  141. def get_arg_types(self):
  142. return None
  143. def get_result_type(self, obj):
  144. return gdb.lookup_type('int')
  145. def __call__(self, obj):
  146. return obj['a_']
  147. class MyClassWorker_plus(gdb.xmethod.XMethodWorker):
  148. def get_arg_types(self):
  149. return gdb.lookup_type('MyClass')
  150. def get_result_type(self, obj):
  151. return gdb.lookup_type('int')
  152. def __call__(self, obj, other):
  153. return obj['a_'] + other['a_']
  154. </pre></div>
  155. <p>For <small>GDB</small> to actually lookup a xmethod, it has to be
  156. registered with it. The matcher defined above is registered with
  157. <small>GDB</small> globally as follows:
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">gdb.xmethod.register_xmethod_matcher(None, MyClassMatcher())
  161. </pre></div>
  162. <p>If an object <code>obj</code> of type <code>MyClass</code> is initialized in C<tt>++</tt>
  163. code as follows:
  164. </p>
  165. <div class="smallexample">
  166. <pre class="smallexample">MyClass obj(5);
  167. </pre></div>
  168. <p>then, after loading the Python script defining the xmethod matchers
  169. and workers into <code>GDBN</code>, invoking the method <code>geta</code> or using
  170. the operator <code>+</code> on <code>obj</code> will invoke the xmethods
  171. defined above:
  172. </p>
  173. <div class="smallexample">
  174. <pre class="smallexample">(gdb) p obj.geta()
  175. $1 = 5
  176. (gdb) p obj + obj
  177. $2 = 10
  178. </pre></div>
  179. <p>Consider another example with a C++ template class:
  180. </p>
  181. <div class="smallexample">
  182. <pre class="smallexample">template &lt;class T&gt;
  183. class MyTemplate
  184. {
  185. public:
  186. MyTemplate () : dsize_(10), data_ (new T [10]) { }
  187. ~MyTemplate () { delete [] data_; }
  188. int footprint (void)
  189. {
  190. return sizeof (T) * dsize_ + sizeof (MyTemplate&lt;T&gt;);
  191. }
  192. private:
  193. int dsize_;
  194. T *data_;
  195. };
  196. </pre></div>
  197. <p>Let us implement an xmethod for the above class which serves as a
  198. replacement for the <code>footprint</code> method. The full code listing
  199. of the xmethod workers and xmethod matchers is as follows:
  200. </p>
  201. <div class="smallexample">
  202. <pre class="smallexample">class MyTemplateWorker_footprint(gdb.xmethod.XMethodWorker):
  203. def __init__(self, class_type):
  204. self.class_type = class_type
  205. def get_arg_types(self):
  206. return None
  207. def get_result_type(self):
  208. return gdb.lookup_type('int')
  209. def __call__(self, obj):
  210. return (self.class_type.sizeof +
  211. obj['dsize_'] *
  212. self.class_type.template_argument(0).sizeof)
  213. class MyTemplateMatcher_footprint(gdb.xmethod.XMethodMatcher):
  214. def __init__(self):
  215. gdb.xmethod.XMethodMatcher.__init__(self, 'MyTemplateMatcher')
  216. def match(self, class_type, method_name):
  217. if (re.match('MyTemplate&lt;[ \t\n]*[_a-zA-Z][ _a-zA-Z0-9]*&gt;',
  218. class_type.tag) and
  219. method_name == 'footprint'):
  220. return MyTemplateWorker_footprint(class_type)
  221. </pre></div>
  222. <p>Notice that, in this example, we have not used the <code>methods</code>
  223. attribute of the matcher as the matcher manages only one xmethod. The
  224. user can enable/disable this xmethod by enabling/disabling the matcher
  225. itself.
  226. </p>
  227. <hr>
  228. <div class="header">
  229. <p>
  230. 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>
  231. </div>
  232. </body>
  233. </html>