1 /* 2 * Copyright (c) 2021-2024 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_PROCESSDUMP_H 17 #define DFX_PROCESSDUMP_H 18 19 #include <cinttypes> 20 #include <condition_variable> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <thread> 25 26 #include "cppcrash_reporter.h" 27 #include "dfx_dump_request.h" 28 #include "dfx_process.h" 29 #include "nocopyable.h" 30 #include "unwinder.h" 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 class ProcessDumper final { 35 public: 36 static ProcessDumper &GetInstance(); 37 ~ProcessDumper() = default; 38 39 void Dump(); 40 void WriteDumpRes(int32_t res, pid_t pid); 41 bool IsCrash() const; 42 43 private: 44 ProcessDumper() = default; 45 DISALLOW_COPY_AND_MOVE(ProcessDumper); 46 static int WriteDumpBuf(int fd, const char* buf, const int len); 47 int32_t CreateFileForCrash(int32_t pid, uint64_t time) const; 48 static void RemoveFileIfNeed(); 49 int DumpProcess(std::shared_ptr<ProcessDumpRequest> request); 50 bool InitKeyThread(std::shared_ptr<ProcessDumpRequest> request); 51 int InitPrintThread(std::shared_ptr<ProcessDumpRequest> request); 52 int InitProcessInfo(std::shared_ptr<ProcessDumpRequest> request); 53 bool InitVmThread(std::shared_ptr<ProcessDumpRequest> request); 54 bool InitUnwinder(std::shared_ptr<ProcessDumpRequest> request, pid_t &vmPid, int &dumpRes); 55 void InitRegs(std::shared_ptr<ProcessDumpRequest> request, int &dumpRes); 56 bool IsTargetProcessAlive(std::shared_ptr<ProcessDumpRequest> request); 57 bool Unwind(std::shared_ptr<ProcessDumpRequest> request, int &dumpRes, pid_t vmPid); 58 static int GetLogTypeByRequest(const ProcessDumpRequest &request); 59 void ReportSigDumpStats(const std::shared_ptr<ProcessDumpRequest> &request) const; 60 void ReportCrashInfo(const std::string& jsonInfo, const ProcessDumpRequest &request); 61 void UnwindWriteJit(const ProcessDumpRequest &request); 62 void Report(std::shared_ptr<ProcessDumpRequest> request, std::string &jsonInfo); 63 void ReadFdTable(const ProcessDumpRequest &request); 64 static std::string ReadStringByPtrace(pid_t tid, uintptr_t addr, size_t maxLen = DEFAULT_MAX_STRING_LEN); 65 void UpdateFatalMessageWhenDebugSignal(const ProcessDumpRequest& request, pid_t vmPid); 66 std::string ReadCrashObjString(pid_t tid, uintptr_t addr) const; 67 std::string ReadCrashObjMemory(pid_t tid, uintptr_t addr, size_t length) const; 68 void GetCrashObj(std::shared_ptr<ProcessDumpRequest> request); 69 void ReportAddrSanitizer(ProcessDumpRequest &request, std::string &jsonInfo); 70 void UnwindFinish(std::shared_ptr<ProcessDumpRequest> request, pid_t vmPid); 71 72 private: 73 std::shared_ptr<DfxProcess> process_ = nullptr; 74 std::shared_ptr<Unwinder> unwinder_ = nullptr; 75 76 bool isCrash_ = false; 77 bool isJsonDump_ = false; 78 int32_t resFd_ = -1; 79 int32_t bufferFd_ = -1; 80 int32_t resDump_ = 0; 81 82 uint64_t startTime_ = 0; 83 uint64_t finishTime_ = 0; 84 static constexpr size_t DEFAULT_MAX_STRING_LEN = 2048; 85 }; 86 } // namespace HiviewDFX 87 } // namespace OHOS 88 89 #endif // DFX_PROCESSDUMP_H 90