1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef DFX_FRAME_H
16800b99b8Sopenharmony_ci#define DFX_FRAME_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cinttypes>
19800b99b8Sopenharmony_ci#include <memory>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include "string_printf.h"
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_cinamespace OHOS {
24800b99b8Sopenharmony_cinamespace HiviewDFX {
25800b99b8Sopenharmony_ciclass DfxMap;
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_ci/**
28800b99b8Sopenharmony_ci * @brief Native Frame struct
29800b99b8Sopenharmony_ci * It serves as the public definition of the native stack frame.
30800b99b8Sopenharmony_ci */
31800b99b8Sopenharmony_cistruct DfxFrame {
32800b99b8Sopenharmony_ci    /** whether is Js frame */
33800b99b8Sopenharmony_ci    bool isJsFrame {false};
34800b99b8Sopenharmony_ci    /** frame index */
35800b99b8Sopenharmony_ci    size_t index {0};
36800b99b8Sopenharmony_ci    /** symbol file index */
37800b99b8Sopenharmony_ci    int32_t symbolFileIndex = -1;
38800b99b8Sopenharmony_ci    /** program counter register value */
39800b99b8Sopenharmony_ci    uint64_t pc {0};
40800b99b8Sopenharmony_ci    /** relative program counter value */
41800b99b8Sopenharmony_ci    uint64_t relPc {0};
42800b99b8Sopenharmony_ci    /** stack pointer value */
43800b99b8Sopenharmony_ci    uint64_t sp {0};
44800b99b8Sopenharmony_ci    /** frame pointer value */
45800b99b8Sopenharmony_ci    uint64_t fp {0};
46800b99b8Sopenharmony_ci    /** map offset */
47800b99b8Sopenharmony_ci    uint64_t mapOffset {0};
48800b99b8Sopenharmony_ci    /** function byte offset */
49800b99b8Sopenharmony_ci    uint64_t funcOffset {0};
50800b99b8Sopenharmony_ci    /** elf file name */
51800b99b8Sopenharmony_ci    std::string mapName {""};
52800b99b8Sopenharmony_ci    /** function name */
53800b99b8Sopenharmony_ci    std::string funcName {""};
54800b99b8Sopenharmony_ci    /** elf file build id */
55800b99b8Sopenharmony_ci    std::string buildId {""};
56800b99b8Sopenharmony_ci    /** map cache */
57800b99b8Sopenharmony_ci    std::shared_ptr<DfxMap> map = nullptr;
58800b99b8Sopenharmony_ci    /** Js frame code line */
59800b99b8Sopenharmony_ci    int32_t line {0};
60800b99b8Sopenharmony_ci    /** Js frame code column */
61800b99b8Sopenharmony_ci    int32_t column {0};
62800b99b8Sopenharmony_ci
63800b99b8Sopenharmony_ci    DfxFrame() {}
64800b99b8Sopenharmony_ci    DfxFrame(uint64_t pc, uint64_t sp = 0) : pc(pc), sp(sp) {}
65800b99b8Sopenharmony_ci    // only for UT
66800b99b8Sopenharmony_ci    DfxFrame(uint64_t pc, uint64_t funcOffset, const char *mapName, const char *funcName)
67800b99b8Sopenharmony_ci        : pc(pc), funcOffset(funcOffset), mapName(mapName), funcName(funcName) {}
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ci    bool operator==(const DfxFrame &b) const
70800b99b8Sopenharmony_ci    {
71800b99b8Sopenharmony_ci        return (pc == b.pc) && (sp == b.sp);
72800b99b8Sopenharmony_ci    }
73800b99b8Sopenharmony_ci    bool operator!=(const DfxFrame &b) const
74800b99b8Sopenharmony_ci    {
75800b99b8Sopenharmony_ci        return (pc != b.pc) || (sp != b.sp);
76800b99b8Sopenharmony_ci    }
77800b99b8Sopenharmony_ci
78800b99b8Sopenharmony_ci#ifndef is_ohos_lite
79800b99b8Sopenharmony_ci    std::string ToString() const
80800b99b8Sopenharmony_ci    {
81800b99b8Sopenharmony_ci#ifdef __LP64__
82800b99b8Sopenharmony_ci        return StringPrintf("pc: 0x%016lx, sp: 0x%016lx", pc, sp);
83800b99b8Sopenharmony_ci#else
84800b99b8Sopenharmony_ci        return StringPrintf("pc: 0x%08llx, sp: 0x%08llx", pc, sp);
85800b99b8Sopenharmony_ci#endif
86800b99b8Sopenharmony_ci    }
87800b99b8Sopenharmony_ci    std::string ToSymbolString() const
88800b99b8Sopenharmony_ci    {
89800b99b8Sopenharmony_ci        std::string output = StringPrintf("0x%016" PRIx64 " : ", pc);
90800b99b8Sopenharmony_ci        output.append(funcName);
91800b99b8Sopenharmony_ci        if (funcOffset != 0) {
92800b99b8Sopenharmony_ci            output += StringPrintf("[0x%016" PRIx64 ":0x%016" PRIx64 "][+0x%" PRIx64 "]",
93800b99b8Sopenharmony_ci                pc - mapOffset, funcOffset, mapOffset);
94800b99b8Sopenharmony_ci        }
95800b99b8Sopenharmony_ci        output.append("@");
96800b99b8Sopenharmony_ci        output.append(mapName);
97800b99b8Sopenharmony_ci        output.append(":");
98800b99b8Sopenharmony_ci        output.append(std::to_string(index));
99800b99b8Sopenharmony_ci        return output;
100800b99b8Sopenharmony_ci    }
101800b99b8Sopenharmony_ci#endif
102800b99b8Sopenharmony_ci};
103800b99b8Sopenharmony_ci} // namespace HiviewDFX
104800b99b8Sopenharmony_ci} // namespace OHOS
105800b99b8Sopenharmony_ci#endif
106