15796c8dcSSimon Schubert /* Definitions for frame unwinder, for GDB, the GNU debugger. 25796c8dcSSimon Schubert 3*cf7f2e2dSJohn Marino Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 4*cf7f2e2dSJohn Marino Free Software Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of GDB. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 115796c8dcSSimon Schubert (at your option) any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165796c8dcSSimon Schubert GNU General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "frame.h" 235796c8dcSSimon Schubert #include "frame-unwind.h" 245796c8dcSSimon Schubert #include "dummy-frame.h" 255796c8dcSSimon Schubert #include "inline-frame.h" 265796c8dcSSimon Schubert #include "value.h" 275796c8dcSSimon Schubert #include "regcache.h" 285796c8dcSSimon Schubert 295796c8dcSSimon Schubert #include "gdb_assert.h" 305796c8dcSSimon Schubert #include "gdb_obstack.h" 315796c8dcSSimon Schubert 325796c8dcSSimon Schubert static struct gdbarch_data *frame_unwind_data; 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert struct frame_unwind_table_entry 355796c8dcSSimon Schubert { 365796c8dcSSimon Schubert const struct frame_unwind *unwinder; 375796c8dcSSimon Schubert struct frame_unwind_table_entry *next; 385796c8dcSSimon Schubert }; 395796c8dcSSimon Schubert 405796c8dcSSimon Schubert struct frame_unwind_table 415796c8dcSSimon Schubert { 425796c8dcSSimon Schubert struct frame_unwind_table_entry *list; 435796c8dcSSimon Schubert /* The head of the OSABI part of the search list. */ 445796c8dcSSimon Schubert struct frame_unwind_table_entry **osabi_head; 455796c8dcSSimon Schubert }; 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert static void * 485796c8dcSSimon Schubert frame_unwind_init (struct obstack *obstack) 495796c8dcSSimon Schubert { 505796c8dcSSimon Schubert struct frame_unwind_table *table 515796c8dcSSimon Schubert = OBSTACK_ZALLOC (obstack, struct frame_unwind_table); 52*cf7f2e2dSJohn Marino 535796c8dcSSimon Schubert /* Start the table out with a few default sniffers. OSABI code 545796c8dcSSimon Schubert can't override this. */ 555796c8dcSSimon Schubert table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry); 565796c8dcSSimon Schubert table->list->unwinder = dummy_frame_unwind; 575796c8dcSSimon Schubert table->list->next = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry); 585796c8dcSSimon Schubert 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 915796c8dcSSimon Schubert const struct frame_unwind * 925796c8dcSSimon Schubert frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache) 935796c8dcSSimon Schubert { 945796c8dcSSimon Schubert struct gdbarch *gdbarch = get_frame_arch (this_frame); 955796c8dcSSimon Schubert struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data); 965796c8dcSSimon Schubert struct frame_unwind_table_entry *entry; 97*cf7f2e2dSJohn Marino 985796c8dcSSimon Schubert for (entry = table->list; entry != NULL; entry = entry->next) 995796c8dcSSimon Schubert { 1005796c8dcSSimon Schubert struct cleanup *old_cleanup; 1015796c8dcSSimon Schubert 1025796c8dcSSimon Schubert old_cleanup = frame_prepare_for_sniffer (this_frame, entry->unwinder); 1035796c8dcSSimon Schubert if (entry->unwinder->sniffer (entry->unwinder, this_frame, 1045796c8dcSSimon Schubert this_cache)) 1055796c8dcSSimon Schubert { 1065796c8dcSSimon Schubert discard_cleanups (old_cleanup); 1075796c8dcSSimon Schubert return entry->unwinder; 1085796c8dcSSimon Schubert } 1095796c8dcSSimon Schubert do_cleanups (old_cleanup); 1105796c8dcSSimon Schubert } 1115796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed")); 1125796c8dcSSimon Schubert } 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert /* A default frame sniffer which always accepts the frame. Used by 1155796c8dcSSimon Schubert fallback prologue unwinders. */ 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert int 1185796c8dcSSimon Schubert default_frame_sniffer (const struct frame_unwind *self, 1195796c8dcSSimon Schubert struct frame_info *this_frame, 1205796c8dcSSimon Schubert void **this_prologue_cache) 1215796c8dcSSimon Schubert { 1225796c8dcSSimon Schubert return 1; 1235796c8dcSSimon Schubert } 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert /* Helper functions for value-based register unwinding. These return 1265796c8dcSSimon Schubert a (possibly lazy) value of the appropriate type. */ 1275796c8dcSSimon Schubert 1285796c8dcSSimon Schubert /* Return a value which indicates that FRAME did not save REGNUM. */ 1295796c8dcSSimon Schubert 1305796c8dcSSimon Schubert struct value * 1315796c8dcSSimon Schubert frame_unwind_got_optimized (struct frame_info *frame, int regnum) 1325796c8dcSSimon Schubert { 1335796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1345796c8dcSSimon Schubert struct value *reg_val; 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 1375796c8dcSSimon Schubert set_value_optimized_out (reg_val, 1); 1385796c8dcSSimon Schubert return reg_val; 1395796c8dcSSimon Schubert } 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert /* Return a value which indicates that FRAME copied REGNUM into 1425796c8dcSSimon Schubert register NEW_REGNUM. */ 1435796c8dcSSimon Schubert 1445796c8dcSSimon Schubert struct value * 1455796c8dcSSimon Schubert frame_unwind_got_register (struct frame_info *frame, int regnum, int new_regnum) 1465796c8dcSSimon Schubert { 1475796c8dcSSimon Schubert return value_of_register_lazy (frame, new_regnum); 1485796c8dcSSimon Schubert } 1495796c8dcSSimon Schubert 1505796c8dcSSimon Schubert /* Return a value which indicates that FRAME saved REGNUM in memory at 1515796c8dcSSimon Schubert ADDR. */ 1525796c8dcSSimon Schubert 1535796c8dcSSimon Schubert struct value * 1545796c8dcSSimon Schubert frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr) 1555796c8dcSSimon Schubert { 1565796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1575796c8dcSSimon Schubert struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr); 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert set_value_stack (v, 1); 1605796c8dcSSimon Schubert return v; 1615796c8dcSSimon Schubert } 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of 1645796c8dcSSimon Schubert REGNUM has a known constant (computed) value of VAL. */ 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert struct value * 1675796c8dcSSimon Schubert frame_unwind_got_constant (struct frame_info *frame, int regnum, 1685796c8dcSSimon Schubert ULONGEST val) 1695796c8dcSSimon Schubert { 1705796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1715796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 1725796c8dcSSimon Schubert struct value *reg_val; 1735796c8dcSSimon Schubert 1745796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 1755796c8dcSSimon Schubert store_unsigned_integer (value_contents_writeable (reg_val), 1765796c8dcSSimon Schubert register_size (gdbarch, regnum), byte_order, val); 1775796c8dcSSimon Schubert return reg_val; 1785796c8dcSSimon Schubert } 1795796c8dcSSimon Schubert 1805796c8dcSSimon Schubert struct value * 1815796c8dcSSimon Schubert frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf) 1825796c8dcSSimon Schubert { 1835796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 1845796c8dcSSimon Schubert struct value *reg_val; 1855796c8dcSSimon Schubert 1865796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 1875796c8dcSSimon Schubert memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum)); 1885796c8dcSSimon Schubert return reg_val; 1895796c8dcSSimon Schubert } 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert /* Return a value which indicates that FRAME's saved version of REGNUM 1925796c8dcSSimon Schubert has a known constant (computed) value of ADDR. Convert the 1935796c8dcSSimon Schubert CORE_ADDR to a target address if necessary. */ 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert struct value * 1965796c8dcSSimon Schubert frame_unwind_got_address (struct frame_info *frame, int regnum, 1975796c8dcSSimon Schubert CORE_ADDR addr) 1985796c8dcSSimon Schubert { 1995796c8dcSSimon Schubert struct gdbarch *gdbarch = frame_unwind_arch (frame); 2005796c8dcSSimon Schubert struct value *reg_val; 2015796c8dcSSimon Schubert 2025796c8dcSSimon Schubert reg_val = value_zero (register_type (gdbarch, regnum), not_lval); 2035796c8dcSSimon Schubert pack_long (value_contents_writeable (reg_val), 2045796c8dcSSimon Schubert register_type (gdbarch, regnum), addr); 2055796c8dcSSimon Schubert return reg_val; 2065796c8dcSSimon Schubert } 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert extern initialize_file_ftype _initialize_frame_unwind; /* -Wmissing-prototypes */ 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert void 2115796c8dcSSimon Schubert _initialize_frame_unwind (void) 2125796c8dcSSimon Schubert { 2135796c8dcSSimon Schubert frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init); 2145796c8dcSSimon Schubert } 215