15796c8dcSSimon Schubert /* Definitions for frame unwinder, for GDB, the GNU debugger. 25796c8dcSSimon Schubert 3*ef5ccd6cSJohn Marino Copyright (C) 2003-2013 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 #include "defs.h" 215796c8dcSSimon Schubert #include "frame.h" 225796c8dcSSimon Schubert #include "frame-unwind.h" 235796c8dcSSimon Schubert #include "dummy-frame.h" 245796c8dcSSimon Schubert #include "inline-frame.h" 255796c8dcSSimon Schubert #include "value.h" 265796c8dcSSimon Schubert #include "regcache.h" 27c50c785cSJohn Marino #include "exceptions.h" 285796c8dcSSimon Schubert #include "gdb_assert.h" 295796c8dcSSimon Schubert #include "gdb_obstack.h" 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert static struct gdbarch_data *frame_unwind_data; 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert struct frame_unwind_table_entry 345796c8dcSSimon Schubert { 355796c8dcSSimon Schubert const struct frame_unwind *unwinder; 365796c8dcSSimon Schubert struct frame_unwind_table_entry *next; 375796c8dcSSimon Schubert }; 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert struct frame_unwind_table 405796c8dcSSimon Schubert { 415796c8dcSSimon Schubert struct frame_unwind_table_entry *list; 425796c8dcSSimon Schubert /* The head of the OSABI part of the search list. */ 435796c8dcSSimon Schubert struct frame_unwind_table_entry **osabi_head; 445796c8dcSSimon Schubert }; 455796c8dcSSimon Schubert 465796c8dcSSimon Schubert static void * 475796c8dcSSimon Schubert frame_unwind_init (struct obstack *obstack) 485796c8dcSSimon Schubert { 495796c8dcSSimon Schubert struct frame_unwind_table *table 505796c8dcSSimon Schubert = OBSTACK_ZALLOC (obstack, struct frame_unwind_table); 51cf7f2e2dSJohn Marino 525796c8dcSSimon Schubert /* Start the table out with a few default sniffers. OSABI code 535796c8dcSSimon Schubert can't override this. */ 545796c8dcSSimon Schubert table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry); 55c50c785cSJohn Marino table->list->unwinder = &dummy_frame_unwind; 56c50c785cSJohn Marino table->list->next = OBSTACK_ZALLOC (obstack, 57c50c785cSJohn Marino struct frame_unwind_table_entry); 58c50c785cSJohn Marino table->list->next->unwinder = &inline_frame_unwind; 595796c8dcSSimon Schubert /* The insertion point for OSABI sniffers. */ 605796c8dcSSimon Schubert table->osabi_head = &table->list->next->next; 615796c8dcSSimon Schubert return table; 625796c8dcSSimon Schubert } 635796c8dcSSimon Schubert 645796c8dcSSimon Schubert void 655796c8dcSSimon Schubert frame_unwind_prepend_unwinder (struct gdbarch *gdbarch, 665796c8dcSSimon Schubert const struct frame_unwind *unwinder) 675796c8dcSSimon Schubert { 685796c8dcSSimon Schubert struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data); 695796c8dcSSimon Schubert struct frame_unwind_table_entry *entry; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert /* Insert the new entry at the start of the list. */ 725796c8dcSSimon Schubert entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry); 735796c8dcSSimon Schubert entry->unwinder = unwinder; 745796c8dcSSimon Schubert entry->next = (*table->osabi_head); 755796c8dcSSimon Schubert (*table->osabi_head) = entry; 765796c8dcSSimon Schubert } 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert void 795796c8dcSSimon Schubert frame_unwind_append_unwinder (struct gdbarch *gdbarch, 805796c8dcSSimon Schubert const struct frame_unwind *unwinder) 815796c8dcSSimon Schubert { 825796c8dcSSimon Schubert struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data); 835796c8dcSSimon Schubert struct frame_unwind_table_entry **ip; 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* Find the end of the list and insert the new entry there. */ 865796c8dcSSimon Schubert for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next); 875796c8dcSSimon Schubert (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry); 885796c8dcSSimon Schubert (*ip)->unwinder = unwinder; 895796c8dcSSimon Schubert } 905796c8dcSSimon Schubert 91c50c785cSJohn Marino /* Iterate through sniffers for THIS_FRAME frame until one returns with an 92c50c785cSJohn Marino unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set 93c50c785cSJohn Marino by this function. Possibly initialize THIS_CACHE. */ 94c50c785cSJohn Marino 95c50c785cSJohn Marino void 965796c8dcSSimon Schubert frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache) 975796c8dcSSimon Schubert { 985796c8dcSSimon Schubert struct gdbarch *gdbarch = get_frame_arch (this_frame); 995796c8dcSSimon Schubert struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data); 1005796c8dcSSimon Schubert struct frame_unwind_table_entry *entry; 101cf7f2e2dSJohn Marino 1025796c8dcSSimon Schubert for (entry = table->list; entry != NULL; entry = entry->next) 1035796c8dcSSimon Schubert { 1045796c8dcSSimon Schubert struct cleanup *old_cleanup; 105c50c785cSJohn Marino volatile struct gdb_exception ex; 106c50c785cSJohn Marino int res = 0; 1075796c8dcSSimon Schubert 1085796c8dcSSimon Schubert old_cleanup = frame_prepare_for_sniffer (this_frame, entry->unwinder); 109c50c785cSJohn Marino 110c50c785cSJohn Marino TRY_CATCH (ex, RETURN_MASK_ERROR) 111c50c785cSJohn Marino { 112c50c785cSJohn Marino res = entry->unwinder->sniffer (entry->unwinder, this_frame, 113c50c785cSJohn Marino this_cache); 114c50c785cSJohn Marino } 115c50c785cSJohn Marino if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR) 116c50c785cSJohn Marino { 117c50c785cSJohn Marino /* This usually means that not even the PC is available, 118c50c785cSJohn Marino thus most unwinders aren't able to determine if they're 119c50c785cSJohn Marino the best fit. Keep trying. Fallback prologue unwinders 120c50c785cSJohn Marino should always accept the frame. */ 121c50c785cSJohn Marino } 122c50c785cSJohn Marino else if (ex.reason < 0) 123c50c785cSJohn Marino throw_exception (ex); 124c50c785cSJohn Marino else if (res) 1255796c8dcSSimon Schubert { 1265796c8dcSSimon Schubert discard_cleanups (old_cleanup); 127c50c785cSJohn Marino return; 1285796c8dcSSimon Schubert } 129c50c785cSJohn Marino 1305796c8dcSSimon Schubert do_cleanups (old_cleanup); 1315796c8dcSSimon Schubert } 1325796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed")); 1335796c8dcSSimon Schubert } 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert /* A default frame sniffer which always accepts the frame. Used by 1365796c8dcSSimon Schubert fallback prologue unwinders. */ 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert int 1395796c8dcSSimon Schubert default_frame_sniffer (const struct frame_unwind *self, 1405796c8dcSSimon Schubert struct frame_info *this_frame, 1415796c8dcSSimon Schubert void **this_prologue_cache) 1425796c8dcSSimon Schubert { 1435796c8dcSSimon Schubert return 1; 1445796c8dcSSimon Schubert } 1455796c8dcSSimon Schubert 146c50c785cSJohn Marino /* A default frame unwinder stop_reason callback that always claims 147c50c785cSJohn Marino the frame is unwindable. */ 148c50c785cSJohn Marino 149c50c785cSJohn Marino enum unwind_stop_reason 150c50c785cSJohn Marino default_frame_unwind_stop_reason (struct frame_info *this_frame, 151c50c785cSJohn Marino void **this_cache) 152c50c785cSJohn Marino { 153c50c785cSJohn Marino return UNWIND_NO_REASON; 154c50c785cSJohn Marino } 155c50c785cSJohn Marino 1565796c8dcSSimon Schubert /* Helper functions for value-based register unwinding. These return 1575796c8dcSSimon Schubert a (possibly lazy) value of the appropriate type. */ 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert /* Return a value which indicates that FRAME did not save REGNUM. */ 1605796c8dcSSimon Schubert 1615796c8dcSSimon Schubert struct value * 1625796c8dcSSimon Schubert frame_unwind_got_optimized (struct frame_info *frame, int regnum) 1635796c8dcSSimon Schubert { 1645796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1655796c8dcSSimon Schubert struct value *reg_val; 1665796c8dcSSimon Schubert 1675796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 1685796c8dcSSimon Schubert set_value_optimized_out (reg_val, 1); 1695796c8dcSSimon Schubert return reg_val; 1705796c8dcSSimon Schubert } 1715796c8dcSSimon Schubert 1725796c8dcSSimon Schubert /* Return a value which indicates that FRAME copied REGNUM into 1735796c8dcSSimon Schubert register NEW_REGNUM. */ 1745796c8dcSSimon Schubert 1755796c8dcSSimon Schubert struct value * 176c50c785cSJohn Marino frame_unwind_got_register (struct frame_info *frame, 177c50c785cSJohn Marino int regnum, int new_regnum) 1785796c8dcSSimon Schubert { 1795796c8dcSSimon Schubert return value_of_register_lazy (frame, new_regnum); 1805796c8dcSSimon Schubert } 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert /* Return a value which indicates that FRAME saved REGNUM in memory at 1835796c8dcSSimon Schubert ADDR. */ 1845796c8dcSSimon Schubert 1855796c8dcSSimon Schubert struct value * 1865796c8dcSSimon Schubert frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr) 1875796c8dcSSimon Schubert { 1885796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1895796c8dcSSimon Schubert struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr); 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert set_value_stack (v, 1); 1925796c8dcSSimon Schubert return v; 1935796c8dcSSimon Schubert } 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of 1965796c8dcSSimon Schubert REGNUM has a known constant (computed) value of VAL. */ 1975796c8dcSSimon Schubert 1985796c8dcSSimon Schubert struct value * 1995796c8dcSSimon Schubert frame_unwind_got_constant (struct frame_info *frame, int regnum, 2005796c8dcSSimon Schubert ULONGEST val) 2015796c8dcSSimon Schubert { 2025796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 2035796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 2045796c8dcSSimon Schubert struct value *reg_val; 2055796c8dcSSimon Schubert 2065796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 2075796c8dcSSimon Schubert store_unsigned_integer (value_contents_writeable (reg_val), 2085796c8dcSSimon Schubert register_size (gdbarch, regnum), byte_order, val); 2095796c8dcSSimon Schubert return reg_val; 2105796c8dcSSimon Schubert } 2115796c8dcSSimon Schubert 2125796c8dcSSimon Schubert struct value * 2135796c8dcSSimon Schubert frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf) 2145796c8dcSSimon Schubert { 2155796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 2165796c8dcSSimon Schubert struct value *reg_val; 2175796c8dcSSimon Schubert 2185796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 2195796c8dcSSimon Schubert memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum)); 2205796c8dcSSimon Schubert return reg_val; 2215796c8dcSSimon Schubert } 2225796c8dcSSimon Schubert 2235796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of REGNUM 2245796c8dcSSimon Schubert has a known constant (computed) value of ADDR. Convert the 2255796c8dcSSimon Schubert CORE_ADDR to a target address if necessary. */ 2265796c8dcSSimon Schubert 2275796c8dcSSimon Schubert struct value * 2285796c8dcSSimon Schubert frame_unwind_got_address (struct frame_info *frame, int regnum, 2295796c8dcSSimon Schubert CORE_ADDR addr) 2305796c8dcSSimon Schubert { 2315796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 2325796c8dcSSimon Schubert struct value *reg_val; 2335796c8dcSSimon Schubert 2345796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 2355796c8dcSSimon Schubert pack_long (value_contents_writeable (reg_val), 2365796c8dcSSimon Schubert register_type (gdbarch, regnum), addr); 2375796c8dcSSimon Schubert return reg_val; 2385796c8dcSSimon Schubert } 2395796c8dcSSimon Schubert 240c50c785cSJohn Marino /* -Wmissing-prototypes */ 241c50c785cSJohn Marino extern initialize_file_ftype _initialize_frame_unwind; 2425796c8dcSSimon Schubert 2435796c8dcSSimon Schubert void 2445796c8dcSSimon Schubert _initialize_frame_unwind (void) 2455796c8dcSSimon Schubert { 2465796c8dcSSimon Schubert frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init); 2475796c8dcSSimon Schubert } 248