• 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 /* Add For Cache MAP And ELF */
41 #include "map_info.h"
42 /* Add For Cache MAP And ELF */
43 #include "mempool.h"
44 #include "dwarf.h"
45 
46 typedef enum
47   {
48     UNW_X86_64_FRAME_ALIGNED = -3,       /* frame stack pointer aligned */
49     UNW_X86_64_FRAME_STANDARD = -2,     /* regular rbp, rsp +/- offset */
50     UNW_X86_64_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
51     UNW_X86_64_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
52     UNW_X86_64_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
53   }
54 unw_tdep_frame_type_t;
55 
56 typedef struct
57   {
58     uint64_t virtual_address;
59     int64_t frame_type     : 3;  /* unw_tdep_frame_type_t classification */
60     int64_t last_frame     : 1;  /* non-zero if last frame in chain */
61     int64_t cfa_reg_rsp    : 1;  /* cfa dwarf base register is rsp vs. rbp */
62     int64_t cfa_reg_offset : 29; /* cfa is at this offset from base register value */
63     int64_t rbp_cfa_offset : 15; /* rbp saved at this offset from cfa (-1 = not saved) */
64     int64_t rsp_cfa_offset : 15; /* rsp saved at this offset from cfa (-1 = not saved) */
65   }
66 unw_tdep_frame_t;
67 
68 struct unw_addr_space
69   {
70     struct unw_accessors acc;
71     unw_caching_policy_t caching_policy;
72     _Atomic uint32_t cache_generation;
73     unw_word_t dyn_generation;          /* see dyn-common.h */
74     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
75     struct dwarf_rs_cache global_cache;
76     struct unw_debug_frame_list *debug_frames;
77     /* Add For Cache MAP And ELF */
78     struct map_info *map_list;
79     /* Add For Cache MAP And ELF */
80     unw_cursor_t *cursor;
81     int pid;
82    };
83 
84 static inline struct cursor *
get_cursor_from_as(unw_addr_space_t as)85 get_cursor_from_as(unw_addr_space_t as)
86 {
87   if (as->cursor) {
88     return (struct cursor *)(as->cursor);
89   }
90 
91   return NULL;
92 }
93 
94 struct cursor
95   {
96     struct dwarf_cursor dwarf;          /* must be first */
97 
98     unw_tdep_frame_t frame_info;        /* quick tracing assist info */
99 
100     /* Format of sigcontext structure and address at which it is
101        stored: */
102     enum
103       {
104         X86_64_SCF_NONE,                /* no signal frame encountered */
105         X86_64_SCF_LINUX_RT_SIGFRAME,   /* Linux ucontext_t */
106         X86_64_SCF_FREEBSD_SIGFRAME,    /* FreeBSD signal frame */
107         X86_64_SCF_FREEBSD_SYSCALL,     /* FreeBSD syscall */
108         X86_64_SCF_SOLARIS_SIGFRAME,    /* illumos/Solaris signal frame */
109       }
110     sigcontext_format;
111     unw_word_t sigcontext_addr;
112   };
113 
114 #define AS_ARG_UCONTEXT_MASK ~0x1UL
115 #define AS_ARG_VALIDATE_MASK 0x1UL
116 
117 #define AS_ARG_GET_UC_PTR(arg) \
118   ((ucontext_t *) ((uintptr_t) arg & AS_ARG_UCONTEXT_MASK))
119 #define AS_ARG_GET_VALIDATE(arg) \
120   ((int) ((uintptr_t) arg & AS_ARG_VALIDATE_MASK))
121 
122 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)123 dwarf_get_uc(const struct dwarf_cursor *cursor)
124 {
125   assert(cursor->as == unw_local_addr_space);
126   return AS_ARG_GET_UC_PTR(cursor->as_arg);
127 }
128 
129 static inline int
dwarf_get_validate(const struct dwarf_cursor * cursor)130 dwarf_get_validate(const struct dwarf_cursor *cursor)
131 {
132   assert(cursor->as == unw_local_addr_space);
133   return AS_ARG_GET_VALIDATE(cursor->as_arg);
134 }
135 
136 static inline void
dwarf_set_validate(const struct dwarf_cursor * cursor,const int validate)137 dwarf_set_validate(const struct dwarf_cursor *cursor, const int validate)
138 {
139   assert(cursor->as == unw_local_addr_space);
140   uintptr_t *packed_args = (uintptr_t *) &cursor->as_arg;
141   *packed_args |= (AS_ARG_VALIDATE_MASK & validate);
142 }
143 
144 static inline void *
dwarf_build_as_arg(const ucontext_t * uc,const int validate)145 dwarf_build_as_arg(const ucontext_t *uc, const int validate) {
146   uintptr_t packed_args = (uintptr_t) uc;
147   assert((packed_args & AS_ARG_VALIDATE_MASK) == 0);
148   packed_args |= (AS_ARG_VALIDATE_MASK & validate);
149   return (void *) packed_args;
150 }
151 
152 #define DWARF_GET_LOC(l)        ((l).val)
153 # define DWARF_LOC_TYPE_MEM     (0 << 0)
154 # define DWARF_LOC_TYPE_FP      (1 << 0)
155 # define DWARF_LOC_TYPE_REG     (1 << 1)
156 # define DWARF_LOC_TYPE_VAL     (1 << 2)
157 
158 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
159 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
160 # define DWARF_IS_MEM_LOC(l)    ((l).type == DWARF_LOC_TYPE_MEM)
161 # define DWARF_IS_VAL_LOC(l)    (((l).type & DWARF_LOC_TYPE_VAL) != 0)
162 
163 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
164 # define DWARF_VAL_LOC(c,v)     DWARF_LOC ((v), DWARF_LOC_TYPE_VAL)
165 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), DWARF_LOC_TYPE_MEM)
166 
167 #ifdef UNW_LOCAL_ONLY
168 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
169 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
170 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
171                                  x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
172 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
173                                  x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
174 
175 #else /* !UNW_LOCAL_ONLY */
176 
177 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
178 
179 static inline int
dwarf_is_null_loc(dwarf_loc_t l)180 dwarf_is_null_loc(dwarf_loc_t l)
181 {
182   return l.val == 0 && l.type == 0;
183 }
184 
185 # define DWARF_IS_NULL_LOC(l)   dwarf_is_null_loc(l)
186 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
187 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
188                                                 | DWARF_LOC_TYPE_FP))
189 
190 #endif /* !UNW_LOCAL_ONLY */
191 
192 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)193 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
194 {
195   if (DWARF_IS_NULL_LOC (loc))
196     return -UNW_EBADREG;
197 
198   abort ();
199 }
200 
201 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)202 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
203 {
204   if (DWARF_IS_NULL_LOC (loc))
205     return -UNW_EBADREG;
206 
207   abort ();
208 }
209 
210 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)211 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
212 {
213 
214   int reg_num;
215   if (DWARF_IS_NULL_LOC (loc))
216     return -UNW_EBADREG;
217 
218   if (DWARF_IS_REG_LOC (loc)) {
219     reg_num = DWARF_GET_LOC (loc);
220     if (reg_num >= 0 && reg_num < c->reg_sz) {
221       *val = c->ctx[reg_num];
222       return 1;
223     } else {
224       return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
225                                       0, c->as_arg);
226     }
227   }
228   if (DWARF_IS_MEM_LOC (loc))
229     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
230                                      0, c->as_arg);
231   assert(DWARF_IS_VAL_LOC (loc));
232   *val = DWARF_GET_LOC (loc);
233   return 0;
234 }
235 
236 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)237 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
238 {
239   assert(!DWARF_IS_VAL_LOC (loc));
240 
241   if (DWARF_IS_NULL_LOC (loc))
242     return -UNW_EBADREG;
243 
244   if (DWARF_IS_REG_LOC (loc))
245     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
246                                      1, c->as_arg);
247   else
248     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
249                                      1, c->as_arg);
250 }
251 
252 #define tdep_getcontext_trace           UNW_ARCH_OBJ(getcontext_trace)
253 #define tdep_init_done                  UNW_OBJ(init_done)
254 #define tdep_init_mem_validate          UNW_OBJ(init_mem_validate)
255 #define tdep_init                       UNW_OBJ(init)
256 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
257    tdep_search_unwind_table.  */
258 #define tdep_search_unwind_table        dwarf_search_unwind_table
259 #define tdep_find_unwind_table          dwarf_find_unwind_table
260 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
261 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
262 #define tdep_access_reg                 UNW_OBJ(access_reg)
263 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
264 #if __linux__ || defined(UNW_TARGET_X86_64_LINUX)
265 # define tdep_fetch_frame               UNW_OBJ(fetch_frame)
266 # define tdep_cache_frame               UNW_OBJ(cache_frame)
267 # define tdep_reuse_frame               UNW_OBJ(reuse_frame)
268 #else
269 # define tdep_fetch_frame(c,ip,n)       do {} while(0)
270 # define tdep_cache_frame(c)            0
271 # define tdep_reuse_frame(c,frame)      do {} while(0)
272 #endif
273 #define tdep_stash_frame                UNW_OBJ(stash_frame)
274 #define tdep_trace                      UNW_OBJ(tdep_trace)
275 #define x86_64_r_uc_addr                UNW_OBJ(r_uc_addr)
276 
277 #ifdef UNW_LOCAL_ONLY
278 # define tdep_find_proc_info(c,ip,n)                            \
279         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
280                                        (c)->as_arg)
281 # define tdep_put_unwind_info(as,pi,arg)                \
282         dwarf_put_unwind_info((as), (pi), (arg))
283 #else
284 # define tdep_find_proc_info(c,ip,n)                                    \
285         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
286                                        (c)->as_arg)
287 # define tdep_put_unwind_info(as,pi,arg)                        \
288         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
289 #endif
290 
291 #define tdep_get_as(c)                  ((c)->dwarf.as)
292 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
293 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
294 #define tdep_big_endian(as)             0
295 
296 extern atomic_bool tdep_init_done;
297 
298 extern void tdep_init (void);
299 extern void tdep_init_mem_validate (void);
300 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
301                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
302                                      int need_unwind_info, void *arg);
303 extern void *x86_64_r_uc_addr (ucontext_t *uc, int reg);
304 /* Add For Cache MAP And ELF */
305 extern struct map_info *tdep_get_elf_image (unw_addr_space_t as, pid_t pid,
306 					    unw_word_t ip);
307 /* Add For Cache MAP And ELF */
308 extern void tdep_get_exe_image_path (char *path);
309 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
310                             unw_word_t *valp, int write);
311 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
312                               unw_fpreg_t *valp, int write);
313 #if __linux__ || defined(UNW_TARGET_X86_64_LINUX)
314 extern void tdep_fetch_frame (struct dwarf_cursor *c, unw_word_t ip,
315                               int need_unwind_info);
316 extern int tdep_cache_frame (struct dwarf_cursor *c);
317 extern void tdep_reuse_frame (struct dwarf_cursor *c,
318                               int frame);
319 extern void tdep_stash_frame (struct dwarf_cursor *c,
320                               struct dwarf_reg_state *rs);
321 #endif
322 
323 extern int tdep_getcontext_trace (unw_tdep_context_t *);
324 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
325 
326 #endif /* X86_64_LIBUNWIND_I_H */
327