gdb/ia64-libunwind-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Frame unwinder for ia64 frames using the libunwind library.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Written by Jeff Johnston, contributed by Red Hat Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"

  16. #include "inferior.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "gdbcore.h"
  21. #include "gdbtypes.h"
  22. #include "symtab.h"
  23. #include "objfiles.h"
  24. #include "regcache.h"

  25. #include <dlfcn.h>

  26. #include "ia64-libunwind-tdep.h"

  27. #include "complaints.h"

  28. /* IA-64 is the only target that currently uses ia64-libunwind-tdep.
  29.    Note how UNW_TARGET, UNW_OBJ, etc. are compile time constants below.
  30.    Those come from libunwind's headers, and are target dependent.
  31.    Also, some of libunwind's typedefs are target dependent, as e.g.,
  32.    unw_word_t.  If some other target wants to use this, we will need
  33.    to do some abstracting in order to make it possible to select which
  34.    libunwind we're talking to at runtime (and have one per arch).  */

  35. /* The following two macros are normally defined in <endian.h>.
  36.    But systems such as ia64-hpux do not provide such header, so
  37.    we just define them here if not already defined.  */
  38. #ifndef __LITTLE_ENDIAN
  39. #define __LITTLE_ENDIAN 1234
  40. #endif
  41. #ifndef __BIG_ENDIAN
  42. #define __BIG_ENDIAN    4321
  43. #endif

  44. static int libunwind_initialized;
  45. static struct gdbarch_data *libunwind_descr_handle;

  46. /* Required function pointers from libunwind.  */
  47. static int (*unw_get_reg_p) (unw_cursor_t *, unw_regnum_t, unw_word_t *);
  48. static int (*unw_get_fpreg_p) (unw_cursor_t *, unw_regnum_t, unw_fpreg_t *);
  49. static int (*unw_get_saveloc_p) (unw_cursor_t *, unw_regnum_t,
  50.                                  unw_save_loc_t *);
  51. static int (*unw_is_signal_frame_p) (unw_cursor_t *);
  52. static int (*unw_step_p) (unw_cursor_t *);
  53. static int (*unw_init_remote_p) (unw_cursor_t *, unw_addr_space_t, void *);
  54. static unw_addr_space_t (*unw_create_addr_space_p) (unw_accessors_t *, int);
  55. static void (*unw_destroy_addr_space_p) (unw_addr_space_t);
  56. static int (*unw_search_unwind_table_p) (unw_addr_space_t, unw_word_t,
  57.                                          unw_dyn_info_t *,
  58.                                          unw_proc_info_t *, int, void *);
  59. static unw_word_t (*unw_find_dyn_list_p) (unw_addr_space_t, unw_dyn_info_t *,
  60.                                           void *);


  61. struct libunwind_frame_cache
  62. {
  63.   CORE_ADDR base;
  64.   CORE_ADDR func_addr;
  65.   unw_cursor_t cursor;
  66.   unw_addr_space_t as;
  67. };

  68. /* We need to qualify the function names with a platform-specific prefix
  69.    to match the names used by the libunwind library.  The UNW_OBJ macro is
  70.    provided by the libunwind.h header file.  */
  71. #define STRINGIFY2(name)        #name
  72. #define STRINGIFY(name)                STRINGIFY2(name)

  73. #ifndef LIBUNWIND_SO
  74. /* Use the stable ABI major version number.  `libunwind-ia64.so' is a link time
  75.    only library, not a runtime one.  */
  76. #define LIBUNWIND_SO "libunwind-" STRINGIFY(UNW_TARGET) ".so.8"

  77. /* Provide also compatibility with older .so.  The two APIs are compatible, .8
  78.    is only extended a bit, GDB does not use the extended API at all.  */
  79. #define LIBUNWIND_SO_7 "libunwind-" STRINGIFY(UNW_TARGET) ".so.7"
  80. #endif

  81. static char *get_reg_name = STRINGIFY(UNW_OBJ(get_reg));
  82. static char *get_fpreg_name = STRINGIFY(UNW_OBJ(get_fpreg));
  83. static char *get_saveloc_name = STRINGIFY(UNW_OBJ(get_save_loc));
  84. static char *is_signal_frame_name = STRINGIFY(UNW_OBJ(is_signal_frame));
  85. static char *step_name = STRINGIFY(UNW_OBJ(step));
  86. static char *init_remote_name = STRINGIFY(UNW_OBJ(init_remote));
  87. static char *create_addr_space_name = STRINGIFY(UNW_OBJ(create_addr_space));
  88. static char *destroy_addr_space_name = STRINGIFY(UNW_OBJ(destroy_addr_space));
  89. static char *search_unwind_table_name
  90.   = STRINGIFY(UNW_OBJ(search_unwind_table));
  91. static char *find_dyn_list_name = STRINGIFY(UNW_OBJ(find_dyn_list));

  92. static struct libunwind_descr *
  93. libunwind_descr (struct gdbarch *gdbarch)
  94. {
  95.   return gdbarch_data (gdbarch, libunwind_descr_handle);
  96. }

  97. static void *
  98. libunwind_descr_init (struct gdbarch *gdbarch)
  99. {
  100.   struct libunwind_descr *descr
  101.     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct libunwind_descr);

  102.   return descr;
  103. }

  104. void
  105. libunwind_frame_set_descr (struct gdbarch *gdbarch,
  106.                            struct libunwind_descr *descr)
  107. {
  108.   struct libunwind_descr *arch_descr;

  109.   gdb_assert (gdbarch != NULL);

  110.   arch_descr = gdbarch_data (gdbarch, libunwind_descr_handle);

  111.   if (arch_descr == NULL)
  112.     {
  113.       /* First time here.  Must initialize data area.  */
  114.       arch_descr = libunwind_descr_init (gdbarch);
  115.       deprecated_set_gdbarch_data (gdbarch,
  116.                                    libunwind_descr_handle, arch_descr);
  117.     }

  118.   /* Copy new descriptor info into arch descriptor.  */
  119.   arch_descr->gdb2uw = descr->gdb2uw;
  120.   arch_descr->uw2gdb = descr->uw2gdb;
  121.   arch_descr->is_fpreg = descr->is_fpreg;
  122.   arch_descr->accessors = descr->accessors;
  123.   arch_descr->special_accessors = descr->special_accessors;
  124. }

  125. static struct libunwind_frame_cache *
  126. libunwind_frame_cache (struct frame_info *this_frame, void **this_cache)
  127. {
  128.   unw_accessors_t *acc;
  129.   unw_addr_space_t as;
  130.   unw_word_t fp;
  131.   unw_regnum_t uw_sp_regnum;
  132.   struct libunwind_frame_cache *cache;
  133.   struct libunwind_descr *descr;
  134.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  135.   int i, ret;

  136.   if (*this_cache)
  137.     return *this_cache;

  138.   /* Allocate a new cache.  */
  139.   cache = FRAME_OBSTACK_ZALLOC (struct libunwind_frame_cache);

  140.   cache->func_addr = get_frame_func (this_frame);
  141.   if (cache->func_addr == 0)
  142.     /* This can happen when the frame corresponds to a function for which
  143.        there is no debugging information nor any entry in the symbol table.
  144.        This is probably a static function for which an entry in the symbol
  145.        table was not created when the objfile got linked (observed in
  146.        libpthread.so on ia64-hpux).

  147.        The best we can do, in that case, is use the frame PC as the function
  148.        address.  We don't need to give up since we still have the unwind
  149.        record to help us perform the unwinding.  There is also another
  150.        compelling to continue, because abandonning now means stopping
  151.        the backtrace, which can never be helpful for the user.  */
  152.     cache->func_addr = get_frame_pc (this_frame);

  153.   /* Get a libunwind cursor to the previous frame.

  154.      We do this by initializing a cursor.  Libunwind treats a new cursor
  155.      as the top of stack and will get the current register set via the
  156.      libunwind register accessor.  Now, we provide the platform-specific
  157.      accessors and we set up the register accessor to use the frame
  158.      register unwinding interfaces so that we properly get the registers
  159.      for the current frame rather than the top.  We then use the unw_step
  160.      function to move the libunwind cursor back one frame.  We can later
  161.      use this cursor to find previous registers via the unw_get_reg
  162.      interface which will invoke libunwind's special logic.  */
  163.   descr = libunwind_descr (gdbarch);
  164.   acc = descr->accessors;
  165.   as =  unw_create_addr_space_p (acc,
  166.                                  gdbarch_byte_order (gdbarch)
  167.                                  == BFD_ENDIAN_BIG
  168.                                  ? __BIG_ENDIAN
  169.                                  : __LITTLE_ENDIAN);

  170.   unw_init_remote_p (&cache->cursor, as, this_frame);
  171.   if (unw_step_p (&cache->cursor) < 0)
  172.     {
  173.       unw_destroy_addr_space_p (as);
  174.       return NULL;
  175.     }

  176.   /* To get base address, get sp from previous frame.  */
  177.   uw_sp_regnum = descr->gdb2uw (gdbarch_sp_regnum (gdbarch));
  178.   ret = unw_get_reg_p (&cache->cursor, uw_sp_regnum, &fp);
  179.   if (ret < 0)
  180.     {
  181.       unw_destroy_addr_space_p (as);
  182.       error (_("Can't get libunwind sp register."));
  183.     }

  184.   cache->base = (CORE_ADDR)fp;
  185.   cache->as = as;

  186.   *this_cache = cache;
  187.   return cache;
  188. }

  189. void
  190. libunwind_frame_dealloc_cache (struct frame_info *self, void *this_cache)
  191. {
  192.   struct libunwind_frame_cache *cache = this_cache;

  193.   if (cache->as)
  194.     unw_destroy_addr_space_p (cache->as);
  195. }

  196. unw_word_t
  197. libunwind_find_dyn_list (unw_addr_space_t as, unw_dyn_info_t *di, void *arg)
  198. {
  199.   return unw_find_dyn_list_p (as, di, arg);
  200. }

  201. /* Verify if there is sufficient libunwind information for the frame to use
  202.    libunwind frame unwinding.  */
  203. int
  204. libunwind_frame_sniffer (const struct frame_unwind *self,
  205.                          struct frame_info *this_frame, void **this_cache)
  206. {
  207.   unw_cursor_t cursor;
  208.   unw_accessors_t *acc;
  209.   unw_addr_space_t as;
  210.   struct libunwind_descr *descr;
  211.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  212.   int i, ret;

  213.   /* To test for libunwind unwind support, initialize a cursor to
  214.      the current frame and try to back up.  We use this same method
  215.      when setting up the frame cache (see libunwind_frame_cache()).
  216.      If libunwind returns success for this operation, it means that
  217.      it has found sufficient libunwind unwinding information to do so.  */

  218.   descr = libunwind_descr (gdbarch);
  219.   acc = descr->accessors;
  220.   as =  unw_create_addr_space_p (acc,
  221.                                  gdbarch_byte_order (gdbarch)
  222.                                  == BFD_ENDIAN_BIG
  223.                                  ? __BIG_ENDIAN
  224.                                  : __LITTLE_ENDIAN);

  225.   ret = unw_init_remote_p (&cursor, as, this_frame);

  226.   if (ret < 0)
  227.     {
  228.       unw_destroy_addr_space_p (as);
  229.       return 0;
  230.     }


  231.   /* Check to see if we have libunwind info by checking if we are in a
  232.      signal frame.  If it doesn't return an error, we have libunwind info
  233.      and can use libunwind.  */
  234.   ret = unw_is_signal_frame_p (&cursor);
  235.   unw_destroy_addr_space_p (as);

  236.   if (ret < 0)
  237.     return 0;

  238.   return 1;
  239. }

  240. void
  241. libunwind_frame_this_id (struct frame_info *this_frame, void **this_cache,
  242.                          struct frame_id *this_id)
  243. {
  244.   struct libunwind_frame_cache *cache =
  245.     libunwind_frame_cache (this_frame, this_cache);

  246.   if (cache != NULL)
  247.     (*this_id) = frame_id_build (cache->base, cache->func_addr);
  248. }

  249. struct value *
  250. libunwind_frame_prev_register (struct frame_info *this_frame,
  251.                                void **this_cache, int regnum)
  252. {
  253.   struct libunwind_frame_cache *cache =
  254.     libunwind_frame_cache (this_frame, this_cache);
  255.   struct gdbarch *gdbarch = get_frame_arch (this_frame);

  256.   void *ptr;
  257.   unw_cursor_t *c;
  258.   unw_save_loc_t sl;
  259.   int i, ret;
  260.   unw_word_t intval;
  261.   unw_fpreg_t fpval;
  262.   unw_regnum_t uw_regnum;
  263.   struct libunwind_descr *descr;
  264.   struct value *val = NULL;

  265.   if (cache == NULL)
  266.     return frame_unwind_got_constant (this_frame, regnum, 0);

  267.   /* Convert from gdb register number to libunwind register number.  */
  268.   descr = libunwind_descr (get_frame_arch (this_frame));
  269.   uw_regnum = descr->gdb2uw (regnum);

  270.   gdb_assert (regnum >= 0);

  271.   if (!target_has_registers)
  272.     error (_("No registers."));

  273.   if (uw_regnum < 0)
  274.     return frame_unwind_got_constant (this_frame, regnum, 0);

  275.   if (unw_get_saveloc_p (&cache->cursor, uw_regnum, &sl) < 0)
  276.     return frame_unwind_got_constant (this_frame, regnum, 0);

  277.   switch (sl.type)
  278.     {
  279.     case UNW_SLT_MEMORY:
  280.       val = frame_unwind_got_memory (this_frame, regnum, sl.u.addr);
  281.       break;

  282.     case UNW_SLT_REG:
  283.       val = frame_unwind_got_register (this_frame, regnum,
  284.                                        descr->uw2gdb (sl.u.regnum));
  285.       break;
  286.     case UNW_SLT_NONE:
  287.       {
  288.         /* The register is not stored at a specific memory address nor
  289.            inside another register.  So use libunwind to fetch the register
  290.            value for us, and create a constant value with the result.  */
  291.         if (descr->is_fpreg (uw_regnum))
  292.           {
  293.             ret = unw_get_fpreg_p (&cache->cursor, uw_regnum, &fpval);
  294.             if (ret < 0)
  295.               return frame_unwind_got_constant (this_frame, regnum, 0);
  296.             val = frame_unwind_got_bytes (this_frame, regnum,
  297.                                           (gdb_byte *) &fpval);
  298.           }
  299.         else
  300.           {
  301.             ret = unw_get_reg_p (&cache->cursor, uw_regnum, &intval);
  302.             if (ret < 0)
  303.               return frame_unwind_got_constant (this_frame, regnum, 0);
  304.             val = frame_unwind_got_constant (this_frame, regnum, intval);
  305.           }
  306.         break;
  307.       }
  308.     }

  309.   return val;
  310. }

  311. /* The following is a glue routine to call the libunwind unwind table
  312.    search function to get unwind information for a specified ip address.  */
  313. int
  314. libunwind_search_unwind_table (void *as, long ip, void *di,
  315.                                void *pi, int need_unwind_info, void *args)
  316. {
  317.   return unw_search_unwind_table_p (*(unw_addr_space_t *)as, (unw_word_t )ip,
  318.                                     di, pi, need_unwind_info, args);
  319. }

  320. /* Verify if we are in a sigtramp frame and we can use libunwind to unwind.  */
  321. int
  322. libunwind_sigtramp_frame_sniffer (const struct frame_unwind *self,
  323.                                   struct frame_info *this_frame,
  324.                                   void **this_cache)
  325. {
  326.   unw_cursor_t cursor;
  327.   unw_accessors_t *acc;
  328.   unw_addr_space_t as;
  329.   struct libunwind_descr *descr;
  330.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  331.   int i, ret;

  332.   /* To test for libunwind unwind support, initialize a cursor to the
  333.      current frame and try to back up.  We use this same method when
  334.      setting up the frame cache (see libunwind_frame_cache()).  If
  335.      libunwind returns success for this operation, it means that it
  336.      has found sufficient libunwind unwinding information to do
  337.      so.  */

  338.   descr = libunwind_descr (gdbarch);
  339.   acc = descr->accessors;
  340.   as =  unw_create_addr_space_p (acc,
  341.                                  gdbarch_byte_order (gdbarch)
  342.                                  == BFD_ENDIAN_BIG
  343.                                  ? __BIG_ENDIAN
  344.                                  : __LITTLE_ENDIAN);

  345.   ret = unw_init_remote_p (&cursor, as, this_frame);

  346.   if (ret < 0)
  347.     {
  348.       unw_destroy_addr_space_p (as);
  349.       return 0;
  350.     }

  351.   /* Check to see if we are in a signal frame.  */
  352.   ret = unw_is_signal_frame_p (&cursor);
  353.   unw_destroy_addr_space_p (as);
  354.   if (ret > 0)
  355.     return 1;

  356.   return 0;
  357. }

  358. /* The following routine is for accessing special registers of the top frame.
  359.    A special set of accessors must be given that work without frame info.
  360.    This is used by ia64 to access the rse registers r32-r127.  While they
  361.    are usually located at BOF, this is not always true and only the libunwind
  362.    info can decipher where they actually are.  */
  363. int
  364. libunwind_get_reg_special (struct gdbarch *gdbarch, struct regcache *regcache,
  365.                            int regnum, void *buf)
  366. {
  367.   unw_cursor_t cursor;
  368.   unw_accessors_t *acc;
  369.   unw_addr_space_t as;
  370.   struct libunwind_descr *descr;
  371.   int ret;
  372.   unw_regnum_t uw_regnum;
  373.   unw_word_t intval;
  374.   unw_fpreg_t fpval;
  375.   void *ptr;


  376.   descr = libunwind_descr (gdbarch);
  377.   acc = descr->special_accessors;
  378.   as =  unw_create_addr_space_p (acc,
  379.                                  gdbarch_byte_order (gdbarch)
  380.                                  == BFD_ENDIAN_BIG
  381.                                  ? __BIG_ENDIAN
  382.                                  : __LITTLE_ENDIAN);

  383.   ret = unw_init_remote_p (&cursor, as, regcache);
  384.   if (ret < 0)
  385.     {
  386.       unw_destroy_addr_space_p (as);
  387.       return -1;
  388.     }

  389.   uw_regnum = descr->gdb2uw (regnum);

  390.   if (descr->is_fpreg (uw_regnum))
  391.     {
  392.       ret = unw_get_fpreg_p (&cursor, uw_regnum, &fpval);
  393.       ptr = &fpval;
  394.     }
  395.   else
  396.     {
  397.       ret = unw_get_reg_p (&cursor, uw_regnum, &intval);
  398.       ptr = &intval;
  399.     }

  400.   unw_destroy_addr_space_p (as);

  401.   if (ret < 0)
  402.     return -1;

  403.   if (buf)
  404.     memcpy (buf, ptr, register_size (gdbarch, regnum));

  405.   return 0;
  406. }

  407. static int
  408. libunwind_load (void)
  409. {
  410.   void *handle;
  411.   char *so_error = NULL;

  412.   handle = dlopen (LIBUNWIND_SO, RTLD_NOW);
  413.   if (handle == NULL)
  414.     {
  415.       so_error = xstrdup (dlerror ());
  416. #ifdef LIBUNWIND_SO_7
  417.       handle = dlopen (LIBUNWIND_SO_7, RTLD_NOW);
  418. #endif /* LIBUNWIND_SO_7 */
  419.     }
  420.   if (handle == NULL)
  421.     {
  422.       fprintf_unfiltered (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
  423.                           LIBUNWIND_SO, so_error);
  424. #ifdef LIBUNWIND_SO_7
  425.       fprintf_unfiltered (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
  426.                           LIBUNWIND_SO_7, dlerror ());
  427. #endif /* LIBUNWIND_SO_7 */
  428.     }
  429.   xfree (so_error);
  430.   if (handle == NULL)
  431.     return 0;

  432.   /* Initialize pointers to the dynamic library functions we will use.  */

  433.   unw_get_reg_p = dlsym (handle, get_reg_name);
  434.   if (unw_get_reg_p == NULL)
  435.     return 0;

  436.   unw_get_fpreg_p = dlsym (handle, get_fpreg_name);
  437.   if (unw_get_fpreg_p == NULL)
  438.     return 0;

  439.   unw_get_saveloc_p = dlsym (handle, get_saveloc_name);
  440.   if (unw_get_saveloc_p == NULL)
  441.     return 0;

  442.   unw_is_signal_frame_p = dlsym (handle, is_signal_frame_name);
  443.   if (unw_is_signal_frame_p == NULL)
  444.     return 0;

  445.   unw_step_p = dlsym (handle, step_name);
  446.   if (unw_step_p == NULL)
  447.     return 0;

  448.   unw_init_remote_p = dlsym (handle, init_remote_name);
  449.   if (unw_init_remote_p == NULL)
  450.     return 0;

  451.   unw_create_addr_space_p = dlsym (handle, create_addr_space_name);
  452.   if (unw_create_addr_space_p == NULL)
  453.     return 0;

  454.   unw_destroy_addr_space_p = dlsym (handle, destroy_addr_space_name);
  455.   if (unw_destroy_addr_space_p == NULL)
  456.     return 0;

  457.   unw_search_unwind_table_p = dlsym (handle, search_unwind_table_name);
  458.   if (unw_search_unwind_table_p == NULL)
  459.     return 0;

  460.   unw_find_dyn_list_p = dlsym (handle, find_dyn_list_name);
  461.   if (unw_find_dyn_list_p == NULL)
  462.     return 0;

  463.   return 1;
  464. }

  465. int
  466. libunwind_is_initialized (void)
  467. {
  468.   return libunwind_initialized;
  469. }

  470. /* Provide a prototype to silence -Wmissing-prototypes.  */
  471. void _initialize_libunwind_frame (void);

  472. void
  473. _initialize_libunwind_frame (void)
  474. {
  475.   libunwind_descr_handle
  476.     = gdbarch_data_register_post_init (libunwind_descr_init);

  477.   libunwind_initialized = libunwind_load ();
  478. }