1 /* 2 * Copyright (c) 2023 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 DFX_PROCINFO_H 17 #define DFX_PROCINFO_H 18 19 #include <cstdio> 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 /** 28 * @brief process information 29 */ 30 typedef struct ProcInfo { 31 /** process id */ 32 int pid; 33 /** parent process id */ 34 int ppid; 35 /** namespace process id */ 36 int nsPid; 37 /** namespace is enabled or not */ 38 bool ns = false; 39 } ProcInfo; 40 41 /** 42 * @brief Get process status 43 * 44 * @param procInfo structure containing information about process(output parameter) 45 * @return if succeed return true, otherwise return false 46 */ 47 bool GetProcStatus(struct ProcInfo& procInfo); 48 /** 49 * @brief Get process status by specified process id 50 * 51 * @param realPid real process id 52 * @param procInfo structure containing information about process(output parameter) 53 * @return if succeed return true, otherwise return false 54 */ 55 bool GetProcStatusByPid(int realPid, struct ProcInfo& procInfo); 56 /** 57 * @brief convert real tid to namespace tid 58 * 59 * @param pid process id 60 * @param tid thread id 61 * @param nsTid namespace tid(output parameter) 62 * @return if succeed return true, otherwise return false 63 */ 64 bool TidToNstid(const int pid, const int tid, int& nstid); 65 /** 66 * @brief convert real tid to namespace tid 67 * 68 * @param pid process id 69 * @param tid thread id 70 * @return if succeed return true, otherwise return false 71 */ 72 bool IsThreadInPid(int32_t pid, int32_t tid); 73 /** 74 * @brief Get thread id by process id and function 75 * 76 * @param pid process id 77 * @param tids thread ids(output parameter) 78 * @param func function 79 * @return if succeed return true, otherwise return false 80 */ 81 bool GetTidsByPidWithFunc(const int pid, std::vector<int>& tids, std::function<bool(int)> const& func); 82 /** 83 * @brief Get thread ids and namespace thread ids by process id 84 * 85 * @param pid process id 86 * @param tids thread ids(output parameter) 87 * @param nsTid namespace tids(output parameter) 88 * @return if succeed return true, otherwise return false 89 */ 90 bool GetTidsByPid(const int pid, std::vector<int>& tids, std::vector<int>& nstids); 91 /** 92 * @brief read thread name by id 93 * @param tid thread id 94 * @param str thread name 95 */ 96 void ReadThreadName(const int tid, std::string& str); 97 /** 98 * @brief read thread name by id 99 * @param tid thread id 100 * @param str thread name 101 */ 102 void ReadThreadNameByPidAndTid(const int pid, const int tid, std::string& str); 103 /** 104 * @brief read process name by id 105 * @param pid process id 106 * @param str process name 107 */ 108 void ReadProcessName(const int pid, std::string& str); 109 110 /** 111 * @brief read process status by id 112 * @param result content of status 113 * @param pid process id 114 * @param withThreadName whether output thread name or not 115 */ 116 void ReadProcessStatus(std::string& result, const int pid); 117 118 /** 119 * @brief read process wchan by id 120 * @param result content of wchan 121 * @param pid process id 122 * @param onlyPid if only print process wchan 123 * @param withThreadName whether output thread name or not 124 */ 125 void ReadProcessWchan(std::string& result, const int pid, bool onlyPid = false, bool withThreadName = false); 126 127 /** 128 * @brief read thread wchan by id 129 * @param result content of wchan 130 * @param tid thread id 131 * @param withThreadName whether output thread name or not 132 */ 133 void ReadThreadWchan(std::string& result, const int tid, bool withThreadName = false); 134 /** 135 * @brief Get stacktrace head info 136 */ 137 std::string GetStacktraceHeader(); 138 } // nameapace HiviewDFX 139 } // nameapace OHOS 140 #endif 141