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 | |
Ryan Prichard | c2adad1 | 2022-12-08 16:52:39 -0800 | [diff] [blame^] | 29 | #include <cxxabi.h> |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 30 | #include <inttypes.h> |
| 31 | #include <pthread.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include <android-base/stringprintf.h> |
Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 40 | #include <unwindstack/AndroidUnwinder.h> |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 41 | #include <unwindstack/Unwinder.h> |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 42 | |
| 43 | #include "UnwindBacktrace.h" |
| 44 | #include "debug_log.h" |
| 45 | |
| 46 | #if defined(__LP64__) |
| 47 | #define PAD_PTR "016" PRIx64 |
| 48 | #else |
| 49 | #define PAD_PTR "08" PRIx64 |
| 50 | #endif |
| 51 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 52 | bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* frame_info, |
| 53 | size_t max_frames) { |
Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 54 | [[clang::no_destroy]] static unwindstack::AndroidLocalUnwinder unwinder( |
| 55 | std::vector<std::string>{"libc_malloc_debug.so"}); |
| 56 | unwindstack::AndroidUnwinderData data(max_frames); |
| 57 | if (!unwinder.Unwind(data)) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 58 | frames->clear(); |
| 59 | frame_info->clear(); |
| 60 | return false; |
| 61 | } |
| 62 | |
Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 63 | frames->resize(data.frames.size()); |
| 64 | for (const auto& frame : data.frames) { |
| 65 | frames->at(frame.num) = frame.pc; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 66 | } |
Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 67 | *frame_info = std::move(data.frames); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 71 | void UnwindLog(const std::vector<unwindstack::FrameData>& frame_info) { |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 72 | for (size_t i = 0; i < frame_info.size(); i++) { |
Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 73 | const unwindstack::FrameData* info = &frame_info[i]; |
| 74 | auto map_info = info->map_info; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 75 | |
| 76 | 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] | 77 | if (map_info != nullptr && map_info->offset() != 0) { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 78 | line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset()); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Christopher Ferris | d49ad1e | 2022-02-02 17:56:48 -0800 | [diff] [blame] | 81 | if (map_info == nullptr) { |
| 82 | line += "<unknown>"; |
| 83 | } else if (map_info->name().empty()) { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 84 | line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start()); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 85 | } else { |
David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 86 | line += map_info->name(); |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (!info->function_name.empty()) { |
Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 90 | line += " ("; |
Ryan Prichard | c2adad1 | 2022-12-08 16:52:39 -0800 | [diff] [blame^] | 91 | char* demangled_name = |
| 92 | abi::__cxa_demangle(info->function_name.c_str(), nullptr, nullptr, nullptr); |
Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 93 | if (demangled_name != nullptr) { |
| 94 | line += demangled_name; |
| 95 | free(demangled_name); |
| 96 | } else { |
| 97 | line += info->function_name; |
| 98 | } |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 99 | if (info->function_offset != 0) { |
| 100 | line += "+" + std::to_string(info->function_offset); |
| 101 | } |
| 102 | line += ")"; |
| 103 | } |
| 104 | error_log_string(line.c_str()); |
| 105 | } |
| 106 | } |