gdb/
[external/binutils.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2    functions and pc values.
3
4    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
6    2010, 2011 Free Software Foundation, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "bfd.h"
26 #include "objfiles.h"
27 #include "frame.h"
28 #include "gdbcore.h"
29 #include "value.h"
30 #include "target.h"
31 #include "inferior.h"
32 #include "annotate.h"
33 #include "regcache.h"
34 #include "gdb_assert.h"
35 #include "dummy-frame.h"
36 #include "command.h"
37 #include "gdbcmd.h"
38 #include "block.h"
39 #include "inline-frame.h"
40 #include "psymtab.h"
41
42 /* Return the innermost lexical block in execution in a specified
43    stack frame.  The frame address is assumed valid.
44
45    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
46    address we used to choose the block.  We use this to find a source
47    line, to decide which macro definitions are in scope.
48
49    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
50    PC, and may not really be a valid PC at all.  For example, in the
51    caller of a function declared to never return, the code at the
52    return address will never be reached, so the call instruction may
53    be the very last instruction in the block.  So the address we use
54    to choose the block is actually one byte before the return address
55    --- hopefully pointing us at the call instruction, or its delay
56    slot instruction.  */
57
58 struct block *
59 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
60 {
61   CORE_ADDR pc;
62   struct block *bl;
63   int inline_count;
64
65   if (!get_frame_address_in_block_if_available (frame, &pc))
66     return NULL;
67
68   if (addr_in_block)
69     *addr_in_block = pc;
70
71   bl = block_for_pc (pc);
72   if (bl == NULL)
73     return NULL;
74
75   inline_count = frame_inlined_callees (frame);
76
77   while (inline_count > 0)
78     {
79       if (block_inlined_p (bl))
80         inline_count--;
81
82       bl = BLOCK_SUPERBLOCK (bl);
83       gdb_assert (bl != NULL);
84     }
85
86   return bl;
87 }
88
89 CORE_ADDR
90 get_pc_function_start (CORE_ADDR pc)
91 {
92   struct block *bl;
93   struct minimal_symbol *msymbol;
94
95   bl = block_for_pc (pc);
96   if (bl)
97     {
98       struct symbol *symbol = block_linkage_function (bl);
99
100       if (symbol)
101         {
102           bl = SYMBOL_BLOCK_VALUE (symbol);
103           return BLOCK_START (bl);
104         }
105     }
106
107   msymbol = lookup_minimal_symbol_by_pc (pc);
108   if (msymbol)
109     {
110       CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
111
112       if (find_pc_section (fstart))
113         return fstart;
114     }
115
116   return 0;
117 }
118
119 /* Return the symbol for the function executing in frame FRAME.  */
120
121 struct symbol *
122 get_frame_function (struct frame_info *frame)
123 {
124   struct block *bl = get_frame_block (frame, 0);
125
126   if (bl == NULL)
127     return NULL;
128
129   while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
130     bl = BLOCK_SUPERBLOCK (bl);
131
132   return BLOCK_FUNCTION (bl);
133 }
134 \f
135
136 /* Return the function containing pc value PC in section SECTION.
137    Returns 0 if function is not known.  */
138
139 struct symbol *
140 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
141 {
142   struct block *b = block_for_pc_sect (pc, section);
143
144   if (b == 0)
145     return 0;
146   return block_linkage_function (b);
147 }
148
149 /* Return the function containing pc value PC.
150    Returns 0 if function is not known.  
151    Backward compatibility, no section */
152
153 struct symbol *
154 find_pc_function (CORE_ADDR pc)
155 {
156   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
157 }
158
159 /* These variables are used to cache the most recent result
160    of find_pc_partial_function.  */
161
162 static CORE_ADDR cache_pc_function_low = 0;
163 static CORE_ADDR cache_pc_function_high = 0;
164 static char *cache_pc_function_name = 0;
165 static struct obj_section *cache_pc_function_section = NULL;
166
167 /* Clear cache, e.g. when symbol table is discarded.  */
168
169 void
170 clear_pc_function_cache (void)
171 {
172   cache_pc_function_low = 0;
173   cache_pc_function_high = 0;
174   cache_pc_function_name = (char *) 0;
175   cache_pc_function_section = NULL;
176 }
177
178 /* Finds the "function" (text symbol) that is smaller than PC but
179    greatest of all of the potential text symbols in SECTION.  Sets
180    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
181    If ENDADDR is non-null, then set *ENDADDR to be the end of the
182    function (exclusive), but passing ENDADDR as non-null means that
183    the function might cause symbols to be read.  This function either
184    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
185    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
186    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
187    returns 0.  */
188
189 /* Backward compatibility, no section argument.  */
190
191 int
192 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
193                           CORE_ADDR *endaddr)
194 {
195   struct obj_section *section;
196   struct symbol *f;
197   struct minimal_symbol *msymbol;
198   struct symtab *symtab = NULL;
199   struct objfile *objfile;
200   int i;
201   CORE_ADDR mapped_pc;
202
203   /* To ensure that the symbol returned belongs to the correct setion
204      (and that the last [random] symbol from the previous section
205      isn't returned) try to find the section containing PC.  First try
206      the overlay code (which by default returns NULL); and second try
207      the normal section code (which almost always succeeds).  */
208   section = find_pc_overlay (pc);
209   if (section == NULL)
210     section = find_pc_section (pc);
211
212   mapped_pc = overlay_mapped_address (pc, section);
213
214   if (mapped_pc >= cache_pc_function_low
215       && mapped_pc < cache_pc_function_high
216       && section == cache_pc_function_section)
217     goto return_cached_value;
218
219   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
220   ALL_OBJFILES (objfile)
221   {
222     if (objfile->sf)
223       symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
224                                                      mapped_pc, section, 0);
225     if (symtab)
226       break;
227   }
228
229   if (symtab)
230     {
231       /* Checking whether the msymbol has a larger value is for the
232          "pathological" case mentioned in print_frame_info.  */
233       f = find_pc_sect_function (mapped_pc, section);
234       if (f != NULL
235           && (msymbol == NULL
236               || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
237                   >= SYMBOL_VALUE_ADDRESS (msymbol))))
238         {
239           cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
240           cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
241           cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
242           cache_pc_function_section = section;
243           goto return_cached_value;
244         }
245     }
246
247   /* Not in the normal symbol tables, see if the pc is in a known
248      section.  If it's not, then give up.  This ensures that anything
249      beyond the end of the text seg doesn't appear to be part of the
250      last function in the text segment.  */
251
252   if (!section)
253     msymbol = NULL;
254
255   /* Must be in the minimal symbol table.  */
256   if (msymbol == NULL)
257     {
258       /* No available symbol.  */
259       if (name != NULL)
260         *name = 0;
261       if (address != NULL)
262         *address = 0;
263       if (endaddr != NULL)
264         *endaddr = 0;
265       return 0;
266     }
267
268   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
269   cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
270   cache_pc_function_section = section;
271
272   /* If the minimal symbol has a size, use it for the cache.
273      Otherwise use the lesser of the next minimal symbol in the same
274      section, or the end of the section, as the end of the
275      function.  */
276
277   if (MSYMBOL_SIZE (msymbol) != 0)
278     cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
279   else
280     {
281       /* Step over other symbols at this same address, and symbols in
282          other sections, to find the next symbol in this section with
283          a different address.  */
284
285       for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
286         {
287           if (SYMBOL_VALUE_ADDRESS (msymbol + i)
288               != SYMBOL_VALUE_ADDRESS (msymbol)
289               && SYMBOL_OBJ_SECTION (msymbol + i)
290               == SYMBOL_OBJ_SECTION (msymbol))
291             break;
292         }
293
294       if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
295           && SYMBOL_VALUE_ADDRESS (msymbol + i)
296           < obj_section_endaddr (section))
297         cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
298       else
299         /* We got the start address from the last msymbol in the objfile.
300            So the end address is the end of the section.  */
301         cache_pc_function_high = obj_section_endaddr (section);
302     }
303
304  return_cached_value:
305
306   if (address)
307     {
308       if (pc_in_unmapped_range (pc, section))
309         *address = overlay_unmapped_address (cache_pc_function_low, section);
310       else
311         *address = cache_pc_function_low;
312     }
313
314   if (name)
315     *name = cache_pc_function_name;
316
317   if (endaddr)
318     {
319       if (pc_in_unmapped_range (pc, section))
320         {
321           /* Because the high address is actually beyond the end of
322              the function (and therefore possibly beyond the end of
323              the overlay), we must actually convert (high - 1) and
324              then add one to that.  */
325
326           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
327                                                    section);
328         }
329       else
330         *endaddr = cache_pc_function_high;
331     }
332
333   return 1;
334 }
335
336 /* Return the innermost stack frame executing inside of BLOCK, or NULL
337    if there is no such frame.  If BLOCK is NULL, just return NULL.  */
338
339 struct frame_info *
340 block_innermost_frame (struct block *block)
341 {
342   struct frame_info *frame;
343   CORE_ADDR start;
344   CORE_ADDR end;
345
346   if (block == NULL)
347     return NULL;
348
349   start = BLOCK_START (block);
350   end = BLOCK_END (block);
351
352   frame = get_current_frame ();
353   while (frame != NULL)
354     {
355       struct block *frame_block = get_frame_block (frame, NULL);
356       if (frame_block != NULL && contained_in (frame_block, block))
357         return frame;
358
359       frame = get_prev_frame (frame);
360     }
361
362   return NULL;
363 }