annotate gcc/gdbhooks.py @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 # Python hooks for gdb for debugging GCC
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 # Copyright (C) 2013-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 # Contributed by David Malcolm <dmalcolm@redhat.com>
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 # This file is part of GCC.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 # GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
9 # the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
10 # Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
11 # version.
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 # GCC is distributed in the hope that it will be useful, but WITHOUT
kono
parents:
diff changeset
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
15 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
16 # for more details.
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
19 # along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
20 # <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 """
kono
parents:
diff changeset
23 Enabling the debugging hooks
kono
parents:
diff changeset
24 ----------------------------
kono
parents:
diff changeset
25 gcc/configure (from configure.ac) generates a .gdbinit within the "gcc"
kono
parents:
diff changeset
26 subdirectory of the build directory, and when run by gdb, this imports
kono
parents:
diff changeset
27 gcc/gdbhooks.py from the source directory, injecting useful Python code
kono
parents:
diff changeset
28 into gdb.
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 You may see a message from gdb of the form:
kono
parents:
diff changeset
31 "path-to-build/gcc/.gdbinit" auto-loading has been declined by your `auto-load safe-path'
kono
parents:
diff changeset
32 as a protection against untrustworthy python scripts. See
kono
parents:
diff changeset
33 http://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 The fix is to mark the paths of the build/gcc directory as trustworthy.
kono
parents:
diff changeset
36 An easy way to do so is by adding the following to your ~/.gdbinit script:
kono
parents:
diff changeset
37 add-auto-load-safe-path /absolute/path/to/build/gcc
kono
parents:
diff changeset
38 for the build directories for your various checkouts of gcc.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 If it's working, you should see the message:
kono
parents:
diff changeset
41 Successfully loaded GDB hooks for GCC
kono
parents:
diff changeset
42 as gdb starts up.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 During development, I've been manually invoking the code in this way, as a
kono
parents:
diff changeset
45 precanned way of printing a variety of different kinds of value:
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 gdb \
kono
parents:
diff changeset
48 -ex "break expand_gimple_stmt" \
kono
parents:
diff changeset
49 -ex "run" \
kono
parents:
diff changeset
50 -ex "bt" \
kono
parents:
diff changeset
51 --args \
kono
parents:
diff changeset
52 ./cc1 foo.c -O3
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 Examples of output using the pretty-printers
kono
parents:
diff changeset
55 --------------------------------------------
kono
parents:
diff changeset
56 Pointer values are generally shown in the form:
kono
parents:
diff changeset
57 <type address extra_info>
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 For example, an opt_pass* might appear as:
kono
parents:
diff changeset
60 (gdb) p pass
kono
parents:
diff changeset
61 $2 = <opt_pass* 0x188b600 "expand"(170)>
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 The name of the pass is given ("expand"), together with the
kono
parents:
diff changeset
64 static_pass_number.
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 Note that you can dereference the pointer in the normal way:
kono
parents:
diff changeset
67 (gdb) p *pass
kono
parents:
diff changeset
68 $4 = {type = RTL_PASS, name = 0x120a312 "expand",
kono
parents:
diff changeset
69 [etc, ...snipped...]
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 and you can suppress pretty-printers using /r (for "raw"):
kono
parents:
diff changeset
72 (gdb) p /r pass
kono
parents:
diff changeset
73 $3 = (opt_pass *) 0x188b600
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 Basic blocks are shown with their index in parentheses, apart from the
kono
parents:
diff changeset
76 CFG's entry and exit blocks, which are given as "ENTRY" and "EXIT":
kono
parents:
diff changeset
77 (gdb) p bb
kono
parents:
diff changeset
78 $9 = <basic_block 0x7ffff041f1a0 (2)>
kono
parents:
diff changeset
79 (gdb) p cfun->cfg->x_entry_block_ptr
kono
parents:
diff changeset
80 $10 = <basic_block 0x7ffff041f0d0 (ENTRY)>
kono
parents:
diff changeset
81 (gdb) p cfun->cfg->x_exit_block_ptr
kono
parents:
diff changeset
82 $11 = <basic_block 0x7ffff041f138 (EXIT)>
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 CFG edges are shown with the src and dest blocks given in parentheses:
kono
parents:
diff changeset
85 (gdb) p e
kono
parents:
diff changeset
86 $1 = <edge 0x7ffff043f118 (ENTRY -> 6)>
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 Tree nodes are printed using Python code that emulates print_node_brief,
kono
parents:
diff changeset
89 running in gdb, rather than in the inferior:
kono
parents:
diff changeset
90 (gdb) p cfun->decl
kono
parents:
diff changeset
91 $1 = <function_decl 0x7ffff0420b00 foo>
kono
parents:
diff changeset
92 For usability, the type is printed first (e.g. "function_decl"), rather
kono
parents:
diff changeset
93 than just "tree".
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 RTL expressions use a kludge: they are pretty-printed by injecting
kono
parents:
diff changeset
96 calls into print-rtl.c into the inferior:
kono
parents:
diff changeset
97 Value returned is $1 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK)
kono
parents:
diff changeset
98 (gdb) p $1
kono
parents:
diff changeset
99 $2 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK)
kono
parents:
diff changeset
100 (gdb) p /r $1
kono
parents:
diff changeset
101 $3 = (rtx_def *) 0x7ffff043e140
kono
parents:
diff changeset
102 This won't work for coredumps, and probably in other circumstances, but
kono
parents:
diff changeset
103 it's a quick way of getting lots of debuggability quickly.
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 Callgraph nodes are printed with the name of the function decl, if
kono
parents:
diff changeset
106 available:
kono
parents:
diff changeset
107 (gdb) frame 5
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
108 #5 0x00000000006c288a in expand_function (node=<cgraph_node* 0x7ffff0312720 "foo"/12345>) at ../../src/gcc/cgraphunit.c:1594
111
kono
parents:
diff changeset
109 1594 execute_pass_list (g->get_passes ()->all_passes);
kono
parents:
diff changeset
110 (gdb) p node
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
111 $1 = <cgraph_node* 0x7ffff0312720 "foo"/12345>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
112
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
113 Similarly for symtab_node and varpool_node classes.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
114
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
115 Cgraph edges are printed with the name of caller and callee:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
116 (gdb) p this->callees
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
117 $4 = <cgraph_edge* 0x7fffe25aa000 (<cgraph_node * 0x7fffe62b22e0 "_GLOBAL__sub_I__ZN5Pooma5pinfoE"/19660> -> <cgraph_node * 0x7fffe620f730 "__static_initialization_and_destruction_1"/19575>)>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
118
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
119 IPA reference follow very similar format:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
120 (gdb) Value returned is $5 = <ipa_ref* 0x7fffefcb80c8 (<symtab_node * 0x7ffff562f000 "__dt_base "/875> -> <symtab_node * 0x7fffe795f000 "_ZTVN6Smarts8RunnableE"/16056>:IPA_REF_ADDR)>
111
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 vec<> pointers are printed as the address followed by the elements in
kono
parents:
diff changeset
123 braces. Here's a length 2 vec:
kono
parents:
diff changeset
124 (gdb) p bb->preds
kono
parents:
diff changeset
125 $18 = 0x7ffff0428b68 = {<edge 0x7ffff044d380 (3 -> 5)>, <edge 0x7ffff044d3b8 (4 -> 5)>}
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 and here's a length 1 vec:
kono
parents:
diff changeset
128 (gdb) p bb->succs
kono
parents:
diff changeset
129 $19 = 0x7ffff0428bb8 = {<edge 0x7ffff044d3f0 (5 -> EXIT)>}
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 You cannot yet use array notation [] to access the elements within the
kono
parents:
diff changeset
132 vector: attempting to do so instead gives you the vec itself (for vec[0]),
kono
parents:
diff changeset
133 or a (probably) invalid cast to vec<> for the memory after the vec (for
kono
parents:
diff changeset
134 vec[1] onwards).
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 Instead (for now) you must access m_vecdata:
kono
parents:
diff changeset
137 (gdb) p bb->preds->m_vecdata[0]
kono
parents:
diff changeset
138 $20 = <edge 0x7ffff044d380 (3 -> 5)>
kono
parents:
diff changeset
139 (gdb) p bb->preds->m_vecdata[1]
kono
parents:
diff changeset
140 $21 = <edge 0x7ffff044d3b8 (4 -> 5)>
kono
parents:
diff changeset
141 """
kono
parents:
diff changeset
142 import os.path
kono
parents:
diff changeset
143 import re
kono
parents:
diff changeset
144 import sys
kono
parents:
diff changeset
145 import tempfile
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 import gdb
kono
parents:
diff changeset
148 import gdb.printing
kono
parents:
diff changeset
149 import gdb.types
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 # Convert "enum tree_code" (tree.def and tree.h) to a dict:
kono
parents:
diff changeset
152 tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code'))
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 # ...and look up specific values for use later:
kono
parents:
diff changeset
155 IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE']
kono
parents:
diff changeset
156 TYPE_DECL = tree_code_dict['TYPE_DECL']
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 # Similarly for "enum tree_code_class" (tree.h):
kono
parents:
diff changeset
159 tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class'))
kono
parents:
diff changeset
160 tcc_type = tree_code_class_dict['tcc_type']
kono
parents:
diff changeset
161 tcc_declaration = tree_code_class_dict['tcc_declaration']
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 # Python3 has int() with arbitrary precision (bignum). Python2 int() is 32-bit
kono
parents:
diff changeset
164 # on 32-bit hosts but remote targets may have 64-bit pointers there; Python2
kono
parents:
diff changeset
165 # long() is always 64-bit but Python3 no longer has anything named long.
kono
parents:
diff changeset
166 def intptr(gdbval):
kono
parents:
diff changeset
167 return long(gdbval) if sys.version_info.major == 2 else int(gdbval)
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 class Tree:
kono
parents:
diff changeset
170 """
kono
parents:
diff changeset
171 Wrapper around a gdb.Value for a tree, with various methods
kono
parents:
diff changeset
172 corresponding to macros in gcc/tree.h
kono
parents:
diff changeset
173 """
kono
parents:
diff changeset
174 def __init__(self, gdbval):
kono
parents:
diff changeset
175 self.gdbval = gdbval
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 def is_nonnull(self):
kono
parents:
diff changeset
178 return intptr(self.gdbval)
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 def TREE_CODE(self):
kono
parents:
diff changeset
181 """
kono
parents:
diff changeset
182 Get gdb.Value corresponding to TREE_CODE (self)
kono
parents:
diff changeset
183 as per:
kono
parents:
diff changeset
184 #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
kono
parents:
diff changeset
185 """
kono
parents:
diff changeset
186 return self.gdbval['base']['code']
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 def DECL_NAME(self):
kono
parents:
diff changeset
189 """
kono
parents:
diff changeset
190 Get Tree instance corresponding to DECL_NAME (self)
kono
parents:
diff changeset
191 """
kono
parents:
diff changeset
192 return Tree(self.gdbval['decl_minimal']['name'])
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 def TYPE_NAME(self):
kono
parents:
diff changeset
195 """
kono
parents:
diff changeset
196 Get Tree instance corresponding to result of TYPE_NAME (self)
kono
parents:
diff changeset
197 """
kono
parents:
diff changeset
198 return Tree(self.gdbval['type_common']['name'])
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 def IDENTIFIER_POINTER(self):
kono
parents:
diff changeset
201 """
kono
parents:
diff changeset
202 Get str correspoinding to result of IDENTIFIER_NODE (self)
kono
parents:
diff changeset
203 """
kono
parents:
diff changeset
204 return self.gdbval['identifier']['id']['str'].string()
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 class TreePrinter:
kono
parents:
diff changeset
207 "Prints a tree"
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 def __init__ (self, gdbval):
kono
parents:
diff changeset
210 self.gdbval = gdbval
kono
parents:
diff changeset
211 self.node = Tree(gdbval)
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 def to_string (self):
kono
parents:
diff changeset
214 # like gcc/print-tree.c:print_node_brief
kono
parents:
diff changeset
215 # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code)
kono
parents:
diff changeset
216 # tree_code_name[(int) TREE_CODE (node)])
kono
parents:
diff changeset
217 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
218 return '<tree 0x0>'
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 val_TREE_CODE = self.node.TREE_CODE()
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 # extern const enum tree_code_class tree_code_type[];
kono
parents:
diff changeset
223 # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
kono
parents:
diff changeset
224
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
225 if val_TREE_CODE == 0xa5a5:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
226 return '<ggc_freed 0x%x>' % intptr(self.gdbval)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
227
111
kono
parents:
diff changeset
228 val_tree_code_type = gdb.parse_and_eval('tree_code_type')
kono
parents:
diff changeset
229 val_tclass = val_tree_code_type[val_TREE_CODE]
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 val_tree_code_name = gdb.parse_and_eval('tree_code_name')
kono
parents:
diff changeset
232 val_code_name = val_tree_code_name[intptr(val_TREE_CODE)]
kono
parents:
diff changeset
233 #print(val_code_name.string())
kono
parents:
diff changeset
234
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
235 try:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
236 result = '<%s 0x%x' % (val_code_name.string(), intptr(self.gdbval))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
237 except:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
238 return '<tree 0x%x>' % intptr(self.gdbval)
111
kono
parents:
diff changeset
239 if intptr(val_tclass) == tcc_declaration:
kono
parents:
diff changeset
240 tree_DECL_NAME = self.node.DECL_NAME()
kono
parents:
diff changeset
241 if tree_DECL_NAME.is_nonnull():
kono
parents:
diff changeset
242 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
kono
parents:
diff changeset
243 else:
kono
parents:
diff changeset
244 pass # TODO: labels etc
kono
parents:
diff changeset
245 elif intptr(val_tclass) == tcc_type:
kono
parents:
diff changeset
246 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
kono
parents:
diff changeset
247 if tree_TYPE_NAME.is_nonnull():
kono
parents:
diff changeset
248 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
kono
parents:
diff changeset
249 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
kono
parents:
diff changeset
250 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
kono
parents:
diff changeset
251 if tree_TYPE_NAME.DECL_NAME().is_nonnull():
kono
parents:
diff changeset
252 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
kono
parents:
diff changeset
253 if self.node.TREE_CODE() == IDENTIFIER_NODE:
kono
parents:
diff changeset
254 result += ' %s' % self.node.IDENTIFIER_POINTER()
kono
parents:
diff changeset
255 # etc
kono
parents:
diff changeset
256 result += '>'
kono
parents:
diff changeset
257 return result
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 ######################################################################
kono
parents:
diff changeset
260 # Callgraph pretty-printers
kono
parents:
diff changeset
261 ######################################################################
kono
parents:
diff changeset
262
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
263 class SymtabNodePrinter:
111
kono
parents:
diff changeset
264 def __init__(self, gdbval):
kono
parents:
diff changeset
265 self.gdbval = gdbval
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 def to_string (self):
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
268 t = str(self.gdbval.type)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
269 result = '<%s 0x%x' % (t, intptr(self.gdbval))
111
kono
parents:
diff changeset
270 if intptr(self.gdbval):
kono
parents:
diff changeset
271 # symtab_node::name calls lang_hooks.decl_printable_name
kono
parents:
diff changeset
272 # default implementation (lhd_decl_printable_name) is:
kono
parents:
diff changeset
273 # return IDENTIFIER_POINTER (DECL_NAME (decl));
kono
parents:
diff changeset
274 tree_decl = Tree(self.gdbval['decl'])
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275 result += ' "%s"/%d' % (tree_decl.DECL_NAME().IDENTIFIER_POINTER(), self.gdbval['order'])
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
276 result += '>'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277 return result
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
278
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
279 class CgraphEdgePrinter:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
280 def __init__(self, gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
281 self.gdbval = gdbval
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
282
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
283 def to_string (self):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
284 result = '<cgraph_edge* 0x%x' % intptr(self.gdbval)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
285 if intptr(self.gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
286 src = SymtabNodePrinter(self.gdbval['caller']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
287 dest = SymtabNodePrinter(self.gdbval['callee']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
288 result += ' (%s -> %s)' % (src, dest)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
289 result += '>'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
290 return result
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
291
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
292 class IpaReferencePrinter:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
293 def __init__(self, gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
294 self.gdbval = gdbval
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
295
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
296 def to_string (self):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
297 result = '<ipa_ref* 0x%x' % intptr(self.gdbval)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
298 if intptr(self.gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
299 src = SymtabNodePrinter(self.gdbval['referring']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
300 dest = SymtabNodePrinter(self.gdbval['referred']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
301 result += ' (%s -> %s:%s)' % (src, dest, str(self.gdbval['use']))
111
kono
parents:
diff changeset
302 result += '>'
kono
parents:
diff changeset
303 return result
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 ######################################################################
kono
parents:
diff changeset
306 # Dwarf DIE pretty-printers
kono
parents:
diff changeset
307 ######################################################################
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 class DWDieRefPrinter:
kono
parents:
diff changeset
310 def __init__(self, gdbval):
kono
parents:
diff changeset
311 self.gdbval = gdbval
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 def to_string (self):
kono
parents:
diff changeset
314 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
315 return '<dw_die_ref 0x0>'
kono
parents:
diff changeset
316 result = '<dw_die_ref 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
317 result += ' %s' % self.gdbval['die_tag']
kono
parents:
diff changeset
318 if intptr(self.gdbval['die_parent']) != 0:
kono
parents:
diff changeset
319 result += ' <parent=0x%x %s>' % (intptr(self.gdbval['die_parent']),
kono
parents:
diff changeset
320 self.gdbval['die_parent']['die_tag'])
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 result += '>'
kono
parents:
diff changeset
323 return result
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 ######################################################################
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 class GimplePrinter:
kono
parents:
diff changeset
328 def __init__(self, gdbval):
kono
parents:
diff changeset
329 self.gdbval = gdbval
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 def to_string (self):
kono
parents:
diff changeset
332 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
333 return '<gimple 0x0>'
kono
parents:
diff changeset
334 val_gimple_code = self.gdbval['code']
kono
parents:
diff changeset
335 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
kono
parents:
diff changeset
336 val_code_name = val_gimple_code_name[intptr(val_gimple_code)]
kono
parents:
diff changeset
337 result = '<%s 0x%x' % (val_code_name.string(),
kono
parents:
diff changeset
338 intptr(self.gdbval))
kono
parents:
diff changeset
339 result += '>'
kono
parents:
diff changeset
340 return result
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 ######################################################################
kono
parents:
diff changeset
343 # CFG pretty-printers
kono
parents:
diff changeset
344 ######################################################################
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 def bb_index_to_str(index):
kono
parents:
diff changeset
347 if index == 0:
kono
parents:
diff changeset
348 return 'ENTRY'
kono
parents:
diff changeset
349 elif index == 1:
kono
parents:
diff changeset
350 return 'EXIT'
kono
parents:
diff changeset
351 else:
kono
parents:
diff changeset
352 return '%i' % index
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 class BasicBlockPrinter:
kono
parents:
diff changeset
355 def __init__(self, gdbval):
kono
parents:
diff changeset
356 self.gdbval = gdbval
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 def to_string (self):
kono
parents:
diff changeset
359 result = '<basic_block 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
360 if intptr(self.gdbval):
kono
parents:
diff changeset
361 result += ' (%s)' % bb_index_to_str(intptr(self.gdbval['index']))
kono
parents:
diff changeset
362 result += '>'
kono
parents:
diff changeset
363 return result
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 class CfgEdgePrinter:
kono
parents:
diff changeset
366 def __init__(self, gdbval):
kono
parents:
diff changeset
367 self.gdbval = gdbval
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 def to_string (self):
kono
parents:
diff changeset
370 result = '<edge 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
371 if intptr(self.gdbval):
kono
parents:
diff changeset
372 src = bb_index_to_str(intptr(self.gdbval['src']['index']))
kono
parents:
diff changeset
373 dest = bb_index_to_str(intptr(self.gdbval['dest']['index']))
kono
parents:
diff changeset
374 result += ' (%s -> %s)' % (src, dest)
kono
parents:
diff changeset
375 result += '>'
kono
parents:
diff changeset
376 return result
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 ######################################################################
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 class Rtx:
kono
parents:
diff changeset
381 def __init__(self, gdbval):
kono
parents:
diff changeset
382 self.gdbval = gdbval
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 def GET_CODE(self):
kono
parents:
diff changeset
385 return self.gdbval['code']
kono
parents:
diff changeset
386
kono
parents:
diff changeset
387 def GET_RTX_LENGTH(code):
kono
parents:
diff changeset
388 val_rtx_length = gdb.parse_and_eval('rtx_length')
kono
parents:
diff changeset
389 return intptr(val_rtx_length[code])
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 def GET_RTX_NAME(code):
kono
parents:
diff changeset
392 val_rtx_name = gdb.parse_and_eval('rtx_name')
kono
parents:
diff changeset
393 return val_rtx_name[code].string()
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 def GET_RTX_FORMAT(code):
kono
parents:
diff changeset
396 val_rtx_format = gdb.parse_and_eval('rtx_format')
kono
parents:
diff changeset
397 return val_rtx_format[code].string()
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 class RtxPrinter:
kono
parents:
diff changeset
400 def __init__(self, gdbval):
kono
parents:
diff changeset
401 self.gdbval = gdbval
kono
parents:
diff changeset
402 self.rtx = Rtx(gdbval)
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 def to_string (self):
kono
parents:
diff changeset
405 """
kono
parents:
diff changeset
406 For now, a cheap kludge: invoke the inferior's print
kono
parents:
diff changeset
407 function to get a string to use the user, and return an empty
kono
parents:
diff changeset
408 string for gdb
kono
parents:
diff changeset
409 """
kono
parents:
diff changeset
410 # We use print_inline_rtx to avoid a trailing newline
kono
parents:
diff changeset
411 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
kono
parents:
diff changeset
412 % intptr(self.gdbval))
kono
parents:
diff changeset
413 return ''
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 # or by hand; based on gcc/print-rtl.c:print_rtx
kono
parents:
diff changeset
416 result = ('<rtx_def 0x%x'
kono
parents:
diff changeset
417 % (intptr(self.gdbval)))
kono
parents:
diff changeset
418 code = self.rtx.GET_CODE()
kono
parents:
diff changeset
419 result += ' (%s' % GET_RTX_NAME(code)
kono
parents:
diff changeset
420 format_ = GET_RTX_FORMAT(code)
kono
parents:
diff changeset
421 for i in range(GET_RTX_LENGTH(code)):
kono
parents:
diff changeset
422 print(format_[i])
kono
parents:
diff changeset
423 result += ')>'
kono
parents:
diff changeset
424 return result
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 ######################################################################
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 class PassPrinter:
kono
parents:
diff changeset
429 def __init__(self, gdbval):
kono
parents:
diff changeset
430 self.gdbval = gdbval
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 def to_string (self):
kono
parents:
diff changeset
433 result = '<opt_pass* 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
434 if intptr(self.gdbval):
kono
parents:
diff changeset
435 result += (' "%s"(%i)'
kono
parents:
diff changeset
436 % (self.gdbval['name'].string(),
kono
parents:
diff changeset
437 intptr(self.gdbval['static_pass_number'])))
kono
parents:
diff changeset
438 result += '>'
kono
parents:
diff changeset
439 return result
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 ######################################################################
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 class VecPrinter:
kono
parents:
diff changeset
444 # -ex "up" -ex "p bb->preds"
kono
parents:
diff changeset
445 def __init__(self, gdbval):
kono
parents:
diff changeset
446 self.gdbval = gdbval
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 def display_hint (self):
kono
parents:
diff changeset
449 return 'array'
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 def to_string (self):
kono
parents:
diff changeset
452 # A trivial implementation; prettyprinting the contents is done
kono
parents:
diff changeset
453 # by gdb calling the "children" method below.
kono
parents:
diff changeset
454 return '0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 def children (self):
kono
parents:
diff changeset
457 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
458 return
kono
parents:
diff changeset
459 m_vecpfx = self.gdbval['m_vecpfx']
kono
parents:
diff changeset
460 m_num = m_vecpfx['m_num']
kono
parents:
diff changeset
461 m_vecdata = self.gdbval['m_vecdata']
kono
parents:
diff changeset
462 for i in range(m_num):
kono
parents:
diff changeset
463 yield ('[%d]' % i, m_vecdata[i])
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 ######################################################################
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 class MachineModePrinter:
kono
parents:
diff changeset
468 def __init__(self, gdbval):
kono
parents:
diff changeset
469 self.gdbval = gdbval
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 def to_string (self):
kono
parents:
diff changeset
472 name = str(self.gdbval['m_mode'])
kono
parents:
diff changeset
473 return name[2:] if name.startswith('E_') else name
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 ######################################################################
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 class OptMachineModePrinter:
kono
parents:
diff changeset
478 def __init__(self, gdbval):
kono
parents:
diff changeset
479 self.gdbval = gdbval
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 def to_string (self):
kono
parents:
diff changeset
482 name = str(self.gdbval['m_mode'])
kono
parents:
diff changeset
483 if name == 'E_VOIDmode':
kono
parents:
diff changeset
484 return '<None>'
kono
parents:
diff changeset
485 return name[2:] if name.startswith('E_') else name
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 ######################################################################
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 # TODO:
kono
parents:
diff changeset
490 # * hashtab
kono
parents:
diff changeset
491 # * location_t
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
kono
parents:
diff changeset
494 def __init__(self, name, class_):
kono
parents:
diff changeset
495 super(GdbSubprinter, self).__init__(name)
kono
parents:
diff changeset
496 self.class_ = class_
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 def handles_type(self, str_type):
kono
parents:
diff changeset
499 raise NotImplementedError
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 class GdbSubprinterTypeList(GdbSubprinter):
kono
parents:
diff changeset
502 """
kono
parents:
diff changeset
503 A GdbSubprinter that handles a specific set of types
kono
parents:
diff changeset
504 """
kono
parents:
diff changeset
505 def __init__(self, str_types, name, class_):
kono
parents:
diff changeset
506 super(GdbSubprinterTypeList, self).__init__(name, class_)
kono
parents:
diff changeset
507 self.str_types = frozenset(str_types)
kono
parents:
diff changeset
508
kono
parents:
diff changeset
509 def handles_type(self, str_type):
kono
parents:
diff changeset
510 return str_type in self.str_types
kono
parents:
diff changeset
511
kono
parents:
diff changeset
512 class GdbSubprinterRegex(GdbSubprinter):
kono
parents:
diff changeset
513 """
kono
parents:
diff changeset
514 A GdbSubprinter that handles types that match a regex
kono
parents:
diff changeset
515 """
kono
parents:
diff changeset
516 def __init__(self, regex, name, class_):
kono
parents:
diff changeset
517 super(GdbSubprinterRegex, self).__init__(name, class_)
kono
parents:
diff changeset
518 self.regex = re.compile(regex)
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 def handles_type(self, str_type):
kono
parents:
diff changeset
521 return self.regex.match(str_type)
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
kono
parents:
diff changeset
524 def __init__(self, name):
kono
parents:
diff changeset
525 super(GdbPrettyPrinters, self).__init__(name, [])
kono
parents:
diff changeset
526
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
527 def add_printer_for_types(self, types, name, class_):
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
528 self.subprinters.append(GdbSubprinterTypeList(types, name, class_))
111
kono
parents:
diff changeset
529
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
530 def add_printer_for_regex(self, regex, name, class_):
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
531 self.subprinters.append(GdbSubprinterRegex(regex, name, class_))
111
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 def __call__(self, gdbval):
kono
parents:
diff changeset
534 type_ = gdbval.type.unqualified()
kono
parents:
diff changeset
535 str_type = str(type_)
kono
parents:
diff changeset
536 for printer in self.subprinters:
kono
parents:
diff changeset
537 if printer.enabled and printer.handles_type(str_type):
kono
parents:
diff changeset
538 return printer.class_(gdbval)
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 # Couldn't find a pretty printer (or it was disabled):
kono
parents:
diff changeset
541 return None
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 def build_pretty_printer():
kono
parents:
diff changeset
545 pp = GdbPrettyPrinters('gcc')
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
546 pp.add_printer_for_types(['tree', 'const_tree'],
111
kono
parents:
diff changeset
547 'tree', TreePrinter)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
548 pp.add_printer_for_types(['cgraph_node *', 'varpool_node *', 'symtab_node *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
549 'symtab_node', SymtabNodePrinter)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
550 pp.add_printer_for_types(['cgraph_edge *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
551 'cgraph_edge', CgraphEdgePrinter)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
552 pp.add_printer_for_types(['ipa_ref *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
553 'ipa_ref', IpaReferencePrinter)
111
kono
parents:
diff changeset
554 pp.add_printer_for_types(['dw_die_ref'],
kono
parents:
diff changeset
555 'dw_die_ref', DWDieRefPrinter)
kono
parents:
diff changeset
556 pp.add_printer_for_types(['gimple', 'gimple *',
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 # Keep this in the same order as gimple.def:
kono
parents:
diff changeset
559 'gimple_cond', 'const_gimple_cond',
kono
parents:
diff changeset
560 'gimple_statement_cond *',
kono
parents:
diff changeset
561 'gimple_debug', 'const_gimple_debug',
kono
parents:
diff changeset
562 'gimple_statement_debug *',
kono
parents:
diff changeset
563 'gimple_label', 'const_gimple_label',
kono
parents:
diff changeset
564 'gimple_statement_label *',
kono
parents:
diff changeset
565 'gimple_switch', 'const_gimple_switch',
kono
parents:
diff changeset
566 'gimple_statement_switch *',
kono
parents:
diff changeset
567 'gimple_assign', 'const_gimple_assign',
kono
parents:
diff changeset
568 'gimple_statement_assign *',
kono
parents:
diff changeset
569 'gimple_bind', 'const_gimple_bind',
kono
parents:
diff changeset
570 'gimple_statement_bind *',
kono
parents:
diff changeset
571 'gimple_phi', 'const_gimple_phi',
kono
parents:
diff changeset
572 'gimple_statement_phi *'],
kono
parents:
diff changeset
573
kono
parents:
diff changeset
574 'gimple',
kono
parents:
diff changeset
575 GimplePrinter)
kono
parents:
diff changeset
576 pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
kono
parents:
diff changeset
577 'basic_block',
kono
parents:
diff changeset
578 BasicBlockPrinter)
kono
parents:
diff changeset
579 pp.add_printer_for_types(['edge', 'edge_def *'],
kono
parents:
diff changeset
580 'edge',
kono
parents:
diff changeset
581 CfgEdgePrinter)
kono
parents:
diff changeset
582 pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
kono
parents:
diff changeset
583 pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*',
kono
parents:
diff changeset
586 'vec',
kono
parents:
diff changeset
587 VecPrinter)
kono
parents:
diff changeset
588
kono
parents:
diff changeset
589 pp.add_printer_for_regex(r'opt_mode<(\S+)>',
kono
parents:
diff changeset
590 'opt_mode', OptMachineModePrinter)
kono
parents:
diff changeset
591 pp.add_printer_for_types(['opt_scalar_int_mode',
kono
parents:
diff changeset
592 'opt_scalar_float_mode',
kono
parents:
diff changeset
593 'opt_scalar_mode'],
kono
parents:
diff changeset
594 'opt_mode', OptMachineModePrinter)
kono
parents:
diff changeset
595 pp.add_printer_for_regex(r'pod_mode<(\S+)>',
kono
parents:
diff changeset
596 'pod_mode', MachineModePrinter)
kono
parents:
diff changeset
597 pp.add_printer_for_types(['scalar_int_mode_pod',
kono
parents:
diff changeset
598 'scalar_mode_pod'],
kono
parents:
diff changeset
599 'pod_mode', MachineModePrinter)
kono
parents:
diff changeset
600 for mode in ('scalar_mode', 'scalar_int_mode', 'scalar_float_mode',
kono
parents:
diff changeset
601 'complex_mode'):
kono
parents:
diff changeset
602 pp.add_printer_for_types([mode], mode, MachineModePrinter)
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 return pp
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 gdb.printing.register_pretty_printer(
kono
parents:
diff changeset
607 gdb.current_objfile(),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
608 build_pretty_printer(),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
609 replace=True)
111
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 def find_gcc_source_dir():
kono
parents:
diff changeset
612 # Use location of global "g" to locate the source tree
kono
parents:
diff changeset
613 sym_g = gdb.lookup_global_symbol('g')
kono
parents:
diff changeset
614 path = sym_g.symtab.filename # e.g. '../../src/gcc/context.h'
kono
parents:
diff changeset
615 srcdir = os.path.split(path)[0] # e.g. '../../src/gcc'
kono
parents:
diff changeset
616 return srcdir
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 class PassNames:
kono
parents:
diff changeset
619 """Parse passes.def, gathering a list of pass class names"""
kono
parents:
diff changeset
620 def __init__(self):
kono
parents:
diff changeset
621 srcdir = find_gcc_source_dir()
kono
parents:
diff changeset
622 self.names = []
kono
parents:
diff changeset
623 with open(os.path.join(srcdir, 'passes.def')) as f:
kono
parents:
diff changeset
624 for line in f:
kono
parents:
diff changeset
625 m = re.match('\s*NEXT_PASS \(([^,]+).*\);', line)
kono
parents:
diff changeset
626 if m:
kono
parents:
diff changeset
627 self.names.append(m.group(1))
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 class BreakOnPass(gdb.Command):
kono
parents:
diff changeset
630 """
kono
parents:
diff changeset
631 A custom command for putting breakpoints on the execute hook of passes.
kono
parents:
diff changeset
632 This is largely a workaround for issues with tab-completion in gdb when
kono
parents:
diff changeset
633 setting breakpoints on methods on classes within anonymous namespaces.
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 Example of use: putting a breakpoint on "final"
kono
parents:
diff changeset
636 (gdb) break-on-pass
kono
parents:
diff changeset
637 Press <TAB>; it autocompletes to "pass_":
kono
parents:
diff changeset
638 (gdb) break-on-pass pass_
kono
parents:
diff changeset
639 Press <TAB>:
kono
parents:
diff changeset
640 Display all 219 possibilities? (y or n)
kono
parents:
diff changeset
641 Press "n"; then type "f":
kono
parents:
diff changeset
642 (gdb) break-on-pass pass_f
kono
parents:
diff changeset
643 Press <TAB> to autocomplete to pass classnames beginning with "pass_f":
kono
parents:
diff changeset
644 pass_fast_rtl_dce pass_fold_builtins
kono
parents:
diff changeset
645 pass_feedback_split_functions pass_forwprop
kono
parents:
diff changeset
646 pass_final pass_fre
kono
parents:
diff changeset
647 pass_fixup_cfg pass_free_cfg
kono
parents:
diff changeset
648 Type "in<TAB>" to complete to "pass_final":
kono
parents:
diff changeset
649 (gdb) break-on-pass pass_final
kono
parents:
diff changeset
650 ...and hit <RETURN>:
kono
parents:
diff changeset
651 Breakpoint 6 at 0x8396ba: file ../../src/gcc/final.c, line 4526.
kono
parents:
diff changeset
652 ...and we have a breakpoint set; continue execution:
kono
parents:
diff changeset
653 (gdb) cont
kono
parents:
diff changeset
654 Continuing.
kono
parents:
diff changeset
655 Breakpoint 6, (anonymous namespace)::pass_final::execute (this=0x17fb990) at ../../src/gcc/final.c:4526
kono
parents:
diff changeset
656 4526 virtual unsigned int execute (function *) { return rest_of_handle_final (); }
kono
parents:
diff changeset
657 """
kono
parents:
diff changeset
658 def __init__(self):
kono
parents:
diff changeset
659 gdb.Command.__init__(self, 'break-on-pass', gdb.COMMAND_BREAKPOINTS)
kono
parents:
diff changeset
660 self.pass_names = None
kono
parents:
diff changeset
661
kono
parents:
diff changeset
662 def complete(self, text, word):
kono
parents:
diff changeset
663 # Lazily load pass names:
kono
parents:
diff changeset
664 if not self.pass_names:
kono
parents:
diff changeset
665 self.pass_names = PassNames()
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 return [name
kono
parents:
diff changeset
668 for name in sorted(self.pass_names.names)
kono
parents:
diff changeset
669 if name.startswith(text)]
kono
parents:
diff changeset
670
kono
parents:
diff changeset
671 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
672 sym = '(anonymous namespace)::%s::execute' % arg
kono
parents:
diff changeset
673 breakpoint = gdb.Breakpoint(sym)
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 BreakOnPass()
kono
parents:
diff changeset
676
kono
parents:
diff changeset
677 class DumpFn(gdb.Command):
kono
parents:
diff changeset
678 """
kono
parents:
diff changeset
679 A custom command to dump a gimple/rtl function to file. By default, it
kono
parents:
diff changeset
680 dumps the current function using 0 as dump_flags, but the function and flags
kono
parents:
diff changeset
681 can also be specified. If /f <file> are passed as the first two arguments,
kono
parents:
diff changeset
682 the dump is written to that file. Otherwise, a temporary file is created
kono
parents:
diff changeset
683 and opened in the text editor specified in the EDITOR environment variable.
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 Examples of use:
kono
parents:
diff changeset
686 (gdb) dump-fn
kono
parents:
diff changeset
687 (gdb) dump-fn /f foo.1.txt
kono
parents:
diff changeset
688 (gdb) dump-fn cfun->decl
kono
parents:
diff changeset
689 (gdb) dump-fn /f foo.1.txt cfun->decl
kono
parents:
diff changeset
690 (gdb) dump-fn cfun->decl 0
kono
parents:
diff changeset
691 (gdb) dump-fn cfun->decl dump_flags
kono
parents:
diff changeset
692 """
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 def __init__(self):
kono
parents:
diff changeset
695 gdb.Command.__init__(self, 'dump-fn', gdb.COMMAND_USER)
kono
parents:
diff changeset
696
kono
parents:
diff changeset
697 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
698 # Parse args, check number of args
kono
parents:
diff changeset
699 args = gdb.string_to_argv(arg)
kono
parents:
diff changeset
700 if len(args) >= 1 and args[0] == "/f":
kono
parents:
diff changeset
701 if len(args) == 1:
kono
parents:
diff changeset
702 print ("Missing file argument")
kono
parents:
diff changeset
703 return
kono
parents:
diff changeset
704 filename = args[1]
kono
parents:
diff changeset
705 editor_mode = False
kono
parents:
diff changeset
706 base_arg = 2
kono
parents:
diff changeset
707 else:
kono
parents:
diff changeset
708 editor = os.getenv("EDITOR", "")
kono
parents:
diff changeset
709 if editor == "":
kono
parents:
diff changeset
710 print ("EDITOR environment variable not defined")
kono
parents:
diff changeset
711 return
kono
parents:
diff changeset
712 editor_mode = True
kono
parents:
diff changeset
713 base_arg = 0
kono
parents:
diff changeset
714 if len(args) - base_arg > 2:
kono
parents:
diff changeset
715 print ("Too many arguments")
kono
parents:
diff changeset
716 return
kono
parents:
diff changeset
717
kono
parents:
diff changeset
718 # Set func
kono
parents:
diff changeset
719 if len(args) - base_arg >= 1:
kono
parents:
diff changeset
720 funcname = args[base_arg]
kono
parents:
diff changeset
721 printfuncname = "function %s" % funcname
kono
parents:
diff changeset
722 else:
kono
parents:
diff changeset
723 funcname = "cfun ? cfun->decl : current_function_decl"
kono
parents:
diff changeset
724 printfuncname = "current function"
kono
parents:
diff changeset
725 func = gdb.parse_and_eval(funcname)
kono
parents:
diff changeset
726 if func == 0:
kono
parents:
diff changeset
727 print ("Could not find %s" % printfuncname)
kono
parents:
diff changeset
728 return
kono
parents:
diff changeset
729 func = "(tree)%u" % func
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 # Set flags
kono
parents:
diff changeset
732 if len(args) - base_arg >= 2:
kono
parents:
diff changeset
733 flags = gdb.parse_and_eval(args[base_arg + 1])
kono
parents:
diff changeset
734 else:
kono
parents:
diff changeset
735 flags = 0
kono
parents:
diff changeset
736
kono
parents:
diff changeset
737 # Get tempory file, if necessary
kono
parents:
diff changeset
738 if editor_mode:
kono
parents:
diff changeset
739 f = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
kono
parents:
diff changeset
740 filename = f.name
kono
parents:
diff changeset
741 f.close()
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 # Open file
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
744 fp = gdb.parse_and_eval("(FILE *) fopen (\"%s\", \"w\")" % filename)
111
kono
parents:
diff changeset
745 if fp == 0:
kono
parents:
diff changeset
746 print ("Could not open file: %s" % filename)
kono
parents:
diff changeset
747 return
kono
parents:
diff changeset
748
kono
parents:
diff changeset
749 # Dump function to file
kono
parents:
diff changeset
750 _ = gdb.parse_and_eval("dump_function_to_file (%s, %s, %u)" %
kono
parents:
diff changeset
751 (func, fp, flags))
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 # Close file
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
754 ret = gdb.parse_and_eval("(int) fclose (%s)" % fp)
111
kono
parents:
diff changeset
755 if ret != 0:
kono
parents:
diff changeset
756 print ("Could not close file: %s" % filename)
kono
parents:
diff changeset
757 return
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 # Open file in editor, if necessary
kono
parents:
diff changeset
760 if editor_mode:
kono
parents:
diff changeset
761 os.system("( %s \"%s\"; rm \"%s\" ) &" %
kono
parents:
diff changeset
762 (editor, filename, filename))
kono
parents:
diff changeset
763
kono
parents:
diff changeset
764 DumpFn()
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 class DotFn(gdb.Command):
kono
parents:
diff changeset
767 """
kono
parents:
diff changeset
768 A custom command to show a gimple/rtl function control flow graph.
kono
parents:
diff changeset
769 By default, it show the current function, but the function can also be
kono
parents:
diff changeset
770 specified.
kono
parents:
diff changeset
771
kono
parents:
diff changeset
772 Examples of use:
kono
parents:
diff changeset
773 (gdb) dot-fn
kono
parents:
diff changeset
774 (gdb) dot-fn cfun
kono
parents:
diff changeset
775 (gdb) dot-fn cfun 0
kono
parents:
diff changeset
776 (gdb) dot-fn cfun dump_flags
kono
parents:
diff changeset
777 """
kono
parents:
diff changeset
778 def __init__(self):
kono
parents:
diff changeset
779 gdb.Command.__init__(self, 'dot-fn', gdb.COMMAND_USER)
kono
parents:
diff changeset
780
kono
parents:
diff changeset
781 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
782 # Parse args, check number of args
kono
parents:
diff changeset
783 args = gdb.string_to_argv(arg)
kono
parents:
diff changeset
784 if len(args) > 2:
kono
parents:
diff changeset
785 print("Too many arguments")
kono
parents:
diff changeset
786 return
kono
parents:
diff changeset
787
kono
parents:
diff changeset
788 # Set func
kono
parents:
diff changeset
789 if len(args) >= 1:
kono
parents:
diff changeset
790 funcname = args[0]
kono
parents:
diff changeset
791 printfuncname = "function %s" % funcname
kono
parents:
diff changeset
792 else:
kono
parents:
diff changeset
793 funcname = "cfun"
kono
parents:
diff changeset
794 printfuncname = "current function"
kono
parents:
diff changeset
795 func = gdb.parse_and_eval(funcname)
kono
parents:
diff changeset
796 if func == 0:
kono
parents:
diff changeset
797 print("Could not find %s" % printfuncname)
kono
parents:
diff changeset
798 return
kono
parents:
diff changeset
799 func = "(struct function *)%s" % func
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 # Set flags
kono
parents:
diff changeset
802 if len(args) >= 2:
kono
parents:
diff changeset
803 flags = gdb.parse_and_eval(args[1])
kono
parents:
diff changeset
804 else:
kono
parents:
diff changeset
805 flags = 0
kono
parents:
diff changeset
806
kono
parents:
diff changeset
807 # Get temp file
kono
parents:
diff changeset
808 f = tempfile.NamedTemporaryFile(delete=False)
kono
parents:
diff changeset
809 filename = f.name
kono
parents:
diff changeset
810
kono
parents:
diff changeset
811 # Close and reopen temp file to get C FILE*
kono
parents:
diff changeset
812 f.close()
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
813 fp = gdb.parse_and_eval("(FILE *) fopen (\"%s\", \"w\")" % filename)
111
kono
parents:
diff changeset
814 if fp == 0:
kono
parents:
diff changeset
815 print("Cannot open temp file")
kono
parents:
diff changeset
816 return
kono
parents:
diff changeset
817
kono
parents:
diff changeset
818 # Write graph to temp file
kono
parents:
diff changeset
819 _ = gdb.parse_and_eval("start_graph_dump (%s, \"<debug>\")" % fp)
kono
parents:
diff changeset
820 _ = gdb.parse_and_eval("print_graph_cfg (%s, %s, %u)"
kono
parents:
diff changeset
821 % (fp, func, flags))
kono
parents:
diff changeset
822 _ = gdb.parse_and_eval("end_graph_dump (%s)" % fp)
kono
parents:
diff changeset
823
kono
parents:
diff changeset
824 # Close temp file
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
825 ret = gdb.parse_and_eval("(int) fclose (%s)" % fp)
111
kono
parents:
diff changeset
826 if ret != 0:
kono
parents:
diff changeset
827 print("Could not close temp file: %s" % filename)
kono
parents:
diff changeset
828 return
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 # Show graph in temp file
kono
parents:
diff changeset
831 os.system("( dot -Tx11 \"%s\"; rm \"%s\" ) &" % (filename, filename))
kono
parents:
diff changeset
832
kono
parents:
diff changeset
833 DotFn()
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 print('Successfully loaded GDB hooks for GCC')