• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 FREEZE_JSON_UTIL_H
17 #define FREEZE_JSON_UTIL_H
18 
19 #include <list>
20 #include <map>
21 #include <sstream>
22 #include <string>
23 
24 #include "file_util.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace FreezeJsonUtil {
29 
30 const std::string LOGGER_FREEZEJSON_LOG_PATH = "/data/log/freezejson";
31 const std::string COMMON_QUOTE = "\"";
32 const std::string COMMON_COMMA = ", ";
33 const std::string COMMON_COLON = " : ";
34 const std::string COMMON_LEFT_BRACE = "{";
35 const std::string COMMON_RIGHT_BRACE = "}";
36 const std::string COMMON_LEFT_SQUARE_BRACKET = "[";
37 const std::string COMMON_RIGHT_SQUARE_BREAKET = "]";
38 const std::string COMMON_LEFT_PARENTHESIS = "(";
39 const std::string COMMON_RIGHT_PARENTHESIS = ")";
40 const std::string COMMON_EQUAL = " = ";
41 const unsigned int DEFAULT_LOG_FILE_MODE = 0644;
42 
43 // base/hiviewdfx/hiview/plugins/freeze_detector/config/freeze_rules.xml
44 const std::list<std::string> APPFREEZE_TYPE_LIST{
45     "UI_BLOCK_6S",
46     "UI_BLOCK_3S",
47     "UI_BLOCK_RECOVERED",
48     "THREAD_BLOCK_6S",
49     "THREAD_BLOCK_3S",
50     "APP_INPUT_BLOCK",
51     "NO_DRAW",
52     "BUSSINESS_THREAD_BLOCK_3S",
53     "BUSSINESS_THREAD_BLOCK_6S"
54 };
55 
56 const std::list<std::string> APPHICOLLIE_TYPE_LIST{
57     "APP_HICOLLIE"
58 };
59 
60 const std::list<std::string> KEY_IN_LOGFILE{
61     "timestamp",
62     "pid",
63     "uid",
64     "appRunningUniqueId",
65     "uuid",
66     "foreground",
67     "domain",
68     "stringId",
69     "version",
70     "package_name",
71     "process_name",
72     "message",
73     "hilog",
74     "exception",
75     "peer_binder",
76     "event_handler",
77     "event_handler_3s_size",
78     "event_handler_6s_size",
79     "stack",
80     "memory",
81 };
82 
83 struct FreezeJsonCollector {
84     unsigned long long timestamp = 0;
85     long pid = 0;
86     long uid = 0;
87     std::string appRunningUniqueId = "";
88     std::string uuid = "";
89     std::string domain = "";
90     std::string stringId = "";
91     bool foreground = false;
92     std::string version = "unknown";
93     std::string package_name = "";
94     std::string process_name = "";
95     std::string message = "";
96     std::string exception = "{}";
97     std::string hilog = "[]";
98     std::string peer_binder = "[]";
99     std::string event_handler = "[]";
100     std::string event_handler_3s_size = "";
101     std::string event_handler_6s_size = "";
102     std::string stack = "[]";
103     std::string memory = "{}";
104 };
105 
106 bool IsAppFreeze(const std::string& eventName);
107 
108 bool IsAppHicollie(const std::string& eventName);
109 
110 std::string GetFilePath(long pid, long uid, unsigned long long timestamp);
111 
112 int GetFd(const std::string& filePath);
113 
114 bool DelFile(const std::string& filePath);
115 
116 void LoadCollectorFromFile(const std::string& filePath, FreezeJsonCollector& jsonCollector);
117 
118 bool HasBeenWrapped(const std::string& target);
119 
120 template<typename T>
WrapByQuote(const T & wrapped)121 std::string WrapByQuote(const T& wrapped)
122 {
123     if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
124         if (HasBeenWrapped(wrapped)) {
125             return wrapped;
126         }
127         return COMMON_QUOTE + wrapped + COMMON_QUOTE;
128     }
129 
130     if constexpr (std::is_same_v<std::decay_t<T>, bool>) {
131         return wrapped ? "true" : "false";
132     }
133 
134     std::stringstream ss;
135     ss << wrapped;
136     return ss.str();
137 }
138 
139 std::string WrapByParenthesis(const std::string& wrapped);
140 
141 std::string WrapBySquareBracket(const std::string& wrapped);
142 
143 std::string WrapByBrace(const std::string& wrapped);
144 
145 template<typename T>
WriteKeyValue(int fd,const std::string & key,const T & value)146 bool WriteKeyValue(int fd, const std::string& key, const T& value)
147 {
148     std::stringstream ss;
149     ss << key << COMMON_EQUAL << value << std::endl;
150     return FileUtil::SaveStringToFd(fd, ss.str());
151 }
152 
153 template<typename T>
GetStrByKeyValue(const std::string & key,const T & value)154 std::string GetStrByKeyValue(const std::string& key, const T& value)
155 {
156     return WrapByQuote(key) + COMMON_COLON + WrapByQuote(value);
157 }
158 
159 template<typename T>
GetStrByList(std::list<T> & list)160 std::string GetStrByList(std::list<T>& list)
161 {
162     std::stringstream jsonSs;
163     if (!list.empty()) {
164         auto it = list.begin();
165         jsonSs << WrapByQuote(*it);
166         it++;
167         while (it != list.end()) {
168             jsonSs << COMMON_COMMA << WrapByQuote(*it);
169             it++;
170         }
171     }
172     return WrapBySquareBracket(jsonSs.str());
173 }
174 
175 template<typename T>
GetStrByMap(std::map<std::string,T> & map)176 std::string GetStrByMap(std::map<std::string, T>& map)
177 {
178     std::stringstream jsonSs;
179     if (!map.empty()) {
180         auto it = map.begin();
181         jsonSs << GetStrByKeyValue(it -> first, it -> second);
182         it++;
183         while (it != map.end()) {
184             jsonSs << COMMON_COMMA << GetStrByKeyValue(it -> first, it -> second);
185             it++;
186         }
187     }
188     return WrapByBrace(jsonSs.str());
189 }
190 
191 std::string MergeKeyValueList(std::list<std::string>& list);
192 
193 } // namespace FreezeJsonUtil
194 } // namespace HiviewDFX
195 } // namespace OHOS
196 #endif // FREEZE_JSON_UTIL_H