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 
BacktraceLocalThread(int32_t tid, std::shared_ptr<Unwinder> unwinder)33 BacktraceLocalThread::BacktraceLocalThread(int32_t tid, std::shared_ptr<Unwinder> unwinder)
34     : tid_(tid), unwinder_(unwinder)
35 {
36     frames_.clear();
37 }
38 
~BacktraceLocalThread()39 BacktraceLocalThread::~BacktraceLocalThread()
40 {
41     frames_.clear();
42 }
43 
Unwind(bool fast, size_t maxFrameNum, size_t skipFrameNum)44 bool BacktraceLocalThread::Unwind(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 (unwinder_ == nullptr || 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     ss += Unwinder::GetFramesStr(frames_);
100     return ss;
101 }
102 } // namespace HiviewDFX
103 } // namespace OHOS
104