• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef DFX_FRAME_H
16 #define DFX_FRAME_H
17 
18 #include <cinttypes>
19 #include <memory>
20 #include <string>
21 #include "string_printf.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 class DfxMap;
26 
27 /**
28  * @brief Native Frame struct
29  * It serves as the public definition of the native stack frame.
30  */
31 struct DfxFrame {
32     /** whether is Js frame */
33     bool isJsFrame {false};
34     /** frame index */
35     size_t index {0};
36     /** symbol file index */
37     int32_t symbolFileIndex = -1;
38     /** program counter register value */
39     uint64_t pc {0};
40     /** relative program counter value */
41     uint64_t relPc {0};
42     /** stack pointer value */
43     uint64_t sp {0};
44     /** frame pointer value */
45     uint64_t fp {0};
46     /** map offset */
47     uint64_t mapOffset {0};
48     /** function byte offset */
49     uint64_t funcOffset {0};
50     /** elf file name */
51     std::string mapName {""};
52     /** function name */
53     std::string funcName {""};
54     /** elf file build id */
55     std::string buildId {""};
56     /** map cache */
57     std::shared_ptr<DfxMap> map = nullptr;
58     /** Js frame code line */
59     int32_t line {0};
60     /** Js frame code column */
61     int32_t column {0};
62     /** Js frame package name */
63     std::string packageName;
64 
DfxFrameDfxFrame65     DfxFrame() {}
pcDfxFrame66     DfxFrame(uint64_t pc, uint64_t sp = 0) : pc(pc), sp(sp) {}
67     // only for UT
DfxFrameDfxFrame68     DfxFrame(uint64_t pc, uint64_t funcOffset, const char *mapName, const char *funcName)
69         : pc(pc), funcOffset(funcOffset), mapName(mapName), funcName(funcName) {}
70 
71     bool operator==(const DfxFrame &b) const
72     {
73         return (pc == b.pc) && (sp == b.sp);
74     }
75     bool operator!=(const DfxFrame &b) const
76     {
77         return (pc != b.pc) || (sp != b.sp);
78     }
79 
80 #ifndef is_ohos_lite
ToStringDfxFrame81     std::string ToString() const
82     {
83 #ifdef __LP64__
84         return StringPrintf("pc: 0x%016lx, sp: 0x%016lx", pc, sp);
85 #else
86         return StringPrintf("pc: 0x%08llx, sp: 0x%08llx", pc, sp);
87 #endif
88     }
ToSymbolStringDfxFrame89     std::string ToSymbolString() const
90     {
91         std::string output = StringPrintf("0x%016" PRIx64 " : ", pc);
92         output.append(funcName);
93         if (funcOffset != 0) {
94             output += StringPrintf("[0x%016" PRIx64 ":0x%016" PRIx64 "][+0x%" PRIx64 "]",
95                 pc - mapOffset, funcOffset, mapOffset);
96         }
97         output.append("@");
98         output.append(mapName);
99         output.append(":");
100         output.append(std::to_string(index));
101         return output;
102     }
103 #endif
104 };
105 } // namespace HiviewDFX
106 } // namespace OHOS
107 #endif
108