Make find_thread_ptid lookup thread map instead of iterate
[deliverable/binutils-gdb.git] / gdb / inline-frame.c
CommitLineData
edb3359d
DJ
1/* Inline frame unwinder for GDB.
2
b811d2c2 3 Copyright (C) 2008-2020 Free Software Foundation, Inc.
4e5106e6 4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
edb3359d
DJ
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
ddfe970e 22#include "breakpoint.h"
e451c4a1 23#include "inline-frame.h"
edb3359d
DJ
24#include "addrmap.h"
25#include "block.h"
26#include "frame-unwind.h"
27#include "inferior.h"
00431a78 28#include "gdbthread.h"
4e485129 29#include "regcache.h"
edb3359d 30#include "symtab.h"
51d48146 31#include "frame.h"
b24531ed 32#include <algorithm>
edb3359d 33
edb3359d
DJ
34/* We need to save a few variables for every thread stopped at the
35 virtual call site of an inlined function. If there was always a
36 "struct thread_info", we could hang it off that; in the mean time,
37 keep our own list. */
38struct inline_state
39{
00431a78 40 inline_state (thread_info *thread_, int skipped_frames_, CORE_ADDR saved_pc_,
b24531ed 41 symbol *skipped_symbol_)
00431a78 42 : thread (thread_), skipped_frames (skipped_frames_), saved_pc (saved_pc_),
b24531ed
SM
43 skipped_symbol (skipped_symbol_)
44 {}
45
edb3359d 46 /* The thread this data relates to. It should be a currently
00431a78
PA
47 stopped thread. */
48 thread_info *thread;
edb3359d
DJ
49
50 /* The number of inlined functions we are skipping. Each of these
51 functions can be stepped in to. */
52 int skipped_frames;
53
54 /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
55 when calculating SKIPPED_FRAMES; used to check whether we have
56 moved to a new location by user request. If so, we invalidate
57 any skipped frames. */
58 CORE_ADDR saved_pc;
59
60 /* Only valid if SKIPPED_FRAMES is non-zero. This is the symbol
61 of the outermost skipped inline function. It's used to find the
62 call site of the current frame. */
63 struct symbol *skipped_symbol;
64};
65
b24531ed 66static std::vector<inline_state> inline_states;
edb3359d 67
00431a78
PA
68/* Locate saved inlined frame state for THREAD, if it exists and is
69 valid. */
edb3359d
DJ
70
71static struct inline_state *
00431a78 72find_inline_frame_state (thread_info *thread)
edb3359d 73{
b24531ed 74 auto state_it = std::find_if (inline_states.begin (), inline_states.end (),
00431a78 75 [thread] (const inline_state &state)
b24531ed 76 {
00431a78 77 return state.thread == thread;
b24531ed 78 });
edb3359d 79
b24531ed
SM
80 if (state_it == inline_states.end ())
81 return nullptr;
edb3359d 82
b24531ed 83 inline_state &state = *state_it;
00431a78 84 struct regcache *regcache = get_thread_regcache (thread);
b24531ed 85 CORE_ADDR current_pc = regcache_read_pc (regcache);
edb3359d 86
b24531ed
SM
87 if (current_pc != state.saved_pc)
88 {
89 /* PC has changed - this context is invalid. Use the
90 default behavior. */
edb3359d 91
b24531ed
SM
92 unordered_remove (inline_states, state_it);
93 return nullptr;
94 }
edb3359d 95
b24531ed 96 return &state;
edb3359d
DJ
97}
98
5b6d1e4f 99/* See inline-frame.h. */
edb3359d
DJ
100
101void
5b6d1e4f 102clear_inline_frame_state (process_stratum_target *target, ptid_t filter_ptid)
edb3359d 103{
5b6d1e4f 104 gdb_assert (target != NULL);
edb3359d 105
5b6d1e4f 106 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
edb3359d 107 {
5b6d1e4f
PA
108 auto matcher = [target, &filter_ptid] (const inline_state &state)
109 {
110 thread_info *t = state.thread;
111 return (t->inf->process_target () == target
112 && t->ptid.matches (filter_ptid));
113 };
114
b24531ed 115 auto it = std::remove_if (inline_states.begin (), inline_states.end (),
5b6d1e4f 116 matcher);
b24531ed
SM
117
118 inline_states.erase (it, inline_states.end ());
119
edb3359d
DJ
120 return;
121 }
122
5b6d1e4f
PA
123
124 auto matcher = [target, &filter_ptid] (const inline_state &state)
125 {
126 thread_info *t = state.thread;
127 return (t->inf->process_target () == target
128 && filter_ptid == t->ptid);
129 };
130
131 auto it = std::find_if (inline_states.begin (), inline_states.end (),
132 matcher);
133
134 if (it != inline_states.end ())
135 unordered_remove (inline_states, it);
136}
137
138/* See inline-frame.h. */
139
140void
141clear_inline_frame_state (thread_info *thread)
142{
b24531ed 143 auto it = std::find_if (inline_states.begin (), inline_states.end (),
5b6d1e4f 144 [thread] (const inline_state &state)
b24531ed 145 {
5b6d1e4f 146 return thread == state.thread;
b24531ed
SM
147 });
148
149 if (it != inline_states.end ())
150 unordered_remove (inline_states, it);
edb3359d
DJ
151}
152
153static void
154inline_frame_this_id (struct frame_info *this_frame,
155 void **this_cache,
156 struct frame_id *this_id)
157{
158 struct symbol *func;
159
160 /* In order to have a stable frame ID for a given inline function,
161 we must get the stack / special addresses from the underlying
51d48146
PA
162 real frame's this_id method. So we must call
163 get_prev_frame_always. Because we are inlined into some
164 function, there must be previous frames, so this is safe - as
165 long as we're careful not to create any cycles. */
166 *this_id = get_frame_id (get_prev_frame_always (this_frame));
edb3359d
DJ
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);
2b1ffcfd 181 (*this_id).code_addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func));
193facb3 182 (*this_id).artificial_depth++;
edb3359d
DJ
183}
184
185static struct value *
186inline_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
206static int
207inline_frame_sniffer (const struct frame_unwind *self,
208 struct frame_info *this_frame,
209 void **this_cache)
210{
211 CORE_ADDR this_pc;
3977b71f 212 const struct block *frame_block, *cur_block;
edb3359d
DJ
213 int depth;
214 struct frame_info *next_frame;
00431a78 215 struct inline_state *state = find_inline_frame_state (inferior_thread ());
edb3359d
DJ
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++;
0fa7fe50
JB
230 else if (BLOCK_FUNCTION (cur_block) != NULL)
231 break;
edb3359d
DJ
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 {
4e485129
DJ
250 gdb_assert (depth >= state->skipped_frames);
251 depth -= state->skipped_frames;
edb3359d
DJ
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
39d7b0e2 265const struct frame_unwind inline_frame_unwind = {
edb3359d 266 INLINE_FRAME,
8fbca658 267 default_frame_unwind_stop_reason,
edb3359d
DJ
268 inline_frame_this_id,
269 inline_frame_prev_register,
270 NULL,
271 inline_frame_sniffer
272};
273
edb3359d
DJ
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
278static int
3977b71f 279block_starting_point_at (CORE_ADDR pc, const struct block *block)
edb3359d 280{
346d1dfe 281 const struct blockvector *bv;
582942f4 282 const struct block *new_block;
edb3359d
DJ
283
284 bv = blockvector_for_pc (pc, NULL);
285 if (BLOCKVECTOR_MAP (bv) == NULL)
286 return 0;
287
582942f4
TT
288 new_block = (const struct block *) addrmap_find (BLOCKVECTOR_MAP (bv),
289 pc - 1);
edb3359d
DJ
290 if (new_block == NULL)
291 return 1;
292
293 if (new_block == block || contained_in (new_block, block))
294 return 0;
295
177b42fe 296 /* The immediately preceding address belongs to a different block,
edb3359d
DJ
297 which is not a child of this one. Treat this as an entrance into
298 BLOCK. */
299 return 1;
300}
301
ddfe970e 302/* Loop over the stop chain and determine if execution stopped in an
61b04dd0 303 inlined frame because of a user breakpoint set at FRAME_BLOCK. */
ddfe970e
KS
304
305static bool
61b04dd0 306stopped_by_user_bp_inline_frame (const block *frame_block, bpstat stop_chain)
ddfe970e
KS
307{
308 for (bpstat s = stop_chain; s != NULL; s = s->next)
309 {
310 struct breakpoint *bpt = s->breakpoint_at;
311
312 if (bpt != NULL && user_breakpoint_p (bpt))
313 {
314 bp_location *loc = s->bp_location_at;
315 enum bp_loc_type t = loc->loc_type;
316
991ff292
PA
317 if (t == bp_loc_software_breakpoint
318 || t == bp_loc_hardware_breakpoint)
319 {
320 /* If the location has a function symbol, check whether
321 the frame was for that inlined function. If it has
322 no function symbol, then assume it is. I.e., default
323 to presenting the stop at the innermost inline
324 function. */
325 if (loc->symbol == nullptr
326 || frame_block == SYMBOL_BLOCK_VALUE (loc->symbol))
327 return true;
328 }
ddfe970e
KS
329 }
330 }
331
332 return false;
333}
334
335/* See inline-frame.h. */
edb3359d
DJ
336
337void
00431a78 338skip_inline_frames (thread_info *thread, bpstat stop_chain)
edb3359d 339{
3977b71f 340 const struct block *frame_block, *cur_block;
edb3359d
DJ
341 struct symbol *last_sym = NULL;
342 int skip_count = 0;
edb3359d
DJ
343
344 /* This function is called right after reinitializing the frame
345 cache. We try not to do more unwinding than absolutely
346 necessary, for performance. */
b24531ed 347 CORE_ADDR this_pc = get_frame_pc (get_current_frame ());
edb3359d
DJ
348 frame_block = block_for_pc (this_pc);
349
350 if (frame_block != NULL)
351 {
352 cur_block = frame_block;
353 while (BLOCK_SUPERBLOCK (cur_block))
354 {
355 if (block_inlined_p (cur_block))
356 {
357 /* See comments in inline_frame_this_id about this use
2b1ffcfd
KB
358 of BLOCK_ENTRY_PC. */
359 if (BLOCK_ENTRY_PC (cur_block) == this_pc
edb3359d
DJ
360 || block_starting_point_at (this_pc, cur_block))
361 {
ddfe970e
KS
362 /* Do not skip the inlined frame if execution
363 stopped in an inlined frame because of a user
61b04dd0
PA
364 breakpoint for this inline function. */
365 if (stopped_by_user_bp_inline_frame (cur_block, stop_chain))
366 break;
367
368 skip_count++;
369 last_sym = BLOCK_FUNCTION (cur_block);
edb3359d
DJ
370 }
371 else
372 break;
373 }
0fa7fe50
JB
374 else if (BLOCK_FUNCTION (cur_block) != NULL)
375 break;
376
edb3359d
DJ
377 cur_block = BLOCK_SUPERBLOCK (cur_block);
378 }
379 }
380
00431a78
PA
381 gdb_assert (find_inline_frame_state (thread) == NULL);
382 inline_states.emplace_back (thread, skip_count, this_pc, last_sym);
edb3359d
DJ
383
384 if (skip_count != 0)
385 reinit_frame_cache ();
386}
387
388/* Step into an inlined function by unhiding it. */
389
390void
00431a78 391step_into_inline_frame (thread_info *thread)
edb3359d 392{
00431a78 393 inline_state *state = find_inline_frame_state (thread);
edb3359d
DJ
394
395 gdb_assert (state != NULL && state->skipped_frames > 0);
396 state->skipped_frames--;
397 reinit_frame_cache ();
398}
399
400/* Return the number of hidden functions inlined into the current
401 frame. */
402
403int
00431a78 404inline_skipped_frames (thread_info *thread)
edb3359d 405{
00431a78 406 inline_state *state = find_inline_frame_state (thread);
edb3359d
DJ
407
408 if (state == NULL)
409 return 0;
410 else
411 return state->skipped_frames;
412}
413
414/* If one or more inlined functions are hidden, return the symbol for
415 the function inlined into the current frame. */
416
417struct symbol *
00431a78 418inline_skipped_symbol (thread_info *thread)
edb3359d 419{
00431a78 420 inline_state *state = find_inline_frame_state (thread);
edb3359d
DJ
421
422 gdb_assert (state != NULL);
423 return state->skipped_symbol;
424}
425
426/* Return the number of functions inlined into THIS_FRAME. Some of
427 the callees may not have associated frames (see
428 skip_inline_frames). */
429
430int
431frame_inlined_callees (struct frame_info *this_frame)
432{
433 struct frame_info *next_frame;
434 int inline_count = 0;
435
436 /* First count how many inlined functions at this PC have frames
437 above FRAME (are inlined into FRAME). */
438 for (next_frame = get_next_frame (this_frame);
439 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
440 next_frame = get_next_frame (next_frame))
441 inline_count++;
442
443 /* Simulate some most-inner inlined frames which were suppressed, so
444 they can be stepped into later. If we are unwinding already
445 outer frames from some non-inlined frame this does not apply. */
446 if (next_frame == NULL)
00431a78 447 inline_count += inline_skipped_frames (inferior_thread ());
edb3359d
DJ
448
449 return inline_count;
450}
This page took 1.269643 seconds and 4 git commands to generate.