1 /* Inline frame unwinder for GDB.
3 Copyright (C) 2008 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/>. */
23 #include "frame-unwind.h"
28 #include "gdb_assert.h"
30 /* We need to save a few variables for every thread stopped at the
31 virtual call site of an inlined function. If there was always a
32 "struct thread_info", we could hang it off that; in the mean time,
36 /* The thread this data relates to. It should be a currently
37 stopped thread; we assume thread IDs never change while the
41 /* The number of inlined functions we are skipping. Each of these
42 functions can be stepped in to. */
45 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
46 when calculating SKIPPED_FRAMES; used to check whether we have
47 moved to a new location by user request. If so, we invalidate
48 any skipped frames. */
51 /* Only valid if SKIPPED_FRAMES is non-zero. This is the symbol
52 of the outermost skipped inline function. It's used to find the
53 call site of the current frame. */
54 struct symbol *skipped_symbol;
57 typedef struct inline_state inline_state_s;
58 DEF_VEC_O(inline_state_s);
60 static VEC(inline_state_s) *inline_states;
62 /* Locate saved inlined frame state for PTID, if it exists. */
64 static struct inline_state *
65 find_inline_frame_state (ptid_t ptid)
67 struct inline_state *state;
70 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
72 if (ptid_equal (state->ptid, ptid))
79 /* Allocate saved inlined frame state for PTID. */
81 static struct inline_state *
82 allocate_inline_frame_state (ptid_t ptid)
84 struct inline_state *state;
86 state = VEC_safe_push (inline_state_s, inline_states, NULL);
87 memset (state, 0, sizeof (*state));
93 /* Forget about any hidden inlined functions in PTID, which is new or
94 about to be resumed. PTID may be minus_one_ptid (all processes)
95 or a PID (all threads in this process). */
98 clear_inline_frame_state (ptid_t ptid)
100 struct inline_state *state;
103 if (ptid_equal (ptid, minus_one_ptid))
105 VEC_free (inline_state_s, inline_states);
109 if (ptid_is_pid (ptid))
111 VEC (inline_state_s) *new_states = NULL;
112 int pid = ptid_get_pid (ptid);
113 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
114 if (pid != ptid_get_pid (state->ptid))
115 VEC_safe_push (inline_state_s, new_states, state);
116 VEC_free (inline_state_s, inline_states);
117 inline_states = new_states;
121 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
122 if (ptid_equal (state->ptid, ptid))
124 VEC_unordered_remove (inline_state_s, inline_states, ix);
130 inline_frame_this_id (struct frame_info *this_frame,
132 struct frame_id *this_id)
136 /* In order to have a stable frame ID for a given inline function,
137 we must get the stack / special addresses from the underlying
138 real frame's this_id method. So we must call get_prev_frame.
139 Because we are inlined into some function, there must be previous
140 frames, so this is safe - as long as we're careful not to
141 create any cycles. */
142 *this_id = get_frame_id (get_prev_frame (this_frame));
144 /* We need a valid frame ID, so we need to be based on a valid
145 frame. FSF submission NOTE: this would be a good assertion to
146 apply to all frames, all the time. That would fix the ambiguity
147 of null_frame_id (between "no/any frame" and "the outermost
148 frame"). This will take work. */
149 gdb_assert (frame_id_p (*this_id));
151 /* For now, require we don't match outer_frame_id either (see
153 gdb_assert (!frame_id_eq (*this_id, outer_frame_id));
155 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
156 which generates DW_AT_entry_pc for inlined functions when
157 possible. If this attribute is available, we should use it
158 in the frame ID (and eventually, to set breakpoints). */
159 func = get_frame_function (this_frame);
160 gdb_assert (func != NULL);
161 (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
162 (*this_id).inline_depth++;
165 static struct value *
166 inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
169 /* Use get_frame_register_value instead of
170 frame_unwind_got_register, to avoid requiring this frame's ID.
171 This frame's ID depends on the previous frame's ID (unusual), and
172 the previous frame's ID depends on this frame's unwound
173 registers. If unwinding registers from this frame called
174 get_frame_id, there would be a loop.
176 Do not copy this code into any other unwinder! Inlined functions
177 are special; other unwinders must not have a dependency on the
178 previous frame's ID, and therefore can and should use
179 frame_unwind_got_register instead. */
180 return get_frame_register_value (this_frame, regnum);
183 /* Check whether we are at an inlining site that does not already
184 have an associated frame. */
187 inline_frame_sniffer (const struct frame_unwind *self,
188 struct frame_info *this_frame,
192 struct block *frame_block, *cur_block;
194 struct frame_info *next_frame;
195 struct inline_state *state = find_inline_frame_state (inferior_ptid);
197 this_pc = get_frame_address_in_block (this_frame);
198 frame_block = block_for_pc (this_pc);
199 if (frame_block == NULL)
202 /* Calculate DEPTH, the number of inlined functions at this
205 cur_block = frame_block;
206 while (BLOCK_SUPERBLOCK (cur_block))
208 if (block_inlined_p (cur_block))
211 cur_block = BLOCK_SUPERBLOCK (cur_block);
214 /* Check how many inlined functions already have frames. */
215 for (next_frame = get_next_frame (this_frame);
216 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
217 next_frame = get_next_frame (next_frame))
219 gdb_assert (depth > 0);
223 /* If this is the topmost frame, or all frames above us are inlined,
224 then check whether we were requested to skip some frames (so they
225 can be stepped into later). */
226 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
228 if (this_pc != state->saved_pc)
229 state->skipped_frames = 0;
232 gdb_assert (depth >= state->skipped_frames);
233 depth -= state->skipped_frames;
237 /* If all the inlined functions here already have frames, then pass
238 to the normal unwinder for this PC. */
242 /* If the next frame is an inlined function, but not the outermost, then
243 we are the next outer. If it is not an inlined function, then we
244 are the innermost inlined function of a different real frame. */
248 const struct frame_unwind inline_frame_unwinder = {
250 inline_frame_this_id,
251 inline_frame_prev_register,
256 const struct frame_unwind *const inline_frame_unwind = &inline_frame_unwinder;
258 /* Return non-zero if BLOCK, an inlined function block containing PC,
259 has a group of contiguous instructions starting at PC (but not
263 block_starting_point_at (CORE_ADDR pc, struct block *block)
265 struct blockvector *bv;
266 struct block *new_block;
268 bv = blockvector_for_pc (pc, NULL);
269 if (BLOCKVECTOR_MAP (bv) == NULL)
272 new_block = addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
273 if (new_block == NULL)
276 if (new_block == block || contained_in (new_block, block))
279 /* The immediately preceeding address belongs to a different block,
280 which is not a child of this one. Treat this as an entrance into
285 /* Skip all inlined functions whose call sites are at the current PC.
286 Frames for the hidden functions will not appear in the backtrace until the
287 user steps into them. */
290 skip_inline_frames (ptid_t ptid)
293 struct block *frame_block, *cur_block;
294 struct symbol *last_sym = NULL;
296 struct inline_state *state;
298 /* This function is called right after reinitializing the frame
299 cache. We try not to do more unwinding than absolutely
300 necessary, for performance. */
301 this_pc = get_frame_pc (get_current_frame ());
302 frame_block = block_for_pc (this_pc);
304 if (frame_block != NULL)
306 cur_block = frame_block;
307 while (BLOCK_SUPERBLOCK (cur_block))
309 if (block_inlined_p (cur_block))
311 /* See comments in inline_frame_this_id about this use
313 if (BLOCK_START (cur_block) == this_pc
314 || block_starting_point_at (this_pc, cur_block))
317 last_sym = BLOCK_FUNCTION (cur_block);
322 cur_block = BLOCK_SUPERBLOCK (cur_block);
326 gdb_assert (find_inline_frame_state (ptid) == NULL);
327 state = allocate_inline_frame_state (ptid);
328 state->skipped_frames = skip_count;
329 state->saved_pc = this_pc;
330 state->skipped_symbol = last_sym;
333 reinit_frame_cache ();
336 /* Step into an inlined function by unhiding it. */
339 step_into_inline_frame (ptid_t ptid)
341 struct inline_state *state = find_inline_frame_state (ptid);
343 gdb_assert (state != NULL && state->skipped_frames > 0);
344 state->skipped_frames--;
345 reinit_frame_cache ();
348 /* Return the number of hidden functions inlined into the current
352 inline_skipped_frames (ptid_t ptid)
354 struct inline_state *state = find_inline_frame_state (ptid);
359 return state->skipped_frames;
362 /* If one or more inlined functions are hidden, return the symbol for
363 the function inlined into the current frame. */
366 inline_skipped_symbol (ptid_t ptid)
368 struct inline_state *state = find_inline_frame_state (ptid);
370 gdb_assert (state != NULL);
371 return state->skipped_symbol;
374 /* Return the number of functions inlined into THIS_FRAME. Some of
375 the callees may not have associated frames (see
376 skip_inline_frames). */
379 frame_inlined_callees (struct frame_info *this_frame)
381 struct frame_info *next_frame;
382 int inline_count = 0;
384 /* First count how many inlined functions at this PC have frames
385 above FRAME (are inlined into FRAME). */
386 for (next_frame = get_next_frame (this_frame);
387 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
388 next_frame = get_next_frame (next_frame))
391 /* Simulate some most-inner inlined frames which were suppressed, so
392 they can be stepped into later. If we are unwinding already
393 outer frames from some non-inlined frame this does not apply. */
394 if (next_frame == NULL)
395 inline_count += inline_skipped_frames (inferior_ptid);