1# Python hooks for gdb for debugging GCC 2# Copyright (C) 2013-2014 Free Software Foundation, Inc. 3 4# Contributed by David Malcolm <dmalcolm@redhat.com> 5 6# This file is part of GCC. 7 8# GCC is free software; you can redistribute it and/or modify it under 9# the terms of the GNU General Public License as published by the Free 10# Software Foundation; either version 3, or (at your option) any later 11# version. 12 13# GCC is distributed in the hope that it will be useful, but WITHOUT 14# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16# for more details. 17 18# You should have received a copy of the GNU General Public License 19# along with GCC; see the file COPYING3. If not see 20# <http://www.gnu.org/licenses/>. 21 22""" 23Enabling the debugging hooks 24---------------------------- 25gcc/configure (from configure.ac) generates a .gdbinit within the "gcc" 26subdirectory of the build directory, and when run by gdb, this imports 27gcc/gdbhooks.py from the source directory, injecting useful Python code 28into gdb. 29 30You may see a message from gdb of the form: 31 "path-to-build/gcc/.gdbinit" auto-loading has been declined by your `auto-load safe-path' 32as a protection against untrustworthy python scripts. See 33 http://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html 34 35The fix is to mark the paths of the build/gcc directory as trustworthy. 36An easy way to do so is by adding the following to your ~/.gdbinit script: 37 add-auto-load-safe-path /absolute/path/to/build/gcc 38for the build directories for your various checkouts of gcc. 39 40If it's working, you should see the message: 41 Successfully loaded GDB hooks for GCC 42as gdb starts up. 43 44During development, I've been manually invoking the code in this way, as a 45precanned way of printing a variety of different kinds of value: 46 47 gdb \ 48 -ex "break expand_gimple_stmt" \ 49 -ex "run" \ 50 -ex "bt" \ 51 --args \ 52 ./cc1 foo.c -O3 53 54Examples of output using the pretty-printers 55-------------------------------------------- 56Pointer values are generally shown in the form: 57 <type address extra_info> 58 59For example, an opt_pass* might appear as: 60 (gdb) p pass 61 $2 = <opt_pass* 0x188b600 "expand"(170)> 62 63The name of the pass is given ("expand"), together with the 64static_pass_number. 65 66Note that you can dereference the pointer in the normal way: 67 (gdb) p *pass 68 $4 = {type = RTL_PASS, name = 0x120a312 "expand", 69 [etc, ...snipped...] 70 71and you can suppress pretty-printers using /r (for "raw"): 72 (gdb) p /r pass 73 $3 = (opt_pass *) 0x188b600 74 75Basic blocks are shown with their index in parentheses, apart from the 76CFG's entry and exit blocks, which are given as "ENTRY" and "EXIT": 77 (gdb) p bb 78 $9 = <basic_block 0x7ffff041f1a0 (2)> 79 (gdb) p cfun->cfg->x_entry_block_ptr 80 $10 = <basic_block 0x7ffff041f0d0 (ENTRY)> 81 (gdb) p cfun->cfg->x_exit_block_ptr 82 $11 = <basic_block 0x7ffff041f138 (EXIT)> 83 84CFG edges are shown with the src and dest blocks given in parentheses: 85 (gdb) p e 86 $1 = <edge 0x7ffff043f118 (ENTRY -> 6)> 87 88Tree nodes are printed using Python code that emulates print_node_brief, 89running in gdb, rather than in the inferior: 90 (gdb) p cfun->decl 91 $1 = <function_decl 0x7ffff0420b00 foo> 92For usability, the type is printed first (e.g. "function_decl"), rather 93than just "tree". 94 95RTL expressions use a kludge: they are pretty-printed by injecting 96calls into print-rtl.c into the inferior: 97 Value returned is $1 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK) 98 (gdb) p $1 99 $2 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK) 100 (gdb) p /r $1 101 $3 = (rtx_def *) 0x7ffff043e140 102This won't work for coredumps, and probably in other circumstances, but 103it's a quick way of getting lots of debuggability quickly. 104 105Callgraph nodes are printed with the name of the function decl, if 106available: 107 (gdb) frame 5 108 #5 0x00000000006c288a in expand_function (node=<cgraph_node* 0x7ffff0312720 "foo">) at ../../src/gcc/cgraphunit.c:1594 109 1594 execute_pass_list (g->get_passes ()->all_passes); 110 (gdb) p node 111 $1 = <cgraph_node* 0x7ffff0312720 "foo"> 112 113vec<> pointers are printed as the address followed by the elements in 114braces. Here's a length 2 vec: 115 (gdb) p bb->preds 116 $18 = 0x7ffff0428b68 = {<edge 0x7ffff044d380 (3 -> 5)>, <edge 0x7ffff044d3b8 (4 -> 5)>} 117 118and here's a length 1 vec: 119 (gdb) p bb->succs 120 $19 = 0x7ffff0428bb8 = {<edge 0x7ffff044d3f0 (5 -> EXIT)>} 121 122You cannot yet use array notation [] to access the elements within the 123vector: attempting to do so instead gives you the vec itself (for vec[0]), 124or a (probably) invalid cast to vec<> for the memory after the vec (for 125vec[1] onwards). 126 127Instead (for now) you must access m_vecdata: 128 (gdb) p bb->preds->m_vecdata[0] 129 $20 = <edge 0x7ffff044d380 (3 -> 5)> 130 (gdb) p bb->preds->m_vecdata[1] 131 $21 = <edge 0x7ffff044d3b8 (4 -> 5)> 132""" 133import re 134 135import gdb 136import gdb.printing 137import gdb.types 138 139# Convert "enum tree_code" (tree.def and tree.h) to a dict: 140tree_code_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code')) 141 142# ...and look up specific values for use later: 143IDENTIFIER_NODE = tree_code_dict['IDENTIFIER_NODE'] 144TYPE_DECL = tree_code_dict['TYPE_DECL'] 145 146# Similarly for "enum tree_code_class" (tree.h): 147tree_code_class_dict = gdb.types.make_enum_dict(gdb.lookup_type('enum tree_code_class')) 148tcc_type = tree_code_class_dict['tcc_type'] 149tcc_declaration = tree_code_class_dict['tcc_declaration'] 150 151class Tree: 152 """ 153 Wrapper around a gdb.Value for a tree, with various methods 154 corresponding to macros in gcc/tree.h 155 """ 156 def __init__(self, gdbval): 157 self.gdbval = gdbval 158 159 def is_nonnull(self): 160 return long(self.gdbval) 161 162 def TREE_CODE(self): 163 """ 164 Get gdb.Value corresponding to TREE_CODE (self) 165 as per: 166 #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code) 167 """ 168 return self.gdbval['base']['code'] 169 170 def DECL_NAME(self): 171 """ 172 Get Tree instance corresponding to DECL_NAME (self) 173 """ 174 return Tree(self.gdbval['decl_minimal']['name']) 175 176 def TYPE_NAME(self): 177 """ 178 Get Tree instance corresponding to result of TYPE_NAME (self) 179 """ 180 return Tree(self.gdbval['type_common']['name']) 181 182 def IDENTIFIER_POINTER(self): 183 """ 184 Get str correspoinding to result of IDENTIFIER_NODE (self) 185 """ 186 return self.gdbval['identifier']['id']['str'].string() 187 188class TreePrinter: 189 "Prints a tree" 190 191 def __init__ (self, gdbval): 192 self.gdbval = gdbval 193 self.node = Tree(gdbval) 194 195 def to_string (self): 196 # like gcc/print-tree.c:print_node_brief 197 # #define TREE_CODE(NODE) ((enum tree_code) (NODE)->base.code) 198 # tree_code_name[(int) TREE_CODE (node)]) 199 if long(self.gdbval) == 0: 200 return '<tree 0x0>' 201 202 val_TREE_CODE = self.node.TREE_CODE() 203 204 # extern const enum tree_code_class tree_code_type[]; 205 # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)] 206 207 val_tree_code_type = gdb.parse_and_eval('tree_code_type') 208 val_tclass = val_tree_code_type[val_TREE_CODE] 209 210 val_tree_code_name = gdb.parse_and_eval('tree_code_name') 211 val_code_name = val_tree_code_name[long(val_TREE_CODE)] 212 #print val_code_name.string() 213 214 result = '<%s 0x%x' % (val_code_name.string(), long(self.gdbval)) 215 if long(val_tclass) == tcc_declaration: 216 tree_DECL_NAME = self.node.DECL_NAME() 217 if tree_DECL_NAME.is_nonnull(): 218 result += ' %s' % tree_DECL_NAME.IDENTIFIER_POINTER() 219 else: 220 pass # TODO: labels etc 221 elif long(val_tclass) == tcc_type: 222 tree_TYPE_NAME = Tree(self.gdbval['type_common']['name']) 223 if tree_TYPE_NAME.is_nonnull(): 224 if tree_TYPE_NAME.TREE_CODE() == IDENTIFIER_NODE: 225 result += ' %s' % tree_TYPE_NAME.IDENTIFIER_POINTER() 226 elif tree_TYPE_NAME.TREE_CODE() == TYPE_DECL: 227 if tree_TYPE_NAME.DECL_NAME().is_nonnull(): 228 result += ' %s' % tree_TYPE_NAME.DECL_NAME().IDENTIFIER_POINTER() 229 if self.node.TREE_CODE() == IDENTIFIER_NODE: 230 result += ' %s' % self.node.IDENTIFIER_POINTER() 231 # etc 232 result += '>' 233 return result 234 235###################################################################### 236# Callgraph pretty-printers 237###################################################################### 238 239class CGraphNodePrinter: 240 def __init__(self, gdbval): 241 self.gdbval = gdbval 242 243 def to_string (self): 244 result = '<cgraph_node* 0x%x' % long(self.gdbval) 245 if long(self.gdbval): 246 # symtab_node::name calls lang_hooks.decl_printable_name 247 # default implementation (lhd_decl_printable_name) is: 248 # return IDENTIFIER_POINTER (DECL_NAME (decl)); 249 tree_decl = Tree(self.gdbval['decl']) 250 result += ' "%s"' % tree_decl.DECL_NAME().IDENTIFIER_POINTER() 251 result += '>' 252 return result 253 254###################################################################### 255 256class GimplePrinter: 257 def __init__(self, gdbval): 258 self.gdbval = gdbval 259 260 def to_string (self): 261 if long(self.gdbval) == 0: 262 return '<gimple 0x0>' 263 val_gimple_code = self.gdbval['code'] 264 val_gimple_code_name = gdb.parse_and_eval('gimple_code_name') 265 val_code_name = val_gimple_code_name[long(val_gimple_code)] 266 result = '<%s 0x%x' % (val_code_name.string(), 267 long(self.gdbval)) 268 result += '>' 269 return result 270 271###################################################################### 272# CFG pretty-printers 273###################################################################### 274 275def bb_index_to_str(index): 276 if index == 0: 277 return 'ENTRY' 278 elif index == 1: 279 return 'EXIT' 280 else: 281 return '%i' % index 282 283class BasicBlockPrinter: 284 def __init__(self, gdbval): 285 self.gdbval = gdbval 286 287 def to_string (self): 288 result = '<basic_block 0x%x' % long(self.gdbval) 289 if long(self.gdbval): 290 result += ' (%s)' % bb_index_to_str(long(self.gdbval['index'])) 291 result += '>' 292 return result 293 294class CfgEdgePrinter: 295 def __init__(self, gdbval): 296 self.gdbval = gdbval 297 298 def to_string (self): 299 result = '<edge 0x%x' % long(self.gdbval) 300 if long(self.gdbval): 301 src = bb_index_to_str(long(self.gdbval['src']['index'])) 302 dest = bb_index_to_str(long(self.gdbval['dest']['index'])) 303 result += ' (%s -> %s)' % (src, dest) 304 result += '>' 305 return result 306 307###################################################################### 308 309class Rtx: 310 def __init__(self, gdbval): 311 self.gdbval = gdbval 312 313 def GET_CODE(self): 314 return self.gdbval['code'] 315 316def GET_RTX_LENGTH(code): 317 val_rtx_length = gdb.parse_and_eval('rtx_length') 318 return long(val_rtx_length[code]) 319 320def GET_RTX_NAME(code): 321 val_rtx_name = gdb.parse_and_eval('rtx_name') 322 return val_rtx_name[code].string() 323 324def GET_RTX_FORMAT(code): 325 val_rtx_format = gdb.parse_and_eval('rtx_format') 326 return val_rtx_format[code].string() 327 328class RtxPrinter: 329 def __init__(self, gdbval): 330 self.gdbval = gdbval 331 self.rtx = Rtx(gdbval) 332 333 def to_string (self): 334 """ 335 For now, a cheap kludge: invoke the inferior's print 336 function to get a string to use the user, and return an empty 337 string for gdb 338 """ 339 # We use print_inline_rtx to avoid a trailing newline 340 gdb.execute('call print_inline_rtx (stderr, (const_rtx) %s, 0)' 341 % long(self.gdbval)) 342 return '' 343 344 # or by hand; based on gcc/print-rtl.c:print_rtx 345 result = ('<rtx_def 0x%x' 346 % (long(self.gdbval))) 347 code = self.rtx.GET_CODE() 348 result += ' (%s' % GET_RTX_NAME(code) 349 format_ = GET_RTX_FORMAT(code) 350 for i in range(GET_RTX_LENGTH(code)): 351 print format_[i] 352 result += ')>' 353 return result 354 355###################################################################### 356 357class PassPrinter: 358 def __init__(self, gdbval): 359 self.gdbval = gdbval 360 361 def to_string (self): 362 result = '<opt_pass* 0x%x' % long(self.gdbval) 363 if long(self.gdbval): 364 result += (' "%s"(%i)' 365 % (self.gdbval['name'].string(), 366 long(self.gdbval['static_pass_number']))) 367 result += '>' 368 return result 369 370###################################################################### 371 372class VecPrinter: 373 # -ex "up" -ex "p bb->preds" 374 def __init__(self, gdbval): 375 self.gdbval = gdbval 376 377 def display_hint (self): 378 return 'array' 379 380 def to_string (self): 381 # A trivial implementation; prettyprinting the contents is done 382 # by gdb calling the "children" method below. 383 return '0x%x' % long(self.gdbval) 384 385 def children (self): 386 if long(self.gdbval) == 0: 387 return 388 m_vecpfx = self.gdbval['m_vecpfx'] 389 m_num = m_vecpfx['m_num'] 390 m_vecdata = self.gdbval['m_vecdata'] 391 for i in range(m_num): 392 yield ('[%d]' % i, m_vecdata[i]) 393 394###################################################################### 395 396# TODO: 397# * hashtab 398# * location_t 399 400class GdbSubprinter(gdb.printing.SubPrettyPrinter): 401 def __init__(self, name, class_): 402 super(GdbSubprinter, self).__init__(name) 403 self.class_ = class_ 404 405 def handles_type(self, str_type): 406 raise NotImplementedError 407 408class GdbSubprinterTypeList(GdbSubprinter): 409 """ 410 A GdbSubprinter that handles a specific set of types 411 """ 412 def __init__(self, str_types, name, class_): 413 super(GdbSubprinterTypeList, self).__init__(name, class_) 414 self.str_types = frozenset(str_types) 415 416 def handles_type(self, str_type): 417 return str_type in self.str_types 418 419class GdbSubprinterRegex(GdbSubprinter): 420 """ 421 A GdbSubprinter that handles types that match a regex 422 """ 423 def __init__(self, regex, name, class_): 424 super(GdbSubprinterRegex, self).__init__(name, class_) 425 self.regex = re.compile(regex) 426 427 def handles_type(self, str_type): 428 return self.regex.match(str_type) 429 430class GdbPrettyPrinters(gdb.printing.PrettyPrinter): 431 def __init__(self, name): 432 super(GdbPrettyPrinters, self).__init__(name, []) 433 434 def add_printer_for_types(self, name, class_, types): 435 self.subprinters.append(GdbSubprinterTypeList(name, class_, types)) 436 437 def add_printer_for_regex(self, name, class_, regex): 438 self.subprinters.append(GdbSubprinterRegex(name, class_, regex)) 439 440 def __call__(self, gdbval): 441 type_ = gdbval.type.unqualified() 442 str_type = str(type_) 443 for printer in self.subprinters: 444 if printer.enabled and printer.handles_type(str_type): 445 return printer.class_(gdbval) 446 447 # Couldn't find a pretty printer (or it was disabled): 448 return None 449 450 451def build_pretty_printer(): 452 pp = GdbPrettyPrinters('gcc') 453 pp.add_printer_for_types(['tree'], 454 'tree', TreePrinter) 455 pp.add_printer_for_types(['cgraph_node *'], 456 'cgraph_node', CGraphNodePrinter) 457 pp.add_printer_for_types(['gimple', 'gimple_statement_base *'], 458 'gimple', 459 GimplePrinter) 460 pp.add_printer_for_types(['basic_block', 'basic_block_def *'], 461 'basic_block', 462 BasicBlockPrinter) 463 pp.add_printer_for_types(['edge', 'edge_def *'], 464 'edge', 465 CfgEdgePrinter) 466 pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter) 467 pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter) 468 469 pp.add_printer_for_regex(r'vec<(\S+), (\S+), (\S+)> \*', 470 'vec', 471 VecPrinter) 472 473 return pp 474 475gdb.printing.register_pretty_printer( 476 gdb.current_objfile(), 477 build_pretty_printer()) 478 479print('Successfully loaded GDB hooks for GCC') 480