1 /* 2 * Copyright (c) 2021-2023 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_fault_stack.h" 26 #include "dfx_frame.h" 27 #include "dfx_maps.h" 28 #include "dfx_regs.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(DfxFrame& frame); 49 const std::vector<DfxFrame>& GetFrames() const; 50 void SetFrames(const std::vector<DfxFrame>& frames); 51 void InitFaultStack(bool needParseStack = false); 52 std::shared_ptr<FaultStack> GetFaultStack() const; 53 std::string ToString() const; 54 55 void Detach(); 56 bool Attach(int timeout = PTRACE_ATTATCH_KEY_THREAD_TIMEOUT); 57 58 DfxThreadInfo threadInfo_; 59 private: 60 enum class ThreadStatus { 61 THREAD_STATUS_INVALID = -1, 62 THREAD_STATUS_INIT = 0, 63 THREAD_STATUS_ATTACHED = 1 64 }; 65 66 DfxThread() = default; 67 void InitThreadInfo(pid_t pid, pid_t tid, pid_t nsTid); 68 69 ThreadStatus threadStatus = ThreadStatus::THREAD_STATUS_INVALID; 70 std::shared_ptr<DfxRegs> regs_; 71 std::vector<DfxFrame> frames_; 72 std::shared_ptr<FaultStack> faultStack_; 73 }; 74 } // namespace HiviewDFX 75 } // namespace OHOS 76 77 #endif 78