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