blob: f072d55e0be4db35ab9658cde2ec7023a465074c [file] [log] [blame]
Ed Maste6f723382016-08-30 13:08:21 +00001//===--------------------------- libunwind.cpp ----------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
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>
Saleem Abdulrasoole1539392015-05-11 16:35:13 +000019#include <algorithm>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000020
21#include "libunwind_ext.h"
22#include "config.h"
23
24#include <stdlib.h>
25
26
Ed Schoutenedb76802017-03-09 08:04:07 +000027#include "AddressSpace.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000028#include "UnwindCursor.hpp"
29
30using namespace libunwind;
31
32/// internal object to represent this processes address space
33LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
34
35_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
36 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
37
38/// record the registers and stack position of the caller
39extern int unw_getcontext(unw_context_t *);
40// note: unw_getcontext() implemented in assembly
41
42/// Create a cursor of a thread in this process given 'context' recorded by
43/// unw_getcontext().
44_LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
45 unw_context_t *context) {
Ed Maste41bc5a72016-08-30 15:38:10 +000046 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000047 static_cast<void *>(cursor),
48 static_cast<void *>(context));
Saleem Abdulrasool17552662015-04-24 19:39:17 +000049#if defined(__i386__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000050# define REGISTER_KIND Registers_x86
Saleem Abdulrasool17552662015-04-24 19:39:17 +000051#elif defined(__x86_64__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000052# define REGISTER_KIND Registers_x86_64
Saleem Abdulrasool17552662015-04-24 19:39:17 +000053#elif defined(__ppc__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000054# define REGISTER_KIND Registers_ppc
55#elif defined(__aarch64__)
56# define REGISTER_KIND Registers_arm64
Ranjeet Singh421231a2017-03-31 15:28:06 +000057#elif defined(_LIBUNWIND_ARM_EHABI)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000058# define REGISTER_KIND Registers_arm
Peter Zotov8d639992015-08-31 05:26:37 +000059#elif defined(__or1k__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000060# define REGISTER_KIND Registers_or1k
Vasileios Kalintiris3bd271c2015-09-26 18:26:01 +000061#elif defined(__mips__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000062# warning The MIPS architecture is not supported.
Ed Maste846892c2015-08-13 14:21:03 +000063#else
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000064# error Architecture not supported
Saleem Abdulrasool17552662015-04-24 19:39:17 +000065#endif
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000066 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
67 new ((void *)cursor) UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
68 context, LocalAddressSpace::sThisAddressSpace);
69#undef REGISTER_KIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +000070 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
71 co->setInfoBasedOnIPRegister();
72
73 return UNW_ESUCCESS;
74}
75
76#ifdef UNW_REMOTE
77/// Create a cursor into a thread in another process.
78_LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
79 unw_addr_space_t as,
80 void *arg) {
81 // special case: unw_init_remote(xx, unw_local_addr_space, xx)
82 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace)
83 return unw_init_local(cursor, NULL); //FIXME
84
85 // use "placement new" to allocate UnwindCursor in the cursor buffer
86 switch (as->cpuType) {
87 case CPU_TYPE_I386:
88 new ((void *)cursor)
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +000089 UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000090 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
91 break;
92 case CPU_TYPE_X86_64:
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +000093 new ((void *)cursor)
94 UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>,
95 Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 break;
97 case CPU_TYPE_POWERPC:
98 new ((void *)cursor)
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +000099 UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>,
100 Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000101 break;
102 default:
103 return UNW_EUNSPEC;
104 }
105 return UNW_ESUCCESS;
106}
107
108
109static bool is64bit(task_t task) {
110 return false; // FIXME
111}
112
113/// Create an address_space object for use in examining another task.
114_LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
115#if __i386__
116 if (is64bit(task)) {
117 unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
118 as->taskPort = task;
119 as->cpuType = CPU_TYPE_X86_64;
120 //as->oas
121 } else {
122 unw_addr_space_i386 *as = new unw_addr_space_i386(task);
123 as->taskPort = task;
124 as->cpuType = CPU_TYPE_I386;
125 //as->oas
126 }
127#else
128// FIXME
129#endif
130}
131
132
133/// Delete an address_space object.
134_LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
135 switch (asp->cpuType) {
136#if __i386__ || __x86_64__
137 case CPU_TYPE_I386: {
138 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
139 delete as;
140 }
141 break;
142 case CPU_TYPE_X86_64: {
143 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
144 delete as;
145 }
146 break;
147#endif
148 case CPU_TYPE_POWERPC: {
149 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
150 delete as;
151 }
152 break;
153 }
154}
155#endif // UNW_REMOTE
156
157
158/// Get value of specified register at cursor position in stack frame.
159_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
160 unw_word_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000161 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000162 static_cast<void *>(cursor), regNum,
163 static_cast<void *>(value));
164 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
165 if (co->validReg(regNum)) {
166 *value = co->getReg(regNum);
167 return UNW_ESUCCESS;
168 }
169 return UNW_EBADREG;
170}
171
172
173/// Set value of specified register at cursor position in stack frame.
174_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
175 unw_word_t value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000176 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%llX)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000177 static_cast<void *>(cursor), regNum, (long long)value);
178 typedef LocalAddressSpace::pint_t pint_t;
179 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
180 if (co->validReg(regNum)) {
181 co->setReg(regNum, (pint_t)value);
182 // specical case altering IP to re-find info (being called by personality
183 // function)
184 if (regNum == UNW_REG_IP)
185 co->setInfoBasedOnIPRegister(false);
186 return UNW_ESUCCESS;
187 }
188 return UNW_EBADREG;
189}
190
191
192/// Get value of specified float register at cursor position in stack frame.
193_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
194 unw_fpreg_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000195 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000196 static_cast<void *>(cursor), regNum,
197 static_cast<void *>(value));
198 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
199 if (co->validFloatReg(regNum)) {
200 *value = co->getFloatReg(regNum);
201 return UNW_ESUCCESS;
202 }
203 return UNW_EBADREG;
204}
205
206
207/// Set value of specified float register at cursor position in stack frame.
208_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
209 unw_fpreg_t value) {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000210#if defined(_LIBUNWIND_ARM_EHABI)
Ed Maste41bc5a72016-08-30 15:38:10 +0000211 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000212 static_cast<void *>(cursor), regNum, value);
213#else
Ed Maste41bc5a72016-08-30 15:38:10 +0000214 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000215 static_cast<void *>(cursor), regNum, value);
216#endif
217 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
218 if (co->validFloatReg(regNum)) {
219 co->setFloatReg(regNum, value);
220 return UNW_ESUCCESS;
221 }
222 return UNW_EBADREG;
223}
224
225
226/// Move cursor to next frame.
227_LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000228 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000229 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
230 return co->step();
231}
232
233
234/// Get unwind info at cursor position in stack frame.
235_LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
236 unw_proc_info_t *info) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000237 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000238 static_cast<void *>(cursor), static_cast<void *>(info));
239 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
240 co->getInfo(info);
241 if (info->end_ip == 0)
242 return UNW_ENOINFO;
243 else
244 return UNW_ESUCCESS;
245}
246
247
248/// Resume execution at cursor position (aka longjump).
249_LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000250 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000251 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
252 co->jumpto();
253 return UNW_EUNSPEC;
254}
255
256
257/// Get name of function at cursor position in stack frame.
258_LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
259 size_t bufLen, unw_word_t *offset) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000260 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000261 static_cast<void *>(cursor), static_cast<void *>(buf),
262 static_cast<unsigned long>(bufLen));
263 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
264 if (co->getFunctionName(buf, bufLen, offset))
265 return UNW_ESUCCESS;
266 else
267 return UNW_EUNSPEC;
268}
269
270
271/// Checks if a register is a floating-point register.
272_LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000273 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000274 static_cast<void *>(cursor), regNum);
275 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
276 return co->validFloatReg(regNum);
277}
278
279
280/// Checks if a register is a floating-point register.
281_LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
282 unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000283 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000284 static_cast<void *>(cursor), regNum);
285 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
286 return co->getRegisterName(regNum);
287}
288
289
290/// Checks if current frame is signal trampoline.
291_LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000292 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000293 static_cast<void *>(cursor));
294 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
295 return co->isSignalFrame();
296}
297
298#ifdef __arm__
299// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
300_LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000301 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000302 static_cast<void *>(cursor));
303 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
304 return co->saveVFPAsX();
305}
306#endif
307
308
Ranjeet Singh421231a2017-03-31 15:28:06 +0000309#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ed Maste4c43c3d2016-07-19 17:15:50 +0000310/// SPI: walks cached DWARF entries
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000311_LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
312 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000313 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000314 reinterpret_cast<void *>(func));
315 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
316}
317
318
319/// IPI: for __register_frame()
320void _unw_add_dynamic_fde(unw_word_t fde) {
321 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
322 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
323 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
324 LocalAddressSpace::sThisAddressSpace,
325 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
326 if (message == NULL) {
327 // dynamically registered FDEs don't have a mach_header group they are in.
328 // Use fde as mh_group
329 unw_word_t mh_group = fdeInfo.fdeStart;
330 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
331 fdeInfo.pcStart, fdeInfo.pcEnd,
332 fdeInfo.fdeStart);
333 } else {
334 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
335 }
336}
337
338/// IPI: for __deregister_frame()
339void _unw_remove_dynamic_fde(unw_word_t fde) {
340 // fde is own mh_group
341 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
342}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000343#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000344
345
346
347// Add logging hooks in Debug builds only
348#ifndef NDEBUG
349#include <stdlib.h>
350
351_LIBUNWIND_HIDDEN
352bool logAPIs() {
353 // do manual lock to avoid use of _cxa_guard_acquire or initializers
354 static bool checked = false;
355 static bool log = false;
356 if (!checked) {
357 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
358 checked = true;
359 }
360 return log;
361}
362
363_LIBUNWIND_HIDDEN
364bool logUnwinding() {
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_UNWINDING") != NULL);
370 checked = true;
371 }
372 return log;
373}
374
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000375_LIBUNWIND_HIDDEN
376bool logDWARF() {
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_DWARF") != NULL);
382 checked = true;
383 }
384 return log;
385}
386
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000387#endif // NDEBUG
388