Commit | Line | Data |
---|---|---|
7cc19214 AC |
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, | |
26b0da32 MK |
5 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
6 | Free Software Foundation, Inc. | |
c906108c | 7 | |
c5aa993b | 8 | This file is part of GDB. |
c906108c | 9 | |
c5aa993b JM |
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. | |
c906108c | 14 | |
c5aa993b JM |
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. | |
c906108c | 19 | |
c5aa993b JM |
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. */ | |
c906108c SS |
24 | |
25 | #include "defs.h" | |
26 | #include "symtab.h" | |
27 | #include "bfd.h" | |
c906108c SS |
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" | |
4e052eda | 35 | #include "regcache.h" |
4f460812 | 36 | #include "gdb_assert.h" |
9c1412c1 | 37 | #include "dummy-frame.h" |
51603483 DJ |
38 | #include "command.h" |
39 | #include "gdbcmd.h" | |
fe898f56 | 40 | #include "block.h" |
c906108c | 41 | |
51603483 | 42 | /* Prototypes for exported functions. */ |
c5aa993b | 43 | |
51603483 | 44 | void _initialize_blockframe (void); |
c906108c | 45 | |
f614e9d9 | 46 | /* Test whether PC is in the range of addresses that corresponds to |
c6831537 | 47 | the "main" function. */ |
c906108c SS |
48 | |
49 | int | |
fba45db2 | 50 | inside_main_func (CORE_ADDR pc) |
c906108c | 51 | { |
8d4ce20a JB |
52 | struct minimal_symbol *msymbol; |
53 | ||
c906108c SS |
54 | if (symfile_objfile == 0) |
55 | return 0; | |
56 | ||
8d4ce20a JB |
57 | msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile); |
58 | ||
f614e9d9 MK |
59 | /* If the address range hasn't been set up at symbol reading time, |
60 | set it up now. */ | |
c906108c | 61 | |
8d4ce20a JB |
62 | if (msymbol != NULL |
63 | && symfile_objfile->ei.main_func_lowpc == INVALID_ENTRY_LOWPC | |
64 | && symfile_objfile->ei.main_func_highpc == INVALID_ENTRY_HIGHPC) | |
c906108c | 65 | { |
f614e9d9 MK |
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)); | |
c906108c | 77 | |
c5aa993b JM |
78 | if (mainsym && SYMBOL_CLASS (mainsym) == LOC_BLOCK) |
79 | { | |
80 | symfile_objfile->ei.main_func_lowpc = | |
c906108c | 81 | BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym)); |
c5aa993b | 82 | symfile_objfile->ei.main_func_highpc = |
c906108c | 83 | BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym)); |
c5aa993b | 84 | } |
c906108c | 85 | } |
0714963c AC |
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. */ | |
f614e9d9 MK |
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); | |
c906108c SS |
128 | } |
129 | ||
6e4c6c91 | 130 | /* Test whether THIS_FRAME is inside the process entry point function. */ |
c906108c SS |
131 | |
132 | int | |
6e4c6c91 DJ |
133 | inside_entry_func (struct frame_info *this_frame) |
134 | { | |
135 | return (get_frame_func (this_frame) == entry_point_address ()); | |
136 | } | |
137 | ||
c906108c | 138 | /* Return the innermost lexical block in execution |
ae767bfb JB |
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. */ | |
c906108c SS |
153 | |
154 | struct block * | |
ae767bfb | 155 | get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) |
c906108c | 156 | { |
c4a09524 | 157 | const CORE_ADDR pc = get_frame_address_in_block (frame); |
ae767bfb JB |
158 | |
159 | if (addr_in_block) | |
160 | *addr_in_block = pc; | |
161 | ||
c906108c SS |
162 | return block_for_pc (pc); |
163 | } | |
164 | ||
c906108c | 165 | CORE_ADDR |
fba45db2 | 166 | get_pc_function_start (CORE_ADDR pc) |
c906108c | 167 | { |
2cdd89cb MK |
168 | struct block *bl; |
169 | struct minimal_symbol *msymbol; | |
c906108c | 170 | |
2cdd89cb MK |
171 | bl = block_for_pc (pc); |
172 | if (bl) | |
c906108c | 173 | { |
2cdd89cb MK |
174 | struct symbol *symbol = block_function (bl); |
175 | ||
176 | if (symbol) | |
177 | { | |
178 | bl = SYMBOL_BLOCK_VALUE (symbol); | |
179 | return BLOCK_START (bl); | |
180 | } | |
c906108c | 181 | } |
2cdd89cb MK |
182 | |
183 | msymbol = lookup_minimal_symbol_by_pc (pc); | |
184 | if (msymbol) | |
c906108c | 185 | { |
2cdd89cb MK |
186 | CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol); |
187 | ||
188 | if (find_pc_section (fstart)) | |
189 | return fstart; | |
c906108c | 190 | } |
2cdd89cb MK |
191 | |
192 | return 0; | |
c906108c SS |
193 | } |
194 | ||
195 | /* Return the symbol for the function executing in frame FRAME. */ | |
196 | ||
197 | struct symbol * | |
fba45db2 | 198 | get_frame_function (struct frame_info *frame) |
c906108c | 199 | { |
52f0bd74 | 200 | struct block *bl = get_frame_block (frame, 0); |
c906108c SS |
201 | if (bl == 0) |
202 | return 0; | |
203 | return block_function (bl); | |
204 | } | |
205 | \f | |
206 | ||
c906108c SS |
207 | /* Return the function containing pc value PC in section SECTION. |
208 | Returns 0 if function is not known. */ | |
209 | ||
210 | struct symbol * | |
198beae2 | 211 | find_pc_sect_function (CORE_ADDR pc, struct bfd_section *section) |
c906108c | 212 | { |
52f0bd74 | 213 | struct block *b = block_for_pc_sect (pc, section); |
c906108c SS |
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 * | |
fba45db2 | 223 | find_pc_function (CORE_ADDR pc) |
c906108c SS |
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 | ||
c5aa993b JM |
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; | |
198beae2 | 234 | static struct bfd_section *cache_pc_function_section = NULL; |
c906108c SS |
235 | |
236 | /* Clear cache, e.g. when symbol table is discarded. */ | |
237 | ||
238 | void | |
fba45db2 | 239 | clear_pc_function_cache (void) |
c906108c SS |
240 | { |
241 | cache_pc_function_low = 0; | |
242 | cache_pc_function_high = 0; | |
c5aa993b | 243 | cache_pc_function_name = (char *) 0; |
c906108c SS |
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 | ||
73912b9b AC |
258 | /* Backward compatibility, no section argument. */ |
259 | ||
c906108c | 260 | int |
73912b9b AC |
261 | find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address, |
262 | CORE_ADDR *endaddr) | |
c906108c | 263 | { |
73912b9b | 264 | struct bfd_section *section; |
c906108c | 265 | struct partial_symtab *pst; |
c5aa993b | 266 | struct symbol *f; |
c906108c SS |
267 | struct minimal_symbol *msymbol; |
268 | struct partial_symbol *psb; | |
c5aa993b | 269 | struct obj_section *osect; |
c906108c SS |
270 | int i; |
271 | CORE_ADDR mapped_pc; | |
272 | ||
73912b9b AC |
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 | ||
c906108c SS |
288 | mapped_pc = overlay_mapped_address (pc, section); |
289 | ||
247055de MK |
290 | if (mapped_pc >= cache_pc_function_low |
291 | && mapped_pc < cache_pc_function_high | |
292 | && section == cache_pc_function_section) | |
c906108c SS |
293 | goto return_cached_value; |
294 | ||
c906108c SS |
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 | { | |
c5aa993b JM |
318 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
319 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
22abf04a | 320 | cache_pc_function_name = DEPRECATED_SYMBOL_NAME (f); |
c906108c SS |
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) | |
22abf04a | 341 | *name = DEPRECATED_SYMBOL_NAME (psb); |
c906108c SS |
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 | ||
c5aa993b | 371 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); |
22abf04a | 372 | cache_pc_function_name = DEPRECATED_SYMBOL_NAME (msymbol); |
c906108c SS |
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. */ | |
c5aa993b | 377 | |
c906108c SS |
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 | ||
22abf04a | 382 | for (i = 1; DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL; i++) |
c906108c | 383 | { |
c5aa993b | 384 | if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol) |
247055de | 385 | && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol)) |
c906108c SS |
386 | break; |
387 | } | |
388 | ||
22abf04a | 389 | if (DEPRECATED_SYMBOL_NAME (msymbol + i) != NULL |
c906108c SS |
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 | ||
247055de | 397 | return_cached_value: |
c906108c SS |
398 | |
399 | if (address) | |
400 | { | |
401 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 402 | *address = overlay_unmapped_address (cache_pc_function_low, section); |
c906108c | 403 | else |
c5aa993b | 404 | *address = cache_pc_function_low; |
c906108c | 405 | } |
c5aa993b | 406 | |
c906108c SS |
407 | if (name) |
408 | *name = cache_pc_function_name; | |
409 | ||
410 | if (endaddr) | |
411 | { | |
412 | if (pc_in_unmapped_range (pc, section)) | |
c5aa993b | 413 | { |
c906108c SS |
414 | /* Because the high address is actually beyond the end of |
415 | the function (and therefore possibly beyond the end of | |
247055de MK |
416 | the overlay), we must actually convert (high - 1) and |
417 | then add one to that. */ | |
c906108c | 418 | |
c5aa993b | 419 | *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, |
c906108c | 420 | section); |
c5aa993b | 421 | } |
c906108c | 422 | else |
c5aa993b | 423 | *endaddr = cache_pc_function_high; |
c906108c SS |
424 | } |
425 | ||
426 | return 1; | |
427 | } | |
428 | ||
c906108c SS |
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 * | |
fba45db2 | 433 | block_innermost_frame (struct block *block) |
c906108c SS |
434 | { |
435 | struct frame_info *frame; | |
52f0bd74 AC |
436 | CORE_ADDR start; |
437 | CORE_ADDR end; | |
42f99ac2 | 438 | CORE_ADDR calling_pc; |
c906108c SS |
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; | |
c4a09524 | 452 | calling_pc = get_frame_address_in_block (frame); |
42f99ac2 | 453 | if (calling_pc >= start && calling_pc < end) |
c906108c SS |
454 | return frame; |
455 | } | |
456 | } |