Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <inttypes.h> |
| 30 | #include <pthread.h> |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <memory> |
| 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <android-base/stringprintf.h> |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 39 | #include <unwindstack/MapInfo.h> |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 40 | #include <unwindstack/Maps.h> |
| 41 | #include <unwindstack/Memory.h> |
| 42 | #include <unwindstack/Regs.h> |
| 43 | #include <unwindstack/RegsGetLocal.h> |
| 44 | #include <unwindstack/Unwinder.h> |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 45 | |
| 46 | #include "UnwindBacktrace.h" |
| 47 | #include "debug_log.h" |
| 48 | |
| 49 | #if defined(__LP64__) |
| 50 | #define PAD_PTR "016" PRIx64 |
| 51 | #else |
| 52 | #define PAD_PTR "08" PRIx64 |
| 53 | #endif |
| 54 | |
Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 55 | extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*); |
| 56 | |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 57 | static pthread_once_t g_setup_once = PTHREAD_ONCE_INIT; |
| 58 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 59 | static unwindstack::LocalUpdatableMaps* g_maps; |
| 60 | static std::shared_ptr<unwindstack::Memory> g_process_memory; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 61 | #if defined(__LP64__) |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 62 | static std::vector<std::string> g_skip_libraries{"/system/lib64/libunwindstack.so", |
| 63 | "/system/lib64/libc_malloc_debug.so"}; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 64 | #else |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 65 | static std::vector<std::string> g_skip_libraries{"/system/lib/libunwindstack.so", |
| 66 | "/system/lib/libc_malloc_debug.so"}; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 69 | static void Setup() { |
| 70 | g_maps = new unwindstack::LocalUpdatableMaps; |
| 71 | if (!g_maps->Parse()) { |
| 72 | delete g_maps; |
| 73 | g_maps = nullptr; |
| 74 | } |
| 75 | |
| 76 | g_process_memory = unwindstack::Memory::CreateProcessMemoryThreadCached(getpid()); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 79 | bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* frame_info, |
| 80 | size_t max_frames) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 81 | pthread_once(&g_setup_once, Setup); |
| 82 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 83 | if (g_maps == nullptr) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 87 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); |
| 88 | unwindstack::RegsGetLocal(regs.get()); |
| 89 | unwindstack::Unwinder unwinder(max_frames, g_maps, regs.get(), g_process_memory); |
| 90 | unwinder.Unwind(&g_skip_libraries); |
| 91 | if (unwinder.NumFrames() == 0) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 92 | frames->clear(); |
| 93 | frame_info->clear(); |
| 94 | return false; |
| 95 | } |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 96 | *frame_info = unwinder.ConsumeFrames(); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 97 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 98 | frames->resize(frame_info->size()); |
| 99 | for (size_t i = 0; i < frame_info->size(); i++) { |
| 100 | frames->at(i) = frame_info->at(i).pc; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 105 | void UnwindLog(const std::vector<unwindstack::FrameData>& frame_info) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 106 | for (size_t i = 0; i < frame_info.size(); i++) { |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame^] | 107 | const unwindstack::FrameData* info = &frame_info[i]; |
| 108 | auto map_info = info->map_info; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 109 | |
| 110 | std::string line = android::base::StringPrintf(" #%0zd pc %" PAD_PTR " ", i, info->rel_pc); |
Christopher Ferris | d49ad1e | 2022-02-02 17:56:48 -0800 | [diff] [blame] | 111 | if (map_info != nullptr && map_info->offset() != 0) { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 112 | line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset()); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Christopher Ferris | d49ad1e | 2022-02-02 17:56:48 -0800 | [diff] [blame] | 115 | if (map_info == nullptr) { |
| 116 | line += "<unknown>"; |
| 117 | } else if (map_info->name().empty()) { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 118 | line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start()); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 119 | } else { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 120 | line += map_info->name(); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (!info->function_name.empty()) { |
Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 124 | line += " ("; |
| 125 | char* demangled_name = __cxa_demangle(info->function_name.c_str(), nullptr, nullptr, nullptr); |
| 126 | if (demangled_name != nullptr) { |
| 127 | line += demangled_name; |
| 128 | free(demangled_name); |
| 129 | } else { |
| 130 | line += info->function_name; |
| 131 | } |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 132 | if (info->function_offset != 0) { |
| 133 | line += "+" + std::to_string(info->function_offset); |
| 134 | } |
| 135 | line += ")"; |
| 136 | } |
| 137 | error_log_string(line.c_str()); |
| 138 | } |
| 139 | } |