111
|
1 # Python hooks for gdb for debugging GCC
|
|
2 # Copyright (C) 2013-2017 Free Software Foundation, Inc.
|
|
3
|
|
4 # Contributed by David Malcolm <dmalcolm@redhat.com>
|
|
5
|
|
6 # This file is part of GCC.
|
|
7
|
|
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.
|
|
12
|
|
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.
|
|
17
|
|
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/>.
|
|
21
|
|
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.
|
|
29
|
|
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
|
|
34
|
|
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.
|
|
39
|
|
40 If it's working, you should see the message:
|
|
41 Successfully loaded GDB hooks for GCC
|
|
42 as gdb starts up.
|
|
43
|
|
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:
|
|
46
|
|
47 gdb \
|
|
48 -ex "break expand_gimple_stmt" \
|
|
49 -ex "run" \
|
|
50 -ex "bt" \
|
|
51 --args \
|
|
52 ./cc1 foo.c -O3
|
|
53
|
|
54 Examples of output using the pretty-printers
|
|
55 --------------------------------------------
|
|
56 Pointer values are generally shown in the form:
|
|
57 <type address extra_info>
|
|
58
|
|
59 For example, an opt_pass* might appear as:
|
|
60 (gdb) p pass
|
|
61 $2 = <opt_pass* 0x188b600 "expand"(170)>
|
|
62
|
|
63 The name of the pass is given ("expand"), together with the
|
|
64 static_pass_number.
|
|
65
|
|
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...]
|
|
70
|
|
71 and you can suppress pretty-printers using /r (for "raw"):
|
|
72 (gdb) p /r pass
|
|
73 $3 = (opt_pass *) 0x188b600
|
|
74
|
|
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)>
|
|
83
|
|
84 CFG edges are shown with the src and dest blocks given in parentheses:
|
|
85 (gdb) p e
|
|
86 $1 = <edge 0x7ffff043f118 (ENTRY -> 6)>
|
|
87
|
|
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".
|
|
94
|
|
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.
|
|
104
|
|
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">
|
|
112
|
|
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)>}
|
|
117
|
|
118 and here's a length 1 vec:
|
|
119 (gdb) p bb->succs
|
|
120 $19 = 0x7ffff0428bb8 = {<edge 0x7ffff044d3f0 (5 -> EXIT)>}
|
|
121
|
|
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).
|
|
126
|
|
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)>
|
|
132 """
|
|
133 import os.path
|
|
134 import re
|
|
135 import sys
|
|
136 import tempfile
|
|
137
|
|
138 import gdb
|
|
139 import gdb.printing
|
|
140 import gdb.types
|
|
141
|
|
142 # Convert "enum tree_code" (tree.def and tree.h) to a dict:
|
|
143 tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code'))
|
|
144
|
|
145 # ...and look up specific values for use later:
|
|
146 IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE']
|
|
147 TYPE_DECL = tree_code_dict['TYPE_DECL']
|
|
148
|
|
149 # Similarly for "enum tree_code_class" (tree.h):
|
|
150 tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class'))
|
|
151 tcc_type = tree_code_class_dict['tcc_type']
|
|
152 tcc_declaration = tree_code_class_dict['tcc_declaration']
|
|
153
|
|
154 # Python3 has int() with arbitrary precision (bignum). Python2 int() is 32-bit
|
|
155 # on 32-bit hosts but remote targets may have 64-bit pointers there; Python2
|
|
156 # long() is always 64-bit but Python3 no longer has anything named long.
|
|
157 def intptr(gdbval):
|
|
158 return long(gdbval) if sys.version_info.major == 2 else int(gdbval)
|
|
159
|
|
160 class Tree:
|
|
161 """
|
|
162 Wrapper around a gdb.Value for a tree, with various methods
|
|
163 corresponding to macros in gcc/tree.h
|
|
164 """
|
|
165 def __init__(self, gdbval):
|
|
166 self.gdbval = gdbval
|
|
167
|
|
168 def is_nonnull(self):
|
|
169 return intptr(self.gdbval)
|
|
170
|
|
171 def TREE_CODE(self):
|
|
172 """
|
|
173 Get gdb.Value corresponding to TREE_CODE (self)
|
|
174 as per:
|
|
175 #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
|
|
176 """
|
|
177 return self.gdbval['base']['code']
|
|
178
|
|
179 def DECL_NAME(self):
|
|
180 """
|
|
181 Get Tree instance corresponding to DECL_NAME (self)
|
|
182 """
|
|
183 return Tree(self.gdbval['decl_minimal']['name'])
|
|
184
|
|
185 def TYPE_NAME(self):
|
|
186 """
|
|
187 Get Tree instance corresponding to result of TYPE_NAME (self)
|
|
188 """
|
|
189 return Tree(self.gdbval['type_common']['name'])
|
|
190
|
|
191 def IDENTIFIER_POINTER(self):
|
|
192 """
|
|
193 Get str correspoinding to result of IDENTIFIER_NODE (self)
|
|
194 """
|
|
195 return self.gdbval['identifier']['id']['str'].string()
|
|
196
|
|
197 class TreePrinter:
|
|
198 "Prints a tree"
|
|
199
|
|
200 def __init__ (self, gdbval):
|
|
201 self.gdbval = gdbval
|
|
202 self.node = Tree(gdbval)
|
|
203
|
|
204 def to_string (self):
|
|
205 # like gcc/print-tree.c:print_node_brief
|
|
206 # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
|
|
207 # tree_code_name[(int) TREE_CODE (node)])
|
|
208 if intptr(self.gdbval) == 0:
|
|
209 return '<tree 0x0>'
|
|
210
|
|
211 val_TREE_CODE = self.node.TREE_CODE()
|
|
212
|
|
213 # extern const enum tree_code_class tree_code_type[];
|
|
214 # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
|
|
215
|
|
216 val_tree_code_type = gdb.parse_and_eval('tree_code_type')
|
|
217 val_tclass = val_tree_code_type[val_TREE_CODE]
|
|
218
|
|
219 val_tree_code_name = gdb.parse_and_eval('tree_code_name')
|
|
220 val_code_name = val_tree_code_name[intptr(val_TREE_CODE)]
|
|
221 #print(val_code_name.string())
|
|
222
|
|
223 result = '<%s 0x%x' % (val_code_name.string(), intptr(self.gdbval))
|
|
224 if intptr(val_tclass) == tcc_declaration:
|
|
225 tree_DECL_NAME = self.node.DECL_NAME()
|
|
226 if tree_DECL_NAME.is_nonnull():
|
|
227 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
|
|
228 else:
|
|
229 pass # TODO: labels etc
|
|
230 elif intptr(val_tclass) == tcc_type:
|
|
231 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
|
|
232 if tree_TYPE_NAME.is_nonnull():
|
|
233 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
|
|
234 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
|
|
235 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
|
|
236 if tree_TYPE_NAME.DECL_NAME().is_nonnull():
|
|
237 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
|
|
238 if self.node.TREE_CODE() == IDENTIFIER_NODE:
|
|
239 result += ' %s' % self.node.IDENTIFIER_POINTER()
|
|
240 # etc
|
|
241 result += '>'
|
|
242 return result
|
|
243
|
|
244 ######################################################################
|
|
245 # Callgraph pretty-printers
|
|
246 ######################################################################
|
|
247
|
|
248 class CGraphNodePrinter:
|
|
249 def __init__(self, gdbval):
|
|
250 self.gdbval = gdbval
|
|
251
|
|
252 def to_string (self):
|
|
253 result = '<cgraph_node* 0x%x' % intptr(self.gdbval)
|
|
254 if intptr(self.gdbval):
|
|
255 # symtab_node::name calls lang_hooks.decl_printable_name
|
|
256 # default implementation (lhd_decl_printable_name) is:
|
|
257 # return IDENTIFIER_POINTER (DECL_NAME (decl));
|
|
258 tree_decl = Tree(self.gdbval['decl'])
|
|
259 result += ' "%s"' % tree_decl.DECL_NAME().IDENTIFIER_POINTER()
|
|
260 result += '>'
|
|
261 return result
|
|
262
|
|
263 ######################################################################
|
|
264 # Dwarf DIE pretty-printers
|
|
265 ######################################################################
|
|
266
|
|
267 class DWDieRefPrinter:
|
|
268 def __init__(self, gdbval):
|
|
269 self.gdbval = gdbval
|
|
270
|
|
271 def to_string (self):
|
|
272 if intptr(self.gdbval) == 0:
|
|
273 return '<dw_die_ref 0x0>'
|
|
274 result = '<dw_die_ref 0x%x' % intptr(self.gdbval)
|
|
275 result += ' %s' % self.gdbval['die_tag']
|
|
276 if intptr(self.gdbval['die_parent']) != 0:
|
|
277 result += ' <parent=0x%x %s>' % (intptr(self.gdbval['die_parent']),
|
|
278 self.gdbval['die_parent']['die_tag'])
|
|
279
|
|
280 result += '>'
|
|
281 return result
|
|
282
|
|
283 ######################################################################
|
|
284
|
|
285 class GimplePrinter:
|
|
286 def __init__(self, gdbval):
|
|
287 self.gdbval = gdbval
|
|
288
|
|
289 def to_string (self):
|
|
290 if intptr(self.gdbval) == 0:
|
|
291 return '<gimple 0x0>'
|
|
292 val_gimple_code = self.gdbval['code']
|
|
293 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
|
|
294 val_code_name = val_gimple_code_name[intptr(val_gimple_code)]
|
|
295 result = '<%s 0x%x' % (val_code_name.string(),
|
|
296 intptr(self.gdbval))
|
|
297 result += '>'
|
|
298 return result
|
|
299
|
|
300 ######################################################################
|
|
301 # CFG pretty-printers
|
|
302 ######################################################################
|
|
303
|
|
304 def bb_index_to_str(index):
|
|
305 if index == 0:
|
|
306 return 'ENTRY'
|
|
307 elif index == 1:
|
|
308 return 'EXIT'
|
|
309 else:
|
|
310 return '%i' % index
|
|
311
|
|
312 class BasicBlockPrinter:
|
|
313 def __init__(self, gdbval):
|
|
314 self.gdbval = gdbval
|
|
315
|
|
316 def to_string (self):
|
|
317 result = '<basic_block 0x%x' % intptr(self.gdbval)
|
|
318 if intptr(self.gdbval):
|
|
319 result += ' (%s)' % bb_index_to_str(intptr(self.gdbval['index']))
|
|
320 result += '>'
|
|
321 return result
|
|
322
|
|
323 class CfgEdgePrinter:
|
|
324 def __init__(self, gdbval):
|
|
325 self.gdbval = gdbval
|
|
326
|
|
327 def to_string (self):
|
|
328 result = '<edge 0x%x' % intptr(self.gdbval)
|
|
329 if intptr(self.gdbval):
|
|
330 src = bb_index_to_str(intptr(self.gdbval['src']['index']))
|
|
331 dest = bb_index_to_str(intptr(self.gdbval['dest']['index']))
|
|
332 result += ' (%s -> %s)' % (src, dest)
|
|
333 result += '>'
|
|
334 return result
|
|
335
|
|
336 ######################################################################
|
|
337
|
|
338 class Rtx:
|
|
339 def __init__(self, gdbval):
|
|
340 self.gdbval = gdbval
|
|
341
|
|
342 def GET_CODE(self):
|
|
343 return self.gdbval['code']
|
|
344
|
|
345 def GET_RTX_LENGTH(code):
|
|
346 val_rtx_length = gdb.parse_and_eval('rtx_length')
|
|
347 return intptr(val_rtx_length[code])
|
|
348
|
|
349 def GET_RTX_NAME(code):
|
|
350 val_rtx_name = gdb.parse_and_eval('rtx_name')
|
|
351 return val_rtx_name[code].string()
|
|
352
|
|
353 def GET_RTX_FORMAT(code):
|
|
354 val_rtx_format = gdb.parse_and_eval('rtx_format')
|
|
355 return val_rtx_format[code].string()
|
|
356
|
|
357 class RtxPrinter:
|
|
358 def __init__(self, gdbval):
|
|
359 self.gdbval = gdbval
|
|
360 self.rtx = Rtx(gdbval)
|
|
361
|
|
362 def to_string (self):
|
|
363 """
|
|
364 For now, a cheap kludge: invoke the inferior's print
|
|
365 function to get a string to use the user, and return an empty
|
|
366 string for gdb
|
|
367 """
|
|
368 # We use print_inline_rtx to avoid a trailing newline
|
|
369 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
|
|
370 % intptr(self.gdbval))
|
|
371 return ''
|
|
372
|
|
373 # or by hand; based on gcc/print-rtl.c:print_rtx
|
|
374 result = ('<rtx_def 0x%x'
|
|
375 % (intptr(self.gdbval)))
|
|
376 code = self.rtx.GET_CODE()
|
|
377 result += ' (%s' % GET_RTX_NAME(code)
|
|
378 format_ = GET_RTX_FORMAT(code)
|
|
379 for i in range(GET_RTX_LENGTH(code)):
|
|
380 print(format_[i])
|
|
381 result += ')>'
|
|
382 return result
|
|
383
|
|
384 ######################################################################
|
|
385
|
|
386 class PassPrinter:
|
|
387 def __init__(self, gdbval):
|
|
388 self.gdbval = gdbval
|
|
389
|
|
390 def to_string (self):
|
|
391 result = '<opt_pass* 0x%x' % intptr(self.gdbval)
|
|
392 if intptr(self.gdbval):
|
|
393 result += (' "%s"(%i)'
|
|
394 % (self.gdbval['name'].string(),
|
|
395 intptr(self.gdbval['static_pass_number'])))
|
|
396 result += '>'
|
|
397 return result
|
|
398
|
|
399 ######################################################################
|
|
400
|
|
401 class VecPrinter:
|
|
402 # -ex "up" -ex "p bb->preds"
|
|
403 def __init__(self, gdbval):
|
|
404 self.gdbval = gdbval
|
|
405
|
|
406 def display_hint (self):
|
|
407 return 'array'
|
|
408
|
|
409 def to_string (self):
|
|
410 # A trivial implementation; prettyprinting the contents is done
|
|
411 # by gdb calling the "children" method below.
|
|
412 return '0x%x' % intptr(self.gdbval)
|
|
413
|
|
414 def children (self):
|
|
415 if intptr(self.gdbval) == 0:
|
|
416 return
|
|
417 m_vecpfx = self.gdbval['m_vecpfx']
|
|
418 m_num = m_vecpfx['m_num']
|
|
419 m_vecdata = self.gdbval['m_vecdata']
|
|
420 for i in range(m_num):
|
|
421 yield ('[%d]' % i, m_vecdata[i])
|
|
422
|
|
423 ######################################################################
|
|
424
|
|
425 class MachineModePrinter:
|
|
426 def __init__(self, gdbval):
|
|
427 self.gdbval = gdbval
|
|
428
|
|
429 def to_string (self):
|
|
430 name = str(self.gdbval['m_mode'])
|
|
431 return name[2:] if name.startswith('E_') else name
|
|
432
|
|
433 ######################################################################
|
|
434
|
|
435 class OptMachineModePrinter:
|
|
436 def __init__(self, gdbval):
|
|
437 self.gdbval = gdbval
|
|
438
|
|
439 def to_string (self):
|
|
440 name = str(self.gdbval['m_mode'])
|
|
441 if name == 'E_VOIDmode':
|
|
442 return '<None>'
|
|
443 return name[2:] if name.startswith('E_') else name
|
|
444
|
|
445 ######################################################################
|
|
446
|
|
447 # TODO:
|
|
448 # * hashtab
|
|
449 # * location_t
|
|
450
|
|
451 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
|
|
452 def __init__(self, name, class_):
|
|
453 super(GdbSubprinter, self).__init__(name)
|
|
454 self.class_ = class_
|
|
455
|
|
456 def handles_type(self, str_type):
|
|
457 raise NotImplementedError
|
|
458
|
|
459 class GdbSubprinterTypeList(GdbSubprinter):
|
|
460 """
|
|
461 A GdbSubprinter that handles a specific set of types
|
|
462 """
|
|
463 def __init__(self, str_types, name, class_):
|
|
464 super(GdbSubprinterTypeList, self).__init__(name, class_)
|
|
465 self.str_types = frozenset(str_types)
|
|
466
|
|
467 def handles_type(self, str_type):
|
|
468 return str_type in self.str_types
|
|
469
|
|
470 class GdbSubprinterRegex(GdbSubprinter):
|
|
471 """
|
|
472 A GdbSubprinter that handles types that match a regex
|
|
473 """
|
|
474 def __init__(self, regex, name, class_):
|
|
475 super(GdbSubprinterRegex, self).__init__(name, class_)
|
|
476 self.regex = re.compile(regex)
|
|
477
|
|
478 def handles_type(self, str_type):
|
|
479 return self.regex.match(str_type)
|
|
480
|
|
481 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
|
|
482 def __init__(self, name):
|
|
483 super(GdbPrettyPrinters, self).__init__(name, [])
|
|
484
|
|
485 def add_printer_for_types(self, name, class_, types):
|
|
486 self.subprinters.append(GdbSubprinterTypeList(name, class_, types))
|
|
487
|
|
488 def add_printer_for_regex(self, name, class_, regex):
|
|
489 self.subprinters.append(GdbSubprinterRegex(name, class_, regex))
|
|
490
|
|
491 def __call__(self, gdbval):
|
|
492 type_ = gdbval.type.unqualified()
|
|
493 str_type = str(type_)
|
|
494 for printer in self.subprinters:
|
|
495 if printer.enabled and printer.handles_type(str_type):
|
|
496 return printer.class_(gdbval)
|
|
497
|
|
498 # Couldn't find a pretty printer (or it was disabled):
|
|
499 return None
|
|
500
|
|
501
|
|
502 def build_pretty_printer():
|
|
503 pp = GdbPrettyPrinters('gcc')
|
|
504 pp.add_printer_for_types(['tree'],
|
|
505 'tree', TreePrinter)
|
|
506 pp.add_printer_for_types(['cgraph_node *'],
|
|
507 'cgraph_node', CGraphNodePrinter)
|
|
508 pp.add_printer_for_types(['dw_die_ref'],
|
|
509 'dw_die_ref', DWDieRefPrinter)
|
|
510 pp.add_printer_for_types(['gimple', 'gimple *',
|
|
511
|
|
512 # Keep this in the same order as gimple.def:
|
|
513 'gimple_cond', 'const_gimple_cond',
|
|
514 'gimple_statement_cond *',
|
|
515 'gimple_debug', 'const_gimple_debug',
|
|
516 'gimple_statement_debug *',
|
|
517 'gimple_label', 'const_gimple_label',
|
|
518 'gimple_statement_label *',
|
|
519 'gimple_switch', 'const_gimple_switch',
|
|
520 'gimple_statement_switch *',
|
|
521 'gimple_assign', 'const_gimple_assign',
|
|
522 'gimple_statement_assign *',
|
|
523 'gimple_bind', 'const_gimple_bind',
|
|
524 'gimple_statement_bind *',
|
|
525 'gimple_phi', 'const_gimple_phi',
|
|
526 'gimple_statement_phi *'],
|
|
527
|
|
528 'gimple',
|
|
529 GimplePrinter)
|
|
530 pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
|
|
531 'basic_block',
|
|
532 BasicBlockPrinter)
|
|
533 pp.add_printer_for_types(['edge', 'edge_def *'],
|
|
534 'edge',
|
|
535 CfgEdgePrinter)
|
|
536 pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
|
|
537 pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
|
|
538
|
|
539 pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*',
|
|
540 'vec',
|
|
541 VecPrinter)
|
|
542
|
|
543 pp.add_printer_for_regex(r'opt_mode<(\S+)>',
|
|
544 'opt_mode', OptMachineModePrinter)
|
|
545 pp.add_printer_for_types(['opt_scalar_int_mode',
|
|
546 'opt_scalar_float_mode',
|
|
547 'opt_scalar_mode'],
|
|
548 'opt_mode', OptMachineModePrinter)
|
|
549 pp.add_printer_for_regex(r'pod_mode<(\S+)>',
|
|
550 'pod_mode', MachineModePrinter)
|
|
551 pp.add_printer_for_types(['scalar_int_mode_pod',
|
|
552 'scalar_mode_pod'],
|
|
553 'pod_mode', MachineModePrinter)
|
|
554 for mode in ('scalar_mode', 'scalar_int_mode', 'scalar_float_mode',
|
|
555 'complex_mode'):
|
|
556 pp.add_printer_for_types([mode], mode, MachineModePrinter)
|
|
557
|
|
558 return pp
|
|
559
|
|
560 gdb.printing.register_pretty_printer(
|
|
561 gdb.current_objfile(),
|
|
562 build_pretty_printer())
|
|
563
|
|
564 def find_gcc_source_dir():
|
|
565 # Use location of global "g" to locate the source tree
|
|
566 sym_g = gdb.lookup_global_symbol('g')
|
|
567 path = sym_g.symtab.filename # e.g. '../../src/gcc/context.h'
|
|
568 srcdir = os.path.split(path)[0] # e.g. '../../src/gcc'
|
|
569 return srcdir
|
|
570
|
|
571 class PassNames:
|
|
572 """Parse passes.def, gathering a list of pass class names"""
|
|
573 def __init__(self):
|
|
574 srcdir = find_gcc_source_dir()
|
|
575 self.names = []
|
|
576 with open(os.path.join(srcdir, 'passes.def')) as f:
|
|
577 for line in f:
|
|
578 m = re.match('\s*NEXT_PASS \(([^,]+).*\);', line)
|
|
579 if m:
|
|
580 self.names.append(m.group(1))
|
|
581
|
|
582 class BreakOnPass(gdb.Command):
|
|
583 """
|
|
584 A custom command for putting breakpoints on the execute hook of passes.
|
|
585 This is largely a workaround for issues with tab-completion in gdb when
|
|
586 setting breakpoints on methods on classes within anonymous namespaces.
|
|
587
|
|
588 Example of use: putting a breakpoint on "final"
|
|
589 (gdb) break-on-pass
|
|
590 Press <TAB>; it autocompletes to "pass_":
|
|
591 (gdb) break-on-pass pass_
|
|
592 Press <TAB>:
|
|
593 Display all 219 possibilities? (y or n)
|
|
594 Press "n"; then type "f":
|
|
595 (gdb) break-on-pass pass_f
|
|
596 Press <TAB> to autocomplete to pass classnames beginning with "pass_f":
|
|
597 pass_fast_rtl_dce pass_fold_builtins
|
|
598 pass_feedback_split_functions pass_forwprop
|
|
599 pass_final pass_fre
|
|
600 pass_fixup_cfg pass_free_cfg
|
|
601 Type "in<TAB>" to complete to "pass_final":
|
|
602 (gdb) break-on-pass pass_final
|
|
603 ...and hit <RETURN>:
|
|
604 Breakpoint 6 at 0x8396ba: file ../../src/gcc/final.c, line 4526.
|
|
605 ...and we have a breakpoint set; continue execution:
|
|
606 (gdb) cont
|
|
607 Continuing.
|
|
608 Breakpoint 6, (anonymous namespace)::pass_final::execute (this=0x17fb990) at ../../src/gcc/final.c:4526
|
|
609 4526 virtual unsigned int execute (function *) { return rest_of_handle_final (); }
|
|
610 """
|
|
611 def __init__(self):
|
|
612 gdb.Command.__init__(self, 'break-on-pass', gdb.COMMAND_BREAKPOINTS)
|
|
613 self.pass_names = None
|
|
614
|
|
615 def complete(self, text, word):
|
|
616 # Lazily load pass names:
|
|
617 if not self.pass_names:
|
|
618 self.pass_names = PassNames()
|
|
619
|
|
620 return [name
|
|
621 for name in sorted(self.pass_names.names)
|
|
622 if name.startswith(text)]
|
|
623
|
|
624 def invoke(self, arg, from_tty):
|
|
625 sym = '(anonymous namespace)::%s::execute' % arg
|
|
626 breakpoint = gdb.Breakpoint(sym)
|
|
627
|
|
628 BreakOnPass()
|
|
629
|
|
630 class DumpFn(gdb.Command):
|
|
631 """
|
|
632 A custom command to dump a gimple/rtl function to file. By default, it
|
|
633 dumps the current function using 0 as dump_flags, but the function and flags
|
|
634 can also be specified. If /f <file> are passed as the first two arguments,
|
|
635 the dump is written to that file. Otherwise, a temporary file is created
|
|
636 and opened in the text editor specified in the EDITOR environment variable.
|
|
637
|
|
638 Examples of use:
|
|
639 (gdb) dump-fn
|
|
640 (gdb) dump-fn /f foo.1.txt
|
|
641 (gdb) dump-fn cfun->decl
|
|
642 (gdb) dump-fn /f foo.1.txt cfun->decl
|
|
643 (gdb) dump-fn cfun->decl 0
|
|
644 (gdb) dump-fn cfun->decl dump_flags
|
|
645 """
|
|
646
|
|
647 def __init__(self):
|
|
648 gdb.Command.__init__(self, 'dump-fn', gdb.COMMAND_USER)
|
|
649
|
|
650 def invoke(self, arg, from_tty):
|
|
651 # Parse args, check number of args
|
|
652 args = gdb.string_to_argv(arg)
|
|
653 if len(args) >= 1 and args[0] == "/f":
|
|
654 if len(args) == 1:
|
|
655 print ("Missing file argument")
|
|
656 return
|
|
657 filename = args[1]
|
|
658 editor_mode = False
|
|
659 base_arg = 2
|
|
660 else:
|
|
661 editor = os.getenv("EDITOR", "")
|
|
662 if editor == "":
|
|
663 print ("EDITOR environment variable not defined")
|
|
664 return
|
|
665 editor_mode = True
|
|
666 base_arg = 0
|
|
667 if len(args) - base_arg > 2:
|
|
668 print ("Too many arguments")
|
|
669 return
|
|
670
|
|
671 # Set func
|
|
672 if len(args) - base_arg >= 1:
|
|
673 funcname = args[base_arg]
|
|
674 printfuncname = "function %s" % funcname
|
|
675 else:
|
|
676 funcname = "cfun ? cfun->decl : current_function_decl"
|
|
677 printfuncname = "current function"
|
|
678 func = gdb.parse_and_eval(funcname)
|
|
679 if func == 0:
|
|
680 print ("Could not find %s" % printfuncname)
|
|
681 return
|
|
682 func = "(tree)%u" % func
|
|
683
|
|
684 # Set flags
|
|
685 if len(args) - base_arg >= 2:
|
|
686 flags = gdb.parse_and_eval(args[base_arg + 1])
|
|
687 else:
|
|
688 flags = 0
|
|
689
|
|
690 # Get tempory file, if necessary
|
|
691 if editor_mode:
|
|
692 f = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
|
693 filename = f.name
|
|
694 f.close()
|
|
695
|
|
696 # Open file
|
|
697 fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
|
|
698 if fp == 0:
|
|
699 print ("Could not open file: %s" % filename)
|
|
700 return
|
|
701 fp = "(FILE *)%u" % fp
|
|
702
|
|
703 # Dump function to file
|
|
704 _ = gdb.parse_and_eval("dump_function_to_file (%s, %s, %u)" %
|
|
705 (func, fp, flags))
|
|
706
|
|
707 # Close file
|
|
708 ret = gdb.parse_and_eval("fclose (%s)" % fp)
|
|
709 if ret != 0:
|
|
710 print ("Could not close file: %s" % filename)
|
|
711 return
|
|
712
|
|
713 # Open file in editor, if necessary
|
|
714 if editor_mode:
|
|
715 os.system("( %s \"%s\"; rm \"%s\" ) &" %
|
|
716 (editor, filename, filename))
|
|
717
|
|
718 DumpFn()
|
|
719
|
|
720 class DotFn(gdb.Command):
|
|
721 """
|
|
722 A custom command to show a gimple/rtl function control flow graph.
|
|
723 By default, it show the current function, but the function can also be
|
|
724 specified.
|
|
725
|
|
726 Examples of use:
|
|
727 (gdb) dot-fn
|
|
728 (gdb) dot-fn cfun
|
|
729 (gdb) dot-fn cfun 0
|
|
730 (gdb) dot-fn cfun dump_flags
|
|
731 """
|
|
732 def __init__(self):
|
|
733 gdb.Command.__init__(self, 'dot-fn', gdb.COMMAND_USER)
|
|
734
|
|
735 def invoke(self, arg, from_tty):
|
|
736 # Parse args, check number of args
|
|
737 args = gdb.string_to_argv(arg)
|
|
738 if len(args) > 2:
|
|
739 print("Too many arguments")
|
|
740 return
|
|
741
|
|
742 # Set func
|
|
743 if len(args) >= 1:
|
|
744 funcname = args[0]
|
|
745 printfuncname = "function %s" % funcname
|
|
746 else:
|
|
747 funcname = "cfun"
|
|
748 printfuncname = "current function"
|
|
749 func = gdb.parse_and_eval(funcname)
|
|
750 if func == 0:
|
|
751 print("Could not find %s" % printfuncname)
|
|
752 return
|
|
753 func = "(struct function *)%s" % func
|
|
754
|
|
755 # Set flags
|
|
756 if len(args) >= 2:
|
|
757 flags = gdb.parse_and_eval(args[1])
|
|
758 else:
|
|
759 flags = 0
|
|
760
|
|
761 # Get temp file
|
|
762 f = tempfile.NamedTemporaryFile(delete=False)
|
|
763 filename = f.name
|
|
764
|
|
765 # Close and reopen temp file to get C FILE*
|
|
766 f.close()
|
|
767 fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
|
|
768 if fp == 0:
|
|
769 print("Cannot open temp file")
|
|
770 return
|
|
771 fp = "(FILE *)%u" % fp
|
|
772
|
|
773 # Write graph to temp file
|
|
774 _ = gdb.parse_and_eval("start_graph_dump (%s, \"<debug>\")" % fp)
|
|
775 _ = gdb.parse_and_eval("print_graph_cfg (%s, %s, %u)"
|
|
776 % (fp, func, flags))
|
|
777 _ = gdb.parse_and_eval("end_graph_dump (%s)" % fp)
|
|
778
|
|
779 # Close temp file
|
|
780 ret = gdb.parse_and_eval("fclose (%s)" % fp)
|
|
781 if ret != 0:
|
|
782 print("Could not close temp file: %s" % filename)
|
|
783 return
|
|
784
|
|
785 # Show graph in temp file
|
|
786 os.system("( dot -Tx11 \"%s\"; rm \"%s\" ) &" % (filename, filename))
|
|
787
|
|
788 DotFn()
|
|
789
|
|
790 print('Successfully loaded GDB hooks for GCC')
|