Commit | Line | Data |
---|---|---|
edb3359d DJ |
1 | /* Inline frame unwinder for GDB. |
2 | ||
e2882c85 | 3 | Copyright (C) 2008-2018 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 DJ |
29 | #include "symtab.h" |
30 | #include "vec.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. */ | |
38 | struct 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 | 66 | static 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 | |
71 | static struct inline_state * | |
00431a78 | 72 | find_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 | ||
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 | { | |
b24531ed | 106 | if (ptid == minus_one_ptid) |
edb3359d | 107 | { |
b24531ed | 108 | inline_states.clear (); |
edb3359d DJ |
109 | return; |
110 | } | |
111 | ||
b24531ed | 112 | if (ptid.is_pid ()) |
edb3359d | 113 | { |
b24531ed SM |
114 | int pid = ptid.pid (); |
115 | auto it = std::remove_if (inline_states.begin (), inline_states.end (), | |
116 | [pid] (const inline_state &state) | |
117 | { | |
00431a78 | 118 | return pid == state.thread->inf->pid; |
b24531ed SM |
119 | }); |
120 | ||
121 | inline_states.erase (it, inline_states.end ()); | |
122 | ||
edb3359d DJ |
123 | return; |
124 | } | |
125 | ||
b24531ed SM |
126 | auto it = std::find_if (inline_states.begin (), inline_states.end (), |
127 | [&ptid] (const inline_state &state) | |
128 | { | |
00431a78 | 129 | return ptid == state.thread->ptid; |
b24531ed SM |
130 | }); |
131 | ||
132 | if (it != inline_states.end ()) | |
133 | unordered_remove (inline_states, it); | |
edb3359d DJ |
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 | |
51d48146 PA |
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)); | |
edb3359d DJ |
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 | ||
005ca36a JB |
158 | /* For now, require we don't match outer_frame_id either (see |
159 | comment above). */ | |
160 | gdb_assert (!frame_id_eq (*this_id, outer_frame_id)); | |
161 | ||
edb3359d DJ |
162 | /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3 |
163 | which generates DW_AT_entry_pc for inlined functions when | |
164 | possible. If this attribute is available, we should use it | |
165 | in the frame ID (and eventually, to set breakpoints). */ | |
166 | func = get_frame_function (this_frame); | |
167 | gdb_assert (func != NULL); | |
168 | (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func)); | |
193facb3 | 169 | (*this_id).artificial_depth++; |
edb3359d DJ |
170 | } |
171 | ||
172 | static struct value * | |
173 | inline_frame_prev_register (struct frame_info *this_frame, void **this_cache, | |
174 | int regnum) | |
175 | { | |
176 | /* Use get_frame_register_value instead of | |
177 | frame_unwind_got_register, to avoid requiring this frame's ID. | |
178 | This frame's ID depends on the previous frame's ID (unusual), and | |
179 | the previous frame's ID depends on this frame's unwound | |
180 | registers. If unwinding registers from this frame called | |
181 | get_frame_id, there would be a loop. | |
182 | ||
183 | Do not copy this code into any other unwinder! Inlined functions | |
184 | are special; other unwinders must not have a dependency on the | |
185 | previous frame's ID, and therefore can and should use | |
186 | frame_unwind_got_register instead. */ | |
187 | return get_frame_register_value (this_frame, regnum); | |
188 | } | |
189 | ||
190 | /* Check whether we are at an inlining site that does not already | |
191 | have an associated frame. */ | |
192 | ||
193 | static int | |
194 | inline_frame_sniffer (const struct frame_unwind *self, | |
195 | struct frame_info *this_frame, | |
196 | void **this_cache) | |
197 | { | |
198 | CORE_ADDR this_pc; | |
3977b71f | 199 | const struct block *frame_block, *cur_block; |
edb3359d DJ |
200 | int depth; |
201 | struct frame_info *next_frame; | |
00431a78 | 202 | struct inline_state *state = find_inline_frame_state (inferior_thread ()); |
edb3359d DJ |
203 | |
204 | this_pc = get_frame_address_in_block (this_frame); | |
205 | frame_block = block_for_pc (this_pc); | |
206 | if (frame_block == NULL) | |
207 | return 0; | |
208 | ||
209 | /* Calculate DEPTH, the number of inlined functions at this | |
210 | location. */ | |
211 | depth = 0; | |
212 | cur_block = frame_block; | |
213 | while (BLOCK_SUPERBLOCK (cur_block)) | |
214 | { | |
215 | if (block_inlined_p (cur_block)) | |
216 | depth++; | |
0fa7fe50 JB |
217 | else if (BLOCK_FUNCTION (cur_block) != NULL) |
218 | break; | |
edb3359d DJ |
219 | |
220 | cur_block = BLOCK_SUPERBLOCK (cur_block); | |
221 | } | |
222 | ||
223 | /* Check how many inlined functions already have frames. */ | |
224 | for (next_frame = get_next_frame (this_frame); | |
225 | next_frame && get_frame_type (next_frame) == INLINE_FRAME; | |
226 | next_frame = get_next_frame (next_frame)) | |
227 | { | |
228 | gdb_assert (depth > 0); | |
229 | depth--; | |
230 | } | |
231 | ||
232 | /* If this is the topmost frame, or all frames above us are inlined, | |
233 | then check whether we were requested to skip some frames (so they | |
234 | can be stepped into later). */ | |
235 | if (state != NULL && state->skipped_frames > 0 && next_frame == NULL) | |
236 | { | |
4e485129 DJ |
237 | gdb_assert (depth >= state->skipped_frames); |
238 | depth -= state->skipped_frames; | |
edb3359d DJ |
239 | } |
240 | ||
241 | /* If all the inlined functions here already have frames, then pass | |
242 | to the normal unwinder for this PC. */ | |
243 | if (depth == 0) | |
244 | return 0; | |
245 | ||
246 | /* If the next frame is an inlined function, but not the outermost, then | |
247 | we are the next outer. If it is not an inlined function, then we | |
248 | are the innermost inlined function of a different real frame. */ | |
249 | return 1; | |
250 | } | |
251 | ||
39d7b0e2 | 252 | const struct frame_unwind inline_frame_unwind = { |
edb3359d | 253 | INLINE_FRAME, |
8fbca658 | 254 | default_frame_unwind_stop_reason, |
edb3359d DJ |
255 | inline_frame_this_id, |
256 | inline_frame_prev_register, | |
257 | NULL, | |
258 | inline_frame_sniffer | |
259 | }; | |
260 | ||
edb3359d DJ |
261 | /* Return non-zero if BLOCK, an inlined function block containing PC, |
262 | has a group of contiguous instructions starting at PC (but not | |
263 | before it). */ | |
264 | ||
265 | static int | |
3977b71f | 266 | block_starting_point_at (CORE_ADDR pc, const struct block *block) |
edb3359d | 267 | { |
346d1dfe | 268 | const struct blockvector *bv; |
edb3359d DJ |
269 | struct block *new_block; |
270 | ||
271 | bv = blockvector_for_pc (pc, NULL); | |
272 | if (BLOCKVECTOR_MAP (bv) == NULL) | |
273 | return 0; | |
274 | ||
9a3c8263 | 275 | new_block = (struct block *) addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1); |
edb3359d DJ |
276 | if (new_block == NULL) |
277 | return 1; | |
278 | ||
279 | if (new_block == block || contained_in (new_block, block)) | |
280 | return 0; | |
281 | ||
177b42fe | 282 | /* The immediately preceding address belongs to a different block, |
edb3359d DJ |
283 | which is not a child of this one. Treat this as an entrance into |
284 | BLOCK. */ | |
285 | return 1; | |
286 | } | |
287 | ||
ddfe970e | 288 | /* Loop over the stop chain and determine if execution stopped in an |
61b04dd0 | 289 | inlined frame because of a user breakpoint set at FRAME_BLOCK. */ |
ddfe970e KS |
290 | |
291 | static bool | |
61b04dd0 | 292 | stopped_by_user_bp_inline_frame (const block *frame_block, bpstat stop_chain) |
ddfe970e KS |
293 | { |
294 | for (bpstat s = stop_chain; s != NULL; s = s->next) | |
295 | { | |
296 | struct breakpoint *bpt = s->breakpoint_at; | |
297 | ||
298 | if (bpt != NULL && user_breakpoint_p (bpt)) | |
299 | { | |
300 | bp_location *loc = s->bp_location_at; | |
301 | enum bp_loc_type t = loc->loc_type; | |
302 | ||
61b04dd0 PA |
303 | if ((t == bp_loc_software_breakpoint |
304 | || t == bp_loc_hardware_breakpoint) | |
305 | && frame_block == SYMBOL_BLOCK_VALUE (loc->symbol)) | |
ddfe970e KS |
306 | return true; |
307 | } | |
308 | } | |
309 | ||
310 | return false; | |
311 | } | |
312 | ||
313 | /* See inline-frame.h. */ | |
edb3359d DJ |
314 | |
315 | void | |
00431a78 | 316 | skip_inline_frames (thread_info *thread, bpstat stop_chain) |
edb3359d | 317 | { |
3977b71f | 318 | const struct block *frame_block, *cur_block; |
edb3359d DJ |
319 | struct symbol *last_sym = NULL; |
320 | int skip_count = 0; | |
edb3359d DJ |
321 | |
322 | /* This function is called right after reinitializing the frame | |
323 | cache. We try not to do more unwinding than absolutely | |
324 | necessary, for performance. */ | |
b24531ed | 325 | CORE_ADDR this_pc = get_frame_pc (get_current_frame ()); |
edb3359d DJ |
326 | frame_block = block_for_pc (this_pc); |
327 | ||
328 | if (frame_block != NULL) | |
329 | { | |
330 | cur_block = frame_block; | |
331 | while (BLOCK_SUPERBLOCK (cur_block)) | |
332 | { | |
333 | if (block_inlined_p (cur_block)) | |
334 | { | |
335 | /* See comments in inline_frame_this_id about this use | |
336 | of BLOCK_START. */ | |
337 | if (BLOCK_START (cur_block) == this_pc | |
338 | || block_starting_point_at (this_pc, cur_block)) | |
339 | { | |
ddfe970e KS |
340 | /* Do not skip the inlined frame if execution |
341 | stopped in an inlined frame because of a user | |
61b04dd0 PA |
342 | breakpoint for this inline function. */ |
343 | if (stopped_by_user_bp_inline_frame (cur_block, stop_chain)) | |
344 | break; | |
345 | ||
346 | skip_count++; | |
347 | last_sym = BLOCK_FUNCTION (cur_block); | |
edb3359d DJ |
348 | } |
349 | else | |
350 | break; | |
351 | } | |
0fa7fe50 JB |
352 | else if (BLOCK_FUNCTION (cur_block) != NULL) |
353 | break; | |
354 | ||
edb3359d DJ |
355 | cur_block = BLOCK_SUPERBLOCK (cur_block); |
356 | } | |
357 | } | |
358 | ||
00431a78 PA |
359 | gdb_assert (find_inline_frame_state (thread) == NULL); |
360 | inline_states.emplace_back (thread, skip_count, this_pc, last_sym); | |
edb3359d DJ |
361 | |
362 | if (skip_count != 0) | |
363 | reinit_frame_cache (); | |
364 | } | |
365 | ||
366 | /* Step into an inlined function by unhiding it. */ | |
367 | ||
368 | void | |
00431a78 | 369 | step_into_inline_frame (thread_info *thread) |
edb3359d | 370 | { |
00431a78 | 371 | inline_state *state = find_inline_frame_state (thread); |
edb3359d DJ |
372 | |
373 | gdb_assert (state != NULL && state->skipped_frames > 0); | |
374 | state->skipped_frames--; | |
375 | reinit_frame_cache (); | |
376 | } | |
377 | ||
378 | /* Return the number of hidden functions inlined into the current | |
379 | frame. */ | |
380 | ||
381 | int | |
00431a78 | 382 | inline_skipped_frames (thread_info *thread) |
edb3359d | 383 | { |
00431a78 | 384 | inline_state *state = find_inline_frame_state (thread); |
edb3359d DJ |
385 | |
386 | if (state == NULL) | |
387 | return 0; | |
388 | else | |
389 | return state->skipped_frames; | |
390 | } | |
391 | ||
392 | /* If one or more inlined functions are hidden, return the symbol for | |
393 | the function inlined into the current frame. */ | |
394 | ||
395 | struct symbol * | |
00431a78 | 396 | inline_skipped_symbol (thread_info *thread) |
edb3359d | 397 | { |
00431a78 | 398 | inline_state *state = find_inline_frame_state (thread); |
edb3359d DJ |
399 | |
400 | gdb_assert (state != NULL); | |
401 | return state->skipped_symbol; | |
402 | } | |
403 | ||
404 | /* Return the number of functions inlined into THIS_FRAME. Some of | |
405 | the callees may not have associated frames (see | |
406 | skip_inline_frames). */ | |
407 | ||
408 | int | |
409 | frame_inlined_callees (struct frame_info *this_frame) | |
410 | { | |
411 | struct frame_info *next_frame; | |
412 | int inline_count = 0; | |
413 | ||
414 | /* First count how many inlined functions at this PC have frames | |
415 | above FRAME (are inlined into FRAME). */ | |
416 | for (next_frame = get_next_frame (this_frame); | |
417 | next_frame && get_frame_type (next_frame) == INLINE_FRAME; | |
418 | next_frame = get_next_frame (next_frame)) | |
419 | inline_count++; | |
420 | ||
421 | /* Simulate some most-inner inlined frames which were suppressed, so | |
422 | they can be stepped into later. If we are unwinding already | |
423 | outer frames from some non-inlined frame this does not apply. */ | |
424 | if (next_frame == NULL) | |
00431a78 | 425 | inline_count += inline_skipped_frames (inferior_thread ()); |
edb3359d DJ |
426 | |
427 | return inline_count; | |
428 | } |