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 |
zhanglimin | 5870472 | 2022-11-15 14:36:30 +0800 | [diff] [blame^] | 80 | #elif defined(__loongarch__) && __loongarch_grlen == 64 |
| 81 | #define REGISTER_KIND Registers_loongarch |
Ed Maste | 846892c | 2015-08-13 14:21:03 +0000 | [diff] [blame] | 82 | #else |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 83 | # error Architecture not supported |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 84 | #endif |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 85 | // Use "placement new" to allocate UnwindCursor in the cursor buffer. |
Petr Hosek | 36f6154 | 2019-02-02 21:15:49 +0000 | [diff] [blame] | 86 | new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) |
| 87 | UnwindCursor<LocalAddressSpace, REGISTER_KIND>( |
| 88 | context, LocalAddressSpace::sThisAddressSpace); |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 89 | #undef REGISTER_KIND |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 90 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 91 | co->setInfoBasedOnIPRegister(); |
| 92 | |
| 93 | return UNW_ESUCCESS; |
| 94 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 95 | _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 96 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 97 | /// Get value of specified register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 98 | _LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 99 | unw_word_t *value) { |
| 100 | _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 101 | static_cast<void *>(cursor), regNum, |
| 102 | static_cast<void *>(value)); |
| 103 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 104 | if (co->validReg(regNum)) { |
| 105 | *value = co->getReg(regNum); |
| 106 | return UNW_ESUCCESS; |
| 107 | } |
| 108 | return UNW_EBADREG; |
| 109 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 110 | _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 111 | |
| 112 | /// Set value of specified register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 113 | _LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 114 | unw_word_t value) { |
| 115 | _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR |
| 116 | ")", |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 117 | static_cast<void *>(cursor), regNum, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 118 | typedef LocalAddressSpace::pint_t pint_t; |
| 119 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 120 | if (co->validReg(regNum)) { |
| 121 | co->setReg(regNum, (pint_t)value); |
Gabriel Ravier | 42aa6de | 2022-08-20 18:09:03 -0700 | [diff] [blame] | 122 | // special case altering IP to re-find info (being called by personality |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 123 | // function) |
Joerg Sonnenberger | 7ac54d3 | 2018-07-17 19:00:51 +0000 | [diff] [blame] | 124 | if (regNum == UNW_REG_IP) { |
| 125 | unw_proc_info_t info; |
| 126 | // First, get the FDE for the old location and then update it. |
| 127 | co->getInfo(&info); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 128 | co->setInfoBasedOnIPRegister(false); |
Joerg Sonnenberger | 7ac54d3 | 2018-07-17 19:00:51 +0000 | [diff] [blame] | 129 | // If the original call expects stack adjustment, perform this now. |
| 130 | // Normal frame unwinding would have included the offset already in the |
| 131 | // CFA computation. |
| 132 | // Note: for PA-RISC and other platforms where the stack grows up, |
| 133 | // this should actually be - info.gp. LLVM doesn't currently support |
| 134 | // any such platforms and Clang doesn't export a macro for them. |
| 135 | if (info.gp) |
| 136 | co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); |
| 137 | } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 138 | return UNW_ESUCCESS; |
| 139 | } |
| 140 | return UNW_EBADREG; |
| 141 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 142 | _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 143 | |
| 144 | /// Get value of specified float register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 145 | _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 146 | unw_fpreg_t *value) { |
| 147 | _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 148 | static_cast<void *>(cursor), regNum, |
| 149 | static_cast<void *>(value)); |
| 150 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 151 | if (co->validFloatReg(regNum)) { |
| 152 | *value = co->getFloatReg(regNum); |
| 153 | return UNW_ESUCCESS; |
| 154 | } |
| 155 | return UNW_EBADREG; |
| 156 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 157 | _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 158 | |
| 159 | /// Set value of specified float register at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 160 | _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 161 | unw_fpreg_t value) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 162 | #if defined(_LIBUNWIND_ARM_EHABI) |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 163 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 164 | static_cast<void *>(cursor), regNum, value); |
| 165 | #else |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 166 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 167 | static_cast<void *>(cursor), regNum, value); |
| 168 | #endif |
| 169 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 170 | if (co->validFloatReg(regNum)) { |
| 171 | co->setFloatReg(regNum, value); |
| 172 | return UNW_ESUCCESS; |
| 173 | } |
| 174 | return UNW_EBADREG; |
| 175 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 176 | _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 177 | |
| 178 | /// Move cursor to next frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 179 | _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { |
| 180 | _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 181 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 182 | return co->step(); |
| 183 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 184 | _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 185 | |
Florian Mayer | 7ff728a | 2022-06-03 14:33:08 -0700 | [diff] [blame] | 186 | // Move cursor to next frame and for stage2 of unwinding. |
| 187 | // This resets MTE tags of tagged frames to zero. |
| 188 | extern "C" _LIBUNWIND_HIDDEN int __unw_step_stage2(unw_cursor_t *cursor) { |
| 189 | _LIBUNWIND_TRACE_API("__unw_step_stage2(cursor=%p)", |
| 190 | static_cast<void *>(cursor)); |
| 191 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 192 | return co->step(true); |
| 193 | } |
| 194 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 195 | /// Get unwind info at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 196 | _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, |
| 197 | unw_proc_info_t *info) { |
| 198 | _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 199 | static_cast<void *>(cursor), static_cast<void *>(info)); |
| 200 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 201 | co->getInfo(info); |
| 202 | if (info->end_ip == 0) |
| 203 | return UNW_ENOINFO; |
Saleem Abdulrasool | 2aa34a8 | 2019-09-18 16:15:56 +0000 | [diff] [blame] | 204 | return UNW_ESUCCESS; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 205 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 206 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 207 | |
| 208 | /// Resume execution at cursor position (aka longjump). |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 209 | _LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) { |
| 210 | _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor)); |
Saleem Abdulrasool | 7e85c7a | 2021-06-13 14:28:43 -0700 | [diff] [blame] | 211 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
Shoaib Meenai | c8d0fb8 | 2021-05-23 20:41:57 -0700 | [diff] [blame] | 212 | // Inform the ASan runtime that now might be a good time to clean stuff up. |
| 213 | __asan_handle_no_return(); |
| 214 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 215 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 216 | co->jumpto(); |
| 217 | return UNW_EUNSPEC; |
| 218 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 219 | _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 220 | |
| 221 | /// Get name of function at cursor position in stack frame. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 222 | _LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf, |
| 223 | size_t bufLen, unw_word_t *offset) { |
| 224 | _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] | 225 | static_cast<void *>(cursor), static_cast<void *>(buf), |
| 226 | static_cast<unsigned long>(bufLen)); |
| 227 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 228 | if (co->getFunctionName(buf, bufLen, offset)) |
| 229 | return UNW_ESUCCESS; |
Saleem Abdulrasool | 2aa34a8 | 2019-09-18 16:15:56 +0000 | [diff] [blame] | 230 | return UNW_EUNSPEC; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 231 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 232 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 233 | |
| 234 | /// Checks if a register is a floating-point register. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 235 | _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, |
| 236 | unw_regnum_t regNum) { |
| 237 | _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 238 | static_cast<void *>(cursor), regNum); |
| 239 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 240 | return co->validFloatReg(regNum); |
| 241 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 242 | _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 243 | |
| 244 | /// Checks if a register is a floating-point register. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 245 | _LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor, |
| 246 | unw_regnum_t regNum) { |
| 247 | _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 248 | static_cast<void *>(cursor), regNum); |
| 249 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 250 | return co->getRegisterName(regNum); |
| 251 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 252 | _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 253 | |
| 254 | /// Checks if current frame is signal trampoline. |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 255 | _LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) { |
| 256 | _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 257 | static_cast<void *>(cursor)); |
| 258 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 259 | return co->isSignalFrame(); |
| 260 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 261 | _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 262 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 263 | #ifdef _AIX |
| 264 | _LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) { |
| 265 | _LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)", |
| 266 | static_cast<void *>(cursor)); |
| 267 | AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); |
| 268 | return co->getDataRelBase(); |
| 269 | } |
| 270 | _LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base) |
| 271 | #endif |
| 272 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 273 | #ifdef __arm__ |
| 274 | // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 275 | _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { |
| 276 | _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 277 | static_cast<void *>(cursor)); |
| 278 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 279 | return co->saveVFPAsX(); |
| 280 | } |
Petr Hosek | 01fc413 | 2019-04-11 13:08:44 +0000 | [diff] [blame] | 281 | _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] | 282 | #endif |
| 283 | |
| 284 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 285 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Ed Maste | 4c43c3d | 2016-07-19 17:15:50 +0000 | [diff] [blame] | 286 | /// SPI: walks cached DWARF entries |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 287 | _LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 288 | 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] | 289 | _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 290 | reinterpret_cast<void *>(func)); |
| 291 | DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); |
| 292 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 293 | _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, |
| 294 | unw_iterate_dwarf_unwind_cache) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 295 | |
| 296 | /// IPI: for __register_frame() |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 297 | void __unw_add_dynamic_fde(unw_word_t fde) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 298 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 299 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 300 | const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 301 | LocalAddressSpace::sThisAddressSpace, |
| 302 | (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); |
| 303 | if (message == NULL) { |
| 304 | // dynamically registered FDEs don't have a mach_header group they are in. |
| 305 | // Use fde as mh_group |
| 306 | unw_word_t mh_group = fdeInfo.fdeStart; |
| 307 | DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, |
| 308 | fdeInfo.pcStart, fdeInfo.pcEnd, |
| 309 | fdeInfo.fdeStart); |
| 310 | } else { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 311 | _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /// IPI: for __deregister_frame() |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 316 | void __unw_remove_dynamic_fde(unw_word_t fde) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 317 | // fde is own mh_group |
| 318 | DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); |
| 319 | } |
Peter S. Housel | 038090f | 2021-10-14 13:31:05 -0700 | [diff] [blame] | 320 | |
| 321 | void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 322 | // The eh_frame section start serves as the mh_group |
| 323 | unw_word_t mh_group = eh_frame_start; |
| 324 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 325 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 326 | auto p = (LocalAddressSpace::pint_t)eh_frame_start; |
| 327 | while (true) { |
| 328 | if (CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 329 | LocalAddressSpace::sThisAddressSpace, p, &fdeInfo, &cieInfo, |
| 330 | true) == NULL) { |
| 331 | DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, |
| 332 | fdeInfo.pcStart, fdeInfo.pcEnd, |
| 333 | fdeInfo.fdeStart); |
| 334 | p += fdeInfo.fdeLength; |
| 335 | } else if (CFI_Parser<LocalAddressSpace>::parseCIE( |
| 336 | LocalAddressSpace::sThisAddressSpace, p, &cieInfo) == NULL) { |
| 337 | p += cieInfo.cieLength; |
| 338 | } else |
| 339 | return; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 344 | // The eh_frame section start serves as the mh_group |
| 345 | DwarfFDECache<LocalAddressSpace>::removeAllIn( |
| 346 | (LocalAddressSpace::pint_t)eh_frame_start); |
| 347 | } |
| 348 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 349 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 350 | #endif // !defined(__USING_SJLJ_EXCEPTIONS__) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 351 | |
| 352 | |
| 353 | |
| 354 | // Add logging hooks in Debug builds only |
| 355 | #ifndef NDEBUG |
| 356 | #include <stdlib.h> |
| 357 | |
| 358 | _LIBUNWIND_HIDDEN |
| 359 | bool logAPIs() { |
| 360 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 361 | static bool checked = false; |
| 362 | static bool log = false; |
| 363 | if (!checked) { |
| 364 | log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); |
| 365 | checked = true; |
| 366 | } |
| 367 | return log; |
| 368 | } |
| 369 | |
| 370 | _LIBUNWIND_HIDDEN |
| 371 | bool logUnwinding() { |
| 372 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 373 | static bool checked = false; |
| 374 | static bool log = false; |
| 375 | if (!checked) { |
| 376 | log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); |
| 377 | checked = true; |
| 378 | } |
| 379 | return log; |
| 380 | } |
| 381 | |
Saleem Abdulrasool | d1be5b0 | 2017-01-21 16:22:57 +0000 | [diff] [blame] | 382 | _LIBUNWIND_HIDDEN |
| 383 | bool logDWARF() { |
| 384 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 385 | static bool checked = false; |
| 386 | static bool log = false; |
| 387 | if (!checked) { |
| 388 | log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); |
| 389 | checked = true; |
| 390 | } |
| 391 | return log; |
| 392 | } |
| 393 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 394 | #endif // NDEBUG |
| 395 | |