GDB (xrefs)

/tmp/gdb-8.1/gdb/inline-frame.c
Go to the documentation of this file.
1 /* Inline frame unwinder for GDB.
2
3 Copyright (C) 2008-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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"
29 #include "frame.h"
30
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,
34 keep our own list. */
35 struct inline_state
36 {
37 /* The thread this data relates to. It should be a currently
38 stopped thread; we assume thread IDs never change while the
39 thread is stopped. */
40 ptid_t ptid;
41
42 /* The number of inlined functions we are skipping. Each of these
43 functions can be stepped in to. */
44 int skipped_frames;
45
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. */
50 CORE_ADDR saved_pc;
51
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;
56 };
57
58 typedef struct inline_state inline_state_s;
59 DEF_VEC_O(inline_state_s);
60
61 static VEC(inline_state_s) *inline_states;
62
63 /* Locate saved inlined frame state for PTID, if it exists
64 and is valid. */
65
66 static struct inline_state *
67 find_inline_frame_state (ptid_t ptid)
68 {
69 struct inline_state *state;
70 int ix;
71
72 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
73 {
74 if (ptid_equal (state->ptid, ptid))
75 {
76 struct regcache *regcache = get_thread_regcache (ptid);
77 CORE_ADDR current_pc = regcache_read_pc (regcache);
78
79 if (current_pc != state->saved_pc)
80 {
81 /* PC has changed - this context is invalid. Use the
82 default behavior. */
83 VEC_unordered_remove (inline_state_s, inline_states, ix);
84 return NULL;
85 }
86 else
87 return state;
88 }
89 }
90
91 return NULL;
92 }
93
94 /* Allocate saved inlined frame state for PTID. */
95
96 static struct inline_state *
97 allocate_inline_frame_state (ptid_t ptid)
98 {
99 struct inline_state *state;
100
101 state = VEC_safe_push (inline_state_s, inline_states, NULL);
102 memset (state, 0, sizeof (*state));
103 state->ptid = ptid;
104
105 return state;
106 }
107
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). */
111
112 void
113 clear_inline_frame_state (ptid_t ptid)
114 {
115 struct inline_state *state;
116 int ix;
117
118 if (ptid_equal (ptid, minus_one_ptid))
119 {
120 VEC_free (inline_state_s, inline_states);
121 return;
122 }
123
124 if (ptid_is_pid (ptid))
125 {
126 VEC (inline_state_s) *new_states = NULL;
127 int pid = ptid_get_pid (ptid);
128
129 for (ix = 0;
130 VEC_iterate (inline_state_s, inline_states, ix, state);
131 ix++)
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;
136 return;
137 }
138
139 for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
140 if (ptid_equal (state->ptid, ptid))
141 {
142 VEC_unordered_remove (inline_state_s, inline_states, ix);
143 return;
144 }
145 }
146
147 static void
148 inline_frame_this_id (struct frame_info *this_frame,
149 void **this_cache,
150 struct frame_id *this_id)
151 {
152 struct symbol *func;
153
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));
161
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));
168
169 /* For now, require we don't match outer_frame_id either (see
170 comment above). */
171 gdb_assert (!frame_id_eq (*this_id, outer_frame_id));
172
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++;
181 }
182
183 static struct value *
184 inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
185 int regnum)
186 {
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.
193
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);
199 }
200
201 /* Check whether we are at an inlining site that does not already
202 have an associated frame. */
203
204 static int
205 inline_frame_sniffer (const struct frame_unwind *self,
206 struct frame_info *this_frame,
207 void **this_cache)
208 {
209 CORE_ADDR this_pc;
210 const struct block *frame_block, *cur_block;
211 int depth;
212 struct frame_info *next_frame;
213 struct inline_state *state = find_inline_frame_state (inferior_ptid);
214
215 this_pc = get_frame_address_in_block (this_frame);
216 frame_block = block_for_pc (this_pc);
217 if (frame_block == NULL)
218 return 0;
219
220 /* Calculate DEPTH, the number of inlined functions at this
221 location. */
222 depth = 0;
223 cur_block = frame_block;
224 while (BLOCK_SUPERBLOCK (cur_block))
225 {
226 if (block_inlined_p (cur_block))
227 depth++;
228 else if (BLOCK_FUNCTION (cur_block) != NULL)
229 break;
230
231 cur_block = BLOCK_SUPERBLOCK (cur_block);
232 }
233
234 /* Check how many inlined functions already have frames. */
235 for (next_frame = get_next_frame (this_frame);
236 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
237 next_frame = get_next_frame (next_frame))
238 {
239 gdb_assert (depth > 0);
240 depth--;
241 }
242
243 /* If this is the topmost frame, or all frames above us are inlined,
244 then check whether we were requested to skip some frames (so they
245 can be stepped into later). */
246 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
247 {
248 gdb_assert (depth >= state->skipped_frames);
249 depth -= state->skipped_frames;
250 }
251
252 /* If all the inlined functions here already have frames, then pass
253 to the normal unwinder for this PC. */
254 if (depth == 0)
255 return 0;
256
257 /* If the next frame is an inlined function, but not the outermost, then
258 we are the next outer. If it is not an inlined function, then we
259 are the innermost inlined function of a different real frame. */
260 return 1;
261 }
262
263 const struct frame_unwind inline_frame_unwind = {
264 INLINE_FRAME,
265 default_frame_unwind_stop_reason,
266 inline_frame_this_id,
267 inline_frame_prev_register,
268 NULL,
269 inline_frame_sniffer
270 };
271
272 /* Return non-zero if BLOCK, an inlined function block containing PC,
273 has a group of contiguous instructions starting at PC (but not
274 before it). */
275
276 static int
277 block_starting_point_at (CORE_ADDR pc, const struct block *block)
278 {
279 const struct blockvector *bv;
280 struct block *new_block;
281
282 bv = blockvector_for_pc (pc, NULL);
283 if (BLOCKVECTOR_MAP (bv) == NULL)
284 return 0;
285
286 new_block = (struct block *) addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
287 if (new_block == NULL)
288 return 1;
289
290 if (new_block == block || contained_in (new_block, block))
291 return 0;
292
293 /* The immediately preceding address belongs to a different block,
294 which is not a child of this one. Treat this as an entrance into
295 BLOCK. */
296 return 1;
297 }
298
299 /* Skip all inlined functions whose call sites are at the current PC.
300 Frames for the hidden functions will not appear in the backtrace until the
301 user steps into them. */
302
303 void
304 skip_inline_frames (ptid_t ptid)
305 {
306 CORE_ADDR this_pc;
307 const struct block *frame_block, *cur_block;
308 struct symbol *last_sym = NULL;
309 int skip_count = 0;
310 struct inline_state *state;
311
312 /* This function is called right after reinitializing the frame
313 cache. We try not to do more unwinding than absolutely
314 necessary, for performance. */
315 this_pc = get_frame_pc (get_current_frame ());
316 frame_block = block_for_pc (this_pc);
317
318 if (frame_block != NULL)
319 {
320 cur_block = frame_block;
321 while (BLOCK_SUPERBLOCK (cur_block))
322 {
323 if (block_inlined_p (cur_block))
324 {
325 /* See comments in inline_frame_this_id about this use
326 of BLOCK_START. */
327 if (BLOCK_START (cur_block) == this_pc
328 || block_starting_point_at (this_pc, cur_block))
329 {
330 skip_count++;
331 last_sym = BLOCK_FUNCTION (cur_block);
332 }
333 else
334 break;
335 }
336 else if (BLOCK_FUNCTION (cur_block) != NULL)
337 break;
338
339 cur_block = BLOCK_SUPERBLOCK (cur_block);
340 }
341 }
342
343 gdb_assert (find_inline_frame_state (ptid) == NULL);
344 state = allocate_inline_frame_state (ptid);
345 state->skipped_frames = skip_count;
346 state->saved_pc = this_pc;
347 state->skipped_symbol = last_sym;
348
349 if (skip_count != 0)
350 reinit_frame_cache ();
351 }
352
353 /* Step into an inlined function by unhiding it. */
354
355 void
356 step_into_inline_frame (ptid_t ptid)
357 {
358 struct inline_state *state = find_inline_frame_state (ptid);
359
360 gdb_assert (state != NULL && state->skipped_frames > 0);
361 state->skipped_frames--;
362 reinit_frame_cache ();
363 }
364
365 /* Return the number of hidden functions inlined into the current
366 frame. */
367
368 int
369 inline_skipped_frames (ptid_t ptid)
370 {
371 struct inline_state *state = find_inline_frame_state (ptid);
372
373 if (state == NULL)
374 return 0;
375 else
376 return state->skipped_frames;
377 }
378
379 /* If one or more inlined functions are hidden, return the symbol for
380 the function inlined into the current frame. */
381
382 struct symbol *
383 inline_skipped_symbol (ptid_t ptid)
384 {
385 struct inline_state *state = find_inline_frame_state (ptid);
386
387 gdb_assert (state != NULL);
388 return state->skipped_symbol;
389 }
390
391 /* Return the number of functions inlined into THIS_FRAME. Some of
392 the callees may not have associated frames (see
393 skip_inline_frames). */
394
395 int
396 frame_inlined_callees (struct frame_info *this_frame)
397 {
398 struct frame_info *next_frame;
399 int inline_count = 0;
400
401 /* First count how many inlined functions at this PC have frames
402 above FRAME (are inlined into FRAME). */
403 for (next_frame = get_next_frame (this_frame);
404 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
405 next_frame = get_next_frame (next_frame))
406 inline_count++;
407
408 /* Simulate some most-inner inlined frames which were suppressed, so
409 they can be stepped into later. If we are unwinding already
410 outer frames from some non-inlined frame this does not apply. */
411 if (next_frame == NULL)
412 inline_count += inline_skipped_frames (inferior_ptid);
413
414 return inline_count;
415 }
frame_id_p
int frame_id_p(struct frame_id l)
Definition: frame.c:646
block_starting_point_at
static int block_starting_point_at(CORE_ADDR pc, const struct block *block)
Definition: inline-frame.c:277
get_frame_address_in_block
CORE_ADDR get_frame_address_in_block(struct frame_info *this_frame)
Definition: frame.c:2407
ptid_is_pid
int ptid_is_pid(const ptid_t &ptid)
Definition: ptid.c:79
get_frame_pc
CORE_ADDR get_frame_pc(struct frame_info *frame)
Definition: frame.c:2376
get_current_frame
struct frame_info * get_current_frame(void)
Definition: frame.c:1563
clear_inline_frame_state
void clear_inline_frame_state(ptid_t ptid)
Definition: inline-frame.c:113
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
ptid_get_pid
int ptid_get_pid(const ptid_t &ptid)
Definition: ptid.c:47
get_thread_regcache
struct regcache * get_thread_regcache(ptid_t ptid)
Definition: regcache.c:434
pid
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1824
func
void(* func)(char *)
Definition: remote-fileio.c:1104
memset
void * memset(T *s, int c, size_t n)=delete
VEC_unordered_remove
#define VEC_unordered_remove(T, V, I)
Definition: vec.h:367
frame_info
Definition: frame.c:85
VEC_safe_push
#define VEC_safe_push(T, V, O)
Definition: vec.h:276
inline_state::saved_pc
CORE_ADDR saved_pc
Definition: inline-frame.c:50
defs.h
BLOCK_START
#define BLOCK_START(bl)
Definition: block.h:105
block_for_pc
const struct block * block_for_pc(CORE_ADDR pc)
Definition: block.c:282
BLOCK_FUNCTION
#define BLOCK_FUNCTION(bl)
Definition: block.h:107
symtab.h
frame_id_eq
int frame_id_eq(struct frame_id l, struct frame_id r)
Definition: frame.c:674
get_frame_type
enum frame_type get_frame_type(struct frame_info *frame)
Definition: frame.c:2619
VEC_iterate
#define VEC_iterate(T, V, I, P)
Definition: vec.h:181
get_frame_register_value
struct value * get_frame_register_value(struct frame_info *frame, int regnum)
Definition: frame.c:1238
inline-frame.h
get_frame_id
struct frame_id get_frame_id(struct frame_info *fi)
Definition: frame.c:520
allocate_inline_frame_state
static struct inline_state * allocate_inline_frame_state(ptid_t ptid)
Definition: inline-frame.c:97
contained_in
int contained_in(const struct block *a, const struct block *b)
Definition: block.c:73
inline_state
Definition: inline-frame.c:35
inline_skipped_symbol
struct symbol * inline_skipped_symbol(ptid_t ptid)
Definition: inline-frame.c:383
inline_skipped_frames
int inline_skipped_frames(ptid_t ptid)
Definition: inline-frame.c:369
regcache.h
ptid_t
Definition: ptid.h:35
frame.h
get_frame_function
struct symbol * get_frame_function(struct frame_info *frame)
Definition: blockframe.c:118
inferior.h
inline_frame_unwind
const struct frame_unwind inline_frame_unwind
Definition: inline-frame.c:263
BLOCK_SUPERBLOCK
#define BLOCK_SUPERBLOCK(bl)
Definition: block.h:108
frame-unwind.h
get_prev_frame_always
struct frame_info * get_prev_frame_always(struct frame_info *this_frame)
Definition: frame.c:2097
inline_frame_sniffer
static int inline_frame_sniffer(const struct frame_unwind *self, struct frame_info *this_frame, void **this_cache)
Definition: inline-frame.c:205
inline_frame_this_id
static void inline_frame_this_id(struct frame_info *this_frame, void **this_cache, struct frame_id *this_id)
Definition: inline-frame.c:148
regnum
int regnum
Definition: aarch64-tdep.c:77
skip_inline_frames
void skip_inline_frames(ptid_t ptid)
Definition: inline-frame.c:304
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
block
Definition: block.h:60
value
Definition: value.c:169
blockvector
Definition: block.h:112
addrmap.h
VEC
static VEC(inline_state_s)
Definition: inline-frame.c:61
frame_inlined_callees
int frame_inlined_callees(struct frame_info *this_frame)
Definition: inline-frame.c:396
DEF_VEC_O
DEF_VEC_O(inline_state_s)
get_next_frame
struct frame_info * get_next_frame(struct frame_info *this_frame)
Definition: frame.c:1771
block_inlined_p
int block_inlined_p(const struct block *bl)
Definition: block.c:126
INLINE_FRAME
Definition: frame.h:250
SYMBOL_BLOCK_VALUE
#define SYMBOL_BLOCK_VALUE(symbol)
Definition: symtab.h:467
regcache_read_pc
CORE_ADDR regcache_read_pc(struct regcache *regcache)
Definition: regcache.c:1229
inferior_ptid
ptid_t inferior_ptid
Definition: infcmd.c:94
inline_state::skipped_symbol
struct symbol * skipped_symbol
Definition: inline-frame.c:55
VEC_free
#define VEC_free(T, V)
Definition: vec.h:196
frame_unwind
Definition: frame-unwind.h:144
inline_state::ptid
ptid_t ptid
Definition: inline-frame.c:40
inline_state::skipped_frames
int skipped_frames
Definition: inline-frame.c:44
ptid_equal
int ptid_equal(const ptid_t &ptid1, const ptid_t &ptid2)
Definition: ptid.c:71
default_frame_unwind_stop_reason
enum unwind_stop_reason default_frame_unwind_stop_reason(struct frame_info *this_frame, void **this_cache)
Definition: frame-unwind.c:184
outer_frame_id
const struct frame_id outer_frame_id
Definition: frame.c:577
regcache
Definition: regcache.h:232
inline_frame_prev_register
static struct value * inline_frame_prev_register(struct frame_info *this_frame, void **this_cache, int regnum)
Definition: inline-frame.c:184
symbol
Definition: symtab.h:1049
addrmap_find
void * addrmap_find(struct addrmap *map, CORE_ADDR addr)
Definition: addrmap.c:59
BLOCKVECTOR_MAP
#define BLOCKVECTOR_MAP(blocklist)
Definition: block.h:126
reinit_frame_cache
void reinit_frame_cache(void)
Definition: frame.c:1809
regcache::ptid
ptid_t ptid() const
Definition: regcache.h:325
blockvector_for_pc
const struct blockvector * blockvector_for_pc(CORE_ADDR pc, const struct block **pblock)
Definition: block.c:257
minus_one_ptid
ptid_t minus_one_ptid
Definition: ptid.c:26
frame_id
Definition: frame.h:111
block.h
vec.h
step_into_inline_frame
void step_into_inline_frame(ptid_t ptid)
Definition: inline-frame.c:356
new_block
static struct block * new_block(enum block_type, enum language)
Definition: mdebugread.c:4821