blob: 03dfeaee07b2f37ef977b94753a95be22e463445 [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
26/// internal object to represent this processes address space
27LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
28
29_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
30 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
31
32/// record the registers and stack position of the caller
33extern int unw_getcontext(unw_context_t *);
34// note: unw_getcontext() implemented in assembly
35
36/// Create a cursor of a thread in this process given 'context' recorded by
37/// unw_getcontext().
38_LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
39 unw_context_t *context) {
Ed Maste41bc5a72016-08-30 15:38:10 +000040 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000041 static_cast<void *>(cursor),
42 static_cast<void *>(context));
Saleem Abdulrasool17552662015-04-24 19:39:17 +000043#if defined(__i386__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000044# define REGISTER_KIND Registers_x86
Saleem Abdulrasool17552662015-04-24 19:39:17 +000045#elif defined(__x86_64__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000046# define REGISTER_KIND Registers_x86_64
Martin Storsjo8338b0a2018-01-02 22:11:30 +000047#elif defined(__powerpc64__)
48# define REGISTER_KIND Registers_ppc64
Saleem Abdulrasool17552662015-04-24 19:39:17 +000049#elif defined(__ppc__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000050# define REGISTER_KIND Registers_ppc
51#elif defined(__aarch64__)
52# define REGISTER_KIND Registers_arm64
Martin Storsjoa72285f2017-11-02 08:16:16 +000053#elif defined(__arm__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000054# define REGISTER_KIND Registers_arm
Peter Zotov8d639992015-08-31 05:26:37 +000055#elif defined(__or1k__)
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000056# define REGISTER_KIND Registers_or1k
John Baldwin40300292018-05-15 22:44:56 +000057#elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
John Baldwin56441d42017-12-12 21:43:36 +000058# define REGISTER_KIND Registers_mips_o32
John Baldwin40300292018-05-15 22:44:56 +000059#elif defined(__mips64)
John Baldwin541a4352018-01-09 17:07:18 +000060# define REGISTER_KIND Registers_mips_newabi
Vasileios Kalintiris3bd271c2015-09-26 18:26:01 +000061#elif defined(__mips__)
John Baldwin56441d42017-12-12 21:43:36 +000062# warning The MIPS architecture is not supported with this ABI and environment!
Daniel Cederman9f2f07a2019-01-14 10:15:20 +000063#elif defined(__sparc__)
64# define REGISTER_KIND Registers_sparc
Ed Maste846892c2015-08-13 14:21:03 +000065#else
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000066# error Architecture not supported
Saleem Abdulrasool17552662015-04-24 19:39:17 +000067#endif
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000068 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
Petr Hosek36f61542019-02-02 21:15:49 +000069 new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor))
70 UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
71 context, LocalAddressSpace::sThisAddressSpace);
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +000072#undef REGISTER_KIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +000073 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
74 co->setInfoBasedOnIPRegister();
75
76 return UNW_ESUCCESS;
77}
78
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079/// Get value of specified register at cursor position in stack frame.
80_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
81 unw_word_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +000082 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000083 static_cast<void *>(cursor), regNum,
84 static_cast<void *>(value));
85 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
86 if (co->validReg(regNum)) {
87 *value = co->getReg(regNum);
88 return UNW_ESUCCESS;
89 }
90 return UNW_EBADREG;
91}
92
93
94/// Set value of specified register at cursor position in stack frame.
95_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
96 unw_word_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +000097 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")",
98 static_cast<void *>(cursor), regNum, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000099 typedef LocalAddressSpace::pint_t pint_t;
100 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
101 if (co->validReg(regNum)) {
102 co->setReg(regNum, (pint_t)value);
103 // specical case altering IP to re-find info (being called by personality
104 // function)
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000105 if (regNum == UNW_REG_IP) {
106 unw_proc_info_t info;
107 // First, get the FDE for the old location and then update it.
108 co->getInfo(&info);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000109 co->setInfoBasedOnIPRegister(false);
Joerg Sonnenberger7ac54d32018-07-17 19:00:51 +0000110 // If the original call expects stack adjustment, perform this now.
111 // Normal frame unwinding would have included the offset already in the
112 // CFA computation.
113 // Note: for PA-RISC and other platforms where the stack grows up,
114 // this should actually be - info.gp. LLVM doesn't currently support
115 // any such platforms and Clang doesn't export a macro for them.
116 if (info.gp)
117 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp);
118 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000119 return UNW_ESUCCESS;
120 }
121 return UNW_EBADREG;
122}
123
124
125/// Get value of specified float register at cursor position in stack frame.
126_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
127 unw_fpreg_t *value) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000128 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000129 static_cast<void *>(cursor), regNum,
130 static_cast<void *>(value));
131 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
132 if (co->validFloatReg(regNum)) {
133 *value = co->getFloatReg(regNum);
134 return UNW_ESUCCESS;
135 }
136 return UNW_EBADREG;
137}
138
139
140/// Set value of specified float register at cursor position in stack frame.
141_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
142 unw_fpreg_t value) {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000143#if defined(_LIBUNWIND_ARM_EHABI)
Ed Maste41bc5a72016-08-30 15:38:10 +0000144 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000145 static_cast<void *>(cursor), regNum, value);
146#else
Ed Maste41bc5a72016-08-30 15:38:10 +0000147 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000148 static_cast<void *>(cursor), regNum, value);
149#endif
150 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
151 if (co->validFloatReg(regNum)) {
152 co->setFloatReg(regNum, value);
153 return UNW_ESUCCESS;
154 }
155 return UNW_EBADREG;
156}
157
158
159/// Move cursor to next frame.
160_LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000161 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000162 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
163 return co->step();
164}
165
166
167/// Get unwind info at cursor position in stack frame.
168_LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
169 unw_proc_info_t *info) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000170 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171 static_cast<void *>(cursor), static_cast<void *>(info));
172 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
173 co->getInfo(info);
174 if (info->end_ip == 0)
175 return UNW_ENOINFO;
176 else
177 return UNW_ESUCCESS;
178}
179
180
181/// Resume execution at cursor position (aka longjump).
182_LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000183 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000184 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
185 co->jumpto();
186 return UNW_EUNSPEC;
187}
188
189
190/// Get name of function at cursor position in stack frame.
191_LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
192 size_t bufLen, unw_word_t *offset) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000193 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000194 static_cast<void *>(cursor), static_cast<void *>(buf),
195 static_cast<unsigned long>(bufLen));
196 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
197 if (co->getFunctionName(buf, bufLen, offset))
198 return UNW_ESUCCESS;
199 else
200 return UNW_EUNSPEC;
201}
202
203
204/// Checks if a register is a floating-point register.
205_LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000206 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000207 static_cast<void *>(cursor), regNum);
208 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
209 return co->validFloatReg(regNum);
210}
211
212
213/// Checks if a register is a floating-point register.
214_LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
215 unw_regnum_t regNum) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000216 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000217 static_cast<void *>(cursor), regNum);
218 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
219 return co->getRegisterName(regNum);
220}
221
222
223/// Checks if current frame is signal trampoline.
224_LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000225 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000226 static_cast<void *>(cursor));
227 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
228 return co->isSignalFrame();
229}
230
231#ifdef __arm__
232// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
233_LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000234 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000235 static_cast<void *>(cursor));
236 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
237 return co->saveVFPAsX();
238}
239#endif
240
241
Ranjeet Singh421231a2017-03-31 15:28:06 +0000242#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ed Maste4c43c3d2016-07-19 17:15:50 +0000243/// SPI: walks cached DWARF entries
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000244_LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
245 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 +0000246 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000247 reinterpret_cast<void *>(func));
248 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
249}
250
251
252/// IPI: for __register_frame()
253void _unw_add_dynamic_fde(unw_word_t fde) {
254 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
255 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
256 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
257 LocalAddressSpace::sThisAddressSpace,
258 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
259 if (message == NULL) {
260 // dynamically registered FDEs don't have a mach_header group they are in.
261 // Use fde as mh_group
262 unw_word_t mh_group = fdeInfo.fdeStart;
263 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
264 fdeInfo.pcStart, fdeInfo.pcEnd,
265 fdeInfo.fdeStart);
266 } else {
267 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
268 }
269}
270
271/// IPI: for __deregister_frame()
272void _unw_remove_dynamic_fde(unw_word_t fde) {
273 // fde is own mh_group
274 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
275}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000276#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000277#endif // !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000278
279
280
281// Add logging hooks in Debug builds only
282#ifndef NDEBUG
283#include <stdlib.h>
284
285_LIBUNWIND_HIDDEN
286bool logAPIs() {
287 // do manual lock to avoid use of _cxa_guard_acquire or initializers
288 static bool checked = false;
289 static bool log = false;
290 if (!checked) {
291 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
292 checked = true;
293 }
294 return log;
295}
296
297_LIBUNWIND_HIDDEN
298bool logUnwinding() {
299 // do manual lock to avoid use of _cxa_guard_acquire or initializers
300 static bool checked = false;
301 static bool log = false;
302 if (!checked) {
303 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
304 checked = true;
305 }
306 return log;
307}
308
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000309_LIBUNWIND_HIDDEN
310bool logDWARF() {
311 // do manual lock to avoid use of _cxa_guard_acquire or initializers
312 static bool checked = false;
313 static bool log = false;
314 if (!checked) {
315 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
316 checked = true;
317 }
318 return log;
319}
320
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000321#endif // NDEBUG
322