blob: f6c3e69e0d23bb94e44610315491beee951aeddf [file] [log] [blame]
Christopher Ferris93bdd6a2018-04-05 11:12:38 -07001/*
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 Ferris93bdd6a2018-04-05 11:12:38 -070039#include <unwindstack/MapInfo.h>
Christopher Ferris459eecb2022-01-07 13:38:10 -080040#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 Ferris93bdd6a2018-04-05 11:12:38 -070045
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 Ferris9782b872019-07-18 13:36:50 -070055extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
56
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070057static pthread_once_t g_setup_once = PTHREAD_ONCE_INIT;
58
Christopher Ferris459eecb2022-01-07 13:38:10 -080059static unwindstack::LocalUpdatableMaps* g_maps;
60static std::shared_ptr<unwindstack::Memory> g_process_memory;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070061#if defined(__LP64__)
Christopher Ferris459eecb2022-01-07 13:38:10 -080062static std::vector<std::string> g_skip_libraries{"/system/lib64/libunwindstack.so",
63 "/system/lib64/libc_malloc_debug.so"};
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070064#else
Christopher Ferris459eecb2022-01-07 13:38:10 -080065static std::vector<std::string> g_skip_libraries{"/system/lib/libunwindstack.so",
66 "/system/lib/libc_malloc_debug.so"};
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070067#endif
68
Christopher Ferris459eecb2022-01-07 13:38:10 -080069static 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 Ferris93bdd6a2018-04-05 11:12:38 -070077}
78
Christopher Ferris459eecb2022-01-07 13:38:10 -080079bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* frame_info,
80 size_t max_frames) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070081 pthread_once(&g_setup_once, Setup);
82
Christopher Ferris459eecb2022-01-07 13:38:10 -080083 if (g_maps == nullptr) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070084 return false;
85 }
86
Christopher Ferris459eecb2022-01-07 13:38:10 -080087 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 Ferris93bdd6a2018-04-05 11:12:38 -070092 frames->clear();
93 frame_info->clear();
94 return false;
95 }
Christopher Ferris459eecb2022-01-07 13:38:10 -080096 *frame_info = unwinder.ConsumeFrames();
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070097
Christopher Ferris459eecb2022-01-07 13:38:10 -080098 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 Ferris93bdd6a2018-04-05 11:12:38 -0700101 }
102 return true;
103}
104
Christopher Ferris459eecb2022-01-07 13:38:10 -0800105void UnwindLog(const std::vector<unwindstack::FrameData>& frame_info) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700106 for (size_t i = 0; i < frame_info.size(); i++) {
Christopher Ferris459eecb2022-01-07 13:38:10 -0800107 const unwindstack::FrameData* info = &frame_info[i];
108 auto map_info = info->map_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700109
110 std::string line = android::base::StringPrintf(" #%0zd pc %" PAD_PTR " ", i, info->rel_pc);
Christopher Ferrisd49ad1e2022-02-02 17:56:48 -0800111 if (map_info != nullptr && map_info->offset() != 0) {
David Srbecky92b8d642021-05-13 00:03:26 +0100112 line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700113 }
114
Christopher Ferrisd49ad1e2022-02-02 17:56:48 -0800115 if (map_info == nullptr) {
116 line += "<unknown>";
117 } else if (map_info->name().empty()) {
David Srbecky92b8d642021-05-13 00:03:26 +0100118 line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700119 } else {
David Srbecky92b8d642021-05-13 00:03:26 +0100120 line += map_info->name();
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700121 }
122
123 if (!info->function_name.empty()) {
Christopher Ferris9782b872019-07-18 13:36:50 -0700124 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 Ferris93bdd6a2018-04-05 11:12:38 -0700132 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}