2004-08-02 Andrew Cagney <cagney@gnu.org>
[external/binutils.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2    functions and pc values.
3
4    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
6    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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "objfiles.h"
29 #include "frame.h"
30 #include "gdbcore.h"
31 #include "value.h"              /* for read_register */
32 #include "target.h"             /* for target_has_stack */
33 #include "inferior.h"           /* for read_pc */
34 #include "annotate.h"
35 #include "regcache.h"
36 #include "gdb_assert.h"
37 #include "dummy-frame.h"
38 #include "command.h"
39 #include "gdbcmd.h"
40 #include "block.h"
41
42 /* Prototypes for exported functions. */
43
44 void _initialize_blockframe (void);
45
46 /* Test whether PC is in the range of addresses that corresponds to
47    the "main" function.  */
48
49 int
50 inside_main_func (CORE_ADDR pc)
51 {
52   struct minimal_symbol *msymbol;
53
54   if (symfile_objfile == 0)
55     return 0;
56
57   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
58
59   /* If the address range hasn't been set up at symbol reading time,
60      set it up now.  */
61
62   if (msymbol != NULL
63       && symfile_objfile->ei.main_func_lowpc == INVALID_ENTRY_LOWPC
64       && symfile_objfile->ei.main_func_highpc == INVALID_ENTRY_HIGHPC)
65     {
66       /* brobecker/2003-10-10: We used to rely on lookup_symbol() to
67          search the symbol associated to the "main" function.
68          Unfortunately, lookup_symbol() uses the current-language
69          la_lookup_symbol_nonlocal function to do the global symbol
70          search.  Depending on the language, this can introduce
71          certain side-effects, because certain languages, for instance
72          Ada, may find more than one match.  Therefore we prefer to
73          search the "main" function symbol using its address rather
74          than its name.  */
75       struct symbol *mainsym =
76         find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
77
78       if (mainsym && SYMBOL_CLASS (mainsym) == LOC_BLOCK)
79         {
80           symfile_objfile->ei.main_func_lowpc =
81             BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
82           symfile_objfile->ei.main_func_highpc =
83             BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
84         }
85     }
86
87   /* Not in the normal symbol tables, see if "main" is in the partial
88      symbol table.  If it's not, then give up.  */
89   if (msymbol != NULL && MSYMBOL_TYPE (msymbol) == mst_text)
90     {
91       CORE_ADDR maddr = SYMBOL_VALUE_ADDRESS (msymbol);
92       asection *msect = SYMBOL_BFD_SECTION (msymbol);
93       struct obj_section *osect = find_pc_sect_section (maddr, msect);
94
95       if (osect != NULL)
96         {
97           int i;
98
99           /* Step over other symbols at this same address, and symbols
100              in other sections, to find the next symbol in this
101              section with a different address.  */
102           for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
103             {
104               if (SYMBOL_VALUE_ADDRESS (msymbol + i) != maddr
105                   && SYMBOL_BFD_SECTION (msymbol + i) == msect)
106                 break;
107             }
108
109           symfile_objfile->ei.main_func_lowpc = maddr;
110
111           /* Use the lesser of the next minimal symbol in the same
112              section, or the end of the section, as the end of the
113              function.  */
114           if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
115               && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
116             symfile_objfile->ei.main_func_highpc =
117               SYMBOL_VALUE_ADDRESS (msymbol + i);
118           else
119             /* We got the start address from the last msymbol in the
120                objfile.  So the end address is the end of the
121                section.  */
122             symfile_objfile->ei.main_func_highpc = osect->endaddr;
123         }
124     }
125
126   return (symfile_objfile->ei.main_func_lowpc <= pc
127           && symfile_objfile->ei.main_func_highpc > pc);
128 }
129
130 /* Test whether THIS_FRAME is inside the process entry point function.  */
131
132 int
133 inside_entry_func (struct frame_info *this_frame)
134 {
135   return (get_frame_func (this_frame) == entry_point_address ());
136 }
137
138 /* Return the innermost lexical block in execution
139    in a specified stack frame.  The frame address is assumed valid.
140
141    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
142    address we used to choose the block.  We use this to find a source
143    line, to decide which macro definitions are in scope.
144
145    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
146    PC, and may not really be a valid PC at all.  For example, in the
147    caller of a function declared to never return, the code at the
148    return address will never be reached, so the call instruction may
149    be the very last instruction in the block.  So the address we use
150    to choose the block is actually one byte before the return address
151    --- hopefully pointing us at the call instruction, or its delay
152    slot instruction.  */
153
154 struct block *
155 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
156 {
157   const CORE_ADDR pc = get_frame_address_in_block (frame);
158
159   if (addr_in_block)
160     *addr_in_block = pc;
161
162   return block_for_pc (pc);
163 }
164
165 CORE_ADDR
166 get_pc_function_start (CORE_ADDR pc)
167 {
168   struct block *bl;
169   struct minimal_symbol *msymbol;
170
171   bl = block_for_pc (pc);
172   if (bl)
173     {
174       struct symbol *symbol = block_function (bl);
175
176       if (symbol)
177         {
178           bl = SYMBOL_BLOCK_VALUE (symbol);
179           return BLOCK_START (bl);
180         }
181     }
182
183   msymbol = lookup_minimal_symbol_by_pc (pc);
184   if (msymbol)
185     {
186       CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
187
188       if (find_pc_section (fstart))
189         return fstart;
190     }
191
192   return 0;
193 }
194
195 /* Return the symbol for the function executing in frame FRAME.  */
196
197 struct symbol *
198 get_frame_function (struct frame_info *frame)
199 {
200   struct block *bl = get_frame_block (frame, 0);
201   if (bl == 0)
202     return 0;
203   return block_function (bl);
204 }
205 \f
206
207 /* Return the function containing pc value PC in section SECTION.
208    Returns 0 if function is not known.  */
209
210 struct symbol *
211 find_pc_sect_function (CORE_ADDR pc, struct bfd_section *section)
212 {
213   struct block *b = block_for_pc_sect (pc, section);
214   if (b == 0)
215     return 0;
216   return block_function (b);
217 }
218
219 /* Return the function containing pc value PC.
220    Returns 0 if function is not known.  Backward compatibility, no section */
221
222 struct symbol *
223 find_pc_function (CORE_ADDR pc)
224 {
225   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
226 }
227
228 /* These variables are used to cache the most recent result
229  * of find_pc_partial_function. */
230
231 static CORE_ADDR cache_pc_function_low = 0;
232 static CORE_ADDR cache_pc_function_high = 0;
233 static char *cache_pc_function_name = 0;
234 static struct bfd_section *cache_pc_function_section = NULL;
235
236 /* Clear cache, e.g. when symbol table is discarded. */
237
238 void
239 clear_pc_function_cache (void)
240 {
241   cache_pc_function_low = 0;
242   cache_pc_function_high = 0;
243   cache_pc_function_name = (char *) 0;
244   cache_pc_function_section = NULL;
245 }
246
247 /* Finds the "function" (text symbol) that is smaller than PC but
248    greatest of all of the potential text symbols in SECTION.  Sets
249    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
250    If ENDADDR is non-null, then set *ENDADDR to be the end of the
251    function (exclusive), but passing ENDADDR as non-null means that
252    the function might cause symbols to be read.  This function either
253    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
254    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
255    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
256    returns 0.  */
257
258 /* Backward compatibility, no section argument.  */
259
260 int
261 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
262                           CORE_ADDR *endaddr)
263 {
264   struct bfd_section *section;
265   struct partial_symtab *pst;
266   struct symbol *f;
267   struct minimal_symbol *msymbol;
268   struct partial_symbol *psb;
269   struct obj_section *osect;
270   int i;
271   CORE_ADDR mapped_pc;
272
273   /* To ensure that the symbol returned belongs to the correct setion
274      (and that the last [random] symbol from the previous section
275      isn't returned) try to find the section containing PC.  First try
276      the overlay code (which by default returns NULL); and second try
277      the normal section code (which almost always succeeds).  */
278   section = find_pc_overlay (pc);
279   if (section == NULL)
280     {
281       struct obj_section *obj_section = find_pc_section (pc);
282       if (obj_section == NULL)
283         section = NULL;
284       else
285         section = obj_section->the_bfd_section;
286     }
287
288   mapped_pc = overlay_mapped_address (pc, section);
289
290   if (mapped_pc >= cache_pc_function_low
291       && mapped_pc < cache_pc_function_high
292       && section == cache_pc_function_section)
293     goto return_cached_value;
294
295   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
296   pst = find_pc_sect_psymtab (mapped_pc, section);
297   if (pst)
298     {
299       /* Need to read the symbols to get a good value for the end address.  */
300       if (endaddr != NULL && !pst->readin)
301         {
302           /* Need to get the terminal in case symbol-reading produces
303              output.  */
304           target_terminal_ours_for_output ();
305           PSYMTAB_TO_SYMTAB (pst);
306         }
307
308       if (pst->readin)
309         {
310           /* Checking whether the msymbol has a larger value is for the
311              "pathological" case mentioned in print_frame_info.  */
312           f = find_pc_sect_function (mapped_pc, section);
313           if (f != NULL
314               && (msymbol == NULL
315                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
316                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
317             {
318               cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
319               cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
320               cache_pc_function_name = DEPRECATED_SYMBOL_NAME (f);
321               cache_pc_function_section = section;
322               goto return_cached_value;
323             }
324         }
325       else
326         {
327           /* Now that static symbols go in the minimal symbol table, perhaps
328              we could just ignore the partial symbols.  But at least for now
329              we use the partial or minimal symbol, whichever is larger.  */
330           psb = find_pc_sect_psymbol (pst, mapped_pc, section);
331
332           if (psb
333               && (msymbol == NULL ||
334                   (SYMBOL_VALUE_ADDRESS (psb)
335                    >= SYMBOL_VALUE_ADDRESS (msymbol))))
336             {
337               /* This case isn't being cached currently. */
338               if (address)
339                 *address = SYMBOL_VALUE_ADDRESS (psb);
340               if (name)
341                 *name = DEPRECATED_SYMBOL_NAME (psb);
342               /* endaddr non-NULL can't happen here.  */
343               return 1;
344             }
345         }
346     }
347
348   /* Not in the normal symbol tables, see if the pc is in a known section.
349      If it's not, then give up.  This ensures that anything beyond the end
350      of the text seg doesn't appear to be part of the last function in the
351      text segment.  */
352
353   osect = find_pc_sect_section (mapped_pc, section);
354
355   if (!osect)
356     msymbol = NULL;
357
358   /* Must be in the minimal symbol table.  */
359   if (msymbol == NULL)
360     {
361       /* No available symbol.  */
362       if (name != NULL)
363         *name = 0;
364       if (address != NULL)
365         *address = 0;
366       if (endaddr != NULL)
367         *endaddr = 0;
368       return 0;
369     }
370
371   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
372   cache_pc_function_name = DEPRECATED_SYMBOL_NAME (msymbol);
373   cache_pc_function_section = section;
374
375   /* Use the lesser of the next minimal symbol in the same section, or
376      the end of the section, as the end of the function.  */
377
378   /* Step over other symbols at this same address, and symbols in
379      other sections, to find the next symbol in this section with
380      a different address.  */
381
382   for (i = 1; DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL; i++)
383     {
384       if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
385           && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
386         break;
387     }
388
389   if (DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL
390       && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
391     cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
392   else
393     /* We got the start address from the last msymbol in the objfile.
394        So the end address is the end of the section.  */
395     cache_pc_function_high = osect->endaddr;
396
397  return_cached_value:
398
399   if (address)
400     {
401       if (pc_in_unmapped_range (pc, section))
402         *address = overlay_unmapped_address (cache_pc_function_low, section);
403       else
404         *address = cache_pc_function_low;
405     }
406
407   if (name)
408     *name = cache_pc_function_name;
409
410   if (endaddr)
411     {
412       if (pc_in_unmapped_range (pc, section))
413         {
414           /* Because the high address is actually beyond the end of
415              the function (and therefore possibly beyond the end of
416              the overlay), we must actually convert (high - 1) and
417              then add one to that. */
418
419           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
420                                                    section);
421         }
422       else
423         *endaddr = cache_pc_function_high;
424     }
425
426   return 1;
427 }
428
429 /* Return the innermost stack frame executing inside of BLOCK,
430    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
431
432 struct frame_info *
433 block_innermost_frame (struct block *block)
434 {
435   struct frame_info *frame;
436   CORE_ADDR start;
437   CORE_ADDR end;
438   CORE_ADDR calling_pc;
439
440   if (block == NULL)
441     return NULL;
442
443   start = BLOCK_START (block);
444   end = BLOCK_END (block);
445
446   frame = NULL;
447   while (1)
448     {
449       frame = get_prev_frame (frame);
450       if (frame == NULL)
451         return NULL;
452       calling_pc = get_frame_address_in_block (frame);
453       if (calling_pc >= start && calling_pc < end)
454         return frame;
455     }
456 }