xref: /dflybsd-src/contrib/gdb-7/gdb/frame-unwind.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Definitions for frame unwinder, for GDB, the GNU debugger.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
4*5796c8dcSSimon Schubert 
5*5796c8dcSSimon Schubert    This file is part of GDB.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
8*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
9*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
10*5796c8dcSSimon Schubert    (at your option) any later version.
11*5796c8dcSSimon Schubert 
12*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
13*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*5796c8dcSSimon Schubert    GNU General Public License for more details.
16*5796c8dcSSimon Schubert 
17*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
18*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert #include "defs.h"
21*5796c8dcSSimon Schubert #include "frame.h"
22*5796c8dcSSimon Schubert #include "frame-unwind.h"
23*5796c8dcSSimon Schubert #include "dummy-frame.h"
24*5796c8dcSSimon Schubert #include "inline-frame.h"
25*5796c8dcSSimon Schubert #include "value.h"
26*5796c8dcSSimon Schubert #include "regcache.h"
27*5796c8dcSSimon Schubert 
28*5796c8dcSSimon Schubert #include "gdb_assert.h"
29*5796c8dcSSimon Schubert #include "gdb_obstack.h"
30*5796c8dcSSimon Schubert 
31*5796c8dcSSimon Schubert static struct gdbarch_data *frame_unwind_data;
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert struct frame_unwind_table_entry
34*5796c8dcSSimon Schubert {
35*5796c8dcSSimon Schubert   const struct frame_unwind *unwinder;
36*5796c8dcSSimon Schubert   struct frame_unwind_table_entry *next;
37*5796c8dcSSimon Schubert };
38*5796c8dcSSimon Schubert 
39*5796c8dcSSimon Schubert struct frame_unwind_table
40*5796c8dcSSimon Schubert {
41*5796c8dcSSimon Schubert   struct frame_unwind_table_entry *list;
42*5796c8dcSSimon Schubert   /* The head of the OSABI part of the search list.  */
43*5796c8dcSSimon Schubert   struct frame_unwind_table_entry **osabi_head;
44*5796c8dcSSimon Schubert };
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert static void *
47*5796c8dcSSimon Schubert frame_unwind_init (struct obstack *obstack)
48*5796c8dcSSimon Schubert {
49*5796c8dcSSimon Schubert   struct frame_unwind_table *table
50*5796c8dcSSimon Schubert     = OBSTACK_ZALLOC (obstack, struct frame_unwind_table);
51*5796c8dcSSimon Schubert   /* Start the table out with a few default sniffers.  OSABI code
52*5796c8dcSSimon Schubert      can't override this.  */
53*5796c8dcSSimon Schubert   table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
54*5796c8dcSSimon Schubert   table->list->unwinder = dummy_frame_unwind;
55*5796c8dcSSimon Schubert   table->list->next = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
56*5796c8dcSSimon Schubert   table->list->next->unwinder = inline_frame_unwind;
57*5796c8dcSSimon Schubert   /* The insertion point for OSABI sniffers.  */
58*5796c8dcSSimon Schubert   table->osabi_head = &table->list->next->next;
59*5796c8dcSSimon Schubert   return table;
60*5796c8dcSSimon Schubert }
61*5796c8dcSSimon Schubert 
62*5796c8dcSSimon Schubert void
63*5796c8dcSSimon Schubert frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
64*5796c8dcSSimon Schubert 				const struct frame_unwind *unwinder)
65*5796c8dcSSimon Schubert {
66*5796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
67*5796c8dcSSimon Schubert   struct frame_unwind_table_entry *entry;
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert   /* Insert the new entry at the start of the list.  */
70*5796c8dcSSimon Schubert   entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
71*5796c8dcSSimon Schubert   entry->unwinder = unwinder;
72*5796c8dcSSimon Schubert   entry->next = (*table->osabi_head);
73*5796c8dcSSimon Schubert   (*table->osabi_head) = entry;
74*5796c8dcSSimon Schubert }
75*5796c8dcSSimon Schubert 
76*5796c8dcSSimon Schubert void
77*5796c8dcSSimon Schubert frame_unwind_append_unwinder (struct gdbarch *gdbarch,
78*5796c8dcSSimon Schubert 			      const struct frame_unwind *unwinder)
79*5796c8dcSSimon Schubert {
80*5796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
81*5796c8dcSSimon Schubert   struct frame_unwind_table_entry **ip;
82*5796c8dcSSimon Schubert 
83*5796c8dcSSimon Schubert   /* Find the end of the list and insert the new entry there.  */
84*5796c8dcSSimon Schubert   for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
85*5796c8dcSSimon Schubert   (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
86*5796c8dcSSimon Schubert   (*ip)->unwinder = unwinder;
87*5796c8dcSSimon Schubert }
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert const struct frame_unwind *
90*5796c8dcSSimon Schubert frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache)
91*5796c8dcSSimon Schubert {
92*5796c8dcSSimon Schubert   int i;
93*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
94*5796c8dcSSimon Schubert   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
95*5796c8dcSSimon Schubert   struct frame_unwind_table_entry *entry;
96*5796c8dcSSimon Schubert   struct cleanup *old_cleanup;
97*5796c8dcSSimon Schubert   for (entry = table->list; entry != NULL; entry = entry->next)
98*5796c8dcSSimon Schubert     {
99*5796c8dcSSimon Schubert       struct cleanup *old_cleanup;
100*5796c8dcSSimon Schubert 
101*5796c8dcSSimon Schubert       old_cleanup = frame_prepare_for_sniffer (this_frame, entry->unwinder);
102*5796c8dcSSimon Schubert       if (entry->unwinder->sniffer (entry->unwinder, this_frame,
103*5796c8dcSSimon Schubert 				    this_cache))
104*5796c8dcSSimon Schubert 	{
105*5796c8dcSSimon Schubert 	  discard_cleanups (old_cleanup);
106*5796c8dcSSimon Schubert 	  return entry->unwinder;
107*5796c8dcSSimon Schubert 	}
108*5796c8dcSSimon Schubert       do_cleanups (old_cleanup);
109*5796c8dcSSimon Schubert     }
110*5796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed"));
111*5796c8dcSSimon Schubert }
112*5796c8dcSSimon Schubert 
113*5796c8dcSSimon Schubert /* A default frame sniffer which always accepts the frame.  Used by
114*5796c8dcSSimon Schubert    fallback prologue unwinders.  */
115*5796c8dcSSimon Schubert 
116*5796c8dcSSimon Schubert int
117*5796c8dcSSimon Schubert default_frame_sniffer (const struct frame_unwind *self,
118*5796c8dcSSimon Schubert 		       struct frame_info *this_frame,
119*5796c8dcSSimon Schubert 		       void **this_prologue_cache)
120*5796c8dcSSimon Schubert {
121*5796c8dcSSimon Schubert   return 1;
122*5796c8dcSSimon Schubert }
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert /* Helper functions for value-based register unwinding.  These return
125*5796c8dcSSimon Schubert    a (possibly lazy) value of the appropriate type.  */
126*5796c8dcSSimon Schubert 
127*5796c8dcSSimon Schubert /* Return a value which indicates that FRAME did not save REGNUM.  */
128*5796c8dcSSimon Schubert 
129*5796c8dcSSimon Schubert struct value *
130*5796c8dcSSimon Schubert frame_unwind_got_optimized (struct frame_info *frame, int regnum)
131*5796c8dcSSimon Schubert {
132*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
133*5796c8dcSSimon Schubert   struct value *reg_val;
134*5796c8dcSSimon Schubert 
135*5796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
136*5796c8dcSSimon Schubert   set_value_optimized_out (reg_val, 1);
137*5796c8dcSSimon Schubert   return reg_val;
138*5796c8dcSSimon Schubert }
139*5796c8dcSSimon Schubert 
140*5796c8dcSSimon Schubert /* Return a value which indicates that FRAME copied REGNUM into
141*5796c8dcSSimon Schubert    register NEW_REGNUM.  */
142*5796c8dcSSimon Schubert 
143*5796c8dcSSimon Schubert struct value *
144*5796c8dcSSimon Schubert frame_unwind_got_register (struct frame_info *frame, int regnum, int new_regnum)
145*5796c8dcSSimon Schubert {
146*5796c8dcSSimon Schubert   return value_of_register_lazy (frame, new_regnum);
147*5796c8dcSSimon Schubert }
148*5796c8dcSSimon Schubert 
149*5796c8dcSSimon Schubert /* Return a value which indicates that FRAME saved REGNUM in memory at
150*5796c8dcSSimon Schubert    ADDR.  */
151*5796c8dcSSimon Schubert 
152*5796c8dcSSimon Schubert struct value *
153*5796c8dcSSimon Schubert frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr)
154*5796c8dcSSimon Schubert {
155*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
156*5796c8dcSSimon Schubert   struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
157*5796c8dcSSimon Schubert 
158*5796c8dcSSimon Schubert   set_value_stack (v, 1);
159*5796c8dcSSimon Schubert   return v;
160*5796c8dcSSimon Schubert }
161*5796c8dcSSimon Schubert 
162*5796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of
163*5796c8dcSSimon Schubert    REGNUM has a known constant (computed) value of VAL.  */
164*5796c8dcSSimon Schubert 
165*5796c8dcSSimon Schubert struct value *
166*5796c8dcSSimon Schubert frame_unwind_got_constant (struct frame_info *frame, int regnum,
167*5796c8dcSSimon Schubert 			   ULONGEST val)
168*5796c8dcSSimon Schubert {
169*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
170*5796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
171*5796c8dcSSimon Schubert   struct value *reg_val;
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
174*5796c8dcSSimon Schubert   store_unsigned_integer (value_contents_writeable (reg_val),
175*5796c8dcSSimon Schubert 			  register_size (gdbarch, regnum), byte_order, val);
176*5796c8dcSSimon Schubert   return reg_val;
177*5796c8dcSSimon Schubert }
178*5796c8dcSSimon Schubert 
179*5796c8dcSSimon Schubert struct value *
180*5796c8dcSSimon Schubert frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf)
181*5796c8dcSSimon Schubert {
182*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
183*5796c8dcSSimon Schubert   struct value *reg_val;
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
186*5796c8dcSSimon Schubert   memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum));
187*5796c8dcSSimon Schubert   return reg_val;
188*5796c8dcSSimon Schubert }
189*5796c8dcSSimon Schubert 
190*5796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of REGNUM
191*5796c8dcSSimon Schubert    has a known constant (computed) value of ADDR.  Convert the
192*5796c8dcSSimon Schubert    CORE_ADDR to a target address if necessary.  */
193*5796c8dcSSimon Schubert 
194*5796c8dcSSimon Schubert struct value *
195*5796c8dcSSimon Schubert frame_unwind_got_address (struct frame_info *frame, int regnum,
196*5796c8dcSSimon Schubert 			  CORE_ADDR addr)
197*5796c8dcSSimon Schubert {
198*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
199*5796c8dcSSimon Schubert   struct value *reg_val;
200*5796c8dcSSimon Schubert 
201*5796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
202*5796c8dcSSimon Schubert   pack_long (value_contents_writeable (reg_val),
203*5796c8dcSSimon Schubert 	     register_type (gdbarch, regnum), addr);
204*5796c8dcSSimon Schubert   return reg_val;
205*5796c8dcSSimon Schubert }
206*5796c8dcSSimon Schubert 
207*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_frame_unwind; /* -Wmissing-prototypes */
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert void
210*5796c8dcSSimon Schubert _initialize_frame_unwind (void)
211*5796c8dcSSimon Schubert {
212*5796c8dcSSimon Schubert   frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init);
213*5796c8dcSSimon Schubert }
214