• 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     "LIFECYCLE_HALF_TIMEOUT",
55     "LIFECYCLE_TIMEOUT"
56 };
57 
58 const std::list<std::string> APPHICOLLIE_TYPE_LIST{
59     "APP_HICOLLIE"
60 };
61 
62 const std::list<std::string> KEY_IN_LOGFILE{
63     "timestamp",
64     "pid",
65     "uid",
66     "appRunningUniqueId",
67     "uuid",
68     "foreground",
69     "domain",
70     "stringId",
71     "version",
72     "package_name",
73     "process_name",
74     "message",
75     "hilog",
76     "exception",
77     "peer_binder",
78     "event_handler",
79     "event_handler_3s_size",
80     "event_handler_6s_size",
81     "stack",
82     "memory",
83 };
84 
85 struct FreezeJsonCollector {
86     unsigned long long timestamp = 0;
87     long pid = 0;
88     long uid = 0;
89     std::string appRunningUniqueId = "";
90     std::string uuid = "";
91     std::string domain = "";
92     std::string stringId = "";
93     bool foreground = false;
94     std::string version = "unknown";
95     std::string package_name = "";
96     std::string process_name = "";
97     std::string message = "";
98     std::string exception = "{}";
99     std::string hilog = "[]";
100     std::string peer_binder = "[]";
101     std::string event_handler = "[]";
102     std::string event_handler_3s_size = "";
103     std::string event_handler_6s_size = "";
104     std::string stack = "[]";
105     std::string memory = "{}";
106 };
107 
108 bool IsAppFreeze(const std::string& eventName);
109 
110 bool IsAppHicollie(const std::string& eventName);
111 
112 std::string GetFilePath(long pid, long uid, unsigned long long timestamp);
113 
114 int GetFd(const std::string& filePath);
115 
116 bool DelFile(const std::string& filePath);
117 
118 void LoadCollectorFromFile(const std::string& filePath, FreezeJsonCollector& jsonCollector);
119 
120 bool HasBeenWrapped(const std::string& target);
121 
122 template<typename T>
WrapByQuote(const T & wrapped)123 std::string WrapByQuote(const T& wrapped)
124 {
125     if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
126         if (HasBeenWrapped(wrapped)) {
127             return wrapped;
128         }
129         return COMMON_QUOTE + wrapped + COMMON_QUOTE;
130     }
131 
132     if constexpr (std::is_same_v<std::decay_t<T>, bool>) {
133         return wrapped ? "true" : "false";
134     }
135 
136     std::stringstream ss;
137     ss << wrapped;
138     return ss.str();
139 }
140 
141 std::string WrapByParenthesis(const std::string& wrapped);
142 
143 std::string WrapBySquareBracket(const std::string& wrapped);
144 
145 std::string WrapByBrace(const std::string& wrapped);
146 
147 template<typename T>
WriteKeyValue(int fd,const std::string & key,const T & value)148 bool WriteKeyValue(int fd, const std::string& key, const T& value)
149 {
150     std::stringstream ss;
151     ss << key << COMMON_EQUAL << value << std::endl;
152     return FileUtil::SaveStringToFd(fd, ss.str());
153 }
154 
155 template<typename T>
GetStrByKeyValue(const std::string & key,const T & value)156 std::string GetStrByKeyValue(const std::string& key, const T& value)
157 {
158     return WrapByQuote(key) + COMMON_COLON + WrapByQuote(value);
159 }
160 
161 template<typename T>
GetStrByList(std::list<T> & list)162 std::string GetStrByList(std::list<T>& list)
163 {
164     std::stringstream jsonSs;
165     if (!list.empty()) {
166         auto it = list.begin();
167         jsonSs << WrapByQuote(*it);
168         it++;
169         while (it != list.end()) {
170             jsonSs << COMMON_COMMA << WrapByQuote(*it);
171             it++;
172         }
173     }
174     return WrapBySquareBracket(jsonSs.str());
175 }
176 
177 template<typename T>
GetStrByMap(std::map<std::string,T> & map)178 std::string GetStrByMap(std::map<std::string, T>& map)
179 {
180     std::stringstream jsonSs;
181     if (!map.empty()) {
182         auto it = map.begin();
183         jsonSs << GetStrByKeyValue(it -> first, it -> second);
184         it++;
185         while (it != map.end()) {
186             jsonSs << COMMON_COMMA << GetStrByKeyValue(it -> first, it -> second);
187             it++;
188         }
189     }
190     return WrapByBrace(jsonSs.str());
191 }
192 
193 std::string MergeKeyValueList(std::list<std::string>& list);
194 
195 } // namespace FreezeJsonUtil
196 } // namespace HiviewDFX
197 } // namespace OHOS
198 #endif // FREEZE_JSON_UTIL_H