• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_reporter.h"
17 
18 #include <dlfcn.h>
19 
20 #include "dfx_log.h"
21 #ifndef HISYSEVENT_DISABLE
22 #include "hisysevent.h"
23 #include "hisysevent_c.h"
24 #endif
25 
26 #include "kernel_snapshot_content_builder.h"
27 #include "dfx_util.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
ReportEvents(std::vector<CrashMap> & outputs,const std::string & snapshot)31 void KernelSnapshotReporter::ReportEvents(std::vector<CrashMap>& outputs, const std::string& snapshot)
32 {
33     for (auto& output : outputs) {
34         if (output[CrashSection::UID].empty()) {
35             DFXLOGE("msg format fail, report raw msg");
36             ReportRawMsg(snapshot);
37             return;
38         }
39         ReportCrashNoLogEvent(output);
40     }
41 }
42 
ReportCrashNoLogEvent(CrashMap & output)43 bool KernelSnapshotReporter::ReportCrashNoLogEvent(CrashMap& output)
44 {
45 #ifndef HISYSEVENT_DISABLE
46     int32_t uid = static_cast<int32_t>(strtol(output[CrashSection::UID].c_str(), nullptr, DECIMAL_BASE));
47     int32_t pid = static_cast<int32_t>(strtol(output[CrashSection::PID].c_str(), nullptr, DECIMAL_BASE));
48     int64_t timeStamp = strtoll(output[CrashSection::TIME_STAMP].c_str(), nullptr, DECIMAL_BASE);
49     if (errno == ERANGE) {
50         DFXLOGE("Failed to convert string to number, error: %{public}s", strerror(errno));
51         return false;
52     }
53 
54     int ret = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::RELIABILITY, "CPP_CRASH_NO_LOG",
55         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
56         "UID", uid,
57         "PID", pid,
58         "PROCESS_NAME", output[CrashSection::PROCESS_NAME],
59         "HAPPEN_TIME", timeStamp,
60         "SUMMARY", KernelSnapshotContentBuilder(output).GenerateSummary());
61     DFXLOGI("Report pid %{public}d kernel snapshot complate event ret %{public}d", pid, ret);
62     return ret == 0;
63 #else
64     DFXLOGI("Not supported for kernel snapshot report.");
65     return true;
66 #endif
67 }
68 
GetSnapshotPid(const std::string & content)69 int32_t KernelSnapshotReporter::GetSnapshotPid(const std::string& content)
70 {
71     std::string pidKey = "pid=";
72     auto pos = content.find(pidKey);
73     if (pos == std::string::npos) {
74         return 0;
75     }
76     auto end = content.find_first_of(')', pos);
77     if (end == std::string::npos) {
78         return 0;
79     }
80     pos += pidKey.size();
81     std::string pidStr = content.substr(pos, end - pos);
82     auto pid = static_cast<int32_t>(strtol(pidStr.c_str(), nullptr, 10));
83     if (errno == ERANGE) {
84         return 0;
85     }
86     return pid;
87 }
88 
ReportRawMsg(const std::string & content)89 bool KernelSnapshotReporter::ReportRawMsg(const std::string& content)
90 {
91     if (content.empty()) {
92         return false;
93     }
94 #ifndef HISYSEVENT_DISABLE
95     int32_t pid = GetSnapshotPid(content);
96     char procName[] = "encrypt_snapshot_proc";
97     HiSysEventParam params[] = {
98         {.name = "PID", .t = HISYSEVENT_UINT32, .v = { .ui32 = pid}, .arraySize = 0},
99         {.name = "PROCESS_NAME", .t = HISYSEVENT_STRING, .v = {.s = procName}, .arraySize = 0},
100         {.name = "HAPPEN_TIME", .t = HISYSEVENT_UINT64, .v = {.ui64 = GetTimeMilliSeconds()}, .arraySize = 0},
101     };
102     int ret = OH_HiSysEvent_Write("RELIABILITY", "CPP_CRASH_NO_LOG",
103                                   HISYSEVENT_FAULT, params, sizeof(params) / sizeof(params[0]));
104     DFXLOGI("Report pid %{public}d kernel snapshot raw event ret %{public}d", pid, ret);
105     return ret == 0;
106 #else
107     DFXLOGI("Not supported for kernel snapshot report.");
108     return true;
109 #endif
110 }
111 } // namespace HiviewDFX
112 } // namespace OHOS
113