1 //===--------------------------- libunwind.cpp ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //
9 // Implements unw_* functions from <libunwind.h>
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include <libunwind.h>
14
15 #ifndef NDEBUG
16 #include <cstdlib> // getenv
17 #endif
18 #include <new>
19 #include <algorithm>
20
21 #include "libunwind_ext.h"
22 #include "config.h"
23
24 #include <stdlib.h>
25
26
27 #include "UnwindCursor.hpp"
28
29 using namespace libunwind;
30
31 /// internal object to represent this processes address space
32 LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
33
34 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
35 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
36
37 /// record the registers and stack position of the caller
38 extern int unw_getcontext(unw_context_t *);
39 // note: unw_getcontext() implemented in assembly
40
41 /// Create a cursor of a thread in this process given 'context' recorded by
42 /// unw_getcontext().
unw_init_local(unw_cursor_t * cursor,unw_context_t * context)43 _LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
44 unw_context_t *context) {
45 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
46 static_cast<void *>(cursor),
47 static_cast<void *>(context));
48 #if defined(__i386__)
49 # define REGISTER_KIND Registers_x86
50 #elif defined(__x86_64__)
51 # define REGISTER_KIND Registers_x86_64
52 #elif defined(__ppc__)
53 # define REGISTER_KIND Registers_ppc
54 #elif defined(__aarch64__)
55 # define REGISTER_KIND Registers_arm64
56 #elif _LIBUNWIND_ARM_EHABI
57 # define REGISTER_KIND Registers_arm
58 #elif defined(__or1k__)
59 # define REGISTER_KIND Registers_or1k
60 #elif defined(__mips__)
61 # warning The MIPS architecture is not supported.
62 #else
63 # error Architecture not supported
64 #endif
65 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
66 new ((void *)cursor) UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
67 context, LocalAddressSpace::sThisAddressSpace);
68 #undef REGISTER_KIND
69 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
70 co->setInfoBasedOnIPRegister();
71
72 return UNW_ESUCCESS;
73 }
74
75 #ifdef UNW_REMOTE
76 /// Create a cursor into a thread in another process.
unw_init_remote_thread(unw_cursor_t * cursor,unw_addr_space_t as,void * arg)77 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
78 unw_addr_space_t as,
79 void *arg) {
80 // special case: unw_init_remote(xx, unw_local_addr_space, xx)
81 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace)
82 return unw_init_local(cursor, NULL); //FIXME
83
84 // use "placement new" to allocate UnwindCursor in the cursor buffer
85 switch (as->cpuType) {
86 case CPU_TYPE_I386:
87 new ((void *)cursor)
88 UnwindCursor<OtherAddressSpace<Pointer32<LittleEndian> >,
89 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
90 break;
91 case CPU_TYPE_X86_64:
92 new ((void *)cursor) UnwindCursor<
93 OtherAddressSpace<Pointer64<LittleEndian> >, Registers_x86_64>(
94 ((unw_addr_space_x86_64 *)as)->oas, arg);
95 break;
96 case CPU_TYPE_POWERPC:
97 new ((void *)cursor)
98 UnwindCursor<OtherAddressSpace<Pointer32<BigEndian> >, Registers_ppc>(
99 ((unw_addr_space_ppc *)as)->oas, arg);
100 break;
101 default:
102 return UNW_EUNSPEC;
103 }
104 return UNW_ESUCCESS;
105 }
106
107
is64bit(task_t task)108 static bool is64bit(task_t task) {
109 return false; // FIXME
110 }
111
112 /// Create an address_space object for use in examining another task.
unw_create_addr_space_for_task(task_t task)113 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
114 #if __i386__
115 if (is64bit(task)) {
116 unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
117 as->taskPort = task;
118 as->cpuType = CPU_TYPE_X86_64;
119 //as->oas
120 } else {
121 unw_addr_space_i386 *as = new unw_addr_space_i386(task);
122 as->taskPort = task;
123 as->cpuType = CPU_TYPE_I386;
124 //as->oas
125 }
126 #else
127 // FIXME
128 #endif
129 }
130
131
132 /// Delete an address_space object.
unw_destroy_addr_space(unw_addr_space_t asp)133 _LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
134 switch (asp->cpuType) {
135 #if __i386__ || __x86_64__
136 case CPU_TYPE_I386: {
137 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
138 delete as;
139 }
140 break;
141 case CPU_TYPE_X86_64: {
142 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
143 delete as;
144 }
145 break;
146 #endif
147 case CPU_TYPE_POWERPC: {
148 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
149 delete as;
150 }
151 break;
152 }
153 }
154 #endif // UNW_REMOTE
155
156
157 /// Get value of specified register at cursor position in stack frame.
unw_get_reg(unw_cursor_t * cursor,unw_regnum_t regNum,unw_word_t * value)158 _LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
159 unw_word_t *value) {
160 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
161 static_cast<void *>(cursor), regNum,
162 static_cast<void *>(value));
163 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
164 if (co->validReg(regNum)) {
165 *value = co->getReg(regNum);
166 return UNW_ESUCCESS;
167 }
168 return UNW_EBADREG;
169 }
170
171
172 /// Set value of specified register at cursor position in stack frame.
unw_set_reg(unw_cursor_t * cursor,unw_regnum_t regNum,unw_word_t value)173 _LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
174 unw_word_t value) {
175 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%llX)",
176 static_cast<void *>(cursor), regNum, (long long)value);
177 typedef LocalAddressSpace::pint_t pint_t;
178 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
179 if (co->validReg(regNum)) {
180 co->setReg(regNum, (pint_t)value, 0);
181 // specical case altering IP to re-find info (being called by personality
182 // function)
183 if (regNum == UNW_REG_IP)
184 co->setInfoBasedOnIPRegister(false);
185 return UNW_ESUCCESS;
186 }
187 return UNW_EBADREG;
188 }
189
190
191 /// Get value of specified float register at cursor position in stack frame.
unw_get_fpreg(unw_cursor_t * cursor,unw_regnum_t regNum,unw_fpreg_t * value)192 _LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
193 unw_fpreg_t *value) {
194 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
195 static_cast<void *>(cursor), regNum,
196 static_cast<void *>(value));
197 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
198 if (co->validFloatReg(regNum)) {
199 *value = co->getFloatReg(regNum);
200 return UNW_ESUCCESS;
201 }
202 return UNW_EBADREG;
203 }
204
205
206 /// Set value of specified float register at cursor position in stack frame.
unw_set_fpreg(unw_cursor_t * cursor,unw_regnum_t regNum,unw_fpreg_t value)207 _LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
208 unw_fpreg_t value) {
209 #if _LIBUNWIND_ARM_EHABI
210 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
211 static_cast<void *>(cursor), regNum, value);
212 #else
213 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
214 static_cast<void *>(cursor), regNum, value);
215 #endif
216 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
217 if (co->validFloatReg(regNum)) {
218 co->setFloatReg(regNum, value);
219 return UNW_ESUCCESS;
220 }
221 return UNW_EBADREG;
222 }
223
224 /// Get location of specified register at cursor position in stack frame.
unw_get_save_loc(unw_cursor_t * cursor,int regNum,unw_save_loc_t * location)225 _LIBUNWIND_EXPORT int unw_get_save_loc(unw_cursor_t* cursor, int regNum,
226 unw_save_loc_t* location)
227 {
228 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
229 if (co->validReg(regNum)) {
230 // We only support memory locations, not register locations
231 location->u.addr = co->getRegLocation(regNum);
232 location->type = (location->u.addr == 0) ? UNW_SLT_NONE : UNW_SLT_MEMORY;
233 return UNW_ESUCCESS;
234 }
235 return UNW_EBADREG;
236 }
237
238 /// Move cursor to next frame.
unw_step(unw_cursor_t * cursor)239 _LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
240 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
241 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
242 return co->step();
243 }
244
245
246 /// Get unwind info at cursor position in stack frame.
unw_get_proc_info(unw_cursor_t * cursor,unw_proc_info_t * info)247 _LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
248 unw_proc_info_t *info) {
249 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
250 static_cast<void *>(cursor), static_cast<void *>(info));
251 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
252 co->getInfo(info);
253 if (info->end_ip == 0)
254 return UNW_ENOINFO;
255 else
256 return UNW_ESUCCESS;
257 }
258
259
260 /// Resume execution at cursor position (aka longjump).
unw_resume(unw_cursor_t * cursor)261 _LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
262 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
263 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
264 co->jumpto();
265 return UNW_EUNSPEC;
266 }
267
268
269 /// Get name of function at cursor position in stack frame.
unw_get_proc_name(unw_cursor_t * cursor,char * buf,size_t bufLen,unw_word_t * offset)270 _LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
271 size_t bufLen, unw_word_t *offset) {
272 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
273 static_cast<void *>(cursor), static_cast<void *>(buf),
274 static_cast<unsigned long>(bufLen));
275 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
276 if (co->getFunctionName(buf, bufLen, offset))
277 return UNW_ESUCCESS;
278 else
279 return UNW_EUNSPEC;
280 }
281
282
283 /// Checks if a register is a floating-point register.
unw_is_fpreg(unw_cursor_t * cursor,unw_regnum_t regNum)284 _LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
285 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
286 static_cast<void *>(cursor), regNum);
287 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
288 return co->validFloatReg(regNum);
289 }
290
291
292 /// Checks if a register is a floating-point register.
unw_regname(unw_cursor_t * cursor,unw_regnum_t regNum)293 _LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
294 unw_regnum_t regNum) {
295 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
296 static_cast<void *>(cursor), regNum);
297 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
298 return co->getRegisterName(regNum);
299 }
300
301
302 /// Checks if current frame is signal trampoline.
unw_is_signal_frame(unw_cursor_t * cursor)303 _LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
304 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
305 static_cast<void *>(cursor));
306 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
307 return co->isSignalFrame();
308 }
309
310 #ifdef __arm__
311 // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
unw_save_vfp_as_X(unw_cursor_t * cursor)312 _LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
313 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
314 static_cast<void *>(cursor));
315 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
316 return co->saveVFPAsX();
317 }
318 #endif
319
320
321 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
322 /// SPI: walks cached DWARF entries
unw_iterate_dwarf_unwind_cache(void (* func)(unw_word_t ip_start,unw_word_t ip_end,unw_word_t fde,unw_word_t mh))323 _LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
324 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
325 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
326 reinterpret_cast<void *>(func));
327 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
328 }
329
330
331 /// IPI: for __register_frame()
_unw_add_dynamic_fde(unw_word_t fde)332 void _unw_add_dynamic_fde(unw_word_t fde) {
333 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
334 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
335 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
336 LocalAddressSpace::sThisAddressSpace,
337 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
338 if (message == NULL) {
339 // dynamically registered FDEs don't have a mach_header group they are in.
340 // Use fde as mh_group
341 unw_word_t mh_group = fdeInfo.fdeStart;
342 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
343 fdeInfo.pcStart, fdeInfo.pcEnd,
344 fdeInfo.fdeStart);
345 } else {
346 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
347 }
348 }
349
350 /// IPI: for __deregister_frame()
_unw_remove_dynamic_fde(unw_word_t fde)351 void _unw_remove_dynamic_fde(unw_word_t fde) {
352 // fde is own mh_group
353 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
354 }
355 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
356
357
358
359 // Add logging hooks in Debug builds only
360 #ifndef NDEBUG
361 #include <stdlib.h>
362
363 _LIBUNWIND_HIDDEN
logAPIs()364 bool logAPIs() {
365 // do manual lock to avoid use of _cxa_guard_acquire or initializers
366 static bool checked = false;
367 static bool log = false;
368 if (!checked) {
369 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
370 checked = true;
371 }
372 return log;
373 }
374
375 _LIBUNWIND_HIDDEN
logUnwinding()376 bool logUnwinding() {
377 // do manual lock to avoid use of _cxa_guard_acquire or initializers
378 static bool checked = false;
379 static bool log = false;
380 if (!checked) {
381 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
382 checked = true;
383 }
384 return log;
385 }
386
387 #endif // NDEBUG
388
389