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