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