1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/platform/backtrace.h"
17
18 #include <cstring>
19 #include <cinttypes>
20 #include <dlfcn.h>
21 #include <iomanip>
22 #include <ios>
23 #include <map>
24 #include <unwind.h>
25 #include "securec.h"
26
27 #include "ecmascript/mem/mem.h"
28 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
29 #if defined(__clang__)
30 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Wshadow"
32 #pragma clang diagnostic ignored "-Wunused-parameter"
33 #endif
34 #include "fp_unwinder.h"
35 #if defined(__clang__)
36 #pragma clang diagnostic pop
37 #endif
38 #endif
39
40 namespace panda::ecmascript {
41 static const std::string LIB_UNWIND_SO_NAME = "libunwind.so";
42 static const std::string LIB_UNWIND_Z_SO_NAME = "libunwind.z.so";
43 static const int MAX_STACK_SIZE = 16;
44 static const int LOG_BUF_LEN = 1024;
45
46 using UnwBackTraceFunc = int (*)(void**, int);
47
48 static std::map<void *, Dl_info> stackInfoCache;
49
50 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
GetPcFpRegs([[maybe_unused]] void *regs)51 static inline ARK_INLINE void GetPcFpRegs([[maybe_unused]] void *regs)
52 {
53 asm volatile(
54 "1:\n"
55 "adr x12, 1b\n"
56 "stp x12, x29, [%[base], #0]\n"
57 : [base] "+r"(regs)
58 :
59 : "x12", "memory");
60 }
61 #endif
62
GetPcs(size_t &size, uintptr_t* pcs)63 bool GetPcs(size_t &size, uintptr_t* pcs)
64 {
65 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
66 uintptr_t regs[2] = {0}; // 2: pc and fp reg
67 GetPcFpRegs(regs);
68 uintptr_t pc = regs[0];
69 uintptr_t fp = regs[1];
70 size = OHOS::HiviewDFX::FpUnwinder::GetPtr()->Unwind(pc, fp, pcs, MAX_STACK_SIZE);
71 if (size <= 1) {
72 size = OHOS::HiviewDFX::FpUnwinder::GetPtr()->UnwindSafe(pc, fp, pcs, MAX_STACK_SIZE);
73 }
74 #else
75 static UnwBackTraceFunc unwBackTrace = nullptr;
76 if (!unwBackTrace) {
77 void *handle = dlopen(LIB_UNWIND_SO_NAME.c_str(), RTLD_NOW);
78 if (handle == nullptr) {
79 handle = dlopen(LIB_UNWIND_Z_SO_NAME.c_str(), RTLD_NOW);
80 if (handle == nullptr) {
81 LOG_ECMA(INFO) << "dlopen libunwind.so failed";
82 return false;
83 }
84 }
85 unwBackTrace = reinterpret_cast<UnwBackTraceFunc>(dlsym(handle, "unw_backtrace"));
86 if (unwBackTrace == nullptr) {
87 LOG_ECMA(INFO) << "dlsym unw_backtrace failed";
88 return false;
89 }
90 }
91 size = unwBackTrace(reinterpret_cast<void**>(pcs), MAX_STACK_SIZE);
92 #endif
93 return true;
94 }
95
Backtrace(std::ostringstream &stack, bool enableCache)96 void Backtrace(std::ostringstream &stack, bool enableCache)
97 {
98 uintptr_t pcs[MAX_STACK_SIZE] = {0};
99 size_t unwSz = 0;
100 if (!GetPcs(unwSz, pcs)) {
101 return;
102 }
103 stack << "=====================Backtrace========================";
104 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
105 size_t i = 0;
106 #else
107 size_t i = 1;
108 #endif
109 for (; i < unwSz; i++) {
110 Dl_info info;
111 auto iter = stackInfoCache.find(reinterpret_cast<void *>(pcs[i]));
112 if (enableCache && iter != stackInfoCache.end()) {
113 info = iter->second;
114 } else {
115 if (!dladdr(reinterpret_cast<void *>(pcs[i]), &info)) {
116 break;
117 }
118 if (enableCache) {
119 stackInfoCache.emplace(reinterpret_cast<void *>(pcs[i]), info);
120 }
121 }
122 const char *file = info.dli_fname ? info.dli_fname : "";
123 uint64_t offset = info.dli_fbase ? pcs[i] - ToUintPtr(info.dli_fbase) : 0;
124 char buf[LOG_BUF_LEN] = {0};
125 char frameFormatWithMapName[] = "#%02zu pc %016" PRIx64 " %s";
126 int ret = 0;
127 ret = static_cast<int>(snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, frameFormatWithMapName, \
128 i, offset, file));
129 if (ret <= 0) {
130 LOG_ECMA(ERROR) << "Backtrace snprintf_s failed";
131 return;
132 }
133 stack << std::endl;
134 stack << buf;
135 }
136 }
137 } // namespace panda::ecmascript
138