]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libunwind/src/libunwind.cpp
Import DTS files from Linux 5.3
[FreeBSD/FreeBSD.git] / contrib / libunwind / src / libunwind.cpp
1 //===--------------------------- libunwind.cpp ----------------------------===//
2 //
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>
19 #include <algorithm>
20
21 #include "libunwind_ext.h"
22 #include "config.h"
23
24 #include <stdlib.h>
25
26
27 #if !defined(__USING_SJLJ_EXCEPTIONS__)
28 #include "AddressSpace.hpp"
29 #include "UnwindCursor.hpp"
30
31 using namespace libunwind;
32
33 /// internal object to represent this processes address space
34 LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
35
36 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
37     (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
38
39 /// record the registers and stack position of the caller
40 extern int unw_getcontext(unw_context_t *);
41 // note: unw_getcontext() implemented in assembly
42
43 /// Create a cursor of a thread in this process given 'context' recorded by
44 /// unw_getcontext().
45 _LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
46                                      unw_context_t *context) {
47   _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
48                        static_cast<void *>(cursor),
49                        static_cast<void *>(context));
50 #if defined(__i386__)
51 # define REGISTER_KIND Registers_x86
52 #elif defined(__x86_64__)
53 # define REGISTER_KIND Registers_x86_64
54 #elif defined(__powerpc64__)
55 # define REGISTER_KIND Registers_ppc64
56 #elif defined(__ppc__)
57 # define REGISTER_KIND Registers_ppc
58 #elif defined(__aarch64__)
59 # define REGISTER_KIND Registers_arm64
60 #elif defined(__arm__)
61 # define REGISTER_KIND Registers_arm
62 #elif defined(__or1k__)
63 # define REGISTER_KIND Registers_or1k
64 #elif defined(__riscv)
65 # define REGISTER_KIND Registers_riscv
66 #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
67 # define REGISTER_KIND Registers_mips_o32
68 #elif defined(__mips64)
69 # define REGISTER_KIND Registers_mips_newabi
70 #elif defined(__mips__)
71 # warning The MIPS architecture is not supported with this ABI and environment!
72 #elif defined(__sparc__)
73 # define REGISTER_KIND Registers_sparc
74 #else
75 # error Architecture not supported
76 #endif
77   // Use "placement new" to allocate UnwindCursor in the cursor buffer.
78   new ((void *)cursor) UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
79                                  context, LocalAddressSpace::sThisAddressSpace);
80 #undef REGISTER_KIND
81   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
82   co->setInfoBasedOnIPRegister();
83
84   return UNW_ESUCCESS;
85 }
86
87 #ifdef UNW_REMOTE
88 /// Create a cursor into a thread in another process.
89 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
90                                              unw_addr_space_t as,
91                                              void *arg) {
92   // special case: unw_init_remote(xx, unw_local_addr_space, xx)
93   if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace)
94     return unw_init_local(cursor, NULL); //FIXME
95
96   // use "placement new" to allocate UnwindCursor in the cursor buffer
97   switch (as->cpuType) {
98   case CPU_TYPE_I386:
99     new ((void *)cursor)
100         UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>,
101                      Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
102     break;
103   case CPU_TYPE_X86_64:
104     new ((void *)cursor)
105         UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>,
106                      Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg);
107     break;
108   case CPU_TYPE_POWERPC:
109     new ((void *)cursor)
110         UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>,
111                      Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg);
112     break;
113   default:
114     return UNW_EUNSPEC;
115   }
116   return UNW_ESUCCESS;
117 }
118
119
120 static bool is64bit(task_t task) {
121   return false; // FIXME
122 }
123
124 /// Create an address_space object for use in examining another task.
125 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
126 #if __i386__
127   if (is64bit(task)) {
128     unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
129     as->taskPort = task;
130     as->cpuType = CPU_TYPE_X86_64;
131     //as->oas
132   } else {
133     unw_addr_space_i386 *as = new unw_addr_space_i386(task);
134     as->taskPort = task;
135     as->cpuType = CPU_TYPE_I386;
136     //as->oas
137   }
138 #else
139 // FIXME
140 #endif
141 }
142
143
144 /// Delete an address_space object.
145 _LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
146   switch (asp->cpuType) {
147 #if __i386__ || __x86_64__
148   case CPU_TYPE_I386: {
149     unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
150     delete as;
151   }
152   break;
153   case CPU_TYPE_X86_64: {
154     unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
155     delete as;
156   }
157   break;
158 #endif
159   case CPU_TYPE_POWERPC: {
160     unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
161     delete as;
162   }
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) {
172   _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
173                        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) {
187   _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")",
188                        static_cast<void *>(cursor), regNum, value);
189   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)
195     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);
199       co->setInfoBasedOnIPRegister(false);
200       // 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     }
209     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) {
218   _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
219                        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) {
233 #if defined(_LIBUNWIND_ARM_EHABI)
234   _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
235                        static_cast<void *>(cursor), regNum, value);
236 #else
237   _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
238                        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) {
251   _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
252   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) {
260   _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
261                        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) {
273   _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
274   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) {
283   _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
284                        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) {
296   _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
297                        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) {
306   _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
307                        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) {
315   _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
316                        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) {
324   _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
325                        static_cast<void *>(cursor));
326   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
327   return co->saveVFPAsX();
328 }
329 #endif
330
331
332 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
333 /// SPI: walks cached DWARF entries
334 _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)) {
336   _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
337                        reinterpret_cast<void *>(func));
338   DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
339 }
340
341
342 /// IPI: for __register_frame()
343 void _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()
362 void _unw_remove_dynamic_fde(unw_word_t fde) {
363   // fde is own mh_group
364   DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
365 }
366 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
367 #endif // !defined(__USING_SJLJ_EXCEPTIONS__)
368
369
370
371 // Add logging hooks in Debug builds only
372 #ifndef NDEBUG
373 #include <stdlib.h>
374
375 _LIBUNWIND_HIDDEN
376 bool 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
388 bool 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
399 _LIBUNWIND_HIDDEN
400 bool 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
411 #endif // NDEBUG
412