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