GDB (API)
/home/stan/gdb/src/gdb/frame-unwind.c
Go to the documentation of this file.
00001 /* Definitions for frame unwinder, for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "frame-unwind.h"
00023 #include "dummy-frame.h"
00024 #include "inline-frame.h"
00025 #include "value.h"
00026 #include "regcache.h"
00027 #include "exceptions.h"
00028 #include "gdb_assert.h"
00029 #include "gdb_obstack.h"
00030 
00031 static struct gdbarch_data *frame_unwind_data;
00032 
00033 struct frame_unwind_table_entry
00034 {
00035   const struct frame_unwind *unwinder;
00036   struct frame_unwind_table_entry *next;
00037 };
00038 
00039 struct frame_unwind_table
00040 {
00041   struct frame_unwind_table_entry *list;
00042   /* The head of the OSABI part of the search list.  */
00043   struct frame_unwind_table_entry **osabi_head;
00044 };
00045 
00046 static void *
00047 frame_unwind_init (struct obstack *obstack)
00048 {
00049   struct frame_unwind_table *table
00050     = OBSTACK_ZALLOC (obstack, struct frame_unwind_table);
00051 
00052   /* Start the table out with a few default sniffers.  OSABI code
00053      can't override this.  */
00054   table->list = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
00055   table->list->unwinder = &dummy_frame_unwind;
00056   table->list->next = OBSTACK_ZALLOC (obstack,
00057                                       struct frame_unwind_table_entry);
00058   table->list->next->unwinder = &inline_frame_unwind;
00059   /* The insertion point for OSABI sniffers.  */
00060   table->osabi_head = &table->list->next->next;
00061   return table;
00062 }
00063 
00064 void
00065 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
00066                                 const struct frame_unwind *unwinder)
00067 {
00068   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
00069   struct frame_unwind_table_entry *entry;
00070 
00071   /* Insert the new entry at the start of the list.  */
00072   entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
00073   entry->unwinder = unwinder;
00074   entry->next = (*table->osabi_head);
00075   (*table->osabi_head) = entry;
00076 }
00077 
00078 void
00079 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
00080                               const struct frame_unwind *unwinder)
00081 {
00082   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
00083   struct frame_unwind_table_entry **ip;
00084 
00085   /* Find the end of the list and insert the new entry there.  */
00086   for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
00087   (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
00088   (*ip)->unwinder = unwinder;
00089 }
00090 
00091 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
00092    unwinder implementation.  THIS_FRAME->UNWIND must be NULL, it will get set
00093    by this function.  Possibly initialize THIS_CACHE.  */
00094 
00095 void
00096 frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache)
00097 {
00098   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00099   struct frame_unwind_table *table = gdbarch_data (gdbarch, frame_unwind_data);
00100   struct frame_unwind_table_entry *entry;
00101 
00102   for (entry = table->list; entry != NULL; entry = entry->next)
00103     {
00104       struct cleanup *old_cleanup;
00105       volatile struct gdb_exception ex;
00106       int res = 0;
00107 
00108       old_cleanup = frame_prepare_for_sniffer (this_frame, entry->unwinder);
00109 
00110       TRY_CATCH (ex, RETURN_MASK_ERROR)
00111         {
00112           res = entry->unwinder->sniffer (entry->unwinder, this_frame,
00113                                           this_cache);
00114         }
00115       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
00116         {
00117           /* This usually means that not even the PC is available,
00118              thus most unwinders aren't able to determine if they're
00119              the best fit.  Keep trying.  Fallback prologue unwinders
00120              should always accept the frame.  */
00121         }
00122       else if (ex.reason < 0)
00123         throw_exception (ex);
00124       else if (res)
00125         {
00126           discard_cleanups (old_cleanup);
00127           return;
00128         }
00129 
00130       do_cleanups (old_cleanup);
00131     }
00132   internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed"));
00133 }
00134 
00135 /* A default frame sniffer which always accepts the frame.  Used by
00136    fallback prologue unwinders.  */
00137 
00138 int
00139 default_frame_sniffer (const struct frame_unwind *self,
00140                        struct frame_info *this_frame,
00141                        void **this_prologue_cache)
00142 {
00143   return 1;
00144 }
00145 
00146 /* A default frame unwinder stop_reason callback that always claims
00147    the frame is unwindable.  */
00148 
00149 enum unwind_stop_reason
00150 default_frame_unwind_stop_reason (struct frame_info *this_frame,
00151                                   void **this_cache)
00152 {
00153   return UNWIND_NO_REASON;
00154 }
00155 
00156 /* Helper functions for value-based register unwinding.  These return
00157    a (possibly lazy) value of the appropriate type.  */
00158 
00159 /* Return a value which indicates that FRAME did not save REGNUM.  */
00160 
00161 struct value *
00162 frame_unwind_got_optimized (struct frame_info *frame, int regnum)
00163 {
00164   struct gdbarch *gdbarch = frame_unwind_arch (frame);
00165   struct type *reg_type = register_type (gdbarch, regnum);
00166 
00167   return allocate_optimized_out_value (reg_type);
00168 }
00169 
00170 /* Return a value which indicates that FRAME copied REGNUM into
00171    register NEW_REGNUM.  */
00172 
00173 struct value *
00174 frame_unwind_got_register (struct frame_info *frame,
00175                            int regnum, int new_regnum)
00176 {
00177   return value_of_register_lazy (frame, new_regnum);
00178 }
00179 
00180 /* Return a value which indicates that FRAME saved REGNUM in memory at
00181    ADDR.  */
00182 
00183 struct value *
00184 frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr)
00185 {
00186   struct gdbarch *gdbarch = frame_unwind_arch (frame);
00187   struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
00188 
00189   set_value_stack (v, 1);
00190   return v;
00191 }
00192 
00193 /* Return a value which indicates that FRAME's saved version of
00194    REGNUM has a known constant (computed) value of VAL.  */
00195 
00196 struct value *
00197 frame_unwind_got_constant (struct frame_info *frame, int regnum,
00198                            ULONGEST val)
00199 {
00200   struct gdbarch *gdbarch = frame_unwind_arch (frame);
00201   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00202   struct value *reg_val;
00203 
00204   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
00205   store_unsigned_integer (value_contents_writeable (reg_val),
00206                           register_size (gdbarch, regnum), byte_order, val);
00207   return reg_val;
00208 }
00209 
00210 struct value *
00211 frame_unwind_got_bytes (struct frame_info *frame, int regnum, gdb_byte *buf)
00212 {
00213   struct gdbarch *gdbarch = frame_unwind_arch (frame);
00214   struct value *reg_val;
00215 
00216   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
00217   memcpy (value_contents_raw (reg_val), buf, register_size (gdbarch, regnum));
00218   return reg_val;
00219 }
00220 
00221 /* Return a value which indicates that FRAME's saved version of REGNUM
00222    has a known constant (computed) value of ADDR.  Convert the
00223    CORE_ADDR to a target address if necessary.  */
00224 
00225 struct value *
00226 frame_unwind_got_address (struct frame_info *frame, int regnum,
00227                           CORE_ADDR addr)
00228 {
00229   struct gdbarch *gdbarch = frame_unwind_arch (frame);
00230   struct value *reg_val;
00231 
00232   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
00233   pack_long (value_contents_writeable (reg_val),
00234              register_type (gdbarch, regnum), addr);
00235   return reg_val;
00236 }
00237 
00238 /* -Wmissing-prototypes */
00239 extern initialize_file_ftype _initialize_frame_unwind;
00240 
00241 void
00242 _initialize_frame_unwind (void)
00243 {
00244   frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init);
00245 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines