1 /* 2 * Copyright (c) 2021 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 /* This files contains process dump write log to file module. */ 17 18 #ifndef CPP_CRASH_REPORTER_H 19 #define CPP_CRASH_REPORTER_H 20 21 #include <cinttypes> 22 #include <csignal> 23 #include <map> 24 #include <memory> 25 #include <string> 26 #include "dfx_process.h" 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 class CppCrashReporter { 31 public: CppCrashReporter(uint64_t time,siginfo_t siginfo,std::shared_ptr<DfxProcess> process)32 CppCrashReporter(uint64_t time, siginfo_t siginfo, std::shared_ptr<DfxProcess> process) \ 33 : time_(time), siginfo_(siginfo), process_(process) 34 { 35 pid_ = 0; 36 uid_ = 0; 37 cmdline_ = ""; 38 reason_ = ""; 39 stack_ = ""; 40 }; ~CppCrashReporter()41 virtual ~CppCrashReporter() {}; 42 SetCrashReason(const std::string & reason)43 void SetCrashReason(const std::string& reason) 44 { 45 reason_ = reason; 46 }; 47 AppendCrashStack(const std::string & frame)48 void AppendCrashStack(const std::string& frame) { 49 stack_.append(frame).append("\n"); 50 }; 51 SetValue(const std::string & key,const std::string & value)52 void SetValue(const std::string& key, const std::string& value) 53 { 54 if (key.empty() || value.empty()) { 55 return; 56 } 57 58 kvPairs_[key] = value; 59 }; 60 void ReportToHiview(); 61 62 private: 63 bool Format(); 64 65 private: 66 uint64_t time_; 67 siginfo_t siginfo_; 68 int32_t pid_; 69 uint32_t uid_; 70 std::string cmdline_; 71 std::string reason_; 72 std::string stack_; 73 std::map<std::string, std::string> kvPairs_; 74 std::shared_ptr<DfxProcess> process_; 75 }; 76 } // namespace HiviewDFX 77 } // namespace OHOS 78 #endif 79