1 /* Definitions for frame unwinder, for GDB, the GNU debugger.
2 
3    Copyright (C) 2003-2024 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 "extract-store-integer.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 "gdbsupport/gdb_obstack.h"
28 #include "target.h"
29 #include "gdbarch.h"
30 #include "dwarf2/frame-tailcall.h"
31 #include "cli/cli-cmds.h"
32 #include "inferior.h"
33 
34 struct frame_unwind_table_entry
35 {
36   const struct frame_unwind *unwinder;
37   struct frame_unwind_table_entry *next;
38 };
39 
40 struct frame_unwind_table
41 {
42   struct frame_unwind_table_entry *list = nullptr;
43   /* The head of the OSABI part of the search list.  */
44   struct frame_unwind_table_entry **osabi_head = nullptr;
45 };
46 
47 static const registry<gdbarch>::key<struct frame_unwind_table>
48      frame_unwind_data;
49 
50 /* A helper function to add an unwinder to a list.  LINK says where to
51    install the new unwinder.  The new link is returned.  */
52 
53 static struct frame_unwind_table_entry **
add_unwinder(struct obstack * obstack,const struct frame_unwind * unwinder,struct frame_unwind_table_entry ** link)54 add_unwinder (struct obstack *obstack, const struct frame_unwind *unwinder,
55                 struct frame_unwind_table_entry **link)
56 {
57   *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
58   (*link)->unwinder = unwinder;
59   return &(*link)->next;
60 }
61 
62 static struct frame_unwind_table *
get_frame_unwind_table(struct gdbarch * gdbarch)63 get_frame_unwind_table (struct gdbarch *gdbarch)
64 {
65   struct frame_unwind_table *table = frame_unwind_data.get (gdbarch);
66   if (table != nullptr)
67     return table;
68 
69   table = new frame_unwind_table;
70 
71   /* Start the table out with a few default sniffers.  OSABI code
72      can't override this.  */
73   struct frame_unwind_table_entry **link = &table->list;
74 
75   struct obstack *obstack = gdbarch_obstack (gdbarch);
76   link = add_unwinder (obstack, &dummy_frame_unwind, link);
77   /* The DWARF tailcall sniffer must come before the inline sniffer.
78      Otherwise, we can end up in a situation where a DWARF frame finds
79      tailcall information, but then the inline sniffer claims a frame
80      before the tailcall sniffer, resulting in confusion.  This is
81      safe to do always because the tailcall sniffer can only ever be
82      activated if the newer frame was created using the DWARF
83      unwinder, and it also found tailcall information.  */
84   link = add_unwinder (obstack, &dwarf2_tailcall_frame_unwind, link);
85   link = add_unwinder (obstack, &inline_frame_unwind, link);
86 
87   /* The insertion point for OSABI sniffers.  */
88   table->osabi_head = link;
89   frame_unwind_data.set (gdbarch, table);
90 
91   return table;
92 }
93 
94 void
frame_unwind_prepend_unwinder(struct gdbarch * gdbarch,const struct frame_unwind * unwinder)95 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
96                                         const struct frame_unwind *unwinder)
97 {
98   struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
99   struct frame_unwind_table_entry *entry;
100 
101   /* Insert the new entry at the start of the list.  */
102   entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
103   entry->unwinder = unwinder;
104   entry->next = (*table->osabi_head);
105   (*table->osabi_head) = entry;
106 }
107 
108 void
frame_unwind_append_unwinder(struct gdbarch * gdbarch,const struct frame_unwind * unwinder)109 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
110                                     const struct frame_unwind *unwinder)
111 {
112   struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
113   struct frame_unwind_table_entry **ip;
114 
115   /* Find the end of the list and insert the new entry there.  */
116   for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
117   (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
118   (*ip)->unwinder = unwinder;
119 }
120 
121 /* Call SNIFFER from UNWINDER.  If it succeeded set UNWINDER for
122    THIS_FRAME and return 1.  Otherwise the function keeps THIS_FRAME
123    unchanged and returns 0.  */
124 
125 static int
frame_unwind_try_unwinder(const frame_info_ptr & this_frame,void ** this_cache,const struct frame_unwind * unwinder)126 frame_unwind_try_unwinder (const frame_info_ptr &this_frame, void **this_cache,
127                                 const struct frame_unwind *unwinder)
128 {
129   int res = 0;
130 
131   unsigned int entry_generation = get_frame_cache_generation ();
132 
133   frame_prepare_for_sniffer (this_frame, unwinder);
134 
135   try
136     {
137       frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
138       res = unwinder->sniffer (unwinder, this_frame, this_cache);
139     }
140   catch (const gdb_exception &ex)
141     {
142       frame_debug_printf ("caught exception: %s", ex.message->c_str ());
143 
144       /* Catch all exceptions, caused by either interrupt or error.
145            Reset *THIS_CACHE, unless something reinitialized the frame
146            cache meanwhile, in which case THIS_FRAME/THIS_CACHE are now
147            dangling.  */
148       if (get_frame_cache_generation () == entry_generation)
149           {
150             *this_cache = NULL;
151             frame_cleanup_after_sniffer (this_frame);
152           }
153 
154       if (ex.error == NOT_AVAILABLE_ERROR)
155           {
156             /* This usually means that not even the PC is available,
157                thus most unwinders aren't able to determine if they're
158                the best fit.  Keep trying.  Fallback prologue unwinders
159                should always accept the frame.  */
160             return 0;
161           }
162       throw;
163     }
164 
165   if (res)
166     {
167       frame_debug_printf ("yes");
168       return 1;
169     }
170   else
171     {
172       frame_debug_printf ("no");
173       /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
174            so.  */
175       frame_cleanup_after_sniffer (this_frame);
176       return 0;
177     }
178   gdb_assert_not_reached ("frame_unwind_try_unwinder");
179 }
180 
181 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
182    unwinder implementation.  THIS_FRAME->UNWIND must be NULL, it will get set
183    by this function.  Possibly initialize THIS_CACHE.  */
184 
185 void
frame_unwind_find_by_frame(const frame_info_ptr & this_frame,void ** this_cache)186 frame_unwind_find_by_frame (const frame_info_ptr &this_frame, void **this_cache)
187 {
188   FRAME_SCOPED_DEBUG_ENTER_EXIT;
189   frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
190 
191   struct gdbarch *gdbarch = get_frame_arch (this_frame);
192   struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
193   struct frame_unwind_table_entry *entry;
194   const struct frame_unwind *unwinder_from_target;
195 
196   unwinder_from_target = target_get_unwinder ();
197   if (unwinder_from_target != NULL
198       && frame_unwind_try_unwinder (this_frame, this_cache,
199                                            unwinder_from_target))
200     return;
201 
202   unwinder_from_target = target_get_tailcall_unwinder ();
203   if (unwinder_from_target != NULL
204       && frame_unwind_try_unwinder (this_frame, this_cache,
205                                            unwinder_from_target))
206     return;
207 
208   for (entry = table->list; entry != NULL; entry = entry->next)
209     if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
210       return;
211 
212   internal_error (_("frame_unwind_find_by_frame failed"));
213 }
214 
215 /* A default frame sniffer which always accepts the frame.  Used by
216    fallback prologue unwinders.  */
217 
218 int
default_frame_sniffer(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_prologue_cache)219 default_frame_sniffer (const struct frame_unwind *self,
220                            const frame_info_ptr &this_frame,
221                            void **this_prologue_cache)
222 {
223   return 1;
224 }
225 
226 /* The default frame unwinder stop_reason callback.  */
227 
228 enum unwind_stop_reason
default_frame_unwind_stop_reason(const frame_info_ptr & this_frame,void ** this_cache)229 default_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
230                                           void **this_cache)
231 {
232   struct frame_id this_id = get_frame_id (this_frame);
233 
234   if (this_id == outer_frame_id)
235     return UNWIND_OUTERMOST;
236   else
237     return UNWIND_NO_REASON;
238 }
239 
240 /* See frame-unwind.h.  */
241 
242 CORE_ADDR
default_unwind_pc(struct gdbarch * gdbarch,const frame_info_ptr & next_frame)243 default_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
244 {
245   int pc_regnum = gdbarch_pc_regnum (gdbarch);
246   CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
247   pc = gdbarch_addr_bits_remove (gdbarch, pc);
248   return pc;
249 }
250 
251 /* See frame-unwind.h.  */
252 
253 CORE_ADDR
default_unwind_sp(struct gdbarch * gdbarch,const frame_info_ptr & next_frame)254 default_unwind_sp (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
255 {
256   int sp_regnum = gdbarch_sp_regnum (gdbarch);
257   return frame_unwind_register_unsigned (next_frame, sp_regnum);
258 }
259 
260 /* Helper functions for value-based register unwinding.  These return
261    a (possibly lazy) value of the appropriate type.  */
262 
263 /* Return a value which indicates that FRAME did not save REGNUM.  */
264 
265 struct value *
frame_unwind_got_optimized(const frame_info_ptr & frame,int regnum)266 frame_unwind_got_optimized (const frame_info_ptr &frame, int regnum)
267 {
268   struct gdbarch *gdbarch = frame_unwind_arch (frame);
269   struct type *type = register_type (gdbarch, regnum);
270 
271   return value::allocate_optimized_out (type);
272 }
273 
274 /* Return a value which indicates that FRAME copied REGNUM into
275    register NEW_REGNUM.  */
276 
277 struct value *
frame_unwind_got_register(const frame_info_ptr & frame,int regnum,int new_regnum)278 frame_unwind_got_register (const frame_info_ptr &frame,
279                                  int regnum, int new_regnum)
280 {
281   return value_of_register_lazy (get_next_frame_sentinel_okay (frame),
282                                          new_regnum);
283 }
284 
285 /* Return a value which indicates that FRAME saved REGNUM in memory at
286    ADDR.  */
287 
288 struct value *
frame_unwind_got_memory(const frame_info_ptr & frame,int regnum,CORE_ADDR addr)289 frame_unwind_got_memory (const frame_info_ptr &frame, int regnum, CORE_ADDR addr)
290 {
291   struct gdbarch *gdbarch = frame_unwind_arch (frame);
292   struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
293 
294   v->set_stack (true);
295   return v;
296 }
297 
298 /* Return a value which indicates that FRAME's saved version of
299    REGNUM has a known constant (computed) value of VAL.  */
300 
301 struct value *
frame_unwind_got_constant(const frame_info_ptr & frame,int regnum,ULONGEST val)302 frame_unwind_got_constant (const frame_info_ptr &frame, int regnum,
303                                  ULONGEST val)
304 {
305   struct gdbarch *gdbarch = frame_unwind_arch (frame);
306   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
307   struct value *reg_val;
308 
309   reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
310   store_unsigned_integer (reg_val->contents_writeable ().data (),
311                                 register_size (gdbarch, regnum), byte_order, val);
312   return reg_val;
313 }
314 
315 struct value *
frame_unwind_got_bytes(const frame_info_ptr & frame,int regnum,const gdb_byte * buf)316 frame_unwind_got_bytes (const frame_info_ptr &frame, int regnum, const gdb_byte *buf)
317 {
318   struct gdbarch *gdbarch = frame_unwind_arch (frame);
319   struct value *reg_val;
320 
321   reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
322   memcpy (reg_val->contents_raw ().data (), buf,
323             register_size (gdbarch, regnum));
324   return reg_val;
325 }
326 
327 /* Return a value which indicates that FRAME's saved version of REGNUM
328    has a known constant (computed) value of ADDR.  Convert the
329    CORE_ADDR to a target address if necessary.  */
330 
331 struct value *
frame_unwind_got_address(const frame_info_ptr & frame,int regnum,CORE_ADDR addr)332 frame_unwind_got_address (const frame_info_ptr &frame, int regnum,
333                                 CORE_ADDR addr)
334 {
335   struct gdbarch *gdbarch = frame_unwind_arch (frame);
336   struct value *reg_val;
337 
338   reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
339   pack_long (reg_val->contents_writeable ().data (),
340                register_type (gdbarch, regnum), addr);
341   return reg_val;
342 }
343 
344 /* Implement "maintenance info frame-unwinders" command.  */
345 
346 static void
maintenance_info_frame_unwinders(const char * args,int from_tty)347 maintenance_info_frame_unwinders (const char *args, int from_tty)
348 {
349   gdbarch *gdbarch = current_inferior ()->arch ();
350   struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
351 
352   ui_out *uiout = current_uiout;
353   ui_out_emit_table table_emitter (uiout, 2, -1, "FrameUnwinders");
354   uiout->table_header (27, ui_left, "name", "Name");
355   uiout->table_header (25, ui_left, "type", "Type");
356   uiout->table_body ();
357 
358   for (struct frame_unwind_table_entry *entry = table->list; entry != NULL;
359        entry = entry->next)
360     {
361       const char *name = entry->unwinder->name;
362       const char *type = frame_type_str (entry->unwinder->type);
363 
364       ui_out_emit_list tuple_emitter (uiout, nullptr);
365       uiout->field_string ("name", name);
366       uiout->field_string ("type", type);
367       uiout->text ("\n");
368     }
369 }
370 
371 void _initialize_frame_unwind ();
372 void
_initialize_frame_unwind()373 _initialize_frame_unwind ()
374 {
375   /* Add "maint info frame-unwinders".  */
376   add_cmd ("frame-unwinders",
377              class_maintenance,
378              maintenance_info_frame_unwinders,
379              _("List the frame unwinders currently in effect, "
380                "starting with the highest priority."),
381              &maintenanceinfolist);
382 }
383