1 /*
2  * Copyright (C) 2021 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 HIDUMPER_ZIDL_COMMON_UTILS_H
16 #define HIDUMPER_ZIDL_COMMON_UTILS_H
17 #include <string>
18 #include <vector>
19 namespace OHOS {
20 namespace HiviewDFX {
21 class DumpCommonUtils {
22 public:
23     struct CpuInfo {
24         int id_;
25     public:
26         CpuInfo();
27     };
28     // get all cpu information in device.
29     static bool GetCpuInfos(std::vector<CpuInfo> &infos);
30     struct PidInfo {
31         int pid_;
32         int ppid_;
33         int uid_;
34         int gid_;
35         std::string name_;
36         std::string cmdline_;
37     public:
38         PidInfo();
39         void Reset();
40     };
41     // get subNodes information by pid.
42     static std::vector<std::string> GetSubNodes(const std::string &path, bool digit);
43     // get subDir information by pid.
44     static bool IsDirectory(const std::string &path);
45     static std::vector<std::string> GetSubDir(const std::string &path, bool digit);
46     // get all pids in device.
47     static std::vector<int32_t> GetAllPids();
48     // get all process information in device.
49     static bool GetPidInfos(std::vector<PidInfo> &infos, bool all = false);
50     // get process name by pid.
51     static bool GetProcessNameByPid(int pid, std::string &name);
52     // get process information by pid.
53     static bool GetProcessInfo(int pid, PidInfo &info);
54     // check head of string.
55     static bool StartWith(const std::string& str, const std::string& head);
56     static bool GetUserPids(std::vector<int> &pids);
57     static bool IsUserPid(const std::string &pid);
58     static int FindDigitIndex(const std::string& fullFileName);
59 private:
60     static bool GetLinesInFile(const std::string& file, std::vector<std::string>& lines);
61     static bool GetNamesInFolder(const std::string& folder, std::vector<std::string>& names);
62 };
63 } // namespace HiviewDFX
64 } // namespace OHOS
65 #endif // HIDUMPER_ZIDL_COMMON_UTILS_H
66