gdb/blockframe.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Get info from stack frames; convert between frames, blocks,
  2.    functions and pc values.

  3.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "bfd.h"
  18. #include "objfiles.h"
  19. #include "frame.h"
  20. #include "gdbcore.h"
  21. #include "value.h"
  22. #include "target.h"
  23. #include "inferior.h"
  24. #include "annotate.h"
  25. #include "regcache.h"
  26. #include "dummy-frame.h"
  27. #include "command.h"
  28. #include "gdbcmd.h"
  29. #include "block.h"
  30. #include "inline-frame.h"

  31. /* Return the innermost lexical block in execution in a specified
  32.    stack frame.  The frame address is assumed valid.

  33.    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
  34.    address we used to choose the block.  We use this to find a source
  35.    line, to decide which macro definitions are in scope.

  36.    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
  37.    PC, and may not really be a valid PC at all.  For example, in the
  38.    caller of a function declared to never return, the code at the
  39.    return address will never be reached, so the call instruction may
  40.    be the very last instruction in the block.  So the address we use
  41.    to choose the block is actually one byte before the return address
  42.    --- hopefully pointing us at the call instruction, or its delay
  43.    slot instruction.  */

  44. const struct block *
  45. get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
  46. {
  47.   CORE_ADDR pc;
  48.   const struct block *bl;
  49.   int inline_count;

  50.   if (!get_frame_address_in_block_if_available (frame, &pc))
  51.     return NULL;

  52.   if (addr_in_block)
  53.     *addr_in_block = pc;

  54.   bl = block_for_pc (pc);
  55.   if (bl == NULL)
  56.     return NULL;

  57.   inline_count = frame_inlined_callees (frame);

  58.   while (inline_count > 0)
  59.     {
  60.       if (block_inlined_p (bl))
  61.         inline_count--;

  62.       bl = BLOCK_SUPERBLOCK (bl);
  63.       gdb_assert (bl != NULL);
  64.     }

  65.   return bl;
  66. }

  67. CORE_ADDR
  68. get_pc_function_start (CORE_ADDR pc)
  69. {
  70.   const struct block *bl;
  71.   struct bound_minimal_symbol msymbol;

  72.   bl = block_for_pc (pc);
  73.   if (bl)
  74.     {
  75.       struct symbol *symbol = block_linkage_function (bl);

  76.       if (symbol)
  77.         {
  78.           bl = SYMBOL_BLOCK_VALUE (symbol);
  79.           return BLOCK_START (bl);
  80.         }
  81.     }

  82.   msymbol = lookup_minimal_symbol_by_pc (pc);
  83.   if (msymbol.minsym)
  84.     {
  85.       CORE_ADDR fstart = BMSYMBOL_VALUE_ADDRESS (msymbol);

  86.       if (find_pc_section (fstart))
  87.         return fstart;
  88.     }

  89.   return 0;
  90. }

  91. /* Return the symbol for the function executing in frame FRAME.  */

  92. struct symbol *
  93. get_frame_function (struct frame_info *frame)
  94. {
  95.   const struct block *bl = get_frame_block (frame, 0);

  96.   if (bl == NULL)
  97.     return NULL;

  98.   while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
  99.     bl = BLOCK_SUPERBLOCK (bl);

  100.   return BLOCK_FUNCTION (bl);
  101. }


  102. /* Return the function containing pc value PC in section SECTION.
  103.    Returns 0 if function is not known.  */

  104. struct symbol *
  105. find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
  106. {
  107.   const struct block *b = block_for_pc_sect (pc, section);

  108.   if (b == 0)
  109.     return 0;
  110.   return block_linkage_function (b);
  111. }

  112. /* Return the function containing pc value PC.
  113.    Returns 0 if function is not known.
  114.    Backward compatibility, no section */

  115. struct symbol *
  116. find_pc_function (CORE_ADDR pc)
  117. {
  118.   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
  119. }

  120. /* These variables are used to cache the most recent result
  121.    of find_pc_partial_function.  */

  122. static CORE_ADDR cache_pc_function_low = 0;
  123. static CORE_ADDR cache_pc_function_high = 0;
  124. static const char *cache_pc_function_name = 0;
  125. static struct obj_section *cache_pc_function_section = NULL;
  126. static int cache_pc_function_is_gnu_ifunc = 0;

  127. /* Clear cache, e.g. when symbol table is discarded.  */

  128. void
  129. clear_pc_function_cache (void)
  130. {
  131.   cache_pc_function_low = 0;
  132.   cache_pc_function_high = 0;
  133.   cache_pc_function_name = (char *) 0;
  134.   cache_pc_function_section = NULL;
  135.   cache_pc_function_is_gnu_ifunc = 0;
  136. }

  137. /* Finds the "function" (text symbol) that is smaller than PC but
  138.    greatest of all of the potential text symbols in SECTION.  Sets
  139.    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
  140.    If ENDADDR is non-null, then set *ENDADDR to be the end of the
  141.    function (exclusive), but passing ENDADDR as non-null means that
  142.    the function might cause symbols to be read.  If IS_GNU_IFUNC_P is provided
  143.    *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
  144.    This function either succeeds or fails (not halfway succeeds).  If it
  145.    succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
  146.    returns 1.  If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
  147.    *IS_GNU_IFUNC_P to zero and returns 0.  */

  148. /* Backward compatibility, no section argument.  */

  149. int
  150. find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
  151.                                     CORE_ADDR *address, CORE_ADDR *endaddr,
  152.                                     int *is_gnu_ifunc_p)
  153. {
  154.   struct obj_section *section;
  155.   struct symbol *f;
  156.   struct bound_minimal_symbol msymbol;
  157.   struct compunit_symtab *compunit_symtab = NULL;
  158.   struct objfile *objfile;
  159.   int i;
  160.   CORE_ADDR mapped_pc;

  161.   /* To ensure that the symbol returned belongs to the correct setion
  162.      (and that the last [random] symbol from the previous section
  163.      isn't returned) try to find the section containing PCFirst try
  164.      the overlay code (which by default returns NULL); and second try
  165.      the normal section code (which almost always succeeds).  */
  166.   section = find_pc_overlay (pc);
  167.   if (section == NULL)
  168.     section = find_pc_section (pc);

  169.   mapped_pc = overlay_mapped_address (pc, section);

  170.   if (mapped_pc >= cache_pc_function_low
  171.       && mapped_pc < cache_pc_function_high
  172.       && section == cache_pc_function_section)
  173.     goto return_cached_value;

  174.   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
  175.   ALL_OBJFILES (objfile)
  176.   {
  177.     if (objfile->sf)
  178.       {
  179.         compunit_symtab
  180.           = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
  181.                                                            mapped_pc, section,
  182.                                                            0);
  183.       }
  184.     if (compunit_symtab != NULL)
  185.       break;
  186.   }

  187.   if (compunit_symtab != NULL)
  188.     {
  189.       /* Checking whether the msymbol has a larger value is for the
  190.          "pathological" case mentioned in print_frame_info.  */
  191.       f = find_pc_sect_function (mapped_pc, section);
  192.       if (f != NULL
  193.           && (msymbol.minsym == NULL
  194.               || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
  195.                   >= BMSYMBOL_VALUE_ADDRESS (msymbol))))
  196.         {
  197.           cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  198.           cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
  199.           cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
  200.           cache_pc_function_section = section;
  201.           cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
  202.           goto return_cached_value;
  203.         }
  204.     }

  205.   /* Not in the normal symbol tables, see if the pc is in a known
  206.      section.  If it's not, then give up.  This ensures that anything
  207.      beyond the end of the text seg doesn't appear to be part of the
  208.      last function in the text segment.  */

  209.   if (!section)
  210.     msymbol.minsym = NULL;

  211.   /* Must be in the minimal symbol table.  */
  212.   if (msymbol.minsym == NULL)
  213.     {
  214.       /* No available symbol.  */
  215.       if (name != NULL)
  216.         *name = 0;
  217.       if (address != NULL)
  218.         *address = 0;
  219.       if (endaddr != NULL)
  220.         *endaddr = 0;
  221.       if (is_gnu_ifunc_p != NULL)
  222.         *is_gnu_ifunc_p = 0;
  223.       return 0;
  224.     }

  225.   cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
  226.   cache_pc_function_name = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
  227.   cache_pc_function_section = section;
  228.   cache_pc_function_is_gnu_ifunc = (MSYMBOL_TYPE (msymbol.minsym)
  229.                                     == mst_text_gnu_ifunc);
  230.   cache_pc_function_high = minimal_symbol_upper_bound (msymbol);

  231. return_cached_value:

  232.   if (address)
  233.     {
  234.       if (pc_in_unmapped_range (pc, section))
  235.         *address = overlay_unmapped_address (cache_pc_function_low, section);
  236.       else
  237.         *address = cache_pc_function_low;
  238.     }

  239.   if (name)
  240.     *name = cache_pc_function_name;

  241.   if (endaddr)
  242.     {
  243.       if (pc_in_unmapped_range (pc, section))
  244.         {
  245.           /* Because the high address is actually beyond the end of
  246.              the function (and therefore possibly beyond the end of
  247.              the overlay), we must actually convert (high - 1) and
  248.              then add one to that.  */

  249.           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
  250.                                                    section);
  251.         }
  252.       else
  253.         *endaddr = cache_pc_function_high;
  254.     }

  255.   if (is_gnu_ifunc_p)
  256.     *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;

  257.   return 1;
  258. }

  259. /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
  260.    is omitted here for backward API compatibility.  */

  261. int
  262. find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
  263.                           CORE_ADDR *endaddr)
  264. {
  265.   return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
  266. }

  267. /* Return the innermost stack frame that is executing inside of BLOCK and is
  268.    at least as old as the selected frame. Return NULL if there is no
  269.    such frame.  If BLOCK is NULL, just return NULL.  */

  270. struct frame_info *
  271. block_innermost_frame (const struct block *block)
  272. {
  273.   struct frame_info *frame;

  274.   if (block == NULL)
  275.     return NULL;

  276.   frame = get_selected_frame_if_set ();
  277.   if (frame == NULL)
  278.     frame = get_current_frame ();
  279.   while (frame != NULL)
  280.     {
  281.       const struct block *frame_block = get_frame_block (frame, NULL);
  282.       if (frame_block != NULL && contained_in (frame_block, block))
  283.         return frame;

  284.       frame = get_prev_frame (frame);
  285.     }

  286.   return NULL;
  287. }