15796c8dcSSimon Schubert /* Get info from stack frames; convert between frames, blocks, 25796c8dcSSimon Schubert functions and pc values. 35796c8dcSSimon Schubert 45796c8dcSSimon Schubert Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 5*cf7f2e2dSJohn Marino 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 6*cf7f2e2dSJohn Marino 2010 Free Software Foundation, Inc. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This file is part of GDB. 95796c8dcSSimon Schubert 105796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 115796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 125796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 135796c8dcSSimon Schubert (at your option) any later version. 145796c8dcSSimon Schubert 155796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 165796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 175796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 185796c8dcSSimon Schubert GNU General Public License for more details. 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 215796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 225796c8dcSSimon Schubert 235796c8dcSSimon Schubert #include "defs.h" 245796c8dcSSimon Schubert #include "symtab.h" 255796c8dcSSimon Schubert #include "bfd.h" 265796c8dcSSimon Schubert #include "objfiles.h" 275796c8dcSSimon Schubert #include "frame.h" 285796c8dcSSimon Schubert #include "gdbcore.h" 295796c8dcSSimon Schubert #include "value.h" 305796c8dcSSimon Schubert #include "target.h" 315796c8dcSSimon Schubert #include "inferior.h" 325796c8dcSSimon Schubert #include "annotate.h" 335796c8dcSSimon Schubert #include "regcache.h" 345796c8dcSSimon Schubert #include "gdb_assert.h" 355796c8dcSSimon Schubert #include "dummy-frame.h" 365796c8dcSSimon Schubert #include "command.h" 375796c8dcSSimon Schubert #include "gdbcmd.h" 385796c8dcSSimon Schubert #include "block.h" 395796c8dcSSimon Schubert #include "inline-frame.h" 40*cf7f2e2dSJohn Marino #include "psymtab.h" 415796c8dcSSimon Schubert 425796c8dcSSimon Schubert /* Return the innermost lexical block in execution 435796c8dcSSimon Schubert in a specified stack frame. The frame address is assumed valid. 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code 465796c8dcSSimon Schubert address we used to choose the block. We use this to find a source 475796c8dcSSimon Schubert line, to decide which macro definitions are in scope. 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's 505796c8dcSSimon Schubert PC, and may not really be a valid PC at all. For example, in the 515796c8dcSSimon Schubert caller of a function declared to never return, the code at the 525796c8dcSSimon Schubert return address will never be reached, so the call instruction may 535796c8dcSSimon Schubert be the very last instruction in the block. So the address we use 545796c8dcSSimon Schubert to choose the block is actually one byte before the return address 555796c8dcSSimon Schubert --- hopefully pointing us at the call instruction, or its delay 565796c8dcSSimon Schubert slot instruction. */ 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert struct block * 595796c8dcSSimon Schubert get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block) 605796c8dcSSimon Schubert { 615796c8dcSSimon Schubert const CORE_ADDR pc = get_frame_address_in_block (frame); 625796c8dcSSimon Schubert struct block *bl; 635796c8dcSSimon Schubert int inline_count; 645796c8dcSSimon Schubert 655796c8dcSSimon Schubert if (addr_in_block) 665796c8dcSSimon Schubert *addr_in_block = pc; 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert bl = block_for_pc (pc); 695796c8dcSSimon Schubert if (bl == NULL) 705796c8dcSSimon Schubert return NULL; 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert inline_count = frame_inlined_callees (frame); 735796c8dcSSimon Schubert 745796c8dcSSimon Schubert while (inline_count > 0) 755796c8dcSSimon Schubert { 765796c8dcSSimon Schubert if (block_inlined_p (bl)) 775796c8dcSSimon Schubert inline_count--; 785796c8dcSSimon Schubert 795796c8dcSSimon Schubert bl = BLOCK_SUPERBLOCK (bl); 805796c8dcSSimon Schubert gdb_assert (bl != NULL); 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert 835796c8dcSSimon Schubert return bl; 845796c8dcSSimon Schubert } 855796c8dcSSimon Schubert 865796c8dcSSimon Schubert CORE_ADDR 875796c8dcSSimon Schubert get_pc_function_start (CORE_ADDR pc) 885796c8dcSSimon Schubert { 895796c8dcSSimon Schubert struct block *bl; 905796c8dcSSimon Schubert struct minimal_symbol *msymbol; 915796c8dcSSimon Schubert 925796c8dcSSimon Schubert bl = block_for_pc (pc); 935796c8dcSSimon Schubert if (bl) 945796c8dcSSimon Schubert { 955796c8dcSSimon Schubert struct symbol *symbol = block_linkage_function (bl); 965796c8dcSSimon Schubert 975796c8dcSSimon Schubert if (symbol) 985796c8dcSSimon Schubert { 995796c8dcSSimon Schubert bl = SYMBOL_BLOCK_VALUE (symbol); 1005796c8dcSSimon Schubert return BLOCK_START (bl); 1015796c8dcSSimon Schubert } 1025796c8dcSSimon Schubert } 1035796c8dcSSimon Schubert 1045796c8dcSSimon Schubert msymbol = lookup_minimal_symbol_by_pc (pc); 1055796c8dcSSimon Schubert if (msymbol) 1065796c8dcSSimon Schubert { 1075796c8dcSSimon Schubert CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol); 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert if (find_pc_section (fstart)) 1105796c8dcSSimon Schubert return fstart; 1115796c8dcSSimon Schubert } 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert return 0; 1145796c8dcSSimon Schubert } 1155796c8dcSSimon Schubert 1165796c8dcSSimon Schubert /* Return the symbol for the function executing in frame FRAME. */ 1175796c8dcSSimon Schubert 1185796c8dcSSimon Schubert struct symbol * 1195796c8dcSSimon Schubert get_frame_function (struct frame_info *frame) 1205796c8dcSSimon Schubert { 1215796c8dcSSimon Schubert struct block *bl = get_frame_block (frame, 0); 1225796c8dcSSimon Schubert 1235796c8dcSSimon Schubert if (bl == NULL) 1245796c8dcSSimon Schubert return NULL; 1255796c8dcSSimon Schubert 1265796c8dcSSimon Schubert while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) 1275796c8dcSSimon Schubert bl = BLOCK_SUPERBLOCK (bl); 1285796c8dcSSimon Schubert 1295796c8dcSSimon Schubert return BLOCK_FUNCTION (bl); 1305796c8dcSSimon Schubert } 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert 1335796c8dcSSimon Schubert /* Return the function containing pc value PC in section SECTION. 1345796c8dcSSimon Schubert Returns 0 if function is not known. */ 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert struct symbol * 1375796c8dcSSimon Schubert find_pc_sect_function (CORE_ADDR pc, struct obj_section *section) 1385796c8dcSSimon Schubert { 1395796c8dcSSimon Schubert struct block *b = block_for_pc_sect (pc, section); 140*cf7f2e2dSJohn Marino 1415796c8dcSSimon Schubert if (b == 0) 1425796c8dcSSimon Schubert return 0; 1435796c8dcSSimon Schubert return block_linkage_function (b); 1445796c8dcSSimon Schubert } 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert /* Return the function containing pc value PC. 1475796c8dcSSimon Schubert Returns 0 if function is not known. Backward compatibility, no section */ 1485796c8dcSSimon Schubert 1495796c8dcSSimon Schubert struct symbol * 1505796c8dcSSimon Schubert find_pc_function (CORE_ADDR pc) 1515796c8dcSSimon Schubert { 1525796c8dcSSimon Schubert return find_pc_sect_function (pc, find_pc_mapped_section (pc)); 1535796c8dcSSimon Schubert } 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert /* These variables are used to cache the most recent result 1565796c8dcSSimon Schubert * of find_pc_partial_function. */ 1575796c8dcSSimon Schubert 1585796c8dcSSimon Schubert static CORE_ADDR cache_pc_function_low = 0; 1595796c8dcSSimon Schubert static CORE_ADDR cache_pc_function_high = 0; 1605796c8dcSSimon Schubert static char *cache_pc_function_name = 0; 1615796c8dcSSimon Schubert static struct obj_section *cache_pc_function_section = NULL; 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert /* Clear cache, e.g. when symbol table is discarded. */ 1645796c8dcSSimon Schubert 1655796c8dcSSimon Schubert void 1665796c8dcSSimon Schubert clear_pc_function_cache (void) 1675796c8dcSSimon Schubert { 1685796c8dcSSimon Schubert cache_pc_function_low = 0; 1695796c8dcSSimon Schubert cache_pc_function_high = 0; 1705796c8dcSSimon Schubert cache_pc_function_name = (char *) 0; 1715796c8dcSSimon Schubert cache_pc_function_section = NULL; 1725796c8dcSSimon Schubert } 1735796c8dcSSimon Schubert 1745796c8dcSSimon Schubert /* Finds the "function" (text symbol) that is smaller than PC but 1755796c8dcSSimon Schubert greatest of all of the potential text symbols in SECTION. Sets 1765796c8dcSSimon Schubert *NAME and/or *ADDRESS conditionally if that pointer is non-null. 1775796c8dcSSimon Schubert If ENDADDR is non-null, then set *ENDADDR to be the end of the 1785796c8dcSSimon Schubert function (exclusive), but passing ENDADDR as non-null means that 1795796c8dcSSimon Schubert the function might cause symbols to be read. This function either 1805796c8dcSSimon Schubert succeeds or fails (not halfway succeeds). If it succeeds, it sets 1815796c8dcSSimon Schubert *NAME, *ADDRESS, and *ENDADDR to real information and returns 1. 1825796c8dcSSimon Schubert If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and 1835796c8dcSSimon Schubert returns 0. */ 1845796c8dcSSimon Schubert 1855796c8dcSSimon Schubert /* Backward compatibility, no section argument. */ 1865796c8dcSSimon Schubert 1875796c8dcSSimon Schubert int 1885796c8dcSSimon Schubert find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address, 1895796c8dcSSimon Schubert CORE_ADDR *endaddr) 1905796c8dcSSimon Schubert { 1915796c8dcSSimon Schubert struct obj_section *section; 1925796c8dcSSimon Schubert struct symbol *f; 1935796c8dcSSimon Schubert struct minimal_symbol *msymbol; 194*cf7f2e2dSJohn Marino struct symtab *symtab = NULL; 195*cf7f2e2dSJohn Marino struct objfile *objfile; 1965796c8dcSSimon Schubert int i; 1975796c8dcSSimon Schubert CORE_ADDR mapped_pc; 1985796c8dcSSimon Schubert 1995796c8dcSSimon Schubert /* To ensure that the symbol returned belongs to the correct setion 2005796c8dcSSimon Schubert (and that the last [random] symbol from the previous section 2015796c8dcSSimon Schubert isn't returned) try to find the section containing PC. First try 2025796c8dcSSimon Schubert the overlay code (which by default returns NULL); and second try 2035796c8dcSSimon Schubert the normal section code (which almost always succeeds). */ 2045796c8dcSSimon Schubert section = find_pc_overlay (pc); 2055796c8dcSSimon Schubert if (section == NULL) 2065796c8dcSSimon Schubert section = find_pc_section (pc); 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert mapped_pc = overlay_mapped_address (pc, section); 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert if (mapped_pc >= cache_pc_function_low 2115796c8dcSSimon Schubert && mapped_pc < cache_pc_function_high 2125796c8dcSSimon Schubert && section == cache_pc_function_section) 2135796c8dcSSimon Schubert goto return_cached_value; 2145796c8dcSSimon Schubert 2155796c8dcSSimon Schubert msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section); 216*cf7f2e2dSJohn Marino ALL_OBJFILES (objfile) 2175796c8dcSSimon Schubert { 218*cf7f2e2dSJohn Marino if (objfile->sf) 219*cf7f2e2dSJohn Marino symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol, 220*cf7f2e2dSJohn Marino mapped_pc, section, 0); 221*cf7f2e2dSJohn Marino if (symtab) 222*cf7f2e2dSJohn Marino break; 2235796c8dcSSimon Schubert } 2245796c8dcSSimon Schubert 225*cf7f2e2dSJohn Marino if (symtab) 2265796c8dcSSimon Schubert { 2275796c8dcSSimon Schubert /* Checking whether the msymbol has a larger value is for the 2285796c8dcSSimon Schubert "pathological" case mentioned in print_frame_info. */ 2295796c8dcSSimon Schubert f = find_pc_sect_function (mapped_pc, section); 2305796c8dcSSimon Schubert if (f != NULL 2315796c8dcSSimon Schubert && (msymbol == NULL 2325796c8dcSSimon Schubert || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) 2335796c8dcSSimon Schubert >= SYMBOL_VALUE_ADDRESS (msymbol)))) 2345796c8dcSSimon Schubert { 2355796c8dcSSimon Schubert cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); 2365796c8dcSSimon Schubert cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); 2375796c8dcSSimon Schubert cache_pc_function_name = SYMBOL_LINKAGE_NAME (f); 2385796c8dcSSimon Schubert cache_pc_function_section = section; 2395796c8dcSSimon Schubert goto return_cached_value; 2405796c8dcSSimon Schubert } 2415796c8dcSSimon Schubert } 2425796c8dcSSimon Schubert 2435796c8dcSSimon Schubert /* Not in the normal symbol tables, see if the pc is in a known section. 2445796c8dcSSimon Schubert If it's not, then give up. This ensures that anything beyond the end 2455796c8dcSSimon Schubert of the text seg doesn't appear to be part of the last function in the 2465796c8dcSSimon Schubert text segment. */ 2475796c8dcSSimon Schubert 2485796c8dcSSimon Schubert if (!section) 2495796c8dcSSimon Schubert msymbol = NULL; 2505796c8dcSSimon Schubert 2515796c8dcSSimon Schubert /* Must be in the minimal symbol table. */ 2525796c8dcSSimon Schubert if (msymbol == NULL) 2535796c8dcSSimon Schubert { 2545796c8dcSSimon Schubert /* No available symbol. */ 2555796c8dcSSimon Schubert if (name != NULL) 2565796c8dcSSimon Schubert *name = 0; 2575796c8dcSSimon Schubert if (address != NULL) 2585796c8dcSSimon Schubert *address = 0; 2595796c8dcSSimon Schubert if (endaddr != NULL) 2605796c8dcSSimon Schubert *endaddr = 0; 2615796c8dcSSimon Schubert return 0; 2625796c8dcSSimon Schubert } 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); 2655796c8dcSSimon Schubert cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol); 2665796c8dcSSimon Schubert cache_pc_function_section = section; 2675796c8dcSSimon Schubert 2685796c8dcSSimon Schubert /* If the minimal symbol has a size, use it for the cache. 2695796c8dcSSimon Schubert Otherwise use the lesser of the next minimal symbol in the same 2705796c8dcSSimon Schubert section, or the end of the section, as the end of the 2715796c8dcSSimon Schubert function. */ 2725796c8dcSSimon Schubert 2735796c8dcSSimon Schubert if (MSYMBOL_SIZE (msymbol) != 0) 2745796c8dcSSimon Schubert cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol); 2755796c8dcSSimon Schubert else 2765796c8dcSSimon Schubert { 2775796c8dcSSimon Schubert /* Step over other symbols at this same address, and symbols in 2785796c8dcSSimon Schubert other sections, to find the next symbol in this section with 2795796c8dcSSimon Schubert a different address. */ 2805796c8dcSSimon Schubert 2815796c8dcSSimon Schubert for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++) 2825796c8dcSSimon Schubert { 2835796c8dcSSimon Schubert if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol) 2845796c8dcSSimon Schubert && SYMBOL_OBJ_SECTION (msymbol + i) == SYMBOL_OBJ_SECTION (msymbol)) 2855796c8dcSSimon Schubert break; 2865796c8dcSSimon Schubert } 2875796c8dcSSimon Schubert 2885796c8dcSSimon Schubert if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL 2895796c8dcSSimon Schubert && SYMBOL_VALUE_ADDRESS (msymbol + i) < obj_section_endaddr (section)) 2905796c8dcSSimon Schubert cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i); 2915796c8dcSSimon Schubert else 2925796c8dcSSimon Schubert /* We got the start address from the last msymbol in the objfile. 2935796c8dcSSimon Schubert So the end address is the end of the section. */ 2945796c8dcSSimon Schubert cache_pc_function_high = obj_section_endaddr (section); 2955796c8dcSSimon Schubert } 2965796c8dcSSimon Schubert 2975796c8dcSSimon Schubert return_cached_value: 2985796c8dcSSimon Schubert 2995796c8dcSSimon Schubert if (address) 3005796c8dcSSimon Schubert { 3015796c8dcSSimon Schubert if (pc_in_unmapped_range (pc, section)) 3025796c8dcSSimon Schubert *address = overlay_unmapped_address (cache_pc_function_low, section); 3035796c8dcSSimon Schubert else 3045796c8dcSSimon Schubert *address = cache_pc_function_low; 3055796c8dcSSimon Schubert } 3065796c8dcSSimon Schubert 3075796c8dcSSimon Schubert if (name) 3085796c8dcSSimon Schubert *name = cache_pc_function_name; 3095796c8dcSSimon Schubert 3105796c8dcSSimon Schubert if (endaddr) 3115796c8dcSSimon Schubert { 3125796c8dcSSimon Schubert if (pc_in_unmapped_range (pc, section)) 3135796c8dcSSimon Schubert { 3145796c8dcSSimon Schubert /* Because the high address is actually beyond the end of 3155796c8dcSSimon Schubert the function (and therefore possibly beyond the end of 3165796c8dcSSimon Schubert the overlay), we must actually convert (high - 1) and 3175796c8dcSSimon Schubert then add one to that. */ 3185796c8dcSSimon Schubert 3195796c8dcSSimon Schubert *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, 3205796c8dcSSimon Schubert section); 3215796c8dcSSimon Schubert } 3225796c8dcSSimon Schubert else 3235796c8dcSSimon Schubert *endaddr = cache_pc_function_high; 3245796c8dcSSimon Schubert } 3255796c8dcSSimon Schubert 3265796c8dcSSimon Schubert return 1; 3275796c8dcSSimon Schubert } 3285796c8dcSSimon Schubert 3295796c8dcSSimon Schubert /* Return the innermost stack frame executing inside of BLOCK, 3305796c8dcSSimon Schubert or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */ 3315796c8dcSSimon Schubert 3325796c8dcSSimon Schubert struct frame_info * 3335796c8dcSSimon Schubert block_innermost_frame (struct block *block) 3345796c8dcSSimon Schubert { 3355796c8dcSSimon Schubert struct frame_info *frame; 3365796c8dcSSimon Schubert CORE_ADDR start; 3375796c8dcSSimon Schubert CORE_ADDR end; 3385796c8dcSSimon Schubert 3395796c8dcSSimon Schubert if (block == NULL) 3405796c8dcSSimon Schubert return NULL; 3415796c8dcSSimon Schubert 3425796c8dcSSimon Schubert start = BLOCK_START (block); 3435796c8dcSSimon Schubert end = BLOCK_END (block); 3445796c8dcSSimon Schubert 3455796c8dcSSimon Schubert frame = get_current_frame (); 3465796c8dcSSimon Schubert while (frame != NULL) 3475796c8dcSSimon Schubert { 3485796c8dcSSimon Schubert struct block *frame_block = get_frame_block (frame, NULL); 3495796c8dcSSimon Schubert if (frame_block != NULL && contained_in (frame_block, block)) 3505796c8dcSSimon Schubert return frame; 3515796c8dcSSimon Schubert 3525796c8dcSSimon Schubert frame = get_prev_frame (frame); 3535796c8dcSSimon Schubert } 3545796c8dcSSimon Schubert 3555796c8dcSSimon Schubert return NULL; 3565796c8dcSSimon Schubert } 357