• 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 "backtrace_local.h"
17 
18 #include <cstring>
19 #include <unistd.h>
20 
21 #include <mutex>
22 #include <vector>
23 
24 #include "backtrace_local_thread.h"
25 #include "dfx_frame_formatter.h"
26 #include "dfx_kernel_stack.h"
27 #include "dfx_log.h"
28 #include "dfx_util.h"
29 #include "directory_ex.h"
30 #include "procinfo.h"
31 #include "thread_context.h"
32 #include "unwinder.h"
33 
34 namespace OHOS {
35 namespace HiviewDFX {
36 namespace {
37 #undef LOG_DOMAIN
38 #undef LOG_TAG
39 #define LOG_TAG "DfxBacktrace"
40 #define LOG_DOMAIN 0xD002D11
41 }
42 
43 namespace OtherThread {
GetMapByPc(uintptr_t pc,std::shared_ptr<DfxMap> & map,void * arg)44 int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg)
45 {
46     auto& instance = LocalThreadContextMix::GetInstance();
47     return instance.GetMapByPc(pc, map);
48 }
49 
FindUnwindTable(uintptr_t pc,UnwindTableInfo & outTableInfo,void * arg)50 int FindUnwindTable(uintptr_t pc, UnwindTableInfo& outTableInfo, void *arg)
51 {
52     auto& instance = LocalThreadContextMix::GetInstance();
53     return instance.FindUnwindTable(pc, outTableInfo);
54 }
55 
AccessMem(uintptr_t addr,uintptr_t * val,void * arg)56 int AccessMem(uintptr_t addr, uintptr_t *val, void *arg)
57 {
58     auto& instance = LocalThreadContextMix::GetInstance();
59     return instance.AccessMem(addr, val);
60 }
61 }
62 
GetBacktraceStringByTidWithMix(std::string & out,int32_t tid,size_t skipFrameNum,bool fast,size_t maxFrameNums,bool enableKernelStack)63 bool GetBacktraceStringByTidWithMix(std::string& out, int32_t tid, size_t skipFrameNum, bool fast,
64     size_t maxFrameNums, bool enableKernelStack)
65 {
66 #if defined(__x86_64__)
67     return false;
68 #else
69 #if defined(__arm__)
70     fast = false;
71 #endif
72     std::shared_ptr<Unwinder> unwinder = nullptr;
73     if ((tid == gettid()) || (tid == BACKTRACE_CURRENT_THREAD)) {
74         unwinder = std::make_shared<Unwinder>();
75     } else {
76         std::shared_ptr<UnwindAccessors> accssors = std::make_shared<UnwindAccessors>();
77         accssors->AccessReg = nullptr;
78         accssors->AccessMem = &OtherThread::AccessMem;
79         accssors->GetMapByPc = &OtherThread::GetMapByPc;
80         accssors->FindUnwindTable = &OtherThread::FindUnwindTable;
81         unwinder = std::make_shared<Unwinder>(accssors, true);
82     }
83     std::vector<DfxFrame> frames{};
84     bool ret = false;
85     if (unwinder) {
86         BacktraceLocalThread thread(tid);
87         ret = thread.UnwindOtherThreadMix(*unwinder, fast, maxFrameNums, skipFrameNum + 1);
88         frames = thread.GetFrames();
89     }
90     if (!ret && enableKernelStack) {
91         std::string msg = "";
92         DfxThreadStack threadStack;
93         if (DfxGetKernelStack(tid, msg) == 0 && FormatThreadKernelStack(msg, threadStack)) {
94             frames = std::move(threadStack.frames);
95             ret = true;
96             DFXLOGI("Failed to get tid(%{public}d) user stack, try kernel", tid);
97             if (IsBetaVersion()) {
98                 DFXLOGI("%{public}s", msg.c_str());
99             }
100         }
101     }
102     if (ret) {
103         std::string threadHead = GetThreadHead(tid);
104         out = threadHead + Unwinder::GetFramesStr(frames);
105     }
106     return ret;
107 #endif
108 }
109 } // namespace HiviewDFX
110 } // namespace OHOS
111