GDB (API)
/home/stan/gdb/src/gdb/ia64-libunwind-tdep.c
Go to the documentation of this file.
00001 /* Frame unwinder for ia64 frames using the libunwind library.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    Written by Jeff Johnston, contributed by Red Hat Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 
00024 #include "inferior.h"
00025 #include "frame.h"
00026 #include "frame-base.h"
00027 #include "frame-unwind.h"
00028 #include "gdbcore.h"
00029 #include "gdbtypes.h"
00030 #include "symtab.h"
00031 #include "objfiles.h"
00032 #include "regcache.h"
00033 
00034 #include <dlfcn.h>
00035 
00036 #include "gdb_assert.h"
00037 #include "gdb_string.h"
00038 
00039 #include "ia64-libunwind-tdep.h"
00040 
00041 #include "complaints.h"
00042 
00043 /* IA-64 is the only target that currently uses ia64-libunwind-tdep.
00044    Note how UNW_TARGET, UNW_OBJ, etc. are compile time constants below.
00045    Those come from libunwind's headers, and are target dependent.
00046    Also, some of libunwind's typedefs are target dependent, as e.g.,
00047    unw_word_t.  If some other target wants to use this, we will need
00048    to do some abstracting in order to make it possible to select which
00049    libunwind we're talking to at runtime (and have one per arch).  */
00050 
00051 /* The following two macros are normally defined in <endian.h>.
00052    But systems such as ia64-hpux do not provide such header, so
00053    we just define them here if not already defined.  */
00054 #ifndef __LITTLE_ENDIAN
00055 #define __LITTLE_ENDIAN 1234
00056 #endif
00057 #ifndef __BIG_ENDIAN
00058 #define __BIG_ENDIAN    4321
00059 #endif
00060 
00061 static int libunwind_initialized;
00062 static struct gdbarch_data *libunwind_descr_handle;
00063 
00064 /* Required function pointers from libunwind.  */
00065 static int (*unw_get_reg_p) (unw_cursor_t *, unw_regnum_t, unw_word_t *);
00066 static int (*unw_get_fpreg_p) (unw_cursor_t *, unw_regnum_t, unw_fpreg_t *);
00067 static int (*unw_get_saveloc_p) (unw_cursor_t *, unw_regnum_t,
00068                                  unw_save_loc_t *);
00069 static int (*unw_is_signal_frame_p) (unw_cursor_t *);
00070 static int (*unw_step_p) (unw_cursor_t *);
00071 static int (*unw_init_remote_p) (unw_cursor_t *, unw_addr_space_t, void *);
00072 static unw_addr_space_t (*unw_create_addr_space_p) (unw_accessors_t *, int);
00073 static void (*unw_destroy_addr_space_p) (unw_addr_space_t);
00074 static int (*unw_search_unwind_table_p) (unw_addr_space_t, unw_word_t,
00075                                          unw_dyn_info_t *,
00076                                          unw_proc_info_t *, int, void *);
00077 static unw_word_t (*unw_find_dyn_list_p) (unw_addr_space_t, unw_dyn_info_t *,
00078                                           void *);
00079 
00080 
00081 struct libunwind_frame_cache
00082 {
00083   CORE_ADDR base;
00084   CORE_ADDR func_addr;
00085   unw_cursor_t cursor;
00086   unw_addr_space_t as;
00087 };
00088 
00089 /* We need to qualify the function names with a platform-specific prefix
00090    to match the names used by the libunwind library.  The UNW_OBJ macro is
00091    provided by the libunwind.h header file.  */
00092 #define STRINGIFY2(name)        #name
00093 #define STRINGIFY(name)         STRINGIFY2(name)
00094 
00095 #ifndef LIBUNWIND_SO
00096 /* Use the stable ABI major version number.  `libunwind-ia64.so' is a link time
00097    only library, not a runtime one.  */
00098 #define LIBUNWIND_SO "libunwind-" STRINGIFY(UNW_TARGET) ".so.8"
00099 
00100 /* Provide also compatibility with older .so.  The two APIs are compatible, .8
00101    is only extended a bit, GDB does not use the extended API at all.  */
00102 #define LIBUNWIND_SO_7 "libunwind-" STRINGIFY(UNW_TARGET) ".so.7"
00103 #endif
00104 
00105 static char *get_reg_name = STRINGIFY(UNW_OBJ(get_reg));
00106 static char *get_fpreg_name = STRINGIFY(UNW_OBJ(get_fpreg));
00107 static char *get_saveloc_name = STRINGIFY(UNW_OBJ(get_save_loc));
00108 static char *is_signal_frame_name = STRINGIFY(UNW_OBJ(is_signal_frame));
00109 static char *step_name = STRINGIFY(UNW_OBJ(step));
00110 static char *init_remote_name = STRINGIFY(UNW_OBJ(init_remote));
00111 static char *create_addr_space_name = STRINGIFY(UNW_OBJ(create_addr_space));
00112 static char *destroy_addr_space_name = STRINGIFY(UNW_OBJ(destroy_addr_space));
00113 static char *search_unwind_table_name
00114   = STRINGIFY(UNW_OBJ(search_unwind_table));
00115 static char *find_dyn_list_name = STRINGIFY(UNW_OBJ(find_dyn_list));
00116 
00117 static struct libunwind_descr *
00118 libunwind_descr (struct gdbarch *gdbarch)
00119 {
00120   return gdbarch_data (gdbarch, libunwind_descr_handle);
00121 }
00122 
00123 static void *
00124 libunwind_descr_init (struct gdbarch *gdbarch)
00125 {
00126   struct libunwind_descr *descr
00127     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct libunwind_descr);
00128 
00129   return descr;
00130 }
00131 
00132 void
00133 libunwind_frame_set_descr (struct gdbarch *gdbarch,
00134                            struct libunwind_descr *descr)
00135 {
00136   struct libunwind_descr *arch_descr;
00137 
00138   gdb_assert (gdbarch != NULL);
00139 
00140   arch_descr = gdbarch_data (gdbarch, libunwind_descr_handle);
00141 
00142   if (arch_descr == NULL)
00143     {
00144       /* First time here.  Must initialize data area.  */
00145       arch_descr = libunwind_descr_init (gdbarch);
00146       deprecated_set_gdbarch_data (gdbarch,
00147                                    libunwind_descr_handle, arch_descr);
00148     }
00149 
00150   /* Copy new descriptor info into arch descriptor.  */
00151   arch_descr->gdb2uw = descr->gdb2uw;
00152   arch_descr->uw2gdb = descr->uw2gdb;
00153   arch_descr->is_fpreg = descr->is_fpreg;
00154   arch_descr->accessors = descr->accessors;
00155   arch_descr->special_accessors = descr->special_accessors;
00156 }
00157 
00158 static struct libunwind_frame_cache *
00159 libunwind_frame_cache (struct frame_info *this_frame, void **this_cache)
00160 {
00161   unw_accessors_t *acc;
00162   unw_addr_space_t as;
00163   unw_word_t fp;
00164   unw_regnum_t uw_sp_regnum;
00165   struct libunwind_frame_cache *cache;
00166   struct libunwind_descr *descr;
00167   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00168   int i, ret;
00169 
00170   if (*this_cache)
00171     return *this_cache;
00172 
00173   /* Allocate a new cache.  */
00174   cache = FRAME_OBSTACK_ZALLOC (struct libunwind_frame_cache);
00175 
00176   cache->func_addr = get_frame_func (this_frame);
00177   if (cache->func_addr == 0)
00178     /* This can happen when the frame corresponds to a function for which
00179        there is no debugging information nor any entry in the symbol table.
00180        This is probably a static function for which an entry in the symbol
00181        table was not created when the objfile got linked (observed in
00182        libpthread.so on ia64-hpux).
00183 
00184        The best we can do, in that case, is use the frame PC as the function
00185        address.  We don't need to give up since we still have the unwind
00186        record to help us perform the unwinding.  There is also another
00187        compelling to continue, because abandonning now means stopping
00188        the backtrace, which can never be helpful for the user.  */
00189     cache->func_addr = get_frame_pc (this_frame);
00190 
00191   /* Get a libunwind cursor to the previous frame.
00192   
00193      We do this by initializing a cursor.  Libunwind treats a new cursor
00194      as the top of stack and will get the current register set via the
00195      libunwind register accessor.  Now, we provide the platform-specific
00196      accessors and we set up the register accessor to use the frame
00197      register unwinding interfaces so that we properly get the registers
00198      for the current frame rather than the top.  We then use the unw_step
00199      function to move the libunwind cursor back one frame.  We can later
00200      use this cursor to find previous registers via the unw_get_reg
00201      interface which will invoke libunwind's special logic.  */
00202   descr = libunwind_descr (gdbarch);
00203   acc = descr->accessors;
00204   as =  unw_create_addr_space_p (acc,
00205                                  gdbarch_byte_order (gdbarch)
00206                                  == BFD_ENDIAN_BIG
00207                                  ? __BIG_ENDIAN
00208                                  : __LITTLE_ENDIAN);
00209 
00210   unw_init_remote_p (&cache->cursor, as, this_frame);
00211   if (unw_step_p (&cache->cursor) < 0)
00212     {
00213       unw_destroy_addr_space_p (as);
00214       return NULL;
00215     }
00216 
00217   /* To get base address, get sp from previous frame.  */
00218   uw_sp_regnum = descr->gdb2uw (gdbarch_sp_regnum (gdbarch));
00219   ret = unw_get_reg_p (&cache->cursor, uw_sp_regnum, &fp);
00220   if (ret < 0)
00221     {
00222       unw_destroy_addr_space_p (as);
00223       error (_("Can't get libunwind sp register."));
00224     }
00225 
00226   cache->base = (CORE_ADDR)fp;
00227   cache->as = as;
00228 
00229   *this_cache = cache;
00230   return cache;
00231 }
00232 
00233 void
00234 libunwind_frame_dealloc_cache (struct frame_info *self, void *this_cache)
00235 {
00236   struct libunwind_frame_cache *cache = this_cache;
00237 
00238   if (cache->as)
00239     unw_destroy_addr_space_p (cache->as);
00240 }
00241 
00242 unw_word_t
00243 libunwind_find_dyn_list (unw_addr_space_t as, unw_dyn_info_t *di, void *arg)
00244 {
00245   return unw_find_dyn_list_p (as, di, arg);
00246 }
00247 
00248 /* Verify if there is sufficient libunwind information for the frame to use
00249    libunwind frame unwinding.  */
00250 int
00251 libunwind_frame_sniffer (const struct frame_unwind *self,
00252                          struct frame_info *this_frame, void **this_cache)
00253 {
00254   unw_cursor_t cursor;
00255   unw_accessors_t *acc;
00256   unw_addr_space_t as;
00257   struct libunwind_descr *descr;
00258   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00259   int i, ret;
00260 
00261   /* To test for libunwind unwind support, initialize a cursor to
00262      the current frame and try to back up.  We use this same method
00263      when setting up the frame cache (see libunwind_frame_cache()).
00264      If libunwind returns success for this operation, it means that
00265      it has found sufficient libunwind unwinding information to do so.  */
00266 
00267   descr = libunwind_descr (gdbarch);
00268   acc = descr->accessors;
00269   as =  unw_create_addr_space_p (acc,
00270                                  gdbarch_byte_order (gdbarch)
00271                                  == BFD_ENDIAN_BIG
00272                                  ? __BIG_ENDIAN
00273                                  : __LITTLE_ENDIAN);
00274 
00275   ret = unw_init_remote_p (&cursor, as, this_frame);
00276 
00277   if (ret < 0)
00278     {
00279       unw_destroy_addr_space_p (as);
00280       return 0;
00281     }
00282 
00283  
00284   /* Check to see if we have libunwind info by checking if we are in a 
00285      signal frame.  If it doesn't return an error, we have libunwind info
00286      and can use libunwind.  */
00287   ret = unw_is_signal_frame_p (&cursor);
00288   unw_destroy_addr_space_p (as);
00289 
00290   if (ret < 0)
00291     return 0;
00292 
00293   return 1;
00294 }
00295 
00296 void
00297 libunwind_frame_this_id (struct frame_info *this_frame, void **this_cache,
00298                          struct frame_id *this_id)
00299 {
00300   struct libunwind_frame_cache *cache =
00301     libunwind_frame_cache (this_frame, this_cache);
00302 
00303   if (cache != NULL)
00304     (*this_id) = frame_id_build (cache->base, cache->func_addr);
00305 }
00306 
00307 struct value *
00308 libunwind_frame_prev_register (struct frame_info *this_frame,
00309                                void **this_cache, int regnum)
00310 {
00311   struct libunwind_frame_cache *cache =
00312     libunwind_frame_cache (this_frame, this_cache);
00313   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00314 
00315   void *ptr;
00316   unw_cursor_t *c;
00317   unw_save_loc_t sl;
00318   int i, ret;
00319   unw_word_t intval;
00320   unw_fpreg_t fpval;
00321   unw_regnum_t uw_regnum;
00322   struct libunwind_descr *descr;
00323   struct value *val = NULL;
00324 
00325   if (cache == NULL)
00326     return frame_unwind_got_constant (this_frame, regnum, 0);
00327   
00328   /* Convert from gdb register number to libunwind register number.  */
00329   descr = libunwind_descr (get_frame_arch (this_frame));
00330   uw_regnum = descr->gdb2uw (regnum);
00331 
00332   gdb_assert (regnum >= 0);
00333 
00334   if (!target_has_registers)
00335     error (_("No registers."));
00336 
00337   if (uw_regnum < 0)
00338     return frame_unwind_got_constant (this_frame, regnum, 0);
00339 
00340   if (unw_get_saveloc_p (&cache->cursor, uw_regnum, &sl) < 0)
00341     return frame_unwind_got_constant (this_frame, regnum, 0);
00342 
00343   switch (sl.type)
00344     {
00345     case UNW_SLT_MEMORY:
00346       val = frame_unwind_got_memory (this_frame, regnum, sl.u.addr);
00347       break;
00348 
00349     case UNW_SLT_REG:
00350       val = frame_unwind_got_register (this_frame, regnum,
00351                                        descr->uw2gdb (sl.u.regnum));
00352       break;
00353     case UNW_SLT_NONE:
00354       {
00355         /* The register is not stored at a specific memory address nor
00356            inside another register.  So use libunwind to fetch the register
00357            value for us, and create a constant value with the result.  */
00358         if (descr->is_fpreg (uw_regnum))
00359           {
00360             ret = unw_get_fpreg_p (&cache->cursor, uw_regnum, &fpval);
00361             if (ret < 0)
00362               return frame_unwind_got_constant (this_frame, regnum, 0);
00363             val = frame_unwind_got_bytes (this_frame, regnum,
00364                                           (gdb_byte *) &fpval);
00365           }
00366         else
00367           {
00368             ret = unw_get_reg_p (&cache->cursor, uw_regnum, &intval);
00369             if (ret < 0)
00370               return frame_unwind_got_constant (this_frame, regnum, 0);
00371             val = frame_unwind_got_constant (this_frame, regnum, intval);
00372           }
00373         break;
00374       }
00375     }
00376 
00377   return val;
00378 } 
00379 
00380 /* The following is a glue routine to call the libunwind unwind table
00381    search function to get unwind information for a specified ip address.  */ 
00382 int
00383 libunwind_search_unwind_table (void *as, long ip, void *di,
00384                                void *pi, int need_unwind_info, void *args)
00385 {
00386   return unw_search_unwind_table_p (*(unw_addr_space_t *)as, (unw_word_t )ip, 
00387                                     di, pi, need_unwind_info, args);
00388 }
00389 
00390 /* Verify if we are in a sigtramp frame and we can use libunwind to unwind.  */
00391 int
00392 libunwind_sigtramp_frame_sniffer (const struct frame_unwind *self,
00393                                   struct frame_info *this_frame,
00394                                   void **this_cache)
00395 {
00396   unw_cursor_t cursor;
00397   unw_accessors_t *acc;
00398   unw_addr_space_t as;
00399   struct libunwind_descr *descr;
00400   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00401   int i, ret;
00402 
00403   /* To test for libunwind unwind support, initialize a cursor to the
00404      current frame and try to back up.  We use this same method when
00405      setting up the frame cache (see libunwind_frame_cache()).  If
00406      libunwind returns success for this operation, it means that it
00407      has found sufficient libunwind unwinding information to do
00408      so.  */
00409 
00410   descr = libunwind_descr (gdbarch);
00411   acc = descr->accessors;
00412   as =  unw_create_addr_space_p (acc,
00413                                  gdbarch_byte_order (gdbarch)
00414                                  == BFD_ENDIAN_BIG
00415                                  ? __BIG_ENDIAN
00416                                  : __LITTLE_ENDIAN);
00417 
00418   ret = unw_init_remote_p (&cursor, as, this_frame);
00419 
00420   if (ret < 0)
00421     {
00422       unw_destroy_addr_space_p (as);
00423       return 0;
00424     }
00425 
00426   /* Check to see if we are in a signal frame.  */
00427   ret = unw_is_signal_frame_p (&cursor);
00428   unw_destroy_addr_space_p (as);
00429   if (ret > 0)
00430     return 1;
00431 
00432   return 0;
00433 }
00434 
00435 /* The following routine is for accessing special registers of the top frame.
00436    A special set of accessors must be given that work without frame info.
00437    This is used by ia64 to access the rse registers r32-r127.  While they
00438    are usually located at BOF, this is not always true and only the libunwind
00439    info can decipher where they actually are.  */
00440 int
00441 libunwind_get_reg_special (struct gdbarch *gdbarch, struct regcache *regcache,
00442                            int regnum, void *buf)
00443 {
00444   unw_cursor_t cursor;
00445   unw_accessors_t *acc;
00446   unw_addr_space_t as;
00447   struct libunwind_descr *descr;
00448   int ret;
00449   unw_regnum_t uw_regnum;
00450   unw_word_t intval;
00451   unw_fpreg_t fpval;
00452   void *ptr;
00453 
00454 
00455   descr = libunwind_descr (gdbarch);
00456   acc = descr->special_accessors;
00457   as =  unw_create_addr_space_p (acc,
00458                                  gdbarch_byte_order (gdbarch)
00459                                  == BFD_ENDIAN_BIG
00460                                  ? __BIG_ENDIAN
00461                                  : __LITTLE_ENDIAN);
00462 
00463   ret = unw_init_remote_p (&cursor, as, regcache);
00464   if (ret < 0)
00465     {
00466       unw_destroy_addr_space_p (as);
00467       return -1;
00468     }
00469 
00470   uw_regnum = descr->gdb2uw (regnum);
00471 
00472   if (descr->is_fpreg (uw_regnum))
00473     {
00474       ret = unw_get_fpreg_p (&cursor, uw_regnum, &fpval);
00475       ptr = &fpval;
00476     }
00477   else
00478     {
00479       ret = unw_get_reg_p (&cursor, uw_regnum, &intval);
00480       ptr = &intval;
00481     }
00482 
00483   unw_destroy_addr_space_p (as);
00484 
00485   if (ret < 0)
00486     return -1;
00487 
00488   if (buf)
00489     memcpy (buf, ptr, register_size (gdbarch, regnum));
00490 
00491   return 0;
00492 }
00493   
00494 static int
00495 libunwind_load (void)
00496 {
00497   void *handle;
00498   char *so_error = NULL;
00499 
00500   handle = dlopen (LIBUNWIND_SO, RTLD_NOW);
00501   if (handle == NULL)
00502     {
00503       so_error = xstrdup (dlerror ());
00504 #ifdef LIBUNWIND_SO_7
00505       handle = dlopen (LIBUNWIND_SO_7, RTLD_NOW);
00506 #endif /* LIBUNWIND_SO_7 */
00507     }
00508   if (handle == NULL)
00509     {
00510       fprintf_unfiltered (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
00511                           LIBUNWIND_SO, so_error);
00512 #ifdef LIBUNWIND_SO_7
00513       fprintf_unfiltered (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
00514                           LIBUNWIND_SO_7, dlerror ());
00515 #endif /* LIBUNWIND_SO_7 */
00516     }
00517   xfree (so_error);
00518   if (handle == NULL)
00519     return 0;
00520 
00521   /* Initialize pointers to the dynamic library functions we will use.  */
00522 
00523   unw_get_reg_p = dlsym (handle, get_reg_name);
00524   if (unw_get_reg_p == NULL)
00525     return 0;
00526 
00527   unw_get_fpreg_p = dlsym (handle, get_fpreg_name);
00528   if (unw_get_fpreg_p == NULL)
00529     return 0;
00530 
00531   unw_get_saveloc_p = dlsym (handle, get_saveloc_name);
00532   if (unw_get_saveloc_p == NULL)
00533     return 0;
00534 
00535   unw_is_signal_frame_p = dlsym (handle, is_signal_frame_name);
00536   if (unw_is_signal_frame_p == NULL)
00537     return 0;
00538 
00539   unw_step_p = dlsym (handle, step_name);
00540   if (unw_step_p == NULL)
00541     return 0;
00542 
00543   unw_init_remote_p = dlsym (handle, init_remote_name);
00544   if (unw_init_remote_p == NULL)
00545     return 0;
00546 
00547   unw_create_addr_space_p = dlsym (handle, create_addr_space_name);
00548   if (unw_create_addr_space_p == NULL)
00549     return 0;
00550 
00551   unw_destroy_addr_space_p = dlsym (handle, destroy_addr_space_name);
00552   if (unw_destroy_addr_space_p == NULL)
00553     return 0;
00554 
00555   unw_search_unwind_table_p = dlsym (handle, search_unwind_table_name);
00556   if (unw_search_unwind_table_p == NULL)
00557     return 0;
00558 
00559   unw_find_dyn_list_p = dlsym (handle, find_dyn_list_name);
00560   if (unw_find_dyn_list_p == NULL)
00561     return 0;
00562    
00563   return 1;
00564 }
00565 
00566 int
00567 libunwind_is_initialized (void)
00568 {
00569   return libunwind_initialized;
00570 }
00571 
00572 /* Provide a prototype to silence -Wmissing-prototypes.  */
00573 void _initialize_libunwind_frame (void);
00574 
00575 void
00576 _initialize_libunwind_frame (void)
00577 {
00578   libunwind_descr_handle
00579     = gdbarch_data_register_post_init (libunwind_descr_init);
00580 
00581   libunwind_initialized = libunwind_load ();
00582 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines