Imported Upstream version 7.8
[platform/upstream/gdb.git] / gdb / inline-frame.c
1 /* Inline frame unwinder for GDB.
2
3    Copyright (C) 2008-2014 Free Software Foundation, Inc.
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"
21 #include "inline-frame.h"
22 #include "addrmap.h"
23 #include "block.h"
24 #include "frame-unwind.h"
25 #include "inferior.h"
26 #include "regcache.h"
27 #include "symtab.h"
28 #include "vec.h"
29 #include "frame.h"
30
31 #include "gdb_assert.h"
32
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.  */
37 struct inline_state
38 {
39   /* The thread this data relates to.  It should be a currently
40      stopped thread; we assume thread IDs never change while the
41      thread is stopped.  */
42   ptid_t ptid;
43
44   /* The number of inlined functions we are skipping.  Each of these
45      functions can be stepped in to.  */
46   int skipped_frames;
47
48   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the PC used
49      when calculating SKIPPED_FRAMES; used to check whether we have
50      moved to a new location by user request.  If so, we invalidate
51      any skipped frames.  */
52   CORE_ADDR saved_pc;
53
54   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the symbol
55      of the outermost skipped inline function.  It's used to find the
56      call site of the current frame.  */
57   struct symbol *skipped_symbol;
58 };
59
60 typedef struct inline_state inline_state_s;
61 DEF_VEC_O(inline_state_s);
62
63 static VEC(inline_state_s) *inline_states;
64
65 /* Locate saved inlined frame state for PTID, if it exists
66    and is valid.  */
67
68 static struct inline_state *
69 find_inline_frame_state (ptid_t ptid)
70 {
71   struct inline_state *state;
72   int ix;
73
74   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
75     {
76       if (ptid_equal (state->ptid, ptid))
77         {
78           struct regcache *regcache = get_thread_regcache (ptid);
79           CORE_ADDR current_pc = regcache_read_pc (regcache);
80
81           if (current_pc != state->saved_pc)
82             {
83               /* PC has changed - this context is invalid.  Use the
84                  default behavior.  */
85               VEC_unordered_remove (inline_state_s, inline_states, ix);
86               return NULL;
87             }
88           else
89             return state;
90         }
91     }
92
93   return NULL;
94 }
95
96 /* Allocate saved inlined frame state for PTID.  */
97
98 static struct inline_state *
99 allocate_inline_frame_state (ptid_t ptid)
100 {
101   struct inline_state *state;
102
103   state = VEC_safe_push (inline_state_s, inline_states, NULL);
104   memset (state, 0, sizeof (*state));
105   state->ptid = ptid;
106
107   return state;
108 }
109
110 /* Forget about any hidden inlined functions in PTID, which is new or
111    about to be resumed.  PTID may be minus_one_ptid (all processes)
112    or a PID (all threads in this process).  */
113
114 void
115 clear_inline_frame_state (ptid_t ptid)
116 {
117   struct inline_state *state;
118   int ix;
119
120   if (ptid_equal (ptid, minus_one_ptid))
121     {
122       VEC_free (inline_state_s, inline_states);
123       return;
124     }
125
126   if (ptid_is_pid (ptid))
127     {
128       VEC (inline_state_s) *new_states = NULL;
129       int pid = ptid_get_pid (ptid);
130
131       for (ix = 0;
132            VEC_iterate (inline_state_s, inline_states, ix, state);
133            ix++)
134         if (pid != ptid_get_pid (state->ptid))
135           VEC_safe_push (inline_state_s, new_states, state);
136       VEC_free (inline_state_s, inline_states);
137       inline_states = new_states;
138       return;
139     }
140
141   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
142     if (ptid_equal (state->ptid, ptid))
143       {
144         VEC_unordered_remove (inline_state_s, inline_states, ix);
145         return;
146       }
147 }
148
149 static void
150 inline_frame_this_id (struct frame_info *this_frame,
151                       void **this_cache,
152                       struct frame_id *this_id)
153 {
154   struct symbol *func;
155
156   /* In order to have a stable frame ID for a given inline function,
157      we must get the stack / special addresses from the underlying
158      real frame's this_id method.  So we must call
159      get_prev_frame_always.  Because we are inlined into some
160      function, there must be previous frames, so this is safe - as
161      long as we're careful not to create any cycles.  */
162   *this_id = get_frame_id (get_prev_frame_always (this_frame));
163
164   /* We need a valid frame ID, so we need to be based on a valid
165      frame.  FSF submission NOTE: this would be a good assertion to
166      apply to all frames, all the time.  That would fix the ambiguity
167      of null_frame_id (between "no/any frame" and "the outermost
168      frame").  This will take work.  */
169   gdb_assert (frame_id_p (*this_id));
170
171   /* For now, require we don't match outer_frame_id either (see
172      comment above).  */
173   gdb_assert (!frame_id_eq (*this_id, outer_frame_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);
181   (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
182   (*this_id).artificial_depth++;
183 }
184
185 static struct value *
186 inline_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
206 static int
207 inline_frame_sniffer (const struct frame_unwind *self,
208                       struct frame_info *this_frame,
209                       void **this_cache)
210 {
211   CORE_ADDR this_pc;
212   struct block *frame_block, *cur_block;
213   int depth;
214   struct frame_info *next_frame;
215   struct inline_state *state = find_inline_frame_state (inferior_ptid);
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++;
230
231       cur_block = BLOCK_SUPERBLOCK (cur_block);
232     }
233
234   /* Check how many inlined functions already have frames.  */
235   for (next_frame = get_next_frame (this_frame);
236        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
237        next_frame = get_next_frame (next_frame))
238     {
239       gdb_assert (depth > 0);
240       depth--;
241     }
242
243   /* If this is the topmost frame, or all frames above us are inlined,
244      then check whether we were requested to skip some frames (so they
245      can be stepped into later).  */
246   if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
247     {
248       gdb_assert (depth >= state->skipped_frames);
249       depth -= state->skipped_frames;
250     }
251
252   /* If all the inlined functions here already have frames, then pass
253      to the normal unwinder for this PC.  */
254   if (depth == 0)
255     return 0;
256
257   /* If the next frame is an inlined function, but not the outermost, then
258      we are the next outer.  If it is not an inlined function, then we
259      are the innermost inlined function of a different real frame.  */
260   return 1;
261 }
262
263 const struct frame_unwind inline_frame_unwind = {
264   INLINE_FRAME,
265   default_frame_unwind_stop_reason,
266   inline_frame_this_id,
267   inline_frame_prev_register,
268   NULL,
269   inline_frame_sniffer
270 };
271
272 /* Return non-zero if BLOCK, an inlined function block containing PC,
273    has a group of contiguous instructions starting at PC (but not
274    before it).  */
275
276 static int
277 block_starting_point_at (CORE_ADDR pc, struct block *block)
278 {
279   struct blockvector *bv;
280   struct block *new_block;
281
282   bv = blockvector_for_pc (pc, NULL);
283   if (BLOCKVECTOR_MAP (bv) == NULL)
284     return 0;
285
286   new_block = addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
287   if (new_block == NULL)
288     return 1;
289
290   if (new_block == block || contained_in (new_block, block))
291     return 0;
292
293   /* The immediately preceding address belongs to a different block,
294      which is not a child of this one.  Treat this as an entrance into
295      BLOCK.  */
296   return 1;
297 }
298
299 /* Skip all inlined functions whose call sites are at the current PC.
300    Frames for the hidden functions will not appear in the backtrace until the
301    user steps into them.  */
302
303 void
304 skip_inline_frames (ptid_t ptid)
305 {
306   CORE_ADDR this_pc;
307   struct block *frame_block, *cur_block;
308   struct symbol *last_sym = NULL;
309   int skip_count = 0;
310   struct inline_state *state;
311
312   /* This function is called right after reinitializing the frame
313      cache.  We try not to do more unwinding than absolutely
314      necessary, for performance.  */
315   this_pc = get_frame_pc (get_current_frame ());
316   frame_block = block_for_pc (this_pc);
317
318   if (frame_block != NULL)
319     {
320       cur_block = frame_block;
321       while (BLOCK_SUPERBLOCK (cur_block))
322         {
323           if (block_inlined_p (cur_block))
324             {
325               /* See comments in inline_frame_this_id about this use
326                  of BLOCK_START.  */
327               if (BLOCK_START (cur_block) == this_pc
328                   || block_starting_point_at (this_pc, cur_block))
329                 {
330                   skip_count++;
331                   last_sym = BLOCK_FUNCTION (cur_block);
332                 }
333               else
334                 break;
335             }
336           cur_block = BLOCK_SUPERBLOCK (cur_block);
337         }
338     }
339
340   gdb_assert (find_inline_frame_state (ptid) == NULL);
341   state = allocate_inline_frame_state (ptid);
342   state->skipped_frames = skip_count;
343   state->saved_pc = this_pc;
344   state->skipped_symbol = last_sym;
345
346   if (skip_count != 0)
347     reinit_frame_cache ();
348 }
349
350 /* Step into an inlined function by unhiding it.  */
351
352 void
353 step_into_inline_frame (ptid_t ptid)
354 {
355   struct inline_state *state = find_inline_frame_state (ptid);
356
357   gdb_assert (state != NULL && state->skipped_frames > 0);
358   state->skipped_frames--;
359   reinit_frame_cache ();
360 }
361
362 /* Return the number of hidden functions inlined into the current
363    frame.  */
364
365 int
366 inline_skipped_frames (ptid_t ptid)
367 {
368   struct inline_state *state = find_inline_frame_state (ptid);
369
370   if (state == NULL)
371     return 0;
372   else
373     return state->skipped_frames;
374 }
375
376 /* If one or more inlined functions are hidden, return the symbol for
377    the function inlined into the current frame.  */
378
379 struct symbol *
380 inline_skipped_symbol (ptid_t ptid)
381 {
382   struct inline_state *state = find_inline_frame_state (ptid);
383
384   gdb_assert (state != NULL);
385   return state->skipped_symbol;
386 }
387
388 /* Return the number of functions inlined into THIS_FRAME.  Some of
389    the callees may not have associated frames (see
390    skip_inline_frames).  */
391
392 int
393 frame_inlined_callees (struct frame_info *this_frame)
394 {
395   struct frame_info *next_frame;
396   int inline_count = 0;
397
398   /* First count how many inlined functions at this PC have frames
399      above FRAME (are inlined into FRAME).  */
400   for (next_frame = get_next_frame (this_frame);
401        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
402        next_frame = get_next_frame (next_frame))
403     inline_count++;
404
405   /* Simulate some most-inner inlined frames which were suppressed, so
406      they can be stepped into later.  If we are unwinding already
407      outer frames from some non-inlined frame this does not apply.  */
408   if (next_frame == NULL)
409     inline_count += inline_skipped_frames (inferior_ptid);
410
411   return inline_count;
412 }