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 
16 #ifndef RELIABILITY_XCOLLIE_UTILS_H
17 #define RELIABILITY_XCOLLIE_UTILS_H
18 
19 #include <chrono>
20 #include <string>
21 #include <sys/ioctl.h>
22 #include <fstream>
23 #include <vector>
24 
25 #include "hilog/log.h"
26 
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002D06
29 
30 #undef LOG_TAG
31 #define LOG_TAG "XCollie"
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 enum DumpStackState {
36     DEFAULT = 0,
37     COMPLETE = 1,
38     SAMPLE_COMPLETE = 2
39 };
40 constexpr char WMS_FULL_NAME[] = "WindowManagerService";
41 constexpr char IPC_FULL[] = "IPC_FULL";
42 constexpr char IPC_CHECKER[] = "IpcChecker";
43 constexpr char STACK_CHECKER[] = "ThreadSampler";
44 constexpr char TRACE_CHECKER[] = "TraceCollector";
45 constexpr int64_t SEC_TO_MANOSEC = 1000000000;
46 constexpr int64_t SEC_TO_MICROSEC = 1000000;
47 constexpr uint64_t MIN_APP_UID = 20000;
48 constexpr int64_t ONE_DAY_LIMIT = 86400000;
49 constexpr int64_t ONE_HOUR_LIMIT = 3600000;
50 constexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
51 constexpr uint64_t MAX_FILE_SIZE = 10 * 1024 * 1024; // 10M
52 const int BUFF_STACK_SIZE = 20 * 1024;
53 const int FFRT_BUFFER_SIZE = 512 * 1024;
54 const int MAX_NAME_SIZE = 128;
55 const int MIN_WAIT_NUM = 3;
56 const int TIME_INDEX_MAX = 32;
57 const int DETECT_STACK_COUNT = 2;
58 const int COLLECT_STACK_COUNT = 10;
59 const int COLLECT_TRACE_MIN = 1;
60 const int COLLECT_TRACE_MAX = 20;
61 const int TASK_INTERVAL = 155;
62 const int DURATION_TIME = 150;
63 const int INIT_PID = 1;
64 const int DISTRIBUTE_TIME = 2000;
65 const int DUMPTRACE_TIME = 450;
66 const inline std::string LOGGER_BINDER_PROC_PATH = "/proc/transaction_proc";
67 const std::string WATCHDOG_DIR = "/data/storage/el2/log/watchdog";
68 
69 #define XCOLLIE_LOGF(...) HILOG_FATAL(LOG_CORE, ##__VA_ARGS__)
70 #define XCOLLIE_LOGE(...) HILOG_ERROR(LOG_CORE, ##__VA_ARGS__)
71 #define XCOLLIE_LOGW(...) HILOG_WARN(LOG_CORE, ##__VA_ARGS__)
72 #define XCOLLIE_LOGI(...) HILOG_INFO(LOG_CORE, ##__VA_ARGS__)
73 #define XCOLLIE_LOGD(...) HILOG_DEBUG(LOG_CORE, ##__VA_ARGS__)
74 #define MAGIC_NUM           0x9517
75 #define HTRANSIO            0xAB
76 #define LOGGER_GET_STACK    _IO(HTRANSIO, 9)
77 
78 uint64_t GetCurrentTickMillseconds();
79 
80 bool IsFileNameFormat(char c);
81 
82 std::string GetSelfProcName();
83 
84 std::string GetFirstLine(const std::string& path);
85 
86 std::string GetProcessNameFromProcCmdline(int32_t pid = 0);
87 
88 std::string GetLimitedSizeName(std::string name);
89 
90 bool IsProcessDebug(int32_t pid);
91 
92 void DelayBeforeExit(unsigned int leftTime);
93 
94 std::string TrimStr(const std::string& str, const char cTrim = ' ');
95 
96 void SplitStr(const std::string& str, const std::string& sep,
97     std::vector<std::string>& strs, bool canEmpty = false, bool needTrim = true);
98 
99 int ParsePeerBinderPid(std::ifstream& fin, int32_t pid);
100 
101 bool KillProcessByPid(int32_t pid);
102 
103 bool IsDeveloperOpen();
104 
105 bool IsBetaVersion();
106 
107 std::string GetFormatDate();
108 
109 bool CreateWatchdogDir();
110 
111 bool WriteStackToFd(int32_t pid, std::string& path, std::string& stack,
112     const std::string& eventName);
113 
114 int64_t GetTimeStamp();
115 
116 void* FunctionOpen(void* funcHandler, const char* funcName);
117 } // end of HiviewDFX
118 } // end of OHOS
119 #endif
120