Writing-a-Guile-Pretty_002dPrinter.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Guile Pretty-Printer</title>
  16. <meta name="description" content="Debugging with GDB: Writing a Guile Pretty-Printer">
  17. <meta name="keywords" content="Debugging with GDB: Writing a Guile 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="Guile-API.html#Guile-API" rel="up" title="Guile API">
  26. <link href="Commands-In-Guile.html#Commands-In-Guile" rel="next" title="Commands In Guile">
  27. <link href="Selecting-Guile-Pretty_002dPrinters.html#Selecting-Guile-Pretty_002dPrinters" rel="previous" title="Selecting Guile 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-Guile-Pretty_002dPrinter"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Commands-In-Guile.html#Commands-In-Guile" accesskey="n" rel="next">Commands In Guile</a>, Previous: <a href="Selecting-Guile-Pretty_002dPrinters.html#Selecting-Guile-Pretty_002dPrinters" accesskey="p" rel="previous">Selecting Guile Pretty-Printers</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile 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-Guile-Pretty_002dPrinter-1"></a>
  65. <h4 class="subsubsection">23.3.3.10 Writing a Guile Pretty-Printer</h4>
  66. <a name="index-writing-a-Guile-pretty_002dprinter"></a>
  67. <p>A pretty-printer consists of two basic parts: a lookup function to determine
  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="Guile-Pretty-Printing-API.html#Guile-Pretty-Printing-API">Guile Pretty Printing API</a>, for details.
  72. </p>
  73. <div class="smallexample">
  74. <pre class="smallexample">(define (make-my-string-printer value)
  75. &quot;Print a my::string string&quot;
  76. (make-pretty-printer-worker
  77. &quot;string&quot;
  78. (lambda (printer)
  79. (value-field value &quot;_data&quot;))
  80. #f))
  81. </pre></div>
  82. <p>And here is an example showing how a lookup function for the printer
  83. example above might be written.
  84. </p>
  85. <div class="smallexample">
  86. <pre class="smallexample">(define (str-lookup-function pretty-printer value)
  87. (let ((tag (type-tag (value-type value))))
  88. (and tag
  89. (string-prefix? &quot;std::string&lt;&quot; tag)
  90. (make-my-string-printer value))))
  91. </pre></div>
  92. <p>Then to register this printer in the global printer list:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">(append-pretty-printer!
  96. (make-pretty-printer &quot;my-string&quot; str-lookup-function))
  97. </pre></div>
  98. <p>The example lookup function extracts the value&rsquo;s type, and attempts to
  99. match it to a type that it can pretty-print. If it is a type the
  100. printer can pretty-print, it will return a &lt;gdb:pretty-printer-worker&gt; object.
  101. If not, it returns <code>#f</code>.
  102. </p>
  103. <p>We recommend that you put your core pretty-printers into a Guile
  104. package. If your pretty-printers are for use with a library, we
  105. further recommend embedding a version number into the package name.
  106. This practice will enable <small>GDB</small> to load multiple versions of
  107. your pretty-printers at the same time, because they will have
  108. different names.
  109. </p>
  110. <p>You should write auto-loaded code (see <a href="Guile-Auto_002dloading.html#Guile-Auto_002dloading">Guile Auto-loading</a>) such that it
  111. can be evaluated multiple times without changing its meaning. An
  112. ideal auto-load file will consist solely of <code>import</code>s of your
  113. printer modules, followed by a call to a register pretty-printers with
  114. the current objfile.
  115. </p>
  116. <p>Taken as a whole, this approach will scale nicely to multiple
  117. inferiors, each potentially using a different library version.
  118. Embedding a version number in the Guile package name will ensure that
  119. <small>GDB</small> is able to load both sets of printers simultaneously.
  120. Then, because the search for pretty-printers is done by objfile, and
  121. because your auto-loaded code took care to register your library&rsquo;s
  122. printers with a specific objfile, <small>GDB</small> will find the correct
  123. printers for the specific version of the library used by each
  124. inferior.
  125. </p>
  126. <p>To continue the <code>my::string</code> example,
  127. this code might appear in <code>(my-project my-library v1)</code>:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">(use-modules (gdb))
  131. (define (register-printers objfile)
  132. (append-objfile-pretty-printer!
  133. (make-pretty-printer &quot;my-string&quot; str-lookup-function)))
  134. </pre></div>
  135. <p>And then the corresponding contents of the auto-load file would be:
  136. </p>
  137. <div class="smallexample">
  138. <pre class="smallexample">(use-modules (gdb) (my-project my-library v1))
  139. (register-printers (current-objfile))
  140. </pre></div>
  141. <p>The previous example illustrates a basic pretty-printer.
  142. There are a few things that can be improved on.
  143. 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 this
  151. problem (see <a href="Guile-Printing-Module.html#Guile-Printing-Module">Guile Printing Module</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">(define (make-foo-printer value)
  164. &quot;Print a foo object&quot;
  165. (make-pretty-printer-worker
  166. &quot;foo&quot;
  167. (lambda (printer)
  168. (format #f &quot;a=&lt;~a&gt; b=&lt;~a&gt;&quot;
  169. (value-field value &quot;a&quot;) (value-field value &quot;a&quot;)))
  170. #f))
  171. (define (make-bar-printer value)
  172. &quot;Print a bar object&quot;
  173. (make-pretty-printer-worker
  174. &quot;foo&quot;
  175. (lambda (printer)
  176. (format #f &quot;x=&lt;~a&gt; y=&lt;~a&gt;&quot;
  177. (value-field value &quot;x&quot;) (value-field value &quot;y&quot;)))
  178. #f))
  179. </pre></div>
  180. <p>This example doesn&rsquo;t need a lookup function, that is handled by the
  181. <code>(gdb printing)</code> module. Instead a function is provided to build up
  182. the object that handles the lookup.
  183. </p>
  184. <div class="smallexample">
  185. <pre class="smallexample">(use-modules (gdb printing))
  186. (define (build-pretty-printer)
  187. (let ((pp (make-pretty-printer-collection &quot;my-library&quot;)))
  188. (pp-collection-add-tag-printer &quot;foo&quot; make-foo-printer)
  189. (pp-collection-add-tag-printer &quot;bar&quot; make-bar-printer)
  190. pp))
  191. </pre></div>
  192. <p>And here is the autoload support:
  193. </p>
  194. <div class="smallexample">
  195. <pre class="smallexample">(use-modules (gdb) (my-library))
  196. (append-objfile-pretty-printer! (current-objfile) (build-pretty-printer))
  197. </pre></div>
  198. <p>Finally, when this printer is loaded into <small>GDB</small>, here is the
  199. corresponding output of &lsquo;<samp>info pretty-printer</samp>&rsquo;:
  200. </p>
  201. <div class="smallexample">
  202. <pre class="smallexample">(gdb) info pretty-printer
  203. my_library.so:
  204. my-library
  205. foo
  206. bar
  207. </pre></div>
  208. <hr>
  209. <div class="header">
  210. <p>
  211. Next: <a href="Commands-In-Guile.html#Commands-In-Guile" accesskey="n" rel="next">Commands In Guile</a>, Previous: <a href="Selecting-Guile-Pretty_002dPrinters.html#Selecting-Guile-Pretty_002dPrinters" accesskey="p" rel="previous">Selecting Guile Pretty-Printers</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile 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>
  212. </div>
  213. </body>
  214. </html>