1 /* 2 * Copyright (c) 2021-2025 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 #ifndef DFX_THREAD_H 17 #define DFX_THREAD_H 18 19 #include <cstdint> 20 #include <string> 21 #include <sys/types.h> 22 #include <vector> 23 24 #include "dfx_define.h" 25 #include "dfx_frame.h" 26 #include "dfx_maps.h" 27 #include "dfx_regs.h" 28 #include "unwinder.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 33 struct DfxThreadInfo { 34 pid_t pid = 0; 35 pid_t tid = 0; 36 pid_t nsTid = 0; 37 std::string threadName = ""; 38 }; 39 40 class DfxThread { 41 public: 42 static std::shared_ptr<DfxThread> Create(pid_t pid, pid_t tid, pid_t nsTid); 43 DfxThread(pid_t pid, pid_t tid, pid_t nsTid); 44 virtual ~DfxThread(); 45 46 std::shared_ptr<DfxRegs> GetThreadRegs() const; 47 void SetThreadRegs(const std::shared_ptr<DfxRegs> ®s); 48 void AddFrame(const DfxFrame& frame); 49 const std::vector<DfxFrame>& GetFrames() const; 50 void SetFrames(const std::vector<DfxFrame>& frames); SetSubmitterFrames(const std::vector<DfxFrame> & frames)51 void SetSubmitterFrames(const std::vector<DfxFrame>& frames) 52 { 53 submitterFrames_ = frames; 54 } 55 void ParseSymbol(Unwinder& unwinder); 56 std::string ToString(bool needPrintTid = true) const; GetThreadInfo()57 const DfxThreadInfo& GetThreadInfo() const 58 { 59 return threadInfo_; 60 } SetThreadName(const std::string & threadName)61 void SetThreadName(const std::string& threadName) 62 { 63 threadInfo_.threadName = threadName; 64 } 65 66 void Detach(); 67 bool Attach(int timeout = PTRACE_ATTATCH_KEY_THREAD_TIMEOUT); 68 void SetParseSymbolNecessity(bool needParseSymbol); 69 private: 70 enum class ThreadStatus { 71 THREAD_STATUS_INVALID = -1, 72 THREAD_STATUS_INIT = 0, 73 THREAD_STATUS_ATTACHED = 1 74 }; 75 76 DfxThread() = default; 77 void InitThreadInfo(pid_t pid, pid_t tid, pid_t nsTid); 78 DfxThreadInfo threadInfo_; 79 ThreadStatus threadStatus = ThreadStatus::THREAD_STATUS_INVALID; 80 std::shared_ptr<DfxRegs> regs_ = nullptr; 81 std::vector<DfxFrame> frames_; 82 std::vector<DfxFrame> submitterFrames_; 83 bool needParseSymbol_ = true; 84 }; 85 } // namespace HiviewDFX 86 } // namespace OHOS 87 88 #endif 89