1 /* Inline frame unwinder for GDB.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "breakpoint.h"
21 #include "inline-frame.h"
24 #include "frame-unwind.h"
26 #include "gdbthread.h"
32 /* We need to save a few variables for every thread stopped at the
33 virtual call site of an inlined function. If there was always a
34 "struct thread_info", we could hang it off that; in the mean time,
38 inline_state (thread_info
*thread_
, int skipped_frames_
, CORE_ADDR saved_pc_
,
39 std::vector
<symbol
*> &&skipped_symbols_
)
40 : thread (thread_
), skipped_frames (skipped_frames_
), saved_pc (saved_pc_
),
41 skipped_symbols (std::move (skipped_symbols_
))
44 /* The thread this data relates to. It should be a currently
48 /* The number of inlined functions we are skipping. Each of these
49 functions can be stepped in to. */
52 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
53 when calculating SKIPPED_FRAMES; used to check whether we have
54 moved to a new location by user request. If so, we invalidate
55 any skipped frames. */
58 /* Only valid if SKIPPED_FRAMES is non-zero. This is the list of all
59 function symbols that have been skipped, from inner most to outer
60 most. It is used to find the call site of the current frame. */
61 std::vector
<struct symbol
*> skipped_symbols
;
64 static std::vector
<inline_state
> inline_states
;
66 /* Locate saved inlined frame state for THREAD, if it exists and is
69 static struct inline_state
*
70 find_inline_frame_state (thread_info
*thread
)
72 auto state_it
= std::find_if (inline_states
.begin (), inline_states
.end (),
73 [thread
] (const inline_state
&state
)
75 return state
.thread
== thread
;
78 if (state_it
== inline_states
.end ())
81 inline_state
&state
= *state_it
;
82 struct regcache
*regcache
= get_thread_regcache (thread
);
83 CORE_ADDR current_pc
= regcache_read_pc (regcache
);
85 if (current_pc
!= state
.saved_pc
)
87 /* PC has changed - this context is invalid. Use the
90 unordered_remove (inline_states
, state_it
);
97 /* See inline-frame.h. */
100 clear_inline_frame_state (process_stratum_target
*target
, ptid_t filter_ptid
)
102 gdb_assert (target
!= NULL
);
104 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
106 auto matcher
= [target
, &filter_ptid
] (const inline_state
&state
)
108 thread_info
*t
= state
.thread
;
109 return (t
->inf
->process_target () == target
110 && t
->ptid
.matches (filter_ptid
));
113 auto it
= std::remove_if (inline_states
.begin (), inline_states
.end (),
116 inline_states
.erase (it
, inline_states
.end ());
122 auto matcher
= [target
, &filter_ptid
] (const inline_state
&state
)
124 thread_info
*t
= state
.thread
;
125 return (t
->inf
->process_target () == target
126 && filter_ptid
== t
->ptid
);
129 auto it
= std::find_if (inline_states
.begin (), inline_states
.end (),
132 if (it
!= inline_states
.end ())
133 unordered_remove (inline_states
, it
);
136 /* See inline-frame.h. */
139 clear_inline_frame_state (thread_info
*thread
)
141 auto it
= std::find_if (inline_states
.begin (), inline_states
.end (),
142 [thread
] (const inline_state
&state
)
144 return thread
== state
.thread
;
147 if (it
!= inline_states
.end ())
148 unordered_remove (inline_states
, it
);
152 inline_frame_this_id (const frame_info_ptr
&this_frame
,
154 struct frame_id
*this_id
)
158 /* In order to have a stable frame ID for a given inline function,
159 we must get the stack / special addresses from the underlying
160 real frame's this_id method. So we must call
161 get_prev_frame_always. Because we are inlined into some
162 function, there must be previous frames, so this is safe - as
163 long as we're careful not to create any cycles. See related
164 comments in get_prev_frame_always_1. */
165 frame_info_ptr prev_frame
= get_prev_frame_always (this_frame
);
166 if (prev_frame
== nullptr)
167 error (_("failed to find previous frame when computing inline frame id"));
168 *this_id
= get_frame_id (prev_frame
);
170 /* We need a valid frame ID, so we need to be based on a valid
171 frame. FSF submission NOTE: this would be a good assertion to
172 apply to all frames, all the time. That would fix the ambiguity
173 of null_frame_id (between "no/any frame" and "the outermost
174 frame"). This will take work. */
175 gdb_assert (frame_id_p (*this_id
));
177 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
178 which generates DW_AT_entry_pc for inlined functions when
179 possible. If this attribute is available, we should use it
180 in the frame ID (and eventually, to set breakpoints). */
181 func
= get_frame_function (this_frame
);
182 gdb_assert (func
!= NULL
);
183 (*this_id
).code_addr
= func
->value_block ()->entry_pc ();
184 (*this_id
).artificial_depth
++;
187 static struct value
*
188 inline_frame_prev_register (const frame_info_ptr
&this_frame
, void **this_cache
,
191 /* Use get_frame_register_value instead of
192 frame_unwind_got_register, to avoid requiring this frame's ID.
193 This frame's ID depends on the previous frame's ID (unusual), and
194 the previous frame's ID depends on this frame's unwound
195 registers. If unwinding registers from this frame called
196 get_frame_id, there would be a loop.
198 Do not copy this code into any other unwinder! Inlined functions
199 are special; other unwinders must not have a dependency on the
200 previous frame's ID, and therefore can and should use
201 frame_unwind_got_register instead. */
202 return get_frame_register_value (this_frame
, regnum
);
205 /* Check whether we are at an inlining site that does not already
206 have an associated frame. */
209 inline_frame_sniffer (const struct frame_unwind
*self
,
210 const frame_info_ptr
&this_frame
,
214 const struct block
*frame_block
, *cur_block
;
216 frame_info_ptr next_frame
;
217 struct inline_state
*state
= find_inline_frame_state (inferior_thread ());
219 this_pc
= get_frame_address_in_block (this_frame
);
220 frame_block
= block_for_pc (this_pc
);
221 if (frame_block
== NULL
)
224 /* Calculate DEPTH, the number of inlined functions at this
227 cur_block
= frame_block
;
228 while (cur_block
->superblock ())
230 if (cur_block
->inlined_p ())
232 else if (cur_block
->function () != NULL
)
235 cur_block
= cur_block
->superblock ();
238 /* Check how many inlined functions already have frames. */
239 for (next_frame
= get_next_frame (this_frame
);
240 next_frame
&& get_frame_type (next_frame
) == INLINE_FRAME
;
241 next_frame
= get_next_frame (next_frame
))
243 gdb_assert (depth
> 0);
247 /* If this is the topmost frame, or all frames above us are inlined,
248 then check whether we were requested to skip some frames (so they
249 can be stepped into later). */
250 if (state
!= NULL
&& state
->skipped_frames
> 0 && next_frame
== NULL
)
252 gdb_assert (depth
>= state
->skipped_frames
);
253 depth
-= state
->skipped_frames
;
256 /* If all the inlined functions here already have frames, then pass
257 to the normal unwinder for this PC. */
261 /* If the next frame is an inlined function, but not the outermost, then
262 we are the next outer. If it is not an inlined function, then we
263 are the innermost inlined function of a different real frame. */
267 const struct frame_unwind inline_frame_unwind
= {
270 default_frame_unwind_stop_reason
,
271 inline_frame_this_id
,
272 inline_frame_prev_register
,
277 /* Return non-zero if BLOCK, an inlined function block containing PC,
278 has a group of contiguous instructions starting at PC (but not
282 block_starting_point_at (CORE_ADDR pc
, const struct block
*block
)
284 const struct blockvector
*bv
;
285 const struct block
*new_block
;
287 bv
= blockvector_for_pc (pc
, NULL
);
288 if (bv
->map () == nullptr)
291 new_block
= (const struct block
*) bv
->map ()->find (pc
- 1);
292 if (new_block
== NULL
)
295 if (new_block
== block
|| block
->contains (new_block
))
298 /* The immediately preceding address belongs to a different block,
299 which is not a child of this one. Treat this as an entrance into
304 /* Loop over the stop chain and determine if execution stopped in an
305 inlined frame because of a breakpoint with a user-specified location
306 set at FRAME_BLOCK. */
309 stopped_by_user_bp_inline_frame (const block
*frame_block
, bpstat
*stop_chain
)
311 for (bpstat
*s
= stop_chain
; s
!= nullptr; s
= s
->next
)
313 struct breakpoint
*bpt
= s
->breakpoint_at
;
316 && (user_breakpoint_p (bpt
) || bpt
->type
== bp_until
))
318 bp_location
*loc
= s
->bp_location_at
.get ();
319 enum bp_loc_type t
= loc
->loc_type
;
321 if (t
== bp_loc_software_breakpoint
322 || t
== bp_loc_hardware_breakpoint
)
324 /* If the location has a function symbol, check whether
325 the frame was for that inlined function. If it has
326 no function symbol, then assume it is. I.e., default
327 to presenting the stop at the innermost inline
329 if (loc
->symbol
== nullptr
330 || frame_block
== loc
->symbol
->value_block ())
339 /* See inline-frame.h. */
342 skip_inline_frames (thread_info
*thread
, bpstat
*stop_chain
)
344 const struct block
*frame_block
, *cur_block
;
345 std::vector
<struct symbol
*> skipped_syms
;
348 /* This function is called right after reinitializing the frame
349 cache. We try not to do more unwinding than absolutely
350 necessary, for performance. */
351 CORE_ADDR this_pc
= get_frame_pc (get_current_frame ());
352 frame_block
= block_for_pc (this_pc
);
354 if (frame_block
!= NULL
)
356 cur_block
= frame_block
;
357 while (cur_block
->superblock ())
359 if (cur_block
->inlined_p ())
361 /* See comments in inline_frame_this_id about this use
362 of BLOCK_ENTRY_PC. */
363 if (cur_block
->entry_pc () == this_pc
364 || block_starting_point_at (this_pc
, cur_block
))
366 /* Do not skip the inlined frame if execution
367 stopped in an inlined frame because of a user
368 breakpoint for this inline function. */
369 if (stopped_by_user_bp_inline_frame (cur_block
, stop_chain
))
373 skipped_syms
.push_back (cur_block
->function ());
378 else if (cur_block
->function () != NULL
)
381 cur_block
= cur_block
->superblock ();
385 gdb_assert (find_inline_frame_state (thread
) == NULL
);
386 inline_states
.emplace_back (thread
, skip_count
, this_pc
,
387 std::move (skipped_syms
));
390 reinit_frame_cache ();
393 /* Step into an inlined function by unhiding it. */
396 step_into_inline_frame (thread_info
*thread
)
398 inline_state
*state
= find_inline_frame_state (thread
);
400 gdb_assert (state
!= NULL
&& state
->skipped_frames
> 0);
401 state
->skipped_frames
--;
402 reinit_frame_cache ();
405 /* Return the number of hidden functions inlined into the current
409 inline_skipped_frames (thread_info
*thread
)
411 inline_state
*state
= find_inline_frame_state (thread
);
416 return state
->skipped_frames
;
419 /* If one or more inlined functions are hidden, return the symbol for
420 the function inlined into the current frame. */
423 inline_skipped_symbol (thread_info
*thread
)
425 inline_state
*state
= find_inline_frame_state (thread
);
426 gdb_assert (state
!= NULL
);
428 /* This should only be called when we are skipping at least one frame,
429 hence SKIPPED_FRAMES will be greater than zero when we get here.
430 We initialise SKIPPED_FRAMES at the same time as we build
431 SKIPPED_SYMBOLS, hence it should be true that SKIPPED_FRAMES never
432 indexes outside of the SKIPPED_SYMBOLS vector. */
433 gdb_assert (state
->skipped_frames
> 0);
434 gdb_assert (state
->skipped_frames
<= state
->skipped_symbols
.size ());
435 return state
->skipped_symbols
[state
->skipped_frames
- 1];
438 /* Return the number of functions inlined into THIS_FRAME. Some of
439 the callees may not have associated frames (see
440 skip_inline_frames). */
443 frame_inlined_callees (const frame_info_ptr
&this_frame
)
445 frame_info_ptr next_frame
;
446 int inline_count
= 0;
448 /* First count how many inlined functions at this PC have frames
449 above FRAME (are inlined into FRAME). */
450 for (next_frame
= get_next_frame (this_frame
);
451 next_frame
&& get_frame_type (next_frame
) == INLINE_FRAME
;
452 next_frame
= get_next_frame (next_frame
))
455 /* Simulate some most-inner inlined frames which were suppressed, so
456 they can be stepped into later. If we are unwinding already
457 outer frames from some non-inlined frame this does not apply. */
458 if (next_frame
== NULL
)
459 inline_count
+= inline_skipped_frames (inferior_thread ());