ncurses: Add few missing dependencies.
[dragonfly.git] / contrib / gdb-7 / gdb / inline-frame.c
blob2773814a138b056f4f29e400aae9853bd7fd3d50
1 /* Inline frame unwinder for GDB.
3 Copyright (C) 2008-2013 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 "defs.h"
21 #include "inline-frame.h"
22 #include "addrmap.h"
23 #include "block.h"
24 #include "frame-unwind.h"
25 #include "inferior.h"
26 #include "regcache.h"
27 #include "symtab.h"
28 #include "vec.h"
30 #include "gdb_assert.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,
35 keep our own list. */
36 struct inline_state
38 /* The thread this data relates to. It should be a currently
39 stopped thread; we assume thread IDs never change while the
40 thread is stopped. */
41 ptid_t ptid;
43 /* The number of inlined functions we are skipping. Each of these
44 functions can be stepped in to. */
45 int skipped_frames;
47 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
48 when calculating SKIPPED_FRAMES; used to check whether we have
49 moved to a new location by user request. If so, we invalidate
50 any skipped frames. */
51 CORE_ADDR saved_pc;
53 /* Only valid if SKIPPED_FRAMES is non-zero. This is the symbol
54 of the outermost skipped inline function. It's used to find the
55 call site of the current frame. */
56 struct symbol *skipped_symbol;
59 typedef struct inline_state inline_state_s;
60 DEF_VEC_O(inline_state_s);
62 static VEC(inline_state_s) *inline_states;
64 /* Locate saved inlined frame state for PTID, if it exists
65 and is valid. */
67 static struct inline_state *
68 find_inline_frame_state (ptid_t ptid)
70 struct inline_state *state;
71 int ix;
73 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
75 if (ptid_equal (state->ptid, ptid))
77 struct regcache *regcache = get_thread_regcache (ptid);
78 CORE_ADDR current_pc = regcache_read_pc (regcache);
80 if (current_pc != state->saved_pc)
82 /* PC has changed - this context is invalid. Use the
83 default behavior. */
84 VEC_unordered_remove (inline_state_s, inline_states, ix);
85 return NULL;
87 else
88 return state;
92 return NULL;
95 /* Allocate saved inlined frame state for PTID. */
97 static struct inline_state *
98 allocate_inline_frame_state (ptid_t ptid)
100 struct inline_state *state;
102 state = VEC_safe_push (inline_state_s, inline_states, NULL);
103 memset (state, 0, sizeof (*state));
104 state->ptid = ptid;
106 return state;
109 /* Forget about any hidden inlined functions in PTID, which is new or
110 about to be resumed. PTID may be minus_one_ptid (all processes)
111 or a PID (all threads in this process). */
113 void
114 clear_inline_frame_state (ptid_t ptid)
116 struct inline_state *state;
117 int ix;
119 if (ptid_equal (ptid, minus_one_ptid))
121 VEC_free (inline_state_s, inline_states);
122 return;
125 if (ptid_is_pid (ptid))
127 VEC (inline_state_s) *new_states = NULL;
128 int pid = ptid_get_pid (ptid);
130 for (ix = 0;
131 VEC_iterate (inline_state_s, inline_states, ix, state);
132 ix++)
133 if (pid != ptid_get_pid (state->ptid))
134 VEC_safe_push (inline_state_s, new_states, state);
135 VEC_free (inline_state_s, inline_states);
136 inline_states = new_states;
137 return;
140 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
141 if (ptid_equal (state->ptid, ptid))
143 VEC_unordered_remove (inline_state_s, inline_states, ix);
144 return;
148 static void
149 inline_frame_this_id (struct frame_info *this_frame,
150 void **this_cache,
151 struct frame_id *this_id)
153 struct symbol *func;
155 /* In order to have a stable frame ID for a given inline function,
156 we must get the stack / special addresses from the underlying
157 real frame's this_id method. So we must call get_prev_frame.
158 Because we are inlined into some function, there must be previous
159 frames, so this is safe - as long as we're careful not to
160 create any cycles. */
161 *this_id = get_frame_id (get_prev_frame (this_frame));
163 /* We need a valid frame ID, so we need to be based on a valid
164 frame. FSF submission NOTE: this would be a good assertion to
165 apply to all frames, all the time. That would fix the ambiguity
166 of null_frame_id (between "no/any frame" and "the outermost
167 frame"). This will take work. */
168 gdb_assert (frame_id_p (*this_id));
170 /* For now, require we don't match outer_frame_id either (see
171 comment above). */
172 gdb_assert (!frame_id_eq (*this_id, outer_frame_id));
174 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
175 which generates DW_AT_entry_pc for inlined functions when
176 possible. If this attribute is available, we should use it
177 in the frame ID (and eventually, to set breakpoints). */
178 func = get_frame_function (this_frame);
179 gdb_assert (func != NULL);
180 (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
181 (*this_id).artificial_depth++;
184 static struct value *
185 inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
186 int regnum)
188 /* Use get_frame_register_value instead of
189 frame_unwind_got_register, to avoid requiring this frame's ID.
190 This frame's ID depends on the previous frame's ID (unusual), and
191 the previous frame's ID depends on this frame's unwound
192 registers. If unwinding registers from this frame called
193 get_frame_id, there would be a loop.
195 Do not copy this code into any other unwinder! Inlined functions
196 are special; other unwinders must not have a dependency on the
197 previous frame's ID, and therefore can and should use
198 frame_unwind_got_register instead. */
199 return get_frame_register_value (this_frame, regnum);
202 /* Check whether we are at an inlining site that does not already
203 have an associated frame. */
205 static int
206 inline_frame_sniffer (const struct frame_unwind *self,
207 struct frame_info *this_frame,
208 void **this_cache)
210 CORE_ADDR this_pc;
211 struct block *frame_block, *cur_block;
212 int depth;
213 struct frame_info *next_frame;
214 struct inline_state *state = find_inline_frame_state (inferior_ptid);
216 this_pc = get_frame_address_in_block (this_frame);
217 frame_block = block_for_pc (this_pc);
218 if (frame_block == NULL)
219 return 0;
221 /* Calculate DEPTH, the number of inlined functions at this
222 location. */
223 depth = 0;
224 cur_block = frame_block;
225 while (BLOCK_SUPERBLOCK (cur_block))
227 if (block_inlined_p (cur_block))
228 depth++;
230 cur_block = BLOCK_SUPERBLOCK (cur_block);
233 /* Check how many inlined functions already have frames. */
234 for (next_frame = get_next_frame (this_frame);
235 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
236 next_frame = get_next_frame (next_frame))
238 gdb_assert (depth > 0);
239 depth--;
242 /* If this is the topmost frame, or all frames above us are inlined,
243 then check whether we were requested to skip some frames (so they
244 can be stepped into later). */
245 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
247 gdb_assert (depth >= state->skipped_frames);
248 depth -= state->skipped_frames;
251 /* If all the inlined functions here already have frames, then pass
252 to the normal unwinder for this PC. */
253 if (depth == 0)
254 return 0;
256 /* If the next frame is an inlined function, but not the outermost, then
257 we are the next outer. If it is not an inlined function, then we
258 are the innermost inlined function of a different real frame. */
259 return 1;
262 const struct frame_unwind inline_frame_unwind = {
263 INLINE_FRAME,
264 default_frame_unwind_stop_reason,
265 inline_frame_this_id,
266 inline_frame_prev_register,
267 NULL,
268 inline_frame_sniffer
271 /* Return non-zero if BLOCK, an inlined function block containing PC,
272 has a group of contiguous instructions starting at PC (but not
273 before it). */
275 static int
276 block_starting_point_at (CORE_ADDR pc, struct block *block)
278 struct blockvector *bv;
279 struct block *new_block;
281 bv = blockvector_for_pc (pc, NULL);
282 if (BLOCKVECTOR_MAP (bv) == NULL)
283 return 0;
285 new_block = addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
286 if (new_block == NULL)
287 return 1;
289 if (new_block == block || contained_in (new_block, block))
290 return 0;
292 /* The immediately preceding address belongs to a different block,
293 which is not a child of this one. Treat this as an entrance into
294 BLOCK. */
295 return 1;
298 /* Skip all inlined functions whose call sites are at the current PC.
299 Frames for the hidden functions will not appear in the backtrace until the
300 user steps into them. */
302 void
303 skip_inline_frames (ptid_t ptid)
305 CORE_ADDR this_pc;
306 struct block *frame_block, *cur_block;
307 struct symbol *last_sym = NULL;
308 int skip_count = 0;
309 struct inline_state *state;
311 /* This function is called right after reinitializing the frame
312 cache. We try not to do more unwinding than absolutely
313 necessary, for performance. */
314 this_pc = get_frame_pc (get_current_frame ());
315 frame_block = block_for_pc (this_pc);
317 if (frame_block != NULL)
319 cur_block = frame_block;
320 while (BLOCK_SUPERBLOCK (cur_block))
322 if (block_inlined_p (cur_block))
324 /* See comments in inline_frame_this_id about this use
325 of BLOCK_START. */
326 if (BLOCK_START (cur_block) == this_pc
327 || block_starting_point_at (this_pc, cur_block))
329 skip_count++;
330 last_sym = BLOCK_FUNCTION (cur_block);
332 else
333 break;
335 cur_block = BLOCK_SUPERBLOCK (cur_block);
339 gdb_assert (find_inline_frame_state (ptid) == NULL);
340 state = allocate_inline_frame_state (ptid);
341 state->skipped_frames = skip_count;
342 state->saved_pc = this_pc;
343 state->skipped_symbol = last_sym;
345 if (skip_count != 0)
346 reinit_frame_cache ();
349 /* Step into an inlined function by unhiding it. */
351 void
352 step_into_inline_frame (ptid_t ptid)
354 struct inline_state *state = find_inline_frame_state (ptid);
356 gdb_assert (state != NULL && state->skipped_frames > 0);
357 state->skipped_frames--;
358 reinit_frame_cache ();
361 /* Return the number of hidden functions inlined into the current
362 frame. */
365 inline_skipped_frames (ptid_t ptid)
367 struct inline_state *state = find_inline_frame_state (ptid);
369 if (state == NULL)
370 return 0;
371 else
372 return state->skipped_frames;
375 /* If one or more inlined functions are hidden, return the symbol for
376 the function inlined into the current frame. */
378 struct symbol *
379 inline_skipped_symbol (ptid_t ptid)
381 struct inline_state *state = find_inline_frame_state (ptid);
383 gdb_assert (state != NULL);
384 return state->skipped_symbol;
387 /* Return the number of functions inlined into THIS_FRAME. Some of
388 the callees may not have associated frames (see
389 skip_inline_frames). */
392 frame_inlined_callees (struct frame_info *this_frame)
394 struct frame_info *next_frame;
395 int inline_count = 0;
397 /* First count how many inlined functions at this PC have frames
398 above FRAME (are inlined into FRAME). */
399 for (next_frame = get_next_frame (this_frame);
400 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
401 next_frame = get_next_frame (next_frame))
402 inline_count++;
404 /* Simulate some most-inner inlined frames which were suppressed, so
405 they can be stepped into later. If we are unwinding already
406 outer frames from some non-inlined frame this does not apply. */
407 if (next_frame == NULL)
408 inline_count += inline_skipped_frames (inferior_ptid);
410 return inline_count;