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,std::shared_ptr<DfxProcess> process)30 CppCrashReporter(uint64_t time, std::shared_ptr<DfxProcess> process) \ 31 : time_(time), 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 { 48 stack_.append(frame).append("\n"); 49 }; 50 SetCppCrashInfo(const std::string & cppCrashInfo)51 void SetCppCrashInfo(const std::string& cppCrashInfo) 52 { 53 cppCrashInfo_ = cppCrashInfo; 54 }; 55 void ReportToHiview(); 56 void ReportToAbilityManagerService(); 57 58 private: 59 bool Format(); 60 static std::string GetRegsString(std::shared_ptr<DfxThread> thread); 61 int32_t WriteCppCrashInfoByPipe(); 62 63 private: 64 uint64_t time_; 65 int32_t pid_; 66 uint32_t uid_; 67 std::string cmdline_; 68 std::string reason_; 69 std::string stack_; 70 std::string registers_ = ""; 71 std::string cppCrashInfo_ = ""; 72 std::shared_ptr<DfxProcess> process_; 73 }; 74 } // namespace HiviewDFX 75 } // namespace OHOS 76 #endif 77