1 //===--------------------------- libuwind.cpp -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Implements unw_* functions from <libunwind.h>
11 //===----------------------------------------------------------------------===//
13 #include <libunwind.h>
17 #include "libunwind_ext.h"
21 #if _LIBUNWIND_BUILD_ZERO_COST_APIS
23 #include "UnwindCursor.hpp"
25 using namespace libunwind;
27 /// internal object to represent this processes address space
28 LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
30 /// record the registers and stack position of the caller
31 extern int unw_getcontext(unw_context_t *);
32 // note: unw_getcontext() implemented in assembly
34 /// Create a cursor of a thread in this process given 'context' recorded by
36 _LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
37 unw_context_t *context) {
38 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)\n",
40 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
42 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86>(
43 context, LocalAddressSpace::sThisAddressSpace);
45 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86_64>(
46 context, LocalAddressSpace::sThisAddressSpace);
48 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_ppc>(
49 context, LocalAddressSpace::sThisAddressSpace);
51 new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_arm64>(
52 context, LocalAddressSpace::sThisAddressSpace);
54 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
55 co->setInfoBasedOnIPRegister();
62 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
63 (unw_addr_space_t) & sThisAddressSpace;
65 /// Create a cursor into a thread in another process.
66 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
69 // special case: unw_init_remote(xx, unw_local_addr_space, xx)
70 if (as == (unw_addr_space_t) & sThisAddressSpace)
71 return unw_init_local(cursor, NULL); //FIXME
73 // use "placement new" to allocate UnwindCursor in the cursor buffer
74 switch (as->cpuType) {
77 UnwindCursor<OtherAddressSpace<Pointer32<LittleEndian> >,
78 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
81 new ((void *)cursor) UnwindCursor<
82 OtherAddressSpace<Pointer64<LittleEndian> >, Registers_x86_64>(
83 ((unw_addr_space_x86_64 *)as)->oas, arg);
85 case CPU_TYPE_POWERPC:
87 UnwindCursor<OtherAddressSpace<Pointer32<BigEndian> >, Registers_ppc>(
88 ((unw_addr_space_ppc *)as)->oas, arg);
97 static bool is64bit(task_t task) {
98 return false; // FIXME
101 /// Create an address_space object for use in examining another task.
102 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
105 unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
107 as->cpuType = CPU_TYPE_X86_64;
110 unw_addr_space_i386 *as = new unw_addr_space_i386(task);
112 as->cpuType = CPU_TYPE_I386;
121 /// Delete an address_space object.
122 _LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
123 switch (asp->cpuType) {
124 #if __i386__ || __x86_64__
125 case CPU_TYPE_I386: {
126 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
130 case CPU_TYPE_X86_64: {
131 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
136 case CPU_TYPE_POWERPC: {
137 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
146 /// Get value of specified register at cursor position in stack frame.
147 _LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
149 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)\n",
150 cursor, regNum, value);
151 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
152 if (co->validReg(regNum)) {
153 *value = co->getReg(regNum);
160 /// Set value of specified register at cursor position in stack frame.
161 _LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
163 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%llX)\n",
164 cursor, regNum, value);
165 typedef LocalAddressSpace::pint_t pint_t;
166 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
167 if (co->validReg(regNum)) {
168 co->setReg(regNum, (pint_t)value);
169 // specical case altering IP to re-find info (being called by personality
171 if (regNum == UNW_REG_IP)
172 co->setInfoBasedOnIPRegister(false);
179 /// Get value of specified float register at cursor position in stack frame.
180 _LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
181 unw_fpreg_t *value) {
182 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)\n",
183 cursor, regNum, value);
184 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
185 if (co->validFloatReg(regNum)) {
186 *value = co->getFloatReg(regNum);
193 /// Set value of specified float register at cursor position in stack frame.
194 _LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
196 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)\n",
197 cursor, regNum, value);
198 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
199 if (co->validFloatReg(regNum)) {
200 co->setFloatReg(regNum, value);
207 /// Move cursor to next frame.
208 _LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
209 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)\n", cursor);
210 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
215 /// Get unwind info at cursor position in stack frame.
216 _LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
217 unw_proc_info_t *info) {
218 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)\n",
220 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
222 if (info->end_ip == 0)
229 /// Resume execution at cursor position (aka longjump).
230 _LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
231 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)\n", cursor);
232 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
238 /// Get name of function at cursor position in stack frame.
239 _LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
240 size_t bufLen, unw_word_t *offset) {
241 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p,"
242 "bufLen=%ld)\n", cursor, buf, bufLen);
243 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
244 if (co->getFunctionName(buf, bufLen, offset))
251 /// Checks if a register is a floating-point register.
252 _LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
253 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)\n",
255 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
256 return co->validFloatReg(regNum);
260 /// Checks if a register is a floating-point register.
261 _LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
262 unw_regnum_t regNum) {
263 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)\n",
265 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
266 return co->getRegisterName(regNum);
270 /// Checks if current frame is signal trampoline.
271 _LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
272 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)\n", cursor);
273 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
274 return co->isSignalFrame();
278 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
279 /// SPI: walks cached dwarf entries
280 _LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
281 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
282 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)\n", func);
283 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
287 /// IPI: for __register_frame()
288 void _unw_add_dynamic_fde(unw_word_t fde) {
289 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
290 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
291 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
292 LocalAddressSpace::sThisAddressSpace,
293 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
294 if (message == NULL) {
295 // dynamically registered FDEs don't have a mach_header group they are in.
296 // Use fde as mh_group
297 unw_word_t mh_group = fdeInfo.fdeStart;
298 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
299 fdeInfo.pcStart, fdeInfo.pcEnd,
302 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
306 /// IPI: for __deregister_frame()
307 void _unw_remove_dynamic_fde(unw_word_t fde) {
308 // fde is own mh_group
309 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
311 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
313 #endif // _LIBUNWIND_BUILD_ZERO_COST_APIS
317 // Add logging hooks in Debug builds only
323 // do manual lock to avoid use of _cxa_guard_acquire or initializers
324 static bool checked = false;
325 static bool log = false;
327 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
334 bool logUnwinding() {
335 // do manual lock to avoid use of _cxa_guard_acquire or initializers
336 static bool checked = false;
337 static bool log = false;
339 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);