vectorizer cost model enhancement
[official-gcc.git] / gcc / gdbhooks.py
blob3d69b11bfe95dac85d88b7f04372058eef44b2ae
1 # Python hooks for gdb for debugging GCC
2 # Copyright (C) 2013 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 import re
115 import gdb
116 import gdb.printing
117 import gdb.types
119 # Convert "enum tree_code" (tree.def and tree.h) to a dict:
120 tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code'))
122 # ...and look up specific values for use later:
123 IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE']
124 TYPE_DECL = tree_code_dict['TYPE_DECL']
126 # Similarly for "enum tree_code_class" (tree.h):
127 tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class'))
128 tcc_type = tree_code_class_dict['tcc_type']
129 tcc_declaration = tree_code_class_dict['tcc_declaration']
131 class Tree:
133 Wrapper around a gdb.Value for a tree, with various methods
134 corresponding to macros in gcc/tree.h
136 def __init__(self, gdbval):
137 self.gdbval = gdbval
139 def is_nonnull(self):
140 return long(self.gdbval)
142 def TREE_CODE(self):
144 Get gdb.Value corresponding to TREE_CODE (self)
145 as per:
146 #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
148 return self.gdbval['base']['code']
150 def DECL_NAME(self):
152 Get Tree instance corresponding to DECL_NAME (self)
154 return Tree(self.gdbval['decl_minimal']['name'])
156 def TYPE_NAME(self):
158 Get Tree instance corresponding to result of TYPE_NAME (self)
160 return Tree(self.gdbval['type_common']['name'])
162 def IDENTIFIER_POINTER(self):
164 Get str correspoinding to result of IDENTIFIER_NODE (self)
166 return self.gdbval['identifier']['id']['str'].string()
168 class TreePrinter:
169 "Prints a tree"
171 def __init__ (self, gdbval):
172 self.gdbval = gdbval
173 self.node = Tree(gdbval)
175 def to_string (self):
176 # like gcc/print-tree.c:print_node_brief
177 # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
178 # tree_code_name[(int) TREE_CODE (node)])
179 if long(self.gdbval) == 0:
180 return '<tree 0x0>'
182 val_TREE_CODE = self.node.TREE_CODE()
184 # extern const enum tree_code_class tree_code_type[];
185 # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
187 val_tree_code_type = gdb.parse_and_eval('tree_code_type')
188 val_tclass = val_tree_code_type[val_TREE_CODE]
190 val_tree_code_name = gdb.parse_and_eval('tree_code_name')
191 val_code_name = val_tree_code_name[long(val_TREE_CODE)]
192 #print val_code_name.string()
194 result = '<%s 0x%x' % (val_code_name.string(), long(self.gdbval))
195 if long(val_tclass) == tcc_declaration:
196 tree_DECL_NAME = self.node.DECL_NAME()
197 if tree_DECL_NAME.is_nonnull():
198 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
199 else:
200 pass # TODO: labels etc
201 elif long(val_tclass) == tcc_type:
202 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
203 if tree_TYPE_NAME.is_nonnull():
204 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
205 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
206 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
207 if tree_TYPE_NAME.DECL_NAME().is_nonnull():
208 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
209 if self.node.TREE_CODE() == IDENTIFIER_NODE:
210 result += ' %s' % self.node.IDENTIFIER_POINTER()
211 # etc
212 result += '>'
213 return result
215 ######################################################################
216 # Callgraph pretty-printers
217 ######################################################################
219 class CGraphNodePrinter:
220 def __init__(self, gdbval):
221 self.gdbval = gdbval
223 def to_string (self):
224 result = '<cgraph_node* 0x%x' % long(self.gdbval)
225 if long(self.gdbval):
226 # symtab_node_name calls lang_hooks.decl_printable_name
227 # default implementation (lhd_decl_printable_name) is:
228 # return IDENTIFIER_POINTER (DECL_NAME (decl));
229 symbol = self.gdbval['symbol']
230 tree_decl = Tree(symbol['decl'])
231 result += ' "%s"' % tree_decl.DECL_NAME().IDENTIFIER_POINTER()
232 result += '>'
233 return result
235 ######################################################################
237 class GimplePrinter:
238 def __init__(self, gdbval):
239 self.gdbval = gdbval
241 def to_string (self):
242 if long(self.gdbval) == 0:
243 return '<gimple 0x0>'
244 val_gimple_code = self.gdbval['gsbase']['code']
245 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
246 val_code_name = val_gimple_code_name[long(val_gimple_code)]
247 result = '<%s 0x%x' % (val_code_name.string(),
248 long(self.gdbval))
249 result += '>'
250 return result
252 ######################################################################
253 # CFG pretty-printers
254 ######################################################################
256 def bb_index_to_str(index):
257 if index == 0:
258 return 'ENTRY'
259 elif index == 1:
260 return 'EXIT'
261 else:
262 return '%i' % index
264 class BasicBlockPrinter:
265 def __init__(self, gdbval):
266 self.gdbval = gdbval
268 def to_string (self):
269 result = '<basic_block 0x%x' % long(self.gdbval)
270 if long(self.gdbval):
271 result += ' (%s)' % bb_index_to_str(long(self.gdbval['index']))
272 result += '>'
273 return result
275 class CfgEdgePrinter:
276 def __init__(self, gdbval):
277 self.gdbval = gdbval
279 def to_string (self):
280 result = '<edge 0x%x' % long(self.gdbval)
281 if long(self.gdbval):
282 src = bb_index_to_str(long(self.gdbval['src']['index']))
283 dest = bb_index_to_str(long(self.gdbval['dest']['index']))
284 result += ' (%s -> %s)' % (src, dest)
285 result += '>'
286 return result
288 ######################################################################
290 class Rtx:
291 def __init__(self, gdbval):
292 self.gdbval = gdbval
294 def GET_CODE(self):
295 return self.gdbval['code']
297 def GET_RTX_LENGTH(code):
298 val_rtx_length = gdb.parse_and_eval('rtx_length')
299 return long(val_rtx_length[code])
301 def GET_RTX_NAME(code):
302 val_rtx_name = gdb.parse_and_eval('rtx_name')
303 return val_rtx_name[code].string()
305 def GET_RTX_FORMAT(code):
306 val_rtx_format = gdb.parse_and_eval('rtx_format')
307 return val_rtx_format[code].string()
309 class RtxPrinter:
310 def __init__(self, gdbval):
311 self.gdbval = gdbval
312 self.rtx = Rtx(gdbval)
314 def to_string (self):
316 For now, a cheap kludge: invoke the inferior's print
317 function to get a string to use the user, and return an empty
318 string for gdb
320 # We use print_inline_rtx to avoid a trailing newline
321 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
322 % long(self.gdbval))
323 return ''
325 # or by hand; based on gcc/print-rtl.c:print_rtx
326 result = ('<rtx_def 0x%x'
327 % (long(self.gdbval)))
328 code = self.rtx.GET_CODE()
329 result += ' (%s' % GET_RTX_NAME(code)
330 format_ = GET_RTX_FORMAT(code)
331 for i in range(GET_RTX_LENGTH(code)):
332 print format_[i]
333 result += ')>'
334 return result
336 ######################################################################
338 class PassPrinter:
339 def __init__(self, gdbval):
340 self.gdbval = gdbval
342 def to_string (self):
343 result = '<opt_pass* 0x%x' % long(self.gdbval)
344 if long(self.gdbval):
345 result += (' "%s"(%i)'
346 % (self.gdbval['name'].string(),
347 long(self.gdbval['static_pass_number'])))
348 result += '>'
349 return result
351 ######################################################################
353 # TODO:
354 # * vec
355 # * hashtab
356 # * location_t
358 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
359 def __init__(self, name, str_type_, class_):
360 super(GdbSubprinter, self).__init__(name)
361 self.str_type_ = str_type_
362 self.class_ = class_
364 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
365 def __init__(self, name):
366 super(GdbPrettyPrinters, self).__init__(name, [])
368 def add_printer(self, name, exp, class_):
369 self.subprinters.append(GdbSubprinter(name, exp, class_))
371 def __call__(self, gdbval):
372 type_ = gdbval.type.unqualified()
373 str_type_ = str(type_)
374 for printer in self.subprinters:
375 if printer.enabled and str_type_ == printer.str_type_:
376 return printer.class_(gdbval)
378 # Couldn't find a pretty printer (or it was disabled):
379 return None
382 def build_pretty_printer():
383 pp = GdbPrettyPrinters('gcc')
384 pp.add_printer('tree', 'tree', TreePrinter)
385 pp.add_printer('cgraph_node', 'cgraph_node *', CGraphNodePrinter)
386 pp.add_printer('gimple', 'gimple', GimplePrinter)
387 pp.add_printer('basic_block', 'basic_block', BasicBlockPrinter)
388 pp.add_printer('edge', 'edge', CfgEdgePrinter)
389 pp.add_printer('rtx_def', 'rtx_def *', RtxPrinter)
390 pp.add_printer('opt_pass', 'opt_pass *', PassPrinter)
391 return pp
393 gdb.printing.register_pretty_printer(
394 gdb.current_objfile(),
395 build_pretty_printer())
397 print('Successfully loaded GDB hooks for GCC')