1 /* Inline frame unwinder for GDB.
3 Copyright (C) 2008-2015 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/>. */
21 #include "inline-frame.h"
24 #include "frame-unwind.h"
31 /* We need to save a few variables for every thread stopped at the
32 virtual call site of an inlined function. If there was always a
33 "struct thread_info", we could hang it off that; in the mean time,
37 /* The thread this data relates to. It should be a currently
38 stopped thread; we assume thread IDs never change while the
42 /* The number of inlined functions we are skipping. Each of these
43 functions can be stepped in to. */
46 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
47 when calculating SKIPPED_FRAMES; used to check whether we have
48 moved to a new location by user request. If so, we invalidate
49 any skipped frames. */
52 /* Only valid if SKIPPED_FRAMES is non-zero. This is the symbol
53 of the outermost skipped inline function. It's used to find the
54 call site of the current frame. */
55 struct symbol
*skipped_symbol
;
58 typedef struct inline_state inline_state_s
;
59 DEF_VEC_O(inline_state_s
);
61 static VEC(inline_state_s
) *inline_states
;
63 /* Locate saved inlined frame state for PTID, if it exists
66 static struct inline_state
*
67 find_inline_frame_state (ptid_t ptid
)
69 struct inline_state
*state
;
72 for (ix
= 0; VEC_iterate (inline_state_s
, inline_states
, ix
, state
); ix
++)
74 if (ptid_equal (state
->ptid
, ptid
))
76 struct regcache
*regcache
= get_thread_regcache (ptid
);
77 CORE_ADDR current_pc
= regcache_read_pc (regcache
);
79 if (current_pc
!= state
->saved_pc
)
81 /* PC has changed - this context is invalid. Use the
83 VEC_unordered_remove (inline_state_s
, inline_states
, ix
);
94 /* Allocate saved inlined frame state for PTID. */
96 static struct inline_state
*
97 allocate_inline_frame_state (ptid_t ptid
)
99 struct inline_state
*state
;
101 state
= VEC_safe_push (inline_state_s
, inline_states
, NULL
);
102 memset (state
, 0, sizeof (*state
));
108 /* Forget about any hidden inlined functions in PTID, which is new or
109 about to be resumed. PTID may be minus_one_ptid (all processes)
110 or a PID (all threads in this process). */
113 clear_inline_frame_state (ptid_t ptid
)
115 struct inline_state
*state
;
118 if (ptid_equal (ptid
, minus_one_ptid
))
120 VEC_free (inline_state_s
, inline_states
);
124 if (ptid_is_pid (ptid
))
126 VEC (inline_state_s
) *new_states
= NULL
;
127 int pid
= ptid_get_pid (ptid
);
130 VEC_iterate (inline_state_s
, inline_states
, ix
, state
);
132 if (pid
!= ptid_get_pid (state
->ptid
))
133 VEC_safe_push (inline_state_s
, new_states
, state
);
134 VEC_free (inline_state_s
, inline_states
);
135 inline_states
= new_states
;
139 for (ix
= 0; VEC_iterate (inline_state_s
, inline_states
, ix
, state
); ix
++)
140 if (ptid_equal (state
->ptid
, ptid
))
142 VEC_unordered_remove (inline_state_s
, inline_states
, ix
);
148 inline_frame_this_id (struct frame_info
*this_frame
,
150 struct frame_id
*this_id
)
154 /* In order to have a stable frame ID for a given inline function,
155 we must get the stack / special addresses from the underlying
156 real frame's this_id method. So we must call
157 get_prev_frame_always. Because we are inlined into some
158 function, there must be previous frames, so this is safe - as
159 long as we're careful not to create any cycles. */
160 *this_id
= get_frame_id (get_prev_frame_always (this_frame
));
162 /* We need a valid frame ID, so we need to be based on a valid
163 frame. FSF submission NOTE: this would be a good assertion to
164 apply to all frames, all the time. That would fix the ambiguity
165 of null_frame_id (between "no/any frame" and "the outermost
166 frame"). This will take work. */
167 gdb_assert (frame_id_p (*this_id
));
169 /* For now, require we don't match outer_frame_id either (see
171 gdb_assert (!frame_id_eq (*this_id
, outer_frame_id
));
173 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
174 which generates DW_AT_entry_pc for inlined functions when
175 possible. If this attribute is available, we should use it
176 in the frame ID (and eventually, to set breakpoints). */
177 func
= get_frame_function (this_frame
);
178 gdb_assert (func
!= NULL
);
179 (*this_id
).code_addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (func
));
180 (*this_id
).artificial_depth
++;
183 static struct value
*
184 inline_frame_prev_register (struct frame_info
*this_frame
, void **this_cache
,
187 /* Use get_frame_register_value instead of
188 frame_unwind_got_register, to avoid requiring this frame's ID.
189 This frame's ID depends on the previous frame's ID (unusual), and
190 the previous frame's ID depends on this frame's unwound
191 registers. If unwinding registers from this frame called
192 get_frame_id, there would be a loop.
194 Do not copy this code into any other unwinder! Inlined functions
195 are special; other unwinders must not have a dependency on the
196 previous frame's ID, and therefore can and should use
197 frame_unwind_got_register instead. */
198 return get_frame_register_value (this_frame
, regnum
);
201 /* Check whether we are at an inlining site that does not already
202 have an associated frame. */
205 inline_frame_sniffer (const struct frame_unwind
*self
,
206 struct frame_info
*this_frame
,
210 const struct block
*frame_block
, *cur_block
;
212 struct frame_info
*next_frame
;
213 struct inline_state
*state
= find_inline_frame_state (inferior_ptid
);
215 this_pc
= get_frame_address_in_block (this_frame
);
216 frame_block
= block_for_pc (this_pc
);
217 if (frame_block
== NULL
)
220 /* Calculate DEPTH, the number of inlined functions at this
223 cur_block
= frame_block
;
224 while (BLOCK_SUPERBLOCK (cur_block
))
226 if (block_inlined_p (cur_block
))
229 cur_block
= BLOCK_SUPERBLOCK (cur_block
);
232 /* Check how many inlined functions already have frames. */
233 for (next_frame
= get_next_frame (this_frame
);
234 next_frame
&& get_frame_type (next_frame
) == INLINE_FRAME
;
235 next_frame
= get_next_frame (next_frame
))
237 gdb_assert (depth
> 0);
241 /* If this is the topmost frame, or all frames above us are inlined,
242 then check whether we were requested to skip some frames (so they
243 can be stepped into later). */
244 if (state
!= NULL
&& state
->skipped_frames
> 0 && next_frame
== NULL
)
246 gdb_assert (depth
>= state
->skipped_frames
);
247 depth
-= state
->skipped_frames
;
250 /* If all the inlined functions here already have frames, then pass
251 to the normal unwinder for this PC. */
255 /* If the next frame is an inlined function, but not the outermost, then
256 we are the next outer. If it is not an inlined function, then we
257 are the innermost inlined function of a different real frame. */
261 const struct frame_unwind inline_frame_unwind
= {
263 default_frame_unwind_stop_reason
,
264 inline_frame_this_id
,
265 inline_frame_prev_register
,
270 /* Return non-zero if BLOCK, an inlined function block containing PC,
271 has a group of contiguous instructions starting at PC (but not
275 block_starting_point_at (CORE_ADDR pc
, const struct block
*block
)
277 const struct blockvector
*bv
;
278 struct block
*new_block
;
280 bv
= blockvector_for_pc (pc
, NULL
);
281 if (BLOCKVECTOR_MAP (bv
) == NULL
)
284 new_block
= addrmap_find (BLOCKVECTOR_MAP (bv
), pc
- 1);
285 if (new_block
== NULL
)
288 if (new_block
== block
|| contained_in (new_block
, block
))
291 /* The immediately preceding address belongs to a different block,
292 which is not a child of this one. Treat this as an entrance into
297 /* Skip all inlined functions whose call sites are at the current PC.
298 Frames for the hidden functions will not appear in the backtrace until the
299 user steps into them. */
302 skip_inline_frames (ptid_t ptid
)
305 const struct block
*frame_block
, *cur_block
;
306 struct symbol
*last_sym
= NULL
;
308 struct inline_state
*state
;
310 /* This function is called right after reinitializing the frame
311 cache. We try not to do more unwinding than absolutely
312 necessary, for performance. */
313 this_pc
= get_frame_pc (get_current_frame ());
314 frame_block
= block_for_pc (this_pc
);
316 if (frame_block
!= NULL
)
318 cur_block
= frame_block
;
319 while (BLOCK_SUPERBLOCK (cur_block
))
321 if (block_inlined_p (cur_block
))
323 /* See comments in inline_frame_this_id about this use
325 if (BLOCK_START (cur_block
) == this_pc
326 || block_starting_point_at (this_pc
, cur_block
))
329 last_sym
= BLOCK_FUNCTION (cur_block
);
334 cur_block
= BLOCK_SUPERBLOCK (cur_block
);
338 gdb_assert (find_inline_frame_state (ptid
) == NULL
);
339 state
= allocate_inline_frame_state (ptid
);
340 state
->skipped_frames
= skip_count
;
341 state
->saved_pc
= this_pc
;
342 state
->skipped_symbol
= last_sym
;
345 reinit_frame_cache ();
348 /* Step into an inlined function by unhiding it. */
351 step_into_inline_frame (ptid_t ptid
)
353 struct inline_state
*state
= find_inline_frame_state (ptid
);
355 gdb_assert (state
!= NULL
&& state
->skipped_frames
> 0);
356 state
->skipped_frames
--;
357 reinit_frame_cache ();
360 /* Return the number of hidden functions inlined into the current
364 inline_skipped_frames (ptid_t ptid
)
366 struct inline_state
*state
= find_inline_frame_state (ptid
);
371 return state
->skipped_frames
;
374 /* If one or more inlined functions are hidden, return the symbol for
375 the function inlined into the current frame. */
378 inline_skipped_symbol (ptid_t ptid
)
380 struct inline_state
*state
= find_inline_frame_state (ptid
);
382 gdb_assert (state
!= NULL
);
383 return state
->skipped_symbol
;
386 /* Return the number of functions inlined into THIS_FRAME. Some of
387 the callees may not have associated frames (see
388 skip_inline_frames). */
391 frame_inlined_callees (struct frame_info
*this_frame
)
393 struct frame_info
*next_frame
;
394 int inline_count
= 0;
396 /* First count how many inlined functions at this PC have frames
397 above FRAME (are inlined into FRAME). */
398 for (next_frame
= get_next_frame (this_frame
);
399 next_frame
&& get_frame_type (next_frame
) == INLINE_FRAME
;
400 next_frame
= get_next_frame (next_frame
))
403 /* Simulate some most-inner inlined frames which were suppressed, so
404 they can be stepped into later. If we are unwinding already
405 outer frames from some non-inlined frame this does not apply. */
406 if (next_frame
== NULL
)
407 inline_count
+= inline_skipped_frames (inferior_ptid
);