15796c8dcSSimon Schubert /* Code dealing with dummy stack frames, for GDB, the GNU debugger. 25796c8dcSSimon Schubert 3*a45ae5f8SJohn Marino Copyright (C) 1986-2004, 2007-2012 Free Software Foundation, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "dummy-frame.h" 235796c8dcSSimon Schubert #include "regcache.h" 245796c8dcSSimon Schubert #include "frame.h" 255796c8dcSSimon Schubert #include "inferior.h" 265796c8dcSSimon Schubert #include "gdb_assert.h" 275796c8dcSSimon Schubert #include "frame-unwind.h" 285796c8dcSSimon Schubert #include "command.h" 295796c8dcSSimon Schubert #include "gdbcmd.h" 305796c8dcSSimon Schubert #include "gdb_string.h" 315796c8dcSSimon Schubert #include "observer.h" 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert /* Dummy frame. This saves the processor state just prior to setting 345796c8dcSSimon Schubert up the inferior function call. Older targets save the registers 355796c8dcSSimon Schubert on the target stack (but that really slows down function calls). */ 365796c8dcSSimon Schubert 375796c8dcSSimon Schubert struct dummy_frame 385796c8dcSSimon Schubert { 395796c8dcSSimon Schubert struct dummy_frame *next; 405796c8dcSSimon Schubert /* This frame's ID. Must match the value returned by 415796c8dcSSimon Schubert gdbarch_dummy_id. */ 425796c8dcSSimon Schubert struct frame_id id; 435796c8dcSSimon Schubert /* The caller's state prior to the call. */ 44c50c785cSJohn Marino struct infcall_suspend_state *caller_state; 455796c8dcSSimon Schubert }; 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert static struct dummy_frame *dummy_frame_stack = NULL; 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert /* Function: deprecated_pc_in_call_dummy (pc) 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert Return non-zero if the PC falls in a dummy frame created by gdb for 525796c8dcSSimon Schubert an inferior call. The code below which allows gdbarch_decr_pc_after_break 535796c8dcSSimon Schubert is for infrun.c, which may give the function a PC without that 545796c8dcSSimon Schubert subtracted out. 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can 575796c8dcSSimon Schubert figure out what the real PC (as in the resume address) is BEFORE 585796c8dcSSimon Schubert calling this function. 595796c8dcSSimon Schubert 605796c8dcSSimon Schubert NOTE: cagney/2004-08-02: I'm pretty sure that, with the introduction of 615796c8dcSSimon Schubert infrun.c:adjust_pc_after_break (thanks), this function is now 625796c8dcSSimon Schubert always called with a correctly adjusted PC! 635796c8dcSSimon Schubert 645796c8dcSSimon Schubert NOTE: cagney/2004-08-02: Code should not need to call this. */ 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert int 675796c8dcSSimon Schubert deprecated_pc_in_call_dummy (struct gdbarch *gdbarch, CORE_ADDR pc) 685796c8dcSSimon Schubert { 695796c8dcSSimon Schubert struct dummy_frame *dummyframe; 70cf7f2e2dSJohn Marino 715796c8dcSSimon Schubert for (dummyframe = dummy_frame_stack; 725796c8dcSSimon Schubert dummyframe != NULL; 735796c8dcSSimon Schubert dummyframe = dummyframe->next) 745796c8dcSSimon Schubert { 755796c8dcSSimon Schubert if ((pc >= dummyframe->id.code_addr) 765796c8dcSSimon Schubert && (pc <= dummyframe->id.code_addr 775796c8dcSSimon Schubert + gdbarch_decr_pc_after_break (gdbarch))) 785796c8dcSSimon Schubert return 1; 795796c8dcSSimon Schubert } 805796c8dcSSimon Schubert return 0; 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert 835796c8dcSSimon Schubert /* Push the caller's state, along with the dummy frame info, onto the 845796c8dcSSimon Schubert dummy-frame stack. */ 855796c8dcSSimon Schubert 865796c8dcSSimon Schubert void 87c50c785cSJohn Marino dummy_frame_push (struct infcall_suspend_state *caller_state, 885796c8dcSSimon Schubert const struct frame_id *dummy_id) 895796c8dcSSimon Schubert { 905796c8dcSSimon Schubert struct dummy_frame *dummy_frame; 915796c8dcSSimon Schubert 925796c8dcSSimon Schubert dummy_frame = XZALLOC (struct dummy_frame); 935796c8dcSSimon Schubert dummy_frame->caller_state = caller_state; 945796c8dcSSimon Schubert dummy_frame->id = (*dummy_id); 955796c8dcSSimon Schubert dummy_frame->next = dummy_frame_stack; 965796c8dcSSimon Schubert dummy_frame_stack = dummy_frame; 975796c8dcSSimon Schubert } 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert /* Remove *DUMMY_PTR from the dummy frame stack. */ 1005796c8dcSSimon Schubert 1015796c8dcSSimon Schubert static void 1025796c8dcSSimon Schubert remove_dummy_frame (struct dummy_frame **dummy_ptr) 1035796c8dcSSimon Schubert { 1045796c8dcSSimon Schubert struct dummy_frame *dummy = *dummy_ptr; 1055796c8dcSSimon Schubert 1065796c8dcSSimon Schubert *dummy_ptr = dummy->next; 107c50c785cSJohn Marino discard_infcall_suspend_state (dummy->caller_state); 1085796c8dcSSimon Schubert xfree (dummy); 1095796c8dcSSimon Schubert } 1105796c8dcSSimon Schubert 1115796c8dcSSimon Schubert /* Pop *DUMMY_PTR, restoring program state to that before the 1125796c8dcSSimon Schubert frame was created. */ 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert static void 1155796c8dcSSimon Schubert pop_dummy_frame (struct dummy_frame **dummy_ptr) 1165796c8dcSSimon Schubert { 1175796c8dcSSimon Schubert struct dummy_frame *dummy; 1185796c8dcSSimon Schubert 119c50c785cSJohn Marino restore_infcall_suspend_state ((*dummy_ptr)->caller_state); 1205796c8dcSSimon Schubert 121c50c785cSJohn Marino /* restore_infcall_control_state frees inf_state, 122c50c785cSJohn Marino all that remains is to pop *dummy_ptr. */ 1235796c8dcSSimon Schubert dummy = *dummy_ptr; 1245796c8dcSSimon Schubert *dummy_ptr = dummy->next; 1255796c8dcSSimon Schubert xfree (dummy); 1265796c8dcSSimon Schubert 1275796c8dcSSimon Schubert /* We've made right mess of GDB's local state, just discard 1285796c8dcSSimon Schubert everything. */ 1295796c8dcSSimon Schubert reinit_frame_cache (); 1305796c8dcSSimon Schubert } 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert /* Look up DUMMY_ID. 1335796c8dcSSimon Schubert Return NULL if not found. */ 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert static struct dummy_frame ** 1365796c8dcSSimon Schubert lookup_dummy_frame (struct frame_id dummy_id) 1375796c8dcSSimon Schubert { 1385796c8dcSSimon Schubert struct dummy_frame **dp; 1395796c8dcSSimon Schubert 1405796c8dcSSimon Schubert for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next) 1415796c8dcSSimon Schubert { 1425796c8dcSSimon Schubert if (frame_id_eq ((*dp)->id, dummy_id)) 1435796c8dcSSimon Schubert return dp; 1445796c8dcSSimon Schubert } 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert return NULL; 1475796c8dcSSimon Schubert } 1485796c8dcSSimon Schubert 1495796c8dcSSimon Schubert /* Pop the dummy frame DUMMY_ID, restoring program state to that before the 1505796c8dcSSimon Schubert frame was created. 1515796c8dcSSimon Schubert On return reinit_frame_cache has been called. 1525796c8dcSSimon Schubert If the frame isn't found, flag an internal error. 1535796c8dcSSimon Schubert 1545796c8dcSSimon Schubert NOTE: This can only pop the one frame, even if it is in the middle of the 1555796c8dcSSimon Schubert stack, because the other frames may be for different threads, and there's 1565796c8dcSSimon Schubert currently no way to tell which stack frame is for which thread. */ 1575796c8dcSSimon Schubert 1585796c8dcSSimon Schubert void 1595796c8dcSSimon Schubert dummy_frame_pop (struct frame_id dummy_id) 1605796c8dcSSimon Schubert { 1615796c8dcSSimon Schubert struct dummy_frame **dp; 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert dp = lookup_dummy_frame (dummy_id); 1645796c8dcSSimon Schubert gdb_assert (dp != NULL); 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert pop_dummy_frame (dp); 1675796c8dcSSimon Schubert } 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert /* There may be stale dummy frames, perhaps left over from when a longjump took 1705796c8dcSSimon Schubert us out of a function that was called by the debugger. Clean them up at 1715796c8dcSSimon Schubert least once whenever we start a new inferior. */ 1725796c8dcSSimon Schubert 1735796c8dcSSimon Schubert static void 1745796c8dcSSimon Schubert cleanup_dummy_frames (struct target_ops *target, int from_tty) 1755796c8dcSSimon Schubert { 1765796c8dcSSimon Schubert while (dummy_frame_stack != NULL) 1775796c8dcSSimon Schubert remove_dummy_frame (&dummy_frame_stack); 1785796c8dcSSimon Schubert } 1795796c8dcSSimon Schubert 1805796c8dcSSimon Schubert /* Return the dummy frame cache, it contains both the ID, and a 1815796c8dcSSimon Schubert pointer to the regcache. */ 1825796c8dcSSimon Schubert struct dummy_frame_cache 1835796c8dcSSimon Schubert { 1845796c8dcSSimon Schubert struct frame_id this_id; 1855796c8dcSSimon Schubert struct regcache *prev_regcache; 1865796c8dcSSimon Schubert }; 1875796c8dcSSimon Schubert 1885796c8dcSSimon Schubert static int 1895796c8dcSSimon Schubert dummy_frame_sniffer (const struct frame_unwind *self, 1905796c8dcSSimon Schubert struct frame_info *this_frame, 1915796c8dcSSimon Schubert void **this_prologue_cache) 1925796c8dcSSimon Schubert { 1935796c8dcSSimon Schubert struct dummy_frame *dummyframe; 1945796c8dcSSimon Schubert struct frame_id this_id; 1955796c8dcSSimon Schubert 1965796c8dcSSimon Schubert /* When unwinding a normal frame, the stack structure is determined 1975796c8dcSSimon Schubert by analyzing the frame's function's code (be it using brute force 1985796c8dcSSimon Schubert prologue analysis, or the dwarf2 CFI). In the case of a dummy 1995796c8dcSSimon Schubert frame, that simply isn't possible. The PC is either the program 2005796c8dcSSimon Schubert entry point, or some random address on the stack. Trying to use 2015796c8dcSSimon Schubert that PC to apply standard frame ID unwind techniques is just 2025796c8dcSSimon Schubert asking for trouble. */ 2035796c8dcSSimon Schubert 2045796c8dcSSimon Schubert /* Don't bother unless there is at least one dummy frame. */ 2055796c8dcSSimon Schubert if (dummy_frame_stack != NULL) 2065796c8dcSSimon Schubert { 2075796c8dcSSimon Schubert /* Use an architecture specific method to extract this frame's 2085796c8dcSSimon Schubert dummy ID, assuming it is a dummy frame. */ 2095796c8dcSSimon Schubert this_id = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame); 2105796c8dcSSimon Schubert 2115796c8dcSSimon Schubert /* Use that ID to find the corresponding cache entry. */ 2125796c8dcSSimon Schubert for (dummyframe = dummy_frame_stack; 2135796c8dcSSimon Schubert dummyframe != NULL; 2145796c8dcSSimon Schubert dummyframe = dummyframe->next) 2155796c8dcSSimon Schubert { 2165796c8dcSSimon Schubert if (frame_id_eq (dummyframe->id, this_id)) 2175796c8dcSSimon Schubert { 2185796c8dcSSimon Schubert struct dummy_frame_cache *cache; 219cf7f2e2dSJohn Marino 2205796c8dcSSimon Schubert cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache); 221c50c785cSJohn Marino cache->prev_regcache = get_infcall_suspend_state_regcache 222c50c785cSJohn Marino (dummyframe->caller_state); 2235796c8dcSSimon Schubert cache->this_id = this_id; 2245796c8dcSSimon Schubert (*this_prologue_cache) = cache; 2255796c8dcSSimon Schubert return 1; 2265796c8dcSSimon Schubert } 2275796c8dcSSimon Schubert } 2285796c8dcSSimon Schubert } 2295796c8dcSSimon Schubert return 0; 2305796c8dcSSimon Schubert } 2315796c8dcSSimon Schubert 2325796c8dcSSimon Schubert /* Given a call-dummy dummy-frame, return the registers. Here the 2335796c8dcSSimon Schubert register value is taken from the local copy of the register buffer. */ 2345796c8dcSSimon Schubert 2355796c8dcSSimon Schubert static struct value * 2365796c8dcSSimon Schubert dummy_frame_prev_register (struct frame_info *this_frame, 2375796c8dcSSimon Schubert void **this_prologue_cache, 2385796c8dcSSimon Schubert int regnum) 2395796c8dcSSimon Schubert { 2405796c8dcSSimon Schubert struct dummy_frame_cache *cache = (*this_prologue_cache); 2415796c8dcSSimon Schubert struct gdbarch *gdbarch = get_frame_arch (this_frame); 2425796c8dcSSimon Schubert struct value *reg_val; 2435796c8dcSSimon Schubert 2445796c8dcSSimon Schubert /* The dummy-frame sniffer always fills in the cache. */ 2455796c8dcSSimon Schubert gdb_assert (cache != NULL); 2465796c8dcSSimon Schubert 2475796c8dcSSimon Schubert /* Describe the register's location. Generic dummy frames always 2485796c8dcSSimon Schubert have the register value in an ``expression''. */ 2495796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 2505796c8dcSSimon Schubert 2515796c8dcSSimon Schubert /* Use the regcache_cooked_read() method so that it, on the fly, 2525796c8dcSSimon Schubert constructs either a raw or pseudo register from the raw 2535796c8dcSSimon Schubert register cache. */ 2545796c8dcSSimon Schubert regcache_cooked_read (cache->prev_regcache, regnum, 2555796c8dcSSimon Schubert value_contents_writeable (reg_val)); 2565796c8dcSSimon Schubert return reg_val; 2575796c8dcSSimon Schubert } 2585796c8dcSSimon Schubert 2595796c8dcSSimon Schubert /* Assuming that THIS_FRAME is a dummy, return its ID. That ID is 2605796c8dcSSimon Schubert determined by examining the NEXT frame's unwound registers using 2615796c8dcSSimon Schubert the method dummy_id(). As a side effect, THIS dummy frame's 262*a45ae5f8SJohn Marino dummy cache is located and saved in THIS_PROLOGUE_CACHE. */ 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert static void 2655796c8dcSSimon Schubert dummy_frame_this_id (struct frame_info *this_frame, 2665796c8dcSSimon Schubert void **this_prologue_cache, 2675796c8dcSSimon Schubert struct frame_id *this_id) 2685796c8dcSSimon Schubert { 2695796c8dcSSimon Schubert /* The dummy-frame sniffer always fills in the cache. */ 2705796c8dcSSimon Schubert struct dummy_frame_cache *cache = (*this_prologue_cache); 271cf7f2e2dSJohn Marino 2725796c8dcSSimon Schubert gdb_assert (cache != NULL); 2735796c8dcSSimon Schubert (*this_id) = cache->this_id; 2745796c8dcSSimon Schubert } 2755796c8dcSSimon Schubert 276c50c785cSJohn Marino const struct frame_unwind dummy_frame_unwind = 2775796c8dcSSimon Schubert { 2785796c8dcSSimon Schubert DUMMY_FRAME, 279c50c785cSJohn Marino default_frame_unwind_stop_reason, 2805796c8dcSSimon Schubert dummy_frame_this_id, 2815796c8dcSSimon Schubert dummy_frame_prev_register, 2825796c8dcSSimon Schubert NULL, 2835796c8dcSSimon Schubert dummy_frame_sniffer, 2845796c8dcSSimon Schubert }; 2855796c8dcSSimon Schubert 2865796c8dcSSimon Schubert static void 2875796c8dcSSimon Schubert fprint_dummy_frames (struct ui_file *file) 2885796c8dcSSimon Schubert { 2895796c8dcSSimon Schubert struct dummy_frame *s; 290cf7f2e2dSJohn Marino 2915796c8dcSSimon Schubert for (s = dummy_frame_stack; s != NULL; s = s->next) 2925796c8dcSSimon Schubert { 2935796c8dcSSimon Schubert gdb_print_host_address (s, file); 2945796c8dcSSimon Schubert fprintf_unfiltered (file, ":"); 2955796c8dcSSimon Schubert fprintf_unfiltered (file, " id="); 2965796c8dcSSimon Schubert fprint_frame_id (file, s->id); 2975796c8dcSSimon Schubert fprintf_unfiltered (file, "\n"); 2985796c8dcSSimon Schubert } 2995796c8dcSSimon Schubert } 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert static void 3025796c8dcSSimon Schubert maintenance_print_dummy_frames (char *args, int from_tty) 3035796c8dcSSimon Schubert { 3045796c8dcSSimon Schubert if (args == NULL) 3055796c8dcSSimon Schubert fprint_dummy_frames (gdb_stdout); 3065796c8dcSSimon Schubert else 3075796c8dcSSimon Schubert { 3085796c8dcSSimon Schubert struct cleanup *cleanups; 3095796c8dcSSimon Schubert struct ui_file *file = gdb_fopen (args, "w"); 310cf7f2e2dSJohn Marino 3115796c8dcSSimon Schubert if (file == NULL) 3125796c8dcSSimon Schubert perror_with_name (_("maintenance print dummy-frames")); 3135796c8dcSSimon Schubert cleanups = make_cleanup_ui_file_delete (file); 3145796c8dcSSimon Schubert fprint_dummy_frames (file); 3155796c8dcSSimon Schubert do_cleanups (cleanups); 3165796c8dcSSimon Schubert } 3175796c8dcSSimon Schubert } 3185796c8dcSSimon Schubert 3195796c8dcSSimon Schubert extern void _initialize_dummy_frame (void); 3205796c8dcSSimon Schubert 3215796c8dcSSimon Schubert void 3225796c8dcSSimon Schubert _initialize_dummy_frame (void) 3235796c8dcSSimon Schubert { 3245796c8dcSSimon Schubert add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames, 3255796c8dcSSimon Schubert _("Print the contents of the internal dummy-frame stack."), 3265796c8dcSSimon Schubert &maintenanceprintlist); 3275796c8dcSSimon Schubert 3285796c8dcSSimon Schubert observer_attach_inferior_created (cleanup_dummy_frames); 3295796c8dcSSimon Schubert } 330