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 
DfxFrameDfxFrame63     DfxFrame() {}
64     DfxFrame(uint64_t pc, uint64_t sp = 0) : pc(pc), sp(sp) {}
65     // only for UT
DfxFrameDfxFrame66     DfxFrame(uint64_t pc, uint64_t funcOffset, const char *mapName, const char *funcName)
67         : pc(pc), funcOffset(funcOffset), mapName(mapName), funcName(funcName) {}
68 
69     bool operator==(const DfxFrame &b) const
70     {
71         return (pc == b.pc) && (sp == b.sp);
72     }
73     bool operator!=(const DfxFrame &b) const
74     {
75         return (pc != b.pc) || (sp != b.sp);
76     }
77 
78 #ifndef is_ohos_lite
ToStringDfxFrame79     std::string ToString() const
80     {
81 #ifdef __LP64__
82         return StringPrintf("pc: 0x%016lx, sp: 0x%016lx", pc, sp);
83 #else
84         return StringPrintf("pc: 0x%08llx, sp: 0x%08llx", pc, sp);
85 #endif
86     }
ToSymbolStringDfxFrame87     std::string ToSymbolString() const
88     {
89         std::string output = StringPrintf("0x%016" PRIx64 " : ", pc);
90         output.append(funcName);
91         if (funcOffset != 0) {
92             output += StringPrintf("[0x%016" PRIx64 ":0x%016" PRIx64 "][+0x%" PRIx64 "]",
93                 pc - mapOffset, funcOffset, mapOffset);
94         }
95         output.append("@");
96         output.append(mapName);
97         output.append(":");
98         output.append(std::to_string(index));
99         return output;
100     }
101 #endif
102 };
103 } // namespace HiviewDFX
104 } // namespace OHOS
105 #endif
106