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