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 <sstream>
20 #include <unistd.h>
21 
22 #include "dfx_define.h"
23 #include "procinfo.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 #undef LOG_DOMAIN
29 #undef LOG_TAG
30 #define LOG_DOMAIN 0xD002D11
31 #define LOG_TAG "DfxBacktraceLocal"
32 }
33 
BacktraceLocalThread(int32_t tid,std::shared_ptr<Unwinder> unwinder)34 BacktraceLocalThread::BacktraceLocalThread(int32_t tid, std::shared_ptr<Unwinder> unwinder)
35     : tid_(tid), unwinder_(unwinder)
36 {
37     frames_.clear();
38 }
39 
~BacktraceLocalThread()40 BacktraceLocalThread::~BacktraceLocalThread()
41 {
42     frames_.clear();
43 }
44 
Unwind(bool fast,size_t maxFrameNum,size_t skipFrameNum)45 bool BacktraceLocalThread::Unwind(bool fast, size_t maxFrameNum, size_t skipFrameNum)
46 {
47     static std::mutex mutex;
48     std::unique_lock<std::mutex> lock(mutex);
49     bool ret = false;
50 
51     if (unwinder_ == nullptr || tid_ < BACKTRACE_CURRENT_THREAD) {
52         return ret;
53     }
54 
55     if (tid_ == BACKTRACE_CURRENT_THREAD) {
56         ret = unwinder_->UnwindLocal(false, fast, maxFrameNum, skipFrameNum + 1);
57 #ifdef __aarch64__
58         if (fast) {
59             Unwinder::GetLocalFramesByPcs(frames_, unwinder_->GetPcs());
60         }
61 #endif
62         if (frames_.empty()) {
63             frames_ = unwinder_->GetFrames();
64         }
65         return ret;
66     }
67 
68     ret = unwinder_->UnwindLocalWithTid(tid_, maxFrameNum, skipFrameNum + 1);
69 #ifdef __aarch64__
70     Unwinder::GetLocalFramesByPcs(frames_, unwinder_->GetPcs());
71 #else
72     frames_ = unwinder_->GetFrames();
73 #endif
74     return ret;
75 }
76 
SetFrames(const std::vector<DfxFrame> & frames)77 void BacktraceLocalThread::SetFrames(const std::vector<DfxFrame>& frames)
78 {
79     frames_ = frames;
80 }
81 
GetFrames() const82 const std::vector<DfxFrame>& BacktraceLocalThread::GetFrames() const
83 {
84     return frames_;
85 }
86 
GetFormattedStr(bool withThreadName)87 std::string BacktraceLocalThread::GetFormattedStr(bool withThreadName)
88 {
89     if (frames_.empty()) {
90         return "";
91     }
92 
93     std::ostringstream ss;
94     if (withThreadName && (tid_ > 0)) {
95         std::string threadName;
96         // Tid:1676, Name:IPC_3_1676
97         ReadThreadName(tid_, threadName);
98         ss << "Tid:" << tid_ << ", Name:" << threadName << std::endl;
99     }
100     ss << Unwinder::GetFramesStr(frames_);
101     return ss.str();
102 }
103 } // namespace HiviewDFX
104 } // namespace OHOS
105