1 /*
2 * Copyright (c) 2023-2024 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 "dfx_stack_info_formatter.h"
17
18 #include <cinttypes>
19 #include <string>
20
21 #include "dfx_define.h"
22 #include "dfx_logger.h"
23 #include "dfx_maps.h"
24 #include "dfx_process.h"
25 #include "dfx_signal.h"
26 #include "dfx_thread.h"
27 #include "process_dumper.h"
28 #if defined(__aarch64__)
29 #include "printer.h"
30 #endif
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace {
35 static const char NATIVE_CRASH_TYPE[] = "NativeCrash";
36
37 #ifndef is_ohos_lite
FillJsFrame(const DfxFrame& frame, Json::Value& jsonInfo)38 void FillJsFrame(const DfxFrame& frame, Json::Value& jsonInfo)
39 {
40 Json::Value frameJson;
41 frameJson["file"] = frame.mapName;
42 frameJson["symbol"] = frame.funcName;
43 frameJson["line"] = frame.line;
44 frameJson["column"] = frame.column;
45 jsonInfo.append(frameJson);
46 }
47 #endif
48 }
49
GetStackInfo(bool isJsonDump, std::string& jsonStringInfo) const50 bool DfxStackInfoFormatter::GetStackInfo(bool isJsonDump, std::string& jsonStringInfo) const
51 {
52 bool result = false;
53 #ifndef is_ohos_lite
54 DFXLOGD("GetStackInfo isJsonDump:%{public}d", isJsonDump);
55 Json::Value jsonInfo;
56 if (!GetStackInfo(isJsonDump, jsonInfo)) {
57 return result;
58 }
59 jsonStringInfo.append(Json::FastWriter().write(jsonInfo));
60 result = true;
61 #endif
62 return result;
63 }
64
65 #ifndef is_ohos_lite
GetStackInfo(bool isJsonDump, Json::Value& jsonInfo) const66 bool DfxStackInfoFormatter::GetStackInfo(bool isJsonDump, Json::Value& jsonInfo) const
67 {
68 if ((process_ == nullptr) || (request_ == nullptr)) {
69 DFXLOGE("GetStackInfo var is null");
70 return false;
71 }
72 if (isJsonDump) {
73 GetDumpInfo(jsonInfo);
74 } else {
75 GetNativeCrashInfo(jsonInfo);
76 }
77 return true;
78 }
79
GetNativeCrashInfo(Json::Value& jsonInfo) const80 void DfxStackInfoFormatter::GetNativeCrashInfo(Json::Value& jsonInfo) const
81 {
82 jsonInfo["time"] = request_->timeStamp;
83 jsonInfo["uuid"] = "";
84 jsonInfo["crash_type"] = NATIVE_CRASH_TYPE;
85 jsonInfo["pid"] = process_->processInfo_.pid;
86 jsonInfo["uid"] = process_->processInfo_.uid;
87 jsonInfo["app_running_unique_id"] = request_->appRunningId;
88
89 DfxSignal dfxSignal(request_->siginfo.si_signo);
90 Json::Value signal;
91 signal["signo"] = request_->siginfo.si_signo;
92 signal["code"] = request_->siginfo.si_code;
93 if (dfxSignal.IsAddrAvailable()) {
94 signal["address"] = StringPrintf("%" PRIX64_ADDR, reinterpret_cast<uint64_t>(request_->siginfo.si_addr));
95 } else {
96 signal["address"] = "";
97 }
98 Json::Value exception;
99 exception["signal"] = signal;
100 exception["message"] = process_->GetFatalMessage();
101 exception["thread_name"] = process_->keyThread_->threadInfo_.threadName;
102 exception["tid"] = process_->keyThread_->threadInfo_.tid;
103 Json::Value frames(Json::arrayValue);
104 if (process_->vmThread_ != nullptr) {
105 FillFrames(process_->vmThread_, frames);
106 } else {
107 FillFrames(process_->keyThread_, frames);
108 }
109 exception["frames"] = frames;
110 jsonInfo["exception"] = exception;
111
112 // fill other thread info
113 auto otherThreads = process_->GetOtherThreads();
114 if (otherThreads.size() > 0) {
115 Json::Value threadsJsonArray(Json::arrayValue);
116 AppendThreads(otherThreads, threadsJsonArray);
117 jsonInfo["threads"] = threadsJsonArray;
118 }
119 }
120
GetDumpInfo(Json::Value& jsonInfo) const121 void DfxStackInfoFormatter::GetDumpInfo(Json::Value& jsonInfo) const
122 {
123 Json::Value thread;
124 thread["thread_name"] = process_->keyThread_->threadInfo_.threadName;
125 thread["tid"] = process_->keyThread_->threadInfo_.tid;
126 Json::Value frames(Json::arrayValue);
127 FillFrames(process_->keyThread_, frames);
128 thread["frames"] = frames;
129 jsonInfo.append(thread);
130
131 // fill other thread info
132 auto otherThreads = process_->GetOtherThreads();
133 if (otherThreads.size() > 0) {
134 AppendThreads(otherThreads, jsonInfo);
135 }
136 }
137
FillFrames(const std::shared_ptr<DfxThread>& thread, Json::Value& jsonInfo) const138 bool DfxStackInfoFormatter::FillFrames(const std::shared_ptr<DfxThread>& thread,
139 Json::Value& jsonInfo) const
140 {
141 if (thread == nullptr) {
142 DFXLOGE("FillFrames thread is null");
143 return false;
144 }
145 const auto& threadFrames = thread->GetFrames();
146 for (const auto& frame : threadFrames) {
147 if (frame.isJsFrame) {
148 FillJsFrame(frame, jsonInfo);
149 continue;
150 }
151 FillNativeFrame(frame, jsonInfo);
152 #if defined(__aarch64__)
153 if (Printer::IsLastValidFrame(frame)) {
154 break;
155 }
156 #endif
157 }
158 return true;
159 }
160
FillNativeFrame(const DfxFrame& frame, Json::Value& jsonInfo) const161 void DfxStackInfoFormatter::FillNativeFrame(const DfxFrame& frame, Json::Value& jsonInfo) const
162 {
163 Json::Value frameJson;
164 #ifdef __LP64__
165 frameJson["pc"] = StringPrintf("%016lx", frame.relPc);
166 #else
167 frameJson["pc"] = StringPrintf("%08llx", frame.relPc);
168 #endif
169 if (frame.funcName.length() > MAX_FUNC_NAME_LEN) {
170 DFXLOGD("length of funcName greater than 256 byte, do not report it");
171 frameJson["symbol"] = "";
172 } else {
173 frameJson["symbol"] = frame.funcName;
174 }
175 frameJson["offset"] = frame.funcOffset;
176 std::string strippedMapName = frame.mapName;
177 DfxMap::UnFormatMapName(strippedMapName);
178 frameJson["file"] = strippedMapName;
179 frameJson["buildId"] = frame.buildId;
180 jsonInfo.append(frameJson);
181 }
182
AppendThreads(const std::vector<std::shared_ptr<DfxThread>>& threads, Json::Value& jsonInfo) const183 void DfxStackInfoFormatter::AppendThreads(const std::vector<std::shared_ptr<DfxThread>>& threads,
184 Json::Value& jsonInfo) const
185 {
186 for (auto const& oneThread : threads) {
187 Json::Value threadJson;
188 threadJson["thread_name"] = oneThread->threadInfo_.threadName;
189 threadJson["tid"] = oneThread->threadInfo_.tid;
190 Json::Value frames(Json::arrayValue);
191 FillFrames(oneThread, frames);
192 threadJson["frames"] = frames;
193 jsonInfo.append(threadJson);
194 }
195 }
196 #endif
197 } // namespace HiviewDFX
198 } // namespace OHOS
199