1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include "crash_exception.h" 17800b99b8Sopenharmony_ci 18800b99b8Sopenharmony_ci#include <map> 19800b99b8Sopenharmony_ci#include <regex> 20800b99b8Sopenharmony_ci#include <sys/time.h> 21800b99b8Sopenharmony_ci#include "dfx_errors.h" 22800b99b8Sopenharmony_ci#ifndef HISYSEVENT_DISABLE 23800b99b8Sopenharmony_ci#include "hisysevent.h" 24800b99b8Sopenharmony_ci#endif 25800b99b8Sopenharmony_ci 26800b99b8Sopenharmony_cinamespace OHOS { 27800b99b8Sopenharmony_cinamespace HiviewDFX { 28800b99b8Sopenharmony_ci 29800b99b8Sopenharmony_cistatic bool g_isInitProcessInfo = false; 30800b99b8Sopenharmony_cistatic std::string g_crashProcessName = ""; 31800b99b8Sopenharmony_cistatic int32_t g_crashProcessPid = 0; 32800b99b8Sopenharmony_cistatic int32_t g_crashProcessUid = 0; 33800b99b8Sopenharmony_ci 34800b99b8Sopenharmony_ciuint64_t GetTimeMillisec(void) 35800b99b8Sopenharmony_ci{ 36800b99b8Sopenharmony_ci struct timespec ts; 37800b99b8Sopenharmony_ci (void)clock_gettime(CLOCK_REALTIME, &ts); 38800b99b8Sopenharmony_ci return ((uint64_t)ts.tv_sec * NUMBER_ONE_THOUSAND) + 39800b99b8Sopenharmony_ci (((uint64_t)ts.tv_nsec) / NUMBER_ONE_MILLION); 40800b99b8Sopenharmony_ci} 41800b99b8Sopenharmony_ci 42800b99b8Sopenharmony_civoid SetCrashProcInfo(std::string& name, int32_t pid, int32_t uid) 43800b99b8Sopenharmony_ci{ 44800b99b8Sopenharmony_ci if (pid <= 0) { 45800b99b8Sopenharmony_ci return; 46800b99b8Sopenharmony_ci } 47800b99b8Sopenharmony_ci g_isInitProcessInfo = true; 48800b99b8Sopenharmony_ci g_crashProcessName = name; 49800b99b8Sopenharmony_ci g_crashProcessPid = pid; 50800b99b8Sopenharmony_ci g_crashProcessUid = uid; 51800b99b8Sopenharmony_ci} 52800b99b8Sopenharmony_ci 53800b99b8Sopenharmony_cistatic const char* GetCrashDescription(const int32_t errCode) 54800b99b8Sopenharmony_ci{ 55800b99b8Sopenharmony_ci size_t i; 56800b99b8Sopenharmony_ci 57800b99b8Sopenharmony_ci for (i = 0; i < sizeof(g_crashExceptionMap) / sizeof(g_crashExceptionMap[0]); i++) { 58800b99b8Sopenharmony_ci if (errCode == g_crashExceptionMap[i].errCode) { 59800b99b8Sopenharmony_ci return g_crashExceptionMap[i].str; 60800b99b8Sopenharmony_ci } 61800b99b8Sopenharmony_ci } 62800b99b8Sopenharmony_ci return g_crashExceptionMap[i - 1].str; /* the end of map is "unknown reason" */ 63800b99b8Sopenharmony_ci} 64800b99b8Sopenharmony_ci 65800b99b8Sopenharmony_civoid ReportCrashException(const char* pName, int32_t pid, int32_t uid, int32_t errCode) 66800b99b8Sopenharmony_ci{ 67800b99b8Sopenharmony_ci if (pName == nullptr || strnlen(pName, NAME_BUF_LEN) == NAME_BUF_LEN) { 68800b99b8Sopenharmony_ci return; 69800b99b8Sopenharmony_ci } 70800b99b8Sopenharmony_ci 71800b99b8Sopenharmony_ci ReportCrashException(std::string(pName), pid, uid, errCode); 72800b99b8Sopenharmony_ci} 73800b99b8Sopenharmony_ci 74800b99b8Sopenharmony_civoid ReportCrashException(std::string name, int32_t pid, int32_t uid, int32_t errCode) 75800b99b8Sopenharmony_ci{ 76800b99b8Sopenharmony_ci#ifndef HISYSEVENT_DISABLE 77800b99b8Sopenharmony_ci if (errCode == CrashExceptionCode::CRASH_ESUCCESS) { 78800b99b8Sopenharmony_ci return; 79800b99b8Sopenharmony_ci } 80800b99b8Sopenharmony_ci HiSysEventWrite( 81800b99b8Sopenharmony_ci HiSysEvent::Domain::RELIABILITY, 82800b99b8Sopenharmony_ci "CPP_CRASH_EXCEPTION", 83800b99b8Sopenharmony_ci HiSysEvent::EventType::FAULT, 84800b99b8Sopenharmony_ci "PROCESS_NAME", name, 85800b99b8Sopenharmony_ci "PID", pid, 86800b99b8Sopenharmony_ci "UID", uid, 87800b99b8Sopenharmony_ci "HAPPEN_TIME", GetTimeMillisec(), 88800b99b8Sopenharmony_ci "ERROR_CODE", errCode, 89800b99b8Sopenharmony_ci "ERROR_MSG", GetCrashDescription(errCode)); 90800b99b8Sopenharmony_ci#endif 91800b99b8Sopenharmony_ci} 92800b99b8Sopenharmony_ci 93800b99b8Sopenharmony_civoid ReportUnwinderException(uint16_t unwError) 94800b99b8Sopenharmony_ci{ 95800b99b8Sopenharmony_ci if (!g_isInitProcessInfo) { 96800b99b8Sopenharmony_ci return; 97800b99b8Sopenharmony_ci } 98800b99b8Sopenharmony_ci 99800b99b8Sopenharmony_ci const std::map<uint16_t, int32_t> unwMaps = { 100800b99b8Sopenharmony_ci { UnwindErrorCode::UNW_ERROR_STEP_ARK_FRAME, CrashExceptionCode::CRASH_UNWIND_EFRAME }, 101800b99b8Sopenharmony_ci { UnwindErrorCode::UNW_ERROR_INVALID_CONTEXT, CrashExceptionCode::CRASH_UNWIND_ECONTEXT }, 102800b99b8Sopenharmony_ci }; 103800b99b8Sopenharmony_ci int32_t errCode = 0; 104800b99b8Sopenharmony_ci auto iter = unwMaps.find(unwError); 105800b99b8Sopenharmony_ci if (iter == unwMaps.end()) { 106800b99b8Sopenharmony_ci return; 107800b99b8Sopenharmony_ci } 108800b99b8Sopenharmony_ci errCode = iter->second; 109800b99b8Sopenharmony_ci ReportCrashException(g_crashProcessName, g_crashProcessPid, g_crashProcessUid, errCode); 110800b99b8Sopenharmony_ci} 111800b99b8Sopenharmony_ci 112800b99b8Sopenharmony_cibool CheckFaultSummaryValid(const std::string &summary) 113800b99b8Sopenharmony_ci{ 114800b99b8Sopenharmony_ci return (summary.find("#00 pc") != std::string::npos) && (summary.find("#01 pc") != std::string::npos) && 115800b99b8Sopenharmony_ci (summary.find("#02 pc") != std::string::npos); 116800b99b8Sopenharmony_ci} 117800b99b8Sopenharmony_ci 118800b99b8Sopenharmony_ci} // namespace HiviewDFX 119800b99b8Sopenharmony_ci} // namesapce OHOS 120