gdbserver: Move remote_debug to a single place
[external/binutils.git] / gdb / frame-unwind.c
1 /* Definitions for frame unwinder, for GDB, the GNU debugger.
2
3    Copyright (C) 2003-2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program 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 3 of the License, or
10    (at your option) any later version.
11
12    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-unwind.h"
23 #include "dummy-frame.h"
24 #include "inline-frame.h"
25 #include "value.h"
26 #include "regcache.h"
27 #include "gdb_obstack.h"
28 #include "target.h"
29
30 static struct gdbarch_data *frame_unwind_data;
31
32 struct frame_unwind_table_entry
33 {
34   const struct frame_unwind *unwinder;
35   struct frame_unwind_table_entry *next;
36 };
37
38 struct frame_unwind_table
39 {
40   struct frame_unwind_table_entry *list;
41   /* The head of the OSABI part of the search list.  */
42   struct frame_unwind_table_entry **osabi_head;
43 };
44
45 static void *
46 frame_unwind_init (struct obstack *obstack)
47 {
48   struct frame_unwind_table *table
49     = OBSTACK_ZALLOC (obstack, struct frame_unwind_table);
50
51   /* Start the table out with a few default sniffers.  OSABI code
52      can't override this.  */
53   table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
54   table->list->unwinder = &dummy_frame_unwind;
55   table->list->next = OBSTACK_ZALLOC (obstack,
56                                       struct frame_unwind_table_entry);
57   table->list->next->unwinder = &inline_frame_unwind;
58   /* The insertion point for OSABI sniffers.  */
59   table->osabi_head = &table->list->next->next;
60   return table;
61 }
62
63 void
64 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
65                                 const struct frame_unwind *unwinder)
66 {
67   struct frame_unwind_table *table
68     = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
69   struct frame_unwind_table_entry *entry;
70
71   /* Insert the new entry at the start of the list.  */
72   entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
73   entry->unwinder = unwinder;
74   entry->next = (*table->osabi_head);
75   (*table->osabi_head) = entry;
76 }
77
78 void
79 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
80                               const struct frame_unwind *unwinder)
81 {
82   struct frame_unwind_table *table
83     = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
84   struct frame_unwind_table_entry **ip;
85
86   /* Find the end of the list and insert the new entry there.  */
87   for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
88   (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
89   (*ip)->unwinder = unwinder;
90 }
91
92 /* Call SNIFFER from UNWINDER.  If it succeeded set UNWINDER for
93    THIS_FRAME and return 1.  Otherwise the function keeps THIS_FRAME
94    unchanged and returns 0.  */
95
96 static int
97 frame_unwind_try_unwinder (struct frame_info *this_frame, void **this_cache,
98                           const struct frame_unwind *unwinder)
99 {
100   int res = 0;
101
102   frame_prepare_for_sniffer (this_frame, unwinder);
103
104   try
105     {
106       res = unwinder->sniffer (unwinder, this_frame, this_cache);
107     }
108   catch (const gdb_exception &ex)
109     {
110       /* Catch all exceptions, caused by either interrupt or error.
111          Reset *THIS_CACHE.  */
112       *this_cache = NULL;
113       frame_cleanup_after_sniffer (this_frame);
114
115       if (ex.error == NOT_AVAILABLE_ERROR)
116         {
117           /* This usually means that not even the PC is available,
118              thus most unwinders aren't able to determine if they're
119              the best fit.  Keep trying.  Fallback prologue unwinders
120              should always accept the frame.  */
121           return 0;
122         }
123       throw;
124     }
125
126   if (res)
127     return 1;
128   else
129     {
130       /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
131          so.  */
132       frame_cleanup_after_sniffer (this_frame);
133       return 0;
134     }
135   gdb_assert_not_reached ("frame_unwind_try_unwinder");
136 }
137
138 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
139    unwinder implementation.  THIS_FRAME->UNWIND must be NULL, it will get set
140    by this function.  Possibly initialize THIS_CACHE.  */
141
142 void
143 frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache)
144 {
145   struct gdbarch *gdbarch = get_frame_arch (this_frame);
146   struct frame_unwind_table *table
147     = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
148   struct frame_unwind_table_entry *entry;
149   const struct frame_unwind *unwinder_from_target;
150
151   unwinder_from_target = target_get_unwinder ();
152   if (unwinder_from_target != NULL
153       && frame_unwind_try_unwinder (this_frame, this_cache,
154                                    unwinder_from_target))
155     return;
156
157   unwinder_from_target = target_get_tailcall_unwinder ();
158   if (unwinder_from_target != NULL
159       && frame_unwind_try_unwinder (this_frame, this_cache,
160                                    unwinder_from_target))
161     return;
162
163   for (entry = table->list; entry != NULL; entry = entry->next)
164     if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
165       return;
166
167   internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed"));
168 }
169
170 /* A default frame sniffer which always accepts the frame.  Used by
171    fallback prologue unwinders.  */
172
173 int
174 default_frame_sniffer (const struct frame_unwind *self,
175                        struct frame_info *this_frame,
176                        void **this_prologue_cache)
177 {
178   return 1;
179 }
180
181 /* The default frame unwinder stop_reason callback.  */
182
183 enum unwind_stop_reason
184 default_frame_unwind_stop_reason (struct frame_info *this_frame,
185                                   void **this_cache)
186 {
187   struct frame_id this_id = get_frame_id (this_frame);
188
189   if (frame_id_eq (this_id, outer_frame_id))
190     return UNWIND_OUTERMOST;
191   else
192     return UNWIND_NO_REASON;
193 }
194
195 /* See frame-unwind.h.  */
196
197 CORE_ADDR
198 default_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
199 {
200   int pc_regnum = gdbarch_pc_regnum (gdbarch);
201   CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
202   pc = gdbarch_addr_bits_remove (gdbarch, pc);
203   return pc;
204 }
205
206 /* See frame-unwind.h.  */
207
208 CORE_ADDR
209 default_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
210 {
211   int sp_regnum = gdbarch_sp_regnum (gdbarch);
212   return frame_unwind_register_unsigned (next_frame, sp_regnum);
213 }
214
215 /* Helper functions for value-based register unwinding.  These return
216    a (possibly lazy) value of the appropriate type.  */
217
218 /* Return a value which indicates that FRAME did not save REGNUM.  */
219
220 struct value *
221 frame_unwind_got_optimized (struct frame_info *frame, int regnum)
222 {
223   struct gdbarch *gdbarch = frame_unwind_arch (frame);
224   struct type *type = register_type (gdbarch, regnum);
225   struct value *val;
226
227   /* Return an lval_register value, so that we print it as
228      "<not saved>".  */
229   val = allocate_value_lazy (type);
230   set_value_lazy (val, 0);
231   mark_value_bytes_optimized_out (val, 0, TYPE_LENGTH (type));
232   VALUE_LVAL (val) = lval_register;
233   VALUE_REGNUM (val) = regnum;
234   VALUE_NEXT_FRAME_ID (val)
235     = get_frame_id (get_next_frame_sentinel_okay (frame));
236   return val;
237 }
238
239 /* Return a value which indicates that FRAME copied REGNUM into
240    register NEW_REGNUM.  */
241
242 struct value *
243 frame_unwind_got_register (struct frame_info *frame,
244                            int regnum, int new_regnum)
245 {
246   return value_of_register_lazy (frame, new_regnum);
247 }
248
249 /* Return a value which indicates that FRAME saved REGNUM in memory at
250    ADDR.  */
251
252 struct value *
253 frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr)
254 {
255   struct gdbarch *gdbarch = frame_unwind_arch (frame);
256   struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
257
258   set_value_stack (v, 1);
259   return v;
260 }
261
262 /* Return a value which indicates that FRAME's saved version of
263    REGNUM has a known constant (computed) value of VAL.  */
264
265 struct value *
266 frame_unwind_got_constant (struct frame_info *frame, int regnum,
267                            ULONGEST val)
268 {
269   struct gdbarch *gdbarch = frame_unwind_arch (frame);
270   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
271   struct value *reg_val;
272
273   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
274   store_unsigned_integer (value_contents_writeable (reg_val),
275                           register_size (gdbarch, regnum), byte_order, val);
276   return reg_val;
277 }
278
279 struct value *
280 frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf)
281 {
282   struct gdbarch *gdbarch = frame_unwind_arch (frame);
283   struct value *reg_val;
284
285   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
286   memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum));
287   return reg_val;
288 }
289
290 /* Return a value which indicates that FRAME's saved version of REGNUM
291    has a known constant (computed) value of ADDR.  Convert the
292    CORE_ADDR to a target address if necessary.  */
293
294 struct value *
295 frame_unwind_got_address (struct frame_info *frame, int regnum,
296                           CORE_ADDR addr)
297 {
298   struct gdbarch *gdbarch = frame_unwind_arch (frame);
299   struct value *reg_val;
300
301   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
302   pack_long (value_contents_writeable (reg_val),
303              register_type (gdbarch, regnum), addr);
304   return reg_val;
305 }
306
307 void
308 _initialize_frame_unwind (void)
309 {
310   frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init);
311 }