• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROCESSDUMP_H
17 #define DFX_PROCESSDUMP_H
18 
19 #include <cinttypes>
20 #include <condition_variable>
21 #include <future>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <thread>
26 
27 #include "reporter.h"
28 #include "dfx_coredump_service.h"
29 #include "dfx_dump_request.h"
30 #include "dfx_process.h"
31 #include "dump_info.h"
32 #include "nocopyable.h"
33 #include "unwinder.h"
34 
35 namespace OHOS {
36 namespace HiviewDFX {
37 enum DumpType  {
38     THREAD_STATUS_INVALID = -1,
39     THREAD_STATUS_INIT = 0,
40     THREAD_STATUS_ATTACHED = 1
41 };
42 class ProcessDumper final {
43 public:
44     static ProcessDumper &GetInstance();
45     void Dump();
46     void ReportSigDumpStats();
47 
48 private:
49     ProcessDumper() = default;
50     DISALLOW_COPY_AND_MOVE(ProcessDumper);
51     int DumpProcess();
52     int32_t ReadRequestAndCheck();
53     bool InitBufferWriter();
54     bool InitDfxProcess();
55     bool InitUnwinder(int &dumpRes);
56     int GeFaultloggerdRequestType();
57     void UnwindWriteJit();
58     void FormatJsonInfoIfNeed();
59     void UpdateConfigByRequest();
60     void WriteDumpResIfNeed(int32_t resDump);
61     void PrintDumpInfo(int& dumpRes);
62     int ParseSymbols(std::shared_ptr<DumpInfo> threadDumpInfo);
63     std::vector<std::string> FindDumpInfoByType(const ProcessDumpType& dumpType);
64     int32_t CreateFileForCrash(int32_t pid, uint64_t time) const;
65     void RemoveFileIfNeed(const std::string& dirPath) const;
66 
67 private:
68     void SetProcessdumpTimeout(siginfo_t &si);
69     std::shared_ptr<DfxProcess> process_ = nullptr;
70     std::shared_ptr<Unwinder> unwinder_ = nullptr;
71     ProcessDumpRequest request_{};
72     uint64_t startTime_ = 0;
73     uint64_t finishTime_ = 0;
74 
75     static constexpr size_t DEFAULT_MAX_STRING_LEN = 2048;
76     bool isJsonDump_ = false;
77     uint64_t expectedDumpFinishTime_ = 0;
78     std::future<void> parseSymbolTask_;
79 #if defined(__aarch64__)
80     std::unique_ptr<CoreDumpService> coreDumpService_ = nullptr;
81 #endif
82     uint64_t finishParseSymbolTime_ = 0;
83 };
84 } // namespace HiviewDFX
85 } // namespace OHOS
86 
87 #endif  // DFX_PROCESSDUMP_H
88