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_PROCESS_H 17 #define DFX_PROCESS_H 18 19 #include <cinttypes> 20 #include <map> 21 #include <memory> 22 #include <set> 23 #include <string> 24 25 #include "dfx_dump_request.h" 26 #include "dfx_regs.h" 27 #include "dfx_thread.h" 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 struct DfxProcessInfo { 32 pid_t pid = 0; 33 pid_t nsPid = 0; 34 uid_t uid = 0; 35 std::string processName = ""; 36 }; 37 struct CrashLogConfig { 38 bool extendPcLrPrinting = false; 39 bool simplifyVmaPrinting = false; 40 uint32_t logFileCutoffSizeBytes = 0; 41 }; 42 class DfxProcess final { 43 public: 44 void InitProcessInfo(pid_t pid, pid_t nsPid, uid_t uid, const std::string& processName); 45 void Attach(bool hasKey = false); 46 void Detach(); 47 bool InitKeyThread(const ProcessDumpRequest& request); 48 bool InitOtherThreads(pid_t requestTid); 49 std::vector<std::shared_ptr<DfxThread>>& GetOtherThreads(); GetKeyThread()50 std::shared_ptr<DfxThread>& GetKeyThread() 51 { 52 return keyThread_; 53 } 54 GetProcessInfo()55 const DfxProcessInfo& GetProcessInfo() const 56 { 57 return processInfo_; 58 } 59 SetFaultThreadRegisters(std::shared_ptr<DfxRegs> regs)60 void SetFaultThreadRegisters(std::shared_ptr<DfxRegs> regs) 61 { 62 regs_ = regs; 63 } 64 GetFaultThreadRegisters()65 const std::shared_ptr<DfxRegs>& GetFaultThreadRegisters() const 66 { 67 return regs_; 68 } 69 SetReason(const std::string & reason)70 void SetReason(const std::string& reason) 71 { 72 reason_ = reason; 73 } 74 GetReason()75 const std::string& GetReason() const 76 { 77 return reason_; 78 } 79 GetCrashInfoJson()80 const std::string& GetCrashInfoJson() const 81 { 82 return crashInfoJson_; 83 } 84 SetCrashInfoJson(const std::string & crashInfoJson)85 void SetCrashInfoJson(const std::string& crashInfoJson) 86 { 87 crashInfoJson_ = crashInfoJson; 88 } 89 SetVmPid(pid_t pid)90 void SetVmPid(pid_t pid) 91 { 92 vmPid_ = pid; 93 } 94 GetVmPid()95 pid_t GetVmPid() const 96 { 97 return vmPid_; 98 } 99 GetCrashLogConfig()100 const CrashLogConfig& GetCrashLogConfig() 101 { 102 return crashLogConfig_; 103 } 104 SetCrashLogConfig(const CrashLogConfig & crashLogConfig)105 void SetCrashLogConfig(const CrashLogConfig& crashLogConfig) 106 { 107 crashLogConfig_ = crashLogConfig; 108 } 109 GetMemoryValues()110 const std::set<uintptr_t>& GetMemoryValues() 111 { 112 return memoryValues_; 113 } 114 SetMemoryValues(const std::set<uintptr_t> & memoryValues)115 void SetMemoryValues(const std::set<uintptr_t>& memoryValues) 116 { 117 memoryValues_ = memoryValues; 118 } 119 120 void ClearOtherThreads(); 121 pid_t ChangeTid(pid_t tid, bool ns); 122 123 void AppendFatalMessage(const std::string &msg); 124 const std::string& GetFatalMessage() const; 125 std::string GetProcessLifeCycle(); 126 private: 127 DfxProcessInfo processInfo_; 128 CrashLogConfig crashLogConfig_; 129 std::shared_ptr<DfxRegs> regs_; 130 std::shared_ptr<DfxThread> keyThread_ = nullptr; // comment: crash thread or dump target thread 131 std::vector<std::shared_ptr<DfxThread>> otherThreads_; 132 std::string reason_ = ""; 133 std::string fatalMsg_ = ""; 134 std::map<int, int> kvThreads_; 135 std::string crashInfoJson_ = ""; 136 pid_t vmPid_ = 0; 137 std::set<uintptr_t> memoryValues_; 138 }; 139 } // namespace HiviewDFX 140 } // namespace OHOS 141 #endif 142