diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000..24af5d7 --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,31 @@ +# Copyright (c) 2021 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. + +import("//build/lite/ndk/ndk.gni") + +static_library("blackbox") { + sources = [ + "blackbox_core.c", + "blackbox_detector.c", + "blackbox_adapter.c", + ] + defines = [] + cflags = [ "-Wall" ] + deps = [] + include_dirs = [ + "//base/hiviewdfx/blackbox", + "//base/hiviewdfx/blackbox/interfaces/native/kits", + "//base/hiviewdfx/hiview_lite", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + ] +} \ No newline at end of file diff --git a/blackbox_adapter.c b/blackbox_adapter.c new file mode 100644 index 0000000..f1e5841 --- /dev/null +++ b/blackbox_adapter.c @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2021 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. + */ + +/*-----------includes---------*/ +#include "blackbox.h" +#include "blackbox_adapter.h" +#include "blackbox_detector.h" +#include "ohos_init.h" + +/*----------local macroes-----------*/ +/*----------global variables-----------*/ +/*----------function definitions-----------*/ +WEAK void SystemModuleDump(const char *logDir, struct ErrorInfo *info) +{ + (void)logDir; + (void)info; + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); +} + +WEAK void SystemModuleReset(struct ErrorInfo *info) +{ + (void)info; + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); +} + +WEAK int SystemModuleGetLastLogInfo(struct ErrorInfo *info) +{ + (void)info; + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); + return -1; +} + +WEAK int SystemModuleSaveLastLog(const char *logDir, struct ErrorInfo *info) +{ + (void)logDir; + (void)info; + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); + return -1; +} + +WEAK int FullWriteFile(const char *filePath, const char *buf, + unsigned int bufSize, int isAppend) +{ + (void)filePath; + (void)buf; + (void)bufSize; + (void)isAppend; + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); + return -1; +} + +WEAK char *GetFaultLogPath(void) +{ + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); + return ""; +} + +WEAK void RebootSystem(void) +{ + BBOX_PRINT_ERR("Please implement the interface according to the platform!\n"); +} + +#ifdef BLACKBOX_TEST +static void BBoxTest(void) +{ + struct ModuleOps ops = { + .module = "MODULE_TEST", + .Dump = NULL, + .Reset = NULL, + .GetLastLogInfo = NULL, + .SaveLastLog = NULL, + }; + + if (BBoxRegisterModuleOps(&ops) != 0) { + BBOX_PRINT_ERR("BBoxRegisterModuleOps failed!\n"); + return; + } + BBoxNotifyError("EVENT_TEST1", "MODULE_TEST", "Test BBoxNotifyError1", 0); +} +#endif + +static void BBoxAdapterInit(void) +{ + struct ModuleOps ops = { + .module = MODULE_SYSTEM, + .Dump = SystemModuleDump, + .Reset = SystemModuleReset, + .GetLastLogInfo = SystemModuleGetLastLogInfo, + .SaveLastLog = SystemModuleSaveLastLog, + }; + + if (BBoxRegisterModuleOps(&ops) != 0) { + BBOX_PRINT_ERR("BBoxRegisterModuleOps failed!\n"); + return; + } +#ifdef BLACKBOX_TEST + BBoxTest(); +#endif +} +CORE_INIT_PRI(BBoxAdapterInit, 2); \ No newline at end of file diff --git a/blackbox_core.c b/blackbox_core.c new file mode 100644 index 0000000..1232c84 --- /dev/null +++ b/blackbox_core.c @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2021 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 "blackbox.h" +#include "blackbox_adapter.h" +#include "blackbox_detector.h" +#include "ohos_init.h" +#include "ohos_types.h" +#include "pthread.h" +#include "securec.h" +#include "utils_list.h" + +/******************local macroes*********************/ +#define LOG_ROOT_DIR_WAIT_TIME 1000 +#define LOG_ROOT_DIR_WAIT_COUNT 1 +#ifndef LOS_WAIT_FOREVER +#define LOS_WAIT_FOREVER 0xFFFFFFFF +#endif +#ifndef LOS_NO_WAIT +#define LOS_NO_WAIT 0 +#endif +#ifndef LOS_OK +#define LOS_OK 0 +#endif + +/******************local prototypes******************/ +struct BBoxOps { + UTILS_DL_LIST opsList; + struct ModuleOps ops; +}; + +/******************global functions*******************/ +/******************local variables*******************/ +static UTILS_DL_LIST_HEAD(g_opsList); +static unsigned int g_opsListSem; + +/******************function definitions*******************/ +static void GetDirName(char *dirBuf, unsigned int dirBufSize, const char *path) +{ + if (dirBuf == NULL || dirBufSize == 0 || path == NULL) { + BBOX_PRINT_ERR("dirBuf: %p, dirBufSize: %u, path: %p!\n", dirBuf, dirBufSize, path); + return; + } + + char *end = path + strlen(path); + while (*end != '/' && end >= path) { + end--; + } + if (end >= path) { + (void)memset_s(dirBuf, dirBufSize, 0, dirBufSize); + if (strncpy_s(dirBuf, dirBufSize - 1, path, end - path + strlen("/")) != EOK) { + BBOX_PRINT_ERR("strncpy_s failed or the dirBuf is not enough!\n"); + } + } else { + BBOX_PRINT_ERR("no / has been found!\n"); + } +} + +static void FormatErrorInfo(struct ErrorInfo *info, + const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char errorDesc[ERROR_DESC_MAX_LEN]) +{ + if (info == NULL || event == NULL || module == NULL || errorDesc == NULL) { + BBOX_PRINT_ERR("info: %p, event: %p, module: %p, errorDesc: %p\n", + info, event, module, errorDesc); + return; + } + + (void)memset_s(info, sizeof(*info), 0, sizeof(*info)); + if (strncpy_s(info->event, sizeof(info->event), event, + Min(strlen(event), sizeof(info->event) - 1)) != EOK) { + BBOX_PRINT_ERR("strncpy_s failed or the info->event is not enough!\n"); + } + if (strncpy_s(info->module, sizeof(info->module), module, + Min(strlen(module), sizeof(info->module) - 1)) != EOK) { + BBOX_PRINT_ERR("strncpy_s failed or the info->module is not enough!\n"); + } + if (strncpy_s(info->errorDesc, sizeof(info->errorDesc), errorDesc, + Min(strlen(errorDesc), sizeof(info->errorDesc) - 1)) != EOK) { + BBOX_PRINT_ERR("strncpy_s failed or the info->errorDesc is not enough!\n"); + } +} + +static void WaitForLogRootDir(const char *rootDir) +{ + int i = 0; + + if (rootDir == NULL) { + BBOX_PRINT_ERR("rootDir: %p\n", rootDir); + return; + } + BBOX_PRINT_INFO("wait for log root dir [%s] begin!\n", rootDir); + while (i++ < LOG_ROOT_DIR_WAIT_COUNT) { + LOS_Msleep(LOG_ROOT_DIR_WAIT_TIME); + } + BBOX_PRINT_INFO("wait for log root dir [%s] end!\n", rootDir); +} + +static void SaveBasicErrorInfo(const char *filePath, struct ErrorInfo *info) +{ + char *buf = NULL; + + if (filePath == NULL || info == NULL) { + BBOX_PRINT_ERR("filePath: %p, info: %p!\n", filePath, info); + return; + } + + buf = malloc(ERROR_INFO_MAX_LEN); + if (buf == NULL) { + BBOX_PRINT_ERR("malloc failed!\n"); + return; + } + (void)memset_s(buf, ERROR_INFO_MAX_LEN, 0, ERROR_INFO_MAX_LEN); + if (snprintf_s(buf, ERROR_INFO_MAX_LEN, ERROR_INFO_MAX_LEN - 1, + ERROR_INFO_HEADER ERROR_INFO_HEADER_FORMAT, + info->event, info->module, info->errorDesc) != -1) { + *(buf + ERROR_INFO_MAX_LEN - 1) = '\0'; + (void)FullWriteFile(filePath, buf, strlen(buf), 0); + } else { + PRINT_ERR("buf is not enough or snprintf_s failed\n"); + } + free(buf); + BBOX_PRINT_INFO("[%s] starts uploading event [%s]\n", + info->module, info->event); + (void)UploadEventByFile(filePath); + BBOX_PRINT_INFO("[%s] ends uploading event [%s]\n", + info->module, info->event); +} + +static void* SaveErrorLog(void *param) +{ + struct ErrorInfo *info = NULL; + struct BBoxOps *ops = NULL; + char dirName[PATH_MAX_LEN] = { 0 }; + + info = malloc(sizeof(*info)); + if (info == NULL) { + BBOX_PRINT_ERR("malloc failed!\n"); + return NULL; + } + + GetDirName(dirName, sizeof(dirName), GetFaultLogPath()); + WaitForLogRootDir(dirName); + if (LOS_SemPend(g_opsListSem, LOS_WAIT_FOREVER) != 0) { + BBOX_PRINT_ERR("Request g_opsListSem failed!\n"); + free(info); + return NULL; + } + UTILS_DL_LIST_FOR_EACH_ENTRY(ops, &g_opsList, struct BBoxOps, opsList) { + if (ops == NULL) { + continue; + } + if (ops->ops.GetLastLogInfo != NULL && ops->ops.SaveLastLog != NULL) { + (void)memset_s(info, sizeof(*info), 0, sizeof(*info)); + if (ops->ops.GetLastLogInfo(info) != 0) { + BBOX_PRINT_ERR("[%s] failed to get log info!\n", + ops->ops.module); + continue; + } + BBOX_PRINT_INFO("[%s] starts saving log!\n", ops->ops.module); + if (ops->ops.SaveLastLog(dirName, info) != 0) { + BBOX_PRINT_ERR("[%s] failed to save log!\n", ops->ops.module); + } else { + BBOX_PRINT_INFO("[%s] ends saving log!\n", ops->ops.module); + BBOX_PRINT_INFO("[%s] starts uploading event [%s]\n", info->module, info->event); + (void)UploadEventByFile(GetFaultLogPath()); + BBOX_PRINT_INFO("[%s] ends uploading event [%s]\n", info->module, info->event); + } + } + } + (void)LOS_SemPost(g_opsListSem); + free(info); + + return NULL; +} + +#ifdef BLACKBOX_DEBUG +static void PrintModuleOps(void) +{ + struct BBoxOps *temp = NULL; + + BBOX_PRINT_INFO("The following modules have been registered!\n"); + UTILS_DL_LIST_FOR_EACH_ENTRY(temp, &g_opsList, struct BBoxOps, opsList) { + BBOX_PRINT_INFO("module: %s, Dump: %p, Reset: %p, " + "GetLastLogInfo: %p, SaveLastLog: %p\n", + temp->ops.module, temp->ops.Dump, temp->ops.Reset, + temp->ops.GetLastLogInfo, temp->ops.SaveLastLog); + } +} +#endif + +int BBoxRegisterModuleOps(struct ModuleOps *ops) +{ + struct BBoxOps *newOps = NULL; + struct BBoxOps *temp = NULL; + + if (ops == NULL) { + BBOX_PRINT_ERR("ops: %p!\n", ops); + return -1; + } + + /* Use malloc to avoid the stack overflow */ + newOps = malloc(sizeof(*newOps)); + if (newOps == NULL) { + BBOX_PRINT_ERR("malloc failed!\n"); + return -1; + } + (void)memset_s(newOps, sizeof(*newOps), 0, sizeof(*newOps)); + (void)memcpy_s(&newOps->ops, sizeof(newOps->ops), ops, sizeof(*ops)); + if (LOS_SemPend(g_opsListSem, LOS_WAIT_FOREVER) != 0) { + BBOX_PRINT_ERR("Request g_opsListSem failed!\n"); + free(newOps); + return -1; + } + if (UtilsListEmpty(&g_opsList)) { + goto __out; + } + UTILS_DL_LIST_FOR_EACH_ENTRY(temp, &g_opsList, struct BBoxOps, opsList) { + if (strcmp(temp->ops.module, ops->module) == 0) { + BBOX_PRINT_ERR("[%s] has been registered!\n", ops->module); + (void)LOS_SemPost(g_opsListSem); + free(newOps); + return -1; + } + } + +__out: + BBOX_PRINT_INFO("[%s] is registered successfully!\n", ops->module); + UtilsListTailInsert(&g_opsList, &newOps->opsList); + (void)LOS_SemPost(g_opsListSem); +#ifdef BLACKBOX_DEBUG + PrintModuleOps(); +#endif + + return 0; +} + +int BBoxNotifyError(const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char errorDesc[ERROR_DESC_MAX_LEN], + int needSysReset) +{ + int findModule = 0; + struct BBoxOps *ops = NULL; + struct ErrorInfo *info = NULL; + char dirName[PATH_MAX_LEN] = { 0 }; + + info = malloc(sizeof(*info)); + if (info == NULL) { + BBOX_PRINT_ERR("malloc failed!\n"); + return -1; + } + + GetDirName(dirName, sizeof(dirName), GetFaultLogPath()); + if (needSysReset == 0) { + WaitForLogRootDir(dirName); + if (LOS_SemPend(g_opsListSem, LOS_NO_WAIT) != 0) { + BBOX_PRINT_ERR("Request g_opsListSem failed!\n"); + goto __out; + } + } + + UTILS_DL_LIST_FOR_EACH_ENTRY(ops, &g_opsList, struct BBoxOps, opsList) { + if (ops == NULL) { + BBOX_PRINT_ERR("ops: %p!\n", ops); + continue; + } + if (strcmp(ops->ops.module, module) != 0) { + continue; + } + FormatErrorInfo(info, event, module, errorDesc); + if (ops->ops.Dump == NULL && ops->ops.Reset == NULL) { + SaveBasicErrorInfo(GetFaultLogPath(), info); + break; + } + if (ops->ops.Dump != NULL) { + BBOX_PRINT_INFO("[%s] starts dumping data!\n", ops->ops.module); + ops->ops.Dump(dirName, info); + BBOX_PRINT_INFO("[%s] ends dumping data!\n", ops->ops.module); + } + if (ops->ops.Reset != NULL) { + BBOX_PRINT_INFO("[%s] starts resetting!\n", ops->ops.module); + ops->ops.Reset(info); + BBOX_PRINT_INFO("[%s] ends resetting!\n", ops->ops.module); + } + findModule = 1; + break; + } + if (needSysReset == 0) { + (void)LOS_SemPost(g_opsListSem); + } + +__out: + if (info != NULL) { + free(info); + } + if (needSysReset != 0 && findModule != 0) { + RebootSystem(); + } + + return 0; +} + +static void BBoxInit(void) +{ + int ret = -1; + pthread_t taskId = 0; + + if (LOS_BinarySemCreate(1, &g_opsListSem) != LOS_OK) { + BBOX_PRINT_ERR("Create binary semaphore failed!\n"); + return; + } + UtilsListInit(&g_opsList); + ret = pthread_create(&taskId, NULL, SaveErrorLog, NULL); + if (ret != 0) { + BBOX_PRINT_ERR("Falied to create SaveErrorLog task, ret: %d\n", ret); + } +} +CORE_INIT_PRI(BBoxInit, 1); \ No newline at end of file diff --git a/blackbox_detector.c b/blackbox_detector.c new file mode 100644 index 0000000..cf7bbae --- /dev/null +++ b/blackbox_detector.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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 "blackbox_detector.h" + +int UploadEventByFile(const char *filePath) +{ + if (filePath == NULL) { + BBOX_PRINT_ERR("filePath: %p\n", filePath); + return -1; + } + return 0; +} + +int UploadEventByStream(const char *buf, unsigned int bufSize) +{ + if (buf == NULL || bufSize == 0) { + BBOX_PRINT_ERR("buf: %p, bufSize: %u\n", buf, bufSize); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/blackbox_detector.h b/blackbox_detector.h new file mode 100644 index 0000000..4f6ce9b --- /dev/null +++ b/blackbox_detector.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 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 BLACKBOX_DETECTOR_H +#define BLACKBOX_DETECTOR_H + +#include "blackbox.h" + +int UploadEventByFile(const char *filePath); +int UploadEventByStream(const char *buf, unsigned int bufSize); + +#endif // BLACKBOX_DETECTOR_H \ No newline at end of file diff --git a/interfaces/native/innerkits/blackbox.h b/interfaces/native/innerkits/blackbox.h new file mode 100644 index 0000000..282534a --- /dev/null +++ b/interfaces/native/innerkits/blackbox.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 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 BLACKBOX_H +#define BLACKBOX_H + +#include "hiview_log.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#define ERROR_INFO_HEADER "#### error info ####\r\n" +#define ERROR_INFO_HEADER_FORMAT "event: %s\r\nmodule: %s\r\nerrorDesc: %s\r\n" +#define ERROR_INFO_MAX_LEN 768 +#define Min(a, b) (((a) > (b)) ? (b) : (a)) +#define BBOX_PRINT_ERR(format, ...) HILOG_DEBUG(HILOG_MODULE_HIVIEW, "bbox: func: %s line: %d, Err: " \ + format, __func__, __LINE__, ##__VA_ARGS__) +#define BBOX_PRINT_INFO(format, ...) HILOG_DEBUG(HILOG_MODULE_HIVIEW, "bbox: Info: " format, ##__VA_ARGS__) + +#define PATH_MAX_LEN 256 +#define EVENT_MAX_LEN 32 +#define MODULE_MAX_LEN 32 +#define ERROR_DESC_MAX_LEN 512 +#define MODULE_SYSTEM "SYSTEM" +#define EVENT_SYSREBOOT "SYSREBOOT" +#define EVENT_LONGPRESS "LONGPRESS" +#define EVENT_COMBINATIONKEY "COMBINATIONKEY" +#define EVENT_SUBSYSREBOOT "SUBSYSREBOOT" +#define EVENT_POWEROFF "POWEROFF" +#define EVENT_PANIC "PANIC" +#define EVENT_SYS_WATCHDOG "SYSWATCHDOG" +#define EVENT_HUNGTASK "HUNGTASK" +#define EVENT_BOOTFAIL "BOOTFAIL" + +struct ErrorInfo { + char event[EVENT_MAX_LEN]; + char module[MODULE_MAX_LEN]; + char errorDesc[ERROR_DESC_MAX_LEN]; +}; + +struct ModuleOps { + char module[MODULE_MAX_LEN]; + void (*Dump)(const char *logDir, struct ErrorInfo *info); + void (*Reset)(struct ErrorInfo *info); + int (*GetLastLogInfo)(struct ErrorInfo *info); + int (*SaveLastLog)(const char *logDir, struct ErrorInfo *info); +}; + +int BBoxRegisterModuleOps(struct ModuleOps *ops); +int BBoxNotifyError(const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char errorDesc[ERROR_DESC_MAX_LEN], + int needSysReset); +int BBoxDriverInit(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#endif /* End of #ifndef BLACKBOX_H */ \ No newline at end of file diff --git a/interfaces/native/innerkits/blackbox_adapter.h b/interfaces/native/innerkits/blackbox_adapter.h new file mode 100644 index 0000000..dc228cf --- /dev/null +++ b/interfaces/native/innerkits/blackbox_adapter.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 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 BLACKBOX_ADAPTER_H +#define BLACKBOX_ADAPTER_H + +#include +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#ifndef WEAK +#define WEAK __attribute__((weak)) +#endif + +/* The following functions need to be implemented by yourself */ +extern void SystemModuleDump(const char *logDir, struct ErrorInfo *info); +extern void SystemModuleReset(struct ErrorInfo *info); +extern int SystemModuleGetLastLogInfo(struct ErrorInfo *info); +extern int SystemModuleSaveLastLog(const char *logDir, struct ErrorInfo *info); +extern int FullWriteFile(const char *filePath, const char *buf, unsigned int bufSize, int isAppend); +extern char *GetFaultLogPath(void); +extern void RebootSystem(void); + +/* The following functions have been implemented in the kernel */ +extern unsigned int LOS_BinarySemCreate(unsigned short count, unsigned int *semHandle); +extern unsigned int LOS_SemPend(unsigned int semHandle, unsigned int timeout); +extern unsigned int LOS_SemPost(unsigned int semHandle); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#endif /* End of #ifndef BLACKBOX_ADAPTER_H */ \ No newline at end of file diff --git a/interfaces/native/kits/blackbox.h b/interfaces/native/kits/blackbox.h new file mode 100644 index 0000000..282534a --- /dev/null +++ b/interfaces/native/kits/blackbox.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 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 BLACKBOX_H +#define BLACKBOX_H + +#include "hiview_log.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#define ERROR_INFO_HEADER "#### error info ####\r\n" +#define ERROR_INFO_HEADER_FORMAT "event: %s\r\nmodule: %s\r\nerrorDesc: %s\r\n" +#define ERROR_INFO_MAX_LEN 768 +#define Min(a, b) (((a) > (b)) ? (b) : (a)) +#define BBOX_PRINT_ERR(format, ...) HILOG_DEBUG(HILOG_MODULE_HIVIEW, "bbox: func: %s line: %d, Err: " \ + format, __func__, __LINE__, ##__VA_ARGS__) +#define BBOX_PRINT_INFO(format, ...) HILOG_DEBUG(HILOG_MODULE_HIVIEW, "bbox: Info: " format, ##__VA_ARGS__) + +#define PATH_MAX_LEN 256 +#define EVENT_MAX_LEN 32 +#define MODULE_MAX_LEN 32 +#define ERROR_DESC_MAX_LEN 512 +#define MODULE_SYSTEM "SYSTEM" +#define EVENT_SYSREBOOT "SYSREBOOT" +#define EVENT_LONGPRESS "LONGPRESS" +#define EVENT_COMBINATIONKEY "COMBINATIONKEY" +#define EVENT_SUBSYSREBOOT "SUBSYSREBOOT" +#define EVENT_POWEROFF "POWEROFF" +#define EVENT_PANIC "PANIC" +#define EVENT_SYS_WATCHDOG "SYSWATCHDOG" +#define EVENT_HUNGTASK "HUNGTASK" +#define EVENT_BOOTFAIL "BOOTFAIL" + +struct ErrorInfo { + char event[EVENT_MAX_LEN]; + char module[MODULE_MAX_LEN]; + char errorDesc[ERROR_DESC_MAX_LEN]; +}; + +struct ModuleOps { + char module[MODULE_MAX_LEN]; + void (*Dump)(const char *logDir, struct ErrorInfo *info); + void (*Reset)(struct ErrorInfo *info); + int (*GetLastLogInfo)(struct ErrorInfo *info); + int (*SaveLastLog)(const char *logDir, struct ErrorInfo *info); +}; + +int BBoxRegisterModuleOps(struct ModuleOps *ops); +int BBoxNotifyError(const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char errorDesc[ERROR_DESC_MAX_LEN], + int needSysReset); +int BBoxDriverInit(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#endif /* End of #ifndef BLACKBOX_H */ \ No newline at end of file diff --git a/interfaces/native/kits/blackbox_adapter.h b/interfaces/native/kits/blackbox_adapter.h new file mode 100644 index 0000000..dc228cf --- /dev/null +++ b/interfaces/native/kits/blackbox_adapter.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 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 BLACKBOX_ADAPTER_H +#define BLACKBOX_ADAPTER_H + +#include +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#ifndef WEAK +#define WEAK __attribute__((weak)) +#endif + +/* The following functions need to be implemented by yourself */ +extern void SystemModuleDump(const char *logDir, struct ErrorInfo *info); +extern void SystemModuleReset(struct ErrorInfo *info); +extern int SystemModuleGetLastLogInfo(struct ErrorInfo *info); +extern int SystemModuleSaveLastLog(const char *logDir, struct ErrorInfo *info); +extern int FullWriteFile(const char *filePath, const char *buf, unsigned int bufSize, int isAppend); +extern char *GetFaultLogPath(void); +extern void RebootSystem(void); + +/* The following functions have been implemented in the kernel */ +extern unsigned int LOS_BinarySemCreate(unsigned short count, unsigned int *semHandle); +extern unsigned int LOS_SemPend(unsigned int semHandle, unsigned int timeout); +extern unsigned int LOS_SemPost(unsigned int semHandle); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* End of #if __cplusplus */ +#endif /* End of #ifdef __cplusplus */ + +#endif /* End of #ifndef BLACKBOX_ADAPTER_H */ \ No newline at end of file