xref: /dflybsd-src/contrib/gdb-7/gdb/trad-frame.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Traditional frame unwind support, for GDB the GNU Debugger.
25796c8dcSSimon Schubert 
3*cf7f2e2dSJohn Marino    Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
4*cf7f2e2dSJohn Marino    Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "frame.h"
235796c8dcSSimon Schubert #include "trad-frame.h"
245796c8dcSSimon Schubert #include "regcache.h"
255796c8dcSSimon Schubert #include "frame-unwind.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert struct trad_frame_cache
295796c8dcSSimon Schubert {
305796c8dcSSimon Schubert   struct frame_info *this_frame;
315796c8dcSSimon Schubert   CORE_ADDR this_base;
325796c8dcSSimon Schubert   struct trad_frame_saved_reg *prev_regs;
335796c8dcSSimon Schubert   struct frame_id this_id;
345796c8dcSSimon Schubert };
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert struct trad_frame_cache *
375796c8dcSSimon Schubert trad_frame_cache_zalloc (struct frame_info *this_frame)
385796c8dcSSimon Schubert {
395796c8dcSSimon Schubert   struct trad_frame_cache *this_trad_cache;
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert   this_trad_cache = FRAME_OBSTACK_ZALLOC (struct trad_frame_cache);
425796c8dcSSimon Schubert   this_trad_cache->prev_regs = trad_frame_alloc_saved_regs (this_frame);
435796c8dcSSimon Schubert   this_trad_cache->this_frame = this_frame;
445796c8dcSSimon Schubert   return this_trad_cache;
455796c8dcSSimon Schubert }
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert /* A traditional frame is unwound by analysing the function prologue
485796c8dcSSimon Schubert    and using the information gathered to track registers.  For
495796c8dcSSimon Schubert    non-optimized frames, the technique is reliable (just need to check
505796c8dcSSimon Schubert    for all potential instruction sequences).  */
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert struct trad_frame_saved_reg *
535796c8dcSSimon Schubert trad_frame_alloc_saved_regs (struct frame_info *this_frame)
545796c8dcSSimon Schubert {
555796c8dcSSimon Schubert   int regnum;
565796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
575796c8dcSSimon Schubert   int numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
585796c8dcSSimon Schubert   struct trad_frame_saved_reg *this_saved_regs
595796c8dcSSimon Schubert     = FRAME_OBSTACK_CALLOC (numregs, struct trad_frame_saved_reg);
60*cf7f2e2dSJohn Marino 
615796c8dcSSimon Schubert   for (regnum = 0; regnum < numregs; regnum++)
625796c8dcSSimon Schubert     {
635796c8dcSSimon Schubert       this_saved_regs[regnum].realreg = regnum;
645796c8dcSSimon Schubert       this_saved_regs[regnum].addr = -1;
655796c8dcSSimon Schubert     }
665796c8dcSSimon Schubert   return this_saved_regs;
675796c8dcSSimon Schubert }
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert enum { REG_VALUE = -1, REG_UNKNOWN = -2 };
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert int
725796c8dcSSimon Schubert trad_frame_value_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
735796c8dcSSimon Schubert {
745796c8dcSSimon Schubert   return (this_saved_regs[regnum].realreg == REG_VALUE);
755796c8dcSSimon Schubert }
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert int
785796c8dcSSimon Schubert trad_frame_addr_p (struct trad_frame_saved_reg this_saved_regs[], int regnum)
795796c8dcSSimon Schubert {
805796c8dcSSimon Schubert   return (this_saved_regs[regnum].realreg >= 0
815796c8dcSSimon Schubert 	  && this_saved_regs[regnum].addr != -1);
825796c8dcSSimon Schubert }
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert int
855796c8dcSSimon Schubert trad_frame_realreg_p (struct trad_frame_saved_reg this_saved_regs[],
865796c8dcSSimon Schubert 		      int regnum)
875796c8dcSSimon Schubert {
885796c8dcSSimon Schubert   return (this_saved_regs[regnum].realreg >= 0
895796c8dcSSimon Schubert 	  && this_saved_regs[regnum].addr == -1);
905796c8dcSSimon Schubert }
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert void
935796c8dcSSimon Schubert trad_frame_set_value (struct trad_frame_saved_reg this_saved_regs[],
945796c8dcSSimon Schubert 		      int regnum, LONGEST val)
955796c8dcSSimon Schubert {
965796c8dcSSimon Schubert   /* Make the REALREG invalid, indicating that the ADDR contains the
975796c8dcSSimon Schubert      register's value.  */
985796c8dcSSimon Schubert   this_saved_regs[regnum].realreg = REG_VALUE;
995796c8dcSSimon Schubert   this_saved_regs[regnum].addr = val;
1005796c8dcSSimon Schubert }
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert void
1035796c8dcSSimon Schubert trad_frame_set_reg_value (struct trad_frame_cache *this_trad_cache,
1045796c8dcSSimon Schubert 			  int regnum, LONGEST val)
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert   /* External interface for users of trad_frame_cache
1075796c8dcSSimon Schubert      (who cannot access the prev_regs object directly).  */
1085796c8dcSSimon Schubert   trad_frame_set_value (this_trad_cache->prev_regs, regnum, val);
1095796c8dcSSimon Schubert }
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert void
1125796c8dcSSimon Schubert trad_frame_set_reg_realreg (struct trad_frame_cache *this_trad_cache,
1135796c8dcSSimon Schubert 			    int regnum, int realreg)
1145796c8dcSSimon Schubert {
1155796c8dcSSimon Schubert   this_trad_cache->prev_regs[regnum].realreg = realreg;
1165796c8dcSSimon Schubert   this_trad_cache->prev_regs[regnum].addr = -1;
1175796c8dcSSimon Schubert }
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert void
1205796c8dcSSimon Schubert trad_frame_set_reg_addr (struct trad_frame_cache *this_trad_cache,
1215796c8dcSSimon Schubert 			 int regnum, CORE_ADDR addr)
1225796c8dcSSimon Schubert {
1235796c8dcSSimon Schubert   this_trad_cache->prev_regs[regnum].addr = addr;
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert void
1275796c8dcSSimon Schubert trad_frame_set_unknown (struct trad_frame_saved_reg this_saved_regs[],
1285796c8dcSSimon Schubert 			int regnum)
1295796c8dcSSimon Schubert {
1305796c8dcSSimon Schubert   /* Make the REALREG invalid, indicating that the value is not known.  */
1315796c8dcSSimon Schubert   this_saved_regs[regnum].realreg = REG_UNKNOWN;
1325796c8dcSSimon Schubert   this_saved_regs[regnum].addr = -1;
1335796c8dcSSimon Schubert }
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert struct value *
1365796c8dcSSimon Schubert trad_frame_get_prev_register (struct frame_info *this_frame,
1375796c8dcSSimon Schubert 			      struct trad_frame_saved_reg this_saved_regs[],
1385796c8dcSSimon Schubert 			      int regnum)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   if (trad_frame_addr_p (this_saved_regs, regnum))
1415796c8dcSSimon Schubert     /* The register was saved in memory.  */
1425796c8dcSSimon Schubert     return frame_unwind_got_memory (this_frame, regnum,
1435796c8dcSSimon Schubert 				    this_saved_regs[regnum].addr);
1445796c8dcSSimon Schubert   else if (trad_frame_realreg_p (this_saved_regs, regnum))
1455796c8dcSSimon Schubert     return frame_unwind_got_register (this_frame, regnum,
1465796c8dcSSimon Schubert 				      this_saved_regs[regnum].realreg);
1475796c8dcSSimon Schubert   else if (trad_frame_value_p (this_saved_regs, regnum))
1485796c8dcSSimon Schubert     /* The register's value is available.  */
1495796c8dcSSimon Schubert     return frame_unwind_got_constant (this_frame, regnum,
1505796c8dcSSimon Schubert 				      this_saved_regs[regnum].addr);
1515796c8dcSSimon Schubert   else
1525796c8dcSSimon Schubert     return frame_unwind_got_optimized (this_frame, regnum);
1535796c8dcSSimon Schubert }
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert struct value *
1565796c8dcSSimon Schubert trad_frame_get_register (struct trad_frame_cache *this_trad_cache,
1575796c8dcSSimon Schubert 			 struct frame_info *this_frame,
1585796c8dcSSimon Schubert 			 int regnum)
1595796c8dcSSimon Schubert {
1605796c8dcSSimon Schubert   return trad_frame_get_prev_register (this_frame, this_trad_cache->prev_regs,
1615796c8dcSSimon Schubert 				       regnum);
1625796c8dcSSimon Schubert }
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert void
1655796c8dcSSimon Schubert trad_frame_set_id (struct trad_frame_cache *this_trad_cache,
1665796c8dcSSimon Schubert 		   struct frame_id this_id)
1675796c8dcSSimon Schubert {
1685796c8dcSSimon Schubert   this_trad_cache->this_id = this_id;
1695796c8dcSSimon Schubert }
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert void
1725796c8dcSSimon Schubert trad_frame_get_id (struct trad_frame_cache *this_trad_cache,
1735796c8dcSSimon Schubert 		   struct frame_id *this_id)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   (*this_id) = this_trad_cache->this_id;
1765796c8dcSSimon Schubert }
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert void
1795796c8dcSSimon Schubert trad_frame_set_this_base (struct trad_frame_cache *this_trad_cache,
1805796c8dcSSimon Schubert 			  CORE_ADDR this_base)
1815796c8dcSSimon Schubert {
1825796c8dcSSimon Schubert   this_trad_cache->this_base = this_base;
1835796c8dcSSimon Schubert }
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert CORE_ADDR
1865796c8dcSSimon Schubert trad_frame_get_this_base (struct trad_frame_cache *this_trad_cache)
1875796c8dcSSimon Schubert {
1885796c8dcSSimon Schubert   return this_trad_cache->this_base;
1895796c8dcSSimon Schubert }
190