1 /* 2 * Copyright (c) 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 16 #ifndef RAWHEAP_TRANSLATE_H 17 #define RAWHEAP_TRANSLATE_H 18 19 #include "ecmascript/dfx/hprof/rawheap_translate/common.h" 20 #include "ecmascript/dfx/hprof/rawheap_translate/metadata_parse.h" 21 #include "ecmascript/dfx/hprof/rawheap_translate/string_hashmap.h" 22 23 namespace panda::test { 24 class HeapDumpTestHelper; 25 }; 26 27 namespace rawheap_translate { 28 class RawHeap { 29 public: RawHeap()30 RawHeap() : strTable_(new StringHashMap()) 31 { 32 LOG_INFO_ << "start to translate rawheap!"; 33 } 34 35 virtual ~RawHeap(); 36 37 virtual bool Parse(FileReader &file, uint32_t rawheapFileSize) = 0; 38 virtual bool Translate() = 0; 39 40 static bool TranslateRawheap(const std::string &inputPath, const std::string &outputPath); 41 static bool ParseMetaData(FileReader &file, MetaParser *parser); 42 static RawHeap *ParseRawheap(FileReader &file, MetaParser *metaParser); 43 static std::string ReadVersion(FileReader &file); 44 45 std::vector<Node *>* GetNodes(); 46 std::vector<Edge *>* GetEdges(); 47 size_t GetNodeCount(); 48 size_t GetEdgeCount(); 49 StringHashMap* GetStringTable(); 50 std::string GetVersion(); 51 52 protected: 53 Node *CreateNode(); 54 void InsertEdge(Node *toNode, uint32_t indexOrStrId, EdgeType type); 55 StringId InsertAndGetStringId(const std::string &str); 56 void SetVersion(const std::string &version); 57 58 static bool ReadSectionInfo(FileReader &file, uint32_t offset, std::vector<uint32_t> §ion); 59 60 private: 61 StringHashMap *strTable_ {nullptr}; 62 std::vector<Node *> nodes_ {}; 63 std::vector<Edge *> edges_ {}; 64 std::string version_; 65 uint32_t nodeIndex_ {0}; 66 }; 67 68 class RawHeapTranslateV1 : public RawHeap { 69 public: RawHeapTranslateV1(MetaParser * meta)70 RawHeapTranslateV1(MetaParser *meta) : metaParser_(meta) {} 71 ~RawHeapTranslateV1(); 72 73 bool Parse(FileReader &file, uint32_t rawheapFileSize) override; 74 bool Translate() override; 75 76 private: 77 struct AddrTableItem { 78 uint64_t addr = 0; 79 uint64_t id = 0; 80 uint32_t objSize = 0; 81 uint32_t offset = 0; 82 }; 83 84 bool ReadRootTable(FileReader &file); 85 bool ReadStringTable(FileReader &file); 86 bool ReadObjectTable(FileReader &file, uint32_t offset, uint32_t totalSize); 87 bool ParseStringTable(FileReader &file); 88 void AddSyntheticRootNode(std::vector<uint64_t> &roots); 89 void SetNodeStringId(const std::vector<uint64_t> &objects, StringId strId); 90 Node* FindOrCreateNode(uint64_t addr); 91 Node* FindNode(uint64_t addr); 92 93 void FillNodes(Node *node, JSType type); 94 void BuildEdges(Node *node, JSType type); 95 void BuildGlobalEnvEdges(Node *node, JSType type); 96 void BuildArrayEdges(Node *node, JSType type); 97 void BuildFieldEdges(Node *node, JSType type); 98 void BuildJSObjectEdges(Node *node, JSType type); 99 void CreateEdge(Node *node, uint64_t addr, uint32_t nameOrIndex, EdgeType type); 100 void CreateHClassEdge(Node *node, Node *hclass); 101 EdgeType GenerateEdgeTypeAndRemoveWeak(Node *node, JSType type, uint64_t &addr); 102 103 static bool IsHeapObject(uint64_t addr); 104 static bool IsWeak(uint64_t addr); 105 static void RemoveWeak(uint64_t &addr); 106 static constexpr uint64_t TAG_WEAK = 0x01ULL; 107 static constexpr uint64_t TAG_WEAK_MASK = 0x01ULL; 108 static constexpr uint64_t TAG_HEAPOBJECT_MASK = (0xFFFFULL << 48) | 0x02ULL | 0x04ULL; // 48 means 6 byte shift 109 110 MetaParser *metaParser_ {nullptr}; 111 std::vector<char *> mem_ {}; 112 std::vector<uint32_t> sections_ {}; 113 std::unordered_map<uint64_t, Node *> nodesMap_ {}; 114 friend class panda::test::HeapDumpTestHelper; 115 }; 116 117 class RawHeapTranslateV2 : public RawHeap { 118 public: RawHeapTranslateV2(MetaParser * meta)119 RawHeapTranslateV2(MetaParser *meta) : metaParser_(meta) {} 120 ~RawHeapTranslateV2(); 121 122 bool Parse(FileReader &file, uint32_t rawheapFileSize) override; 123 bool Translate() override; 124 125 private: 126 struct AddrTableItemV2 { 127 uint32_t syntheticAddr; 128 uint32_t size; 129 uint64_t nodeId; 130 uint32_t nativeSize; 131 uint32_t type; 132 }; 133 134 bool ReadRootTable(FileReader &file); 135 bool ReadStringTable(FileReader &file); 136 bool ReadObjectTable(FileReader &file); 137 bool ParseStringTable(FileReader &file); 138 void AddSyntheticRootNode(std::vector<uint32_t> &roots); 139 Node* FindNode(uint32_t addr); 140 141 void FillNodes(); 142 void BuildEdges(Node *node); 143 void BuildArrayEdges(Node *node); 144 void BuildFieldEdges(Node *node, std::vector<Node *> &refs); 145 void BuildJSObjectEdges(Node *node, std::vector<Node *> &refs, uint32_t endOffset); 146 void CreateEdge(Node *node, Node *to, uint32_t nameOrIndex, EdgeType type); 147 Node* GetNextEdgeTo(); 148 EdgeType GenerateEdgeType(Node *node); 149 150 MetaParser *metaParser_ {nullptr}; 151 char *mem_ {}; 152 uint32_t memSize_ {0}; 153 uint32_t memPos_ {0}; 154 std::vector<uint32_t> sections_ {}; 155 std::unordered_map<uint32_t, Node *> nodesMap_ {}; 156 Node *syntheticRoot_ {nullptr}; 157 friend class panda::test::HeapDumpTestHelper; 158 }; 159 } // namespace rawheap_translate 160 #endif // RAWHEAP_TRANSLATE_H