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 #include "unwinder.h" 30 31 namespace OHOS { 32 namespace HiviewDFX { 33 34 struct DfxThreadInfo { 35 pid_t pid = 0; 36 pid_t tid = 0; 37 pid_t nsTid = 0; 38 std::string threadName = ""; 39 }; 40 41 class DfxThread { 42 public: 43 static std::shared_ptr<DfxThread> Create(pid_t pid, pid_t tid, pid_t nsTid); 44 DfxThread(pid_t pid, pid_t tid, pid_t nsTid); 45 virtual ~DfxThread(); 46 47 std::shared_ptr<DfxRegs> GetThreadRegs() const; 48 void SetThreadRegs(const std::shared_ptr<DfxRegs> ®s); 49 void AddFrame(DfxFrame& frame); 50 std::vector<DfxFrame>& GetFrames(); 51 void SetFrames(const std::vector<DfxFrame>& frames); 52 void InitFaultStack(bool needParseStack = false); 53 void ParseSymbol(std::shared_ptr<Unwinder> unwinder); 54 std::shared_ptr<FaultStack> GetFaultStack() const; 55 std::string ToString() const; 56 57 void Detach(); 58 bool Attach(int timeout = PTRACE_ATTATCH_KEY_THREAD_TIMEOUT); 59 void SetParseSymbolNecessity(bool needParseSymbol); 60 61 DfxThreadInfo threadInfo_; 62 private: 63 enum class ThreadStatus { 64 THREAD_STATUS_INVALID = -1, 65 THREAD_STATUS_INIT = 0, 66 THREAD_STATUS_ATTACHED = 1 67 }; 68 69 DfxThread() = default; 70 void InitThreadInfo(pid_t pid, pid_t tid, pid_t nsTid); 71 72 ThreadStatus threadStatus = ThreadStatus::THREAD_STATUS_INVALID; 73 std::shared_ptr<DfxRegs> regs_; 74 std::vector<DfxFrame> frames_; 75 std::shared_ptr<FaultStack> faultStack_; 76 bool needParseSymbol_ = true; 77 }; 78 } // namespace HiviewDFX 79 } // namespace OHOS 80 81 #endif 82