annotate gcc/gdbhooks.py @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 84e7813d76e9
children 1830386684a0
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
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 # Copyright (C) 2013-2018 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
kono
parents:
diff changeset
225 val_tree_code_type = gdb.parse_and_eval('tree_code_type')
kono
parents:
diff changeset
226 val_tclass = val_tree_code_type[val_TREE_CODE]
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 val_tree_code_name = gdb.parse_and_eval('tree_code_name')
kono
parents:
diff changeset
229 val_code_name = val_tree_code_name[intptr(val_TREE_CODE)]
kono
parents:
diff changeset
230 #print(val_code_name.string())
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 result = '<%s 0x%x' % (val_code_name.string(), intptr(self.gdbval))
kono
parents:
diff changeset
233 if intptr(val_tclass) == tcc_declaration:
kono
parents:
diff changeset
234 tree_DECL_NAME = self.node.DECL_NAME()
kono
parents:
diff changeset
235 if tree_DECL_NAME.is_nonnull():
kono
parents:
diff changeset
236 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER()
kono
parents:
diff changeset
237 else:
kono
parents:
diff changeset
238 pass # TODO: labels etc
kono
parents:
diff changeset
239 elif intptr(val_tclass) == tcc_type:
kono
parents:
diff changeset
240 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name'])
kono
parents:
diff changeset
241 if tree_TYPE_NAME.is_nonnull():
kono
parents:
diff changeset
242 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE:
kono
parents:
diff changeset
243 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER()
kono
parents:
diff changeset
244 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL:
kono
parents:
diff changeset
245 if tree_TYPE_NAME.DECL_NAME().is_nonnull():
kono
parents:
diff changeset
246 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER()
kono
parents:
diff changeset
247 if self.node.TREE_CODE() == IDENTIFIER_NODE:
kono
parents:
diff changeset
248 result += ' %s' % self.node.IDENTIFIER_POINTER()
kono
parents:
diff changeset
249 # etc
kono
parents:
diff changeset
250 result += '>'
kono
parents:
diff changeset
251 return result
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 ######################################################################
kono
parents:
diff changeset
254 # Callgraph pretty-printers
kono
parents:
diff changeset
255 ######################################################################
kono
parents:
diff changeset
256
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
257 class SymtabNodePrinter:
111
kono
parents:
diff changeset
258 def __init__(self, gdbval):
kono
parents:
diff changeset
259 self.gdbval = gdbval
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 def to_string (self):
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
262 t = str(self.gdbval.type)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
263 result = '<%s 0x%x' % (t, intptr(self.gdbval))
111
kono
parents:
diff changeset
264 if intptr(self.gdbval):
kono
parents:
diff changeset
265 # symtab_node::name calls lang_hooks.decl_printable_name
kono
parents:
diff changeset
266 # default implementation (lhd_decl_printable_name) is:
kono
parents:
diff changeset
267 # return IDENTIFIER_POINTER (DECL_NAME (decl));
kono
parents:
diff changeset
268 tree_decl = Tree(self.gdbval['decl'])
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
269 result += ' "%s"/%d' % (tree_decl.DECL_NAME().IDENTIFIER_POINTER(), self.gdbval['order'])
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
270 result += '>'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
271 return result
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
272
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
273 class CgraphEdgePrinter:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
274 def __init__(self, gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275 self.gdbval = gdbval
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
276
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277 def to_string (self):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
278 result = '<cgraph_edge* 0x%x' % intptr(self.gdbval)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
279 if intptr(self.gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
280 src = SymtabNodePrinter(self.gdbval['caller']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
281 dest = SymtabNodePrinter(self.gdbval['callee']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
282 result += ' (%s -> %s)' % (src, dest)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
283 result += '>'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
284 return result
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
285
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
286 class IpaReferencePrinter:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
287 def __init__(self, gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
288 self.gdbval = gdbval
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
289
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
290 def to_string (self):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
291 result = '<ipa_ref* 0x%x' % intptr(self.gdbval)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
292 if intptr(self.gdbval):
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
293 src = SymtabNodePrinter(self.gdbval['referring']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
294 dest = SymtabNodePrinter(self.gdbval['referred']).to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
295 result += ' (%s -> %s:%s)' % (src, dest, str(self.gdbval['use']))
111
kono
parents:
diff changeset
296 result += '>'
kono
parents:
diff changeset
297 return result
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 ######################################################################
kono
parents:
diff changeset
300 # Dwarf DIE pretty-printers
kono
parents:
diff changeset
301 ######################################################################
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 class DWDieRefPrinter:
kono
parents:
diff changeset
304 def __init__(self, gdbval):
kono
parents:
diff changeset
305 self.gdbval = gdbval
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 def to_string (self):
kono
parents:
diff changeset
308 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
309 return '<dw_die_ref 0x0>'
kono
parents:
diff changeset
310 result = '<dw_die_ref 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
311 result += ' %s' % self.gdbval['die_tag']
kono
parents:
diff changeset
312 if intptr(self.gdbval['die_parent']) != 0:
kono
parents:
diff changeset
313 result += ' <parent=0x%x %s>' % (intptr(self.gdbval['die_parent']),
kono
parents:
diff changeset
314 self.gdbval['die_parent']['die_tag'])
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 result += '>'
kono
parents:
diff changeset
317 return result
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 ######################################################################
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 class GimplePrinter:
kono
parents:
diff changeset
322 def __init__(self, gdbval):
kono
parents:
diff changeset
323 self.gdbval = gdbval
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 def to_string (self):
kono
parents:
diff changeset
326 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
327 return '<gimple 0x0>'
kono
parents:
diff changeset
328 val_gimple_code = self.gdbval['code']
kono
parents:
diff changeset
329 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name')
kono
parents:
diff changeset
330 val_code_name = val_gimple_code_name[intptr(val_gimple_code)]
kono
parents:
diff changeset
331 result = '<%s 0x%x' % (val_code_name.string(),
kono
parents:
diff changeset
332 intptr(self.gdbval))
kono
parents:
diff changeset
333 result += '>'
kono
parents:
diff changeset
334 return result
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 ######################################################################
kono
parents:
diff changeset
337 # CFG pretty-printers
kono
parents:
diff changeset
338 ######################################################################
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 def bb_index_to_str(index):
kono
parents:
diff changeset
341 if index == 0:
kono
parents:
diff changeset
342 return 'ENTRY'
kono
parents:
diff changeset
343 elif index == 1:
kono
parents:
diff changeset
344 return 'EXIT'
kono
parents:
diff changeset
345 else:
kono
parents:
diff changeset
346 return '%i' % index
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 class BasicBlockPrinter:
kono
parents:
diff changeset
349 def __init__(self, gdbval):
kono
parents:
diff changeset
350 self.gdbval = gdbval
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 def to_string (self):
kono
parents:
diff changeset
353 result = '<basic_block 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
354 if intptr(self.gdbval):
kono
parents:
diff changeset
355 result += ' (%s)' % bb_index_to_str(intptr(self.gdbval['index']))
kono
parents:
diff changeset
356 result += '>'
kono
parents:
diff changeset
357 return result
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 class CfgEdgePrinter:
kono
parents:
diff changeset
360 def __init__(self, gdbval):
kono
parents:
diff changeset
361 self.gdbval = gdbval
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 def to_string (self):
kono
parents:
diff changeset
364 result = '<edge 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
365 if intptr(self.gdbval):
kono
parents:
diff changeset
366 src = bb_index_to_str(intptr(self.gdbval['src']['index']))
kono
parents:
diff changeset
367 dest = bb_index_to_str(intptr(self.gdbval['dest']['index']))
kono
parents:
diff changeset
368 result += ' (%s -> %s)' % (src, dest)
kono
parents:
diff changeset
369 result += '>'
kono
parents:
diff changeset
370 return result
kono
parents:
diff changeset
371
kono
parents:
diff changeset
372 ######################################################################
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 class Rtx:
kono
parents:
diff changeset
375 def __init__(self, gdbval):
kono
parents:
diff changeset
376 self.gdbval = gdbval
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 def GET_CODE(self):
kono
parents:
diff changeset
379 return self.gdbval['code']
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 def GET_RTX_LENGTH(code):
kono
parents:
diff changeset
382 val_rtx_length = gdb.parse_and_eval('rtx_length')
kono
parents:
diff changeset
383 return intptr(val_rtx_length[code])
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 def GET_RTX_NAME(code):
kono
parents:
diff changeset
386 val_rtx_name = gdb.parse_and_eval('rtx_name')
kono
parents:
diff changeset
387 return val_rtx_name[code].string()
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 def GET_RTX_FORMAT(code):
kono
parents:
diff changeset
390 val_rtx_format = gdb.parse_and_eval('rtx_format')
kono
parents:
diff changeset
391 return val_rtx_format[code].string()
kono
parents:
diff changeset
392
kono
parents:
diff changeset
393 class RtxPrinter:
kono
parents:
diff changeset
394 def __init__(self, gdbval):
kono
parents:
diff changeset
395 self.gdbval = gdbval
kono
parents:
diff changeset
396 self.rtx = Rtx(gdbval)
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 def to_string (self):
kono
parents:
diff changeset
399 """
kono
parents:
diff changeset
400 For now, a cheap kludge: invoke the inferior's print
kono
parents:
diff changeset
401 function to get a string to use the user, and return an empty
kono
parents:
diff changeset
402 string for gdb
kono
parents:
diff changeset
403 """
kono
parents:
diff changeset
404 # We use print_inline_rtx to avoid a trailing newline
kono
parents:
diff changeset
405 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)'
kono
parents:
diff changeset
406 % intptr(self.gdbval))
kono
parents:
diff changeset
407 return ''
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 # or by hand; based on gcc/print-rtl.c:print_rtx
kono
parents:
diff changeset
410 result = ('<rtx_def 0x%x'
kono
parents:
diff changeset
411 % (intptr(self.gdbval)))
kono
parents:
diff changeset
412 code = self.rtx.GET_CODE()
kono
parents:
diff changeset
413 result += ' (%s' % GET_RTX_NAME(code)
kono
parents:
diff changeset
414 format_ = GET_RTX_FORMAT(code)
kono
parents:
diff changeset
415 for i in range(GET_RTX_LENGTH(code)):
kono
parents:
diff changeset
416 print(format_[i])
kono
parents:
diff changeset
417 result += ')>'
kono
parents:
diff changeset
418 return result
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 ######################################################################
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 class PassPrinter:
kono
parents:
diff changeset
423 def __init__(self, gdbval):
kono
parents:
diff changeset
424 self.gdbval = gdbval
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 def to_string (self):
kono
parents:
diff changeset
427 result = '<opt_pass* 0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
428 if intptr(self.gdbval):
kono
parents:
diff changeset
429 result += (' "%s"(%i)'
kono
parents:
diff changeset
430 % (self.gdbval['name'].string(),
kono
parents:
diff changeset
431 intptr(self.gdbval['static_pass_number'])))
kono
parents:
diff changeset
432 result += '>'
kono
parents:
diff changeset
433 return result
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 ######################################################################
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 class VecPrinter:
kono
parents:
diff changeset
438 # -ex "up" -ex "p bb->preds"
kono
parents:
diff changeset
439 def __init__(self, gdbval):
kono
parents:
diff changeset
440 self.gdbval = gdbval
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 def display_hint (self):
kono
parents:
diff changeset
443 return 'array'
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 def to_string (self):
kono
parents:
diff changeset
446 # A trivial implementation; prettyprinting the contents is done
kono
parents:
diff changeset
447 # by gdb calling the "children" method below.
kono
parents:
diff changeset
448 return '0x%x' % intptr(self.gdbval)
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 def children (self):
kono
parents:
diff changeset
451 if intptr(self.gdbval) == 0:
kono
parents:
diff changeset
452 return
kono
parents:
diff changeset
453 m_vecpfx = self.gdbval['m_vecpfx']
kono
parents:
diff changeset
454 m_num = m_vecpfx['m_num']
kono
parents:
diff changeset
455 m_vecdata = self.gdbval['m_vecdata']
kono
parents:
diff changeset
456 for i in range(m_num):
kono
parents:
diff changeset
457 yield ('[%d]' % i, m_vecdata[i])
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 ######################################################################
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 class MachineModePrinter:
kono
parents:
diff changeset
462 def __init__(self, gdbval):
kono
parents:
diff changeset
463 self.gdbval = gdbval
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 def to_string (self):
kono
parents:
diff changeset
466 name = str(self.gdbval['m_mode'])
kono
parents:
diff changeset
467 return name[2:] if name.startswith('E_') else name
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 ######################################################################
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 class OptMachineModePrinter:
kono
parents:
diff changeset
472 def __init__(self, gdbval):
kono
parents:
diff changeset
473 self.gdbval = gdbval
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 def to_string (self):
kono
parents:
diff changeset
476 name = str(self.gdbval['m_mode'])
kono
parents:
diff changeset
477 if name == 'E_VOIDmode':
kono
parents:
diff changeset
478 return '<None>'
kono
parents:
diff changeset
479 return name[2:] if name.startswith('E_') else name
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 ######################################################################
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 # TODO:
kono
parents:
diff changeset
484 # * hashtab
kono
parents:
diff changeset
485 # * location_t
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
kono
parents:
diff changeset
488 def __init__(self, name, class_):
kono
parents:
diff changeset
489 super(GdbSubprinter, self).__init__(name)
kono
parents:
diff changeset
490 self.class_ = class_
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 def handles_type(self, str_type):
kono
parents:
diff changeset
493 raise NotImplementedError
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 class GdbSubprinterTypeList(GdbSubprinter):
kono
parents:
diff changeset
496 """
kono
parents:
diff changeset
497 A GdbSubprinter that handles a specific set of types
kono
parents:
diff changeset
498 """
kono
parents:
diff changeset
499 def __init__(self, str_types, name, class_):
kono
parents:
diff changeset
500 super(GdbSubprinterTypeList, self).__init__(name, class_)
kono
parents:
diff changeset
501 self.str_types = frozenset(str_types)
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 def handles_type(self, str_type):
kono
parents:
diff changeset
504 return str_type in self.str_types
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 class GdbSubprinterRegex(GdbSubprinter):
kono
parents:
diff changeset
507 """
kono
parents:
diff changeset
508 A GdbSubprinter that handles types that match a regex
kono
parents:
diff changeset
509 """
kono
parents:
diff changeset
510 def __init__(self, regex, name, class_):
kono
parents:
diff changeset
511 super(GdbSubprinterRegex, self).__init__(name, class_)
kono
parents:
diff changeset
512 self.regex = re.compile(regex)
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 def handles_type(self, str_type):
kono
parents:
diff changeset
515 return self.regex.match(str_type)
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
kono
parents:
diff changeset
518 def __init__(self, name):
kono
parents:
diff changeset
519 super(GdbPrettyPrinters, self).__init__(name, [])
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 def add_printer_for_types(self, name, class_, types):
kono
parents:
diff changeset
522 self.subprinters.append(GdbSubprinterTypeList(name, class_, types))
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 def add_printer_for_regex(self, name, class_, regex):
kono
parents:
diff changeset
525 self.subprinters.append(GdbSubprinterRegex(name, class_, regex))
kono
parents:
diff changeset
526
kono
parents:
diff changeset
527 def __call__(self, gdbval):
kono
parents:
diff changeset
528 type_ = gdbval.type.unqualified()
kono
parents:
diff changeset
529 str_type = str(type_)
kono
parents:
diff changeset
530 for printer in self.subprinters:
kono
parents:
diff changeset
531 if printer.enabled and printer.handles_type(str_type):
kono
parents:
diff changeset
532 return printer.class_(gdbval)
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 # Couldn't find a pretty printer (or it was disabled):
kono
parents:
diff changeset
535 return None
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 def build_pretty_printer():
kono
parents:
diff changeset
539 pp = GdbPrettyPrinters('gcc')
kono
parents:
diff changeset
540 pp.add_printer_for_types(['tree'],
kono
parents:
diff changeset
541 'tree', TreePrinter)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
542 pp.add_printer_for_types(['cgraph_node *', 'varpool_node *', 'symtab_node *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
543 'symtab_node', SymtabNodePrinter)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
544 pp.add_printer_for_types(['cgraph_edge *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
545 'cgraph_edge', CgraphEdgePrinter)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
546 pp.add_printer_for_types(['ipa_ref *'],
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
547 'ipa_ref', IpaReferencePrinter)
111
kono
parents:
diff changeset
548 pp.add_printer_for_types(['dw_die_ref'],
kono
parents:
diff changeset
549 'dw_die_ref', DWDieRefPrinter)
kono
parents:
diff changeset
550 pp.add_printer_for_types(['gimple', 'gimple *',
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 # Keep this in the same order as gimple.def:
kono
parents:
diff changeset
553 'gimple_cond', 'const_gimple_cond',
kono
parents:
diff changeset
554 'gimple_statement_cond *',
kono
parents:
diff changeset
555 'gimple_debug', 'const_gimple_debug',
kono
parents:
diff changeset
556 'gimple_statement_debug *',
kono
parents:
diff changeset
557 'gimple_label', 'const_gimple_label',
kono
parents:
diff changeset
558 'gimple_statement_label *',
kono
parents:
diff changeset
559 'gimple_switch', 'const_gimple_switch',
kono
parents:
diff changeset
560 'gimple_statement_switch *',
kono
parents:
diff changeset
561 'gimple_assign', 'const_gimple_assign',
kono
parents:
diff changeset
562 'gimple_statement_assign *',
kono
parents:
diff changeset
563 'gimple_bind', 'const_gimple_bind',
kono
parents:
diff changeset
564 'gimple_statement_bind *',
kono
parents:
diff changeset
565 'gimple_phi', 'const_gimple_phi',
kono
parents:
diff changeset
566 'gimple_statement_phi *'],
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 'gimple',
kono
parents:
diff changeset
569 GimplePrinter)
kono
parents:
diff changeset
570 pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
kono
parents:
diff changeset
571 'basic_block',
kono
parents:
diff changeset
572 BasicBlockPrinter)
kono
parents:
diff changeset
573 pp.add_printer_for_types(['edge', 'edge_def *'],
kono
parents:
diff changeset
574 'edge',
kono
parents:
diff changeset
575 CfgEdgePrinter)
kono
parents:
diff changeset
576 pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
kono
parents:
diff changeset
577 pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
kono
parents:
diff changeset
578
kono
parents:
diff changeset
579 pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*',
kono
parents:
diff changeset
580 'vec',
kono
parents:
diff changeset
581 VecPrinter)
kono
parents:
diff changeset
582
kono
parents:
diff changeset
583 pp.add_printer_for_regex(r'opt_mode<(\S+)>',
kono
parents:
diff changeset
584 'opt_mode', OptMachineModePrinter)
kono
parents:
diff changeset
585 pp.add_printer_for_types(['opt_scalar_int_mode',
kono
parents:
diff changeset
586 'opt_scalar_float_mode',
kono
parents:
diff changeset
587 'opt_scalar_mode'],
kono
parents:
diff changeset
588 'opt_mode', OptMachineModePrinter)
kono
parents:
diff changeset
589 pp.add_printer_for_regex(r'pod_mode<(\S+)>',
kono
parents:
diff changeset
590 'pod_mode', MachineModePrinter)
kono
parents:
diff changeset
591 pp.add_printer_for_types(['scalar_int_mode_pod',
kono
parents:
diff changeset
592 'scalar_mode_pod'],
kono
parents:
diff changeset
593 'pod_mode', MachineModePrinter)
kono
parents:
diff changeset
594 for mode in ('scalar_mode', 'scalar_int_mode', 'scalar_float_mode',
kono
parents:
diff changeset
595 'complex_mode'):
kono
parents:
diff changeset
596 pp.add_printer_for_types([mode], mode, MachineModePrinter)
kono
parents:
diff changeset
597
kono
parents:
diff changeset
598 return pp
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 gdb.printing.register_pretty_printer(
kono
parents:
diff changeset
601 gdb.current_objfile(),
kono
parents:
diff changeset
602 build_pretty_printer())
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 def find_gcc_source_dir():
kono
parents:
diff changeset
605 # Use location of global "g" to locate the source tree
kono
parents:
diff changeset
606 sym_g = gdb.lookup_global_symbol('g')
kono
parents:
diff changeset
607 path = sym_g.symtab.filename # e.g. '../../src/gcc/context.h'
kono
parents:
diff changeset
608 srcdir = os.path.split(path)[0] # e.g. '../../src/gcc'
kono
parents:
diff changeset
609 return srcdir
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 class PassNames:
kono
parents:
diff changeset
612 """Parse passes.def, gathering a list of pass class names"""
kono
parents:
diff changeset
613 def __init__(self):
kono
parents:
diff changeset
614 srcdir = find_gcc_source_dir()
kono
parents:
diff changeset
615 self.names = []
kono
parents:
diff changeset
616 with open(os.path.join(srcdir, 'passes.def')) as f:
kono
parents:
diff changeset
617 for line in f:
kono
parents:
diff changeset
618 m = re.match('\s*NEXT_PASS \(([^,]+).*\);', line)
kono
parents:
diff changeset
619 if m:
kono
parents:
diff changeset
620 self.names.append(m.group(1))
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 class BreakOnPass(gdb.Command):
kono
parents:
diff changeset
623 """
kono
parents:
diff changeset
624 A custom command for putting breakpoints on the execute hook of passes.
kono
parents:
diff changeset
625 This is largely a workaround for issues with tab-completion in gdb when
kono
parents:
diff changeset
626 setting breakpoints on methods on classes within anonymous namespaces.
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 Example of use: putting a breakpoint on "final"
kono
parents:
diff changeset
629 (gdb) break-on-pass
kono
parents:
diff changeset
630 Press <TAB>; it autocompletes to "pass_":
kono
parents:
diff changeset
631 (gdb) break-on-pass pass_
kono
parents:
diff changeset
632 Press <TAB>:
kono
parents:
diff changeset
633 Display all 219 possibilities? (y or n)
kono
parents:
diff changeset
634 Press "n"; then type "f":
kono
parents:
diff changeset
635 (gdb) break-on-pass pass_f
kono
parents:
diff changeset
636 Press <TAB> to autocomplete to pass classnames beginning with "pass_f":
kono
parents:
diff changeset
637 pass_fast_rtl_dce pass_fold_builtins
kono
parents:
diff changeset
638 pass_feedback_split_functions pass_forwprop
kono
parents:
diff changeset
639 pass_final pass_fre
kono
parents:
diff changeset
640 pass_fixup_cfg pass_free_cfg
kono
parents:
diff changeset
641 Type "in<TAB>" to complete to "pass_final":
kono
parents:
diff changeset
642 (gdb) break-on-pass pass_final
kono
parents:
diff changeset
643 ...and hit <RETURN>:
kono
parents:
diff changeset
644 Breakpoint 6 at 0x8396ba: file ../../src/gcc/final.c, line 4526.
kono
parents:
diff changeset
645 ...and we have a breakpoint set; continue execution:
kono
parents:
diff changeset
646 (gdb) cont
kono
parents:
diff changeset
647 Continuing.
kono
parents:
diff changeset
648 Breakpoint 6, (anonymous namespace)::pass_final::execute (this=0x17fb990) at ../../src/gcc/final.c:4526
kono
parents:
diff changeset
649 4526 virtual unsigned int execute (function *) { return rest_of_handle_final (); }
kono
parents:
diff changeset
650 """
kono
parents:
diff changeset
651 def __init__(self):
kono
parents:
diff changeset
652 gdb.Command.__init__(self, 'break-on-pass', gdb.COMMAND_BREAKPOINTS)
kono
parents:
diff changeset
653 self.pass_names = None
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 def complete(self, text, word):
kono
parents:
diff changeset
656 # Lazily load pass names:
kono
parents:
diff changeset
657 if not self.pass_names:
kono
parents:
diff changeset
658 self.pass_names = PassNames()
kono
parents:
diff changeset
659
kono
parents:
diff changeset
660 return [name
kono
parents:
diff changeset
661 for name in sorted(self.pass_names.names)
kono
parents:
diff changeset
662 if name.startswith(text)]
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
665 sym = '(anonymous namespace)::%s::execute' % arg
kono
parents:
diff changeset
666 breakpoint = gdb.Breakpoint(sym)
kono
parents:
diff changeset
667
kono
parents:
diff changeset
668 BreakOnPass()
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 class DumpFn(gdb.Command):
kono
parents:
diff changeset
671 """
kono
parents:
diff changeset
672 A custom command to dump a gimple/rtl function to file. By default, it
kono
parents:
diff changeset
673 dumps the current function using 0 as dump_flags, but the function and flags
kono
parents:
diff changeset
674 can also be specified. If /f <file> are passed as the first two arguments,
kono
parents:
diff changeset
675 the dump is written to that file. Otherwise, a temporary file is created
kono
parents:
diff changeset
676 and opened in the text editor specified in the EDITOR environment variable.
kono
parents:
diff changeset
677
kono
parents:
diff changeset
678 Examples of use:
kono
parents:
diff changeset
679 (gdb) dump-fn
kono
parents:
diff changeset
680 (gdb) dump-fn /f foo.1.txt
kono
parents:
diff changeset
681 (gdb) dump-fn cfun->decl
kono
parents:
diff changeset
682 (gdb) dump-fn /f foo.1.txt cfun->decl
kono
parents:
diff changeset
683 (gdb) dump-fn cfun->decl 0
kono
parents:
diff changeset
684 (gdb) dump-fn cfun->decl dump_flags
kono
parents:
diff changeset
685 """
kono
parents:
diff changeset
686
kono
parents:
diff changeset
687 def __init__(self):
kono
parents:
diff changeset
688 gdb.Command.__init__(self, 'dump-fn', gdb.COMMAND_USER)
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
691 # Parse args, check number of args
kono
parents:
diff changeset
692 args = gdb.string_to_argv(arg)
kono
parents:
diff changeset
693 if len(args) >= 1 and args[0] == "/f":
kono
parents:
diff changeset
694 if len(args) == 1:
kono
parents:
diff changeset
695 print ("Missing file argument")
kono
parents:
diff changeset
696 return
kono
parents:
diff changeset
697 filename = args[1]
kono
parents:
diff changeset
698 editor_mode = False
kono
parents:
diff changeset
699 base_arg = 2
kono
parents:
diff changeset
700 else:
kono
parents:
diff changeset
701 editor = os.getenv("EDITOR", "")
kono
parents:
diff changeset
702 if editor == "":
kono
parents:
diff changeset
703 print ("EDITOR environment variable not defined")
kono
parents:
diff changeset
704 return
kono
parents:
diff changeset
705 editor_mode = True
kono
parents:
diff changeset
706 base_arg = 0
kono
parents:
diff changeset
707 if len(args) - base_arg > 2:
kono
parents:
diff changeset
708 print ("Too many arguments")
kono
parents:
diff changeset
709 return
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 # Set func
kono
parents:
diff changeset
712 if len(args) - base_arg >= 1:
kono
parents:
diff changeset
713 funcname = args[base_arg]
kono
parents:
diff changeset
714 printfuncname = "function %s" % funcname
kono
parents:
diff changeset
715 else:
kono
parents:
diff changeset
716 funcname = "cfun ? cfun->decl : current_function_decl"
kono
parents:
diff changeset
717 printfuncname = "current function"
kono
parents:
diff changeset
718 func = gdb.parse_and_eval(funcname)
kono
parents:
diff changeset
719 if func == 0:
kono
parents:
diff changeset
720 print ("Could not find %s" % printfuncname)
kono
parents:
diff changeset
721 return
kono
parents:
diff changeset
722 func = "(tree)%u" % func
kono
parents:
diff changeset
723
kono
parents:
diff changeset
724 # Set flags
kono
parents:
diff changeset
725 if len(args) - base_arg >= 2:
kono
parents:
diff changeset
726 flags = gdb.parse_and_eval(args[base_arg + 1])
kono
parents:
diff changeset
727 else:
kono
parents:
diff changeset
728 flags = 0
kono
parents:
diff changeset
729
kono
parents:
diff changeset
730 # Get tempory file, if necessary
kono
parents:
diff changeset
731 if editor_mode:
kono
parents:
diff changeset
732 f = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
kono
parents:
diff changeset
733 filename = f.name
kono
parents:
diff changeset
734 f.close()
kono
parents:
diff changeset
735
kono
parents:
diff changeset
736 # Open file
kono
parents:
diff changeset
737 fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
kono
parents:
diff changeset
738 if fp == 0:
kono
parents:
diff changeset
739 print ("Could not open file: %s" % filename)
kono
parents:
diff changeset
740 return
kono
parents:
diff changeset
741 fp = "(FILE *)%u" % fp
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 # Dump function to file
kono
parents:
diff changeset
744 _ = gdb.parse_and_eval("dump_function_to_file (%s, %s, %u)" %
kono
parents:
diff changeset
745 (func, fp, flags))
kono
parents:
diff changeset
746
kono
parents:
diff changeset
747 # Close file
kono
parents:
diff changeset
748 ret = gdb.parse_and_eval("fclose (%s)" % fp)
kono
parents:
diff changeset
749 if ret != 0:
kono
parents:
diff changeset
750 print ("Could not close file: %s" % filename)
kono
parents:
diff changeset
751 return
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 # Open file in editor, if necessary
kono
parents:
diff changeset
754 if editor_mode:
kono
parents:
diff changeset
755 os.system("( %s \"%s\"; rm \"%s\" ) &" %
kono
parents:
diff changeset
756 (editor, filename, filename))
kono
parents:
diff changeset
757
kono
parents:
diff changeset
758 DumpFn()
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 class DotFn(gdb.Command):
kono
parents:
diff changeset
761 """
kono
parents:
diff changeset
762 A custom command to show a gimple/rtl function control flow graph.
kono
parents:
diff changeset
763 By default, it show the current function, but the function can also be
kono
parents:
diff changeset
764 specified.
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 Examples of use:
kono
parents:
diff changeset
767 (gdb) dot-fn
kono
parents:
diff changeset
768 (gdb) dot-fn cfun
kono
parents:
diff changeset
769 (gdb) dot-fn cfun 0
kono
parents:
diff changeset
770 (gdb) dot-fn cfun dump_flags
kono
parents:
diff changeset
771 """
kono
parents:
diff changeset
772 def __init__(self):
kono
parents:
diff changeset
773 gdb.Command.__init__(self, 'dot-fn', gdb.COMMAND_USER)
kono
parents:
diff changeset
774
kono
parents:
diff changeset
775 def invoke(self, arg, from_tty):
kono
parents:
diff changeset
776 # Parse args, check number of args
kono
parents:
diff changeset
777 args = gdb.string_to_argv(arg)
kono
parents:
diff changeset
778 if len(args) > 2:
kono
parents:
diff changeset
779 print("Too many arguments")
kono
parents:
diff changeset
780 return
kono
parents:
diff changeset
781
kono
parents:
diff changeset
782 # Set func
kono
parents:
diff changeset
783 if len(args) >= 1:
kono
parents:
diff changeset
784 funcname = args[0]
kono
parents:
diff changeset
785 printfuncname = "function %s" % funcname
kono
parents:
diff changeset
786 else:
kono
parents:
diff changeset
787 funcname = "cfun"
kono
parents:
diff changeset
788 printfuncname = "current function"
kono
parents:
diff changeset
789 func = gdb.parse_and_eval(funcname)
kono
parents:
diff changeset
790 if func == 0:
kono
parents:
diff changeset
791 print("Could not find %s" % printfuncname)
kono
parents:
diff changeset
792 return
kono
parents:
diff changeset
793 func = "(struct function *)%s" % func
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 # Set flags
kono
parents:
diff changeset
796 if len(args) >= 2:
kono
parents:
diff changeset
797 flags = gdb.parse_and_eval(args[1])
kono
parents:
diff changeset
798 else:
kono
parents:
diff changeset
799 flags = 0
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 # Get temp file
kono
parents:
diff changeset
802 f = tempfile.NamedTemporaryFile(delete=False)
kono
parents:
diff changeset
803 filename = f.name
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 # Close and reopen temp file to get C FILE*
kono
parents:
diff changeset
806 f.close()
kono
parents:
diff changeset
807 fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
kono
parents:
diff changeset
808 if fp == 0:
kono
parents:
diff changeset
809 print("Cannot open temp file")
kono
parents:
diff changeset
810 return
kono
parents:
diff changeset
811 fp = "(FILE *)%u" % fp
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 # Write graph to temp file
kono
parents:
diff changeset
814 _ = gdb.parse_and_eval("start_graph_dump (%s, \"<debug>\")" % fp)
kono
parents:
diff changeset
815 _ = gdb.parse_and_eval("print_graph_cfg (%s, %s, %u)"
kono
parents:
diff changeset
816 % (fp, func, flags))
kono
parents:
diff changeset
817 _ = gdb.parse_and_eval("end_graph_dump (%s)" % fp)
kono
parents:
diff changeset
818
kono
parents:
diff changeset
819 # Close temp file
kono
parents:
diff changeset
820 ret = gdb.parse_and_eval("fclose (%s)" % fp)
kono
parents:
diff changeset
821 if ret != 0:
kono
parents:
diff changeset
822 print("Could not close temp file: %s" % filename)
kono
parents:
diff changeset
823 return
kono
parents:
diff changeset
824
kono
parents:
diff changeset
825 # Show graph in temp file
kono
parents:
diff changeset
826 os.system("( dot -Tx11 \"%s\"; rm \"%s\" ) &" % (filename, filename))
kono
parents:
diff changeset
827
kono
parents:
diff changeset
828 DotFn()
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 print('Successfully loaded GDB hooks for GCC')