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