libunwind_i.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #ifndef X86_LIBUNWIND_I_H
  22. #define X86_LIBUNWIND_I_H
  23. /* Target-dependent definitions that are internal to libunwind but need
  24. to be shared with target-independent code. */
  25. #include <stdlib.h>
  26. #include <libunwind.h>
  27. #include "elf32.h"
  28. #include "mempool.h"
  29. #include "dwarf.h"
  30. typedef struct
  31. {
  32. /* no x86-specific fast trace */
  33. }
  34. unw_tdep_frame_t;
  35. struct unw_addr_space
  36. {
  37. struct unw_accessors acc;
  38. unw_caching_policy_t caching_policy;
  39. #ifdef HAVE_ATOMIC_OPS_H
  40. AO_t cache_generation;
  41. #else
  42. uint32_t cache_generation;
  43. #endif
  44. unw_word_t dyn_generation; /* see dyn-common.h */
  45. unw_word_t dyn_info_list_addr; /* (cached) dyn_info_list_addr */
  46. struct dwarf_rs_cache global_cache;
  47. struct unw_debug_frame_list *debug_frames;
  48. };
  49. struct cursor
  50. {
  51. struct dwarf_cursor dwarf; /* must be first */
  52. /* Format of sigcontext structure and address at which it is
  53. stored: */
  54. enum
  55. {
  56. X86_SCF_NONE, /* no signal frame encountered */
  57. X86_SCF_LINUX_SIGFRAME, /* Linux x86 sigcontext */
  58. X86_SCF_LINUX_RT_SIGFRAME, /* POSIX ucontext_t */
  59. X86_SCF_FREEBSD_SIGFRAME, /* FreeBSD x86 sigcontext */
  60. X86_SCF_FREEBSD_SIGFRAME4, /* FreeBSD 4.x x86 sigcontext */
  61. X86_SCF_FREEBSD_OSIGFRAME, /* FreeBSD pre-4.x x86 sigcontext */
  62. X86_SCF_FREEBSD_SYSCALL, /* FreeBSD x86 syscall */
  63. }
  64. sigcontext_format;
  65. unw_word_t sigcontext_addr;
  66. int validate;
  67. ucontext_t *uc;
  68. };
  69. static inline ucontext_t *
  70. dwarf_get_uc(const struct dwarf_cursor *cursor)
  71. {
  72. const struct cursor *c = (struct cursor *) cursor->as_arg;
  73. return c->uc;
  74. }
  75. #define DWARF_GET_LOC(l) ((l).val)
  76. #ifdef UNW_LOCAL_ONLY
  77. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  78. # define DWARF_IS_NULL_LOC(l) (DWARF_GET_LOC (l) == 0)
  79. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r) })
  80. # define DWARF_IS_REG_LOC(l) 0
  81. # define DWARF_REG_LOC(c,r) (DWARF_LOC((unw_word_t) \
  82. tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
  83. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  84. # define DWARF_FPREG_LOC(c,r) (DWARF_LOC((unw_word_t) \
  85. tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
  86. static inline int
  87. dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
  88. {
  89. if (!DWARF_GET_LOC (loc))
  90. return -1;
  91. *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
  92. return 0;
  93. }
  94. static inline int
  95. dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
  96. {
  97. if (!DWARF_GET_LOC (loc))
  98. return -1;
  99. *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
  100. return 0;
  101. }
  102. static inline int
  103. dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
  104. {
  105. if (!DWARF_GET_LOC (loc))
  106. return -1;
  107. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
  108. 0, c->as_arg);
  109. }
  110. static inline int
  111. dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
  112. {
  113. if (!DWARF_GET_LOC (loc))
  114. return -1;
  115. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
  116. 1, c->as_arg);
  117. }
  118. #else /* !UNW_LOCAL_ONLY */
  119. # define DWARF_LOC_TYPE_FP (1 << 0)
  120. # define DWARF_LOC_TYPE_REG (1 << 1)
  121. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  122. # define DWARF_IS_NULL_LOC(l) \
  123. ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
  124. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r), .type = (t) })
  125. # define DWARF_IS_REG_LOC(l) (((l).type & DWARF_LOC_TYPE_REG) != 0)
  126. # define DWARF_IS_FP_LOC(l) (((l).type & DWARF_LOC_TYPE_FP) != 0)
  127. # define DWARF_REG_LOC(c,r) DWARF_LOC((r), DWARF_LOC_TYPE_REG)
  128. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  129. # define DWARF_FPREG_LOC(c,r) DWARF_LOC((r), (DWARF_LOC_TYPE_REG \
  130. | DWARF_LOC_TYPE_FP))
  131. static inline int
  132. dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
  133. {
  134. char *valp = (char *) &val;
  135. unw_word_t addr;
  136. int ret;
  137. if (DWARF_IS_NULL_LOC (loc))
  138. return -UNW_EBADREG;
  139. if (DWARF_IS_REG_LOC (loc))
  140. return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
  141. val, 0, c->as_arg);
  142. addr = DWARF_GET_LOC (loc);
  143. if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
  144. 0, c->as_arg)) < 0)
  145. return ret;
  146. return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
  147. c->as_arg);
  148. }
  149. static inline int
  150. dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
  151. {
  152. char *valp = (char *) &val;
  153. unw_word_t addr;
  154. int ret;
  155. if (DWARF_IS_NULL_LOC (loc))
  156. return -UNW_EBADREG;
  157. if (DWARF_IS_REG_LOC (loc))
  158. return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
  159. &val, 1, c->as_arg);
  160. addr = DWARF_GET_LOC (loc);
  161. if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
  162. 1, c->as_arg)) < 0)
  163. return ret;
  164. return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
  165. 1, c->as_arg);
  166. }
  167. static inline int
  168. dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
  169. {
  170. if (DWARF_IS_NULL_LOC (loc))
  171. return -UNW_EBADREG;
  172. /* If a code-generator were to save a value of type unw_word_t in a
  173. floating-point register, we would have to support this case. I
  174. suppose it could happen with MMX registers, but does it really
  175. happen? */
  176. assert (!DWARF_IS_FP_LOC (loc));
  177. if (DWARF_IS_REG_LOC (loc))
  178. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
  179. 0, c->as_arg);
  180. else
  181. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
  182. 0, c->as_arg);
  183. }
  184. static inline int
  185. dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
  186. {
  187. if (DWARF_IS_NULL_LOC (loc))
  188. return -UNW_EBADREG;
  189. /* If a code-generator were to save a value of type unw_word_t in a
  190. floating-point register, we would have to support this case. I
  191. suppose it could happen with MMX registers, but does it really
  192. happen? */
  193. assert (!DWARF_IS_FP_LOC (loc));
  194. if (DWARF_IS_REG_LOC (loc))
  195. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
  196. 1, c->as_arg);
  197. else
  198. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
  199. 1, c->as_arg);
  200. }
  201. #endif /* !UNW_LOCAL_ONLY */
  202. #define tdep_getcontext_trace unw_getcontext
  203. #define tdep_needs_initialization UNW_OBJ(needs_initialization)
  204. #define tdep_init UNW_OBJ(init)
  205. /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
  206. tdep_search_unwind_table. */
  207. #define tdep_search_unwind_table dwarf_search_unwind_table
  208. #define tdep_find_unwind_table dwarf_find_unwind_table
  209. #define tdep_uc_addr UNW_ARCH_OBJ(uc_addr)
  210. #define tdep_get_elf_image UNW_ARCH_OBJ(get_elf_image)
  211. #define tdep_access_reg UNW_OBJ(access_reg)
  212. #define tdep_access_fpreg UNW_OBJ(access_fpreg)
  213. #define tdep_fetch_frame(c,ip,n) do {} while(0)
  214. #define tdep_cache_frame(c,rs) do {} while(0)
  215. #define tdep_reuse_frame(c,rs) do {} while(0)
  216. #define tdep_stash_frame(c,rs) do {} while(0)
  217. #define tdep_trace(cur,addr,n) (-UNW_ENOINFO)
  218. #ifdef UNW_LOCAL_ONLY
  219. # define tdep_find_proc_info(c,ip,n) \
  220. dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n), \
  221. (c)->as_arg)
  222. # define tdep_put_unwind_info(as,pi,arg) \
  223. dwarf_put_unwind_info((as), (pi), (arg))
  224. #else
  225. # define tdep_find_proc_info(c,ip,n) \
  226. (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n), \
  227. (c)->as_arg)
  228. # define tdep_put_unwind_info(as,pi,arg) \
  229. (*(as)->acc.put_unwind_info)((as), (pi), (arg))
  230. #endif
  231. #define tdep_get_as(c) ((c)->dwarf.as)
  232. #define tdep_get_as_arg(c) ((c)->dwarf.as_arg)
  233. #define tdep_get_ip(c) ((c)->dwarf.ip)
  234. #define tdep_big_endian(as) 0
  235. extern int tdep_needs_initialization;
  236. extern void tdep_init (void);
  237. extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
  238. unw_dyn_info_t *di, unw_proc_info_t *pi,
  239. int need_unwind_info, void *arg);
  240. extern void *tdep_uc_addr (ucontext_t *uc, int reg);
  241. extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
  242. unsigned long *segbase, unsigned long *mapoff,
  243. char *path, size_t pathlen);
  244. extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
  245. unw_word_t *valp, int write);
  246. extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
  247. unw_fpreg_t *valp, int write);
  248. #endif /* X86_LIBUNWIND_I_H */