1 /*
2 * Copyright (c) 2024 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 #include "crash_exception.h"
17
18 #include <map>
19 #include <regex>
20 #include <sys/time.h>
21 #include "dfx_errors.h"
22 #ifndef HISYSEVENT_DISABLE
23 #include "hisysevent.h"
24 #endif
25
26 namespace OHOS {
27 namespace HiviewDFX {
28
29 static bool g_isInitProcessInfo = false;
30 static std::string g_crashProcessName = "";
31 static int32_t g_crashProcessPid = 0;
32 static int32_t g_crashProcessUid = 0;
33
GetTimeMillisec(void)34 uint64_t GetTimeMillisec(void)
35 {
36 struct timespec ts;
37 (void)clock_gettime(CLOCK_REALTIME, &ts);
38 return ((uint64_t)ts.tv_sec * NUMBER_ONE_THOUSAND) +
39 (((uint64_t)ts.tv_nsec) / NUMBER_ONE_MILLION);
40 }
41
SetCrashProcInfo(const ProcessDumpType & dumpType,const std::string & name,int32_t pid,int32_t uid)42 void SetCrashProcInfo(const ProcessDumpType& dumpType, const std::string& name, int32_t pid, int32_t uid)
43 {
44 if (pid <= 0 || dumpType != ProcessDumpType::DUMP_TYPE_CPP_CRASH) {
45 return;
46 }
47 g_isInitProcessInfo = true;
48 g_crashProcessName = name;
49 g_crashProcessPid = pid;
50 g_crashProcessUid = uid;
51 }
52
ReportCrashException(int32_t errCode)53 void ReportCrashException(int32_t errCode)
54 {
55 if (!g_isInitProcessInfo) {
56 return;
57 }
58 ReportCrashException(g_crashProcessName, g_crashProcessPid, g_crashProcessUid, errCode);
59 }
60
ReportCrashException(std::string name,int32_t pid,int32_t uid,int32_t errCode)61 void ReportCrashException(std::string name, int32_t pid, int32_t uid, int32_t errCode)
62 {
63 #ifndef HISYSEVENT_DISABLE
64 if (errCode == CrashExceptionCode::CRASH_ESUCCESS) {
65 return;
66 }
67 HiSysEventWrite(
68 HiSysEvent::Domain::RELIABILITY,
69 "CPP_CRASH_EXCEPTION",
70 HiSysEvent::EventType::FAULT,
71 "PROCESS_NAME", name.c_str(),
72 "PID", pid,
73 "UID", uid,
74 "HAPPEN_TIME", GetTimeMillisec(),
75 "ERROR_CODE", errCode,
76 "ERROR_MSG", GetCrashDescription(errCode));
77 #endif
78 }
79
ReportUnwinderException(uint16_t unwError)80 void ReportUnwinderException(uint16_t unwError)
81 {
82 if (!g_isInitProcessInfo) {
83 return;
84 }
85
86 const std::map<uint16_t, int32_t> unwMaps = {
87 { UnwindErrorCode::UNW_ERROR_STEP_ARK_FRAME, CrashExceptionCode::CRASH_UNWIND_EFRAME },
88 { UnwindErrorCode::UNW_ERROR_INVALID_CONTEXT, CrashExceptionCode::CRASH_UNWIND_ECONTEXT },
89 };
90 int32_t errCode = 0;
91 auto iter = unwMaps.find(unwError);
92 if (iter == unwMaps.end()) {
93 return;
94 }
95 errCode = iter->second;
96 ReportCrashException(g_crashProcessName, g_crashProcessPid, g_crashProcessUid, errCode);
97 }
98
CheckFaultSummaryValid(const std::string & summary)99 bool CheckFaultSummaryValid(const std::string &summary)
100 {
101 return (summary.find("#00") != std::string::npos) && (summary.find("#01") != std::string::npos) &&
102 (summary.find("#02") != std::string::npos);
103 }
104
105 } // namespace HiviewDFX
106 } // namesapce OHOS
107