1 /*
2 * Copyright (c) 2021-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_frame_format.h"
17
18 #include <ostream>
19 #include <securec.h>
20 #include <sstream>
21
22 #include "dfx_log.h"
23 #include "dfx_define.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 const int FRAME_BUF_LEN = 1024;
28
GetFrameStr(const DfxFrame & frame)29 std::string DfxFrameFormat::GetFrameStr(const DfxFrame& frame)
30 {
31 return GetFrameStr(std::make_shared<DfxFrame>(frame));
32 }
33
GetFrameStr(const std::shared_ptr<DfxFrame> & frame)34 std::string DfxFrameFormat::GetFrameStr(const std::shared_ptr<DfxFrame>& frame)
35 {
36 char buf[FRAME_BUF_LEN] = {0};
37 #ifdef __LP64__
38 char format[] = "#%02zu pc %016" PRIx64 " %s";
39 #else
40 char format[] = "#%02zu pc %08" PRIx64 " %s";
41 #endif
42
43 if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format,
44 frame->index,
45 frame->relPc,
46 frame->mapName.empty() ? "Unknown" : frame->mapName.c_str()) <= 0) {
47 DFXLOG_ERROR("%s :: snprintf_s failed, mapName: %s", __func__, frame->mapName.c_str());
48 return "[Unknown]";
49 }
50
51 std::ostringstream ss;
52 ss << std::string(buf, strlen(buf));
53 if (!frame->funcName.empty()) {
54 ss << "(";
55 ss << frame->funcName.c_str();
56 ss << "+" << frame->funcOffset << ")";
57 }
58 if (!frame->buildId.empty()) {
59 ss << "(" << frame->buildId << ")";
60 }
61 ss << std::endl;
62 return ss.str();
63 }
64
GetFramesStr(const std::vector<DfxFrame> & frames)65 std::string DfxFrameFormat::GetFramesStr(const std::vector<DfxFrame>& frames)
66 {
67 if (frames.size() == 0) {
68 return "";
69 }
70 std::ostringstream ss;
71 for (const auto& frame : frames) {
72 ss << GetFrameStr(frame);
73 }
74 return ss.str();
75 }
76
GetFramesStr(const std::vector<std::shared_ptr<DfxFrame>> & frames)77 std::string DfxFrameFormat::GetFramesStr(const std::vector<std::shared_ptr<DfxFrame>>& frames)
78 {
79 if (frames.size() == 0) {
80 return "";
81 }
82 std::ostringstream ss;
83 for (const auto& frame : frames) {
84 ss << GetFrameStr(frame);
85 }
86 return ss.str();
87 }
88
ConvertFrames(const std::vector<DfxFrame> & frames)89 std::vector<std::shared_ptr<DfxFrame>> DfxFrameFormat::ConvertFrames(const std::vector<DfxFrame>& frames)
90 {
91 std::vector<std::shared_ptr<DfxFrame>> ptrFrames;
92 for (const auto& frame : frames) {
93 ptrFrames.push_back(std::make_shared<DfxFrame>(frame));
94 }
95 return ptrFrames;
96 }
97
98 #ifndef is_ohos_lite
GetFramesJson(const std::vector<DfxFrame> & frames)99 std::string DfxFrameFormat::GetFramesJson(const std::vector<DfxFrame>& frames)
100 {
101 char buf[FRAME_BUF_LEN] = {0};
102 #ifdef __LP64__
103 char format[] = "%016" PRIx64 "";
104 #else
105 char format[] = "%08" PRIx64 "";
106 #endif
107
108 Json::Value framesJson;
109 for (auto const& frame : frames) {
110 Json::Value frameJson;
111 if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frame.relPc) > 0) {
112 frameJson["pc"] = buf;
113 } else {
114 frameJson["pc"] = frame.relPc;
115 }
116 frameJson["symbol"] = frame.funcName;
117 frameJson["offset"] = frame.funcOffset;
118 frameJson["file"] = frame.mapName;
119 frameJson["buildId"] = frame.buildId;
120 framesJson.append(frameJson);
121 }
122 return Json::FastWriter().write(framesJson);
123 }
124 #endif
125 } // namespace HiviewDFX
126 } // namespace OHOS
127