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_json_formatter.h"
17
18 #include <cstdlib>
19 #include <iostream>
20 #include <ostream>
21 #include <regex>
22 #include <securec.h>
23 #include <sstream>
24
25 #include "dfx_define.h"
26 #include "dfx_frame.h"
27 #include "dfx_frame_format.h"
28 #include "dfx_log.h"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 #ifndef is_ohos_lite
33 namespace {
34 const int FRAME_BUF_LEN = 1024;
FormatJsFrame(const Json::Value & frames,const uint32_t & frameIdx,std::string & outStr)35 static bool FormatJsFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr)
36 {
37 const int jsIdxLen = 10;
38 char buf[jsIdxLen] = { 0 };
39 char idxFmt[] = "#%02u at ";
40 if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, idxFmt, frameIdx) <= 0) {
41 return false;
42 }
43 outStr = std::string(buf, strlen(buf));
44 std::string symbol = frames[frameIdx]["symbol"].asString();
45 std::string file = frames[frameIdx]["file"].asString();
46 std::string line = frames[frameIdx]["line"].asString();
47 std::string column = frames[frameIdx]["column"].asString();
48 outStr.append(symbol + " (" + file + ":" + line + ":" + column + ")");
49 return true;
50 }
51
FormatNativeFrame(const Json::Value & frames,const uint32_t & frameIdx,std::string & outStr)52 static bool FormatNativeFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr)
53 {
54 char buf[FRAME_BUF_LEN] = {0};
55 char format[] = "#%02u pc %s %s";
56 std::string buildId = frames[frameIdx]["buildId"].asString();
57 std::string file = frames[frameIdx]["file"].asString();
58 std::string offset = frames[frameIdx]["offset"].asString();
59 std::string pc = frames[frameIdx]["pc"].asString();
60 std::string symbol = frames[frameIdx]["symbol"].asString();
61 if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frameIdx, pc.c_str(),
62 file.empty() ? "Unknown" : file.c_str()) <= 0) {
63 return false;
64 }
65 outStr = std::string(buf, strlen(buf));
66 if (!symbol.empty()) {
67 outStr.append("(" + symbol + "+" + offset + ")");
68 }
69 if (!buildId.empty()) {
70 outStr.append("(" + buildId + ")");
71 }
72 return true;
73 }
74 }
75
FormatJsonStack(std::string jsonStack,std::string & outStackStr)76 bool DfxJsonFormatter::FormatJsonStack(std::string jsonStack, std::string& outStackStr)
77 {
78 Json::Reader reader;
79 Json::Value threads;
80 if (!(reader.parse(jsonStack, threads))) {
81 outStackStr.append("Failed to parse json stack info.");
82 return false;
83 }
84
85 for (uint32_t i = 0; i < threads.size(); ++i) {
86 std::ostringstream ss;
87 Json::Value thread = threads[i];
88 ss << "Tid:" << thread["tid"].asString() << ", Name:" << thread["thread_name"].asString() << std::endl;
89 const Json::Value frames = thread["frames"];
90 for (uint32_t j = 0; j < frames.size(); ++j) {
91 std::string frameStr = "";
92 bool formatStatus = false;
93 if (frames[j]["line"].asString().empty()) {
94 formatStatus = FormatNativeFrame(frames, j, frameStr);
95 } else {
96 formatStatus = FormatJsFrame(frames, j, frameStr);
97 }
98 if (formatStatus) {
99 ss << frameStr << std::endl;
100 } else {
101 // Shall we try to print more information?
102 outStackStr.append("Frame info is illegal.");
103 return false;
104 }
105 }
106 outStackStr.append(ss.str());
107 }
108 return true;
109 }
110 #endif
111 } // namespace HiviewDFX
112 } // namespace OHOS
113