1 /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-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 
21 #include "dummy-frame.h"
22 #include "regcache.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "frame-unwind.h"
26 #include "command.h"
27 #include "cli/cli-cmds.h"
28 #include "observable.h"
29 #include "gdbthread.h"
30 #include "infcall.h"
31 #include "gdbarch.h"
32 
33 struct dummy_frame_id
34 {
35   /* This frame's ID.  Must match the value returned by
36      gdbarch_dummy_id.  */
37   struct frame_id id;
38 
39   /* The thread this dummy_frame relates to.  */
40   thread_info *thread;
41 };
42 
43 /* Return whether dummy_frame_id *ID1 and *ID2 are equal.  */
44 
45 static int
dummy_frame_id_eq(struct dummy_frame_id * id1,struct dummy_frame_id * id2)46 dummy_frame_id_eq (struct dummy_frame_id *id1,
47                        struct dummy_frame_id *id2)
48 {
49   return id1->id == id2->id && id1->thread == id2->thread;
50 }
51 
52 /* List of dummy_frame destructors.  */
53 
54 struct dummy_frame_dtor_list
55 {
56   /* Next element in the list or NULL if this is the last element.  */
57   struct dummy_frame_dtor_list *next;
58 
59   /* If non-NULL, a destructor that is run when this dummy frame is freed.  */
60   dummy_frame_dtor_ftype *dtor;
61 
62   /* Arbitrary data that is passed to DTOR.  */
63   void *dtor_data;
64 };
65 
66 /* Dummy frame.  This saves the processor state just prior to setting
67    up the inferior function call.  Older targets save the registers
68    on the target stack (but that really slows down function calls).  */
69 
70 struct dummy_frame
71 {
72   struct dummy_frame *next;
73 
74   /* An id represents a dummy frame.  */
75   struct dummy_frame_id id;
76 
77   /* The caller's state prior to the call.  */
78   struct infcall_suspend_state *caller_state;
79 
80   /* First element of destructors list or NULL if there are no
81      destructors registered for this dummy_frame.  */
82   struct dummy_frame_dtor_list *dtor_list;
83 };
84 
85 static struct dummy_frame *dummy_frame_stack = NULL;
86 
87 /* Push the caller's state, along with the dummy frame info, onto the
88    dummy-frame stack.  */
89 
90 void
dummy_frame_push(struct infcall_suspend_state * caller_state,const frame_id * dummy_id,thread_info * thread)91 dummy_frame_push (struct infcall_suspend_state *caller_state,
92                       const frame_id *dummy_id, thread_info *thread)
93 {
94   struct dummy_frame *dummy_frame;
95 
96   dummy_frame = XCNEW (struct dummy_frame);
97   dummy_frame->caller_state = caller_state;
98   dummy_frame->id.id = (*dummy_id);
99   dummy_frame->id.thread = thread;
100   dummy_frame->next = dummy_frame_stack;
101   dummy_frame_stack = dummy_frame;
102 }
103 
104 /* Remove *DUMMY_PTR from the dummy frame stack.  */
105 
106 static void
remove_dummy_frame(struct dummy_frame ** dummy_ptr)107 remove_dummy_frame (struct dummy_frame **dummy_ptr)
108 {
109   struct dummy_frame *dummy = *dummy_ptr;
110 
111   while (dummy->dtor_list != NULL)
112     {
113       struct dummy_frame_dtor_list *list = dummy->dtor_list;
114 
115       dummy->dtor_list = list->next;
116       list->dtor (list->dtor_data, 0);
117       xfree (list);
118     }
119 
120   *dummy_ptr = dummy->next;
121   discard_infcall_suspend_state (dummy->caller_state);
122   xfree (dummy);
123 }
124 
125 /* Delete any breakpoint B which is a momentary breakpoint for return from
126    inferior call matching DUMMY_VOIDP.  */
127 
128 static bool
pop_dummy_frame_bpt(struct breakpoint * b,struct dummy_frame * dummy)129 pop_dummy_frame_bpt (struct breakpoint *b, struct dummy_frame *dummy)
130 {
131   if (b->thread == dummy->id.thread->global_num
132       && b->disposition == disp_del && b->frame_id == dummy->id.id)
133     {
134       while (b->related_breakpoint != b)
135           delete_breakpoint (b->related_breakpoint);
136 
137       delete_breakpoint (b);
138 
139       /* Stop the traversal.  */
140       return true;
141     }
142 
143   /* Continue the traversal.  */
144   return false;
145 }
146 
147 /* Pop *DUMMY_PTR, restoring program state to that before the
148    frame was created.  */
149 
150 static void
pop_dummy_frame(struct dummy_frame ** dummy_ptr)151 pop_dummy_frame (struct dummy_frame **dummy_ptr)
152 {
153   struct dummy_frame *dummy = *dummy_ptr;
154 
155   gdb_assert (dummy->id.thread == inferior_thread ());
156 
157   while (dummy->dtor_list != NULL)
158     {
159       struct dummy_frame_dtor_list *list = dummy->dtor_list;
160 
161       dummy->dtor_list = list->next;
162       list->dtor (list->dtor_data, 1);
163       xfree (list);
164     }
165 
166   restore_infcall_suspend_state (dummy->caller_state);
167 
168   for (breakpoint &bp : all_breakpoints_safe ())
169     if (pop_dummy_frame_bpt (&bp, dummy))
170       break;
171 
172   /* restore_infcall_control_state frees inf_state,
173      all that remains is to pop *dummy_ptr.  */
174   *dummy_ptr = dummy->next;
175   xfree (dummy);
176 
177   /* We've made right mess of GDB's local state, just discard
178      everything.  */
179   reinit_frame_cache ();
180 }
181 
182 /* Look up DUMMY_ID.
183    Return NULL if not found.  */
184 
185 static struct dummy_frame **
lookup_dummy_frame(struct dummy_frame_id * dummy_id)186 lookup_dummy_frame (struct dummy_frame_id *dummy_id)
187 {
188   struct dummy_frame **dp;
189 
190   for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
191     {
192       if (dummy_frame_id_eq (&(*dp)->id, dummy_id))
193           return dp;
194     }
195 
196   return NULL;
197 }
198 
199 /* Find the dummy frame by DUMMY_ID and THREAD, and pop it, restoring
200    program state to that before the frame was created.
201    On return reinit_frame_cache has been called.
202    If the frame isn't found, flag an internal error.  */
203 
204 void
dummy_frame_pop(frame_id dummy_id,thread_info * thread)205 dummy_frame_pop (frame_id dummy_id, thread_info *thread)
206 {
207   struct dummy_frame **dp;
208   struct dummy_frame_id id = { dummy_id, thread };
209 
210   dp = lookup_dummy_frame (&id);
211   gdb_assert (dp != NULL);
212 
213   pop_dummy_frame (dp);
214 }
215 
216 /* Find the dummy frame by DUMMY_ID and PTID and drop it.  Do nothing
217    if it is not found.  Do not restore its state into inferior, just
218    free its memory.  */
219 
220 void
dummy_frame_discard(struct frame_id dummy_id,thread_info * thread)221 dummy_frame_discard (struct frame_id dummy_id, thread_info *thread)
222 {
223   struct dummy_frame **dp;
224   struct dummy_frame_id id = { dummy_id, thread };
225 
226   dp = lookup_dummy_frame (&id);
227   if (dp)
228     remove_dummy_frame (dp);
229 }
230 
231 /* See dummy-frame.h.  */
232 
233 void
register_dummy_frame_dtor(frame_id dummy_id,thread_info * thread,dummy_frame_dtor_ftype * dtor,void * dtor_data)234 register_dummy_frame_dtor (frame_id dummy_id, thread_info *thread,
235                                  dummy_frame_dtor_ftype *dtor, void *dtor_data)
236 {
237   struct dummy_frame_id id = { dummy_id, thread };
238   struct dummy_frame **dp, *d;
239   struct dummy_frame_dtor_list *list;
240 
241   dp = lookup_dummy_frame (&id);
242   gdb_assert (dp != NULL);
243   d = *dp;
244   list = XNEW (struct dummy_frame_dtor_list);
245   list->next = d->dtor_list;
246   d->dtor_list = list;
247   list->dtor = dtor;
248   list->dtor_data = dtor_data;
249 }
250 
251 /* See dummy-frame.h.  */
252 
253 int
find_dummy_frame_dtor(dummy_frame_dtor_ftype * dtor,void * dtor_data)254 find_dummy_frame_dtor (dummy_frame_dtor_ftype *dtor, void *dtor_data)
255 {
256   struct dummy_frame *d;
257 
258   for (d = dummy_frame_stack; d != NULL; d = d->next)
259     {
260       struct dummy_frame_dtor_list *list;
261 
262       for (list = d->dtor_list; list != NULL; list = list->next)
263           if (list->dtor == dtor && list->dtor_data == dtor_data)
264             return 1;
265     }
266   return 0;
267 }
268 
269 /* There may be stale dummy frames, perhaps left over from when an uncaught
270    longjmp took us out of a function that was called by the debugger.  Clean
271    them up at least once whenever we start a new inferior.  */
272 
273 static void
cleanup_dummy_frames(inferior * inf)274 cleanup_dummy_frames (inferior *inf)
275 {
276   while (dummy_frame_stack != NULL)
277     remove_dummy_frame (&dummy_frame_stack);
278 }
279 
280 /* Return the dummy frame cache, it contains both the ID, and a
281    pointer to the regcache.  */
282 struct dummy_frame_cache
283 {
284   struct frame_id this_id;
285   readonly_detached_regcache *prev_regcache;
286 };
287 
288 static int
dummy_frame_sniffer(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_prologue_cache)289 dummy_frame_sniffer (const struct frame_unwind *self,
290                          const frame_info_ptr &this_frame,
291                          void **this_prologue_cache)
292 {
293   /* When unwinding a normal frame, the stack structure is determined
294      by analyzing the frame's function's code (be it using brute force
295      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
296      frame, that simply isn't possible.  The PC is either the program
297      entry point, or some random address on the stack.  Trying to use
298      that PC to apply standard frame ID unwind techniques is just
299      asking for trouble.  */
300 
301   /* Don't bother unless there is at least one dummy frame.  */
302   if (dummy_frame_stack != NULL)
303     {
304       struct dummy_frame *dummyframe;
305       /* Use an architecture specific method to extract this frame's
306            dummy ID, assuming it is a dummy frame.  */
307       struct frame_id this_id
308           = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
309       struct dummy_frame_id dummy_id = { this_id, inferior_thread () };
310 
311       /* Use that ID to find the corresponding cache entry.  */
312       for (dummyframe = dummy_frame_stack;
313              dummyframe != NULL;
314              dummyframe = dummyframe->next)
315           {
316             if (dummy_frame_id_eq (&dummyframe->id, &dummy_id))
317               {
318                 struct dummy_frame_cache *cache;
319 
320                 cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
321                 cache->prev_regcache = get_infcall_suspend_state_regcache
322                                                                (dummyframe->caller_state);
323                 cache->this_id = this_id;
324                 (*this_prologue_cache) = cache;
325                 return 1;
326               }
327           }
328     }
329   return 0;
330 }
331 
332 /* Given a call-dummy dummy-frame, return the registers.  Here the
333    register value is taken from the local copy of the register buffer.  */
334 
335 static struct value *
dummy_frame_prev_register(const frame_info_ptr & this_frame,void ** this_prologue_cache,int regnum)336 dummy_frame_prev_register (const frame_info_ptr &this_frame,
337                                  void **this_prologue_cache,
338                                  int regnum)
339 {
340   struct dummy_frame_cache *cache
341     = (struct dummy_frame_cache *) *this_prologue_cache;
342   struct gdbarch *gdbarch = get_frame_arch (this_frame);
343   struct value *reg_val;
344 
345   /* The dummy-frame sniffer always fills in the cache.  */
346   gdb_assert (cache != NULL);
347 
348   /* Describe the register's location.  Generic dummy frames always
349      have the register value in an ``expression''.  */
350   reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
351 
352   /* Use the regcache_cooked_read() method so that it, on the fly,
353      constructs either a raw or pseudo register from the raw
354      register cache.  */
355   cache->prev_regcache->cooked_read
356     (regnum, reg_val->contents_writeable ().data ());
357   return reg_val;
358 }
359 
360 /* Assuming that THIS_FRAME is a dummy, return its ID.  That ID is
361    determined by examining the NEXT frame's unwound registers using
362    the method dummy_id().  As a side effect, THIS dummy frame's
363    dummy cache is located and saved in THIS_PROLOGUE_CACHE.  */
364 
365 static void
dummy_frame_this_id(const frame_info_ptr & this_frame,void ** this_prologue_cache,struct frame_id * this_id)366 dummy_frame_this_id (const frame_info_ptr &this_frame,
367                          void **this_prologue_cache,
368                          struct frame_id *this_id)
369 {
370   /* The dummy-frame sniffer always fills in the cache.  */
371   struct dummy_frame_cache *cache
372     = (struct dummy_frame_cache *) *this_prologue_cache;
373 
374   gdb_assert (cache != NULL);
375   (*this_id) = cache->this_id;
376 }
377 
378 const struct frame_unwind dummy_frame_unwind =
379 {
380   "dummy",
381   DUMMY_FRAME,
382   default_frame_unwind_stop_reason,
383   dummy_frame_this_id,
384   dummy_frame_prev_register,
385   NULL,
386   dummy_frame_sniffer,
387 };
388 
389 /* See dummy-frame.h.  */
390 
391 struct frame_id
default_dummy_id(struct gdbarch * gdbarch,const frame_info_ptr & this_frame)392 default_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
393 {
394   CORE_ADDR sp, pc;
395 
396   sp = get_frame_sp (this_frame);
397   pc = get_frame_pc (this_frame);
398   return frame_id_build (sp, pc);
399 }
400 
401 static void
fprint_dummy_frames(struct ui_file * file)402 fprint_dummy_frames (struct ui_file *file)
403 {
404   struct dummy_frame *s;
405 
406   for (s = dummy_frame_stack; s != NULL; s = s->next)
407     gdb_printf (file, "%s: id=%s, ptid=%s\n",
408                     host_address_to_string (s),
409                     s->id.id.to_string ().c_str (),
410                     s->id.thread->ptid.to_string ().c_str ());
411 }
412 
413 static void
maintenance_print_dummy_frames(const char * args,int from_tty)414 maintenance_print_dummy_frames (const char *args, int from_tty)
415 {
416   if (args == NULL)
417     fprint_dummy_frames (gdb_stdout);
418   else
419     {
420       stdio_file file;
421 
422       if (!file.open (args, "w"))
423           perror_with_name (_("maintenance print dummy-frames"));
424       fprint_dummy_frames (&file);
425     }
426 }
427 
428 void _initialize_dummy_frame ();
429 void
_initialize_dummy_frame()430 _initialize_dummy_frame ()
431 {
432   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
433              _("Print the contents of the internal dummy-frame stack."),
434              &maintenanceprintlist);
435 
436   gdb::observers::inferior_created.attach (cleanup_dummy_frames, "dummy-frame");
437 }
438