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