1# Copyright (C) 2001-2020 Free Software Foundation, Inc.
2#
3# This file is part of GCC.
4#
5# GCC is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3, or (at your option)
8# any later version.
9#
10# GCC is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with GCC; see the file COPYING3.  If not see
17# <http://www.gnu.org/licenses/>.
18
19define help-gcc-hooks
20help help-gcc-hooks
21end
22
23document help-gcc-hooks
24GCC gdbinit file introduces several debugging shorthands:
25
26    pr [rtx], prl [rtx], prc [rtx], pi [rtx_insn],
27    pt [tree], pct [tree], ptc [tree], trt [tree],
28    pgs [tree], pge [tree], pdn [tree], ptn [tree],
29    pgg [gimple], pgq [gimple_seq],
30    pmz [mpz_t],
31    pdd [dw_die_ref],
32    pbm [bitmap],
33    pel [location_t],
34    pp, pbs, pcfun
35
36They are generally implemented by calling a function that prints to stderr,
37and therefore will not work when the compiler is not executing.
38
39Most shorthands accept an optional argument. When it is not supplied,
40they use value in GDB register $, i.e. the last printed value.
41end
42
43define pp
44eval "set $debug_arg = $%s", $argc ? "arg0" : ""
45call debug ($debug_arg)
46end
47
48document pp
49GCC hook: pp [any]
50Print a representation of any GCC data structure for which an instance of
51overloaded function 'debug' is available.
52See also 'help-gcc-hooks'.
53end
54
55define pr
56eval "set $debug_arg = $%s", $argc ? "arg0" : ""
57call debug_rtx ($debug_arg)
58end
59
60document pr
61GCC hook: pr [rtx]
62Print the full structure of given rtx.
63See also 'help-gcc-hooks'.
64end
65
66define prl
67eval "set $debug_arg = $%s", $argc ? "arg0" : ""
68call debug_rtx_list ($debug_arg, debug_rtx_count)
69end
70
71document prl
72GCC hook: prl [rtx]
73Print the full structure of all rtx insns beginning at given rtx.
74Uses variable debug_rtx_count to control number of insns printed:
75  debug_rtx_count > 0: print from given rtx on.
76  debug_rtx_count < 0: print a window around given rtx.
77
78There is also debug_rtx_find (rtx, uid) that will scan a list for UID and print
79it using debug_rtx_list. Usage example: set $foo=debug_rtx_find(first, 42)
80end
81
82define pt
83eval "set $debug_arg = $%s", $argc ? "arg0" : ""
84call debug_tree ($debug_arg)
85end
86
87document pt
88GCC hook: pt [tree]
89Print the full structure of given tree.
90See also 'help-gcc-hooks'.
91end
92
93define pct
94eval "set $debug_arg = $%s", $argc ? "arg0" : ""
95call debug_c_tree ($debug_arg)
96end
97
98document pct
99GCC hook: pct [tree]
100Print given tree in C syntax.
101See also 'help-gcc-hooks'.
102end
103
104define pgg
105eval "set $debug_arg = $%s", $argc ? "arg0" : ""
106call debug_gimple_stmt ($debug_arg)
107end
108
109document pgg
110GCC hook: pgg [gimple]
111Print given GIMPLE statement in C syntax.
112See also 'help-gcc-hooks'.
113end
114
115define pgq
116eval "set $debug_arg = $%s", $argc ? "arg0" : ""
117call debug_gimple_seq ($debug_arg)
118end
119
120document pgq
121GCC hook: pgq [gimple_seq]
122Print given GIMPLE sequence in C syntax.
123See also 'help-gcc-hooks'.
124end
125
126define pgs
127eval "set $debug_arg = $%s", $argc ? "arg0" : ""
128call debug_generic_stmt ($debug_arg)
129end
130
131document pgs
132GCC hook: pgs [tree]
133Print given GENERIC statement in C syntax.
134See also 'help-gcc-hooks'.
135end
136
137define pge
138eval "set $debug_arg = $%s", $argc ? "arg0" : ""
139call debug_generic_expr ($debug_arg)
140end
141
142document pge
143GCC hook: pge [tree]
144Print given GENERIC expression in C syntax.
145See also 'help-gcc-hooks'.
146end
147
148define pmz
149eval "set $debug_arg = $%s", $argc ? "arg0" : ""
150call mpz_out_str(stderr, 10, $debug_arg)
151end
152
153document pmz
154GCC hook: pmz [mpz_t]
155Print given mpz value.
156See also 'help-gcc-hooks'.
157end
158
159define ptc
160eval "set $debug_arg = $%s", $argc ? "arg0" : ""
161output (enum tree_code) $debug_arg.base.code
162echo \n
163end
164
165document ptc
166GCC hook: ptc [tree]
167Print the tree-code of given tree node.
168See also 'help-gcc-hooks'.
169end
170
171define pdn
172eval "set $debug_arg = $%s", $argc ? "arg0" : ""
173output $debug_arg.decl_minimal.name->identifier.id.str
174echo \n
175end
176
177document pdn
178GCC hook: pdn [tree]
179Print the name of given decl-node.
180See also 'help-gcc-hooks'.
181end
182
183define ptn
184eval "set $debug_arg = $%s", $argc ? "arg0" : ""
185output $debug_arg.type.name->decl_minimal.name->identifier.id.str
186echo \n
187end
188
189document ptn
190GCC hook: ptn [tree]
191Print the name of given type-node.
192See also 'help-gcc-hooks'.
193end
194
195define pdd
196eval "set $debug_arg = $%s", $argc ? "arg0" : ""
197call debug_dwarf_die ($debug_arg)
198end
199
200document pdd
201GCC hook: pdd [dw_die_ref]
202Print given dw_die_ref.
203See also 'help-gcc-hooks'.
204end
205
206define prc
207eval "set $debug_arg = $%s", $argc ? "arg0" : ""
208output (enum rtx_code) $debug_arg.code
209echo \ (
210output $debug_arg.mode
211echo )\n
212end
213
214document prc
215GCC hook: prc [rtx]
216Print the rtx-code and machine mode of given rtx.
217See also 'help-gcc-hooks'.
218end
219
220define pi
221eval "set $debug_arg = $%s", $argc ? "arg0" : ""
222print $debug_arg.u.fld[0].rt_rtx@7
223end
224
225document pi
226GCC hook: pi [rtx_insn]
227Print the fields of given RTL instruction.
228See also 'help-gcc-hooks'.
229end
230
231define pbs
232call print_binding_stack ()
233end
234
235document pbs
236In cc1plus, print the current binding stack, frame by frame, up to and
237including the global binding level.
238end
239
240define pbm
241eval "set $debug_arg = $%s", $argc ? "arg0" : ""
242call bitmap_print (stderr, $debug_arg, "", "\n")
243end
244
245document pbm
246GCC hook: pbm [bitmap]
247Dump given bitmap as a comma-separated list of numbers.
248See also 'help-gcc-hooks'.
249end
250
251define pel
252eval "set $debug_arg = $%s", $argc ? "arg0" : ""
253output expand_location ($debug_arg)
254echo \n
255end
256
257document pel
258GCC hook: pel [location_t]
259Print given location.
260See also 'help-gcc-hooks'.
261end
262
263define pcfun
264output debug_function (cfun ? cfun->decl : current_function_decl, 0)
265echo \n
266end
267
268document pcfun
269Print current function.
270end
271
272define trt
273eval "set $debug_arg = $%s", $argc ? "arg0" : ""
274print ($debug_arg.typed.type)
275end
276
277document trt
278GCC hook: trt [tree]
279Print TREE_TYPE of given tree node.
280See also 'help-gcc-hooks'.
281end
282
283define break-on-diagnostic
284break diagnostic_show_locus
285end
286
287document break-on-diagnostic
288Put a breakpoint on diagnostic_show_locus, called whenever a diagnostic
289is emitted (as opposed to those warnings that are suppressed by
290command-line options).
291end
292
293define break-on-saved-diagnostic
294break ana::diagnostic_manager::add_diagnostic
295end
296
297document break-on-saved-diagnostic
298Put a breakpoint on ana::diagnostic_manager::add_diagnostic, called within
299the analyzer whenever a diagnostic is saved for later de-duplication and
300possible emission.
301end
302
303define reload-gdbhooks
304python import imp; imp.reload(gdbhooks)
305end
306
307document reload-gdbhooks
308Load the gdbhooks.py module again in order to pick up any changes made to it.
309end
310
311alias rh = reload-gdbhooks
312
313# Define some macros helpful to gdb when it is expanding macros.
314macro define __FILE__ "gdb"
315macro define __LINE__ 1
316macro define __FUNCTION__ "gdb"
317macro define __null 0
318macro define input_line expand_location(input_location).line
319macro define input_filename expand_location(input_location).file
320
321# Remember previous pagination status and turn it off, so that
322# the messages for skip commands don't require pagination.
323python __gcc_prev_pagination=gdb.parameter("pagination")
324set pagination off
325
326# Gracefully handle aborts in functions used from gdb.
327set unwindonsignal on
328
329# Put breakpoints at exit and fancy_abort in case abort is mapped
330# to either fprintf/exit or fancy_abort.
331b fancy_abort
332
333# Put a breakpoint on internal_error to help with debugging ICEs.
334b internal_error
335
336set complaints 0
337# Don't let abort actually run, as it will make
338# stdio stop working and therefore the `pr' command above as well.
339# Put this last because gcc does not reference it any more unless
340# USE_SYSTEM_ABORT is defined, so gdb may complain and bail out.
341b exit
342b abort
343
344# Disable strict type checking.  This allows developers to (for example)
345# make inferior calls without casting absolute address to a suitable
346# pointer type.
347set check type off
348
349# Skip all inline functions in tree.h.
350# These are used in accessor macros.
351# Note that this is added at the end because older gdb versions
352# do not understand the 'skip' command.
353# See https://sourceware.org/gdb/current/onlinedocs/gdb/Skipping-Over-Functions-and-Files.html
354skip file tree.h
355
356# Also skip inline functions in is-a.h.
357skip file is-a.h
358
359# And line-map.h.
360skip file line-map.h
361
362# And timevar.h.
363skip file timevar.h
364
365# Likewise, skip various inline functions in rtl.h.
366skip rtx_expr_list::next
367skip rtx_expr_list::element
368skip rtx_insn_list::next
369skip rtx_insn_list::insn
370skip rtx_sequence::len
371skip rtx_sequence::element
372skip rtx_sequence::insn
373skip INSN_UID
374skip PREV_INSN
375skip SET_PREV_INSN
376skip NEXT_INSN
377skip SET_NEXT_INSN
378skip BLOCK_FOR_INSN
379skip PATTERN
380skip INSN_LOCATION
381skip INSN_HAS_LOCATION
382skip JUMP_LABEL_AS_INSN
383
384# Restore pagination to the previous state.
385python if __gcc_prev_pagination: gdb.execute("set pagination on")
386