1 /*
2 * Copyright (c) 2021-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_frame_formatter.h"
17
18 #include <securec.h>
19
20 #include "dfx_define.h"
21 #include "dfx_log.h"
22 #include "dfx_maps.h"
23 #include "string_printf.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 #undef LOG_DOMAIN
29 #undef LOG_TAG
30 #define LOG_DOMAIN 0xD002D11
31 #define LOG_TAG "DfxFrameFormatter"
32 }
33
GetFrameStr(const DfxFrame & frame)34 std::string DfxFrameFormatter::GetFrameStr(const DfxFrame& frame)
35 {
36 std::string data;
37 if (frame.isJsFrame) {
38 #if defined(ENABLE_MIXSTACK)
39 data = StringPrintf("#%02zu at", frame.index);
40 if (!frame.funcName.empty()) {
41 data.append(" " + frame.funcName);
42 }
43 if (!frame.packageName.empty()) {
44 data.append(" " + frame.packageName);
45 }
46 if (!frame.mapName.empty()) {
47 data += StringPrintf(" (%s:%d:%d)", frame.mapName.c_str(), frame.line, frame.column);
48 } else {
49 std::string mapName = frame.map == nullptr ? "" : frame.map->name;
50 data.append(" " + mapName);
51 }
52 #endif
53 } else {
54 uint64_t pc = frame.relPc == 0 ? frame.pc : frame.relPc;
55 #ifdef __LP64__
56 data = StringPrintf("#%02zu pc %016" PRIx64, frame.index, pc);
57 #else
58 data = StringPrintf("#%02zu pc %08" PRIx64, frame.index, pc);
59 #endif
60 if (!frame.mapName.empty()) {
61 data += " " + DfxMap::UnFormatMapName(frame.mapName);
62 } else {
63 data += " [Unknown]";
64 }
65 if (frame.parseSymbolState.IsParseSymbolComplete() && !frame.funcName.empty() &&
66 frame.funcName.length() <= MAX_FUNC_NAME_LEN) {
67 data += "(" + frame.funcName;
68 data += StringPrintf("+%" PRId64, frame.funcOffset);
69 data += ")";
70 }
71 if (!frame.buildId.empty()) {
72 data += "(" + frame.buildId + ")";
73 }
74 }
75 data += "\n";
76 return data;
77 }
78
GetFrameStr(const std::shared_ptr<DfxFrame> & frame)79 std::string DfxFrameFormatter::GetFrameStr(const std::shared_ptr<DfxFrame>& frame)
80 {
81 return frame ? GetFrameStr(*frame) : "";
82 }
83
GetFramesStr(const std::vector<DfxFrame> & frames)84 std::string DfxFrameFormatter::GetFramesStr(const std::vector<DfxFrame>& frames)
85 {
86 std::string ss;
87 for (const auto& f : frames) {
88 ss += GetFrameStr(f);
89 }
90 return ss;
91 }
92 } // namespace HiviewDFX
93 } // namespace OHOS
94