• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_thread.h"
17 
18 #include <securec.h>
19 #include <unistd.h>
20 
21 #include "dfx_define.h"
22 #include "procinfo.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 #undef LOG_DOMAIN
28 #undef LOG_TAG
29 #define LOG_DOMAIN 0xD002D11
30 #define LOG_TAG "DfxBacktraceLocal"
31 }
32 
GetThreadHead(int32_t tid)33 std::string GetThreadHead(int32_t tid)
34 {
35     std::string threadName;
36     if (tid == BACKTRACE_CURRENT_THREAD) {
37         tid = gettid();
38     }
39     ReadThreadName(tid, threadName);
40     std::string threadHead = "Tid:" + std::to_string(tid) + ", Name:" + threadName + "\n";
41     return threadHead;
42 }
43 
Unwind(Unwinder & unwinder,bool fast,size_t maxFrameNum,size_t skipFrameNum)44 bool BacktraceLocalThread::Unwind(Unwinder& unwinder, bool fast, size_t maxFrameNum, size_t skipFrameNum)
45 {
46     static std::mutex mutex;
47     std::unique_lock<std::mutex> lock(mutex);
48     bool ret = false;
49 
50     if (tid_ < BACKTRACE_CURRENT_THREAD) {
51         return ret;
52     }
53 
54     if (tid_ == BACKTRACE_CURRENT_THREAD) {
55         ret = unwinder.UnwindLocal(false, fast, maxFrameNum, skipFrameNum + 1);
56 #ifdef __aarch64__
57         if (fast) {
58             Unwinder::GetLocalFramesByPcs(frames_, unwinder.GetPcs());
59         }
60 #endif
61         if (frames_.empty()) {
62             frames_ = unwinder.GetFrames();
63         }
64         return ret;
65     }
66 
67     ret = unwinder.UnwindLocalWithTid(tid_, maxFrameNum, skipFrameNum + 1);
68 #ifdef __aarch64__
69     Unwinder::GetLocalFramesByPcs(frames_, unwinder.GetPcs());
70 #else
71     frames_ = unwinder.GetFrames();
72 #endif
73     return ret;
74 }
75 
SetFrames(const std::vector<DfxFrame> & frames)76 void BacktraceLocalThread::SetFrames(const std::vector<DfxFrame>& frames)
77 {
78     frames_ = frames;
79 }
80 
GetFrames() const81 const std::vector<DfxFrame>& BacktraceLocalThread::GetFrames() const
82 {
83     return frames_;
84 }
85 
GetFormattedStr(bool withThreadName)86 std::string BacktraceLocalThread::GetFormattedStr(bool withThreadName)
87 {
88     if (frames_.empty()) {
89         return "";
90     }
91 
92     std::string ss;
93     if (withThreadName && (tid_ > 0)) {
94         std::string threadName;
95         // Tid:1676, Name:IPC_3_1676
96         ReadThreadName(tid_, threadName);
97         ss = "Tid:" + std::to_string(tid_) + ", Name:" + threadName + "\n";
98     }
99     if (includeThreadInfo_) {
100         ThreadInfo info;
101         if (info.ParserThreadInfo(tid_)) {
102             ss += "ThreadInfo:" + info.ToString() + "\n";
103         }
104     }
105     ss += Unwinder::GetFramesStr(frames_);
106     return ss;
107 }
108 
UnwindOtherThreadMix(Unwinder & unwinder,bool fast,size_t maxFrameNum,size_t skipFrameNum)109 bool BacktraceLocalThread::UnwindOtherThreadMix(Unwinder& unwinder, bool fast, size_t maxFrameNum, size_t skipFrameNum)
110 {
111     static std::mutex mutex;
112     std::unique_lock<std::mutex> lock(mutex);
113     bool ret = false;
114 
115     if (tid_ < BACKTRACE_CURRENT_THREAD) {
116         return ret;
117     }
118     if (tid_ == BACKTRACE_CURRENT_THREAD || tid_ == gettid()) {
119         ret = unwinder.UnwindLocal(false, fast, maxFrameNum, skipFrameNum + 1, true);
120     } else {
121         ret = unwinder.UnwindLocalByOtherTid(tid_, fast, maxFrameNum, skipFrameNum + 1);
122     }
123 #ifdef __aarch64__
124     if (ret && fast) {
125         unwinder.GetFramesByPcs(frames_, unwinder.GetPcs());
126     }
127 #endif
128     if (frames_.empty()) {
129         frames_ = unwinder.GetFrames();
130     }
131     return ret;
132 }
133 } // namespace HiviewDFX
134 } // namespace OHOS
135