Description:bbox send msg to historyLog and kmsg

Signed-off-by: EurusHomles-zH <zhouhang65@huawei.com>
This commit is contained in:
EurusHomles-zH 2024-09-20 16:53:43 +08:00 committed by hhl
parent ac01c69e92
commit 3e4b0b32c8
8 changed files with 339 additions and 3 deletions

View File

@ -47,6 +47,20 @@ PANIC:
SECOND_FRAME: {type: STRING, desc: second stack info}
LAST_FRAME: {type: STRING, desc: last stack info}
CUSTOM:
__BASE: {type: FAULT, level: CRITICAL, tag: STABILITY, desc: kernel panic}
MSG: {type: STRING, desc: panic event message}
MODULE: {type: STRING, desc: module name}
REASON: {type: STRING, desc: fault reason}
HAPPEN_TIME: {type: INT64, desc: fault trigger time}
LOG_PATH: {type: STRING, desc: fault log dir}
FINGERPRINT: {type: STRING, desc: unique id for grouping same fault}
FIRST_FRAME: {type: STRING, desc: first stack info}
SECOND_FRAME: {type: STRING, desc: second stack info}
LAST_FRAME: {type: STRING, desc: last stack info}
BBOX_TIME: {type: STRING, desc: blackbox hisysevent report timestamp info}
BBOX_SYSRESET: {type: STRING, desc: blackbox hisysevent report sysreset info}
BOOTFAIL:
__BASE: {type: FAULT, level: CRITICAL, tag: STABILITY, desc: device reboot fail}
MSG: {type: STRING, desc: bootFail event message}

View File

@ -29,6 +29,7 @@ ohos_source_set("bbox_detector") {
sources = [
"bbox_detector_plugin.cpp",
"hisysevent_util.cpp",
"panic_error_info_handle.cpp",
"panic_report_recovery.cpp",
]
@ -61,6 +62,7 @@ ohos_source_set("bbox_detector_with_cfi") {
sources = [
"bbox_detector_plugin.cpp",
"hisysevent_util.cpp",
"panic_error_info_handle.cpp",
"panic_report_recovery.cpp",
]

View File

@ -24,6 +24,7 @@
#include "hisysevent.h"
#include "hiview_logger.h"
#include "panic_report_recovery.h"
#include "panic_error_info_handle.h"
#include "plugin_factory.h"
#include "hisysevent_util.h"
#include "smart_parser.h"
@ -89,6 +90,12 @@ void BBoxDetectorPlugin::HandleBBoxEvent(std::shared_ptr<SysEvent> &sysEvent)
if (PanicReport::IsRecoveryPanicEvent(sysEvent)) {
return;
}
string eventName = sysEvent->GetEventName();
if (eventName == "CUSTOM") {
std::string bboxTime = sysEvent->GetEventValue("BBOX_TIME");
std::string bboxSysreset = sysEvent->GetEventValue("BBOX_SYSRESET");
PanicErrorInfoHandle::RKTransData(bboxTime, bboxSysreset);
}
std::string event = sysEvent->GetEventValue("REASON");
std::string module = sysEvent->GetEventValue("MODULE");
std::string timeStr = sysEvent->GetEventValue("SUB_LOG_PATH");
@ -263,7 +270,6 @@ void BBoxDetectorPlugin::NotifyBootCompleted()
void BBoxDetectorPlugin::InitPanicReporter()
{
if (!PanicReport::InitPanicReport()) {
HIVIEW_LOGE("Failed to init panic reporter");
return;
}
AddDetectBootCompletedTask();

View File

@ -1,5 +1,5 @@
{
"events":["PANIC", "HWWATCHDOG", "LPM3EXCEPTION", "BOOTLOADER_CRASH", "BOOTFAIL",
"events":["PANIC", "CUSTOM", "HWWATCHDOG", "LPM3EXCEPTION", "BOOTLOADER_CRASH", "BOOTFAIL",
"TRUSTZONE_REBOOTSYS", "CONNEXCEPTION", "SENSORHUBCRASH", "HIFICRASH",
"HARDWARE_FAULT", "MODEMCRASH", "AUDIO_CODEC_CRASH", "TRUSTZONECRASH",
"ISPCRASH", "IVPCRASH", "PRESS10S", "GENERAL_SEE_CRASH", "DSSCRASH",
@ -7,7 +7,7 @@
"domains": [
{
"domain": "KERNEL_VENDOR",
"include": ["PANIC", "HWWATCHDOG", "LPM3EXCEPTION", "BOOTLOADER_CRASH", "BOOTFAIL",
"include": ["PANIC", "CUSTOM", "HWWATCHDOG", "LPM3EXCEPTION", "BOOTLOADER_CRASH", "BOOTFAIL",
"TRUSTZONE_REBOOTSYS", "CONNEXCEPTION", "SENSORHUBCRASH", "HIFICRASH",
"HARDWARE_FAULT", "MODEMCRASH", "AUDIO_CODEC_CRASH", "TRUSTZONECRASH",
"ISPCRASH", "IVPCRASH", "PRESS10S", "GENERAL_SEE_CRASH", "DSSCRASH",

View File

@ -0,0 +1,227 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "panic_error_info_handle.h"
#include "securec.h"
#include "file_util.h"
#include "hiview_logger.h"
#include "parameters.h"
#include "string_util.h"
namespace OHOS {
namespace HiviewDFX {
namespace PanicErrorInfoHandle {
DEFINE_LOG_LABEL(0xD002D11, "PanicErrorInfoHandle");
using namespace std;
constexpr const char* HISTORY_LOG_PATH = "/data/log/bbox/history.log";
constexpr const char* SYS_FS_PSTORE_PATH = "/data/local/tmp/blackbox-ramoops-0";
/* fault category type */
constexpr const char* CATEGORY_SYSTEM_REBOOT = "SYSREBOOT";
constexpr const char* CATEGORY_SYSTEM_POWEROFF = "POWEROFF";
constexpr const char* CATEGORY_SYSTEM_PANIC = "PANIC";
constexpr const char* CATEGORY_SYSTEM_OOPS = "OOPS";
constexpr const char* CATEGORY_SYSTEM_CUSTOM = "CUSTOM";
constexpr const char* CATEGORY_SYSTEM_WATCHDOG = "HWWATCHDOG";
constexpr const char* CATEGORY_SYSTEM_HUNGTASK = "HUNGTASK";
constexpr const char* CATEGORY_SUBSYSTEM_CUSTOM = "CUSTOM";
/* top category type */
constexpr const char* TOP_CATEGORY_SYSTEM_RESET = "System Reset";
constexpr const char* TOP_CATEGORY_FREEZE = "System Freeze";
constexpr const char* TOP_CATEGORY_SYSTEM_POWEROFF = "POWEROFF";
constexpr const char* TOP_CATEGORY_SUBSYSTEM_CRASH = "Subsystem Crash";
/* module type */
constexpr const char* MODULE_SYSTEM = "SYSTEM";
/* fault event type */
constexpr const char* EVENT_SYSREBOOT = "SYSREBOOT";
constexpr const char* EVENT_LONGPRESS = "LONGPRESS";
constexpr const char* EVENT_COMBINATIONKEY = "COMBINATIONKEY";
constexpr const char* EVENT_SUBSYSREBOOT = "SUBSYSREBOOT";
constexpr const char* EVENT_POWEROFF = "POWEROFF";
constexpr const char* EVENT_PANIC = "PANIC";
constexpr const char* EVENT_OOPS = "OOPS";
constexpr const char* EVENT_SYS_WATCHDOG = "SYSWATCHDOG";
constexpr const char* EVENT_HUNGTASK = "HUNGTASK";
struct ErrorInfoToCategory {
const char *module;
struct {
const char *event;
const char *category;
const char *topCategory;
} map;
};
struct ErrorInfoToCategory g_errorInfoCategories[] = {
{
MODULE_SYSTEM,
{EVENT_SYSREBOOT, CATEGORY_SYSTEM_REBOOT, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_LONGPRESS, CATEGORY_SYSTEM_REBOOT, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_COMBINATIONKEY, CATEGORY_SYSTEM_REBOOT, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_SUBSYSREBOOT, CATEGORY_SYSTEM_REBOOT, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_POWEROFF, CATEGORY_SYSTEM_POWEROFF, TOP_CATEGORY_SYSTEM_POWEROFF}
},
{
MODULE_SYSTEM,
{EVENT_PANIC, CATEGORY_SYSTEM_PANIC, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_OOPS, CATEGORY_SYSTEM_OOPS, TOP_CATEGORY_SYSTEM_RESET}
},
{
MODULE_SYSTEM,
{EVENT_SYS_WATCHDOG, CATEGORY_SYSTEM_WATCHDOG, TOP_CATEGORY_FREEZE}
},
{
MODULE_SYSTEM,
{EVENT_HUNGTASK, CATEGORY_SYSTEM_HUNGTASK, TOP_CATEGORY_FREEZE}
},
};
void RKTransData(std::string bboxTime, std::string bboxSysreset)
{
ifstream fin(SYS_FS_PSTORE_PATH);
if (!fin.is_open()) {
HIVIEW_LOGE("Failed to open file: %{public}s, error=%{public}d", SYS_FS_PSTORE_PATH, errno);
return;
}
if (!FileUtil::FileExists(HISTORY_LOG_PATH)) {
HIVIEW_LOGE("The path of target file: %{public}s is not existed", HISTORY_LOG_PATH);
return;
}
ErrorInfo info = {};
fin.read(reinterpret_cast<char* >(&info), sizeof(ErrorInfo));
if (!fin) {
HIVIEW_LOGE("Read error_info failed");
return;
}
SaveHistoryLog(bboxTime, bboxSysreset, &info);
CopyPstoreFileToHistoryLog(fin);
}
void SaveHistoryLog(string bboxTime, string bboxSysreset, ErrorInfo* info)
{
ofstream fout;
fout.open(HISTORY_LOG_PATH, ios::out);
if (!fout.is_open()) {
HIVIEW_LOGE("Failed to open file: %{public}s, error=%{public}d", HISTORY_LOG_PATH, errno);
return;
}
fout << "[" << GetTopCategory(info->module, info->event) << "],";
fout << "module[" << info->module << "],";
fout << "category[" << GetCategory(info->module, info->event) << "],";
fout << "event[" << info->event << "],";
fout << "time[" << bboxTime << "],";
fout << "sysreboot[" << bboxSysreset << "],";
fout << "errdesc[" << info->errorDesc << "],";
fout << "logpath[/data/log/bbox]";
fout << endl;
HIVIEW_LOGE("GetTopCategory: %{public}s, module: %{public}s",
GetTopCategory(info->module, info->event), info->module);
HIVIEW_LOGE("category: %{public}s, event: %{public}s",
GetCategory(info->module, info->event), info->event);
HIVIEW_LOGE("time: %{public}s, sysreboot: %{public}s, errorDesc: %{public}s",
bboxTime.c_str(), bboxSysreset.c_str(), info->errorDesc);
}
void CopyPstoreFileToHistoryLog(ifstream &fin)
{
string targetPath = "/data/log/bbox/" + GetKmsgDate() + "history.log";
ofstream fout;
fout.open(targetPath, ios::out);
if (!fout.is_open()) {
HIVIEW_LOGE("Failed to open file: %{public}s error=%{public}d", targetPath.c_str(), errno);
return;
}
fout << fin.rdbuf();
fout << endl;
}
const char *GetTopCategory(const char *module, const char *event)
{
int i;
int count = sizeof(g_errorInfoCategories) / sizeof(ErrorInfoToCategory);
if ((!module || !event)) {
HIVIEW_LOGE("module: %{public}p, event: %{public}p\n", module, event);
return TOP_CATEGORY_SUBSYSTEM_CRASH;
}
for (i = 0; i < count; i++) {
if (!strcmp(g_errorInfoCategories[i].module, module) &&
!strcmp(g_errorInfoCategories[i].map.event, event)) {
return g_errorInfoCategories[i].map.topCategory;
}
}
if (!strcmp(module, MODULE_SYSTEM)) {
return TOP_CATEGORY_SYSTEM_RESET;
}
return TOP_CATEGORY_SUBSYSTEM_CRASH;
}
const char *GetCategory(const char *module, const char *event)
{
int i;
int count = sizeof(g_errorInfoCategories) / sizeof(ErrorInfoToCategory);
if ((!module || !event)) {
HIVIEW_LOGE("module: %{public}p, event: %{public}p\n", module, event);
return CATEGORY_SUBSYSTEM_CUSTOM;
}
for (i = 0; i < count; i++) {
if (!strcmp(g_errorInfoCategories[i].module, module) &&
!strcmp(g_errorInfoCategories[i].map.event, event)) {
return g_errorInfoCategories[i].map.category;
}
}
if (!strcmp(module, MODULE_SYSTEM)) {
return CATEGORY_SYSTEM_CUSTOM;
}
return CATEGORY_SUBSYSTEM_CUSTOM;
}
string GetKmsgDate()
{
time_t timeStamp = time(nullptr);
tm tm;
const int timeLength = 64;
char stampStr[timeLength] = {0};
if (localtime_r(&timeStamp, &tm) == nullptr ||
strftime(stampStr, timeLength, "%Y%m%d-%H%M%S", &tm) == 0) {
HIVIEW_LOGE("Failed to get real time");
return "ErrorTimeFormat";
}
string pathParent = string(stampStr);
return pathParent;
}
}
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PANIC_ERROR_INFO_HANDLE_H_
#define PANIC_ERROR_INFO_HANDLE_H_
#include <string>
#include <memory>
namespace OHOS {
namespace HiviewDFX {
namespace PanicErrorInfoHandle {
constexpr int EVENT_MAX_LEN = 32;
constexpr int CATEGORY_MAX_LEN = 32;
constexpr int MODULE_MAX_LEN = 32;
constexpr int TIMESTAMP_MAX_LEN = 24;
constexpr int ERROR_DESC_MAX_LEN = 512;
struct ErrorInfo {
char event[EVENT_MAX_LEN];
char category[CATEGORY_MAX_LEN];
char module[MODULE_MAX_LEN];
char errorTime[TIMESTAMP_MAX_LEN];
char errorDesc[ERROR_DESC_MAX_LEN];
};
/**
* Trans bbox data to history, trans pstore data to kmsg log when panic.
*/
void RKTransData(std::string bboxData, std::string bboxSysreset);
/**
* Save history log.
*/
void SaveHistoryLog(std::string bboxTime, std::string bboxSysreset, ErrorInfo* info);
/**
* Copy pstore file to history log.
*/
void CopyPstoreFileToHistoryLog(std::ifstream &fin);
/**
* Get Top category when painc.
*/
const char *GetTopCategory(const char *module, const char *event);
/**
* Get category when painc.
*/
const char *GetCategory(const char *module, const char *event);
/**
* Get kmsg date yyyymmdd-hhmmss when panic.
*/
std::string GetKmsgDate();
}
}
}
#endif // PANIC_ERROR_INFO_HANDLE_H_

View File

@ -32,6 +32,7 @@ config("bbox_detector_test_config") {
bbox_detector_test_source = [
"../bbox_detector_plugin.cpp",
"../panic_error_info_handle.cpp",
"../panic_report_recovery.cpp",
"mock/bbox_detectors_mock.cpp",
"mock/hisysevent_util_mock.cpp",

View File

@ -18,6 +18,7 @@
#include "bbox_detector_plugin.h"
#include "bbox_detectors_mock.h"
#include "panic_report_recovery.h"
#include "panic_error_info_handle.h"
#include "hisysevent_util_mock.h"
#include "sys_event.h"
#include "sys_event_dao.h"
@ -236,5 +237,17 @@ HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest007, TestSize.Level1)
std::this_thread::sleep_for(std::chrono::seconds(5));
ASSERT_TRUE(PanicReport::LoadBboxSaveFlagFromFile().isPanicUploaded);
}
/**
* @tc.name: BBoxDetectorUnitTest008
* @tc.desc: check the interface panicErrorInfoHandle RKTransData.
* @tc.type: FUNC
* @tc.require:
* @tc.author: liuwei
*/
HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest008, TestSize.Level1)
{
PanicErrorInfoHandle::RKTransData("bboxTime", "true");
}
} // namespace HiviewDFX
} // namespace OHOS