Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

245 lines
10KB

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