1 /*
2 * Copyright (c) 2025 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 "kernel_snapshot_manager.h"
17
18 #include <cstdio>
19 #include <fcntl.h>
20 #include <pthread.h>
21 #include <thread>
22 #include <unistd.h>
23
24 #include "parameters.h"
25
26 #include "dfx_log.h"
27 #include "dfx_util.h"
28
29 #include "kernel_snapshot_content_builder.h"
30 #include "kernel_snapshot_processor_impl.h"
31 #include "smart_fd.h"
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 namespace {
36 constexpr const char * const KERNEL_KBOX_SNAPSHOT = "/sys/kbox/snapshot_clear";
37 constexpr const char * const KERNEL_SNAPSHOT_INTERVAL = "kernel_snapshot_check_interval";
38 constexpr int BUFFER_LEN = 1024;
39 constexpr int DEFAULT_INTERVAL = 60;
40 constexpr int MIN_INTERVAL = 3;
41 }
42
GetSnapshotCheckInterval()43 int KernelSnapshotManager::GetSnapshotCheckInterval()
44 {
45 static int value = OHOS::system::GetIntParameter(KERNEL_SNAPSHOT_INTERVAL, DEFAULT_INTERVAL);
46 value = std::max(value, MIN_INTERVAL);
47 DFXLOGI("monitor crash kernel snapshot interval %{public}d", value);
48 return value;
49 }
50
ReadKernelSnapshot()51 std::string KernelSnapshotManager::ReadKernelSnapshot()
52 {
53 SmartFd snapshotFd(open(KERNEL_KBOX_SNAPSHOT, O_RDONLY));
54 if (!snapshotFd) {
55 DFXLOGE("open snapshot filed %{public}d", errno);
56 return "";
57 }
58
59 char buffer[BUFFER_LEN] = {0};
60 std::string snapshotCont;
61 ssize_t ret = 0;
62 do {
63 ret = read(snapshotFd.GetFd(), buffer, BUFFER_LEN - 1);
64 if (ret > 0) {
65 snapshotCont.append(buffer, static_cast<size_t>(ret));
66 }
67 if (ret < 0) {
68 DFXLOGE("read snapshot filed %{public}d", errno);
69 }
70 } while (ret > 0);
71 if (!snapshotCont.empty()) {
72 DFXLOGI("read snapshot begin with %{public}s", snapshotCont.substr(0, 25).c_str()); // 25 : only need 25
73 }
74 return snapshotCont;
75 }
76
MonitorCrashKernelSnapshot()77 void KernelSnapshotManager::MonitorCrashKernelSnapshot()
78 {
79 DFXLOGI("enter %{public}s ", __func__);
80 pthread_setname_np(pthread_self(), "KernelSnapshot");
81 int interval = GetSnapshotCheckInterval();
82
83 std::unique_ptr<IKernelSnapshotProcessor> processor(new KernelSnapshotProcessorImpl());
84
85 while (true) {
86 std::this_thread::sleep_for(std::chrono::seconds(interval));
87 if (access(KERNEL_KBOX_SNAPSHOT, F_OK) < 0) {
88 DFXLOGE("can't find %{public}s, just exit", KERNEL_KBOX_SNAPSHOT);
89 break;
90 }
91 std::string snapshotCont = ReadKernelSnapshot();
92 if (snapshotCont.empty()) {
93 continue;
94 }
95 processor->Process(snapshotCont);
96 }
97 }
98
StartMonitor()99 void KernelSnapshotManager::StartMonitor()
100 {
101 DFXLOGI("monitor kernel crash snapshot start!");
102 if (!IsBetaVersion()) {
103 DFXLOGW("monitor kernel crash snapshot func not support");
104 return;
105 }
106 std::thread catchThread = std::thread([] {
107 KernelSnapshotManager kernelSnapshotManager;
108 kernelSnapshotManager.MonitorCrashKernelSnapshot();
109 });
110 catchThread.detach();
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS
114