blob: 92fb3fa96306d651949da893e90229020f4906b8 [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/LocalUnwinder.h>
40#include <unwindstack/MapInfo.h>
41
42#include "UnwindBacktrace.h"
43#include "debug_log.h"
44
45#if defined(__LP64__)
46#define PAD_PTR "016" PRIx64
47#else
48#define PAD_PTR "08" PRIx64
49#endif
50
Christopher Ferris9782b872019-07-18 13:36:50 -070051extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
52
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070053static pthread_once_t g_setup_once = PTHREAD_ONCE_INIT;
54
55static unwindstack::LocalUnwinder* g_unwinder;
56
57static void Setup() {
58#if defined(__LP64__)
59 std::vector<std::string> skip_libraries{"/system/lib64/libunwindstack.so", "/system/lib64/libc_malloc_debug.so"};
60#else
61 std::vector<std::string> skip_libraries{"/system/lib/libunwindstack.so", "/system/lib/libc_malloc_debug.so"};
62#endif
63
64 g_unwinder = new unwindstack::LocalUnwinder(skip_libraries);
65 g_unwinder->Init();
66}
67
68bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::LocalFrameData>* frame_info, size_t max_frames) {
69 pthread_once(&g_setup_once, Setup);
70
71 if (g_unwinder == nullptr) {
72 return false;
73 }
74
75 if (!g_unwinder->Unwind(frame_info, max_frames)) {
76 frames->clear();
77 frame_info->clear();
78 return false;
79 }
80
81 for (const auto& frame : *frame_info) {
82 frames->push_back(frame.pc);
83 }
84 return true;
85}
86
87void UnwindLog(const std::vector<unwindstack::LocalFrameData>& frame_info) {
88 for (size_t i = 0; i < frame_info.size(); i++) {
89 const unwindstack::LocalFrameData* info = &frame_info[i];
90 unwindstack::MapInfo* map_info = info->map_info;
91
92 std::string line = android::base::StringPrintf(" #%0zd pc %" PAD_PTR " ", i, info->rel_pc);
93 if (map_info->offset != 0) {
94 line += android::base::StringPrintf("(offset 0x%" PRIx64 ") ", map_info->offset);
95 }
96
97 if (map_info->name.empty()) {
98 line += android::base::StringPrintf("<anonymous:%" PRIx64 ">", map_info->start);
99 } else {
100 line += map_info->name;
101 }
102
103 if (!info->function_name.empty()) {
Christopher Ferris9782b872019-07-18 13:36:50 -0700104 line += " (";
105 char* demangled_name = __cxa_demangle(info->function_name.c_str(), nullptr, nullptr, nullptr);
106 if (demangled_name != nullptr) {
107 line += demangled_name;
108 free(demangled_name);
109 } else {
110 line += info->function_name;
111 }
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700112 if (info->function_offset != 0) {
113 line += "+" + std::to_string(info->function_offset);
114 }
115 line += ")";
116 }
117 error_log_string(line.c_str());
118 }
119}