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.

Writing-a-Guile-Pretty_002dPrinter.html 10.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Guile Pretty-Printer (Debugging with GDB)</title>
  17. <meta name="description" content="Writing a Guile Pretty-Printer (Debugging with GDB)">
  18. <meta name="keywords" content="Writing a Guile 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="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="prev" title="Selecting Guile 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-Guile-Pretty_002dPrinter"></a>
  58. <div class="header">
  59. <p>
  60. 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="prev">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>
  61. </div>
  62. <hr>
  63. <a name="Writing-a-Guile-Pretty_002dPrinter-1"></a>
  64. <h4 class="subsubsection">23.3.3.10 Writing a Guile Pretty-Printer</h4>
  65. <a name="index-writing-a-Guile-pretty_002dprinter"></a>
  66. <p>A pretty-printer consists of two basic parts: a lookup function to determine
  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="Guile-Pretty-Printing-API.html#Guile-Pretty-Printing-API">Guile Pretty Printing API</a>, for details.
  71. </p>
  72. <div class="smallexample">
  73. <pre class="smallexample">(define (make-my-string-printer value)
  74. &quot;Print a my::string string&quot;
  75. (make-pretty-printer-worker
  76. &quot;string&quot;
  77. (lambda (printer)
  78. (value-field value &quot;_data&quot;))
  79. #f))
  80. </pre></div>
  81. <p>And here is an example showing how a lookup function for the printer
  82. example above might be written.
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample">(define (str-lookup-function pretty-printer value)
  86. (let ((tag (type-tag (value-type value))))
  87. (and tag
  88. (string-prefix? &quot;std::string&lt;&quot; tag)
  89. (make-my-string-printer value))))
  90. </pre></div>
  91. <p>Then to register this printer in the global printer list:
  92. </p>
  93. <div class="smallexample">
  94. <pre class="smallexample">(append-pretty-printer!
  95. (make-pretty-printer &quot;my-string&quot; str-lookup-function))
  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 &lt;gdb:pretty-printer-worker&gt; object.
  100. If not, it returns <code>#f</code>.
  101. </p>
  102. <p>We recommend that you put your core pretty-printers into a Guile
  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="Guile-Auto_002dloading.html#Guile-Auto_002dloading">Guile 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 Guile 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>my::string</code> example,
  126. this code might appear in <code>(my-project my-library v1)</code>:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">(use-modules (gdb))
  130. (define (register-printers objfile)
  131. (append-objfile-pretty-printer!
  132. (make-pretty-printer &quot;my-string&quot; str-lookup-function)))
  133. </pre></div>
  134. <p>And then the corresponding contents of the auto-load file would be:
  135. </p>
  136. <div class="smallexample">
  137. <pre class="smallexample">(use-modules (gdb) (my-project my-library v1))
  138. (register-printers (current-objfile))
  139. </pre></div>
  140. <p>The previous example illustrates a basic pretty-printer.
  141. There are a few things that can be improved on.
  142. The printer only handles one type, whereas a library typically has
  143. several types. One could install a lookup function for each desired type
  144. in the library, but one could also have a single lookup function recognize
  145. several types. The latter is the conventional way this is handled.
  146. If a pretty-printer can handle multiple data types, then its
  147. <em>subprinters</em> are the printers for the individual data types.
  148. </p>
  149. <p>The <code>(gdb printing)</code> module provides a formal way of solving this
  150. problem (see <a href="Guile-Printing-Module.html#Guile-Printing-Module">Guile Printing Module</a>).
  151. Here is another example that handles multiple types.
  152. </p>
  153. <p>These are the types we are going to pretty-print:
  154. </p>
  155. <div class="smallexample">
  156. <pre class="smallexample">struct foo { int a, b; };
  157. struct bar { struct foo x, y; };
  158. </pre></div>
  159. <p>Here are the printers:
  160. </p>
  161. <div class="smallexample">
  162. <pre class="smallexample">(define (make-foo-printer value)
  163. &quot;Print a foo object&quot;
  164. (make-pretty-printer-worker
  165. &quot;foo&quot;
  166. (lambda (printer)
  167. (format #f &quot;a=&lt;~a&gt; b=&lt;~a&gt;&quot;
  168. (value-field value &quot;a&quot;) (value-field value &quot;a&quot;)))
  169. #f))
  170. (define (make-bar-printer value)
  171. &quot;Print a bar object&quot;
  172. (make-pretty-printer-worker
  173. &quot;foo&quot;
  174. (lambda (printer)
  175. (format #f &quot;x=&lt;~a&gt; y=&lt;~a&gt;&quot;
  176. (value-field value &quot;x&quot;) (value-field value &quot;y&quot;)))
  177. #f))
  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">(use-modules (gdb printing))
  185. (define (build-pretty-printer)
  186. (let ((pp (make-pretty-printer-collection &quot;my-library&quot;)))
  187. (pp-collection-add-tag-printer &quot;foo&quot; make-foo-printer)
  188. (pp-collection-add-tag-printer &quot;bar&quot; make-bar-printer)
  189. pp))
  190. </pre></div>
  191. <p>And here is the autoload support:
  192. </p>
  193. <div class="smallexample">
  194. <pre class="smallexample">(use-modules (gdb) (my-library))
  195. (append-objfile-pretty-printer! (current-objfile) (build-pretty-printer))
  196. </pre></div>
  197. <p>Finally, when this printer is loaded into <small>GDB</small>, here is the
  198. corresponding output of &lsquo;<samp>info pretty-printer</samp>&rsquo;:
  199. </p>
  200. <div class="smallexample">
  201. <pre class="smallexample">(gdb) info pretty-printer
  202. my_library.so:
  203. my-library
  204. foo
  205. bar
  206. </pre></div>
  207. <hr>
  208. <div class="header">
  209. <p>
  210. 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="prev">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>
  211. </div>
  212. </body>
  213. </html>