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