GDB (API)
/home/stan/gdb/src/gdb/blockframe.c
Go to the documentation of this file.
00001 /* Get info from stack frames; convert between frames, blocks,
00002    functions and pc values.
00003 
00004    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "symtab.h"
00023 #include "bfd.h"
00024 #include "objfiles.h"
00025 #include "frame.h"
00026 #include "gdbcore.h"
00027 #include "value.h"
00028 #include "target.h"
00029 #include "inferior.h"
00030 #include "annotate.h"
00031 #include "regcache.h"
00032 #include "gdb_assert.h"
00033 #include "dummy-frame.h"
00034 #include "command.h"
00035 #include "gdbcmd.h"
00036 #include "block.h"
00037 #include "inline-frame.h"
00038 
00039 /* Return the innermost lexical block in execution in a specified
00040    stack frame.  The frame address is assumed valid.
00041 
00042    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
00043    address we used to choose the block.  We use this to find a source
00044    line, to decide which macro definitions are in scope.
00045 
00046    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
00047    PC, and may not really be a valid PC at all.  For example, in the
00048    caller of a function declared to never return, the code at the
00049    return address will never be reached, so the call instruction may
00050    be the very last instruction in the block.  So the address we use
00051    to choose the block is actually one byte before the return address
00052    --- hopefully pointing us at the call instruction, or its delay
00053    slot instruction.  */
00054 
00055 struct block *
00056 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
00057 {
00058   CORE_ADDR pc;
00059   struct block *bl;
00060   int inline_count;
00061 
00062   if (!get_frame_address_in_block_if_available (frame, &pc))
00063     return NULL;
00064 
00065   if (addr_in_block)
00066     *addr_in_block = pc;
00067 
00068   bl = block_for_pc (pc);
00069   if (bl == NULL)
00070     return NULL;
00071 
00072   inline_count = frame_inlined_callees (frame);
00073 
00074   while (inline_count > 0)
00075     {
00076       if (block_inlined_p (bl))
00077         inline_count--;
00078 
00079       bl = BLOCK_SUPERBLOCK (bl);
00080       gdb_assert (bl != NULL);
00081     }
00082 
00083   return bl;
00084 }
00085 
00086 CORE_ADDR
00087 get_pc_function_start (CORE_ADDR pc)
00088 {
00089   struct block *bl;
00090   struct bound_minimal_symbol msymbol;
00091 
00092   bl = block_for_pc (pc);
00093   if (bl)
00094     {
00095       struct symbol *symbol = block_linkage_function (bl);
00096 
00097       if (symbol)
00098         {
00099           bl = SYMBOL_BLOCK_VALUE (symbol);
00100           return BLOCK_START (bl);
00101         }
00102     }
00103 
00104   msymbol = lookup_minimal_symbol_by_pc (pc);
00105   if (msymbol.minsym)
00106     {
00107       CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
00108 
00109       if (find_pc_section (fstart))
00110         return fstart;
00111     }
00112 
00113   return 0;
00114 }
00115 
00116 /* Return the symbol for the function executing in frame FRAME.  */
00117 
00118 struct symbol *
00119 get_frame_function (struct frame_info *frame)
00120 {
00121   struct block *bl = get_frame_block (frame, 0);
00122 
00123   if (bl == NULL)
00124     return NULL;
00125 
00126   while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
00127     bl = BLOCK_SUPERBLOCK (bl);
00128 
00129   return BLOCK_FUNCTION (bl);
00130 }
00131 
00132 
00133 /* Return the function containing pc value PC in section SECTION.
00134    Returns 0 if function is not known.  */
00135 
00136 struct symbol *
00137 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
00138 {
00139   struct block *b = block_for_pc_sect (pc, section);
00140 
00141   if (b == 0)
00142     return 0;
00143   return block_linkage_function (b);
00144 }
00145 
00146 /* Return the function containing pc value PC.
00147    Returns 0 if function is not known.  
00148    Backward compatibility, no section */
00149 
00150 struct symbol *
00151 find_pc_function (CORE_ADDR pc)
00152 {
00153   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
00154 }
00155 
00156 /* These variables are used to cache the most recent result
00157    of find_pc_partial_function.  */
00158 
00159 static CORE_ADDR cache_pc_function_low = 0;
00160 static CORE_ADDR cache_pc_function_high = 0;
00161 static const char *cache_pc_function_name = 0;
00162 static struct obj_section *cache_pc_function_section = NULL;
00163 static int cache_pc_function_is_gnu_ifunc = 0;
00164 
00165 /* Clear cache, e.g. when symbol table is discarded.  */
00166 
00167 void
00168 clear_pc_function_cache (void)
00169 {
00170   cache_pc_function_low = 0;
00171   cache_pc_function_high = 0;
00172   cache_pc_function_name = (char *) 0;
00173   cache_pc_function_section = NULL;
00174   cache_pc_function_is_gnu_ifunc = 0;
00175 }
00176 
00177 /* Finds the "function" (text symbol) that is smaller than PC but
00178    greatest of all of the potential text symbols in SECTION.  Sets
00179    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
00180    If ENDADDR is non-null, then set *ENDADDR to be the end of the
00181    function (exclusive), but passing ENDADDR as non-null means that
00182    the function might cause symbols to be read.  If IS_GNU_IFUNC_P is provided
00183    *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
00184    This function either succeeds or fails (not halfway succeeds).  If it
00185    succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
00186    returns 1.  If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
00187    *IS_GNU_IFUNC_P to zero and returns 0.  */
00188 
00189 /* Backward compatibility, no section argument.  */
00190 
00191 int
00192 find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
00193                                     CORE_ADDR *address, CORE_ADDR *endaddr,
00194                                     int *is_gnu_ifunc_p)
00195 {
00196   struct obj_section *section;
00197   struct symbol *f;
00198   struct minimal_symbol *msymbol;
00199   struct symtab *symtab = NULL;
00200   struct objfile *objfile;
00201   int i;
00202   CORE_ADDR mapped_pc;
00203 
00204   /* To ensure that the symbol returned belongs to the correct setion
00205      (and that the last [random] symbol from the previous section
00206      isn't returned) try to find the section containing PC.  First try
00207      the overlay code (which by default returns NULL); and second try
00208      the normal section code (which almost always succeeds).  */
00209   section = find_pc_overlay (pc);
00210   if (section == NULL)
00211     section = find_pc_section (pc);
00212 
00213   mapped_pc = overlay_mapped_address (pc, section);
00214 
00215   if (mapped_pc >= cache_pc_function_low
00216       && mapped_pc < cache_pc_function_high
00217       && section == cache_pc_function_section)
00218     goto return_cached_value;
00219 
00220   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section).minsym;
00221   ALL_OBJFILES (objfile)
00222   {
00223     if (objfile->sf)
00224       symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
00225                                                      mapped_pc, section, 0);
00226     if (symtab)
00227       break;
00228   }
00229 
00230   if (symtab)
00231     {
00232       /* Checking whether the msymbol has a larger value is for the
00233          "pathological" case mentioned in print_frame_info.  */
00234       f = find_pc_sect_function (mapped_pc, section);
00235       if (f != NULL
00236           && (msymbol == NULL
00237               || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
00238                   >= SYMBOL_VALUE_ADDRESS (msymbol))))
00239         {
00240           cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
00241           cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
00242           cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
00243           cache_pc_function_section = section;
00244           cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
00245           goto return_cached_value;
00246         }
00247     }
00248 
00249   /* Not in the normal symbol tables, see if the pc is in a known
00250      section.  If it's not, then give up.  This ensures that anything
00251      beyond the end of the text seg doesn't appear to be part of the
00252      last function in the text segment.  */
00253 
00254   if (!section)
00255     msymbol = NULL;
00256 
00257   /* Must be in the minimal symbol table.  */
00258   if (msymbol == NULL)
00259     {
00260       /* No available symbol.  */
00261       if (name != NULL)
00262         *name = 0;
00263       if (address != NULL)
00264         *address = 0;
00265       if (endaddr != NULL)
00266         *endaddr = 0;
00267       if (is_gnu_ifunc_p != NULL)
00268         *is_gnu_ifunc_p = 0;
00269       return 0;
00270     }
00271 
00272   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
00273   cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol);
00274   cache_pc_function_section = section;
00275   cache_pc_function_is_gnu_ifunc = MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc;
00276 
00277   /* If the minimal symbol has a size, use it for the cache.
00278      Otherwise use the lesser of the next minimal symbol in the same
00279      section, or the end of the section, as the end of the
00280      function.  */
00281 
00282   if (MSYMBOL_SIZE (msymbol) != 0)
00283     cache_pc_function_high = cache_pc_function_low + MSYMBOL_SIZE (msymbol);
00284   else
00285     {
00286       /* Step over other symbols at this same address, and symbols in
00287          other sections, to find the next symbol in this section with
00288          a different address.  */
00289 
00290       for (i = 1; SYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
00291         {
00292           if (SYMBOL_VALUE_ADDRESS (msymbol + i)
00293               != SYMBOL_VALUE_ADDRESS (msymbol)
00294               && SYMBOL_SECTION (msymbol + i)
00295               == SYMBOL_SECTION (msymbol))
00296             break;
00297         }
00298 
00299       if (SYMBOL_LINKAGE_NAME (msymbol + i) != NULL
00300           && SYMBOL_VALUE_ADDRESS (msymbol + i)
00301           < obj_section_endaddr (section))
00302         cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
00303       else
00304         /* We got the start address from the last msymbol in the objfile.
00305            So the end address is the end of the section.  */
00306         cache_pc_function_high = obj_section_endaddr (section);
00307     }
00308 
00309  return_cached_value:
00310 
00311   if (address)
00312     {
00313       if (pc_in_unmapped_range (pc, section))
00314         *address = overlay_unmapped_address (cache_pc_function_low, section);
00315       else
00316         *address = cache_pc_function_low;
00317     }
00318 
00319   if (name)
00320     *name = cache_pc_function_name;
00321 
00322   if (endaddr)
00323     {
00324       if (pc_in_unmapped_range (pc, section))
00325         {
00326           /* Because the high address is actually beyond the end of
00327              the function (and therefore possibly beyond the end of
00328              the overlay), we must actually convert (high - 1) and
00329              then add one to that.  */
00330 
00331           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
00332                                                    section);
00333         }
00334       else
00335         *endaddr = cache_pc_function_high;
00336     }
00337 
00338   if (is_gnu_ifunc_p)
00339     *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
00340 
00341   return 1;
00342 }
00343 
00344 /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
00345    is omitted here for backward API compatibility.  */
00346 
00347 int
00348 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
00349                           CORE_ADDR *endaddr)
00350 {
00351   return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
00352 }
00353 
00354 /* Return the innermost stack frame that is executing inside of BLOCK and is
00355    at least as old as the selected frame. Return NULL if there is no
00356    such frame.  If BLOCK is NULL, just return NULL.  */
00357 
00358 struct frame_info *
00359 block_innermost_frame (const struct block *block)
00360 {
00361   struct frame_info *frame;
00362 
00363   if (block == NULL)
00364     return NULL;
00365 
00366   frame = get_selected_frame_if_set ();
00367   if (frame == NULL)
00368     frame = get_current_frame ();
00369   while (frame != NULL)
00370     {
00371       struct block *frame_block = get_frame_block (frame, NULL);
00372       if (frame_block != NULL && contained_in (frame_block, block))
00373         return frame;
00374 
00375       frame = get_prev_frame (frame);
00376     }
00377 
00378   return NULL;
00379 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines