• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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 process name by id
103  * @param pid process id
104  * @param str process name
105 */
106 void ReadProcessName(const int pid, std::string& str);
107 
108 /**
109  * @brief read process status by id
110  * @param result content of status
111  * @param pid process id
112  * @param withThreadName whether output thread name or not
113 */
114 void ReadProcessStatus(std::string& result, const int pid);
115 
116 /**
117  * @brief read process wchan by id
118  * @param result content of wchan
119  * @param pid process id
120  * @param onlyPid if only print process wchan
121  * @param withThreadName whether output thread name or not
122 */
123 void ReadProcessWchan(std::string& result, const int pid, bool onlyPid = false, bool withThreadName = false);
124 
125 /**
126  * @brief read thread wchan by id
127  * @param result content of wchan
128  * @param tid thread id
129  * @param withThreadName whether output thread name or not
130 */
131 void ReadThreadWchan(std::string& result, const int tid, bool withThreadName = false);
132 } // nameapace HiviewDFX
133 } // nameapace OHOS
134 #ifdef __cplusplus
135 }
136 #endif
137 #endif
138