1# Python hooks for gdb for debugging GCC
2# Copyright (C) 2013-2015 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"""
23Enabling the debugging hooks
24----------------------------
25gcc/configure (from configure.ac) generates a .gdbinit within the "gcc"
26subdirectory of the build directory, and when run by gdb, this imports
27gcc/gdbhooks.py from the source directory, injecting useful Python code
28into gdb.
29
30You 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'
32as a protection against untrustworthy python scripts.  See
33  http://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
34
35The fix is to mark the paths of the build/gcc directory as trustworthy.
36An 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
38for the build directories for your various checkouts of gcc.
39
40If it's working, you should see the message:
41  Successfully loaded GDB hooks for GCC
42as gdb starts up.
43
44During development, I've been manually invoking the code in this way, as a
45precanned 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
54Examples of output using the pretty-printers
55--------------------------------------------
56Pointer values are generally shown in the form:
57  <type address extra_info>
58
59For example, an opt_pass* might appear as:
60  (gdb) p pass
61  $2 = <opt_pass* 0x188b600 "expand"(170)>
62
63The name of the pass is given ("expand"), together with the
64static_pass_number.
65
66Note 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
71and you can suppress pretty-printers using /r (for "raw"):
72  (gdb) p /r pass
73  $3 = (opt_pass *) 0x188b600
74
75Basic blocks are shown with their index in parentheses, apart from the
76CFG'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
84CFG edges are shown with the src and dest blocks given in parentheses:
85  (gdb) p e
86  $1 = <edge 0x7ffff043f118 (ENTRY -> 6)>
87
88Tree nodes are printed using Python code that emulates print_node_brief,
89running in gdb, rather than in the inferior:
90  (gdb) p cfun->decl
91  $1 = <function_decl 0x7ffff0420b00 foo>
92For usability, the type is printed first (e.g. "function_decl"), rather
93than just "tree".
94
95RTL expressions use a kludge: they are pretty-printed by injecting
96calls 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
102This won't work for coredumps, and probably in other circumstances, but
103it's a quick way of getting lots of debuggability quickly.
104
105Callgraph nodes are printed with the name of the function decl, if
106available:
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
113vec<> pointers are printed as the address followed by the elements in
114braces.  Here's a length 2 vec:
115  (gdb) p bb->preds
116  $18 = 0x7ffff0428b68 = {<edge 0x7ffff044d380 (3 -> 5)>, <edge 0x7ffff044d3b8 (4 -> 5)>}
117
118and here's a length 1 vec:
119  (gdb) p bb->succs
120  $19 = 0x7ffff0428bb8 = {<edge 0x7ffff044d3f0 (5 -> EXIT)>}
121
122You cannot yet use array notation [] to access the elements within the
123vector: attempting to do so instead gives you the vec itself (for vec[0]),
124or a (probably) invalid cast to vec<> for the memory after the vec (for
125vec[1] onwards).
126
127Instead (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"""
133import os.path
134import re
135
136import gdb
137import gdb.printing
138import gdb.types
139
140# Convert "enum tree_code" (tree.def and tree.h) to a dict:
141tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code'))
142
143# ...and look up specific values for use later:
144IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE']
145TYPE_DECL = tree_code_dict['TYPE_DECL']
146
147# Similarly for "enum tree_code_class" (tree.h):
148tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class'))
149tcc_type = tree_code_class_dict['tcc_type']
150tcc_declaration = tree_code_class_dict['tcc_declaration']
151
152class Tree:
153    """
154    Wrapper around a gdb.Value for a tree, with various methods
155    corresponding to macros in gcc/tree.h
156    """
157    def __init__(self, gdbval):
158        self.gdbval = gdbval
159
160    def is_nonnull(self):
161        return long(self.gdbval)
162
163    def TREE_CODE(self):
164        """
165        Get gdb.Value corresponding to TREE_CODE (self)
166        as per:
167          #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
168        """
169        return self.gdbval['base']['code']
170
171    def DECL_NAME(self):
172        """
173        Get Tree instance corresponding to DECL_NAME (self)
174        """
175        return Tree(self.gdbval['decl_minimal']['name'])
176
177    def TYPE_NAME(self):
178        """
179        Get Tree instance corresponding to result of TYPE_NAME (self)
180        """
181        return Tree(self.gdbval['type_common']['name'])
182
183    def IDENTIFIER_POINTER(self):
184        """
185        Get str correspoinding to result of IDENTIFIER_NODE (self)
186        """
187        return self.gdbval['identifier']['id']['str'].string()
188
189class TreePrinter:
190    "Prints a tree"
191
192    def __init__ (self, gdbval):
193        self.gdbval = gdbval
194        self.node = Tree(gdbval)
195
196    def to_string (self):
197        # like gcc/print-tree.c:print_node_brief
198        # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
199        # tree_code_name[(int) TREE_CODE (node)])
200        if long(self.gdbval) == 0:
201            return '<tree 0x0>'
202
203        val_TREE_CODE = self.node.TREE_CODE()
204
205        # extern const enum tree_code_class tree_code_type[];
206        # #define TREE_CODE_CLASS(CODE)	tree_code_type[(int) (CODE)]
207
208        val_tree_code_type = gdb.parse_and_eval('tree_code_type')
209        val_tclass = val_tree_code_type[val_TREE_CODE]
210
211        val_tree_code_name = gdb.parse_and_eval('tree_code_name')
212        val_code_name = val_tree_code_name[long(val_TREE_CODE)]
213        #print val_code_name.string()
214
215        result = '<%s 0x%x' % (val_code_name.string(), long(self.gdbval))
216        if long(val_tclass) == tcc_declaration:
217            tree_DECL_NAME = self.node.DECL_NAME()
218            if tree_DECL_NAME.is_nonnull():
219                 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
220            else:
221                pass # TODO: labels etc
222        elif long(val_tclass) == tcc_type:
223            tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
224            if tree_TYPE_NAME.is_nonnull():
225                if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
226                    result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
227                elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
228                    if tree_TYPE_NAME.DECL_NAME().is_nonnull():
229                        result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
230        if self.node.TREE_CODE() == IDENTIFIER_NODE:
231            result += ' %s' % self.node.IDENTIFIER_POINTER()
232        # etc
233        result += '>'
234        return result
235
236######################################################################
237# Callgraph pretty-printers
238######################################################################
239
240class CGraphNodePrinter:
241    def __init__(self, gdbval):
242        self.gdbval = gdbval
243
244    def to_string (self):
245        result = '<cgraph_node* 0x%x' % long(self.gdbval)
246        if long(self.gdbval):
247            # symtab_node::name calls lang_hooks.decl_printable_name
248            # default implementation (lhd_decl_printable_name) is:
249            #    return IDENTIFIER_POINTER (DECL_NAME (decl));
250            tree_decl = Tree(self.gdbval['decl'])
251            result += ' "%s"' % tree_decl.DECL_NAME().IDENTIFIER_POINTER()
252        result += '>'
253        return result
254
255######################################################################
256# Dwarf DIE pretty-printers
257######################################################################
258
259class DWDieRefPrinter:
260    def __init__(self, gdbval):
261        self.gdbval = gdbval
262
263    def to_string (self):
264        if long(self.gdbval) == 0:
265            return '<dw_die_ref 0x0>'
266        result = '<dw_die_ref 0x%x' % long(self.gdbval)
267        result += ' %s' % self.gdbval['die_tag']
268        if long(self.gdbval['die_parent']) != 0:
269            result += ' <parent=0x%x %s>' % (long(self.gdbval['die_parent']),
270                                             self.gdbval['die_parent']['die_tag'])
271
272        result += '>'
273        return result
274
275######################################################################
276
277class GimplePrinter:
278    def __init__(self, gdbval):
279        self.gdbval = gdbval
280
281    def to_string (self):
282        if long(self.gdbval) == 0:
283            return '<gimple 0x0>'
284        val_gimple_code = self.gdbval['code']
285        val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
286        val_code_name = val_gimple_code_name[long(val_gimple_code)]
287        result = '<%s 0x%x' % (val_code_name.string(),
288                               long(self.gdbval))
289        result += '>'
290        return result
291
292######################################################################
293# CFG pretty-printers
294######################################################################
295
296def bb_index_to_str(index):
297    if index == 0:
298        return 'ENTRY'
299    elif index == 1:
300        return 'EXIT'
301    else:
302        return '%i' % index
303
304class BasicBlockPrinter:
305    def __init__(self, gdbval):
306        self.gdbval = gdbval
307
308    def to_string (self):
309        result = '<basic_block 0x%x' % long(self.gdbval)
310        if long(self.gdbval):
311            result += ' (%s)' % bb_index_to_str(long(self.gdbval['index']))
312        result += '>'
313        return result
314
315class CfgEdgePrinter:
316    def __init__(self, gdbval):
317        self.gdbval = gdbval
318
319    def to_string (self):
320        result = '<edge 0x%x' % long(self.gdbval)
321        if long(self.gdbval):
322            src = bb_index_to_str(long(self.gdbval['src']['index']))
323            dest = bb_index_to_str(long(self.gdbval['dest']['index']))
324            result += ' (%s -> %s)' % (src, dest)
325        result += '>'
326        return result
327
328######################################################################
329
330class Rtx:
331    def __init__(self, gdbval):
332        self.gdbval = gdbval
333
334    def GET_CODE(self):
335        return self.gdbval['code']
336
337def GET_RTX_LENGTH(code):
338    val_rtx_length = gdb.parse_and_eval('rtx_length')
339    return long(val_rtx_length[code])
340
341def GET_RTX_NAME(code):
342    val_rtx_name = gdb.parse_and_eval('rtx_name')
343    return val_rtx_name[code].string()
344
345def GET_RTX_FORMAT(code):
346    val_rtx_format = gdb.parse_and_eval('rtx_format')
347    return val_rtx_format[code].string()
348
349class RtxPrinter:
350    def __init__(self, gdbval):
351        self.gdbval = gdbval
352        self.rtx = Rtx(gdbval)
353
354    def to_string (self):
355        """
356        For now, a cheap kludge: invoke the inferior's print
357        function to get a string to use the user, and return an empty
358        string for gdb
359        """
360        # We use print_inline_rtx to avoid a trailing newline
361        gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
362                    % long(self.gdbval))
363        return ''
364
365        # or by hand; based on gcc/print-rtl.c:print_rtx
366        result = ('<rtx_def 0x%x'
367                  % (long(self.gdbval)))
368        code = self.rtx.GET_CODE()
369        result += ' (%s' % GET_RTX_NAME(code)
370        format_ = GET_RTX_FORMAT(code)
371        for i in range(GET_RTX_LENGTH(code)):
372            print format_[i]
373        result += ')>'
374        return result
375
376######################################################################
377
378class PassPrinter:
379    def __init__(self, gdbval):
380        self.gdbval = gdbval
381
382    def to_string (self):
383        result = '<opt_pass* 0x%x' % long(self.gdbval)
384        if long(self.gdbval):
385            result += (' "%s"(%i)'
386                       % (self.gdbval['name'].string(),
387                          long(self.gdbval['static_pass_number'])))
388        result += '>'
389        return result
390
391######################################################################
392
393class VecPrinter:
394    #    -ex "up" -ex "p bb->preds"
395    def __init__(self, gdbval):
396        self.gdbval = gdbval
397
398    def display_hint (self):
399        return 'array'
400
401    def to_string (self):
402        # A trivial implementation; prettyprinting the contents is done
403        # by gdb calling the "children" method below.
404        return '0x%x' % long(self.gdbval)
405
406    def children (self):
407        if long(self.gdbval) == 0:
408            return
409        m_vecpfx = self.gdbval['m_vecpfx']
410        m_num = m_vecpfx['m_num']
411        m_vecdata = self.gdbval['m_vecdata']
412        for i in range(m_num):
413            yield ('[%d]' % i, m_vecdata[i])
414
415######################################################################
416
417# TODO:
418#   * hashtab
419#   * location_t
420
421class GdbSubprinter(gdb.printing.SubPrettyPrinter):
422    def __init__(self, name, class_):
423        super(GdbSubprinter, self).__init__(name)
424        self.class_ = class_
425
426    def handles_type(self, str_type):
427        raise NotImplementedError
428
429class GdbSubprinterTypeList(GdbSubprinter):
430    """
431    A GdbSubprinter that handles a specific set of types
432    """
433    def __init__(self, str_types, name, class_):
434        super(GdbSubprinterTypeList, self).__init__(name, class_)
435        self.str_types = frozenset(str_types)
436
437    def handles_type(self, str_type):
438        return str_type in self.str_types
439
440class GdbSubprinterRegex(GdbSubprinter):
441    """
442    A GdbSubprinter that handles types that match a regex
443    """
444    def __init__(self, regex, name, class_):
445        super(GdbSubprinterRegex, self).__init__(name, class_)
446        self.regex = re.compile(regex)
447
448    def handles_type(self, str_type):
449        return self.regex.match(str_type)
450
451class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
452    def __init__(self, name):
453        super(GdbPrettyPrinters, self).__init__(name, [])
454
455    def add_printer_for_types(self, name, class_, types):
456        self.subprinters.append(GdbSubprinterTypeList(name, class_, types))
457
458    def add_printer_for_regex(self, name, class_, regex):
459        self.subprinters.append(GdbSubprinterRegex(name, class_, regex))
460
461    def __call__(self, gdbval):
462        type_ = gdbval.type.unqualified()
463        str_type = str(type_)
464        for printer in self.subprinters:
465            if printer.enabled and printer.handles_type(str_type):
466                return printer.class_(gdbval)
467
468        # Couldn't find a pretty printer (or it was disabled):
469        return None
470
471
472def build_pretty_printer():
473    pp = GdbPrettyPrinters('gcc')
474    pp.add_printer_for_types(['tree'],
475                             'tree', TreePrinter)
476    pp.add_printer_for_types(['cgraph_node *'],
477                             'cgraph_node', CGraphNodePrinter)
478    pp.add_printer_for_types(['dw_die_ref'],
479                             'dw_die_ref', DWDieRefPrinter)
480    pp.add_printer_for_types(['gimple', 'gimple_statement_base *',
481
482                              # Keep this in the same order as gimple.def:
483                              'gimple_cond', 'const_gimple_cond',
484                              'gimple_statement_cond *',
485                              'gimple_debug', 'const_gimple_debug',
486                              'gimple_statement_debug *',
487                              'gimple_label', 'const_gimple_label',
488                              'gimple_statement_label *',
489                              'gimple_switch', 'const_gimple_switch',
490                              'gimple_statement_switch *',
491                              'gimple_assign', 'const_gimple_assign',
492                              'gimple_statement_assign *',
493                              'gimple_bind', 'const_gimple_bind',
494                              'gimple_statement_bind *',
495                              'gimple_phi', 'const_gimple_phi',
496                              'gimple_statement_phi *'],
497
498                             'gimple',
499                             GimplePrinter)
500    pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
501                             'basic_block',
502                             BasicBlockPrinter)
503    pp.add_printer_for_types(['edge', 'edge_def *'],
504                             'edge',
505                             CfgEdgePrinter)
506    pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
507    pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
508
509    pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*',
510                             'vec',
511                             VecPrinter)
512
513    return pp
514
515gdb.printing.register_pretty_printer(
516    gdb.current_objfile(),
517    build_pretty_printer())
518
519def find_gcc_source_dir():
520    # Use location of global "g" to locate the source tree
521    sym_g = gdb.lookup_global_symbol('g')
522    path = sym_g.symtab.filename # e.g. '../../src/gcc/context.h'
523    srcdir = os.path.split(path)[0] # e.g. '../../src/gcc'
524    return srcdir
525
526class PassNames:
527    """Parse passes.def, gathering a list of pass class names"""
528    def __init__(self):
529        srcdir = find_gcc_source_dir()
530        self.names = []
531        with open(os.path.join(srcdir, 'passes.def')) as f:
532            for line in f:
533                m = re.match('\s*NEXT_PASS \((.+)\);', line)
534                if m:
535                    self.names.append(m.group(1))
536
537class BreakOnPass(gdb.Command):
538    """
539    A custom command for putting breakpoints on the execute hook of passes.
540    This is largely a workaround for issues with tab-completion in gdb when
541    setting breakpoints on methods on classes within anonymous namespaces.
542
543    Example of use: putting a breakpoint on "final"
544      (gdb) break-on-pass
545    Press <TAB>; it autocompletes to "pass_":
546      (gdb) break-on-pass pass_
547    Press <TAB>:
548      Display all 219 possibilities? (y or n)
549    Press "n"; then type "f":
550      (gdb) break-on-pass pass_f
551    Press <TAB> to autocomplete to pass classnames beginning with "pass_f":
552      pass_fast_rtl_dce              pass_fold_builtins
553      pass_feedback_split_functions  pass_forwprop
554      pass_final                     pass_fre
555      pass_fixup_cfg                 pass_free_cfg
556    Type "in<TAB>" to complete to "pass_final":
557      (gdb) break-on-pass pass_final
558    ...and hit <RETURN>:
559      Breakpoint 6 at 0x8396ba: file ../../src/gcc/final.c, line 4526.
560    ...and we have a breakpoint set; continue execution:
561      (gdb) cont
562      Continuing.
563      Breakpoint 6, (anonymous namespace)::pass_final::execute (this=0x17fb990) at ../../src/gcc/final.c:4526
564      4526	  virtual unsigned int execute (function *) { return rest_of_handle_final (); }
565    """
566    def __init__(self):
567        gdb.Command.__init__(self, 'break-on-pass', gdb.COMMAND_BREAKPOINTS)
568        self.pass_names = None
569
570    def complete(self, text, word):
571        # Lazily load pass names:
572        if not self.pass_names:
573            self.pass_names = PassNames()
574
575        return [name
576                for name in sorted(self.pass_names.names)
577                if name.startswith(text)]
578
579    def invoke(self, arg, from_tty):
580        sym = '(anonymous namespace)::%s::execute' % arg
581        breakpoint = gdb.Breakpoint(sym)
582
583BreakOnPass()
584
585print('Successfully loaded GDB hooks for GCC')
586