blob: 2f6ce1510bffbd48324680d74184a848f3d0132c [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
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082/// Get value of specified register at cursor position in stack frame.
83_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
84 unw_word_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +000085 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000086 static_cast<void *>(cursor), regNum,
87 static_cast<void *>(value));
88 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
89 if (co->validReg(regNum)) {
90 *value = co->getReg(regNum);
91 return UNW_ESUCCESS;
92 }
93 return UNW_EBADREG;
94}
95
96
97/// Set value of specified register at cursor position in stack frame.
98_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
99 unw_word_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000100 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")",
101 static_cast<void *>(cursor), regNum, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102 typedef LocalAddressSpace::pint_t pint_t;
103 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
104 if (co->validReg(regNum)) {
105 co->setReg(regNum, (pint_t)value);
106 // specical case altering IP to re-find info (being called by personality
107 // function)
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000108 if (regNum == UNW_REG_IP) {
109 unw_proc_info_t info;
110 // First, get the FDE for the old location and then update it.
111 co->getInfo(&info);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000112 co->setInfoBasedOnIPRegister(false);
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000113 // If the original call expects stack adjustment, perform this now.
114 // Normal frame unwinding would have included the offset already in the
115 // CFA computation.
116 // Note: for PA-RISC and other platforms where the stack grows up,
117 // this should actually be - info.gp. LLVM doesn't currently support
118 // any such platforms and Clang doesn't export a macro for them.
119 if (info.gp)
120 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp);
121 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000122 return UNW_ESUCCESS;
123 }
124 return UNW_EBADREG;
125}
126
127
128/// Get value of specified float register at cursor position in stack frame.
129_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
130 unw_fpreg_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000131 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000132 static_cast<void *>(cursor), regNum,
133 static_cast<void *>(value));
134 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
135 if (co->validFloatReg(regNum)) {
136 *value = co->getFloatReg(regNum);
137 return UNW_ESUCCESS;
138 }
139 return UNW_EBADREG;
140}
141
142
143/// Set value of specified float register at cursor position in stack frame.
144_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
145 unw_fpreg_t value) {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000146#if defined(_LIBUNWIND_ARM_EHABI)
Ed Maste41bc5a72016-08-30 15:38:10 +0000147 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000148 static_cast<void *>(cursor), regNum, value);
149#else
Ed Maste41bc5a72016-08-30 15:38:10 +0000150 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000151 static_cast<void *>(cursor), regNum, value);
152#endif
153 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
154 if (co->validFloatReg(regNum)) {
155 co->setFloatReg(regNum, value);
156 return UNW_ESUCCESS;
157 }
158 return UNW_EBADREG;
159}
160
161
162/// Move cursor to next frame.
163_LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000164 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000165 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
166 return co->step();
167}
168
169
170/// Get unwind info at cursor position in stack frame.
171_LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
172 unw_proc_info_t *info) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000173 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000174 static_cast<void *>(cursor), static_cast<void *>(info));
175 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
176 co->getInfo(info);
177 if (info->end_ip == 0)
178 return UNW_ENOINFO;
179 else
180 return UNW_ESUCCESS;
181}
182
183
184/// Resume execution at cursor position (aka longjump).
185_LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000186 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000187 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
188 co->jumpto();
189 return UNW_EUNSPEC;
190}
191
192
193/// Get name of function at cursor position in stack frame.
194_LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
195 size_t bufLen, unw_word_t *offset) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000196 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000197 static_cast<void *>(cursor), static_cast<void *>(buf),
198 static_cast<unsigned long>(bufLen));
199 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
200 if (co->getFunctionName(buf, bufLen, offset))
201 return UNW_ESUCCESS;
202 else
203 return UNW_EUNSPEC;
204}
205
206
207/// Checks if a register is a floating-point register.
208_LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000209 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000210 static_cast<void *>(cursor), regNum);
211 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
212 return co->validFloatReg(regNum);
213}
214
215
216/// Checks if a register is a floating-point register.
217_LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
218 unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000219 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000220 static_cast<void *>(cursor), regNum);
221 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
222 return co->getRegisterName(regNum);
223}
224
225
226/// Checks if current frame is signal trampoline.
227_LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000228 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000229 static_cast<void *>(cursor));
230 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
231 return co->isSignalFrame();
232}
233
234#ifdef __arm__
235// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
236_LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000237 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000238 static_cast<void *>(cursor));
239 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
240 return co->saveVFPAsX();
241}
242#endif
243
244
Ranjeet Singh421231a2017-03-31 15:28:06 +0000245#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ed Maste4c43c3d2016-07-19 17:15:50 +0000246/// SPI: walks cached DWARF entries
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000247_LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
248 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 +0000249 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000250 reinterpret_cast<void *>(func));
251 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
252}
253
254
255/// IPI: for __register_frame()
256void _unw_add_dynamic_fde(unw_word_t fde) {
257 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
258 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
259 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
260 LocalAddressSpace::sThisAddressSpace,
261 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
262 if (message == NULL) {
263 // dynamically registered FDEs don't have a mach_header group they are in.
264 // Use fde as mh_group
265 unw_word_t mh_group = fdeInfo.fdeStart;
266 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
267 fdeInfo.pcStart, fdeInfo.pcEnd,
268 fdeInfo.fdeStart);
269 } else {
270 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
271 }
272}
273
274/// IPI: for __deregister_frame()
275void _unw_remove_dynamic_fde(unw_word_t fde) {
276 // fde is own mh_group
277 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
278}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000279#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000280#endif // !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000281
282
283
284// Add logging hooks in Debug builds only
285#ifndef NDEBUG
286#include <stdlib.h>
287
288_LIBUNWIND_HIDDEN
289bool logAPIs() {
290 // do manual lock to avoid use of _cxa_guard_acquire or initializers
291 static bool checked = false;
292 static bool log = false;
293 if (!checked) {
294 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
295 checked = true;
296 }
297 return log;
298}
299
300_LIBUNWIND_HIDDEN
301bool logUnwinding() {
302 // do manual lock to avoid use of _cxa_guard_acquire or initializers
303 static bool checked = false;
304 static bool log = false;
305 if (!checked) {
306 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
307 checked = true;
308 }
309 return log;
310}
311
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000312_LIBUNWIND_HIDDEN
313bool logDWARF() {
314 // do manual lock to avoid use of _cxa_guard_acquire or initializers
315 static bool checked = false;
316 static bool log = false;
317 if (!checked) {
318 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
319 checked = true;
320 }
321 return log;
322}
323
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000324#endif // NDEBUG
325