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 32namespace OHOS { 33namespace HiviewDFX { 34class ProcessDumper final { 35public: 36 static ProcessDumper &GetInstance(); 37 ~ProcessDumper() = default; 38 39 void Dump(); 40 void WriteDumpRes(int32_t res); 41 bool IsCrash() const; 42 43private: 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 int DumpProcess(std::shared_ptr<ProcessDumpRequest> request); 49 bool InitKeyThread(std::shared_ptr<ProcessDumpRequest> request); 50 int InitPrintThread(std::shared_ptr<ProcessDumpRequest> request); 51 int InitProcessInfo(std::shared_ptr<ProcessDumpRequest> request); 52 bool InitVmThread(std::shared_ptr<ProcessDumpRequest> request); 53 bool InitUnwinder(std::shared_ptr<ProcessDumpRequest> request, pid_t &vmPid, int &dumpRes); 54 void InitRegs(std::shared_ptr<ProcessDumpRequest> request, int &dumpRes); 55 bool IsTargetProcessAlive(std::shared_ptr<ProcessDumpRequest> request); 56 bool Unwind(std::shared_ptr<ProcessDumpRequest> request, int &dumpRes, pid_t vmPid); 57 static int GetLogTypeByRequest(const ProcessDumpRequest &request); 58 void ReportSigDumpStats(const std::shared_ptr<ProcessDumpRequest> &request) const; 59 void ReportCrashInfo(const std::string& jsonInfo); 60 void UnwindWriteJit(const ProcessDumpRequest &request); 61 void Report(std::shared_ptr<ProcessDumpRequest> request, std::string &jsonInfo); 62 void ReadFdTable(const ProcessDumpRequest &request); 63 static std::string ReadStringByPtrace(pid_t tid, uintptr_t addr); 64 void GetCrashObj(std::shared_ptr<ProcessDumpRequest> request); 65 void ReportAddrSanitizer(ProcessDumpRequest &request, std::string &jsonInfo); 66 67private: 68 std::shared_ptr<DfxProcess> process_ = nullptr; 69 std::shared_ptr<CppCrashReporter> reporter_ = nullptr; 70 std::shared_ptr<Unwinder> unwinder_ = nullptr; 71 72 bool isCrash_ = false; 73 bool isJsonDump_ = false; 74 int32_t resFd_ = -1; 75 int32_t jsonFd_ = -1; 76 int32_t resDump_ = 0; 77 78 uint64_t startTime_ = 0; 79 uint64_t finishTime_ = 0; 80}; 81} // namespace HiviewDFX 82} // namespace OHOS 83 84#endif // DFX_PROCESSDUMP_H 85