• 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 
BacktraceLocalThread(int32_t tid,std::shared_ptr<Unwinder> unwinder)44 BacktraceLocalThread::BacktraceLocalThread(int32_t tid, std::shared_ptr<Unwinder> unwinder)
45     : tid_(tid), unwinder_(unwinder)
46 {
47     frames_.clear();
48 }
49 
~BacktraceLocalThread()50 BacktraceLocalThread::~BacktraceLocalThread()
51 {
52     frames_.clear();
53 }
54 
Unwind(bool fast,size_t maxFrameNum,size_t skipFrameNum)55 bool BacktraceLocalThread::Unwind(bool fast, size_t maxFrameNum, size_t skipFrameNum)
56 {
57     static std::mutex mutex;
58     std::unique_lock<std::mutex> lock(mutex);
59     bool ret = false;
60 
61     if (unwinder_ == nullptr || tid_ < BACKTRACE_CURRENT_THREAD) {
62         return ret;
63     }
64 
65     if (tid_ == BACKTRACE_CURRENT_THREAD) {
66         ret = unwinder_->UnwindLocal(false, fast, maxFrameNum, skipFrameNum + 1);
67 #ifdef __aarch64__
68         if (fast) {
69             Unwinder::GetLocalFramesByPcs(frames_, unwinder_->GetPcs());
70         }
71 #endif
72         if (frames_.empty()) {
73             frames_ = unwinder_->GetFrames();
74         }
75         return ret;
76     }
77 
78     ret = unwinder_->UnwindLocalWithTid(tid_, maxFrameNum, skipFrameNum + 1);
79 #ifdef __aarch64__
80     Unwinder::GetLocalFramesByPcs(frames_, unwinder_->GetPcs());
81 #else
82     frames_ = unwinder_->GetFrames();
83 #endif
84     return ret;
85 }
86 
SetFrames(const std::vector<DfxFrame> & frames)87 void BacktraceLocalThread::SetFrames(const std::vector<DfxFrame>& frames)
88 {
89     frames_ = frames;
90 }
91 
GetFrames() const92 const std::vector<DfxFrame>& BacktraceLocalThread::GetFrames() const
93 {
94     return frames_;
95 }
96 
GetFormattedStr(bool withThreadName)97 std::string BacktraceLocalThread::GetFormattedStr(bool withThreadName)
98 {
99     if (frames_.empty()) {
100         return "";
101     }
102 
103     std::string ss;
104     if (withThreadName && (tid_ > 0)) {
105         std::string threadName;
106         // Tid:1676, Name:IPC_3_1676
107         ReadThreadName(tid_, threadName);
108         ss = "Tid:" + std::to_string(tid_) + ", Name:" + threadName + "\n";
109     }
110     ss += Unwinder::GetFramesStr(frames_);
111     return ss;
112 }
113 
UnwindOtherThreadMix(bool fast,size_t maxFrameNum,size_t skipFrameNum)114 bool BacktraceLocalThread::UnwindOtherThreadMix(bool fast, size_t maxFrameNum, size_t skipFrameNum)
115 {
116     static std::mutex mutex;
117     std::unique_lock<std::mutex> lock(mutex);
118     bool ret = false;
119 
120     if (unwinder_ == nullptr || tid_ < BACKTRACE_CURRENT_THREAD) {
121         return ret;
122     }
123     if (tid_ == BACKTRACE_CURRENT_THREAD || tid_ == gettid()) {
124         ret = unwinder_->UnwindLocal(false, fast, maxFrameNum, skipFrameNum + 1, true);
125     } else {
126         ret = unwinder_->UnwindLocalByOtherTid(tid_, fast, maxFrameNum, skipFrameNum + 1);
127     }
128 #ifdef __aarch64__
129     if (ret && fast) {
130         unwinder_->GetFramesByPcs(frames_, unwinder_->GetPcs());
131     }
132 #endif
133     if (frames_.empty()) {
134         frames_ = unwinder_->GetFrames();
135     }
136     return ret;
137 }
138 } // namespace HiviewDFX
139 } // namespace OHOS
140