From 5ee09f23d5844b5cc6845f72d8bc18967a143a21 Mon Sep 17 00:00:00 2001 From: MapleStory Date: Tue, 10 May 2022 21:48:05 +0800 Subject: [PATCH] add detailed log for debugging app unexpectedly exit issues Signed-off-by: MapleStory --- BUILD.gn | 5 ++++ adapter/sysevent/BUILD.gn | 23 +++++++++++++++ adapter/sysevent/event_reporter.cpp | 45 +++++++++++++++++++++++++++++ adapter/sysevent/event_reporter.h | 31 ++++++++++++++++++++ appspawn.gni | 1 + bundle.json | 4 +++ standard/appspawn_service.c | 25 ++++++++++++++++ startup_events.yaml | 21 ++++++++++++++ 8 files changed, 155 insertions(+) create mode 100644 adapter/sysevent/BUILD.gn create mode 100644 adapter/sysevent/event_reporter.cpp create mode 100644 adapter/sysevent/event_reporter.h create mode 100644 startup_events.yaml diff --git a/BUILD.gn b/BUILD.gn index 5c7e3bac..0bdd0d9d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -103,6 +103,11 @@ ohos_static_library("appspawn_server") { external_deps += [ "selinux:libhap_restorecon" ] } + if (appspawn_report_event) { + cflags = [ "-DREPORT_EVENT" ] + deps += [ "adapter/sysevent:event_reporter" ] + } + subsystem_name = "${subsystem_name}" part_name = "${part_name}" } diff --git a/adapter/sysevent/BUILD.gn b/adapter/sysevent/BUILD.gn new file mode 100644 index 00000000..c2d75079 --- /dev/null +++ b/adapter/sysevent/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2022 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/ohos.gni") +config("event_reporter_config") { + visibility = [ "*:*" ] + include_dirs = [ "." ] +} + +ohos_source_set("event_reporter") { + sources = [ "event_reporter.cpp" ] + public_configs = [ ":event_reporter_config" ] + external_deps = [ "hisysevent_native:libhisysevent" ] +} diff --git a/adapter/sysevent/event_reporter.cpp b/adapter/sysevent/event_reporter.cpp new file mode 100644 index 00000000..c00be6aa --- /dev/null +++ b/adapter/sysevent/event_reporter.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 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 "event_reporter.h" + +#include + +using namespace OHOS::HiviewDFX; +namespace OHOS { +namespace system { +constexpr char KEY_PROCESS_EXIT[] = "PROCESS_EXIT"; +constexpr char KEY_NAME[] = "PROCESS_NAME"; +constexpr char KEY_PID[] = "PID"; +constexpr char KEY_UID[] = "UID"; +constexpr char KEY_STATUS[] = "STATUS"; +constexpr int32_t MAX_NAME_LENGTH = 1024; +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status) +{ + std::string pname = "Unknown"; + if ((processName != NULL) && (strlen(processName) <= MAX_NAME_LENGTH)) { + pname = std::string(processName, strlen(processName)); + } + + HiSysEvent::Write(HiSysEvent::Domain::STARTUP, KEY_PROCESS_EXIT, HiSysEvent::EventType::BEHAVIOR, + KEY_NAME, pname, KEY_PID, pid, KEY_UID, uid, KEY_STATUS, status); +} +} // namespace system +} // namespace OHOS + +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status) +{ + OHOS::system::ReportProcessExitInfo(processName, pid, uid, status); +} diff --git a/adapter/sysevent/event_reporter.h b/adapter/sysevent/event_reporter.h new file mode 100644 index 00000000..f96bac1c --- /dev/null +++ b/adapter/sysevent/event_reporter.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 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 STARTUP_EVENT_REPORT_H +#define STARTUP_EVENT_REPORT_H +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif \ No newline at end of file diff --git a/appspawn.gni b/appspawn.gni index 24ada929..369b7fdf 100644 --- a/appspawn.gni +++ b/appspawn.gni @@ -19,4 +19,5 @@ module_output_path = "${part_name}/appspawn_l2" declare_args() { appspawn_support_nweb = true + appspawn_report_event = true } diff --git a/bundle.json b/bundle.json index 18ac474a..616ce543 100644 --- a/bundle.json +++ b/bundle.json @@ -22,9 +22,13 @@ ], "rom": "", "ram": "", + "hisysevent_config": [ + "//base/startup/appspawn_standard/startup_events.yaml" + ], "deps": { "components": [ "ability_base", + "hisysevent_native", "hiviewdfx_hilog_native", "ipc", "safwk", diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index de903307..dfd7f994 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -33,6 +33,10 @@ #include "parameter.h" #include "securec.h" +#ifdef REPORT_EVENT +#include "event_reporter.h" +#endif + static AppSpawnContentExt *g_appSpawnContent = NULL; static const int TV_SEC = 60; @@ -131,6 +135,24 @@ static int SendResponse(AppSpawnClientExt *client, const char *buff, size_t buff return LE_Send(LE_GetDefaultLoop(), client->stream, handle, buffSize); } +#ifdef REPORT_EVENT +static void PrintProcessExitInfo(pid_t pid, uid_t uid, int status) +{ + HashNode *node = HashMapGet(g_appSpawnContent->appMap, (const void *)&pid); + APPSPAWN_CHECK(node != NULL, return, "Handle SIGCHLD from pid:%d status:%d", pid, status); + AppInfo *appInfo = HASHMAP_ENTRY(node, AppInfo, node); + APPSPAWN_CHECK(appInfo != NULL, return, "Handle SIGCHLD from pid:%d status:%d", pid, status); + if (WIFSIGNALED(status)) { + APPSPAWN_LOGW("%s with pid %d exit with signal:%d", appInfo->name, pid, WTERMSIG(status)); + } + if (WIFEXITED(status)) { + APPSPAWN_LOGW("%s with pid %d exit with code:%d", appInfo->name, pid, WEXITSTATUS(status)); + } + + ReportProcessExitInfo(appInfo->name, pid, uid, status); +} +#endif + static void SignalHandler(const struct signalfd_siginfo *siginfo) { APPSPAWN_LOGI("SignalHandler signum %d", siginfo->ssi_signo); @@ -142,6 +164,9 @@ static void SignalHandler(const struct signalfd_siginfo *siginfo) int status; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { APPSPAWN_LOGI("SignalHandler pid %d status %d", pid, status); +#ifdef REPORT_EVENT + PrintProcessExitInfo(pid, siginfo->ssi_uid, status); +#endif RemoveAppInfo(pid); } #endif diff --git a/startup_events.yaml b/startup_events.yaml new file mode 100644 index 00000000..4bf18628 --- /dev/null +++ b/startup_events.yaml @@ -0,0 +1,21 @@ +# Copyright (c) 2022 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. + +domain: STARTUP + +PROCESS_EXIT: + __BASE: {type: BEHAVIOR, level: CRITICAL, tag: Stability, desc: process exit reason} + PROCESS_NAME: {type: STRING, desc: process name} + PID: {type: UINT32, desc: process id} + UID: {type: UINT32, desc: user id} + STATUS: {type: INT32, desc: exit code or signal number} -- Gitee