• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_process.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class CppCrashReporter {
29 public:
CppCrashReporter(uint64_t time,siginfo_t siginfo,std::shared_ptr<DfxProcess> process)30     CppCrashReporter(uint64_t time, siginfo_t siginfo, std::shared_ptr<DfxProcess> process) \
31         : time_(time), siginfo_(siginfo), process_(process)
32     {
33         pid_ = 0;
34         uid_ = 0;
35         cmdline_ = "";
36         reason_ = "";
37         stack_ = "";
38     };
~CppCrashReporter()39     virtual ~CppCrashReporter() {};
40 
SetCrashReason(const std::string & reason)41     void SetCrashReason(const std::string& reason)
42     {
43         reason_ = reason;
44     };
45 
AppendCrashStack(const std::string & frame)46     void AppendCrashStack(const std::string& frame) {
47         stack_.append(frame).append("\n");
48     };
49 
SetValue(const std::string & key,const std::string & value)50     void SetValue(const std::string& key, const std::string& value)
51     {
52         if (key.empty() || value.empty()) {
53             return;
54         }
55 
56         kvPairs_[key] = value;
57     };
58     void ReportToHiview();
59     void ReportToAbilityManagerService();
60 
61 private:
62     bool Format();
63 
64 private:
65     uint64_t time_;
66     siginfo_t siginfo_;
67     int32_t pid_;
68     uint32_t uid_;
69     std::string cmdline_;
70     std::string reason_;
71     std::string stack_;
72     std::map<std::string, std::string> kvPairs_;
73     std::shared_ptr<DfxProcess> process_;
74 };
75 } // namespace HiviewDFX
76 } // namespace OHOS
77 #endif
78