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 <map> 23 #include <memory> 24 #include <string> 25 #include "dfx_process.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 class CppCrashReporter { 30 public: CppCrashReporter(uint64_t time,int32_t signo,std::shared_ptr<DfxProcess> process)31 CppCrashReporter(uint64_t time, int32_t signo, std::shared_ptr<DfxProcess> process) \ 32 : time_(time), signo_(signo), 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 stack_.append(frame).append("\n"); 49 }; 50 SetValue(const std::string & key,const std::string & value)51 void SetValue(const std::string& key, const std::string& value) 52 { 53 if (key.empty() || value.empty()) { 54 return; 55 } 56 57 kvPairs_[key] = value; 58 }; 59 void ReportToHiview(); 60 61 private: 62 bool Format(); 63 64 private: 65 uint64_t time_; 66 int32_t signo_; 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