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 <cinttypes>
19 #include <dlfcn.h>
20 #include <map>
21
22 #include "ecmascript/base/config.h"
23 #include "ecmascript/platform/mutex.h"
24 #include "securec.h"
25
26 #include "ecmascript/mem/mem.h"
27 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
28 #if defined(__clang__)
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wunused-parameter"
31 #pragma clang diagnostic ignored "-Wshadow"
32 #endif
33 #include "fp_unwinder.h"
34 #if defined(__clang__)
35 #pragma clang diagnostic pop
36 #endif
37 #endif
38
39 namespace panda::ecmascript {
40 static const std::string LIB_UNWIND_SO_NAME = "libunwind.so";
41 static const std::string LIB_UNWIND_Z_SO_NAME = "libunwind.z.so";
42 static const int MAX_STACK_SIZE = 16;
43 static const int LOG_BUF_LEN = 1024;
44
45 using UnwBackTraceFunc = int (*)(void**, int);
46
47 static std::map<void *, Dl_info> stackInfoCache;
48
49 RWLock rwMutex;
50
51 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
GetPcFpRegs(void * regs)52 static inline ARK_INLINE void GetPcFpRegs([[maybe_unused]] void *regs)
53 {
54 asm volatile(
55 "1:\n"
56 "adr x12, 1b\n"
57 "stp x12, x29, [%[base], #0]\n"
58 : [base] "+r"(regs)
59 :
60 : "x12", "memory");
61 }
62 #endif
63
GetPcs(size_t & size,uintptr_t * pcs)64 bool GetPcs(size_t &size, uintptr_t* pcs)
65 {
66 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
67 uintptr_t regs[2] = {0}; // 2: pc and fp reg
68 GetPcFpRegs(regs);
69 uintptr_t pc = regs[0];
70 uintptr_t fp = regs[1];
71 size = OHOS::HiviewDFX::FpUnwinder::GetPtr()->Unwind(pc, fp, pcs, MAX_STACK_SIZE);
72 if (size <= 1) {
73 size = OHOS::HiviewDFX::FpUnwinder::GetPtr()->UnwindSafe(pc, fp, pcs, MAX_STACK_SIZE);
74 }
75 #else
76 static UnwBackTraceFunc unwBackTrace = nullptr;
77 if (!unwBackTrace) {
78 void *handle = dlopen(LIB_UNWIND_SO_NAME.c_str(), RTLD_NOW);
79 if (handle == nullptr) {
80 handle = dlopen(LIB_UNWIND_Z_SO_NAME.c_str(), RTLD_NOW);
81 if (handle == nullptr) {
82 LOG_ECMA(INFO) << "dlopen libunwind.so failed";
83 return false;
84 }
85 }
86 unwBackTrace = reinterpret_cast<UnwBackTraceFunc>(dlsym(handle, "unw_backtrace"));
87 if (unwBackTrace == nullptr) {
88 LOG_ECMA(INFO) << "dlsym unw_backtrace failed";
89 return false;
90 }
91 }
92 size = unwBackTrace(reinterpret_cast<void**>(pcs), MAX_STACK_SIZE);
93 #endif
94 return true;
95 }
96
FindStackInfoCache(uintptr_t pcs,Dl_info & info)97 bool FindStackInfoCache(uintptr_t pcs, Dl_info &info)
98 {
99 ReadLockHolder lock(rwMutex);
100 auto iter = stackInfoCache.find(reinterpret_cast<void *>(pcs));
101 if (iter != stackInfoCache.end()) {
102 info = iter->second;
103 return true;
104 } else {
105 return false;
106 }
107 }
108
EmplaceStackInfoCache(uintptr_t pcs,const Dl_info & info)109 void EmplaceStackInfoCache(uintptr_t pcs, const Dl_info &info)
110 {
111 WriteLockHolder lock(rwMutex);
112 stackInfoCache.emplace(reinterpret_cast<void *>(pcs), info);
113 }
114
Backtrace(std::ostringstream & stack,bool enableCache)115 void Backtrace(std::ostringstream &stack, bool enableCache)
116 {
117 uintptr_t pcs[MAX_STACK_SIZE] = {0};
118 size_t unwSz = 0;
119 if (!GetPcs(unwSz, pcs)) {
120 return;
121 }
122 stack << "=====================Backtrace========================";
123 #if defined(ENABLE_UNWINDER) && defined(__aarch64__)
124 size_t i = 0;
125 #else
126 size_t i = 1;
127 #endif
128 for (; i < unwSz; i++) {
129 Dl_info info;
130 if (!FindStackInfoCache(pcs[i], info)) {
131 if (!dladdr(reinterpret_cast<void *>(pcs[i]), &info)) {
132 break;
133 }
134 if (enableCache) {
135 EmplaceStackInfoCache(pcs[i], info);
136 }
137 }
138 const char *file = info.dli_fname ? info.dli_fname : "";
139 uint64_t offset = info.dli_fbase ? pcs[i] - ToUintPtr(info.dli_fbase) : 0;
140 char buf[LOG_BUF_LEN] = {0};
141 char frameFormatWithMapName[] = "#%02zu pc %016" PRIx64 " %s";
142 int ret = 0;
143 ret = static_cast<int>(snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, frameFormatWithMapName, \
144 i, offset, file));
145 if (ret <= 0) {
146 LOG_ECMA(ERROR) << "Backtrace snprintf_s failed";
147 return;
148 }
149 stack << std::endl;
150 stack << buf;
151 }
152 }
153 } // namespace panda::ecmascript
154