gdbhooks.py 20 KB

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