• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CheckCrashLogValid(std::string & file)112 int32_t CheckCrashLogValid(std::string& file)
113 {
114     struct LogValidCheckInfo checkMap[] = {
115         { "Fault thread info:", "Tid:\\d+, Name:\\S+\\s(#(\\d{2})( \\S+){0,10}\\s){3,}", 0,
116           CrashExceptionCode::CRASH_LOG_ESTACKLOS },
117         { "Registers:", "", 0, CrashExceptionCode::CRASH_LOG_EREGLOS }, /* 32bit and 64bit system not same */
118         { "Other thread info:", "Tid:\\d+, Name:\\S+\\s(#(\\d{2})( \\S+){0,10}\\s){3,}", 0,
119           CrashExceptionCode::CRASH_LOG_ECHILDSTACK },
120         { "Memory near registers:", "", 0, CrashExceptionCode::CRASH_LOG_EMEMLOS },
121         { "FaultStack:", "", 0, CrashExceptionCode::CRASH_LOG_ESTACKMEMLOS },
122         { "Maps:", "", 0, CrashExceptionCode::CRASH_LOG_EMAPLOS },
123     };
124 
125     int32_t keySize = sizeof(checkMap) / sizeof(checkMap[0]);
126     for (int i = 0; i < keySize; i++) {
127         checkMap[i].start = file.find(checkMap[i].key);
128         if ((checkMap[i].start == std::string::npos) ||
129             (checkMap[i].start + checkMap[i].key.length() + sizeof(char) >= file.length())) {
130                 return checkMap[i].errCode;
131             }
132     }
133 
134     for (int i = 0; i < keySize; i++) {
135         size_t end = (i == (keySize - 1) ? file.length() : checkMap[i + 1].start);
136         if (end - checkMap[i].start > MAX_FATAL_MSG_SIZE) {
137             end = checkMap[i].start + MAX_FATAL_MSG_SIZE;
138         }
139         std::string tmp = file.substr(checkMap[i].start, end - checkMap[i].start);
140         std::smatch result;
141         if (!std::regex_search(tmp, result, std::regex(checkMap[i].regx))) {
142             return checkMap[i].errCode;
143         }
144     }
145     return CrashExceptionCode::CRASH_ESUCCESS;
146 }
147 
148 } // namespace HiviewDFX
149 } // namesapce OHOS
150