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);
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 GetLogTypeBySignal(int sig);
59     void ReportSigDumpStats(const std::shared_ptr<ProcessDumpRequest> &request) const;
60     void ReportCrashInfo(const std::string& jsonInfo);
61     void UnwindWriteJit(const ProcessDumpRequest &request);
62     std::string ReadStringByPtrace(pid_t tid, uintptr_t addr);
63     void GetCrashObj(std::shared_ptr<ProcessDumpRequest> request);
64 
65 private:
66     std::shared_ptr<DfxProcess> process_ = nullptr;
67     std::shared_ptr<CppCrashReporter> reporter_ = nullptr;
68     std::shared_ptr<Unwinder> unwinder_ = nullptr;
69 
70     bool isCrash_ = false;
71     bool isJsonDump_ = false;
72     int32_t resFd_ = -1;
73     int32_t jsonFd_ = -1;
74     int32_t resDump_ = 0;
75 
76     uint64_t startTime_ = 0;
77     uint64_t finishTime_ = 0;
78 };
79 } // namespace HiviewDFX
80 } // namespace OHOS
81 
82 #endif  // DFX_PROCESSDUMP_H
83