1/* Frame unwinder for frames using the libunwind library.
2
3   Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
4
5   Written by Jeff Johnston, contributed by Red Hat Inc.
6
7   This file is part of GDB.
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 3 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22#include "defs.h"
23
24#include "inferior.h"
25#include "frame.h"
26#include "frame-base.h"
27#include "frame-unwind.h"
28#include "gdbcore.h"
29#include "gdbtypes.h"
30#include "symtab.h"
31#include "objfiles.h"
32#include "regcache.h"
33
34#include <dlfcn.h>
35
36#include "gdb_assert.h"
37#include "gdb_string.h"
38
39#include "libunwind-frame.h"
40
41#include "complaints.h"
42
43static int libunwind_initialized;
44static struct gdbarch_data *libunwind_descr_handle;
45
46/* Required function pointers from libunwind.  */
47static int (*unw_get_reg_p) (unw_cursor_t *, unw_regnum_t, unw_word_t *);
48static int (*unw_get_fpreg_p) (unw_cursor_t *, unw_regnum_t, unw_fpreg_t *);
49static int (*unw_get_saveloc_p) (unw_cursor_t *, unw_regnum_t, unw_save_loc_t *);
50static int (*unw_is_signal_frame_p) (unw_cursor_t *);
51static int (*unw_step_p) (unw_cursor_t *);
52static int (*unw_init_remote_p) (unw_cursor_t *, unw_addr_space_t, void *);
53static unw_addr_space_t (*unw_create_addr_space_p) (unw_accessors_t *, int);
54static void (*unw_destroy_addr_space_p) (unw_addr_space_t);
55static int (*unw_search_unwind_table_p) (unw_addr_space_t, unw_word_t, unw_dyn_info_t *,
56					 unw_proc_info_t *, int, void *);
57static unw_word_t (*unw_find_dyn_list_p) (unw_addr_space_t, unw_dyn_info_t *,
58					  void *);
59
60
61struct 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
69/* We need to qualify the function names with a platform-specific prefix to match
70   the names used by the libunwind library.  The UNW_OBJ macro is provided by the
71   libunwind.h header file.  */
72#define STRINGIFY2(name)	#name
73#define STRINGIFY(name)		STRINGIFY2(name)
74
75#ifndef LIBUNWIND_SO
76/* Use the stable ABI major version number.  `libunwind-ia64.so' is a link time
77   only library, not a runtime one.  */
78#define LIBUNWIND_SO "libunwind-" STRINGIFY(UNW_TARGET) ".so.7"
79#endif
80
81static char *get_reg_name = STRINGIFY(UNW_OBJ(get_reg));
82static char *get_fpreg_name = STRINGIFY(UNW_OBJ(get_fpreg));
83static char *get_saveloc_name = STRINGIFY(UNW_OBJ(get_save_loc));
84static char *is_signal_frame_name = STRINGIFY(UNW_OBJ(is_signal_frame));
85static char *step_name = STRINGIFY(UNW_OBJ(step));
86static char *init_remote_name = STRINGIFY(UNW_OBJ(init_remote));
87static char *create_addr_space_name = STRINGIFY(UNW_OBJ(create_addr_space));
88static char *destroy_addr_space_name = STRINGIFY(UNW_OBJ(destroy_addr_space));
89static char *search_unwind_table_name = STRINGIFY(UNW_OBJ(search_unwind_table));
90static char *find_dyn_list_name = STRINGIFY(UNW_OBJ(find_dyn_list));
91
92static struct libunwind_descr *
93libunwind_descr (struct gdbarch *gdbarch)
94{
95  return gdbarch_data (gdbarch, libunwind_descr_handle);
96}
97
98static void *
99libunwind_descr_init (struct gdbarch *gdbarch)
100{
101  struct libunwind_descr *descr = GDBARCH_OBSTACK_ZALLOC (gdbarch,
102							  struct libunwind_descr);
103  return descr;
104}
105
106void
107libunwind_frame_set_descr (struct gdbarch *gdbarch, struct libunwind_descr *descr)
108{
109  struct libunwind_descr *arch_descr;
110
111  gdb_assert (gdbarch != NULL);
112
113  arch_descr = gdbarch_data (gdbarch, libunwind_descr_handle);
114
115  if (arch_descr == NULL)
116    {
117      /* First time here.  Must initialize data area.  */
118      arch_descr = libunwind_descr_init (gdbarch);
119      deprecated_set_gdbarch_data (gdbarch, libunwind_descr_handle, arch_descr);
120    }
121
122  /* Copy new descriptor info into arch descriptor.  */
123  arch_descr->gdb2uw = descr->gdb2uw;
124  arch_descr->uw2gdb = descr->uw2gdb;
125  arch_descr->is_fpreg = descr->is_fpreg;
126  arch_descr->accessors = descr->accessors;
127  arch_descr->special_accessors = descr->special_accessors;
128}
129
130static struct libunwind_frame_cache *
131libunwind_frame_cache (struct frame_info *next_frame, void **this_cache)
132{
133  unw_accessors_t *acc;
134  unw_addr_space_t as;
135  unw_word_t fp;
136  unw_regnum_t uw_sp_regnum;
137  struct libunwind_frame_cache *cache;
138  struct libunwind_descr *descr;
139  int i, ret;
140
141  if (*this_cache)
142    return *this_cache;
143
144  /* Allocate a new cache.  */
145  cache = FRAME_OBSTACK_ZALLOC (struct libunwind_frame_cache);
146
147  /* We can assume we are unwinding a normal frame.  Even if this is
148     for a signal trampoline, ia64 signal "trampolines" use a normal
149     subroutine call to start the signal handler.  */
150  cache->func_addr = frame_func_unwind (next_frame, NORMAL_FRAME);
151  if (cache->func_addr == 0
152      && frame_relative_level (next_frame) > 0
153      && get_frame_type (next_frame) != SIGTRAMP_FRAME)
154    return NULL;
155
156  /* Get a libunwind cursor to the previous frame.  We do this by initializing
157     a cursor.  Libunwind treats a new cursor as the top of stack and will get
158     the current register set via the libunwind register accessor.  Now, we
159     provide the platform-specific accessors and we set up the register accessor to use
160     the frame register unwinding interfaces so that we properly get the registers for
161     the current frame rather than the top.  We then use the  unw_step function to
162     move the libunwind cursor back one frame.  We can later use this cursor to find previous
163     registers via the unw_get_reg interface which will invoke libunwind's special logic.  */
164  descr = libunwind_descr (get_frame_arch (next_frame));
165  acc = descr->accessors;
166  as =  unw_create_addr_space_p (acc,
167				 gdbarch_byte_order (current_gdbarch)
168				 == BFD_ENDIAN_BIG
169				 ? __BIG_ENDIAN
170				 : __LITTLE_ENDIAN);
171
172  unw_init_remote_p (&cache->cursor, as, next_frame);
173  if (unw_step_p (&cache->cursor) < 0)
174    {
175      unw_destroy_addr_space_p (as);
176      return NULL;
177    }
178
179  /* To get base address, get sp from previous frame.  */
180  uw_sp_regnum = descr->gdb2uw (gdbarch_sp_regnum (current_gdbarch));
181  ret = unw_get_reg_p (&cache->cursor, uw_sp_regnum, &fp);
182  if (ret < 0)
183    {
184      unw_destroy_addr_space_p (as);
185      error (_("Can't get libunwind sp register."));
186    }
187
188  cache->base = (CORE_ADDR)fp;
189  cache->as = as;
190
191  *this_cache = cache;
192  return cache;
193}
194
195void
196libunwind_frame_dealloc_cache (struct frame_info *self, void *this_cache)
197{
198  struct libunwind_frame_cache *cache = this_cache;
199  if (cache->as)
200    unw_destroy_addr_space_p (cache->as);
201}
202
203unw_word_t
204libunwind_find_dyn_list (unw_addr_space_t as, unw_dyn_info_t *di, void *arg)
205{
206  return unw_find_dyn_list_p (as, di, arg);
207}
208
209static const struct frame_unwind libunwind_frame_unwind =
210{
211  NORMAL_FRAME,
212  libunwind_frame_this_id,
213  libunwind_frame_prev_register,
214  NULL,
215  NULL,
216  NULL,
217  libunwind_frame_dealloc_cache
218};
219
220/* Verify if there is sufficient libunwind information for the frame to use
221   libunwind frame unwinding.  */
222const struct frame_unwind *
223libunwind_frame_sniffer (struct frame_info *next_frame)
224{
225  unw_cursor_t cursor;
226  unw_accessors_t *acc;
227  unw_addr_space_t as;
228  struct libunwind_descr *descr;
229  int i, ret;
230
231  /* To test for libunwind unwind support, initialize a cursor to the current frame and try to back
232     up.  We use this same method when setting up the frame cache (see libunwind_frame_cache()).
233     If libunwind returns success for this operation, it means that it has found sufficient
234     libunwind unwinding information to do so.  */
235
236  descr = libunwind_descr (get_frame_arch (next_frame));
237  acc = descr->accessors;
238  as =  unw_create_addr_space_p (acc,
239				 gdbarch_byte_order (current_gdbarch)
240				 == BFD_ENDIAN_BIG
241				 ? __BIG_ENDIAN
242				 : __LITTLE_ENDIAN);
243
244  ret = unw_init_remote_p (&cursor, as, next_frame);
245
246  if (ret < 0)
247    {
248      unw_destroy_addr_space_p (as);
249      return NULL;
250    }
251
252
253  /* Check to see if we have libunwind info by checking if we are in a
254     signal frame.  If it doesn't return an error, we have libunwind info
255     and can use libunwind.  */
256  ret = unw_is_signal_frame_p (&cursor);
257  unw_destroy_addr_space_p (as);
258
259  if (ret < 0)
260    return NULL;
261
262  return &libunwind_frame_unwind;
263}
264
265void
266libunwind_frame_this_id (struct frame_info *next_frame, void **this_cache,
267		      struct frame_id *this_id)
268{
269  struct libunwind_frame_cache *cache =
270    libunwind_frame_cache (next_frame, this_cache);
271
272  if (cache != NULL)
273    (*this_id) = frame_id_build (cache->base, cache->func_addr);
274  else
275    (*this_id) = null_frame_id;
276}
277
278void
279libunwind_frame_prev_register (struct frame_info *next_frame, void **this_cache,
280			       int regnum, int *optimizedp,
281			       enum lval_type *lvalp, CORE_ADDR *addrp,
282			       int *realnump, gdb_byte *valuep)
283{
284  struct libunwind_frame_cache *cache =
285    libunwind_frame_cache (next_frame, this_cache);
286
287  void *ptr;
288  unw_cursor_t *c;
289  unw_save_loc_t sl;
290  int i, ret;
291  unw_word_t intval;
292  unw_fpreg_t fpval;
293  unw_regnum_t uw_regnum;
294  struct libunwind_descr *descr;
295
296  if (cache == NULL)
297    return;
298
299  /* Convert from gdb register number to libunwind register number.  */
300  descr = libunwind_descr (get_frame_arch (next_frame));
301  uw_regnum = descr->gdb2uw (regnum);
302
303  gdb_assert (regnum >= 0);
304
305  if (!target_has_registers)
306    error (_("No registers."));
307
308  *optimizedp = 0;
309  *addrp = 0;
310  *lvalp = not_lval;
311  *realnump = -1;
312
313  if (valuep)
314    memset (valuep, 0, register_size (current_gdbarch, regnum));
315
316  if (uw_regnum < 0)
317    return;
318
319  /* To get the previous register, we use the libunwind register APIs with
320     the cursor we have already pushed back to the previous frame.  */
321
322  if (descr->is_fpreg (uw_regnum))
323    {
324      ret = unw_get_fpreg_p (&cache->cursor, uw_regnum, &fpval);
325      ptr = &fpval;
326    }
327  else
328    {
329      ret = unw_get_reg_p (&cache->cursor, uw_regnum, &intval);
330      ptr = &intval;
331    }
332
333  if (ret < 0)
334    return;
335
336  if (valuep)
337    memcpy (valuep, ptr, register_size (current_gdbarch, regnum));
338
339  if (unw_get_saveloc_p (&cache->cursor, uw_regnum, &sl) < 0)
340    return;
341
342  switch (sl.type)
343    {
344    case UNW_SLT_NONE:
345      *optimizedp = 1;
346      break;
347
348    case UNW_SLT_MEMORY:
349      *lvalp = lval_memory;
350      *addrp = sl.u.addr;
351      break;
352
353    case UNW_SLT_REG:
354      *lvalp = lval_register;
355      *realnump = regnum;
356      break;
357    }
358}
359
360CORE_ADDR
361libunwind_frame_base_address (struct frame_info *next_frame, void **this_cache)
362{
363  struct libunwind_frame_cache *cache =
364    libunwind_frame_cache (next_frame, this_cache);
365
366  if (cache == NULL)
367    return (CORE_ADDR)NULL;
368  return cache->base;
369}
370
371/* The following is a glue routine to call the libunwind unwind table
372   search function to get unwind information for a specified ip address.  */
373int
374libunwind_search_unwind_table (void *as, long ip, void *di,
375			       void *pi, int need_unwind_info, void *args)
376{
377  return unw_search_unwind_table_p (*(unw_addr_space_t *)as, (unw_word_t )ip,
378				    di, pi, need_unwind_info, args);
379}
380
381/* Verify if we are in a sigtramp frame and we can use libunwind to unwind.  */
382const struct frame_unwind *
383libunwind_sigtramp_frame_sniffer (struct frame_info *next_frame)
384{
385  unw_cursor_t cursor;
386  unw_accessors_t *acc;
387  unw_addr_space_t as;
388  struct libunwind_descr *descr;
389  int i, ret;
390
391  /* To test for libunwind unwind support, initialize a cursor to the
392     current frame and try to back up.  We use this same method when
393     setting up the frame cache (see libunwind_frame_cache()).  If
394     libunwind returns success for this operation, it means that it
395     has found sufficient libunwind unwinding information to do
396     so.  */
397
398  descr = libunwind_descr (get_frame_arch (next_frame));
399  acc = descr->accessors;
400  as =  unw_create_addr_space_p (acc,
401				 gdbarch_byte_order (current_gdbarch)
402				 == BFD_ENDIAN_BIG
403				 ? __BIG_ENDIAN
404				 : __LITTLE_ENDIAN);
405
406  ret = unw_init_remote_p (&cursor, as, next_frame);
407
408  if (ret < 0)
409    {
410      unw_destroy_addr_space_p (as);
411      return NULL;
412    }
413
414  /* Check to see if we are in a signal frame.  */
415  ret = unw_is_signal_frame_p (&cursor);
416  unw_destroy_addr_space_p (as);
417  if (ret > 0)
418    return &libunwind_frame_unwind;
419
420  return NULL;
421}
422
423/* The following routine is for accessing special registers of the top frame.
424   A special set of accessors must be given that work without frame info.
425   This is used by ia64 to access the rse registers r32-r127.  While they
426   are usually located at BOF, this is not always true and only the libunwind
427   info can decipher where they actually are.  */
428int
429libunwind_get_reg_special (struct gdbarch *gdbarch, struct regcache *regcache,
430			   int regnum, void *buf)
431{
432  unw_cursor_t cursor;
433  unw_accessors_t *acc;
434  unw_addr_space_t as;
435  struct libunwind_descr *descr;
436  int ret;
437  unw_regnum_t uw_regnum;
438  unw_word_t intval;
439  unw_fpreg_t fpval;
440  void *ptr;
441
442
443  descr = libunwind_descr (gdbarch);
444  acc = descr->special_accessors;
445  as =  unw_create_addr_space_p (acc,
446				 gdbarch_byte_order (current_gdbarch)
447				 == BFD_ENDIAN_BIG
448				 ? __BIG_ENDIAN
449				 : __LITTLE_ENDIAN);
450
451  ret = unw_init_remote_p (&cursor, as, regcache);
452  if (ret < 0)
453    {
454      unw_destroy_addr_space_p (as);
455      return -1;
456    }
457
458  uw_regnum = descr->gdb2uw (regnum);
459
460  if (descr->is_fpreg (uw_regnum))
461    {
462      ret = unw_get_fpreg_p (&cursor, uw_regnum, &fpval);
463      ptr = &fpval;
464    }
465  else
466    {
467      ret = unw_get_reg_p (&cursor, uw_regnum, &intval);
468      ptr = &intval;
469    }
470
471  unw_destroy_addr_space_p (as);
472
473  if (ret < 0)
474    return -1;
475
476  if (buf)
477    memcpy (buf, ptr, register_size (current_gdbarch, regnum));
478
479  return 0;
480}
481
482static int
483libunwind_load (void)
484{
485  void *handle;
486
487  handle = dlopen (LIBUNWIND_SO, RTLD_NOW);
488  if (handle == NULL)
489    return 0;
490
491  /* Initialize pointers to the dynamic library functions we will use.  */
492
493  unw_get_reg_p = dlsym (handle, get_reg_name);
494  if (unw_get_reg_p == NULL)
495    return 0;
496
497  unw_get_fpreg_p = dlsym (handle, get_fpreg_name);
498  if (unw_get_fpreg_p == NULL)
499    return 0;
500
501  unw_get_saveloc_p = dlsym (handle, get_saveloc_name);
502  if (unw_get_saveloc_p == NULL)
503    return 0;
504
505  unw_is_signal_frame_p = dlsym (handle, is_signal_frame_name);
506  if (unw_is_signal_frame_p == NULL)
507    return 0;
508
509  unw_step_p = dlsym (handle, step_name);
510  if (unw_step_p == NULL)
511    return 0;
512
513  unw_init_remote_p = dlsym (handle, init_remote_name);
514  if (unw_init_remote_p == NULL)
515    return 0;
516
517  unw_create_addr_space_p = dlsym (handle, create_addr_space_name);
518  if (unw_create_addr_space_p == NULL)
519    return 0;
520
521  unw_destroy_addr_space_p = dlsym (handle, destroy_addr_space_name);
522  if (unw_destroy_addr_space_p == NULL)
523    return 0;
524
525  unw_search_unwind_table_p = dlsym (handle, search_unwind_table_name);
526  if (unw_search_unwind_table_p == NULL)
527    return 0;
528
529  unw_find_dyn_list_p = dlsym (handle, find_dyn_list_name);
530  if (unw_find_dyn_list_p == NULL)
531    return 0;
532
533  return 1;
534}
535
536int
537libunwind_is_initialized (void)
538{
539  return libunwind_initialized;
540}
541
542/* Provide a prototype to silence -Wmissing-prototypes.  */
543void _initialize_libunwind_frame (void);
544
545void
546_initialize_libunwind_frame (void)
547{
548  libunwind_descr_handle = gdbarch_data_register_post_init (libunwind_descr_init);
549
550  libunwind_initialized = libunwind_load ();
551}
552