• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 METADATA_JSON_PARSE_H
17 #define METADATA_JSON_PARSE_H
18 
19 #include "cJSON.h"
20 #include "ecmascript/dfx/hprof/rawheap_translate/utils.h"
21 
22 namespace rawheap_translate {
23 struct MetaData;
24 using JSType = uint8_t;
25 using NodeType = uint8_t;
26 using ObjRangeVisitor = std::function<void(std::shared_ptr<MetaData> &, uint32_t)>;
27 
28 struct Field {
29     std::string name;
30     uint32_t offset;
31     uint32_t size;
32 
FieldField33     Field() : offset(0), size(0) {}
34 };
35 
36 struct MetaData {
37     std::string name;
38     std::vector<std::shared_ptr<Field>> fields;
39     uint32_t endOffset;
40     std::vector<std::string> parents;
41     std::string visitType;
42 
MetaDataMetaData43     MetaData() : endOffset(0) {}
44 
IsArrayMetaData45     bool IsArray()
46     {
47         return visitType == "Array";
48     }
49 };
50 
51 struct Layout {
52     std::string name;
53     uint32_t keyIndex;
54     uint32_t valueIndex;
55     uint32_t detailIndex;
56     uint32_t entrySize;
57     uint32_t headerSize;
58 
LayoutLayout59     Layout() : keyIndex(0), valueIndex(0), detailIndex(0), entrySize(0), headerSize(0) {}
60 };
61 
62 class Meta {
63 public:
64     Meta() = default;
~Meta()65     ~Meta()
66     {
67         metadata_.clear();
68         enumsMapJSType_.clear();
69         enumsMapNodeType_.clear();
70         layout_.clear();
71         typeDesc_.clear();
72         enumsVec_.clear();
73     }
74 
75     bool Parse(const cJSON *object);
76     void VisitObjectBody(const std::string &name, const ObjRangeVisitor &visitor, uint32_t &baseOffset);
77     uint32_t GetNativateSize(char *obj, char *hclass);
78     NodeType GetNodeTypeFromHClass(char *hclass);
79     std::string GetTypeNameFromHClass(char *hclass);
80     std::shared_ptr<Layout> GetObjectLayout(const std::string &name);
81     bool IsString(char *hclass);
82     bool IsDictionaryMode(char *hclass);
83     bool IsJSObject(char *hclass);
84     bool IsGlobalEnv(char *hclass);
85 
86 private:
87     bool ParseTypeEnums(const cJSON *json);
88     bool ParseTypeList(const cJSON *json);
89     bool ParseTypeLayout(const cJSON *json);
90     bool ParseVersion(const cJSON *json);
91     bool SetObjTypeBitFieldOffset();
92     bool SetNativatePointerBindingSizeOffset();
93     std::shared_ptr<MetaData> GetMetaData(const std::string &name);
94     JSType GetObjTypeFromHClass(char *hclass);
95     JSType GetObjTypeFromTypeName(const std::string &name);
96     std::string GetTypeDesc(const std::string &name);
97 
98     static void IterateJSONArray(const cJSON *array, const std::function<void(const cJSON *, int)> &visitor);
99     static bool GetArray(const cJSON *json, const char *key, cJSON **value);
100     static bool GetString(const cJSON *json, const char *key, std::string &value);
101     static bool GetString(const cJSON *json, std::string &value);
102     static bool GetUInt32(const cJSON *json, const char *key, uint32_t &value);
103     static bool GetUInt32(const cJSON *json, uint32_t &value);
104 
105     static constexpr NodeType DEFAULT_NODETYPE = 8;
106 
107     std::unordered_map<std::string, std::shared_ptr<MetaData>> metadata_ {};
108     std::unordered_map<std::string, JSType> enumsMapJSType_ {};
109     std::unordered_map<std::string, uint8_t> enumsMapNodeType_ {};
110     std::unordered_map<std::string, std::shared_ptr<Layout>> layout_ {};
111     std::unordered_map<std::string, std::string> typeDesc_ {};
112     std::vector<std::string> enumsVec_ {};
113     uint32_t objTypeBitFieldOffset_ {0};
114     uint32_t objTypeBitFieldSize_ {0};
115     uint32_t nativatePointerBindingSizeOffset_ {0};
116     uint32_t nativatePointerBindingSize_ {0};
117 };
118 }  // namespace rawheap_translate
119 #endif  // METADATA_JSON_PARSE_H
120