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_content_builder.h"
17
18 #include "parameters.h"
19
20 #include "dfx_log.h"
21 #include "dfx_util.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 constexpr const char * const KERNEL_SNAPSHOT_REASON = "CppCrashKernelSnapshot";
27
FormatTimestamp(const std::string & timestamp)28 std::string FormatTimestamp(const std::string& timestamp)
29 {
30 uint64_t time = strtoul(timestamp.c_str(), nullptr, DECIMAL_BASE);
31 if (errno == ERANGE) {
32 DFXLOGE("Failed to convert timestamp to uint64_t");
33 time = 0;
34 }
35 return GetCurrentTimeStr(time);
36 }
37
GetBuildInfo()38 std::string GetBuildInfo()
39 {
40 static std::string buildInfo = OHOS::system::GetParameter("const.product.software.version", "Unknown");
41 return buildInfo;
42 }
43 } // namespace
44
IsEmptyContent() const45 bool KernelSnapshotContentBuilder::SnapshotUserSection::IsEmptyContent() const
46 {
47 return content.empty();
48 }
49
Length() const50 size_t KernelSnapshotContentBuilder::SnapshotUserSection::Length() const
51 {
52 if (IsEmptyContent()) {
53 return 0;
54 }
55 return title.length() + content.length() + suffix.length();
56 }
57
WriteTo(std::string & output) const58 void KernelSnapshotContentBuilder::SnapshotUserSection::WriteTo(std::string& output) const
59 {
60 if (IsEmptyContent()) {
61 return;
62 }
63 output += title;
64 output += content;
65 output += suffix;
66 }
67
GenerateSummary()68 std::string KernelSnapshotContentBuilder::GenerateSummary()
69 {
70 contentSections_ = BuildSections();
71 return ComposeSummary();
72 }
73
ComputeTotalLength()74 size_t KernelSnapshotContentBuilder::ComputeTotalLength()
75 {
76 size_t totalLength = 0;
77 for (const auto& section : contentSections_) {
78 if (!section.IsEmptyContent()) {
79 totalLength += section.Length();
80 }
81 }
82 return totalLength;
83 }
84
BuildSections()85 std::vector<KernelSnapshotContentBuilder::SnapshotUserSection> KernelSnapshotContentBuilder::BuildSections()
86 {
87 std::vector<KernelSnapshotContentBuilder::SnapshotUserSection> sections = {
88 {"Build info: ", GetBuildInfo(), "\n"},
89 {"Timestamp: ", FormatTimestamp(crashData_[CrashSection::TIME_STAMP]), ""},
90 {"Pid: ", crashData_[CrashSection::PID], "\n"},
91 {"Uid: ", crashData_[CrashSection::UID], "\n"},
92 {"Process name: ", crashData_[CrashSection::PROCESS_NAME], "\n"},
93 {"Reason: ", KERNEL_SNAPSHOT_REASON, "\n"},
94 {"Exception registers:\n", crashData_[CrashSection::EXCEPTION_REGISTERS], ""},
95 {"Fault thread info:\n", crashData_[CrashSection::FAULT_THREAD_INFO], ""},
96 {"Registers:\n", crashData_[CrashSection::CREGISTERS], ""}
97 };
98 if (isLocal_) {
99 sections.emplace_back("Memory near registers:\n", crashData_[CrashSection::MEMORY_NEAR_REGISTERS], "");
100 sections.emplace_back("FaultStack:\n", crashData_[CrashSection::FAULT_STACK], "");
101 }
102 sections.emplace_back("Elfs:\n", crashData_[CrashSection::MAPS], "");
103
104 return sections;
105 }
106
ComposeSummary()107 std::string KernelSnapshotContentBuilder::ComposeSummary()
108 {
109 const size_t totalLength = ComputeTotalLength();
110 if (totalLength == 0) {
111 return "";
112 }
113
114 std::string summary;
115 summary.reserve(totalLength);
116
117 for (const auto& section : contentSections_) {
118 section.WriteTo(summary);
119 }
120 return summary;
121 }
122 } // namespace HiviewDFX
123 } // namespace OHOS
124