home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / blockframe.c < prev    next >
C/C++ Source or Header  |  1990-06-06  |  17KB  |  629 lines

  1. /* Get info from stack frames;
  2.    convert between frames, blocks, functions and pc values.
  3.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. GDB is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GDB is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GDB; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "frame.h"
  25.  
  26. #include <obstack.h>
  27.  
  28. /* Start and end of object file containing the entry point.
  29.    STARTUP_FILE_END is the first address of the next file.
  30.    This file is assumed to be a startup file
  31.    and frames with pc's inside it
  32.    are treated as nonexistent.
  33.  
  34.    Setting these variables is necessary so that backtraces do not fly off
  35.    the bottom of the stack.  */
  36. CORE_ADDR startup_file_start;
  37. CORE_ADDR startup_file_end;
  38.  
  39. /* Is ADDR outside the startup file?  */
  40. int
  41. outside_startup_file (addr)
  42.      CORE_ADDR addr;
  43. {
  44.   return !(addr >= startup_file_start && addr < startup_file_end);
  45. }
  46.  
  47. /* Address of innermost stack frame (contents of FP register) */
  48.  
  49. static FRAME current_frame;
  50.  
  51. struct block *block_for_pc ();
  52. CORE_ADDR get_pc_function_start ();
  53.  
  54. /*
  55.  * Cache for frame addresses already read by gdb.  Valid only while
  56.  * inferior is stopped.  Control variables for the frame cache should
  57.  * be local to this module.
  58.  */
  59. struct obstack frame_cache_obstack;
  60.  
  61. /* Return the innermost (currently executing) stack frame.  */
  62.  
  63. FRAME
  64. get_current_frame ()
  65. {
  66.   /* We assume its address is kept in a general register;
  67.      param.h says which register.  */
  68.  
  69.   return current_frame;
  70. }
  71.  
  72. void
  73. set_current_frame (frame)
  74.      FRAME frame;
  75. {
  76.   current_frame = frame;
  77. }
  78.  
  79. FRAME
  80. create_new_frame (addr, pc)
  81.      FRAME_ADDR addr;
  82.      CORE_ADDR pc;
  83. {
  84.   struct frame_info *fci;    /* Same type as FRAME */
  85.  
  86.   fci = (struct frame_info *)
  87.     obstack_alloc (&frame_cache_obstack,
  88.            sizeof (struct frame_info));
  89.  
  90.   /* Arbitrary frame */
  91.   fci->next = (struct frame_info *) 0;
  92.   fci->prev = (struct frame_info *) 0;
  93.   fci->frame = addr;
  94.   fci->next_frame = 0;        /* Since arbitrary */
  95.   fci->pc = pc;
  96.  
  97. #ifdef INIT_EXTRA_FRAME_INFO
  98.   INIT_EXTRA_FRAME_INFO (fci);
  99. #endif
  100.  
  101.   return fci;
  102. }
  103.  
  104. /* Return the frame that called FRAME.
  105.    If FRAME is the original frame (it has no caller), return 0.  */
  106.  
  107. FRAME
  108. get_prev_frame (frame)
  109.      FRAME frame;
  110. {
  111.   /* We're allowed to know that FRAME and "struct frame_info *" are
  112.      the same */
  113.   return get_prev_frame_info (frame);
  114. }
  115.  
  116. /* Return the frame that FRAME calls (0 if FRAME is the innermost
  117.    frame).  */
  118.  
  119. FRAME
  120. get_next_frame (frame)
  121.      FRAME frame;
  122. {
  123.   /* We're allowed to know that FRAME and "struct frame_info *" are
  124.      the same */
  125.   return frame->next;
  126. }
  127.  
  128. /*
  129.  * Flush the entire frame cache.
  130.  */
  131. void
  132. flush_cached_frames ()
  133. {
  134.   /* Since we can't really be sure what the first object allocated was */
  135.   obstack_free (&frame_cache_obstack, 0);
  136.   obstack_init (&frame_cache_obstack);
  137.  
  138.   current_frame = (struct frame_info *) 0; /* Invalidate cache */
  139. }
  140.  
  141. /* Return a structure containing various interesting information
  142.    about a specified stack frame.  */
  143. /* How do I justify including this function?  Well, the FRAME
  144.    identifier format has gone through several changes recently, and
  145.    it's not completely inconceivable that it could happen again.  If
  146.    it does, have this routine around will help */
  147.  
  148. struct frame_info *
  149. get_frame_info (frame)
  150.      FRAME frame;
  151. {
  152.   return frame;
  153. }
  154.  
  155. /* If a machine allows frameless functions, it should define a macro
  156.    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
  157.    frame_info for the frame, and FRAMELESS should be set to nonzero
  158.    if it represents a frameless function invocation.  */
  159.  
  160. /* Many machines which allow frameless functions can detect them using
  161.    this macro.  Such machines should define FRAMELESS_FUNCTION_INVOCATION
  162.    to just call this macro.  */
  163. #define FRAMELESS_LOOK_FOR_PROLOGUE(FI, FRAMELESS) \
  164. {                                                                                                   \
  165.   CORE_ADDR func_start, after_prologue;                             \
  166.   func_start = (get_pc_function_start ((FI)->pc) +                     \
  167.         FUNCTION_START_OFFSET);                             \
  168.   if (func_start)                                                        \
  169.     {                                     \
  170.       after_prologue = func_start;                     \
  171.       SKIP_PROLOGUE (after_prologue);                     \
  172.       (FRAMELESS) = (after_prologue == func_start);             \
  173.     }                                     \
  174.   else                                     \
  175.     /* If we can't find the start of the function, we don't really */    \
  176.     /* know whether the function is frameless, but we should be       */    \
  177.     /* able to get a reasonable (i.e. best we can do under the       */    \
  178.     /* circumstances) backtrace by saying that it isn't.  */             \
  179.     (FRAMELESS) = 0;                             \
  180. }
  181.  
  182. /* Return a structure containing various interesting information
  183.    about the frame that called NEXT_FRAME.  Returns NULL
  184.    if there is no such frame.  */
  185.  
  186. struct frame_info *
  187. get_prev_frame_info (next_frame)
  188.      FRAME next_frame;
  189. {
  190.   FRAME_ADDR address;
  191.   struct frame_info *prev;
  192.   int fromleaf = 0;
  193.  
  194.   /* If the requested entry is in the cache, return it.
  195.      Otherwise, figure out what the address should be for the entry
  196.      we're about to add to the cache. */
  197.  
  198.   if (!next_frame)
  199.     {
  200.       if (!current_frame)
  201.     {
  202.       if (!have_inferior_p () && !have_core_file_p ())
  203.         fatal ("get_prev_frame_info: Called before cache primed.  \"Shouldn't happen.\"");
  204.       else
  205.         error ("No inferior or core file.");
  206.     }
  207.  
  208.       return current_frame;
  209.     }
  210.  
  211.   /* If we have the prev one, return it */
  212.   if (next_frame->prev)
  213.     return next_frame->prev;
  214.  
  215.   /* On some machines it is possible to call a function without
  216.      setting up a stack frame for it.  On these machines, we
  217.      define this macro to take two args; a frameinfo pointer
  218.      identifying a frame and a variable to set or clear if it is
  219.      or isn't leafless.  */
  220. #ifdef FRAMELESS_FUNCTION_INVOCATION
  221.   /* Still don't want to worry about this except on the innermost
  222.      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
  223.      frameless function invocation.  */
  224.   if (!(next_frame->next))
  225.     {
  226.       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
  227.       if (fromleaf)
  228.     address = next_frame->frame;
  229.     }
  230. #endif
  231.  
  232.   if (!fromleaf)
  233.     {
  234.       /* Two macros defined in param.h specify the machine-dependent
  235.      actions to be performed here.
  236.      First, get the frame's chain-pointer.
  237.      If that is zero, the frame is the outermost frame or a leaf
  238.      called by the outermost frame.  This means that if start
  239.      calls main without a frame, we'll return 0 (which is fine
  240.      anyway).
  241.  
  242.      Nope; there's a problem.  This also returns when the current
  243.      routine is a leaf of main.  This is unacceptable.  We move
  244.      this to after the ffi test; I'd rather have backtraces from
  245.      start go curfluy than have an abort called from main not show
  246.      main.  */
  247.       address = FRAME_CHAIN (next_frame);
  248.       if (!FRAME_CHAIN_VALID (address, next_frame))
  249.     return 0;
  250.       /* If this frame is a leaf, this will be superceeded by the
  251.      code below.  */
  252.       address = FRAME_CHAIN_COMBINE (address, next_frame);
  253.     }
  254.  
  255.   prev = (struct frame_info *)
  256.     obstack_alloc (&frame_cache_obstack,
  257.            sizeof (struct frame_info));
  258.  
  259.   if (next_frame)
  260.     next_frame->prev = prev;
  261.   prev->next = next_frame;
  262.   prev->prev = (struct frame_info *) 0;
  263.   prev->frame = address;
  264.   prev->next_frame = prev->next ? prev->next->frame : 0;
  265.  
  266. #ifdef INIT_EXTRA_FRAME_INFO
  267.   INIT_EXTRA_FRAME_INFO(prev);
  268. #endif
  269.  
  270.   /* This entry is in the frame queue now, which is good since
  271.      FRAME_SAVED_PC may use that queue to figure out it's value
  272.      (see m-sparc.h).  We want the pc saved in the inferior frame. */
  273.   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (next_frame) :
  274.           next_frame ? FRAME_SAVED_PC (next_frame) : read_pc ());
  275.  
  276.   return prev;
  277. }
  278.  
  279. CORE_ADDR
  280. get_frame_pc (frame)
  281.      FRAME frame;
  282. {
  283.   struct frame_info *fi;
  284.   fi = get_frame_info (frame);
  285.   return fi->pc;
  286. }
  287.  
  288. /* Find the addresses in which registers are saved in FRAME.  */
  289.  
  290. void
  291. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  292.      struct frame_info *frame_info_addr;
  293.      struct frame_saved_regs *saved_regs_addr;
  294. {
  295.   FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
  296. }
  297.  
  298. /* Return the innermost lexical block in execution
  299.    in a specified stack frame.  The frame address is assumed valid.  */
  300.  
  301. struct block *
  302. get_frame_block (frame)
  303.      FRAME frame;
  304. {
  305.   struct frame_info *fi;
  306.   CORE_ADDR pc;
  307.  
  308.   fi = get_frame_info (frame);
  309.  
  310.   pc = fi->pc;
  311.   if (fi->next_frame != 0)
  312.     /* We are not in the innermost frame.  We need to subtract one to
  313.        get the correct block, in case the call instruction was the
  314.        last instruction of the block.  If there are any machines on
  315.        which the saved pc does not point to after the call insn, we
  316.        probably want to make fi->pc point after the call insn anyway.  */
  317.     --pc;
  318.   return block_for_pc (pc);
  319. }
  320.  
  321. struct block *
  322. get_current_block ()
  323. {
  324.   return block_for_pc (read_pc ());
  325. }
  326.  
  327. CORE_ADDR
  328. get_pc_function_start (pc)
  329.      CORE_ADDR pc;
  330. {
  331.   register struct block *bl = block_for_pc (pc);
  332.   register struct symbol *symbol;
  333.   if (bl == 0 || (symbol = block_function (bl)) == 0)
  334.     {
  335.       register int misc_index = find_pc_misc_function (pc);
  336.       if (misc_index >= 0)
  337.     return misc_function_vector[misc_index].address;
  338.       return 0;
  339.     }
  340.   bl = SYMBOL_BLOCK_VALUE (symbol);
  341.   return BLOCK_START (bl);
  342. }
  343.  
  344. /* Return the symbol for the function executing in frame FRAME.  */
  345.  
  346. struct symbol *
  347. get_frame_function (frame)
  348.      FRAME frame;
  349. {
  350.   register struct block *bl = get_frame_block (frame);
  351.   if (bl == 0)
  352.     return 0;
  353.   return block_function (bl);
  354. }
  355.  
  356. /* Return the innermost lexical block containing the specified pc value,
  357.    or 0 if there is none.  */
  358.  
  359. extern struct symtab *psymtab_to_symtab ();
  360.  
  361. /* Return the blockvector immediately containing the innermost lexical block
  362.    containing the specified pc value, or 0 if there is none.
  363.    PINDEX is a pointer to the index value of the block.  If PINDEX
  364.    is NULL, we don't pass this information back to the caller.  */
  365.  
  366. struct blockvector *
  367. blockvector_for_pc (pc, pindex)
  368.      register CORE_ADDR pc;
  369.      int *pindex;
  370. {
  371.   register struct block *b;
  372.   register int bot, top, half;
  373.   register struct symtab *s;
  374.   register struct partial_symtab *ps;
  375.   struct blockvector *bl;
  376.  
  377.   /* First search all symtabs for one whose file contains our pc */
  378.  
  379.   for (s = symtab_list; s; s = s->next)
  380.     {
  381.       bl = BLOCKVECTOR (s);
  382.       b = BLOCKVECTOR_BLOCK (bl, 0);
  383.       if (BLOCK_START (b) <= pc
  384.       && BLOCK_END (b) > pc)
  385.     break;
  386.     }
  387.  
  388.   if (s == 0)
  389.     for (ps = partial_symtab_list; ps; ps = ps->next)
  390.       {
  391.     if (ps->textlow <= pc
  392.         && ps->texthigh > pc)
  393.       {
  394.         if (ps->readin)
  395.           fatal ("Internal error: pc found in readin psymtab and not in any symtab.");
  396.         s = psymtab_to_symtab (ps);
  397.         bl = BLOCKVECTOR (s);
  398.         b = BLOCKVECTOR_BLOCK (bl, 0);
  399.         break;
  400.       }
  401.       }
  402.  
  403.   if (s == 0)
  404.     return 0;
  405.  
  406.   /* Then search that symtab for the smallest block that wins.  */
  407.   /* Use binary search to find the last block that starts before PC.  */
  408.  
  409.   bot = 0;
  410.   top = BLOCKVECTOR_NBLOCKS (bl);
  411.  
  412.   while (top - bot > 1)
  413.     {
  414.       half = (top - bot + 1) >> 1;
  415.       b = BLOCKVECTOR_BLOCK (bl, bot + half);
  416.       if (BLOCK_START (b) <= pc)
  417.     bot += half;
  418.       else
  419.     top = bot + half;
  420.     }
  421.  
  422.   /* Now search backward for a block that ends after PC.  */
  423.  
  424.   while (bot >= 0)
  425.     {
  426.       b = BLOCKVECTOR_BLOCK (bl, bot);
  427.       if (BLOCK_END (b) > pc)
  428.     {
  429.       if (pindex)
  430.         *pindex = bot;
  431.       return bl;
  432.     }
  433.       bot--;
  434.     }
  435.  
  436.   return 0;
  437. }
  438.  
  439. /* Same as above, but return the actual block.  */
  440. struct block *
  441. block_for_pc (pc)
  442.      register CORE_ADDR pc;
  443. {
  444.   register struct blockvector *bl;
  445.   int index;
  446.  
  447.   bl = blockvector_for_pc (pc, &index);
  448.   if (bl)
  449.     return BLOCKVECTOR_BLOCK (bl, index);
  450.   return 0;
  451. }
  452.  
  453. /* Return the function containing pc value PC.
  454.    Returns 0 if function is not known.  */
  455.  
  456. struct symbol *
  457. find_pc_function (pc)
  458.      CORE_ADDR pc;
  459. {
  460.   register struct block *b = block_for_pc (pc);
  461.   if (b == 0)
  462.     return 0;
  463.   return block_function (b);
  464. }
  465.  
  466. /* Finds the "function" (text symbol) that is smaller than PC
  467.    but greatest of all of the potential text symbols.  Sets
  468.    *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
  469.    Returns 0 if it couldn't find anything, 1 if it did.  On a zero
  470.    return, *NAME and *ADDRESS are always set to zero.  On a 1 return,
  471.    *NAME and *ADDRESS contain real information.  */
  472.  
  473. int
  474. find_pc_partial_function (pc, name, address)
  475.      CORE_ADDR pc;
  476.      char **name;
  477.      CORE_ADDR *address;
  478. {
  479.   struct partial_symtab *pst = find_pc_psymtab (pc);
  480.   struct symbol *f;
  481.   int miscfunc;
  482.   struct partial_symbol *psb;
  483.  
  484.   if (pst)
  485.     {
  486.       if (pst->readin)
  487.     {
  488.       /* The information we want has already been read in.
  489.          We can go to the already readin symbols and we'll get
  490.          the best possible answer.  */
  491.       f = find_pc_function (pc);
  492.       if (!f)
  493.         {
  494.         return_error:
  495.           /* No availible symbol.  */
  496.           if (name != 0)
  497.         *name = 0;
  498.           if (address != 0)
  499.         *address = 0;
  500.           return 0;
  501.         }
  502.  
  503.       if (name)
  504.         *name = SYMBOL_NAME (f);
  505.       if (address)
  506.         *address = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  507.       return 1;
  508.     }
  509.  
  510.       /* Get the information from a combination of the pst
  511.      (static symbols), and the misc function vector (extern
  512.      symbols).  */
  513.       miscfunc = find_pc_misc_function (pc);
  514.       psb = find_pc_psymbol (pst, pc);
  515.  
  516.       if (!psb && miscfunc == -1)
  517.     {
  518.       goto return_error;
  519.     }
  520.       if (!psb
  521.       || (miscfunc != -1
  522.           && (SYMBOL_VALUE(psb)
  523.           < misc_function_vector[miscfunc].address)))
  524.     {
  525.       if (address)
  526.         *address = misc_function_vector[miscfunc].address;
  527.       if (name)
  528.         *name = misc_function_vector[miscfunc].name;
  529.       return 1;
  530.     }
  531.       else
  532.     {
  533.       if (address)
  534.         *address = SYMBOL_VALUE (psb);
  535.       if (name)
  536.         *name = SYMBOL_NAME (psb);
  537.       return 1;
  538.     }
  539.     }
  540.   else
  541.     /* Must be in the misc function stuff.  */
  542.     {
  543.       miscfunc = find_pc_misc_function (pc);
  544.       if (miscfunc == -1)
  545.     goto return_error;
  546.       if (address)
  547.     *address = misc_function_vector[miscfunc].address;
  548.       if (name)
  549.     *name = misc_function_vector[miscfunc].name;
  550.       return 1;
  551.     }
  552. }
  553.  
  554. /* Find the misc function whose address is the largest
  555.    while being less than PC.  Return its index in misc_function_vector.
  556.    Returns -1 if PC is not in suitable range.  */
  557.  
  558. int
  559. find_pc_misc_function (pc)
  560.      register CORE_ADDR pc;
  561. {
  562.   register int lo = 0;
  563.   register int hi = misc_function_count-1;
  564.   register int new;
  565.   register int distance;
  566.  
  567.   /* Note that the last thing in the vector is always _etext.  */
  568.   /* Actually, "end", now that non-functions
  569.      go on the misc_function_vector.  */
  570.  
  571.   /* Above statement is not *always* true - fix for case where there are */
  572.   /* no misc functions at all (ie no symbol table has been read). */
  573.   if (hi < 0) return -1;        /* no misc functions recorded */
  574.  
  575.   /* trivial reject range test */
  576.   if (pc < misc_function_vector[0].address ||
  577.       pc > misc_function_vector[hi].address)
  578.     return -1;
  579.  
  580.   /* Note that the following search will not return hi if
  581.      pc == misc_function_vector[hi].address.  If "end" points to the
  582.      first unused location, this is correct and the above test
  583.      simply needs to be changed to
  584.      "pc >= misc_function_vector[hi].address".  */
  585.   do {
  586.     new = (lo + hi) >> 1;
  587.     distance = misc_function_vector[new].address - pc;
  588.     if (distance == 0)
  589.       return new;        /* an exact match */
  590.     else if (distance > 0)
  591.       hi = new;
  592.     else
  593.       lo = new;
  594.   } while (hi-lo != 1);
  595.  
  596.   /* if here, we had no exact match, so return the lower choice */
  597.   return lo;
  598. }
  599.  
  600. /* Return the innermost stack frame executing inside of the specified block,
  601.    or zero if there is no such frame.  */
  602.  
  603. FRAME
  604. block_innermost_frame (block)
  605.      struct block *block;
  606. {
  607.   struct frame_info *fi;
  608.   register FRAME frame;
  609.   register CORE_ADDR start = BLOCK_START (block);
  610.   register CORE_ADDR end = BLOCK_END (block);
  611.  
  612.   frame = 0;
  613.   while (1)
  614.     {
  615.       frame = get_prev_frame (frame);
  616.       if (frame == 0)
  617.     return 0;
  618.       fi = get_frame_info (frame);
  619.       if (fi->pc >= start && fi->pc < end)
  620.     return frame;
  621.     }
  622. }
  623.  
  624. void
  625. _initialize_blockframe ()
  626. {
  627.   obstack_init (&frame_cache_obstack);
  628. }
  629.