• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef HIPERF_UTILITIES_H_
16 #define HIPERF_UTILITIES_H_
17 
18 // for security function
19 #include <securec.h>
20 #include <algorithm>
21 #include <cctype>
22 #include <cinttypes>
23 #include <cstdio>
24 #include <fstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <set>
31 
32 #include <dirent.h>
33 #include <fcntl.h>
34 #include <file_ex.h>
35 #include <stddef.h>
36 #include <sys/stat.h>
37 #include <unique_fd.h>
38 #include <unistd.h>
39 #if !is_mingw
40 #include <gtest/gtest_prod.h>
41 #include <sys/syscall.h>
42 #endif
43 #include <linux/types.h>
44 #include "debug_logger.h"
45 #include "noncopyable.h"
46 
47 // data and value
48 /*
49 long long always 64 only in ILP64, int is 64 otherwise int is always 32
50 */
51 using s8 = __s8;
52 using u8 = __u8;
53 using s16 = __s16;
54 using u16 = __u16;
55 using s32 = __s32;
56 using u32 = __u32;
57 using s64 = __s64;
58 using u64 = __u64;
59 
60 constexpr const int NUMBER_FORMAT_HEX_BASE = 16;
61 constexpr const int BYTE_PRINT_WIDTH = 2;
62 constexpr const int UINT64_PRINT_WIDTH = BYTE_PRINT_WIDTH * 8;
63 constexpr const int BITS_OF_BYTE = 8;
64 constexpr const int BITS_OF_TWO_BYTE = 2 * BITS_OF_BYTE;
65 constexpr const int BITS_OF_FOUR_BYTE = 4 * BITS_OF_BYTE;
66 constexpr const int FULL_PERCENTAGE = 100;
67 constexpr const int FULL_PERCENTAGE_NUM_LEN = 5;      // 100.00
68 constexpr const int FULL_PERCENTAGE_DIFF_NUM_LEN = 6; // +100.00
69 constexpr const int FULL_PERCENTAGE_LEN = 6;          // 100.00%
70 constexpr const int FULL_PERCENTAGE_DIFF_LEN = 7;     // +100.00%
71 constexpr const int THOUSANDS = 1000;
72 constexpr const int HUNDREDS = 100;
73 constexpr const int DEFAULT_STRING_BUF_SIZE = 4096;
74 constexpr const int FIVE_THOUSANDS = 5000;
75 constexpr const int DATA_MAX_SIZE = 1001;
76 constexpr const int LITTLE_MEMORY_SIZE = 1;
77 constexpr const int MULTIPLE_SIZE = 1024;
78 
79 #if !is_mingw
80 #ifndef O_BINARY
81 #define O_BINARY 0
82 #endif
83 #endif
84 
85 constexpr const double MS_DUARTION =
86     static_cast<double>(std::chrono::milliseconds::duration::period::den);
87 
88 constexpr uint64_t KILO = 1024;
89 
90 namespace OHOS {
91 namespace Developtools {
92 namespace HiPerf {
93 std::string CanonicalizeSpecPath(const char* src);
94 const std::string EMPTY_STRING = "";
95 const ssize_t ERRINFOLEN = 512;
96 const std::string USER_DOMESTIC_BETA = "beta";
97 const std::set<int> ALLOW_UIDS = {1201};
98 
99 static const std::string USER_TYPE_PARAM = "const.logsystem.versiontype";
100 static const std::string USER_TYPE_PARAM_GET = "";
101 static FILE *outputDump_ = nullptr;
102 const uint64_t waitAppRunCheckTimeOut = 10;
103 
104 struct ThreadInfos {
105     pid_t tid;
106     pid_t pid;
107 };
108 // string function
109 class MemoryHold {
110 public:
~MemoryHold()111     ~MemoryHold()
112     {
113         Clean();
114     }
115     const char *HoldStringView(std::string_view view);
116     // only use in UT
Clean()117     void Clean()
118     {
119         for (auto &p : holder_) {
120             delete[] p;
121         }
122         holder_.clear();
123     }
Get()124     static MemoryHold &Get()
125     {
126         static MemoryHold instance;
127         return instance;
128     }
129 
130 private:
131     std::vector<char *> holder_;
132 };
133 
134 std::string StringReplace(std::string source, const std::string &from, const std::string &to);
135 
136 template<class T>
VectorToString(const std::vector<T> & items)137 std::string VectorToString(const std::vector<T> &items)
138 {
139     if constexpr (std::is_same<T, std::vector<std::string>>::value) {
140         std::vector<std::string> stringItems;
141         for (auto item : items) {
142             stringItems.push_back("[" + VectorToString(item) + "]");
143         }
144         return VectorToString(stringItems);
145     } else {
146         std::string itemsString;
147         const std::string split = ",";
148         for (auto item : items) {
149             if (!itemsString.empty())
150                 itemsString.append(split);
151             if constexpr (std::is_same<T, std::string>::value) {
152                 itemsString.append(item);
153             } else {
154                 itemsString.append(std::to_string(item));
155             }
156         }
157         if (itemsString.empty())
158             itemsString.append("<empty>");
159         return itemsString;
160     }
161 }
162 
163 std::string BufferToHexString(const std::vector<unsigned char> &vec);
164 std::string BufferToHexString(const unsigned char buf[], size_t size);
165 void HexDump(const void *buf, size_t size, size_t max_size = 0);
166 
167 std::string &StringTrim(std::string &s);
168 
169 std::vector<std::string> StringSplit(std::string source, const std::string split = ",");
170 
171 size_t SubStringCount(const std::string &source, const std::string &sub);
172 
173 bool StringStartsWith(const std::string &string, const std::string &with);
174 
175 bool StringEndsWith(const std::string &string, const std::string &with);
176 
177 bool IsSameCommand(const std::string &cmdLine, const std::string &cmdName);
178 
179 std::vector<pid_t> GetSubthreadIDs(const pid_t pid);
180 
181 bool IsDigits(const std::string &str);
182 
183 bool IsHexDigits(const std::string &str);
184 
185 constexpr const int COMPRESS_READ_BUF_SIZE = 4096;
186 // compress specified dataFile into gzip file
187 bool CompressFile(const std::string &dataFile, const std::string &destFile);
188 // uncompress specified gzip file into dataFile
189 bool UncompressFile(const std::string &gzipFile, const std::string &dataFile);
190 
191 template<typename... VA>
StringPrintf(const char * stringFormat,VA...args)192 std::string StringPrintf(const char *stringFormat, VA... args)
193 {
194     // check howmany bytes we need
195     char bytes[DEFAULT_STRING_BUF_SIZE];
196     bytes[DEFAULT_STRING_BUF_SIZE - 1] = '\0';
197 
198     if (stringFormat == nullptr) {
199         return EMPTY_STRING;
200     }
201 
202     // print it to bytes
203     if (snprintf_s(bytes, sizeof(bytes), sizeof(bytes) - 1, stringFormat,
204                    args...) < 0) {
205         return EMPTY_STRING;
206     }
207 
208     // make a string return
209     return std::string(bytes);
210 }
211 
212 // path check
213 std::vector<std::string> GetEntriesInDir(const std::string &basePath);
214 
215 std::vector<std::string> GetSubDirs(const std::string &basePath);
216 std::vector<pid_t> GetSubthreadIDs(const pid_t pid, std::map<pid_t, ThreadInfos> &thread_map);
217 
218 bool IsDir(const std::string &path);
219 
220 bool IsPath(const std::string &fileName);
221 
222 bool LittleMemory();
223 
224 #if is_mingw
225 const char PATH_SEPARATOR = '\\';
226 #else
227 const char PATH_SEPARATOR = '/';
228 #endif
229 const std::string PATH_SEPARATOR_STR = std::string(1, PATH_SEPARATOR);
230 
231 std::string PlatformPathConvert(const std::string &path);
232 
233 // attribute
234 #define PACKED __attribute__((packed))
235 
236 // data align
237 
238 // some time u will meet signal 7 (SIGBUS), code 1 (BUS_ADRALN) in 32 or 64 arch cpu
239 #define HIPERF_BUF_ALIGN alignas(64)
240 
241 #define ALIGN(size, align) (((size) + (align) - 1) & (~((align) - 1)))
242 
243 uint32_t RoundUp(uint32_t x, const int align);
244 
245 // data convert function
246 template<class T>
247 std::string ToHex(const T &source, int size = sizeof(T), bool prefix = false)
248 {
249     std::stringstream ss;
250     if (prefix) {
251         ss << "0x";
252     }
253     ss << std::hex << std::setw(BYTE_PRINT_WIDTH * size) << std::setfill('0') << (uint64_t)source;
254     return ss.str();
255 }
256 
257 // data move and copy
258 template<class S, class T>
259 size_t inline CopyFromBufferAndMove(S *&buffer, T *dest, size_t size = 0)
260 {
261     if (size == 0) {
262         size = sizeof(T);
263     }
264     if (memcpy_s(dest, size, buffer, size) != EOK) {
265         return size;
266     }
267     buffer = buffer + size;
268     return size;
269 }
270 
271 // file read write
272 bool ReadIntFromProcFile(const std::string &path, int &value);
273 bool WriteIntToProcFile(const std::string &path, int value);
274 std::string ReadFileToString(const std::string &fileName);
275 bool ReadFileToString(const std::string &fileName, std::string &content, size_t fileSize = 0);
276 bool WriteStringToFile(const std::string &fileName, const std::string &value);
277 
278 // stdout
279 class StdoutRecord {
280 public:
~StdoutRecord()281     ~StdoutRecord()
282     {
283         Stop(); // stdout need restore
284     }
285     StdoutRecord(const std::string &tempFile = EMPTY_STRING,
286                  const std::string &mode = EMPTY_STRING);
287 
288     bool Start();
289     std::string Stop();
290 
291 private:
292     OHOS::UniqueFd stdoutFile_;       // back and restore stdout
293     std::FILE *recordFile_ = nullptr; // save the output
294     bool stop_ = true;
295     std::string content_ = EMPTY_STRING;
296 };
297 
298 // misc
299 template<class T>
Percentage(const T & a,const T & b)300 float Percentage(const T &a, const T &b)
301 {
302     return static_cast<float>(a) / static_cast<float>(b) * FULL_PERCENTAGE;
303 }
304 
305 bool IsRoot();
306 bool IsBeta();
307 bool IsAllowProfilingUid();
308 bool PowerOfTwo(uint64_t n);
309 
310 #define INDENT_ONE_LEVEL (indent + 1)
311 #define INDENT_TWO_LEVEL (indent + 2)
312 
313 #define PrintIndent(indent, format, ...)                                                           \
314     if (indent >= 0) {                                                                             \
315         if (outputDump_ == nullptr) {                                                              \
316             printf("%*s" format, (indent)*2, "", ##__VA_ARGS__);                                   \
317         } else {                                                                                   \
318             fprintf(outputDump_, "%*s" format, (indent)*2, "", ##__VA_ARGS__);                     \
319         }                                                                                          \
320     } else {                                                                                       \
321         HLOGV("%s" format, "", ##__VA_ARGS__);                                                     \
322     }
323 
324 #ifndef MMAP_FAILED
325 #define MMAP_FAILED reinterpret_cast<void *>(-1)
326 #endif
327 #ifndef MAP_FAILED
328 #define MAP_FAILED MMAP_FAILED
329 #endif
330 pid_t GetAppPackagePid(const std::string &appPackage, const pid_t oldPid, const int checkAppMs,
331                        const uint64_t waitAppTimeOut);
332 bool IsNeedCheckSamePid(const std::string &fileName, const std::string &appPackage, const std::string &subDir,
333                         pid_t &res, const pid_t oldPid);
334 bool CheckAppIsRunning (std::vector<pid_t> &selectPids, const std::string &appPackage, int checkAppMs);
335 bool IsExistDebugByApp(const std::string& bundleName);
336 bool IsExistDebugByPid(const std::vector<pid_t> pids);
337 bool IsSupportNonDebuggableApp();
338 const std::string GetUserType();
339 std::string GetProcessName(int pid);
340 bool IsDebugableApp(const std::string& bundleName);
341 // Sanbox lib path check
342 std::string AdaptSandboxPath(std::string filePath, int pid);
343 } // namespace HiPerf
344 } // namespace Developtools
345 } // namespace OHOS
346 
347 // this will also used for libunwind head (out of namespace)
348 #if is_mingw
349 #if !is_double_framework
350 #define HAVE_MMAP   1
351 #define MAP_PRIVATE 0x02
352 #define PROT_NONE   0
353 #define PROT_READ   1
354 #define PROT_WRITE  2
355 #define PROT_EXEC   4
356 void *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset);
357 int munmap(void *addr, size_t);
358 #endif
359 #endif
360 
361 #endif // HIPERF_UTILITIES_H_
362