• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_logger.h"
22 #include "dfx_process.h"
23 #include "dfx_signal.h"
24 #include "dfx_thread.h"
25 #include "process_dumper.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 static const char NATIVE_CRASH_TYPE[] = "NativeCrash";
31 #if defined(ENABLE_MIXSTACK)
FillJsFrame(const DfxFrame & frame,Json::Value & jsonInfo)32 void FillJsFrame(const DfxFrame& frame, Json::Value& jsonInfo)
33 {
34     Json::Value frameJson;
35     frameJson["file"] = frame.mapName;
36     frameJson["symbol"] = frame.funcName;
37     frameJson["line"] = frame.line;
38     frameJson["column"] = frame.column;
39     jsonInfo.append(frameJson);
40 }
41 #endif
42 }
43 
GetStackInfo(bool isJsonDump,std::string & jsonStringInfo) const44 bool DfxStackInfoFormatter::GetStackInfo(bool isJsonDump, std::string& jsonStringInfo) const
45 {
46     bool result = false;
47 #ifndef is_ohos_lite
48     DFXLOG_DEBUG("GetStackInfo isJsonDump:%d", isJsonDump);
49     Json::Value jsonInfo;
50     if (!GetStackInfo(isJsonDump, jsonInfo)) {
51         return result;
52     }
53     jsonStringInfo.append(Json::FastWriter().write(jsonInfo));
54     result = true;
55 #endif
56     return result;
57 }
58 
59 #ifndef is_ohos_lite
GetStackInfo(bool isJsonDump,Json::Value & jsonInfo) const60 bool DfxStackInfoFormatter::GetStackInfo(bool isJsonDump, Json::Value& jsonInfo) const
61 {
62     if ((process_ == nullptr) || (request_ == nullptr)) {
63         DFXLOG_ERROR("GetStackInfo var is null");
64         return false;
65     }
66     if (isJsonDump) {
67         GetDumpInfo(jsonInfo);
68     } else {
69         GetNativeCrashInfo(jsonInfo);
70     }
71     return true;
72 }
73 
GetNativeCrashInfo(Json::Value & jsonInfo) const74 void DfxStackInfoFormatter::GetNativeCrashInfo(Json::Value& jsonInfo) const
75 {
76     jsonInfo["time"] = request_->timeStamp;
77     jsonInfo["uuid"] = "";
78     jsonInfo["crash_type"] = NATIVE_CRASH_TYPE;
79     jsonInfo["pid"] = process_->processInfo_.pid;
80     jsonInfo["uid"] = process_->processInfo_.uid;
81 
82     Json::Value signal;
83     signal["signo"] = request_->siginfo.si_signo;
84     signal["code"] = request_->siginfo.si_code;
85     Json::Value exception;
86     exception["signal"] = signal;
87     exception["message"] = process_->GetFatalMessage();
88     exception["thread_name"] = process_->keyThread_->threadInfo_.threadName;
89     exception["tid"] = process_->keyThread_->threadInfo_.tid;
90     Json::Value frames;
91     if (process_->vmThread_ != nullptr) {
92         FillFrames(process_->vmThread_, frames);
93     } else {
94         FillFrames(process_->keyThread_, frames);
95     }
96     exception["frames"] = frames;
97     jsonInfo["exception"] = exception;
98 
99     // fill other thread info
100     auto otherThreads = process_->GetOtherThreads();
101     if (otherThreads.size() > 0) {
102         Json::Value threadsJsonArray;
103         AppendThreads(otherThreads, threadsJsonArray);
104         jsonInfo["threads"] = threadsJsonArray;
105     }
106 }
107 
GetDumpInfo(Json::Value & jsonInfo) const108 void DfxStackInfoFormatter::GetDumpInfo(Json::Value& jsonInfo) const
109 {
110     Json::Value thread;
111     thread["thread_name"] = process_->keyThread_->threadInfo_.threadName;
112     thread["tid"] = process_->keyThread_->threadInfo_.tid;
113     Json::Value frames;
114     FillFrames(process_->keyThread_, frames);
115     thread["frames"] = frames;
116     jsonInfo.append(thread);
117 
118     // fill other thread info
119     auto otherThreads = process_->GetOtherThreads();
120     if (otherThreads.size() > 0) {
121         AppendThreads(otherThreads, jsonInfo);
122     }
123 }
124 
FillFrames(const std::shared_ptr<DfxThread> & thread,Json::Value & jsonInfo) const125 bool DfxStackInfoFormatter::FillFrames(const std::shared_ptr<DfxThread>& thread,
126                                        Json::Value& jsonInfo) const
127 {
128     if (thread == nullptr) {
129         DFXLOG_ERROR("FillFrames thread is null");
130         return false;
131     }
132     const auto& threadFrames = thread->GetFrames();
133     for (const auto& frame : threadFrames) {
134 #if defined(ENABLE_MIXSTACK)
135         if (frame.isJsFrame) {
136             FillJsFrame(frame, jsonInfo);
137             continue;
138         }
139 #endif
140         FillNativeFrame(frame, jsonInfo);
141     }
142     return true;
143 }
144 
FillNativeFrame(const DfxFrame & frame,Json::Value & jsonInfo) const145 void DfxStackInfoFormatter::FillNativeFrame(const DfxFrame& frame, Json::Value& jsonInfo) const
146 {
147     Json::Value frameJson;
148 #ifdef __LP64__
149     frameJson["pc"] = StringPrintf("%016lx", frame.relPc);
150 #else
151     frameJson["pc"] = StringPrintf("%08llx", frame.relPc);
152 #endif
153     frameJson["symbol"] = frame.funcName;
154     frameJson["offset"] = frame.funcOffset;
155     frameJson["file"] = frame.mapName;
156     frameJson["buildId"] = frame.buildId;
157     jsonInfo.append(frameJson);
158 }
159 
AppendThreads(const std::vector<std::shared_ptr<DfxThread>> & threads,Json::Value & jsonInfo) const160 void DfxStackInfoFormatter::AppendThreads(const std::vector<std::shared_ptr<DfxThread>>& threads,
161                                           Json::Value& jsonInfo) const
162 {
163     for (auto const& oneThread : threads) {
164         Json::Value threadJson;
165         threadJson["thread_name"] = oneThread->threadInfo_.threadName;
166         threadJson["tid"] = oneThread->threadInfo_.tid;
167         Json::Value frames;
168         FillFrames(oneThread, frames);
169         threadJson["frames"] = frames;
170         jsonInfo.append(threadJson);
171     }
172 }
173 #endif
174 } // namespace HiviewDFX
175 } // namespace OHOS
176