Louis Dionne | 7f068e5 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
| 8 | // Implements unw_* functions from <libunwind.h> |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include <libunwind.h> |
| 13 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 14 | #include "config.h" |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 15 | #include "libunwind_ext.h" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
Saleem Abdulrasool | 7e85c7a | 2021-06-13 14:28:43 -0700 | [diff] [blame] | 19 | // Define the __has_feature extension for compilers that do not support it so |
| 20 | // that we can later check for the presence of ASan in a compiler-neutral way. |
| 21 | #if !defined(__has_feature) |
| 22 | #define __has_feature(feature) 0 |
| 23 | #endif |
| 24 | |
| 25 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
Shoaib Meenai | c8d0fb8 | 2021-05-23 20:41:57 -0700 | [diff] [blame] | 26 | #include <sanitizer/asan_interface.h> |
| 27 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 28 | |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 29 | #if !defined(__USING_SJLJ_EXCEPTIONS__) |
Ed Schouten | edb7680 | 2017-03-09 08:04:07 +0000 | [diff] [blame] | 30 | #include "AddressSpace.hpp" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 31 | #include "UnwindCursor.hpp" |
| 32 | |
| 33 | using namespace libunwind; |
| 34 | |
| 35 | /// internal object to represent this processes address space |
| 36 | LocalAddressSpace LocalAddressSpace::sThisAddressSpace; |
| 37 | |
| 38 | _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space = |
| 39 | (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace; |
| 40 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 41 | /// Create a cursor of a thread in this process given 'context' recorded by |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 42 | /// __unw_getcontext(). |
| 43 | _LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor, |
| 44 | unw_context_t *context) { |
| 45 | _LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 46 | static_cast<void *>(cursor), |
| 47 | static_cast<void *>(context)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 48 | #if defined(__i386__) |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 49 | # define REGISTER_KIND Registers_x86 |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 50 | #elif defined(__x86_64__) |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 51 | # define REGISTER_KIND Registers_x86_64 |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 52 | #elif defined(__powerpc64__) |
| 53 | # define REGISTER_KIND Registers_ppc64 |
Sam James | 7246413 | 2022-01-27 21:48:38 +0000 | [diff] [blame] | 54 | #elif defined(__powerpc__) |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 55 | # define REGISTER_KIND Registers_ppc |
| 56 | #elif defined(__aarch64__) |
| 57 | # define REGISTER_KIND Registers_arm64 |
Martin Storsjo | a72285f | 2017-11-02 08:16:16 +0000 | [diff] [blame] | 58 | #elif defined(__arm__) |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 59 | # define REGISTER_KIND Registers_arm |
Peter Zotov | 8d63999 | 2015-08-31 05:26:37 +0000 | [diff] [blame] | 60 | #elif defined(__or1k__) |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 61 | # define REGISTER_KIND Registers_or1k |
Brian Cain | b432c47 | 2020-04-09 00:14:02 -0500 | [diff] [blame] | 62 | #elif defined(__hexagon__) |
| 63 | # define REGISTER_KIND Registers_hexagon |
John Baldwin | 4030029 | 2018-05-15 22:44:56 +0000 | [diff] [blame] | 64 | #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 65 | # define REGISTER_KIND Registers_mips_o32 |
John Baldwin | 4030029 | 2018-05-15 22:44:56 +0000 | [diff] [blame] | 66 | #elif defined(__mips64) |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 67 | # define REGISTER_KIND Registers_mips_newabi |
Vasileios Kalintiris | 3bd271c | 2015-09-26 18:26:01 +0000 | [diff] [blame] | 68 | #elif defined(__mips__) |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 69 | # warning The MIPS architecture is not supported with this ABI and environment! |
Koakuma | f2ef96e | 2022-02-05 13:08:26 -0800 | [diff] [blame] | 70 | #elif defined(__sparc__) && defined(__arch64__) |
| 71 | #define REGISTER_KIND Registers_sparc64 |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 72 | #elif defined(__sparc__) |
| 73 | # define REGISTER_KIND Registers_sparc |
Kamlesh Kumar | f6ac3de | 2021-03-02 06:57:54 +0530 | [diff] [blame] | 74 | #elif defined(__riscv) |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 75 | # define REGISTER_KIND Registers_riscv |
Kazushi (Jam) Marukawa | 1fe8aef | 2020-12-26 22:50:17 +0900 | [diff] [blame] | 76 | #elif defined(__ve__) |
| 77 | # define REGISTER_KIND Registers_ve |
Ulrich Weigand | 393e3ee | 2022-05-02 14:35:29 +0200 | [diff] [blame] | 78 | #elif defined(__s390x__) |
| 79 | # define REGISTER_KIND Registers_s390x |
Ed Maste | 846892c | 2015-08-13 14:21:03 +0000 | [diff] [blame] | 80 | #else |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 81 | # error Architecture not supported |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 82 | #endif |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 83 | // Use "placement new" to allocate UnwindCursor in the cursor buffer. |
Petr Hosek | 36f6154 | 2019-02-02 21:15:49 +0000 | [diff] [blame] | 84 | new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) |
| 85 | UnwindCursor<LocalAddressSpace, REGISTER_KIND>( |
| 86 | context, LocalAddressSpace::sThisAddressSpace); |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 87 | #undef REGISTER_KIND |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 88 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 89 | co->setInfoBasedOnIPRegister(); |
| 90 | |
| 91 | return UNW_ESUCCESS; |
| 92 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 93 | _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 94 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 95 | /// Get value of specified register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 96 | _LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 97 | unw_word_t *value) { |
| 98 | _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 99 | static_cast<void *>(cursor), regNum, |
| 100 | static_cast<void *>(value)); |
| 101 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 102 | if (co->validReg(regNum)) { |
| 103 | *value = co->getReg(regNum); |
| 104 | return UNW_ESUCCESS; |
| 105 | } |
| 106 | return UNW_EBADREG; |
| 107 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 108 | _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 109 | |
| 110 | /// Set value of specified register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 111 | _LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 112 | unw_word_t value) { |
| 113 | _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR |
| 114 | ")", |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 115 | static_cast<void *>(cursor), regNum, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 116 | typedef LocalAddressSpace::pint_t pint_t; |
| 117 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 118 | if (co->validReg(regNum)) { |
| 119 | co->setReg(regNum, (pint_t)value); |
Gabriel Ravier | 42aa6de | 2022-08-20 18:09:03 -0700 | [diff] [blame] | 120 | // special case altering IP to re-find info (being called by personality |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 121 | // function) |
Joerg Sonnenberger | 7ac54d3 | 2018-07-17 19:00:51 +0000 | [diff] [blame] | 122 | if (regNum == UNW_REG_IP) { |
| 123 | unw_proc_info_t info; |
| 124 | // First, get the FDE for the old location and then update it. |
| 125 | co->getInfo(&info); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 126 | co->setInfoBasedOnIPRegister(false); |
Joerg Sonnenberger | 7ac54d3 | 2018-07-17 19:00:51 +0000 | [diff] [blame] | 127 | // If the original call expects stack adjustment, perform this now. |
| 128 | // Normal frame unwinding would have included the offset already in the |
| 129 | // CFA computation. |
| 130 | // Note: for PA-RISC and other platforms where the stack grows up, |
| 131 | // this should actually be - info.gp. LLVM doesn't currently support |
| 132 | // any such platforms and Clang doesn't export a macro for them. |
| 133 | if (info.gp) |
| 134 | co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); |
| 135 | } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 136 | return UNW_ESUCCESS; |
| 137 | } |
| 138 | return UNW_EBADREG; |
| 139 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 140 | _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 141 | |
| 142 | /// Get value of specified float register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 143 | _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 144 | unw_fpreg_t *value) { |
| 145 | _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 146 | static_cast<void *>(cursor), regNum, |
| 147 | static_cast<void *>(value)); |
| 148 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 149 | if (co->validFloatReg(regNum)) { |
| 150 | *value = co->getFloatReg(regNum); |
| 151 | return UNW_ESUCCESS; |
| 152 | } |
| 153 | return UNW_EBADREG; |
| 154 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 155 | _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 156 | |
| 157 | /// Set value of specified float register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 158 | _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 159 | unw_fpreg_t value) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 160 | #if defined(_LIBUNWIND_ARM_EHABI) |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 161 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 162 | static_cast<void *>(cursor), regNum, value); |
| 163 | #else |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 164 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 165 | static_cast<void *>(cursor), regNum, value); |
| 166 | #endif |
| 167 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 168 | if (co->validFloatReg(regNum)) { |
| 169 | co->setFloatReg(regNum, value); |
| 170 | return UNW_ESUCCESS; |
| 171 | } |
| 172 | return UNW_EBADREG; |
| 173 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 174 | _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 175 | |
| 176 | /// Move cursor to next frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 177 | _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { |
| 178 | _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 179 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 180 | return co->step(); |
| 181 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 182 | _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 183 | |
| 184 | /// Get unwind info at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 185 | _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, |
| 186 | unw_proc_info_t *info) { |
| 187 | _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 188 | static_cast<void *>(cursor), static_cast<void *>(info)); |
| 189 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 190 | co->getInfo(info); |
| 191 | if (info->end_ip == 0) |
| 192 | return UNW_ENOINFO; |
Saleem Abdulrasool | 2aa34a8 | 2019-09-18 16:15:56 +0000 | [diff] [blame] | 193 | return UNW_ESUCCESS; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 194 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 195 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 196 | |
| 197 | /// Resume execution at cursor position (aka longjump). |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 198 | _LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) { |
| 199 | _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor)); |
Saleem Abdulrasool | 7e85c7a | 2021-06-13 14:28:43 -0700 | [diff] [blame] | 200 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
Shoaib Meenai | c8d0fb8 | 2021-05-23 20:41:57 -0700 | [diff] [blame] | 201 | // Inform the ASan runtime that now might be a good time to clean stuff up. |
| 202 | __asan_handle_no_return(); |
| 203 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 204 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 205 | co->jumpto(); |
| 206 | return UNW_EUNSPEC; |
| 207 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 208 | _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 209 | |
| 210 | /// Get name of function at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 211 | _LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf, |
| 212 | size_t bufLen, unw_word_t *offset) { |
| 213 | _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 214 | static_cast<void *>(cursor), static_cast<void *>(buf), |
| 215 | static_cast<unsigned long>(bufLen)); |
| 216 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 217 | if (co->getFunctionName(buf, bufLen, offset)) |
| 218 | return UNW_ESUCCESS; |
Saleem Abdulrasool | 2aa34a8 | 2019-09-18 16:15:56 +0000 | [diff] [blame] | 219 | return UNW_EUNSPEC; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 220 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 221 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 222 | |
| 223 | /// Checks if a register is a floating-point register. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 224 | _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, |
| 225 | unw_regnum_t regNum) { |
| 226 | _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 227 | static_cast<void *>(cursor), regNum); |
| 228 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 229 | return co->validFloatReg(regNum); |
| 230 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 231 | _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 232 | |
| 233 | /// Checks if a register is a floating-point register. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 234 | _LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor, |
| 235 | unw_regnum_t regNum) { |
| 236 | _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 237 | static_cast<void *>(cursor), regNum); |
| 238 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 239 | return co->getRegisterName(regNum); |
| 240 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 241 | _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 242 | |
| 243 | /// Checks if current frame is signal trampoline. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 244 | _LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) { |
| 245 | _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 246 | static_cast<void *>(cursor)); |
| 247 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 248 | return co->isSignalFrame(); |
| 249 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 250 | _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 251 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 252 | #ifdef _AIX |
| 253 | _LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) { |
| 254 | _LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)", |
| 255 | static_cast<void *>(cursor)); |
| 256 | AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); |
| 257 | return co->getDataRelBase(); |
| 258 | } |
| 259 | _LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base) |
| 260 | #endif |
| 261 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 262 | #ifdef __arm__ |
| 263 | // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 264 | _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { |
| 265 | _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 266 | static_cast<void *>(cursor)); |
| 267 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 268 | return co->saveVFPAsX(); |
| 269 | } |
Petr Hosek | 01fc413 | 2019-04-11 13:08:44 +0000 | [diff] [blame] | 270 | _LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 271 | #endif |
| 272 | |
| 273 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 274 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Ed Maste | 4c43c3d | 2016-07-19 17:15:50 +0000 | [diff] [blame] | 275 | /// SPI: walks cached DWARF entries |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 276 | _LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 277 | unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 278 | _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 279 | reinterpret_cast<void *>(func)); |
| 280 | DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); |
| 281 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 282 | _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, |
| 283 | unw_iterate_dwarf_unwind_cache) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 284 | |
| 285 | /// IPI: for __register_frame() |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 286 | void __unw_add_dynamic_fde(unw_word_t fde) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 287 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 288 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 289 | const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 290 | LocalAddressSpace::sThisAddressSpace, |
| 291 | (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); |
| 292 | if (message == NULL) { |
| 293 | // dynamically registered FDEs don't have a mach_header group they are in. |
| 294 | // Use fde as mh_group |
| 295 | unw_word_t mh_group = fdeInfo.fdeStart; |
| 296 | DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, |
| 297 | fdeInfo.pcStart, fdeInfo.pcEnd, |
| 298 | fdeInfo.fdeStart); |
| 299 | } else { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 300 | _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | /// IPI: for __deregister_frame() |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 305 | void __unw_remove_dynamic_fde(unw_word_t fde) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 306 | // fde is own mh_group |
| 307 | DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); |
| 308 | } |
Peter S. Housel | 038090f | 2021-10-14 13:31:05 -0700 | [diff] [blame] | 309 | |
| 310 | void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 311 | // The eh_frame section start serves as the mh_group |
| 312 | unw_word_t mh_group = eh_frame_start; |
| 313 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 314 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 315 | auto p = (LocalAddressSpace::pint_t)eh_frame_start; |
| 316 | while (true) { |
| 317 | if (CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 318 | LocalAddressSpace::sThisAddressSpace, p, &fdeInfo, &cieInfo, |
| 319 | true) == NULL) { |
| 320 | DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, |
| 321 | fdeInfo.pcStart, fdeInfo.pcEnd, |
| 322 | fdeInfo.fdeStart); |
| 323 | p += fdeInfo.fdeLength; |
| 324 | } else if (CFI_Parser<LocalAddressSpace>::parseCIE( |
| 325 | LocalAddressSpace::sThisAddressSpace, p, &cieInfo) == NULL) { |
| 326 | p += cieInfo.cieLength; |
| 327 | } else |
| 328 | return; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 333 | // The eh_frame section start serves as the mh_group |
| 334 | DwarfFDECache<LocalAddressSpace>::removeAllIn( |
| 335 | (LocalAddressSpace::pint_t)eh_frame_start); |
| 336 | } |
| 337 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 338 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 339 | #endif // !defined(__USING_SJLJ_EXCEPTIONS__) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | |
| 343 | // Add logging hooks in Debug builds only |
| 344 | #ifndef NDEBUG |
| 345 | #include <stdlib.h> |
| 346 | |
| 347 | _LIBUNWIND_HIDDEN |
| 348 | bool logAPIs() { |
| 349 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 350 | static bool checked = false; |
| 351 | static bool log = false; |
| 352 | if (!checked) { |
| 353 | log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); |
| 354 | checked = true; |
| 355 | } |
| 356 | return log; |
| 357 | } |
| 358 | |
| 359 | _LIBUNWIND_HIDDEN |
| 360 | bool logUnwinding() { |
| 361 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 362 | static bool checked = false; |
| 363 | static bool log = false; |
| 364 | if (!checked) { |
| 365 | log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); |
| 366 | checked = true; |
| 367 | } |
| 368 | return log; |
| 369 | } |
| 370 | |
Saleem Abdulrasool | d1be5b0 | 2017-01-21 16:22:57 +0000 | [diff] [blame] | 371 | _LIBUNWIND_HIDDEN |
| 372 | bool logDWARF() { |
| 373 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 374 | static bool checked = false; |
| 375 | static bool log = false; |
| 376 | if (!checked) { |
| 377 | log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); |
| 378 | checked = true; |
| 379 | } |
| 380 | return log; |
| 381 | } |
| 382 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 383 | #endif // NDEBUG |
| 384 | |