Writing-a-Pretty_002dPrinter.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 a Pretty-Printer</title>
  16. <meta name="description" content="Debugging with GDB: Writing a Pretty-Printer">
  17. <meta name="keywords" content="Debugging with GDB: Writing a Pretty-Printer">
  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="Type-Printing-API.html#Type-Printing-API" rel="next" title="Type Printing API">
  27. <link href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" rel="previous" title="Selecting Pretty-Printers">
  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-a-Pretty_002dPrinter"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Type-Printing-API.html#Type-Printing-API" accesskey="n" rel="next">Type Printing API</a>, Previous: <a href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" accesskey="p" rel="previous">Selecting Pretty-Printers</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-a-Pretty_002dPrinter-1"></a>
  65. <h4 class="subsubsection">23.2.2.7 Writing a Pretty-Printer</h4>
  66. <a name="index-writing-a-pretty_002dprinter"></a>
  67. <p>A pretty-printer consists of two parts: a lookup function to detect
  68. if the type is supported, and the printer itself.
  69. </p>
  70. <p>Here is an example showing how a <code>std::string</code> printer might be
  71. written. See <a href="Pretty-Printing-API.html#Pretty-Printing-API">Pretty Printing API</a>, for details on the API this class
  72. must provide.
  73. </p>
  74. <div class="smallexample">
  75. <pre class="smallexample">class StdStringPrinter(object):
  76. &quot;Print a std::string&quot;
  77. def __init__(self, val):
  78. self.val = val
  79. def to_string(self):
  80. return self.val['_M_dataplus']['_M_p']
  81. def display_hint(self):
  82. return 'string'
  83. </pre></div>
  84. <p>And here is an example showing how a lookup function for the printer
  85. example above might be written.
  86. </p>
  87. <div class="smallexample">
  88. <pre class="smallexample">def str_lookup_function(val):
  89. lookup_tag = val.type.tag
  90. if lookup_tag == None:
  91. return None
  92. regex = re.compile(&quot;^std::basic_string&lt;char,.*&gt;$&quot;)
  93. if regex.match(lookup_tag):
  94. return StdStringPrinter(val)
  95. return None
  96. </pre></div>
  97. <p>The example lookup function extracts the value&rsquo;s type, and attempts to
  98. match it to a type that it can pretty-print. If it is a type the
  99. printer can pretty-print, it will return a printer object. If not, it
  100. returns <code>None</code>.
  101. </p>
  102. <p>We recommend that you put your core pretty-printers into a Python
  103. package. If your pretty-printers are for use with a library, we
  104. further recommend embedding a version number into the package name.
  105. This practice will enable <small>GDB</small> to load multiple versions of
  106. your pretty-printers at the same time, because they will have
  107. different names.
  108. </p>
  109. <p>You should write auto-loaded code (see <a href="Python-Auto_002dloading.html#Python-Auto_002dloading">Python Auto-loading</a>) such that it
  110. can be evaluated multiple times without changing its meaning. An
  111. ideal auto-load file will consist solely of <code>import</code>s of your
  112. printer modules, followed by a call to a register pretty-printers with
  113. the current objfile.
  114. </p>
  115. <p>Taken as a whole, this approach will scale nicely to multiple
  116. inferiors, each potentially using a different library version.
  117. Embedding a version number in the Python package name will ensure that
  118. <small>GDB</small> is able to load both sets of printers simultaneously.
  119. Then, because the search for pretty-printers is done by objfile, and
  120. because your auto-loaded code took care to register your library&rsquo;s
  121. printers with a specific objfile, <small>GDB</small> will find the correct
  122. printers for the specific version of the library used by each
  123. inferior.
  124. </p>
  125. <p>To continue the <code>std::string</code> example (see <a href="Pretty-Printing-API.html#Pretty-Printing-API">Pretty Printing API</a>),
  126. this code might appear in <code>gdb.libstdcxx.v6</code>:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">def register_printers(objfile):
  130. objfile.pretty_printers.append(str_lookup_function)
  131. </pre></div>
  132. <p>And then the corresponding contents of the auto-load file would be:
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">import gdb.libstdcxx.v6
  136. gdb.libstdcxx.v6.register_printers(gdb.current_objfile())
  137. </pre></div>
  138. <p>The previous example illustrates a basic pretty-printer.
  139. There are a few things that can be improved on.
  140. The printer doesn&rsquo;t have a name, making it hard to identify in a
  141. list of installed printers. The lookup function has a name, but
  142. lookup functions can have arbitrary, even identical, names.
  143. </p>
  144. <p>Second, the printer only handles one type, whereas a library typically has
  145. several types. One could install a lookup function for each desired type
  146. in the library, but one could also have a single lookup function recognize
  147. several types. The latter is the conventional way this is handled.
  148. If a pretty-printer can handle multiple data types, then its
  149. <em>subprinters</em> are the printers for the individual data types.
  150. </p>
  151. <p>The <code>gdb.printing</code> module provides a formal way of solving these
  152. problems (see <a href="gdb_002eprinting.html#gdb_002eprinting">gdb.printing</a>).
  153. Here is another example that handles multiple types.
  154. </p>
  155. <p>These are the types we are going to pretty-print:
  156. </p>
  157. <div class="smallexample">
  158. <pre class="smallexample">struct foo { int a, b; };
  159. struct bar { struct foo x, y; };
  160. </pre></div>
  161. <p>Here are the printers:
  162. </p>
  163. <div class="smallexample">
  164. <pre class="smallexample">class fooPrinter:
  165. &quot;&quot;&quot;Print a foo object.&quot;&quot;&quot;
  166. def __init__(self, val):
  167. self.val = val
  168. def to_string(self):
  169. return (&quot;a=&lt;&quot; + str(self.val[&quot;a&quot;]) +
  170. &quot;&gt; b=&lt;&quot; + str(self.val[&quot;b&quot;]) + &quot;&gt;&quot;)
  171. class barPrinter:
  172. &quot;&quot;&quot;Print a bar object.&quot;&quot;&quot;
  173. def __init__(self, val):
  174. self.val = val
  175. def to_string(self):
  176. return (&quot;x=&lt;&quot; + str(self.val[&quot;x&quot;]) +
  177. &quot;&gt; y=&lt;&quot; + str(self.val[&quot;y&quot;]) + &quot;&gt;&quot;)
  178. </pre></div>
  179. <p>This example doesn&rsquo;t need a lookup function, that is handled by the
  180. <code>gdb.printing</code> module. Instead a function is provided to build up
  181. the object that handles the lookup.
  182. </p>
  183. <div class="smallexample">
  184. <pre class="smallexample">import gdb.printing
  185. def build_pretty_printer():
  186. pp = gdb.printing.RegexpCollectionPrettyPrinter(
  187. &quot;my_library&quot;)
  188. pp.add_printer('foo', '^foo$', fooPrinter)
  189. pp.add_printer('bar', '^bar$', barPrinter)
  190. return pp
  191. </pre></div>
  192. <p>And here is the autoload support:
  193. </p>
  194. <div class="smallexample">
  195. <pre class="smallexample">import gdb.printing
  196. import my_library
  197. gdb.printing.register_pretty_printer(
  198. gdb.current_objfile(),
  199. my_library.build_pretty_printer())
  200. </pre></div>
  201. <p>Finally, when this printer is loaded into <small>GDB</small>, here is the
  202. corresponding output of &lsquo;<samp>info pretty-printer</samp>&rsquo;:
  203. </p>
  204. <div class="smallexample">
  205. <pre class="smallexample">(gdb) info pretty-printer
  206. my_library.so:
  207. my_library
  208. foo
  209. bar
  210. </pre></div>
  211. <hr>
  212. <div class="header">
  213. <p>
  214. Next: <a href="Type-Printing-API.html#Type-Printing-API" accesskey="n" rel="next">Type Printing API</a>, Previous: <a href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" accesskey="p" rel="previous">Selecting Pretty-Printers</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>
  215. </div>
  216. </body>
  217. </html>