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>
39 #include <demangle.h>
40 #include <unwindstack/LocalUnwinder.h>
41 #include <unwindstack/MapInfo.h>
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
52 static pthread_once_t g_setup_once = PTHREAD_ONCE_INIT;
53
54 static unwindstack::LocalUnwinder* g_unwinder;
55
Setup()56 static void Setup() {
57 #if defined(__LP64__)
58 std::vector<std::string> skip_libraries{"/system/lib64/libunwindstack.so", "/system/lib64/libc_malloc_debug.so"};
59 #else
60 std::vector<std::string> skip_libraries{"/system/lib/libunwindstack.so", "/system/lib/libc_malloc_debug.so"};
61 #endif
62
63 g_unwinder = new unwindstack::LocalUnwinder(skip_libraries);
64 g_unwinder->Init();
65 }
66
Unwind(std::vector<uintptr_t> * frames,std::vector<unwindstack::LocalFrameData> * frame_info,size_t max_frames)67 bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::LocalFrameData>* frame_info, size_t max_frames) {
68 pthread_once(&g_setup_once, Setup);
69
70 if (g_unwinder == nullptr) {
71 return false;
72 }
73
74 if (!g_unwinder->Unwind(frame_info, max_frames)) {
75 frames->clear();
76 frame_info->clear();
77 return false;
78 }
79
80 for (const auto& frame : *frame_info) {
81 frames->push_back(frame.pc);
82 }
83 return true;
84 }
85
UnwindLog(const std::vector<unwindstack::LocalFrameData> & frame_info)86 void UnwindLog(const std::vector<unwindstack::LocalFrameData>& frame_info) {
87 for (size_t i = 0; i < frame_info.size(); i++) {
88 const unwindstack::LocalFrameData* info = &frame_info[i];
89 unwindstack::MapInfo* map_info = info->map_info;
90
91 std::string line = android::base::StringPrintf(" #%0zd pc %" PAD_PTR " ", i, info->rel_pc);
92 if (map_info->offset != 0) {
93 line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset);
94 }
95
96 if (map_info->name.empty()) {
97 line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start);
98 } else {
99 line += map_info->name;
100 }
101
102 if (!info->function_name.empty()) {
103 line += " (" + demangle(info->function_name.c_str());
104 if (info->function_offset != 0) {
105 line += "+" + std::to_string(info->function_offset);
106 }
107 line += ")";
108 }
109 error_log_string(line.c_str());
110 }
111 }
112