]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inline-frame.c
Make format_pieces recognize the \e escape sequence
[thirdparty/binutils-gdb.git] / gdb / inline-frame.c
CommitLineData
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"
e451c4a1 21#include "inline-frame.h"
edb3359d
DJ
22#include "addrmap.h"
23#include "block.h"
24#include "frame-unwind.h"
25#include "inferior.h"
4e485129 26#include "regcache.h"
edb3359d
DJ
27#include "symtab.h"
28#include "vec.h"
51d48146 29#include "frame.h"
b24531ed 30#include <algorithm>
edb3359d 31
edb3359d
DJ
32/* We need to save a few variables for every thread stopped at the
33 virtual call site of an inlined function. If there was always a
34 "struct thread_info", we could hang it off that; in the mean time,
35 keep our own list. */
36struct inline_state
37{
b24531ed
SM
38 inline_state (ptid_t ptid_, int skipped_frames_, CORE_ADDR saved_pc_,
39 symbol *skipped_symbol_)
40 : ptid (ptid_), skipped_frames (skipped_frames_), saved_pc (saved_pc_),
41 skipped_symbol (skipped_symbol_)
42 {}
43
edb3359d
DJ
44 /* The thread this data relates to. It should be a currently
45 stopped thread; we assume thread IDs never change while the
46 thread is stopped. */
47 ptid_t ptid;
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 symbol
60 of the outermost skipped inline function. It's used to find the
61 call site of the current frame. */
62 struct symbol *skipped_symbol;
63};
64
b24531ed 65static std::vector<inline_state> inline_states;
edb3359d 66
4e485129
DJ
67/* Locate saved inlined frame state for PTID, if it exists
68 and is valid. */
edb3359d
DJ
69
70static struct inline_state *
71find_inline_frame_state (ptid_t ptid)
72{
b24531ed
SM
73 auto state_it = std::find_if (inline_states.begin (), inline_states.end (),
74 [&ptid] (const inline_state &state)
75 {
76 return ptid == state.ptid;
77 });
edb3359d 78
b24531ed
SM
79 if (state_it == inline_states.end ())
80 return nullptr;
edb3359d 81
b24531ed
SM
82 inline_state &state = *state_it;
83 struct regcache *regcache = get_thread_regcache (ptid);
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
98/* Forget about any hidden inlined functions in PTID, which is new or
99 about to be resumed. PTID may be minus_one_ptid (all processes)
100 or a PID (all threads in this process). */
101
102void
103clear_inline_frame_state (ptid_t ptid)
104{
b24531ed 105 if (ptid == minus_one_ptid)
edb3359d 106 {
b24531ed 107 inline_states.clear ();
edb3359d
DJ
108 return;
109 }
110
b24531ed 111 if (ptid.is_pid ())
edb3359d 112 {
b24531ed
SM
113 int pid = ptid.pid ();
114 auto it = std::remove_if (inline_states.begin (), inline_states.end (),
115 [pid] (const inline_state &state)
116 {
117 return pid == state.ptid.pid ();
118 });
119
120 inline_states.erase (it, inline_states.end ());
121
edb3359d
DJ
122 return;
123 }
124
b24531ed
SM
125 auto it = std::find_if (inline_states.begin (), inline_states.end (),
126 [&ptid] (const inline_state &state)
127 {
128 return ptid == state.ptid;
129 });
130
131 if (it != inline_states.end ())
132 unordered_remove (inline_states, it);
edb3359d
DJ
133}
134
135static void
136inline_frame_this_id (struct frame_info *this_frame,
137 void **this_cache,
138 struct frame_id *this_id)
139{
140 struct symbol *func;
141
142 /* In order to have a stable frame ID for a given inline function,
143 we must get the stack / special addresses from the underlying
51d48146
PA
144 real frame's this_id method. So we must call
145 get_prev_frame_always. Because we are inlined into some
146 function, there must be previous frames, so this is safe - as
147 long as we're careful not to create any cycles. */
148 *this_id = get_frame_id (get_prev_frame_always (this_frame));
edb3359d
DJ
149
150 /* We need a valid frame ID, so we need to be based on a valid
151 frame. FSF submission NOTE: this would be a good assertion to
152 apply to all frames, all the time. That would fix the ambiguity
153 of null_frame_id (between "no/any frame" and "the outermost
154 frame"). This will take work. */
155 gdb_assert (frame_id_p (*this_id));
156
005ca36a
JB
157 /* For now, require we don't match outer_frame_id either (see
158 comment above). */
159 gdb_assert (!frame_id_eq (*this_id, outer_frame_id));
160
edb3359d
DJ
161 /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
162 which generates DW_AT_entry_pc for inlined functions when
163 possible. If this attribute is available, we should use it
164 in the frame ID (and eventually, to set breakpoints). */
165 func = get_frame_function (this_frame);
166 gdb_assert (func != NULL);
167 (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
193facb3 168 (*this_id).artificial_depth++;
edb3359d
DJ
169}
170
171static struct value *
172inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
173 int regnum)
174{
175 /* Use get_frame_register_value instead of
176 frame_unwind_got_register, to avoid requiring this frame's ID.
177 This frame's ID depends on the previous frame's ID (unusual), and
178 the previous frame's ID depends on this frame's unwound
179 registers. If unwinding registers from this frame called
180 get_frame_id, there would be a loop.
181
182 Do not copy this code into any other unwinder! Inlined functions
183 are special; other unwinders must not have a dependency on the
184 previous frame's ID, and therefore can and should use
185 frame_unwind_got_register instead. */
186 return get_frame_register_value (this_frame, regnum);
187}
188
189/* Check whether we are at an inlining site that does not already
190 have an associated frame. */
191
192static int
193inline_frame_sniffer (const struct frame_unwind *self,
194 struct frame_info *this_frame,
195 void **this_cache)
196{
197 CORE_ADDR this_pc;
3977b71f 198 const struct block *frame_block, *cur_block;
edb3359d
DJ
199 int depth;
200 struct frame_info *next_frame;
201 struct inline_state *state = find_inline_frame_state (inferior_ptid);
202
203 this_pc = get_frame_address_in_block (this_frame);
204 frame_block = block_for_pc (this_pc);
205 if (frame_block == NULL)
206 return 0;
207
208 /* Calculate DEPTH, the number of inlined functions at this
209 location. */
210 depth = 0;
211 cur_block = frame_block;
212 while (BLOCK_SUPERBLOCK (cur_block))
213 {
214 if (block_inlined_p (cur_block))
215 depth++;
0fa7fe50
JB
216 else if (BLOCK_FUNCTION (cur_block) != NULL)
217 break;
edb3359d
DJ
218
219 cur_block = BLOCK_SUPERBLOCK (cur_block);
220 }
221
222 /* Check how many inlined functions already have frames. */
223 for (next_frame = get_next_frame (this_frame);
224 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
225 next_frame = get_next_frame (next_frame))
226 {
227 gdb_assert (depth > 0);
228 depth--;
229 }
230
231 /* If this is the topmost frame, or all frames above us are inlined,
232 then check whether we were requested to skip some frames (so they
233 can be stepped into later). */
234 if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
235 {
4e485129
DJ
236 gdb_assert (depth >= state->skipped_frames);
237 depth -= state->skipped_frames;
edb3359d
DJ
238 }
239
240 /* If all the inlined functions here already have frames, then pass
241 to the normal unwinder for this PC. */
242 if (depth == 0)
243 return 0;
244
245 /* If the next frame is an inlined function, but not the outermost, then
246 we are the next outer. If it is not an inlined function, then we
247 are the innermost inlined function of a different real frame. */
248 return 1;
249}
250
39d7b0e2 251const struct frame_unwind inline_frame_unwind = {
edb3359d 252 INLINE_FRAME,
8fbca658 253 default_frame_unwind_stop_reason,
edb3359d
DJ
254 inline_frame_this_id,
255 inline_frame_prev_register,
256 NULL,
257 inline_frame_sniffer
258};
259
edb3359d
DJ
260/* Return non-zero if BLOCK, an inlined function block containing PC,
261 has a group of contiguous instructions starting at PC (but not
262 before it). */
263
264static int
3977b71f 265block_starting_point_at (CORE_ADDR pc, const struct block *block)
edb3359d 266{
346d1dfe 267 const struct blockvector *bv;
edb3359d
DJ
268 struct block *new_block;
269
270 bv = blockvector_for_pc (pc, NULL);
271 if (BLOCKVECTOR_MAP (bv) == NULL)
272 return 0;
273
9a3c8263 274 new_block = (struct block *) addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
edb3359d
DJ
275 if (new_block == NULL)
276 return 1;
277
278 if (new_block == block || contained_in (new_block, block))
279 return 0;
280
177b42fe 281 /* The immediately preceding address belongs to a different block,
edb3359d
DJ
282 which is not a child of this one. Treat this as an entrance into
283 BLOCK. */
284 return 1;
285}
286
287/* Skip all inlined functions whose call sites are at the current PC.
288 Frames for the hidden functions will not appear in the backtrace until the
289 user steps into them. */
290
291void
292skip_inline_frames (ptid_t ptid)
293{
3977b71f 294 const struct block *frame_block, *cur_block;
edb3359d
DJ
295 struct symbol *last_sym = NULL;
296 int skip_count = 0;
edb3359d
DJ
297
298 /* This function is called right after reinitializing the frame
299 cache. We try not to do more unwinding than absolutely
300 necessary, for performance. */
b24531ed 301 CORE_ADDR this_pc = get_frame_pc (get_current_frame ());
edb3359d
DJ
302 frame_block = block_for_pc (this_pc);
303
304 if (frame_block != NULL)
305 {
306 cur_block = frame_block;
307 while (BLOCK_SUPERBLOCK (cur_block))
308 {
309 if (block_inlined_p (cur_block))
310 {
311 /* See comments in inline_frame_this_id about this use
312 of BLOCK_START. */
313 if (BLOCK_START (cur_block) == this_pc
314 || block_starting_point_at (this_pc, cur_block))
315 {
316 skip_count++;
317 last_sym = BLOCK_FUNCTION (cur_block);
318 }
319 else
320 break;
321 }
0fa7fe50
JB
322 else if (BLOCK_FUNCTION (cur_block) != NULL)
323 break;
324
edb3359d
DJ
325 cur_block = BLOCK_SUPERBLOCK (cur_block);
326 }
327 }
328
329 gdb_assert (find_inline_frame_state (ptid) == NULL);
b24531ed 330 inline_states.emplace_back (ptid, skip_count, this_pc, last_sym);
edb3359d
DJ
331
332 if (skip_count != 0)
333 reinit_frame_cache ();
334}
335
336/* Step into an inlined function by unhiding it. */
337
338void
339step_into_inline_frame (ptid_t ptid)
340{
341 struct inline_state *state = find_inline_frame_state (ptid);
342
343 gdb_assert (state != NULL && state->skipped_frames > 0);
344 state->skipped_frames--;
345 reinit_frame_cache ();
346}
347
348/* Return the number of hidden functions inlined into the current
349 frame. */
350
351int
352inline_skipped_frames (ptid_t ptid)
353{
354 struct inline_state *state = find_inline_frame_state (ptid);
355
356 if (state == NULL)
357 return 0;
358 else
359 return state->skipped_frames;
360}
361
362/* If one or more inlined functions are hidden, return the symbol for
363 the function inlined into the current frame. */
364
365struct symbol *
366inline_skipped_symbol (ptid_t ptid)
367{
368 struct inline_state *state = find_inline_frame_state (ptid);
369
370 gdb_assert (state != NULL);
371 return state->skipped_symbol;
372}
373
374/* Return the number of functions inlined into THIS_FRAME. Some of
375 the callees may not have associated frames (see
376 skip_inline_frames). */
377
378int
379frame_inlined_callees (struct frame_info *this_frame)
380{
381 struct frame_info *next_frame;
382 int inline_count = 0;
383
384 /* First count how many inlined functions at this PC have frames
385 above FRAME (are inlined into FRAME). */
386 for (next_frame = get_next_frame (this_frame);
387 next_frame && get_frame_type (next_frame) == INLINE_FRAME;
388 next_frame = get_next_frame (next_frame))
389 inline_count++;
390
391 /* Simulate some most-inner inlined frames which were suppressed, so
392 they can be stepped into later. If we are unwinding already
393 outer frames from some non-inlined frame this does not apply. */
394 if (next_frame == NULL)
395 inline_count += inline_skipped_frames (inferior_ptid);
396
397 return inline_count;
398}