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 25namespace OHOS { 26namespace HiviewDFX { 27namespace { 28#undef LOG_DOMAIN 29#undef LOG_TAG 30#define LOG_DOMAIN 0xD002D11 31#define LOG_TAG "DfxFrameFormatter" 32} 33 34std::string DfxFrameFormatter::GetFrameStr(const DfxFrame& frame) 35{ 36 return GetFrameStr(std::make_shared<DfxFrame>(frame)); 37} 38 39std::string DfxFrameFormatter::GetFrameStr(const std::shared_ptr<DfxFrame>& frame) 40{ 41 if (frame == nullptr) { 42 return ""; 43 } 44 std::string data; 45 if (frame->isJsFrame) { 46#if defined(ENABLE_MIXSTACK) 47 if (frame->funcName.empty()) { 48 std::string mapName = frame->map == nullptr ? "" : frame->map->name; 49 data = StringPrintf("#%02zu at %s", frame->index, mapName.c_str()); 50 } else { 51 data = StringPrintf("#%02zu at %s (%s:%d:%d)", frame->index, frame->funcName.c_str(), 52 frame->mapName.c_str(), frame->line, frame->column); 53 } 54#endif 55 } else { 56 uint64_t pc = frame->relPc == 0 ? frame->pc : frame->relPc; 57#ifdef __LP64__ 58 data = StringPrintf("#%02zu pc %016" PRIx64, frame->index, pc); 59#else 60 data = StringPrintf("#%02zu pc %08" PRIx64, frame->index, pc); 61#endif 62 if (!frame->mapName.empty()) { 63 DfxMap::UnFormatMapName(frame->mapName); 64 data += " " + frame->mapName; 65 } else { 66 data += " [Unknown]"; 67 } 68 if (frame->funcName.length() > MAX_FUNC_NAME_LEN) { 69 DFXLOGD("length of funcName greater than 256 byte, do not display it"); 70 } else if (!frame->funcName.empty()) { 71 data += "(" + frame->funcName; 72 data += StringPrintf("+%" PRId64, frame->funcOffset); 73 data += ")"; 74 } 75 if (!frame->buildId.empty()) { 76 data += "(" + frame->buildId + ")"; 77 } 78 } 79 data += "\n"; 80 return data; 81} 82 83std::string DfxFrameFormatter::GetFramesStr(const std::vector<DfxFrame>& frames) 84{ 85 if (frames.size() == 0) { 86 return ""; 87 } 88 std::string ss; 89 for (const auto& f : frames) { 90 ss += GetFrameStr(f); 91 } 92 return ss; 93} 94 95std::string DfxFrameFormatter::GetFramesStr(const std::vector<std::shared_ptr<DfxFrame>>& frames) 96{ 97 if (frames.size() == 0) { 98 return ""; 99 } 100 std::string ss; 101 for (const auto& pf : frames) { 102 ss += GetFrameStr(pf); 103 } 104 return ss; 105} 106 107std::vector<std::shared_ptr<DfxFrame>> DfxFrameFormatter::ConvertFrames(const std::vector<DfxFrame>& frames) 108{ 109 std::vector<std::shared_ptr<DfxFrame>> pFrames; 110 for (const auto& frame : frames) { 111 pFrames.emplace_back(std::make_shared<DfxFrame>(frame)); 112 } 113 return pFrames; 114} 115} // namespace HiviewDFX 116} // namespace OHOS 117