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