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 <atomic> 19 #include <cinttypes> 20 #include <memory> 21 #include <string> 22 #include "string_printf.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 class DfxMap; 27 struct ParseSymbolState { 28 ParseSymbolState() = default; ParseSymbolStateParseSymbolState29 ParseSymbolState(const ParseSymbolState &parseSymbolState) 30 { 31 if (parseSymbolState.isParseSymbolComplete) { 32 isParseSymbolComplete.store(true); 33 } else { 34 isParseSymbolComplete.store(false); 35 } 36 } 37 ParseSymbolState& operator=(const ParseSymbolState& parseSymbolState) 38 { 39 if (parseSymbolState.isParseSymbolComplete) { 40 isParseSymbolComplete.store(true); 41 } else { 42 isParseSymbolComplete.store(false); 43 } 44 return *this; 45 } IsParseSymbolCompleteParseSymbolState46 bool IsParseSymbolComplete() const 47 { 48 return isParseSymbolComplete.load(); 49 } SetParseSymbolStateParseSymbolState50 void SetParseSymbolState(bool state) 51 { 52 isParseSymbolComplete.store(state); 53 } 54 private: 55 std::atomic<bool> isParseSymbolComplete {false}; 56 }; 57 /** 58 * @brief Native Frame struct 59 * It serves as the public definition of the native stack frame. 60 */ 61 struct DfxFrame { 62 /** whether is Js frame */ 63 bool isJsFrame {false}; 64 /** frame index */ 65 size_t index {0}; 66 /** symbol file index */ 67 int32_t symbolFileIndex = -1; 68 /** program counter register value */ 69 uint64_t pc {0}; 70 /** relative program counter value */ 71 uint64_t relPc {0}; 72 /** stack pointer value */ 73 uint64_t sp {0}; 74 /** frame pointer value */ 75 uint64_t fp {0}; 76 /** map offset */ 77 uint64_t mapOffset {0}; 78 /** function byte offset */ 79 uint64_t funcOffset {0}; 80 /** elf file name */ 81 std::string mapName {""}; 82 /** function name */ 83 std::string funcName {""}; 84 /** elf file build id */ 85 std::string buildId {""}; 86 /** map cache */ 87 std::shared_ptr<DfxMap> map = nullptr; 88 /** Js frame code line */ 89 int32_t line {0}; 90 /** Js frame code column */ 91 int32_t column {0}; 92 /** Js frame package name */ 93 std::string packageName; 94 /** state of parse symbol */ 95 ParseSymbolState parseSymbolState; 96 DfxFrameDfxFrame97 DfxFrame() {} pcDfxFrame98 DfxFrame(uint64_t pc, uint64_t sp = 0) : pc(pc), sp(sp) {} 99 // only for UT DfxFrameDfxFrame100 DfxFrame(uint64_t pc, uint64_t funcOffset, const char *mapName, const char *funcName) 101 : pc(pc), funcOffset(funcOffset), mapName(mapName), funcName(funcName) {} 102 103 bool operator==(const DfxFrame &b) const 104 { 105 return (pc == b.pc) && (sp == b.sp); 106 } 107 bool operator!=(const DfxFrame &b) const 108 { 109 return (pc != b.pc) || (sp != b.sp); 110 } 111 112 #ifndef is_ohos_lite ToStringDfxFrame113 std::string ToString() const 114 { 115 #ifdef __LP64__ 116 return StringPrintf("pc: 0x%016lx, sp: 0x%016lx", pc, sp); 117 #else 118 return StringPrintf("pc: 0x%08llx, sp: 0x%08llx", pc, sp); 119 #endif 120 } ToSymbolStringDfxFrame121 std::string ToSymbolString() const 122 { 123 std::string output = StringPrintf("0x%016" PRIx64 " : ", pc); 124 output.append(funcName); 125 if (funcOffset != 0) { 126 output += StringPrintf("[0x%016" PRIx64 ":0x%016" PRIx64 "][+0x%" PRIx64 "]", 127 pc - mapOffset, funcOffset, mapOffset); 128 } 129 output.append("@"); 130 output.append(mapName); 131 output.append(":"); 132 output.append(std::to_string(index)); 133 return output; 134 } 135 #endif 136 }; 137 } // namespace HiviewDFX 138 } // namespace OHOS 139 #endif 140