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