1 /*
2  * Copyright (c) 2021-2023 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 CPP_CRASH_REPORTER_H
17 #define CPP_CRASH_REPORTER_H
18 
19 #include <cinttypes>
20 #include <csignal>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include "dfx_dump_request.h"
25 #include "dfx_process.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class CppCrashReporter {
30 public:
CppCrashReporter(uint64_t time,std::shared_ptr<DfxProcess> process,int32_t dumpMode)31     CppCrashReporter(uint64_t time, std::shared_ptr<DfxProcess> process, int32_t dumpMode) \
32         : time_(time), dumpMode_(dumpMode), process_(process)
33     {
34         pid_ = 0;
35         uid_ = 0;
36         cmdline_ = "";
37         reason_ = "";
38         stack_ = "";
39     };
~CppCrashReporter()40     virtual ~CppCrashReporter() {};
41 
SetCrashReason(const std::string & reason)42     void SetCrashReason(const std::string& reason)
43     {
44         reason_ = reason;
45     };
46 
AppendCrashStack(const std::string & frame)47     void AppendCrashStack(const std::string& frame)
48     {
49         stack_.append(frame).append("\n");
50     };
51 
SetCppCrashInfo(const std::string & cppCrashInfo)52     void SetCppCrashInfo(const std::string& cppCrashInfo)
53     {
54         cppCrashInfo_ = cppCrashInfo;
55     };
56     void ReportToHiview();
57     void ReportToAbilityManagerService();
58 
59 private:
60     bool Format();
61     static std::string GetRegsString(std::shared_ptr<DfxRegs> regs);
62     int32_t WriteCppCrashInfoByPipe();
63 
64 private:
65     uint64_t time_;
66     int32_t pid_;
67     uint32_t uid_;
68     int32_t dumpMode_ = FUSION_MODE;
69     std::string cmdline_;
70     std::string reason_;
71     std::string stack_;
72     std::string registers_ = "";
73     std::string cppCrashInfo_ = "";
74     std::shared_ptr<DfxProcess> process_;
75 };
76 } // namespace HiviewDFX
77 } // namespace OHOS
78 #endif
79