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 #include "dfx_define.h" 24 #include "dfx_fault_stack.h" 25 #include "dfx_frame.h" 26 #include "dfx_maps.h" 27 #include "dfx_regs.h" 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 32 struct DfxThreadInfo { 33 pid_t pid = 0; 34 pid_t tid = 0; 35 pid_t nsTid = 0; 36 std::string threadName = ""; 37 }; 38 39 class DfxThread { 40 public: 41 static std::shared_ptr<DfxThread> Create(pid_t pid, pid_t tid, pid_t nsTid); 42 DfxThread(pid_t pid, pid_t tid, pid_t nsTid); 43 virtual ~DfxThread(); 44 45 std::shared_ptr<DfxRegs> GetThreadRegs() const; 46 void SetThreadRegs(const std::shared_ptr<DfxRegs> ®s); 47 void AddFrame(std::shared_ptr<DfxFrame> frame); 48 const std::vector<std::shared_ptr<DfxFrame>>& GetFrames() const; 49 std::string ToString() const; 50 51 void Detach(); 52 bool Attach(); 53 54 DfxThreadInfo threadInfo_; 55 private: 56 enum class ThreadStatus { 57 THREAD_STATUS_INVALID = -1, 58 THREAD_STATUS_INIT = 0, 59 THREAD_STATUS_ATTACHED = 1 60 }; 61 62 DfxThread() = default; 63 void InitThreadInfo(pid_t pid, pid_t tid, pid_t nsTid); 64 65 ThreadStatus threadStatus = ThreadStatus::THREAD_STATUS_INVALID; 66 std::shared_ptr<DfxRegs> regs_; 67 std::vector<std::shared_ptr<DfxFrame>> frames_; 68 }; 69 } // namespace HiviewDFX 70 } // namespace OHOS 71 72 #endif 73