blob: cfa9112a63365e6cda0ecfb8c39b0f1648338bee [file] [log] [blame]
Ed Maste6f723382016-08-30 13:08:21 +00001//===--------------------------- libunwind.cpp ----------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
8// Implements unw_* functions from <libunwind.h>
9//
10//===----------------------------------------------------------------------===//
11
12#include <libunwind.h>
13
Saleem Abdulrasool17552662015-04-24 19:39:17 +000014#include "libunwind_ext.h"
15#include "config.h"
16
17#include <stdlib.h>
18
19
Martin Storsjo9f16c6c2017-09-26 08:07:26 +000020#if !defined(__USING_SJLJ_EXCEPTIONS__)
Ed Schoutenedb76802017-03-09 08:04:07 +000021#include "AddressSpace.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000022#include "UnwindCursor.hpp"
23
24using namespace libunwind;
25
Petr Hosekac0d9e02019-01-29 22:26:18 +000026// libunwind does not and should not depend on C++ library which means that we
27// need our own declaration of global placement new.
28void *operator new(size_t, void*);
29
Saleem Abdulrasool17552662015-04-24 19:39:17 +000030/// internal object to represent this processes address space
31LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
32
33_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
34 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
35
36/// record the registers and stack position of the caller
37extern int unw_getcontext(unw_context_t *);
38// note: unw_getcontext() implemented in assembly
39
40/// Create a cursor of a thread in this process given 'context' recorded by
41/// unw_getcontext().
42_LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
43 unw_context_t *context) {
Ed Maste41bc5a72016-08-30 15:38:10 +000044 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000045 static_cast<void *>(cursor),
46 static_cast<void *>(context));
Saleem Abdulrasool17552662015-04-24 19:39:17 +000047#if defined(__i386__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000048# define REGISTER_KIND Registers_x86
Saleem Abdulrasool17552662015-04-24 19:39:17 +000049#elif defined(__x86_64__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000050# define REGISTER_KIND Registers_x86_64
Martin Storsjo8338b0a2018-01-02 22:11:30 +000051#elif defined(__powerpc64__)
52# define REGISTER_KIND Registers_ppc64
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
Martin Storsjoa72285f2017-11-02 08:16:16 +000057#elif defined(__arm__)
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
John Baldwin40300292018-05-15 22:44:56 +000061#elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
John Baldwin56441d42017-12-12 21:43:36 +000062# define REGISTER_KIND Registers_mips_o32
John Baldwin40300292018-05-15 22:44:56 +000063#elif defined(__mips64)
John Baldwin541a4352018-01-09 17:07:18 +000064# define REGISTER_KIND Registers_mips_newabi
Vasileios Kalintiris3bd271c2015-09-26 18:26:01 +000065#elif defined(__mips__)
John Baldwin56441d42017-12-12 21:43:36 +000066# warning The MIPS architecture is not supported with this ABI and environment!
Daniel Cederman9f2f07a2019-01-14 10:15:20 +000067#elif defined(__sparc__)
68# define REGISTER_KIND Registers_sparc
Ed Maste846892c2015-08-13 14:21:03 +000069#else
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000070# error Architecture not supported
Saleem Abdulrasool17552662015-04-24 19:39:17 +000071#endif
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000072 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
73 new ((void *)cursor) UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
74 context, LocalAddressSpace::sThisAddressSpace);
75#undef REGISTER_KIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +000076 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
77 co->setInfoBasedOnIPRegister();
78
79 return UNW_ESUCCESS;
80}
81
82#ifdef UNW_REMOTE
83/// Create a cursor into a thread in another process.
84_LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
85 unw_addr_space_t as,
86 void *arg) {
87 // special case: unw_init_remote(xx, unw_local_addr_space, xx)
88 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace)
89 return unw_init_local(cursor, NULL); //FIXME
90
91 // use "placement new" to allocate UnwindCursor in the cursor buffer
92 switch (as->cpuType) {
93 case CPU_TYPE_I386:
94 new ((void *)cursor)
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +000095 UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
97 break;
98 case CPU_TYPE_X86_64:
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +000099 new ((void *)cursor)
100 UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>,
101 Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102 break;
103 case CPU_TYPE_POWERPC:
104 new ((void *)cursor)
Saleem Abdulrasool66efa8e2017-01-21 16:22:46 +0000105 UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>,
106 Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000107 break;
108 default:
109 return UNW_EUNSPEC;
110 }
111 return UNW_ESUCCESS;
112}
113
114
115static bool is64bit(task_t task) {
116 return false; // FIXME
117}
118
119/// Create an address_space object for use in examining another task.
120_LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
121#if __i386__
122 if (is64bit(task)) {
Petr Hosek1f978e92019-01-25 21:39:46 +0000123 unw_addr_space_x86_64 *as = malloc(sizeof(unw_addr_space_x86_64));
124 new (as) unw_addr_space_x86_64(task);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000125 as->taskPort = task;
126 as->cpuType = CPU_TYPE_X86_64;
127 //as->oas
128 } else {
Petr Hosek1f978e92019-01-25 21:39:46 +0000129 unw_addr_space_i386 *as = malloc(sizeof(unw_addr_space_i386));
130 new (as) unw_addr_space_i386(task);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000131 as->taskPort = task;
132 as->cpuType = CPU_TYPE_I386;
133 //as->oas
134 }
135#else
136// FIXME
137#endif
138}
139
140
141/// Delete an address_space object.
142_LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
143 switch (asp->cpuType) {
144#if __i386__ || __x86_64__
145 case CPU_TYPE_I386: {
146 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
Petr Hosek1f978e92019-01-25 21:39:46 +0000147 as->~unw_addr_space_i386();
148 free(as);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 }
150 break;
151 case CPU_TYPE_X86_64: {
152 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
Petr Hosek1f978e92019-01-25 21:39:46 +0000153 as->~unw_addr_space_x86_64();
154 free(as);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000155 }
156 break;
157#endif
158 case CPU_TYPE_POWERPC: {
159 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
Petr Hosek1f978e92019-01-25 21:39:46 +0000160 as->~unw_addr_space_ppc();
161 free(as);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000162 }
163 break;
164 }
165}
166#endif // UNW_REMOTE
167
168
169/// Get value of specified register at cursor position in stack frame.
170_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
171 unw_word_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000172 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000173 static_cast<void *>(cursor), regNum,
174 static_cast<void *>(value));
175 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
176 if (co->validReg(regNum)) {
177 *value = co->getReg(regNum);
178 return UNW_ESUCCESS;
179 }
180 return UNW_EBADREG;
181}
182
183
184/// Set value of specified register at cursor position in stack frame.
185_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
186 unw_word_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000187 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")",
188 static_cast<void *>(cursor), regNum, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000189 typedef LocalAddressSpace::pint_t pint_t;
190 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
191 if (co->validReg(regNum)) {
192 co->setReg(regNum, (pint_t)value);
193 // specical case altering IP to re-find info (being called by personality
194 // function)
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000195 if (regNum == UNW_REG_IP) {
196 unw_proc_info_t info;
197 // First, get the FDE for the old location and then update it.
198 co->getInfo(&info);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199 co->setInfoBasedOnIPRegister(false);
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000200 // If the original call expects stack adjustment, perform this now.
201 // Normal frame unwinding would have included the offset already in the
202 // CFA computation.
203 // Note: for PA-RISC and other platforms where the stack grows up,
204 // this should actually be - info.gp. LLVM doesn't currently support
205 // any such platforms and Clang doesn't export a macro for them.
206 if (info.gp)
207 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp);
208 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000209 return UNW_ESUCCESS;
210 }
211 return UNW_EBADREG;
212}
213
214
215/// Get value of specified float register at cursor position in stack frame.
216_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
217 unw_fpreg_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000218 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000219 static_cast<void *>(cursor), regNum,
220 static_cast<void *>(value));
221 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
222 if (co->validFloatReg(regNum)) {
223 *value = co->getFloatReg(regNum);
224 return UNW_ESUCCESS;
225 }
226 return UNW_EBADREG;
227}
228
229
230/// Set value of specified float register at cursor position in stack frame.
231_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
232 unw_fpreg_t value) {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000233#if defined(_LIBUNWIND_ARM_EHABI)
Ed Maste41bc5a72016-08-30 15:38:10 +0000234 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000235 static_cast<void *>(cursor), regNum, value);
236#else
Ed Maste41bc5a72016-08-30 15:38:10 +0000237 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000238 static_cast<void *>(cursor), regNum, value);
239#endif
240 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
241 if (co->validFloatReg(regNum)) {
242 co->setFloatReg(regNum, value);
243 return UNW_ESUCCESS;
244 }
245 return UNW_EBADREG;
246}
247
248
249/// Move cursor to next frame.
250_LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000251 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000252 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
253 return co->step();
254}
255
256
257/// Get unwind info at cursor position in stack frame.
258_LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
259 unw_proc_info_t *info) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000260 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000261 static_cast<void *>(cursor), static_cast<void *>(info));
262 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
263 co->getInfo(info);
264 if (info->end_ip == 0)
265 return UNW_ENOINFO;
266 else
267 return UNW_ESUCCESS;
268}
269
270
271/// Resume execution at cursor position (aka longjump).
272_LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000273 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000274 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
275 co->jumpto();
276 return UNW_EUNSPEC;
277}
278
279
280/// Get name of function at cursor position in stack frame.
281_LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
282 size_t bufLen, unw_word_t *offset) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000283 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000284 static_cast<void *>(cursor), static_cast<void *>(buf),
285 static_cast<unsigned long>(bufLen));
286 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
287 if (co->getFunctionName(buf, bufLen, offset))
288 return UNW_ESUCCESS;
289 else
290 return UNW_EUNSPEC;
291}
292
293
294/// Checks if a register is a floating-point register.
295_LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000296 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000297 static_cast<void *>(cursor), regNum);
298 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
299 return co->validFloatReg(regNum);
300}
301
302
303/// Checks if a register is a floating-point register.
304_LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
305 unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000306 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000307 static_cast<void *>(cursor), regNum);
308 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
309 return co->getRegisterName(regNum);
310}
311
312
313/// Checks if current frame is signal trampoline.
314_LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000315 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000316 static_cast<void *>(cursor));
317 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
318 return co->isSignalFrame();
319}
320
321#ifdef __arm__
322// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
323_LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000324 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000325 static_cast<void *>(cursor));
326 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
327 return co->saveVFPAsX();
328}
329#endif
330
331
Ranjeet Singh421231a2017-03-31 15:28:06 +0000332#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ed Maste4c43c3d2016-07-19 17:15:50 +0000333/// SPI: walks cached DWARF entries
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000334_LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
335 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 +0000336 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000337 reinterpret_cast<void *>(func));
338 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
339}
340
341
342/// IPI: for __register_frame()
343void _unw_add_dynamic_fde(unw_word_t fde) {
344 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
345 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
346 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
347 LocalAddressSpace::sThisAddressSpace,
348 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
349 if (message == NULL) {
350 // dynamically registered FDEs don't have a mach_header group they are in.
351 // Use fde as mh_group
352 unw_word_t mh_group = fdeInfo.fdeStart;
353 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
354 fdeInfo.pcStart, fdeInfo.pcEnd,
355 fdeInfo.fdeStart);
356 } else {
357 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
358 }
359}
360
361/// IPI: for __deregister_frame()
362void _unw_remove_dynamic_fde(unw_word_t fde) {
363 // fde is own mh_group
364 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
365}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000366#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000367#endif // !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000368
369
370
371// Add logging hooks in Debug builds only
372#ifndef NDEBUG
373#include <stdlib.h>
374
375_LIBUNWIND_HIDDEN
376bool logAPIs() {
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_APIS") != NULL);
382 checked = true;
383 }
384 return log;
385}
386
387_LIBUNWIND_HIDDEN
388bool logUnwinding() {
389 // do manual lock to avoid use of _cxa_guard_acquire or initializers
390 static bool checked = false;
391 static bool log = false;
392 if (!checked) {
393 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
394 checked = true;
395 }
396 return log;
397}
398
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000399_LIBUNWIND_HIDDEN
400bool logDWARF() {
401 // do manual lock to avoid use of _cxa_guard_acquire or initializers
402 static bool checked = false;
403 static bool log = false;
404 if (!checked) {
405 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
406 checked = true;
407 }
408 return log;
409}
410
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000411#endif // NDEBUG
412