]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inline-frame.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / inline-frame.c
1 /* Inline frame unwinder for GDB.
2
3 Copyright (C) 2008-2021 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 "breakpoint.h"
22 #include "inline-frame.h"
23 #include "addrmap.h"
24 #include "block.h"
25 #include "frame-unwind.h"
26 #include "inferior.h"
27 #include "gdbthread.h"
28 #include "regcache.h"
29 #include "symtab.h"
30 #include "frame.h"
31 #include <algorithm>
32
33 /* We need to save a few variables for every thread stopped at the
34 virtual call site of an inlined function. If there was always a
35 "struct thread_info", we could hang it off that; in the mean time,
36 keep our own list. */
37 struct inline_state
38 {
39 inline_state (thread_info *thread_, int skipped_frames_, CORE_ADDR saved_pc_,
40 std::vector<symbol *> &&skipped_symbols_)
41 : thread (thread_), skipped_frames (skipped_frames_), saved_pc (saved_pc_),
42 skipped_symbols (std::move (skipped_symbols_))
43 {}
44
45 /* The thread this data relates to. It should be a currently
46 stopped thread. */
47 thread_info *thread;
48
49 /* The number of inlined functions we are skipping. Each of these
50 functions can be stepped in to. */
51 int skipped_frames;
52
53 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
54 when calculating SKIPPED_FRAMES; used to check whether we have
55 moved to a new location by user request. If so, we invalidate
56 any skipped frames. */
57 CORE_ADDR saved_pc;
58
59 /* Only valid if SKIPPED_FRAMES is non-zero. This is the list of all
60 function symbols that have been skipped, from inner most to outer
61 most. It is used to find the call site of the current frame. */
62 std::vector<struct symbol *> skipped_symbols;
63 };
64
65 static std::vector<inline_state> inline_states;
66
67 /* Locate saved inlined frame state for THREAD, if it exists and is
68 valid. */
69
70 static struct inline_state *
71 find_inline_frame_state (thread_info *thread)
72 {
73 auto state_it = std::find_if (inline_states.begin (), inline_states.end (),
74 [thread] (const inline_state &state)
75 {
76 return state.thread == thread;
77 });
78
79 if (state_it == inline_states.end ())
80 return nullptr;
81
82 inline_state &state = *state_it;
83 struct regcache *regcache = get_thread_regcache (thread);
84 CORE_ADDR current_pc = regcache_read_pc (regcache);
85
86 if (current_pc != state.saved_pc)
87 {
88 /* PC has changed - this context is invalid. Use the
89 default behavior. */
90
91 unordered_remove (inline_states, state_it);
92 return nullptr;
93 }
94
95 return &state;
96 }
97
98 /* See inline-frame.h. */
99
100 void
101 clear_inline_frame_state (process_stratum_target *target, ptid_t filter_ptid)
102 {
103 gdb_assert (target != NULL);
104
105 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
106 {
107 auto matcher = [target, &filter_ptid] (const inline_state &state)
108 {
109 thread_info *t = state.thread;
110 return (t->inf->process_target () == target
111 && t->ptid.matches (filter_ptid));
112 };
113
114 auto it = std::remove_if (inline_states.begin (), inline_states.end (),
115 matcher);
116
117 inline_states.erase (it, inline_states.end ());
118
119 return;
120 }
121
122
123 auto matcher = [target, &filter_ptid] (const inline_state &state)
124 {
125 thread_info *t = state.thread;
126 return (t->inf->process_target () == target
127 && filter_ptid == t->ptid);
128 };
129
130 auto it = std::find_if (inline_states.begin (), inline_states.end (),
131 matcher);
132
133 if (it != inline_states.end ())
134 unordered_remove (inline_states, it);
135 }
136
137 /* See inline-frame.h. */
138
139 void
140 clear_inline_frame_state (thread_info *thread)
141 {
142 auto it = std::find_if (inline_states.begin (), inline_states.end (),
143 [thread] (const inline_state &state)
144 {
145 return thread == state.thread;
146 });
147
148 if (it != inline_states.end ())
149 unordered_remove (inline_states, it);
150 }
151
152 static void
153 inline_frame_this_id (struct frame_info *this_frame,
154 void **this_cache,
155 struct frame_id *this_id)
156 {
157 struct symbol *func;
158
159 /* In order to have a stable frame ID for a given inline function,
160 we must get the stack / special addresses from the underlying
161 real frame's this_id method. So we must call
162 get_prev_frame_always. Because we are inlined into some
163 function, there must be previous frames, so this is safe - as
164 long as we're careful not to create any cycles. See related
165 comments in get_prev_frame_always_1. */
166 *this_id = get_frame_id (get_prev_frame_always (this_frame));
167
168 /* We need a valid frame ID, so we need to be based on a valid
169 frame. FSF submission NOTE: this would be a good assertion to
170 apply to all frames, all the time. That would fix the ambiguity
171 of null_frame_id (between "no/any frame" and "the outermost
172 frame"). This will take work. */
173 gdb_assert (frame_id_p (*this_id));
174
175 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
176 which generates DW_AT_entry_pc for inlined functions when
177 possible. If this attribute is available, we should use it
178 in the frame ID (and eventually, to set breakpoints). */
179 func = get_frame_function (this_frame);
180 gdb_assert (func != NULL);
181 (*this_id).code_addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func));
182 (*this_id).artificial_depth++;
183 }
184
185 static struct value *
186 inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
187 int regnum)
188 {
189 /* Use get_frame_register_value instead of
190 frame_unwind_got_register, to avoid requiring this frame's ID.
191 This frame's ID depends on the previous frame's ID (unusual), and
192 the previous frame's ID depends on this frame's unwound
193 registers. If unwinding registers from this frame called
194 get_frame_id, there would be a loop.
195
196 Do not copy this code into any other unwinder! Inlined functions
197 are special; other unwinders must not have a dependency on the
198 previous frame's ID, and therefore can and should use
199 frame_unwind_got_register instead. */
200 return get_frame_register_value (this_frame, regnum);
201 }
202
203 /* Check whether we are at an inlining site that does not already
204 have an associated frame. */
205
206 static int
207 inline_frame_sniffer (const struct frame_unwind *self,
208 struct frame_info *this_frame,
209 void **this_cache)
210 {
211 CORE_ADDR this_pc;
212 const struct block *frame_block, *cur_block;
213 int depth;
214 struct frame_info *next_frame;
215 struct inline_state *state = find_inline_frame_state (inferior_thread ());
216
217 this_pc = get_frame_address_in_block (this_frame);
218 frame_block = block_for_pc (this_pc);
219 if (frame_block == NULL)
220 return 0;
221
222 /* Calculate DEPTH, the number of inlined functions at this
223 location. */
224 depth = 0;
225 cur_block = frame_block;
226 while (BLOCK_SUPERBLOCK (cur_block))
227 {
228 if (block_inlined_p (cur_block))
229 depth++;
230 else if (BLOCK_FUNCTION (cur_block) != NULL)
231 break;
232
233 cur_block = BLOCK_SUPERBLOCK (cur_block);
234 }
235
236 /* Check how many inlined functions already have frames. */
237 for (next_frame = get_next_frame (this_frame);
238 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
239 next_frame = get_next_frame (next_frame))
240 {
241 gdb_assert (depth > 0);
242 depth--;
243 }
244
245 /* If this is the topmost frame, or all frames above us are inlined,
246 then check whether we were requested to skip some frames (so they
247 can be stepped into later). */
248 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
249 {
250 gdb_assert (depth >= state->skipped_frames);
251 depth -= state->skipped_frames;
252 }
253
254 /* If all the inlined functions here already have frames, then pass
255 to the normal unwinder for this PC. */
256 if (depth == 0)
257 return 0;
258
259 /* If the next frame is an inlined function, but not the outermost, then
260 we are the next outer. If it is not an inlined function, then we
261 are the innermost inlined function of a different real frame. */
262 return 1;
263 }
264
265 const struct frame_unwind inline_frame_unwind = {
266 INLINE_FRAME,
267 default_frame_unwind_stop_reason,
268 inline_frame_this_id,
269 inline_frame_prev_register,
270 NULL,
271 inline_frame_sniffer
272 };
273
274 /* Return non-zero if BLOCK, an inlined function block containing PC,
275 has a group of contiguous instructions starting at PC (but not
276 before it). */
277
278 static int
279 block_starting_point_at (CORE_ADDR pc, const struct block *block)
280 {
281 const struct blockvector *bv;
282 const struct block *new_block;
283
284 bv = blockvector_for_pc (pc, NULL);
285 if (BLOCKVECTOR_MAP (bv) == NULL)
286 return 0;
287
288 new_block = (const struct block *) addrmap_find (BLOCKVECTOR_MAP (bv),
289 pc - 1);
290 if (new_block == NULL)
291 return 1;
292
293 if (new_block == block || contained_in (new_block, block))
294 return 0;
295
296 /* The immediately preceding address belongs to a different block,
297 which is not a child of this one. Treat this as an entrance into
298 BLOCK. */
299 return 1;
300 }
301
302 /* Loop over the stop chain and determine if execution stopped in an
303 inlined frame because of a breakpoint with a user-specified location
304 set at FRAME_BLOCK. */
305
306 static bool
307 stopped_by_user_bp_inline_frame (const block *frame_block, bpstat stop_chain)
308 {
309 for (bpstat s = stop_chain; s != NULL; s = s->next)
310 {
311 struct breakpoint *bpt = s->breakpoint_at;
312
313 if (bpt != NULL
314 && (user_breakpoint_p (bpt) || bpt->type == bp_until))
315 {
316 bp_location *loc = s->bp_location_at.get ();
317 enum bp_loc_type t = loc->loc_type;
318
319 if (t == bp_loc_software_breakpoint
320 || t == bp_loc_hardware_breakpoint)
321 {
322 /* If the location has a function symbol, check whether
323 the frame was for that inlined function. If it has
324 no function symbol, then assume it is. I.e., default
325 to presenting the stop at the innermost inline
326 function. */
327 if (loc->symbol == nullptr
328 || frame_block == SYMBOL_BLOCK_VALUE (loc->symbol))
329 return true;
330 }
331 }
332 }
333
334 return false;
335 }
336
337 /* See inline-frame.h. */
338
339 void
340 skip_inline_frames (thread_info *thread, bpstat stop_chain)
341 {
342 const struct block *frame_block, *cur_block;
343 std::vector<struct symbol *> skipped_syms;
344 int skip_count = 0;
345
346 /* This function is called right after reinitializing the frame
347 cache. We try not to do more unwinding than absolutely
348 necessary, for performance. */
349 CORE_ADDR this_pc = get_frame_pc (get_current_frame ());
350 frame_block = block_for_pc (this_pc);
351
352 if (frame_block != NULL)
353 {
354 cur_block = frame_block;
355 while (BLOCK_SUPERBLOCK (cur_block))
356 {
357 if (block_inlined_p (cur_block))
358 {
359 /* See comments in inline_frame_this_id about this use
360 of BLOCK_ENTRY_PC. */
361 if (BLOCK_ENTRY_PC (cur_block) == this_pc
362 || block_starting_point_at (this_pc, cur_block))
363 {
364 /* Do not skip the inlined frame if execution
365 stopped in an inlined frame because of a user
366 breakpoint for this inline function. */
367 if (stopped_by_user_bp_inline_frame (cur_block, stop_chain))
368 break;
369
370 skip_count++;
371 skipped_syms.push_back (BLOCK_FUNCTION (cur_block));
372 }
373 else
374 break;
375 }
376 else if (BLOCK_FUNCTION (cur_block) != NULL)
377 break;
378
379 cur_block = BLOCK_SUPERBLOCK (cur_block);
380 }
381 }
382
383 gdb_assert (find_inline_frame_state (thread) == NULL);
384 inline_states.emplace_back (thread, skip_count, this_pc,
385 std::move (skipped_syms));
386
387 if (skip_count != 0)
388 reinit_frame_cache ();
389 }
390
391 /* Step into an inlined function by unhiding it. */
392
393 void
394 step_into_inline_frame (thread_info *thread)
395 {
396 inline_state *state = find_inline_frame_state (thread);
397
398 gdb_assert (state != NULL && state->skipped_frames > 0);
399 state->skipped_frames--;
400 reinit_frame_cache ();
401 }
402
403 /* Return the number of hidden functions inlined into the current
404 frame. */
405
406 int
407 inline_skipped_frames (thread_info *thread)
408 {
409 inline_state *state = find_inline_frame_state (thread);
410
411 if (state == NULL)
412 return 0;
413 else
414 return state->skipped_frames;
415 }
416
417 /* If one or more inlined functions are hidden, return the symbol for
418 the function inlined into the current frame. */
419
420 struct symbol *
421 inline_skipped_symbol (thread_info *thread)
422 {
423 inline_state *state = find_inline_frame_state (thread);
424 gdb_assert (state != NULL);
425
426 /* This should only be called when we are skipping at least one frame,
427 hence SKIPPED_FRAMES will be greater than zero when we get here.
428 We initialise SKIPPED_FRAMES at the same time as we build
429 SKIPPED_SYMBOLS, hence it should be true that SKIPPED_FRAMES never
430 indexes outside of the SKIPPED_SYMBOLS vector. */
431 gdb_assert (state->skipped_frames > 0);
432 gdb_assert (state->skipped_frames <= state->skipped_symbols.size ());
433 return state->skipped_symbols[state->skipped_frames - 1];
434 }
435
436 /* Return the number of functions inlined into THIS_FRAME. Some of
437 the callees may not have associated frames (see
438 skip_inline_frames). */
439
440 int
441 frame_inlined_callees (struct frame_info *this_frame)
442 {
443 struct frame_info *next_frame;
444 int inline_count = 0;
445
446 /* First count how many inlined functions at this PC have frames
447 above FRAME (are inlined into FRAME). */
448 for (next_frame = get_next_frame (this_frame);
449 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
450 next_frame = get_next_frame (next_frame))
451 inline_count++;
452
453 /* Simulate some most-inner inlined frames which were suppressed, so
454 they can be stepped into later. If we are unwinding already
455 outer frames from some non-inlined frame this does not apply. */
456 if (next_frame == NULL)
457 inline_count += inline_skipped_frames (inferior_thread ());
458
459 return inline_count;
460 }