libunwind_i.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2005 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #ifndef X86_64_LIBUNWIND_I_H
  23. #define X86_64_LIBUNWIND_I_H
  24. /* Target-dependent definitions that are internal to libunwind but need
  25. to be shared with target-independent code. */
  26. #include <stdlib.h>
  27. #include <libunwind.h>
  28. #include "elf64.h"
  29. #include "mempool.h"
  30. #include "dwarf.h"
  31. typedef enum
  32. {
  33. UNW_X86_64_FRAME_STANDARD = -2, /* regular rbp, rsp +/- offset */
  34. UNW_X86_64_FRAME_SIGRETURN = -1, /* special sigreturn frame */
  35. UNW_X86_64_FRAME_OTHER = 0, /* not cacheable (special or unrecognised) */
  36. UNW_X86_64_FRAME_GUESSED = 1 /* guessed it was regular, but not known */
  37. }
  38. unw_tdep_frame_type_t;
  39. typedef struct
  40. {
  41. uint64_t virtual_address;
  42. int64_t frame_type : 2; /* unw_tdep_frame_type_t classification */
  43. int64_t last_frame : 1; /* non-zero if last frame in chain */
  44. int64_t cfa_reg_rsp : 1; /* cfa dwarf base register is rsp vs. rbp */
  45. int64_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
  46. int64_t rbp_cfa_offset : 15; /* rbp saved at this offset from cfa (-1 = not saved) */
  47. int64_t rsp_cfa_offset : 15; /* rsp saved at this offset from cfa (-1 = not saved) */
  48. }
  49. unw_tdep_frame_t;
  50. struct unw_addr_space
  51. {
  52. struct unw_accessors acc;
  53. unw_caching_policy_t caching_policy;
  54. #ifdef HAVE_ATOMIC_OPS_H
  55. AO_t cache_generation;
  56. #else
  57. uint32_t cache_generation;
  58. #endif
  59. unw_word_t dyn_generation; /* see dyn-common.h */
  60. unw_word_t dyn_info_list_addr; /* (cached) dyn_info_list_addr */
  61. struct dwarf_rs_cache global_cache;
  62. struct unw_debug_frame_list *debug_frames;
  63. };
  64. struct cursor
  65. {
  66. struct dwarf_cursor dwarf; /* must be first */
  67. unw_tdep_frame_t frame_info; /* quick tracing assist info */
  68. /* Format of sigcontext structure and address at which it is
  69. stored: */
  70. enum
  71. {
  72. X86_64_SCF_NONE, /* no signal frame encountered */
  73. X86_64_SCF_LINUX_RT_SIGFRAME, /* Linux ucontext_t */
  74. X86_64_SCF_FREEBSD_SIGFRAME, /* FreeBSD signal frame */
  75. X86_64_SCF_FREEBSD_SYSCALL, /* FreeBSD syscall */
  76. }
  77. sigcontext_format;
  78. unw_word_t sigcontext_addr;
  79. int validate;
  80. ucontext_t *uc;
  81. };
  82. static inline ucontext_t *
  83. dwarf_get_uc(const struct dwarf_cursor *cursor)
  84. {
  85. const struct cursor *c = (struct cursor *) cursor->as_arg;
  86. return c->uc;
  87. }
  88. #define DWARF_GET_LOC(l) ((l).val)
  89. #ifdef UNW_LOCAL_ONLY
  90. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  91. # define DWARF_IS_NULL_LOC(l) (DWARF_GET_LOC (l) == 0)
  92. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r) })
  93. # define DWARF_IS_REG_LOC(l) 0
  94. # define DWARF_REG_LOC(c,r) (DWARF_LOC((unw_word_t) \
  95. x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
  96. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  97. # define DWARF_FPREG_LOC(c,r) (DWARF_LOC((unw_word_t) \
  98. x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
  99. #else /* !UNW_LOCAL_ONLY */
  100. # define DWARF_LOC_TYPE_FP (1 << 0)
  101. # define DWARF_LOC_TYPE_REG (1 << 1)
  102. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  103. # define DWARF_IS_NULL_LOC(l) \
  104. ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
  105. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r), .type = (t) })
  106. # define DWARF_IS_REG_LOC(l) (((l).type & DWARF_LOC_TYPE_REG) != 0)
  107. # define DWARF_IS_FP_LOC(l) (((l).type & DWARF_LOC_TYPE_FP) != 0)
  108. # define DWARF_REG_LOC(c,r) DWARF_LOC((r), DWARF_LOC_TYPE_REG)
  109. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  110. # define DWARF_FPREG_LOC(c,r) DWARF_LOC((r), (DWARF_LOC_TYPE_REG \
  111. | DWARF_LOC_TYPE_FP))
  112. #endif /* !UNW_LOCAL_ONLY */
  113. static inline int
  114. dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
  115. {
  116. if (DWARF_IS_NULL_LOC (loc))
  117. return -UNW_EBADREG;
  118. abort ();
  119. }
  120. static inline int
  121. dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
  122. {
  123. if (DWARF_IS_NULL_LOC (loc))
  124. return -UNW_EBADREG;
  125. abort ();
  126. }
  127. static inline int
  128. dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
  129. {
  130. if (DWARF_IS_NULL_LOC (loc))
  131. return -UNW_EBADREG;
  132. if (DWARF_IS_REG_LOC (loc))
  133. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
  134. 0, c->as_arg);
  135. else
  136. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
  137. 0, c->as_arg);
  138. }
  139. static inline int
  140. dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
  141. {
  142. if (DWARF_IS_NULL_LOC (loc))
  143. return -UNW_EBADREG;
  144. if (DWARF_IS_REG_LOC (loc))
  145. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
  146. 1, c->as_arg);
  147. else
  148. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
  149. 1, c->as_arg);
  150. }
  151. #define tdep_getcontext_trace UNW_ARCH_OBJ(getcontext_trace)
  152. #define tdep_needs_initialization UNW_OBJ(needs_initialization)
  153. #define tdep_init_mem_validate UNW_OBJ(init_mem_validate)
  154. #define tdep_init UNW_OBJ(init)
  155. /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
  156. tdep_search_unwind_table. */
  157. #define tdep_search_unwind_table dwarf_search_unwind_table
  158. #define tdep_find_unwind_table dwarf_find_unwind_table
  159. #define tdep_get_elf_image UNW_ARCH_OBJ(get_elf_image)
  160. #define tdep_access_reg UNW_OBJ(access_reg)
  161. #define tdep_access_fpreg UNW_OBJ(access_fpreg)
  162. #if __linux__
  163. # define tdep_fetch_frame UNW_OBJ(fetch_frame)
  164. # define tdep_cache_frame UNW_OBJ(cache_frame)
  165. # define tdep_reuse_frame UNW_OBJ(reuse_frame)
  166. #else
  167. # define tdep_fetch_frame(c,ip,n) do {} while(0)
  168. # define tdep_cache_frame(c,rs) do {} while(0)
  169. # define tdep_reuse_frame(c,rs) do {} while(0)
  170. #endif
  171. #define tdep_stash_frame UNW_OBJ(stash_frame)
  172. #if HAVE_SGX
  173. #define tdep_trace(cur,addr,n) (-UNW_ENOINFO)
  174. #else
  175. #define tdep_trace UNW_OBJ(tdep_trace)
  176. #endif
  177. #define x86_64_r_uc_addr UNW_OBJ(r_uc_addr)
  178. #ifdef UNW_LOCAL_ONLY
  179. # define tdep_find_proc_info(c,ip,n) \
  180. dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n), \
  181. (c)->as_arg)
  182. # define tdep_put_unwind_info(as,pi,arg) \
  183. dwarf_put_unwind_info((as), (pi), (arg))
  184. #else
  185. # define tdep_find_proc_info(c,ip,n) \
  186. (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n), \
  187. (c)->as_arg)
  188. # define tdep_put_unwind_info(as,pi,arg) \
  189. (*(as)->acc.put_unwind_info)((as), (pi), (arg))
  190. #endif
  191. #define tdep_get_as(c) ((c)->dwarf.as)
  192. #define tdep_get_as_arg(c) ((c)->dwarf.as_arg)
  193. #define tdep_get_ip(c) ((c)->dwarf.ip)
  194. #define tdep_big_endian(as) 0
  195. extern int tdep_needs_initialization;
  196. extern void tdep_init (void);
  197. extern void tdep_init_mem_validate (void);
  198. extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
  199. unw_dyn_info_t *di, unw_proc_info_t *pi,
  200. int need_unwind_info, void *arg);
  201. extern void *x86_64_r_uc_addr (ucontext_t *uc, int reg);
  202. extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
  203. unsigned long *segbase, unsigned long *mapoff,
  204. char *path, size_t pathlen);
  205. extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
  206. unw_word_t *valp, int write);
  207. extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
  208. unw_fpreg_t *valp, int write);
  209. #if __linux__
  210. extern void tdep_fetch_frame (struct dwarf_cursor *c, unw_word_t ip,
  211. int need_unwind_info);
  212. extern void tdep_cache_frame (struct dwarf_cursor *c,
  213. struct dwarf_reg_state *rs);
  214. extern void tdep_reuse_frame (struct dwarf_cursor *c,
  215. struct dwarf_reg_state *rs);
  216. extern void tdep_stash_frame (struct dwarf_cursor *c,
  217. struct dwarf_reg_state *rs);
  218. #endif
  219. extern int tdep_getcontext_trace (unw_tdep_context_t *);
  220. #if (!HAVE_SGX)
  221. extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
  222. #endif
  223. #endif /* X86_64_LIBUNWIND_I_H */