OSDN Git Service

Fix typo in filename.
[pf3gnuchains/pf3gnuchains3x.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    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
41 /* Return the innermost lexical block in execution
42    in a specified stack frame.  The frame address is assumed valid.
43
44    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
45    address we used to choose the block.  We use this to find a source
46    line, to decide which macro definitions are in scope.
47
48    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
49    PC, and may not really be a valid PC at all.  For example, in the
50    caller of a function declared to never return, the code at the
51    return address will never be reached, so the call instruction may
52    be the very last instruction in the block.  So the address we use
53    to choose the block is actually one byte before the return address
54    --- hopefully pointing us at the call instruction, or its delay
55    slot instruction.  */
56
57 struct block *
58 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
59 {
60   const CORE_ADDR pc = get_frame_address_in_block (frame);
61   struct frame_info *next_frame;
62   struct block *bl;
63   int inline_count;
64
65   if (addr_in_block)
66     *addr_in_block = pc;
67
68   bl = block_for_pc (pc);
69   if (bl == NULL)
70     return NULL;
71
72   inline_count = frame_inlined_callees (frame);
73
74   while (inline_count > 0)
75     {
76       if (block_inlined_p (bl))
77         inline_count--;
78
79       bl = BLOCK_SUPERBLOCK (bl);
80       gdb_assert (bl != NULL);
81     }
82
83   return bl;
84 }
85
86 CORE_ADDR
87 get_pc_function_start (CORE_ADDR pc)
88 {
89   struct block *bl;
90   struct minimal_symbol *msymbol;
91
92   bl = block_for_pc (pc);
93   if (bl)
94     {
95       struct symbol *symbol = block_linkage_function (bl);
96
97       if (symbol)
98         {
99           bl = SYMBOL_BLOCK_VALUE (symbol);
100           return BLOCK_START (bl);
101         }
102     }
103
104   msymbol = lookup_minimal_symbol_by_pc (pc);
105   if (msymbol)
106     {
107       CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol);
108
109       if (find_pc_section (fstart))
110         return fstart;
111     }
112
113   return 0;
114 }
115
116 /* Return the symbol for the function executing in frame FRAME.  */
117
118 struct symbol *
119 get_frame_function (struct frame_info *frame)
120 {
121   struct block *bl = get_frame_block (frame, 0);
122
123   if (bl == NULL)
124     return NULL;
125
126   while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
127     bl = BLOCK_SUPERBLOCK (bl);
128
129   return BLOCK_FUNCTION (bl);
130 }
131 \f
132
133 /* Return the function containing pc value PC in section SECTION.
134    Returns 0 if function is not known.  */
135
136 struct symbol *
137 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
138 {
139   struct block *b = block_for_pc_sect (pc, section);
140   if (b == 0)
141     return 0;
142   return block_linkage_function (b);
143 }
144
145 /* Return the function containing pc value PC.
146    Returns 0 if function is not known.  Backward compatibility, no section */
147
148 struct symbol *
149 find_pc_function (CORE_ADDR pc)
150 {
151   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
152 }
153
154 /* These variables are used to cache the most recent result
155  * of find_pc_partial_function. */
156
157 static CORE_ADDR cache_pc_function_low = 0;
158 static CORE_ADDR cache_pc_function_high = 0;
159 static char *cache_pc_function_name = 0;
160 static struct obj_section *cache_pc_function_section = NULL;
161
162 /* Clear cache, e.g. when symbol table is discarded. */
163
164 void
165 clear_pc_function_cache (void)
166 {
167   cache_pc_function_low = 0;
168   cache_pc_function_high = 0;
169   cache_pc_function_name = (char *) 0;
170   cache_pc_function_section = NULL;
171 }
172
173 /* Finds the "function" (text symbol) that is smaller than PC but
174    greatest of all of the potential text symbols in SECTION.  Sets
175    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
176    If ENDADDR is non-null, then set *ENDADDR to be the end of the
177    function (exclusive), but passing ENDADDR as non-null means that
178    the function might cause symbols to be read.  This function either
179    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
180    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
181    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
182    returns 0.  */
183
184 /* Backward compatibility, no section argument.  */
185
186 int
187 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
188                           CORE_ADDR *endaddr)
189 {
190   struct obj_section *section;
191   struct partial_symtab *pst;
192   struct symbol *f;
193   struct minimal_symbol *msymbol;
194   struct partial_symbol *psb;
195   int i;
196   CORE_ADDR mapped_pc;
197
198   /* To ensure that the symbol returned belongs to the correct setion
199      (and that the last [random] symbol from the previous section
200      isn't returned) try to find the section containing PC.  First try
201      the overlay code (which by default returns NULL); and second try
202      the normal section code (which almost always succeeds).  */
203   section = find_pc_overlay (pc);
204   if (section == NULL)
205     section = find_pc_section (pc);
206
207   mapped_pc = overlay_mapped_address (pc, section);
208
209   if (mapped_pc >= cache_pc_function_low
210       && mapped_pc < cache_pc_function_high
211       && section == cache_pc_function_section)
212     goto return_cached_value;
213
214   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
215   pst = find_pc_sect_psymtab (mapped_pc, section);
216   if (pst)
217     {
218       /* Need to read the symbols to get a good value for the end address.  */
219       if (endaddr != NULL && !pst->readin)
220         {
221           /* Need to get the terminal in case symbol-reading produces
222              output.  */
223           target_terminal_ours_for_output ();
224           PSYMTAB_TO_SYMTAB (pst);
225         }
226
227       if (pst->readin)
228         {
229           /* Checking whether the msymbol has a larger value is for the
230              "pathological" case mentioned in print_frame_info.  */
231           f = find_pc_sect_function (mapped_pc, section);
232           if (f != NULL
233               && (msymbol == NULL
234                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
235                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
236             {
237               cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
238               cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
239               cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
240               cache_pc_function_section = section;
241               goto return_cached_value;
242             }
243         }
244       else
245         {
246           /* Now that static symbols go in the minimal symbol table, perhaps
247              we could just ignore the partial symbols.  But at least for now
248              we use the partial or minimal symbol, whichever is larger.  */
249           psb = find_pc_sect_psymbol (pst, mapped_pc, section);
250
251           if (psb
252               && (msymbol == NULL
253                   || (SYMBOL_VALUE_ADDRESS (psb)
254                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
255             {
256               /* This case isn't being cached currently. */
257               if (address)
258                 *address = SYMBOL_VALUE_ADDRESS (psb);
259               if (name)
260                 *name = SYMBOL_LINKAGE_NAME (psb);
261               /* endaddr non-NULL can't happen here.  */
262               return 1;
263             }
264         }
265     }
266
267   /* Not in the normal symbol tables, see if the pc is in a known section.
268      If it's not, then give up.  This ensures that anything beyond the end
269      of the text seg doesn't appear to be part of the last function in the
270      text segment.  */
271
272   if (!section)
273     msymbol = NULL;
274
275   /* Must be in the minimal symbol table.  */
276   if (msymbol == NULL)
277     {
278       /* No available symbol.  */
279       if (name != NULL)
280         *name = 0;
281       if (address != NULL)
282         *address = 0;
283       if (endaddr != NULL)
284         *endaddr = 0;
285       return 0;
286     }
287
288   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
289   cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
290   cache_pc_function_section = section;
291
292   /* If the minimal symbol has a size, use it for the cache.
293      Otherwise use the lesser of the next minimal symbol in the same
294      section, or the end of the section, as the end of the
295      function.  */
296
297   if (MSYMBOL_SIZE (msymbol) != 0)
298     cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
299   else
300     {
301       /* Step over other symbols at this same address, and symbols in
302          other sections, to find the next symbol in this section with
303          a different address.  */
304
305       for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
306         {
307           if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
308               && SYMBOL_OBJ_SECTION (msymbol + i) == SYMBOL_OBJ_SECTION (msymbol))
309             break;
310         }
311
312       if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
313           && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (section))
314         cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
315       else
316         /* We got the start address from the last msymbol in the objfile.
317            So the end address is the end of the section.  */
318         cache_pc_function_high = obj_section_endaddr (section);
319     }
320
321  return_cached_value:
322
323   if (address)
324     {
325       if (pc_in_unmapped_range (pc, section))
326         *address = overlay_unmapped_address (cache_pc_function_low, section);
327       else
328         *address = cache_pc_function_low;
329     }
330
331   if (name)
332     *name = cache_pc_function_name;
333
334   if (endaddr)
335     {
336       if (pc_in_unmapped_range (pc, section))
337         {
338           /* Because the high address is actually beyond the end of
339              the function (and therefore possibly beyond the end of
340              the overlay), we must actually convert (high - 1) and
341              then add one to that. */
342
343           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
344                                                    section);
345         }
346       else
347         *endaddr = cache_pc_function_high;
348     }
349
350   return 1;
351 }
352
353 /* Return the innermost stack frame executing inside of BLOCK,
354    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
355
356 struct frame_info *
357 block_innermost_frame (struct block *block)
358 {
359   struct frame_info *frame;
360   CORE_ADDR start;
361   CORE_ADDR end;
362
363   if (block == NULL)
364     return NULL;
365
366   start = BLOCK_START (block);
367   end = BLOCK_END (block);
368
369   frame = get_current_frame ();
370   while (frame != NULL)
371     {
372       struct block *frame_block = get_frame_block (frame, NULL);
373       if (frame_block != NULL && contained_in (frame_block, block))
374         return frame;
375
376       frame = get_prev_frame (frame);
377     }
378
379   return NULL;
380 }