You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.9KB

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