Fix typo/copy'n'pasto.
[official-gcc.git] / gcc / gdbhooks.py
blob85608dca0b0f960fd3e445a4d20eb3be3559bfe3
1 # Python hooks for gdb for debugging GCC
2 # Copyright (C) 2013-2014 Free Software Foundation, Inc.
4 # Contributed by David Malcolm <dmalcolm@redhat.com>
6 # This file is part of GCC.
8 # GCC is free software; you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free
10 # Software Foundation; either version 3, or (at your option) any later
11 # version.
13 # GCC is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 # for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with GCC; see the file COPYING3. If not see
20 # <http://www.gnu.org/licenses/>.
22 """
23 Enabling the debugging hooks
24 ----------------------------
25 gcc/configure (from configure.ac) generates a .gdbinit within the "gcc"
26 subdirectory of the build directory, and when run by gdb, this imports
27 gcc/gdbhooks.py from the source directory, injecting useful Python code
28 into gdb.
30 You may see a message from gdb of the form:
31 "path-to-build/gcc/.gdbinit" auto-loading has been declined by your `auto-load safe-path'
32 as a protection against untrustworthy python scripts. See
33 http://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
35 The fix is to mark the paths of the build/gcc directory as trustworthy.
36 An easy way to do so is by adding the following to your ~/.gdbinit script:
37 add-auto-load-safe-path /absolute/path/to/build/gcc
38 for the build directories for your various checkouts of gcc.
40 If it's working, you should see the message:
41 Successfully loaded GDB hooks for GCC
42 as gdb starts up.
44 During development, I've been manually invoking the code in this way, as a
45 precanned way of printing a variety of different kinds of value:
47 gdb \
48 -ex "break expand_gimple_stmt" \
49 -ex "run" \
50 -ex "bt" \
51 --args \
52 ./cc1 foo.c -O3
54 Examples of output using the pretty-printers
55 --------------------------------------------
56 Pointer values are generally shown in the form:
57 <type address extra_info>
59 For example, an opt_pass* might appear as:
60 (gdb) p pass
61 $2 = <opt_pass* 0x188b600 "expand"(170)>
63 The name of the pass is given ("expand"), together with the
64 static_pass_number.
66 Note that you can dereference the pointer in the normal way:
67 (gdb) p *pass
68 $4 = {type = RTL_PASS, name = 0x120a312 "expand",
69 [etc, ...snipped...]
71 and you can suppress pretty-printers using /r (for "raw"):
72 (gdb) p /r pass
73 $3 = (opt_pass *) 0x188b600
75 Basic blocks are shown with their index in parentheses, apart from the
76 CFG's entry and exit blocks, which are given as "ENTRY" and "EXIT":
77 (gdb) p bb
78 $9 = <basic_block 0x7ffff041f1a0 (2)>
79 (gdb) p cfun->cfg->x_entry_block_ptr
80 $10 = <basic_block 0x7ffff041f0d0 (ENTRY)>
81 (gdb) p cfun->cfg->x_exit_block_ptr
82 $11 = <basic_block 0x7ffff041f138 (EXIT)>
84 CFG edges are shown with the src and dest blocks given in parentheses:
85 (gdb) p e
86 $1 = <edge 0x7ffff043f118 (ENTRY -> 6)>
88 Tree nodes are printed using Python code that emulates print_node_brief,
89 running in gdb, rather than in the inferior:
90 (gdb) p cfun->decl
91 $1 = <function_decl 0x7ffff0420b00 foo>
92 For usability, the type is printed first (e.g. "function_decl"), rather
93 than just "tree".
95 RTL expressions use a kludge: they are pretty-printed by injecting
96 calls into print-rtl.c into the inferior:
97 Value returned is $1 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK)
98 (gdb) p $1
99 $2 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK)
100 (gdb) p /r $1
101 $3 = (rtx_def *) 0x7ffff043e140
102 This won't work for coredumps, and probably in other circumstances, but
103 it's a quick way of getting lots of debuggability quickly.
105 Callgraph nodes are printed with the name of the function decl, if
106 available:
107 (gdb) frame 5
108 #5 0x00000000006c288a in expand_function (node=<cgraph_node* 0x7ffff0312720 "foo">) at ../../src/gcc/cgraphunit.c:1594
109 1594 execute_pass_list (g->get_passes ()->all_passes);
110 (gdb) p node
111 $1 = <cgraph_node* 0x7ffff0312720 "foo">
113 vec<> pointers are printed as the address followed by the elements in
114 braces. Here's a length 2 vec:
115 (gdb) p bb->preds
116 $18 = 0x7ffff0428b68 = {<edge 0x7ffff044d380 (3 -> 5)>, <edge 0x7ffff044d3b8 (4 -> 5)>}
118 and here's a length 1 vec:
119 (gdb) p bb->succs
120 $19 = 0x7ffff0428bb8 = {<edge 0x7ffff044d3f0 (5 -> EXIT)>}
122 You cannot yet use array notation [] to access the elements within the
123 vector: attempting to do so instead gives you the vec itself (for vec[0]),
124 or a (probably) invalid cast to vec<> for the memory after the vec (for
125 vec[1] onwards).
127 Instead (for now) you must access m_vecdata:
128 (gdb) p bb->preds->m_vecdata[0]
129 $20 = <edge 0x7ffff044d380 (3 -> 5)>
130 (gdb) p bb->preds->m_vecdata[1]
131 $21 = <edge 0x7ffff044d3b8 (4 -> 5)>
133 import re
135 import gdb
136 import gdb.printing
137 import gdb.types
139 # Convert "enum tree_code" (tree.def and tree.h) to a dict:
140 tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code'))
142 # ...and look up specific values for use later:
143 IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE']
144 TYPE_DECL = tree_code_dict['TYPE_DECL']
146 # Similarly for "enum tree_code_class" (tree.h):
147 tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class'))
148 tcc_type = tree_code_class_dict['tcc_type']
149 tcc_declaration = tree_code_class_dict['tcc_declaration']
151 class Tree:
153 Wrapper around a gdb.Value for a tree, with various methods
154 corresponding to macros in gcc/tree.h
156 def __init__(self, gdbval):
157 self.gdbval = gdbval
159 def is_nonnull(self):
160 return long(self.gdbval)
162 def TREE_CODE(self):
164 Get gdb.Value corresponding to TREE_CODE (self)
165 as per:
166 #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
168 return self.gdbval['base']['code']
170 def DECL_NAME(self):
172 Get Tree instance corresponding to DECL_NAME (self)
174 return Tree(self.gdbval['decl_minimal']['name'])
176 def TYPE_NAME(self):
178 Get Tree instance corresponding to result of TYPE_NAME (self)
180 return Tree(self.gdbval['type_common']['name'])
182 def IDENTIFIER_POINTER(self):
184 Get str correspoinding to result of IDENTIFIER_NODE (self)
186 return self.gdbval['identifier']['id']['str'].string()
188 class TreePrinter:
189 "Prints a tree"
191 def __init__ (self, gdbval):
192 self.gdbval = gdbval
193 self.node = Tree(gdbval)
195 def to_string (self):
196 # like gcc/print-tree.c:print_node_brief
197 # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
198 # tree_code_name[(int) TREE_CODE (node)])
199 if long(self.gdbval) == 0:
200 return '<tree 0x0>'
202 val_TREE_CODE = self.node.TREE_CODE()
204 # extern const enum tree_code_class tree_code_type[];
205 # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
207 val_tree_code_type = gdb.parse_and_eval('tree_code_type')
208 val_tclass = val_tree_code_type[val_TREE_CODE]
210 val_tree_code_name = gdb.parse_and_eval('tree_code_name')
211 val_code_name = val_tree_code_name[long(val_TREE_CODE)]
212 #print val_code_name.string()
214 result = '<%s 0x%x' % (val_code_name.string(), long(self.gdbval))
215 if long(val_tclass) == tcc_declaration:
216 tree_DECL_NAME = self.node.DECL_NAME()
217 if tree_DECL_NAME.is_nonnull():
218 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
219 else:
220 pass # TODO: labels etc
221 elif long(val_tclass) == tcc_type:
222 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
223 if tree_TYPE_NAME.is_nonnull():
224 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
225 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
226 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
227 if tree_TYPE_NAME.DECL_NAME().is_nonnull():
228 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
229 if self.node.TREE_CODE() == IDENTIFIER_NODE:
230 result += ' %s' % self.node.IDENTIFIER_POINTER()
231 # etc
232 result += '>'
233 return result
235 ######################################################################
236 # Callgraph pretty-printers
237 ######################################################################
239 class CGraphNodePrinter:
240 def __init__(self, gdbval):
241 self.gdbval = gdbval
243 def to_string (self):
244 result = '<cgraph_node* 0x%x' % long(self.gdbval)
245 if long(self.gdbval):
246 # symtab_node::name calls lang_hooks.decl_printable_name
247 # default implementation (lhd_decl_printable_name) is:
248 # return IDENTIFIER_POINTER (DECL_NAME (decl));
249 tree_decl = Tree(self.gdbval['decl'])
250 result += ' "%s"' % tree_decl.DECL_NAME().IDENTIFIER_POINTER()
251 result += '>'
252 return result
254 ######################################################################
256 class GimplePrinter:
257 def __init__(self, gdbval):
258 self.gdbval = gdbval
260 def to_string (self):
261 if long(self.gdbval) == 0:
262 return '<gimple 0x0>'
263 val_gimple_code = self.gdbval['code']
264 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
265 val_code_name = val_gimple_code_name[long(val_gimple_code)]
266 result = '<%s 0x%x' % (val_code_name.string(),
267 long(self.gdbval))
268 result += '>'
269 return result
271 ######################################################################
272 # CFG pretty-printers
273 ######################################################################
275 def bb_index_to_str(index):
276 if index == 0:
277 return 'ENTRY'
278 elif index == 1:
279 return 'EXIT'
280 else:
281 return '%i' % index
283 class BasicBlockPrinter:
284 def __init__(self, gdbval):
285 self.gdbval = gdbval
287 def to_string (self):
288 result = '<basic_block 0x%x' % long(self.gdbval)
289 if long(self.gdbval):
290 result += ' (%s)' % bb_index_to_str(long(self.gdbval['index']))
291 result += '>'
292 return result
294 class CfgEdgePrinter:
295 def __init__(self, gdbval):
296 self.gdbval = gdbval
298 def to_string (self):
299 result = '<edge 0x%x' % long(self.gdbval)
300 if long(self.gdbval):
301 src = bb_index_to_str(long(self.gdbval['src']['index']))
302 dest = bb_index_to_str(long(self.gdbval['dest']['index']))
303 result += ' (%s -> %s)' % (src, dest)
304 result += '>'
305 return result
307 ######################################################################
309 class Rtx:
310 def __init__(self, gdbval):
311 self.gdbval = gdbval
313 def GET_CODE(self):
314 return self.gdbval['code']
316 def GET_RTX_LENGTH(code):
317 val_rtx_length = gdb.parse_and_eval('rtx_length')
318 return long(val_rtx_length[code])
320 def GET_RTX_NAME(code):
321 val_rtx_name = gdb.parse_and_eval('rtx_name')
322 return val_rtx_name[code].string()
324 def GET_RTX_FORMAT(code):
325 val_rtx_format = gdb.parse_and_eval('rtx_format')
326 return val_rtx_format[code].string()
328 class RtxPrinter:
329 def __init__(self, gdbval):
330 self.gdbval = gdbval
331 self.rtx = Rtx(gdbval)
333 def to_string (self):
335 For now, a cheap kludge: invoke the inferior's print
336 function to get a string to use the user, and return an empty
337 string for gdb
339 # We use print_inline_rtx to avoid a trailing newline
340 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
341 % long(self.gdbval))
342 return ''
344 # or by hand; based on gcc/print-rtl.c:print_rtx
345 result = ('<rtx_def 0x%x'
346 % (long(self.gdbval)))
347 code = self.rtx.GET_CODE()
348 result += ' (%s' % GET_RTX_NAME(code)
349 format_ = GET_RTX_FORMAT(code)
350 for i in range(GET_RTX_LENGTH(code)):
351 print format_[i]
352 result += ')>'
353 return result
355 ######################################################################
357 class PassPrinter:
358 def __init__(self, gdbval):
359 self.gdbval = gdbval
361 def to_string (self):
362 result = '<opt_pass* 0x%x' % long(self.gdbval)
363 if long(self.gdbval):
364 result += (' "%s"(%i)'
365 % (self.gdbval['name'].string(),
366 long(self.gdbval['static_pass_number'])))
367 result += '>'
368 return result
370 ######################################################################
372 class VecPrinter:
373 # -ex "up" -ex "p bb->preds"
374 def __init__(self, gdbval):
375 self.gdbval = gdbval
377 def display_hint (self):
378 return 'array'
380 def to_string (self):
381 # A trivial implementation; prettyprinting the contents is done
382 # by gdb calling the "children" method below.
383 return '0x%x' % long(self.gdbval)
385 def children (self):
386 if long(self.gdbval) == 0:
387 return
388 m_vecpfx = self.gdbval['m_vecpfx']
389 m_num = m_vecpfx['m_num']
390 m_vecdata = self.gdbval['m_vecdata']
391 for i in range(m_num):
392 yield ('[%d]' % i, m_vecdata[i])
394 ######################################################################
396 # TODO:
397 # * hashtab
398 # * location_t
400 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
401 def __init__(self, name, class_):
402 super(GdbSubprinter, self).__init__(name)
403 self.class_ = class_
405 def handles_type(self, str_type):
406 raise NotImplementedError
408 class GdbSubprinterTypeList(GdbSubprinter):
410 A GdbSubprinter that handles a specific set of types
412 def __init__(self, str_types, name, class_):
413 super(GdbSubprinterTypeList, self).__init__(name, class_)
414 self.str_types = frozenset(str_types)
416 def handles_type(self, str_type):
417 return str_type in self.str_types
419 class GdbSubprinterRegex(GdbSubprinter):
421 A GdbSubprinter that handles types that match a regex
423 def __init__(self, regex, name, class_):
424 super(GdbSubprinterRegex, self).__init__(name, class_)
425 self.regex = re.compile(regex)
427 def handles_type(self, str_type):
428 return self.regex.match(str_type)
430 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
431 def __init__(self, name):
432 super(GdbPrettyPrinters, self).__init__(name, [])
434 def add_printer_for_types(self, name, class_, types):
435 self.subprinters.append(GdbSubprinterTypeList(name, class_, types))
437 def add_printer_for_regex(self, name, class_, regex):
438 self.subprinters.append(GdbSubprinterRegex(name, class_, regex))
440 def __call__(self, gdbval):
441 type_ = gdbval.type.unqualified()
442 str_type = str(type_)
443 for printer in self.subprinters:
444 if printer.enabled and printer.handles_type(str_type):
445 return printer.class_(gdbval)
447 # Couldn't find a pretty printer (or it was disabled):
448 return None
451 def build_pretty_printer():
452 pp = GdbPrettyPrinters('gcc')
453 pp.add_printer_for_types(['tree'],
454 'tree', TreePrinter)
455 pp.add_printer_for_types(['cgraph_node *'],
456 'cgraph_node', CGraphNodePrinter)
457 pp.add_printer_for_types(['gimple', 'gimple_statement_base *'],
458 'gimple',
459 GimplePrinter)
460 pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
461 'basic_block',
462 BasicBlockPrinter)
463 pp.add_printer_for_types(['edge', 'edge_def *'],
464 'edge',
465 CfgEdgePrinter)
466 pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
467 pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
469 pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*',
470 'vec',
471 VecPrinter)
473 return pp
475 gdb.printing.register_pretty_printer(
476 gdb.current_objfile(),
477 build_pretty_printer())
479 print('Successfully loaded GDB hooks for GCC')