1 /*
2  * Copyright (c) 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 FAULT_DETECTOR_UTILS_H
16 #define FAULT_DETECTOR_UTILS_H
17 
18 #include <ctime>
19 #include <list>
20 #include <memory>
21 #include <string>
22 
23 #define LEAK_SHA256_LENGTH      32
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 // common interface
28 constexpr int TASK_LOOP_INTERVAL = 5;
29 const std::string UNKNOWN_PROCESS = "unknown_process";
30 const std::string KEY_HIVIEW_USER_TYPE = "const.logsystem.versiontype";
31 const std::string RELIABILITY_PATH = "/data/log/reliability";
32 const std::string RESOURCE_LEAK_PATH = "/data/log/reliability/resource_leak";
33 const std::string MEMORY_LEAK_PATH = "/data/log/reliability/resource_leak/memory_leak";
34 const std::string DUMP_NOTIFY_LOG_PATH = "/data/log/faultlog/temp";
35 constexpr uint32_t UNIT_OFFSET_BYTE = 1024;
36 
37 // leaked grade
38 const std::string MEMORY_LEAK_ERROR = "ERROR";
39 const std::string MEMORY_LEAK_WARNING = "WARNING";
40 const std::string MEMORY_LEAK_INFO = "INFO";
41 const std::string NO_MEMORY_LEAK = "/";
42 
43 class FaultDetectorUtil {
44 public:
45     static bool IsMemLeakEnable();
46     static bool IsMemTestEnable();
47     static bool GetIsHmKernel();
48     static std::string GetMemInfoPath();
49     static std::vector<std::string> Split(const std::string &str, char delim);
50     static bool IsDirectory(const std::string &path);
51     static std::vector<std::string> GetSubDir(const std::string &path, bool digit = false);
52     static std::vector<std::string> GetSubFile(const std::string &path, bool digit = false);
53     static std::vector<int32_t> GetAllPids();
54     static std::string ReadFileByChar(const std::string &path);
55     static bool RenameFile(const std::string &path, const std::string &newPath);
56     static std::string GetRealTime();
57     static std::string GetRealTimeStampStr();
58     static std::string TimeStampToStr(const time_t timeStamp);
59     static std::string TimeStampToSimplelyStr(const time_t timeStamp);
60     static std::vector<std::string> GetStatInfo(const std::string &path);
61     static std::string GetProcessName(int32_t pid);
62     static time_t GetProcessStartTime(int pid); // jiffes unit
63     static uint64_t GetProcessRss(int pid);
64     static int GetParentPid(int pid);
65     static bool IsKernelProcess(int pid);
66     static time_t GetRunningMonotonicTime();
67     static int32_t GetProcessUid(int32_t pid);
68     static std::string GetApplicationNameByUid(int32_t uid);
69     static std::string GetApplicationVersion(int32_t pid);
70     static bool IsBetaVersion();
71     static void GetStatm(int32_t pid, uint64_t &vss, uint64_t &rss);
72     static void GetMeminfo(uint64_t &avaliableMem, uint64_t &freeMem, uint64_t &totalMem);
73     static std::list<std::string> GetDightStrArr(const std::string &target);
74 };
75 } // HiviewDFX
76 } // OHOS
77 #endif // FAULT_DETECTOR_UTILS_H
78