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(std::string& name, int32_t pid, int32_t uid)42 void SetCrashProcInfo(std::string& name, int32_t pid, int32_t uid)
43 {
44 if (pid <= 0) {
45 return;
46 }
47 g_isInitProcessInfo = true;
48 g_crashProcessName = name;
49 g_crashProcessPid = pid;
50 g_crashProcessUid = uid;
51 }
52
GetCrashDescription(const int32_t errCode)53 static const char* GetCrashDescription(const int32_t errCode)
54 {
55 size_t i;
56
57 for (i = 0; i < sizeof(g_crashExceptionMap) / sizeof(g_crashExceptionMap[0]); i++) {
58 if (errCode == g_crashExceptionMap[i].errCode) {
59 return g_crashExceptionMap[i].str;
60 }
61 }
62 return g_crashExceptionMap[i - 1].str; /* the end of map is "unknown reason" */
63 }
64
ReportCrashException(const char* pName, int32_t pid, int32_t uid, int32_t errCode)65 void ReportCrashException(const char* pName, int32_t pid, int32_t uid, int32_t errCode)
66 {
67 if (pName == nullptr || strnlen(pName, NAME_BUF_LEN) == NAME_BUF_LEN) {
68 return;
69 }
70
71 ReportCrashException(std::string(pName), pid, uid, errCode);
72 }
73
ReportCrashException(std::string name, int32_t pid, int32_t uid, int32_t errCode)74 void ReportCrashException(std::string name, int32_t pid, int32_t uid, int32_t errCode)
75 {
76 #ifndef HISYSEVENT_DISABLE
77 if (errCode == CrashExceptionCode::CRASH_ESUCCESS) {
78 return;
79 }
80 HiSysEventWrite(
81 HiSysEvent::Domain::RELIABILITY,
82 "CPP_CRASH_EXCEPTION",
83 HiSysEvent::EventType::FAULT,
84 "PROCESS_NAME", name,
85 "PID", pid,
86 "UID", uid,
87 "HAPPEN_TIME", GetTimeMillisec(),
88 "ERROR_CODE", errCode,
89 "ERROR_MSG", GetCrashDescription(errCode));
90 #endif
91 }
92
ReportUnwinderException(uint16_t unwError)93 void ReportUnwinderException(uint16_t unwError)
94 {
95 if (!g_isInitProcessInfo) {
96 return;
97 }
98
99 const std::map<uint16_t, int32_t> unwMaps = {
100 { UnwindErrorCode::UNW_ERROR_STEP_ARK_FRAME, CrashExceptionCode::CRASH_UNWIND_EFRAME },
101 { UnwindErrorCode::UNW_ERROR_INVALID_CONTEXT, CrashExceptionCode::CRASH_UNWIND_ECONTEXT },
102 };
103 int32_t errCode = 0;
104 auto iter = unwMaps.find(unwError);
105 if (iter == unwMaps.end()) {
106 return;
107 }
108 errCode = iter->second;
109 ReportCrashException(g_crashProcessName, g_crashProcessPid, g_crashProcessUid, errCode);
110 }
111
CheckFaultSummaryValid(const std::string &summary)112 bool CheckFaultSummaryValid(const std::string &summary)
113 {
114 return (summary.find("#00 pc") != std::string::npos) && (summary.find("#01 pc") != std::string::npos) &&
115 (summary.find("#02 pc") != std::string::npos);
116 }
117
118 } // namespace HiviewDFX
119 } // namesapce OHOS
120