1*5796c8dcSSimon Schubert /* Code dealing with dummy stack frames, for GDB, the GNU debugger. 2*5796c8dcSSimon Schubert 3*5796c8dcSSimon Schubert Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 4*5796c8dcSSimon Schubert 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009 5*5796c8dcSSimon Schubert Free Software Foundation, Inc. 6*5796c8dcSSimon Schubert 7*5796c8dcSSimon Schubert This file is part of GDB. 8*5796c8dcSSimon Schubert 9*5796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 10*5796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 11*5796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 12*5796c8dcSSimon Schubert (at your option) any later version. 13*5796c8dcSSimon Schubert 14*5796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 15*5796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 16*5796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17*5796c8dcSSimon Schubert GNU General Public License for more details. 18*5796c8dcSSimon Schubert 19*5796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 20*5796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21*5796c8dcSSimon Schubert 22*5796c8dcSSimon Schubert 23*5796c8dcSSimon Schubert #include "defs.h" 24*5796c8dcSSimon Schubert #include "dummy-frame.h" 25*5796c8dcSSimon Schubert #include "regcache.h" 26*5796c8dcSSimon Schubert #include "frame.h" 27*5796c8dcSSimon Schubert #include "inferior.h" 28*5796c8dcSSimon Schubert #include "gdb_assert.h" 29*5796c8dcSSimon Schubert #include "frame-unwind.h" 30*5796c8dcSSimon Schubert #include "command.h" 31*5796c8dcSSimon Schubert #include "gdbcmd.h" 32*5796c8dcSSimon Schubert #include "gdb_string.h" 33*5796c8dcSSimon Schubert #include "observer.h" 34*5796c8dcSSimon Schubert 35*5796c8dcSSimon Schubert /* Dummy frame. This saves the processor state just prior to setting 36*5796c8dcSSimon Schubert up the inferior function call. Older targets save the registers 37*5796c8dcSSimon Schubert on the target stack (but that really slows down function calls). */ 38*5796c8dcSSimon Schubert 39*5796c8dcSSimon Schubert struct dummy_frame 40*5796c8dcSSimon Schubert { 41*5796c8dcSSimon Schubert struct dummy_frame *next; 42*5796c8dcSSimon Schubert /* This frame's ID. Must match the value returned by 43*5796c8dcSSimon Schubert gdbarch_dummy_id. */ 44*5796c8dcSSimon Schubert struct frame_id id; 45*5796c8dcSSimon Schubert /* The caller's state prior to the call. */ 46*5796c8dcSSimon Schubert struct inferior_thread_state *caller_state; 47*5796c8dcSSimon Schubert }; 48*5796c8dcSSimon Schubert 49*5796c8dcSSimon Schubert static struct dummy_frame *dummy_frame_stack = NULL; 50*5796c8dcSSimon Schubert 51*5796c8dcSSimon Schubert /* Function: deprecated_pc_in_call_dummy (pc) 52*5796c8dcSSimon Schubert 53*5796c8dcSSimon Schubert Return non-zero if the PC falls in a dummy frame created by gdb for 54*5796c8dcSSimon Schubert an inferior call. The code below which allows gdbarch_decr_pc_after_break 55*5796c8dcSSimon Schubert is for infrun.c, which may give the function a PC without that 56*5796c8dcSSimon Schubert subtracted out. 57*5796c8dcSSimon Schubert 58*5796c8dcSSimon Schubert FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can 59*5796c8dcSSimon Schubert figure out what the real PC (as in the resume address) is BEFORE 60*5796c8dcSSimon Schubert calling this function. 61*5796c8dcSSimon Schubert 62*5796c8dcSSimon Schubert NOTE: cagney/2004-08-02: I'm pretty sure that, with the introduction of 63*5796c8dcSSimon Schubert infrun.c:adjust_pc_after_break (thanks), this function is now 64*5796c8dcSSimon Schubert always called with a correctly adjusted PC! 65*5796c8dcSSimon Schubert 66*5796c8dcSSimon Schubert NOTE: cagney/2004-08-02: Code should not need to call this. */ 67*5796c8dcSSimon Schubert 68*5796c8dcSSimon Schubert int 69*5796c8dcSSimon Schubert deprecated_pc_in_call_dummy (struct gdbarch *gdbarch, CORE_ADDR pc) 70*5796c8dcSSimon Schubert { 71*5796c8dcSSimon Schubert struct dummy_frame *dummyframe; 72*5796c8dcSSimon Schubert for (dummyframe = dummy_frame_stack; 73*5796c8dcSSimon Schubert dummyframe != NULL; 74*5796c8dcSSimon Schubert dummyframe = dummyframe->next) 75*5796c8dcSSimon Schubert { 76*5796c8dcSSimon Schubert if ((pc >= dummyframe->id.code_addr) 77*5796c8dcSSimon Schubert && (pc <= dummyframe->id.code_addr 78*5796c8dcSSimon Schubert + gdbarch_decr_pc_after_break (gdbarch))) 79*5796c8dcSSimon Schubert return 1; 80*5796c8dcSSimon Schubert } 81*5796c8dcSSimon Schubert return 0; 82*5796c8dcSSimon Schubert } 83*5796c8dcSSimon Schubert 84*5796c8dcSSimon Schubert /* Push the caller's state, along with the dummy frame info, onto the 85*5796c8dcSSimon Schubert dummy-frame stack. */ 86*5796c8dcSSimon Schubert 87*5796c8dcSSimon Schubert void 88*5796c8dcSSimon Schubert dummy_frame_push (struct inferior_thread_state *caller_state, 89*5796c8dcSSimon Schubert const struct frame_id *dummy_id) 90*5796c8dcSSimon Schubert { 91*5796c8dcSSimon Schubert struct dummy_frame *dummy_frame; 92*5796c8dcSSimon Schubert 93*5796c8dcSSimon Schubert dummy_frame = XZALLOC (struct dummy_frame); 94*5796c8dcSSimon Schubert dummy_frame->caller_state = caller_state; 95*5796c8dcSSimon Schubert dummy_frame->id = (*dummy_id); 96*5796c8dcSSimon Schubert dummy_frame->next = dummy_frame_stack; 97*5796c8dcSSimon Schubert dummy_frame_stack = dummy_frame; 98*5796c8dcSSimon Schubert } 99*5796c8dcSSimon Schubert 100*5796c8dcSSimon Schubert /* Remove *DUMMY_PTR from the dummy frame stack. */ 101*5796c8dcSSimon Schubert 102*5796c8dcSSimon Schubert static void 103*5796c8dcSSimon Schubert remove_dummy_frame (struct dummy_frame **dummy_ptr) 104*5796c8dcSSimon Schubert { 105*5796c8dcSSimon Schubert struct dummy_frame *dummy = *dummy_ptr; 106*5796c8dcSSimon Schubert 107*5796c8dcSSimon Schubert *dummy_ptr = dummy->next; 108*5796c8dcSSimon Schubert discard_inferior_thread_state (dummy->caller_state); 109*5796c8dcSSimon Schubert xfree (dummy); 110*5796c8dcSSimon Schubert } 111*5796c8dcSSimon Schubert 112*5796c8dcSSimon Schubert /* Pop *DUMMY_PTR, restoring program state to that before the 113*5796c8dcSSimon Schubert frame was created. */ 114*5796c8dcSSimon Schubert 115*5796c8dcSSimon Schubert static void 116*5796c8dcSSimon Schubert pop_dummy_frame (struct dummy_frame **dummy_ptr) 117*5796c8dcSSimon Schubert { 118*5796c8dcSSimon Schubert struct dummy_frame *dummy; 119*5796c8dcSSimon Schubert 120*5796c8dcSSimon Schubert restore_inferior_thread_state ((*dummy_ptr)->caller_state); 121*5796c8dcSSimon Schubert 122*5796c8dcSSimon Schubert /* restore_inferior_status frees inf_state, 123*5796c8dcSSimon Schubert all that remains is to pop *dummy_ptr */ 124*5796c8dcSSimon Schubert dummy = *dummy_ptr; 125*5796c8dcSSimon Schubert *dummy_ptr = dummy->next; 126*5796c8dcSSimon Schubert xfree (dummy); 127*5796c8dcSSimon Schubert 128*5796c8dcSSimon Schubert /* We've made right mess of GDB's local state, just discard 129*5796c8dcSSimon Schubert everything. */ 130*5796c8dcSSimon Schubert reinit_frame_cache (); 131*5796c8dcSSimon Schubert } 132*5796c8dcSSimon Schubert 133*5796c8dcSSimon Schubert /* Look up DUMMY_ID. 134*5796c8dcSSimon Schubert Return NULL if not found. */ 135*5796c8dcSSimon Schubert 136*5796c8dcSSimon Schubert static struct dummy_frame ** 137*5796c8dcSSimon Schubert lookup_dummy_frame (struct frame_id dummy_id) 138*5796c8dcSSimon Schubert { 139*5796c8dcSSimon Schubert struct dummy_frame **dp; 140*5796c8dcSSimon Schubert 141*5796c8dcSSimon Schubert for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next) 142*5796c8dcSSimon Schubert { 143*5796c8dcSSimon Schubert if (frame_id_eq ((*dp)->id, dummy_id)) 144*5796c8dcSSimon Schubert return dp; 145*5796c8dcSSimon Schubert } 146*5796c8dcSSimon Schubert 147*5796c8dcSSimon Schubert return NULL; 148*5796c8dcSSimon Schubert } 149*5796c8dcSSimon Schubert 150*5796c8dcSSimon Schubert /* Pop the dummy frame DUMMY_ID, restoring program state to that before the 151*5796c8dcSSimon Schubert frame was created. 152*5796c8dcSSimon Schubert On return reinit_frame_cache has been called. 153*5796c8dcSSimon Schubert If the frame isn't found, flag an internal error. 154*5796c8dcSSimon Schubert 155*5796c8dcSSimon Schubert NOTE: This can only pop the one frame, even if it is in the middle of the 156*5796c8dcSSimon Schubert stack, because the other frames may be for different threads, and there's 157*5796c8dcSSimon Schubert currently no way to tell which stack frame is for which thread. */ 158*5796c8dcSSimon Schubert 159*5796c8dcSSimon Schubert void 160*5796c8dcSSimon Schubert dummy_frame_pop (struct frame_id dummy_id) 161*5796c8dcSSimon Schubert { 162*5796c8dcSSimon Schubert struct dummy_frame **dp; 163*5796c8dcSSimon Schubert 164*5796c8dcSSimon Schubert dp = lookup_dummy_frame (dummy_id); 165*5796c8dcSSimon Schubert gdb_assert (dp != NULL); 166*5796c8dcSSimon Schubert 167*5796c8dcSSimon Schubert pop_dummy_frame (dp); 168*5796c8dcSSimon Schubert } 169*5796c8dcSSimon Schubert 170*5796c8dcSSimon Schubert /* There may be stale dummy frames, perhaps left over from when a longjump took 171*5796c8dcSSimon Schubert us out of a function that was called by the debugger. Clean them up at 172*5796c8dcSSimon Schubert least once whenever we start a new inferior. */ 173*5796c8dcSSimon Schubert 174*5796c8dcSSimon Schubert static void 175*5796c8dcSSimon Schubert cleanup_dummy_frames (struct target_ops *target, int from_tty) 176*5796c8dcSSimon Schubert { 177*5796c8dcSSimon Schubert while (dummy_frame_stack != NULL) 178*5796c8dcSSimon Schubert remove_dummy_frame (&dummy_frame_stack); 179*5796c8dcSSimon Schubert } 180*5796c8dcSSimon Schubert 181*5796c8dcSSimon Schubert /* Return the dummy frame cache, it contains both the ID, and a 182*5796c8dcSSimon Schubert pointer to the regcache. */ 183*5796c8dcSSimon Schubert struct dummy_frame_cache 184*5796c8dcSSimon Schubert { 185*5796c8dcSSimon Schubert struct frame_id this_id; 186*5796c8dcSSimon Schubert struct regcache *prev_regcache; 187*5796c8dcSSimon Schubert }; 188*5796c8dcSSimon Schubert 189*5796c8dcSSimon Schubert static int 190*5796c8dcSSimon Schubert dummy_frame_sniffer (const struct frame_unwind *self, 191*5796c8dcSSimon Schubert struct frame_info *this_frame, 192*5796c8dcSSimon Schubert void **this_prologue_cache) 193*5796c8dcSSimon Schubert { 194*5796c8dcSSimon Schubert struct dummy_frame *dummyframe; 195*5796c8dcSSimon Schubert struct frame_id this_id; 196*5796c8dcSSimon Schubert 197*5796c8dcSSimon Schubert /* When unwinding a normal frame, the stack structure is determined 198*5796c8dcSSimon Schubert by analyzing the frame's function's code (be it using brute force 199*5796c8dcSSimon Schubert prologue analysis, or the dwarf2 CFI). In the case of a dummy 200*5796c8dcSSimon Schubert frame, that simply isn't possible. The PC is either the program 201*5796c8dcSSimon Schubert entry point, or some random address on the stack. Trying to use 202*5796c8dcSSimon Schubert that PC to apply standard frame ID unwind techniques is just 203*5796c8dcSSimon Schubert asking for trouble. */ 204*5796c8dcSSimon Schubert 205*5796c8dcSSimon Schubert /* Don't bother unless there is at least one dummy frame. */ 206*5796c8dcSSimon Schubert if (dummy_frame_stack != NULL) 207*5796c8dcSSimon Schubert { 208*5796c8dcSSimon Schubert /* Use an architecture specific method to extract this frame's 209*5796c8dcSSimon Schubert dummy ID, assuming it is a dummy frame. */ 210*5796c8dcSSimon Schubert this_id = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame); 211*5796c8dcSSimon Schubert 212*5796c8dcSSimon Schubert /* Use that ID to find the corresponding cache entry. */ 213*5796c8dcSSimon Schubert for (dummyframe = dummy_frame_stack; 214*5796c8dcSSimon Schubert dummyframe != NULL; 215*5796c8dcSSimon Schubert dummyframe = dummyframe->next) 216*5796c8dcSSimon Schubert { 217*5796c8dcSSimon Schubert if (frame_id_eq (dummyframe->id, this_id)) 218*5796c8dcSSimon Schubert { 219*5796c8dcSSimon Schubert struct dummy_frame_cache *cache; 220*5796c8dcSSimon Schubert cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache); 221*5796c8dcSSimon Schubert cache->prev_regcache = get_inferior_thread_state_regcache (dummyframe->caller_state); 222*5796c8dcSSimon Schubert cache->this_id = this_id; 223*5796c8dcSSimon Schubert (*this_prologue_cache) = cache; 224*5796c8dcSSimon Schubert return 1; 225*5796c8dcSSimon Schubert } 226*5796c8dcSSimon Schubert } 227*5796c8dcSSimon Schubert } 228*5796c8dcSSimon Schubert return 0; 229*5796c8dcSSimon Schubert } 230*5796c8dcSSimon Schubert 231*5796c8dcSSimon Schubert /* Given a call-dummy dummy-frame, return the registers. Here the 232*5796c8dcSSimon Schubert register value is taken from the local copy of the register buffer. */ 233*5796c8dcSSimon Schubert 234*5796c8dcSSimon Schubert static struct value * 235*5796c8dcSSimon Schubert dummy_frame_prev_register (struct frame_info *this_frame, 236*5796c8dcSSimon Schubert void **this_prologue_cache, 237*5796c8dcSSimon Schubert int regnum) 238*5796c8dcSSimon Schubert { 239*5796c8dcSSimon Schubert struct dummy_frame_cache *cache = (*this_prologue_cache); 240*5796c8dcSSimon Schubert struct gdbarch *gdbarch = get_frame_arch (this_frame); 241*5796c8dcSSimon Schubert struct value *reg_val; 242*5796c8dcSSimon Schubert 243*5796c8dcSSimon Schubert /* The dummy-frame sniffer always fills in the cache. */ 244*5796c8dcSSimon Schubert gdb_assert (cache != NULL); 245*5796c8dcSSimon Schubert 246*5796c8dcSSimon Schubert /* Describe the register's location. Generic dummy frames always 247*5796c8dcSSimon Schubert have the register value in an ``expression''. */ 248*5796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 249*5796c8dcSSimon Schubert 250*5796c8dcSSimon Schubert /* Use the regcache_cooked_read() method so that it, on the fly, 251*5796c8dcSSimon Schubert constructs either a raw or pseudo register from the raw 252*5796c8dcSSimon Schubert register cache. */ 253*5796c8dcSSimon Schubert regcache_cooked_read (cache->prev_regcache, regnum, 254*5796c8dcSSimon Schubert value_contents_writeable (reg_val)); 255*5796c8dcSSimon Schubert return reg_val; 256*5796c8dcSSimon Schubert } 257*5796c8dcSSimon Schubert 258*5796c8dcSSimon Schubert /* Assuming that THIS_FRAME is a dummy, return its ID. That ID is 259*5796c8dcSSimon Schubert determined by examining the NEXT frame's unwound registers using 260*5796c8dcSSimon Schubert the method dummy_id(). As a side effect, THIS dummy frame's 261*5796c8dcSSimon Schubert dummy cache is located and and saved in THIS_PROLOGUE_CACHE. */ 262*5796c8dcSSimon Schubert 263*5796c8dcSSimon Schubert static void 264*5796c8dcSSimon Schubert dummy_frame_this_id (struct frame_info *this_frame, 265*5796c8dcSSimon Schubert void **this_prologue_cache, 266*5796c8dcSSimon Schubert struct frame_id *this_id) 267*5796c8dcSSimon Schubert { 268*5796c8dcSSimon Schubert /* The dummy-frame sniffer always fills in the cache. */ 269*5796c8dcSSimon Schubert struct dummy_frame_cache *cache = (*this_prologue_cache); 270*5796c8dcSSimon Schubert gdb_assert (cache != NULL); 271*5796c8dcSSimon Schubert (*this_id) = cache->this_id; 272*5796c8dcSSimon Schubert } 273*5796c8dcSSimon Schubert 274*5796c8dcSSimon Schubert static const struct frame_unwind dummy_frame_unwinder = 275*5796c8dcSSimon Schubert { 276*5796c8dcSSimon Schubert DUMMY_FRAME, 277*5796c8dcSSimon Schubert dummy_frame_this_id, 278*5796c8dcSSimon Schubert dummy_frame_prev_register, 279*5796c8dcSSimon Schubert NULL, 280*5796c8dcSSimon Schubert dummy_frame_sniffer, 281*5796c8dcSSimon Schubert }; 282*5796c8dcSSimon Schubert 283*5796c8dcSSimon Schubert const struct frame_unwind *const dummy_frame_unwind = { 284*5796c8dcSSimon Schubert &dummy_frame_unwinder 285*5796c8dcSSimon Schubert }; 286*5796c8dcSSimon Schubert 287*5796c8dcSSimon Schubert static void 288*5796c8dcSSimon Schubert fprint_dummy_frames (struct ui_file *file) 289*5796c8dcSSimon Schubert { 290*5796c8dcSSimon Schubert struct dummy_frame *s; 291*5796c8dcSSimon Schubert for (s = dummy_frame_stack; s != NULL; s = s->next) 292*5796c8dcSSimon Schubert { 293*5796c8dcSSimon Schubert gdb_print_host_address (s, file); 294*5796c8dcSSimon Schubert fprintf_unfiltered (file, ":"); 295*5796c8dcSSimon Schubert fprintf_unfiltered (file, " id="); 296*5796c8dcSSimon Schubert fprint_frame_id (file, s->id); 297*5796c8dcSSimon Schubert fprintf_unfiltered (file, "\n"); 298*5796c8dcSSimon Schubert } 299*5796c8dcSSimon Schubert } 300*5796c8dcSSimon Schubert 301*5796c8dcSSimon Schubert static void 302*5796c8dcSSimon Schubert maintenance_print_dummy_frames (char *args, int from_tty) 303*5796c8dcSSimon Schubert { 304*5796c8dcSSimon Schubert if (args == NULL) 305*5796c8dcSSimon Schubert fprint_dummy_frames (gdb_stdout); 306*5796c8dcSSimon Schubert else 307*5796c8dcSSimon Schubert { 308*5796c8dcSSimon Schubert struct cleanup *cleanups; 309*5796c8dcSSimon Schubert struct ui_file *file = gdb_fopen (args, "w"); 310*5796c8dcSSimon Schubert if (file == NULL) 311*5796c8dcSSimon Schubert perror_with_name (_("maintenance print dummy-frames")); 312*5796c8dcSSimon Schubert cleanups = make_cleanup_ui_file_delete (file); 313*5796c8dcSSimon Schubert fprint_dummy_frames (file); 314*5796c8dcSSimon Schubert do_cleanups (cleanups); 315*5796c8dcSSimon Schubert } 316*5796c8dcSSimon Schubert } 317*5796c8dcSSimon Schubert 318*5796c8dcSSimon Schubert extern void _initialize_dummy_frame (void); 319*5796c8dcSSimon Schubert 320*5796c8dcSSimon Schubert void 321*5796c8dcSSimon Schubert _initialize_dummy_frame (void) 322*5796c8dcSSimon Schubert { 323*5796c8dcSSimon Schubert add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames, 324*5796c8dcSSimon Schubert _("Print the contents of the internal dummy-frame stack."), 325*5796c8dcSSimon Schubert &maintenanceprintlist); 326*5796c8dcSSimon Schubert 327*5796c8dcSSimon Schubert observer_attach_inferior_created (cleanup_dummy_frames); 328*5796c8dcSSimon Schubert } 329