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