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