1 //===--------------------------- libunwind.cpp ----------------------------===//
2 //
3 // 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
6 //
7 //
8 //  Implements unw_* functions from <libunwind.h>
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include <libunwind.h>
13 
14 #include "libunwind_ext.h"
15 #include "config.h"
16 
17 #include <stdlib.h>
18 
19 
20 #if !defined(__USING_SJLJ_EXCEPTIONS__)
21 #include "AddressSpace.hpp"
22 #include "UnwindCursor.hpp"
23 
24 using namespace libunwind;
25 
26 /// internal object to represent this processes address space
27 LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
28 
29 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
30     (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
31 
32 /// Create a cursor of a thread in this process given 'context' recorded by
33 /// __unw_getcontext().
__unw_init_local(unw_cursor_t * cursor,unw_context_t * context)34 _LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor,
35                                        unw_context_t *context) {
36   _LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)",
37                        static_cast<void *>(cursor),
38                        static_cast<void *>(context));
39 #if defined(__i386__)
40 # define REGISTER_KIND Registers_x86
41 #elif defined(__x86_64__)
42 # define REGISTER_KIND Registers_x86_64
43 #elif defined(__powerpc64__)
44 # define REGISTER_KIND Registers_ppc64
45 #elif defined(__ppc__)
46 # define REGISTER_KIND Registers_ppc
47 #elif defined(__aarch64__)
48 # define REGISTER_KIND Registers_arm64
49 #elif defined(__arm__)
50 # define REGISTER_KIND Registers_arm
51 #elif defined(__or1k__)
52 # define REGISTER_KIND Registers_or1k
53 #elif defined(__hexagon__)
54 # define REGISTER_KIND Registers_hexagon
55 #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
56 # define REGISTER_KIND Registers_mips_o32
57 #elif defined(__mips64)
58 # define REGISTER_KIND Registers_mips_newabi
59 #elif defined(__mips__)
60 # warning The MIPS architecture is not supported with this ABI and environment!
61 #elif defined(__sparc__)
62 # define REGISTER_KIND Registers_sparc
63 #elif defined(__riscv) && __riscv_xlen == 64
64 # define REGISTER_KIND Registers_riscv
65 #else
66 # error Architecture not supported
67 #endif
68   // Use "placement new" to allocate UnwindCursor in the cursor buffer.
69   new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor))
70       UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
71           context, LocalAddressSpace::sThisAddressSpace);
72 #undef REGISTER_KIND
73   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
74   co->setInfoBasedOnIPRegister();
75 
76   return UNW_ESUCCESS;
77 }
_LIBUNWIND_WEAK_ALIAS(__unw_init_local,unw_init_local)78 _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local)
79 
80 /// Get value of specified register at cursor position in stack frame.
81 _LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
82                                     unw_word_t *value) {
83   _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
84                        static_cast<void *>(cursor), regNum,
85                        static_cast<void *>(value));
86   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
87   if (co->validReg(regNum)) {
88     *value = co->getReg(regNum);
89     return UNW_ESUCCESS;
90   }
91   return UNW_EBADREG;
92 }
_LIBUNWIND_WEAK_ALIAS(__unw_get_reg,unw_get_reg)93 _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg)
94 
95 /// Set value of specified register at cursor position in stack frame.
96 _LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
97                                     unw_word_t value) {
98   _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR
99                        ")",
100                        static_cast<void *>(cursor), regNum, value);
101   typedef LocalAddressSpace::pint_t pint_t;
102   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
103   if (co->validReg(regNum)) {
104     co->setReg(regNum, (pint_t)value);
105     // specical case altering IP to re-find info (being called by personality
106     // function)
107     if (regNum == UNW_REG_IP) {
108       unw_proc_info_t info;
109       // First, get the FDE for the old location and then update it.
110       co->getInfo(&info);
111       co->setInfoBasedOnIPRegister(false);
112       // If the original call expects stack adjustment, perform this now.
113       // Normal frame unwinding would have included the offset already in the
114       // CFA computation.
115       // Note: for PA-RISC and other platforms where the stack grows up,
116       // this should actually be - info.gp. LLVM doesn't currently support
117       // any such platforms and Clang doesn't export a macro for them.
118       if (info.gp)
119         co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp);
120     }
121     return UNW_ESUCCESS;
122   }
123   return UNW_EBADREG;
124 }
_LIBUNWIND_WEAK_ALIAS(__unw_set_reg,unw_set_reg)125 _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg)
126 
127 /// Get value of specified float register at cursor position in stack frame.
128 _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
129                                       unw_fpreg_t *value) {
130   _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
131                        static_cast<void *>(cursor), regNum,
132                        static_cast<void *>(value));
133   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
134   if (co->validFloatReg(regNum)) {
135     *value = co->getFloatReg(regNum);
136     return UNW_ESUCCESS;
137   }
138   return UNW_EBADREG;
139 }
_LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg,unw_get_fpreg)140 _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg)
141 
142 /// Set value of specified float register at cursor position in stack frame.
143 _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
144                                       unw_fpreg_t value) {
145 #if defined(_LIBUNWIND_ARM_EHABI)
146   _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
147                        static_cast<void *>(cursor), regNum, value);
148 #else
149   _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
150                        static_cast<void *>(cursor), regNum, value);
151 #endif
152   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
153   if (co->validFloatReg(regNum)) {
154     co->setFloatReg(regNum, value);
155     return UNW_ESUCCESS;
156   }
157   return UNW_EBADREG;
158 }
_LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg,unw_set_fpreg)159 _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg)
160 
161 /// Move cursor to next frame.
162 _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) {
163   _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor));
164   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
165   return co->step();
166 }
_LIBUNWIND_WEAK_ALIAS(__unw_step,unw_step)167 _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step)
168 
169 /// Get unwind info at cursor position in stack frame.
170 _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor,
171                                           unw_proc_info_t *info) {
172   _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)",
173                        static_cast<void *>(cursor), static_cast<void *>(info));
174   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
175   co->getInfo(info);
176   if (info->end_ip == 0)
177     return UNW_ENOINFO;
178   return UNW_ESUCCESS;
179 }
_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info,unw_get_proc_info)180 _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info)
181 
182 /// Resume execution at cursor position (aka longjump).
183 _LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) {
184   _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor));
185   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
186   co->jumpto();
187   return UNW_EUNSPEC;
188 }
_LIBUNWIND_WEAK_ALIAS(__unw_resume,unw_resume)189 _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume)
190 
191 /// Get name of function at cursor position in stack frame.
192 _LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf,
193                                           size_t bufLen, unw_word_t *offset) {
194   _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
195                        static_cast<void *>(cursor), static_cast<void *>(buf),
196                        static_cast<unsigned long>(bufLen));
197   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
198   if (co->getFunctionName(buf, bufLen, offset))
199     return UNW_ESUCCESS;
200   return UNW_EUNSPEC;
201 }
_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name,unw_get_proc_name)202 _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name)
203 
204 /// Checks if a register is a floating-point register.
205 _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor,
206                                      unw_regnum_t regNum) {
207   _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)",
208                        static_cast<void *>(cursor), regNum);
209   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
210   return co->validFloatReg(regNum);
211 }
_LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg,unw_is_fpreg)212 _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg)
213 
214 /// Checks if a register is a floating-point register.
215 _LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor,
216                                             unw_regnum_t regNum) {
217   _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)",
218                        static_cast<void *>(cursor), regNum);
219   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
220   return co->getRegisterName(regNum);
221 }
_LIBUNWIND_WEAK_ALIAS(__unw_regname,unw_regname)222 _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname)
223 
224 /// Checks if current frame is signal trampoline.
225 _LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) {
226   _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)",
227                        static_cast<void *>(cursor));
228   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
229   return co->isSignalFrame();
230 }
_LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame,unw_is_signal_frame)231 _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame)
232 
233 #ifdef __arm__
234 // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
235 _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) {
236   _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)",
237                        static_cast<void *>(cursor));
238   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
239   return co->saveVFPAsX();
240 }
_LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X,unw_save_vfp_as_X)241 _LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X)
242 #endif
243 
244 
245 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
246 /// SPI: walks cached DWARF entries
247 _LIBUNWIND_HIDDEN 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)) {
249   _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)",
250                        reinterpret_cast<void *>(func));
251   DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
252 }
_LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache,unw_iterate_dwarf_unwind_cache)253 _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache,
254                       unw_iterate_dwarf_unwind_cache)
255 
256 /// IPI: for __register_frame()
257 void __unw_add_dynamic_fde(unw_word_t fde) {
258   CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
259   CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
260   const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
261                            LocalAddressSpace::sThisAddressSpace,
262                           (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
263   if (message == NULL) {
264     // dynamically registered FDEs don't have a mach_header group they are in.
265     // Use fde as mh_group
266     unw_word_t mh_group = fdeInfo.fdeStart;
267     DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
268                                           fdeInfo.pcStart, fdeInfo.pcEnd,
269                                           fdeInfo.fdeStart);
270   } else {
271     _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message);
272   }
273 }
274 
275 /// IPI: for __deregister_frame()
__unw_remove_dynamic_fde(unw_word_t fde)276 void __unw_remove_dynamic_fde(unw_word_t fde) {
277   // fde is own mh_group
278   DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
279 }
280 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
281 #endif // !defined(__USING_SJLJ_EXCEPTIONS__)
282 
283 
284 
285 // Add logging hooks in Debug builds only
286 #ifndef NDEBUG
287 #include <stdlib.h>
288 
289 _LIBUNWIND_HIDDEN
logAPIs()290 bool logAPIs() {
291   // do manual lock to avoid use of _cxa_guard_acquire or initializers
292   static bool checked = false;
293   static bool log = false;
294   if (!checked) {
295     log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
296     checked = true;
297   }
298   return log;
299 }
300 
301 _LIBUNWIND_HIDDEN
logUnwinding()302 bool logUnwinding() {
303   // do manual lock to avoid use of _cxa_guard_acquire or initializers
304   static bool checked = false;
305   static bool log = false;
306   if (!checked) {
307     log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
308     checked = true;
309   }
310   return log;
311 }
312 
313 _LIBUNWIND_HIDDEN
logDWARF()314 bool logDWARF() {
315   // do manual lock to avoid use of _cxa_guard_acquire or initializers
316   static bool checked = false;
317   static bool log = false;
318   if (!checked) {
319     log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
320     checked = true;
321   }
322   return log;
323 }
324 
325 #endif // NDEBUG
326 
327