• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <ostream>
18 #include <securec.h>
19 #include <sstream>
20 #include "dfx_log.h"
21 #include "dfx_define.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 const int FRAME_BUF_LEN = 1024;
26 
GetFrameStr(const DfxFrame & frame)27 std::string DfxFrameFormat::GetFrameStr(const DfxFrame& frame)
28 {
29     return GetFrameStr(std::make_shared<DfxFrame>(frame));
30 }
31 
GetFrameStr(const std::shared_ptr<DfxFrame> & frame)32 std::string DfxFrameFormat::GetFrameStr(const std::shared_ptr<DfxFrame>& frame)
33 {
34     char buf[FRAME_BUF_LEN] = {0};
35 #ifdef __LP64__
36     char format[] = "#%02zu pc %016" PRIx64 " %s";
37 #else
38     char format[] = "#%02zu pc %08" PRIx64 " %s";
39 #endif
40 
41     if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format,
42         frame->index,
43         frame->relPc,
44         frame->mapName.empty() ? "Unknown" : frame->mapName.c_str()) <= 0) {
45         DFXLOG_ERROR("%s :: snprintf_s failed, mapName: %s", __func__, frame->mapName.c_str());
46         return "[Unknown]";
47     }
48 
49     std::ostringstream ss;
50     ss << std::string(buf, strlen(buf));
51     if (!frame->funcName.empty()) {
52         ss << "(";
53         ss << frame->funcName.c_str();
54         ss << "+" << frame->funcOffset << ")";
55     }
56     if (!frame->buildId.empty()) {
57         ss << "(" << frame->buildId << ")";
58     }
59     ss << std::endl;
60     return ss.str();
61 }
62 
GetFramesStr(const std::vector<DfxFrame> & frames)63 std::string DfxFrameFormat::GetFramesStr(const std::vector<DfxFrame>& frames)
64 {
65     if (frames.size() == 0) {
66         return "";
67     }
68     std::ostringstream ss;
69     for (const auto& frame : frames) {
70         ss << GetFrameStr(frame);
71     }
72     return ss.str();
73 }
74 
GetFramesStr(const std::vector<std::shared_ptr<DfxFrame>> & frames)75 std::string DfxFrameFormat::GetFramesStr(const std::vector<std::shared_ptr<DfxFrame>>& frames)
76 {
77     if (frames.size() == 0) {
78         return "";
79     }
80     std::ostringstream ss;
81     for (const auto& frame : frames) {
82         ss << GetFrameStr(frame);
83     }
84     return ss.str();
85 }
86 
ConvertFrames(const std::vector<DfxFrame> & frames)87 std::vector<std::shared_ptr<DfxFrame>> DfxFrameFormat::ConvertFrames(const std::vector<DfxFrame>& frames)
88 {
89     std::vector<std::shared_ptr<DfxFrame>> ptrFrames;
90     for (const auto& frame : frames) {
91         ptrFrames.push_back(std::make_shared<DfxFrame>(frame));
92     }
93     return ptrFrames;
94 }
95 } // namespace HiviewDFX
96 } // namespace OHOS
97