gdb.base/share-env-with-gdbserver.exp C++ify
[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.
edb3359d
DJ
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"
ddfe970e 21#include "breakpoint.h"
e451c4a1 22#include "inline-frame.h"
edb3359d
DJ
23#include "addrmap.h"
24#include "block.h"
25#include "frame-unwind.h"
26#include "inferior.h"
00431a78 27#include "gdbthread.h"
4e485129 28#include "regcache.h"
edb3359d 29#include "symtab.h"
51d48146 30#include "frame.h"
b24531ed 31#include <algorithm>
edb3359d 32
edb3359d
DJ
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. */
37struct inline_state
38{
00431a78 39 inline_state (thread_info *thread_, int skipped_frames_, CORE_ADDR saved_pc_,
7ffa82e1 40 std::vector<symbol *> &&skipped_symbols_)
00431a78 41 : thread (thread_), skipped_frames (skipped_frames_), saved_pc (saved_pc_),
7ffa82e1 42 skipped_symbols (std::move (skipped_symbols_))
b24531ed
SM
43 {}
44
edb3359d 45 /* The thread this data relates to. It should be a currently
00431a78
PA
46 stopped thread. */
47 thread_info *thread;
edb3359d
DJ
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
7ffa82e1
AB
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;
edb3359d
DJ
63};
64
b24531ed 65static std::vector<inline_state> inline_states;
edb3359d 66
00431a78
PA
67/* Locate saved inlined frame state for THREAD, if it exists and is
68 valid. */
edb3359d
DJ
69
70static struct inline_state *
00431a78 71find_inline_frame_state (thread_info *thread)
edb3359d 72{
b24531ed 73 auto state_it = std::find_if (inline_states.begin (), inline_states.end (),
00431a78 74 [thread] (const inline_state &state)
b24531ed 75 {
00431a78 76 return state.thread == thread;
b24531ed 77 });
edb3359d 78
b24531ed
SM
79 if (state_it == inline_states.end ())
80 return nullptr;
edb3359d 81
b24531ed 82 inline_state &state = *state_it;
00431a78 83 struct regcache *regcache = get_thread_regcache (thread);
b24531ed 84 CORE_ADDR current_pc = regcache_read_pc (regcache);
edb3359d 85
b24531ed
SM
86 if (current_pc != state.saved_pc)
87 {
88 /* PC has changed - this context is invalid. Use the
89 default behavior. */
edb3359d 90
b24531ed
SM
91 unordered_remove (inline_states, state_it);
92 return nullptr;
93 }
edb3359d 94
b24531ed 95 return &state;
edb3359d
DJ
96}
97
5b6d1e4f 98/* See inline-frame.h. */
edb3359d
DJ
99
100void
5b6d1e4f 101clear_inline_frame_state (process_stratum_target *target, ptid_t filter_ptid)
edb3359d 102{
5b6d1e4f 103 gdb_assert (target != NULL);
edb3359d 104
5b6d1e4f 105 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
edb3359d 106 {
5b6d1e4f
PA
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
b24531ed 114 auto it = std::remove_if (inline_states.begin (), inline_states.end (),
5b6d1e4f 115 matcher);
b24531ed
SM
116
117 inline_states.erase (it, inline_states.end ());
118
edb3359d
DJ
119 return;
120 }
121
5b6d1e4f
PA
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
139void
140clear_inline_frame_state (thread_info *thread)
141{
b24531ed 142 auto it = std::find_if (inline_states.begin (), inline_states.end (),
5b6d1e4f 143 [thread] (const inline_state &state)
b24531ed 144 {
5b6d1e4f 145 return thread == state.thread;
b24531ed
SM
146 });
147
148 if (it != inline_states.end ())
149 unordered_remove (inline_states, it);
edb3359d
DJ
150}
151
152static void
153inline_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
51d48146
PA
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. */
165 *this_id = get_frame_id (get_prev_frame_always (this_frame));
edb3359d
DJ
166
167 /* We need a valid frame ID, so we need to be based on a valid
168 frame. FSF submission NOTE: this would be a good assertion to
169 apply to all frames, all the time. That would fix the ambiguity
170 of null_frame_id (between "no/any frame" and "the outermost
171 frame"). This will take work. */
172 gdb_assert (frame_id_p (*this_id));
173
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);
2b1ffcfd 180 (*this_id).code_addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func));
193facb3 181 (*this_id).artificial_depth++;
edb3359d
DJ
182}
183
184static struct value *
185inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
186 int regnum)
187{
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.
194
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);
200}
201
202/* Check whether we are at an inlining site that does not already
203 have an associated frame. */
204
205static int
206inline_frame_sniffer (const struct frame_unwind *self,
207 struct frame_info *this_frame,
208 void **this_cache)
209{
210 CORE_ADDR this_pc;
3977b71f 211 const struct block *frame_block, *cur_block;
edb3359d
DJ
212 int depth;
213 struct frame_info *next_frame;
00431a78 214 struct inline_state *state = find_inline_frame_state (inferior_thread ());
edb3359d
DJ
215
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;
220
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))
226 {
227 if (block_inlined_p (cur_block))
228 depth++;
0fa7fe50
JB
229 else if (BLOCK_FUNCTION (cur_block) != NULL)
230 break;
edb3359d
DJ
231
232 cur_block = BLOCK_SUPERBLOCK (cur_block);
233 }
234
235 /* Check how many inlined functions already have frames. */
236 for (next_frame = get_next_frame (this_frame);
237 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
238 next_frame = get_next_frame (next_frame))
239 {
240 gdb_assert (depth > 0);
241 depth--;
242 }
243
244 /* If this is the topmost frame, or all frames above us are inlined,
245 then check whether we were requested to skip some frames (so they
246 can be stepped into later). */
247 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
248 {
4e485129
DJ
249 gdb_assert (depth >= state->skipped_frames);
250 depth -= state->skipped_frames;
edb3359d
DJ
251 }
252
253 /* If all the inlined functions here already have frames, then pass
254 to the normal unwinder for this PC. */
255 if (depth == 0)
256 return 0;
257
258 /* If the next frame is an inlined function, but not the outermost, then
259 we are the next outer. If it is not an inlined function, then we
260 are the innermost inlined function of a different real frame. */
261 return 1;
262}
263
39d7b0e2 264const struct frame_unwind inline_frame_unwind = {
edb3359d 265 INLINE_FRAME,
8fbca658 266 default_frame_unwind_stop_reason,
edb3359d
DJ
267 inline_frame_this_id,
268 inline_frame_prev_register,
269 NULL,
270 inline_frame_sniffer
271};
272
edb3359d
DJ
273/* Return non-zero if BLOCK, an inlined function block containing PC,
274 has a group of contiguous instructions starting at PC (but not
275 before it). */
276
277static int
3977b71f 278block_starting_point_at (CORE_ADDR pc, const struct block *block)
edb3359d 279{
346d1dfe 280 const struct blockvector *bv;
582942f4 281 const struct block *new_block;
edb3359d
DJ
282
283 bv = blockvector_for_pc (pc, NULL);
284 if (BLOCKVECTOR_MAP (bv) == NULL)
285 return 0;
286
582942f4
TT
287 new_block = (const struct block *) addrmap_find (BLOCKVECTOR_MAP (bv),
288 pc - 1);
edb3359d
DJ
289 if (new_block == NULL)
290 return 1;
291
292 if (new_block == block || contained_in (new_block, block))
293 return 0;
294
177b42fe 295 /* The immediately preceding address belongs to a different block,
edb3359d
DJ
296 which is not a child of this one. Treat this as an entrance into
297 BLOCK. */
298 return 1;
299}
300
ddfe970e 301/* Loop over the stop chain and determine if execution stopped in an
b2b38aa4
PA
302 inlined frame because of a breakpoint with a user-specified location
303 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
b2b38aa4
PA
312 if (bpt != NULL
313 && (user_breakpoint_p (bpt) || bpt->type == bp_until))
ddfe970e
KS
314 {
315 bp_location *loc = s->bp_location_at;
316 enum bp_loc_type t = loc->loc_type;
317
991ff292
PA
318 if (t == bp_loc_software_breakpoint
319 || t == bp_loc_hardware_breakpoint)
320 {
321 /* If the location has a function symbol, check whether
322 the frame was for that inlined function. If it has
323 no function symbol, then assume it is. I.e., default
324 to presenting the stop at the innermost inline
325 function. */
326 if (loc->symbol == nullptr
327 || frame_block == SYMBOL_BLOCK_VALUE (loc->symbol))
328 return true;
329 }
ddfe970e
KS
330 }
331 }
332
333 return false;
334}
335
336/* See inline-frame.h. */
edb3359d
DJ
337
338void
00431a78 339skip_inline_frames (thread_info *thread, bpstat stop_chain)
edb3359d 340{
3977b71f 341 const struct block *frame_block, *cur_block;
7ffa82e1 342 std::vector<struct symbol *> skipped_syms;
edb3359d 343 int skip_count = 0;
edb3359d
DJ
344
345 /* This function is called right after reinitializing the frame
346 cache. We try not to do more unwinding than absolutely
347 necessary, for performance. */
b24531ed 348 CORE_ADDR this_pc = get_frame_pc (get_current_frame ());
edb3359d
DJ
349 frame_block = block_for_pc (this_pc);
350
351 if (frame_block != NULL)
352 {
353 cur_block = frame_block;
354 while (BLOCK_SUPERBLOCK (cur_block))
355 {
356 if (block_inlined_p (cur_block))
357 {
358 /* See comments in inline_frame_this_id about this use
2b1ffcfd
KB
359 of BLOCK_ENTRY_PC. */
360 if (BLOCK_ENTRY_PC (cur_block) == this_pc
edb3359d
DJ
361 || block_starting_point_at (this_pc, cur_block))
362 {
ddfe970e
KS
363 /* Do not skip the inlined frame if execution
364 stopped in an inlined frame because of a user
61b04dd0
PA
365 breakpoint for this inline function. */
366 if (stopped_by_user_bp_inline_frame (cur_block, stop_chain))
367 break;
368
369 skip_count++;
7ffa82e1 370 skipped_syms.push_back (BLOCK_FUNCTION (cur_block));
edb3359d
DJ
371 }
372 else
373 break;
374 }
0fa7fe50
JB
375 else if (BLOCK_FUNCTION (cur_block) != NULL)
376 break;
377
edb3359d
DJ
378 cur_block = BLOCK_SUPERBLOCK (cur_block);
379 }
380 }
381
00431a78 382 gdb_assert (find_inline_frame_state (thread) == NULL);
7ffa82e1
AB
383 inline_states.emplace_back (thread, skip_count, this_pc,
384 std::move (skipped_syms));
edb3359d
DJ
385
386 if (skip_count != 0)
387 reinit_frame_cache ();
388}
389
390/* Step into an inlined function by unhiding it. */
391
392void
00431a78 393step_into_inline_frame (thread_info *thread)
edb3359d 394{
00431a78 395 inline_state *state = find_inline_frame_state (thread);
edb3359d
DJ
396
397 gdb_assert (state != NULL && state->skipped_frames > 0);
398 state->skipped_frames--;
399 reinit_frame_cache ();
400}
401
402/* Return the number of hidden functions inlined into the current
403 frame. */
404
405int
00431a78 406inline_skipped_frames (thread_info *thread)
edb3359d 407{
00431a78 408 inline_state *state = find_inline_frame_state (thread);
edb3359d
DJ
409
410 if (state == NULL)
411 return 0;
412 else
413 return state->skipped_frames;
414}
415
416/* If one or more inlined functions are hidden, return the symbol for
417 the function inlined into the current frame. */
418
419struct symbol *
00431a78 420inline_skipped_symbol (thread_info *thread)
edb3359d 421{
00431a78 422 inline_state *state = find_inline_frame_state (thread);
edb3359d 423 gdb_assert (state != NULL);
7ffa82e1
AB
424
425 /* This should only be called when we are skipping at least one frame,
426 hence SKIPPED_FRAMES will be greater than zero when we get here.
427 We initialise SKIPPED_FRAMES at the same time as we build
428 SKIPPED_SYMBOLS, hence it should be true that SKIPPED_FRAMES never
429 indexes outside of the SKIPPED_SYMBOLS vector. */
430 gdb_assert (state->skipped_frames > 0);
431 gdb_assert (state->skipped_frames <= state->skipped_symbols.size ());
432 return state->skipped_symbols[state->skipped_frames - 1];
edb3359d
DJ
433}
434
435/* Return the number of functions inlined into THIS_FRAME. Some of
436 the callees may not have associated frames (see
437 skip_inline_frames). */
438
439int
440frame_inlined_callees (struct frame_info *this_frame)
441{
442 struct frame_info *next_frame;
443 int inline_count = 0;
444
445 /* First count how many inlined functions at this PC have frames
446 above FRAME (are inlined into FRAME). */
447 for (next_frame = get_next_frame (this_frame);
448 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
449 next_frame = get_next_frame (next_frame))
450 inline_count++;
451
452 /* Simulate some most-inner inlined frames which were suppressed, so
453 they can be stepped into later. If we are unwinding already
454 outer frames from some non-inlined frame this does not apply. */
455 if (next_frame == NULL)
00431a78 456 inline_count += inline_skipped_frames (inferior_thread ());
edb3359d
DJ
457
458 return inline_count;
459}
This page took 1.047969 seconds and 4 git commands to generate.