• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2013.11/share/doc/arm-arm-none-eabi/html/gdb/
1<html lang="en">
2<head>
3<title>Writing a Pretty-Printer - Debugging with GDB</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="Debugging with GDB">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="Python-API.html#Python-API" title="Python API">
9<link rel="prev" href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" title="Selecting Pretty-Printers">
10<link rel="next" href="Type-Printing-API.html#Type-Printing-API" title="Type Printing API">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1988-2013 Free Software Foundation, Inc.
14
15Permission is granted to copy, distribute and/or modify this document
16under the terms of the GNU Free Documentation License, Version 1.3 or
17any later version published by the Free Software Foundation; with the
18Invariant Sections being ``Free Software'' and ``Free Software Needs
19Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,''
20and with the Back-Cover Texts as in (a) below.
21
22(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
23this GNU Manual.  Buying copies from GNU Press supports the FSF in
24developing GNU and promoting software freedom.''
25-->
26<meta http-equiv="Content-Style-Type" content="text/css">
27<style type="text/css"><!--
28  pre.display { font-family:inherit }
29  pre.format  { font-family:inherit }
30  pre.smalldisplay { font-family:inherit; font-size:smaller }
31  pre.smallformat  { font-family:inherit; font-size:smaller }
32  pre.smallexample { font-size:smaller }
33  pre.smalllisp    { font-size:smaller }
34  span.sc    { font-variant:small-caps }
35  span.roman { font-family:serif; font-weight:normal; } 
36  span.sansserif { font-family:sans-serif; font-weight:normal; } 
37--></style>
38<link rel="stylesheet" type="text/css" href="../cs.css">
39</head>
40<body>
41<div class="node">
42<a name="Writing-a-Pretty-Printer"></a>
43<a name="Writing-a-Pretty_002dPrinter"></a>
44<p>
45Next:&nbsp;<a rel="next" accesskey="n" href="Type-Printing-API.html#Type-Printing-API">Type Printing API</a>,
46Previous:&nbsp;<a rel="previous" accesskey="p" href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters">Selecting Pretty-Printers</a>,
47Up:&nbsp;<a rel="up" accesskey="u" href="Python-API.html#Python-API">Python API</a>
48<hr>
49</div>
50
51<h5 class="subsubsection">23.2.2.7 Writing a Pretty-Printer</h5>
52
53<p><a name="index-writing-a-pretty_002dprinter-1876"></a>
54A pretty-printer consists of two parts: a lookup function to detect
55if the type is supported, and the printer itself.
56
57   <p>Here is an example showing how a <code>std::string</code> printer might be
58written.  See <a href="Pretty-Printing-API.html#Pretty-Printing-API">Pretty Printing API</a>, for details on the API this class
59must provide.
60
61<pre class="smallexample">     class StdStringPrinter(object):
62         "Print a std::string"
63     
64         def __init__(self, val):
65             self.val = val
66     
67         def to_string(self):
68             return self.val['_M_dataplus']['_M_p']
69     
70         def display_hint(self):
71             return 'string'
72</pre>
73   <p>And here is an example showing how a lookup function for the printer
74example above might be written.
75
76<pre class="smallexample">     def str_lookup_function(val):
77         lookup_tag = val.type.tag
78         if lookup_tag == None:
79             return None
80         regex = re.compile("^std::basic_string&lt;char,.*&gt;$")
81         if regex.match(lookup_tag):
82             return StdStringPrinter(val)
83         return None
84</pre>
85   <p>The example lookup function extracts the value's type, and attempts to
86match it to a type that it can pretty-print.  If it is a type the
87printer can pretty-print, it will return a printer object.  If not, it
88returns <code>None</code>.
89
90   <p>We recommend that you put your core pretty-printers into a Python
91package.  If your pretty-printers are for use with a library, we
92further recommend embedding a version number into the package name. 
93This practice will enable <span class="sc">gdb</span> to load multiple versions of
94your pretty-printers at the same time, because they will have
95different names.
96
97   <p>You should write auto-loaded code (see <a href="Python-Auto_002dloading.html#Python-Auto_002dloading">Python Auto-loading</a>) such that it
98can be evaluated multiple times without changing its meaning.  An
99ideal auto-load file will consist solely of <code>import</code>s of your
100printer modules, followed by a call to a register pretty-printers with
101the current objfile.
102
103   <p>Taken as a whole, this approach will scale nicely to multiple
104inferiors, each potentially using a different library version. 
105Embedding a version number in the Python package name will ensure that
106<span class="sc">gdb</span> is able to load both sets of printers simultaneously. 
107Then, because the search for pretty-printers is done by objfile, and
108because your auto-loaded code took care to register your library's
109printers with a specific objfile, <span class="sc">gdb</span> will find the correct
110printers for the specific version of the library used by each
111inferior.
112
113   <p>To continue the <code>std::string</code> example (see <a href="Pretty-Printing-API.html#Pretty-Printing-API">Pretty Printing API</a>),
114this code might appear in <code>gdb.libstdcxx.v6</code>:
115
116<pre class="smallexample">     def register_printers(objfile):
117         objfile.pretty_printers.append(str_lookup_function)
118</pre>
119   <p class="noindent">And then the corresponding contents of the auto-load file would be:
120
121<pre class="smallexample">     import gdb.libstdcxx.v6
122     gdb.libstdcxx.v6.register_printers(gdb.current_objfile())
123</pre>
124   <p>The previous example illustrates a basic pretty-printer. 
125There are a few things that can be improved on. 
126The printer doesn't have a name, making it hard to identify in a
127list of installed printers.  The lookup function has a name, but
128lookup functions can have arbitrary, even identical, names.
129
130   <p>Second, the printer only handles one type, whereas a library typically has
131several types.  One could install a lookup function for each desired type
132in the library, but one could also have a single lookup function recognize
133several types.  The latter is the conventional way this is handled. 
134If a pretty-printer can handle multiple data types, then its
135<dfn>subprinters</dfn> are the printers for the individual data types.
136
137   <p>The <code>gdb.printing</code> module provides a formal way of solving these
138problems (see <a href="gdb_002eprinting.html#gdb_002eprinting">gdb.printing</a>). 
139Here is another example that handles multiple types.
140
141   <p>These are the types we are going to pretty-print:
142
143<pre class="smallexample">     struct foo { int a, b; };
144     struct bar { struct foo x, y; };
145</pre>
146   <p>Here are the printers:
147
148<pre class="smallexample">     class fooPrinter:
149         """Print a foo object."""
150     
151         def __init__(self, val):
152             self.val = val
153     
154         def to_string(self):
155             return ("a=&lt;" + str(self.val["a"]) +
156                     "&gt; b=&lt;" + str(self.val["b"]) + "&gt;")
157     
158     class barPrinter:
159         """Print a bar object."""
160     
161         def __init__(self, val):
162             self.val = val
163     
164         def to_string(self):
165             return ("x=&lt;" + str(self.val["x"]) +
166                     "&gt; y=&lt;" + str(self.val["y"]) + "&gt;")
167</pre>
168   <p>This example doesn't need a lookup function, that is handled by the
169<code>gdb.printing</code> module.  Instead a function is provided to build up
170the object that handles the lookup.
171
172<pre class="smallexample">     import gdb.printing
173     
174     def build_pretty_printer():
175         pp = gdb.printing.RegexpCollectionPrettyPrinter(
176             "my_library")
177         pp.add_printer('foo', '^foo$', fooPrinter)
178         pp.add_printer('bar', '^bar$', barPrinter)
179         return pp
180</pre>
181   <p>And here is the autoload support:
182
183<pre class="smallexample">     import gdb.printing
184     import my_library
185     gdb.printing.register_pretty_printer(
186         gdb.current_objfile(),
187         my_library.build_pretty_printer())
188</pre>
189   <p>Finally, when this printer is loaded into <span class="sc">gdb</span>, here is the
190corresponding output of &lsquo;<samp><span class="samp">info pretty-printer</span></samp>&rsquo;:
191
192<pre class="smallexample">     (gdb) info pretty-printer
193     my_library.so:
194       my_library
195         foo
196         bar
197</pre>
198   </body></html>
199
200