1 /* 2 * Copyright (c) 2021-2022 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 #ifndef DFX_THREAD_H 16 #define DFX_THREAD_H 17 18 #include <cstdint> 19 #include <string> 20 #include <sys/types.h> 21 #include <vector> 22 #include "dfx_define.h" 23 #include "dfx_fault_stack.h" 24 #include "dfx_frame.h" 25 #include "dfx_maps.h" 26 #include "dfx_regs.h" 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 class DfxThread { 31 public: 32 DfxThread(pid_t pid, pid_t tid, pid_t nsTid, const ucontext_t &context); 33 DfxThread(pid_t pid, pid_t tid, pid_t nsTid); 34 ~DfxThread(); 35 void SetIsCrashThread(bool isCrashThread); 36 bool GetIsCrashThread() const; 37 pid_t GetProcessId() const; 38 pid_t GetThreadId() const; 39 pid_t GetRealTid() const; 40 void SetThreadId(pid_t tid); 41 std::string GetThreadName() const; 42 void SetThreadName(const std::string &threadName); 43 std::shared_ptr<DfxRegs> GetThreadRegs() const; 44 std::vector<std::shared_ptr<DfxFrame>> GetThreadDfxFrames() const; 45 void SetThreadRegs(const std::shared_ptr<DfxRegs> ®s); 46 std::shared_ptr<DfxFrame> GetAvailableFrame(); 47 void PrintThread(const int32_t fd, bool isSignalDump); 48 void PrintThreadBacktraceByConfig(const int32_t fd); 49 std::string PrintThreadRegisterByConfig(); 50 void PrintThreadFaultStackByConfig(); 51 void SetThreadUnwStopReason(int reason); 52 void CreateFaultStack(int32_t vmPid = 0); 53 void Detach(); 54 bool Attach(); 55 std::string ToString() const; 56 bool IsThreadInitialized(); 57 void ClearLastFrame(); 58 void AddFrame(std::shared_ptr<DfxFrame> frame); 59 void ReadThreadName(); 60 61 private: 62 enum class ThreadStatus { 63 THREAD_STATUS_INVALID = 0, 64 THREAD_STATUS_INIT = 1, 65 THREAD_STATUS_DETACHED = 2, 66 THREAD_STATUS_ATTACHED = 3 67 }; 68 69 bool isCrashThread_; 70 pid_t pid_; 71 pid_t tid_; 72 pid_t nsTid_; 73 int unwStopReason_; 74 ThreadStatus threadStatus_; 75 std::string threadName_; 76 std::shared_ptr<DfxRegs> regs_; 77 std::vector<std::shared_ptr<DfxFrame>> dfxFrames_; 78 std::unique_ptr<FaultStack> faultstack_ {nullptr}; 79 }; 80 } // namespace HiviewDFX 81 } // namespace OHOS 82 83 #endif 84