xref: /dflybsd-src/contrib/gdb-7/gdb/frame-unwind.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Definitions for frame unwinder, for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*c50c785cSJohn Marino    Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
4cf7f2e2dSJohn 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 "frame-unwind.h"
245796c8dcSSimon Schubert #include "dummy-frame.h"
255796c8dcSSimon Schubert #include "inline-frame.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "regcache.h"
28*c50c785cSJohn Marino #include "exceptions.h"
295796c8dcSSimon Schubert #include "gdb_assert.h"
305796c8dcSSimon Schubert #include "gdb_obstack.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert static struct gdbarch_data *frame_unwind_data;
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert struct frame_unwind_table_entry
355796c8dcSSimon Schubert {
365796c8dcSSimon Schubert   const struct frame_unwind *unwinder;
375796c8dcSSimon Schubert   struct frame_unwind_table_entry *next;
385796c8dcSSimon Schubert };
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert struct frame_unwind_table
415796c8dcSSimon Schubert {
425796c8dcSSimon Schubert   struct frame_unwind_table_entry *list;
435796c8dcSSimon Schubert   /* The head of the OSABI part of the search list.  */
445796c8dcSSimon Schubert   struct frame_unwind_table_entry **osabi_head;
455796c8dcSSimon Schubert };
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static void *
485796c8dcSSimon Schubert frame_unwind_init (struct obstack *obstack)
495796c8dcSSimon Schubert {
505796c8dcSSimon Schubert   struct frame_unwind_table *table
515796c8dcSSimon Schubert     = OBSTACK_ZALLOC (obstack, struct frame_unwind_table);
52cf7f2e2dSJohn Marino 
535796c8dcSSimon Schubert   /* Start the table out with a few default sniffers.  OSABI code
545796c8dcSSimon Schubert      can't override this.  */
555796c8dcSSimon Schubert   table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
56*c50c785cSJohn Marino   table->list->unwinder = &dummy_frame_unwind;
57*c50c785cSJohn Marino   table->list->next = OBSTACK_ZALLOC (obstack,
58*c50c785cSJohn Marino 				      struct frame_unwind_table_entry);
59*c50c785cSJohn Marino   table->list->next->unwinder = &inline_frame_unwind;
605796c8dcSSimon Schubert   /* The insertion point for OSABI sniffers.  */
615796c8dcSSimon Schubert   table->osabi_head = &table->list->next->next;
625796c8dcSSimon Schubert   return table;
635796c8dcSSimon Schubert }
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert void
665796c8dcSSimon Schubert frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
675796c8dcSSimon Schubert 				const struct frame_unwind *unwinder)
685796c8dcSSimon Schubert {
695796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
705796c8dcSSimon Schubert   struct frame_unwind_table_entry *entry;
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert   /* Insert the new entry at the start of the list.  */
735796c8dcSSimon Schubert   entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
745796c8dcSSimon Schubert   entry->unwinder = unwinder;
755796c8dcSSimon Schubert   entry->next = (*table->osabi_head);
765796c8dcSSimon Schubert   (*table->osabi_head) = entry;
775796c8dcSSimon Schubert }
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert void
805796c8dcSSimon Schubert frame_unwind_append_unwinder (struct gdbarch *gdbarch,
815796c8dcSSimon Schubert 			      const struct frame_unwind *unwinder)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
845796c8dcSSimon Schubert   struct frame_unwind_table_entry **ip;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert   /* Find the end of the list and insert the new entry there.  */
875796c8dcSSimon Schubert   for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
885796c8dcSSimon Schubert   (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
895796c8dcSSimon Schubert   (*ip)->unwinder = unwinder;
905796c8dcSSimon Schubert }
915796c8dcSSimon Schubert 
92*c50c785cSJohn Marino /* Iterate through sniffers for THIS_FRAME frame until one returns with an
93*c50c785cSJohn Marino    unwinder implementation.  THIS_FRAME->UNWIND must be NULL, it will get set
94*c50c785cSJohn Marino    by this function.  Possibly initialize THIS_CACHE.  */
95*c50c785cSJohn Marino 
96*c50c785cSJohn Marino void
975796c8dcSSimon Schubert frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache)
985796c8dcSSimon Schubert {
995796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1005796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
1015796c8dcSSimon Schubert   struct frame_unwind_table_entry *entry;
102cf7f2e2dSJohn Marino 
1035796c8dcSSimon Schubert   for (entry = table->list; entry != NULL; entry = entry->next)
1045796c8dcSSimon Schubert     {
1055796c8dcSSimon Schubert       struct cleanup *old_cleanup;
106*c50c785cSJohn Marino       volatile struct gdb_exception ex;
107*c50c785cSJohn Marino       int res = 0;
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert       old_cleanup = frame_prepare_for_sniffer (this_frame, entry->unwinder);
110*c50c785cSJohn Marino 
111*c50c785cSJohn Marino       TRY_CATCH (ex, RETURN_MASK_ERROR)
112*c50c785cSJohn Marino 	{
113*c50c785cSJohn Marino 	  res = entry->unwinder->sniffer (entry->unwinder, this_frame,
114*c50c785cSJohn Marino 					  this_cache);
115*c50c785cSJohn Marino 	}
116*c50c785cSJohn Marino       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
117*c50c785cSJohn Marino 	{
118*c50c785cSJohn Marino 	  /* This usually means that not even the PC is available,
119*c50c785cSJohn Marino 	     thus most unwinders aren't able to determine if they're
120*c50c785cSJohn Marino 	     the best fit.  Keep trying.  Fallback prologue unwinders
121*c50c785cSJohn Marino 	     should always accept the frame.  */
122*c50c785cSJohn Marino 	}
123*c50c785cSJohn Marino       else if (ex.reason < 0)
124*c50c785cSJohn Marino 	throw_exception (ex);
125*c50c785cSJohn Marino       else if (res)
1265796c8dcSSimon Schubert         {
1275796c8dcSSimon Schubert           discard_cleanups (old_cleanup);
128*c50c785cSJohn Marino           return;
1295796c8dcSSimon Schubert         }
130*c50c785cSJohn Marino 
1315796c8dcSSimon Schubert       do_cleanups (old_cleanup);
1325796c8dcSSimon Schubert     }
1335796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed"));
1345796c8dcSSimon Schubert }
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert /* A default frame sniffer which always accepts the frame.  Used by
1375796c8dcSSimon Schubert    fallback prologue unwinders.  */
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert int
1405796c8dcSSimon Schubert default_frame_sniffer (const struct frame_unwind *self,
1415796c8dcSSimon Schubert 		       struct frame_info *this_frame,
1425796c8dcSSimon Schubert 		       void **this_prologue_cache)
1435796c8dcSSimon Schubert {
1445796c8dcSSimon Schubert   return 1;
1455796c8dcSSimon Schubert }
1465796c8dcSSimon Schubert 
147*c50c785cSJohn Marino /* A default frame unwinder stop_reason callback that always claims
148*c50c785cSJohn Marino    the frame is unwindable.  */
149*c50c785cSJohn Marino 
150*c50c785cSJohn Marino enum unwind_stop_reason
151*c50c785cSJohn Marino default_frame_unwind_stop_reason (struct frame_info *this_frame,
152*c50c785cSJohn Marino 				  void **this_cache)
153*c50c785cSJohn Marino {
154*c50c785cSJohn Marino   return UNWIND_NO_REASON;
155*c50c785cSJohn Marino }
156*c50c785cSJohn Marino 
1575796c8dcSSimon Schubert /* Helper functions for value-based register unwinding.  These return
1585796c8dcSSimon Schubert    a (possibly lazy) value of the appropriate type.  */
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert /* Return a value which indicates that FRAME did not save REGNUM.  */
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert struct value *
1635796c8dcSSimon Schubert frame_unwind_got_optimized (struct frame_info *frame, int regnum)
1645796c8dcSSimon Schubert {
1655796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
1665796c8dcSSimon Schubert   struct value *reg_val;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
1695796c8dcSSimon Schubert   set_value_optimized_out (reg_val, 1);
1705796c8dcSSimon Schubert   return reg_val;
1715796c8dcSSimon Schubert }
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert /* Return a value which indicates that FRAME copied REGNUM into
1745796c8dcSSimon Schubert    register NEW_REGNUM.  */
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert struct value *
177*c50c785cSJohn Marino frame_unwind_got_register (struct frame_info *frame,
178*c50c785cSJohn Marino 			   int regnum, int new_regnum)
1795796c8dcSSimon Schubert {
1805796c8dcSSimon Schubert   return value_of_register_lazy (frame, new_regnum);
1815796c8dcSSimon Schubert }
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert /* Return a value which indicates that FRAME saved REGNUM in memory at
1845796c8dcSSimon Schubert    ADDR.  */
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert struct value *
1875796c8dcSSimon Schubert frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr)
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
1905796c8dcSSimon Schubert   struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert   set_value_stack (v, 1);
1935796c8dcSSimon Schubert   return v;
1945796c8dcSSimon Schubert }
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of
1975796c8dcSSimon Schubert    REGNUM has a known constant (computed) value of VAL.  */
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert struct value *
2005796c8dcSSimon Schubert frame_unwind_got_constant (struct frame_info *frame, int regnum,
2015796c8dcSSimon Schubert 			   ULONGEST val)
2025796c8dcSSimon Schubert {
2035796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
2045796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2055796c8dcSSimon Schubert   struct value *reg_val;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
2085796c8dcSSimon Schubert   store_unsigned_integer (value_contents_writeable (reg_val),
2095796c8dcSSimon Schubert 			  register_size (gdbarch, regnum), byte_order, val);
2105796c8dcSSimon Schubert   return reg_val;
2115796c8dcSSimon Schubert }
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert struct value *
2145796c8dcSSimon Schubert frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf)
2155796c8dcSSimon Schubert {
2165796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
2175796c8dcSSimon Schubert   struct value *reg_val;
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
2205796c8dcSSimon Schubert   memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum));
2215796c8dcSSimon Schubert   return reg_val;
2225796c8dcSSimon Schubert }
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of REGNUM
2255796c8dcSSimon Schubert    has a known constant (computed) value of ADDR.  Convert the
2265796c8dcSSimon Schubert    CORE_ADDR to a target address if necessary.  */
2275796c8dcSSimon Schubert 
2285796c8dcSSimon Schubert struct value *
2295796c8dcSSimon Schubert frame_unwind_got_address (struct frame_info *frame, int regnum,
2305796c8dcSSimon Schubert 			  CORE_ADDR addr)
2315796c8dcSSimon Schubert {
2325796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
2335796c8dcSSimon Schubert   struct value *reg_val;
2345796c8dcSSimon Schubert 
2355796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
2365796c8dcSSimon Schubert   pack_long (value_contents_writeable (reg_val),
2375796c8dcSSimon Schubert 	     register_type (gdbarch, regnum), addr);
2385796c8dcSSimon Schubert   return reg_val;
2395796c8dcSSimon Schubert }
2405796c8dcSSimon Schubert 
241*c50c785cSJohn Marino /* -Wmissing-prototypes */
242*c50c785cSJohn Marino extern initialize_file_ftype _initialize_frame_unwind;
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert void
2455796c8dcSSimon Schubert _initialize_frame_unwind (void)
2465796c8dcSSimon Schubert {
2475796c8dcSSimon Schubert   frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init);
2485796c8dcSSimon Schubert }
249