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 #include "procinfo.h"
17
18 #include <cctype>
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <iostream>
24 #include <sstream>
25 #include <securec.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "dfx_define.h"
29 #include "dfx_util.h"
30 #include "file_util.h"
31 #include "string_printf.h"
32 #include "string_util.h"
33 #include <iostream>
34
35 namespace OHOS {
36 namespace HiviewDFX {
37 namespace {
38 const char PID_STR_NAME[] = "Pid:";
39 const char PPID_STR_NAME[] = "PPid:";
40 const char NSPID_STR_NAME[] = "NSpid:";
41 const int ARGS_COUNT_ONE = 1;
42 const int ARGS_COUNT_TWO = 2;
43 const int STATUS_LINE_SIZE = 1024;
44 }
45
GetProcStatusByPath(struct ProcInfo & procInfo,const std::string & path)46 static bool GetProcStatusByPath(struct ProcInfo& procInfo, const std::string& path)
47 {
48 char buf[STATUS_LINE_SIZE];
49 FILE *fp = fopen(path.c_str(), "r");
50 if (fp == nullptr) {
51 return false;
52 }
53
54 int pid = 0;
55 int ppid = 0;
56 int nsPid = 0;
57 while (!feof(fp)) {
58 if (fgets(buf, STATUS_LINE_SIZE, fp) == nullptr) {
59 fclose(fp);
60 return false;
61 }
62
63 if (strncmp(buf, PID_STR_NAME, strlen(PID_STR_NAME)) == 0) {
64 // Pid: 1892
65 if (sscanf_s(buf, "%*[^0-9]%d", &pid) != ARGS_COUNT_ONE) {
66 #if is_ohos
67 procInfo.pid = getprocpid();
68 #else
69 procInfo.pid = getpid();
70 #endif
71 } else {
72 procInfo.pid = pid;
73 }
74 procInfo.nsPid = pid;
75 continue;
76 }
77
78 if (strncmp(buf, PPID_STR_NAME, strlen(PPID_STR_NAME)) == 0) {
79 // PPid: 240
80 if (sscanf_s(buf, "%*[^0-9]%d", &ppid) != ARGS_COUNT_ONE) {
81 procInfo.ppid = getppid();
82 } else {
83 procInfo.ppid = ppid;
84 }
85 continue;
86 }
87
88 if (strncmp(buf, NSPID_STR_NAME, strlen(NSPID_STR_NAME)) == 0) {
89 // NSpid: 1892 1
90 if (sscanf_s(buf, "%*[^0-9]%d%*[^0-9]%d", &pid, &nsPid) != ARGS_COUNT_TWO) {
91 procInfo.ns = false;
92 procInfo.nsPid = pid;
93 } else {
94 procInfo.ns = true;
95 procInfo.nsPid = nsPid;
96 }
97 procInfo.pid = pid;
98 break;
99 }
100 }
101 (void)fclose(fp);
102 return true;
103 }
104
TidToNstid(const int pid,const int tid,int & nstid)105 bool TidToNstid(const int pid, const int tid, int& nstid)
106 {
107 std::string path = StringPrintf("/proc/%d/task/%d/status", pid, tid);
108 if (path.empty()) {
109 return false;
110 }
111
112 struct ProcInfo procInfo;
113 if (!GetProcStatusByPath(procInfo, path)) {
114 return false;
115 }
116 nstid = procInfo.nsPid;
117 return true;
118 }
119
GetProcStatusByPid(int realPid,struct ProcInfo & procInfo)120 bool GetProcStatusByPid(int realPid, struct ProcInfo& procInfo)
121 {
122 #if is_ohos
123 if (realPid == getprocpid()) {
124 #else
125 if (realPid == getpid()) {
126 #endif
127 return GetProcStatus(procInfo);
128 }
129 std::string path = StringPrintf("/proc/%d/status", realPid);
130 return GetProcStatusByPath(procInfo, path);
131 }
132
133 bool GetProcStatus(struct ProcInfo& procInfo)
134 {
135 return GetProcStatusByPath(procInfo, PROC_SELF_STATUS_PATH);
136 }
137
138 bool IsThreadInPid(int32_t pid, int32_t tid)
139 {
140 std::string path;
141 #if is_ohos
142 if (pid == getprocpid()) {
143 #else
144 if (pid == getpid()) {
145 #endif
146 path = StringPrintf("%s/%d", PROC_SELF_TASK_PATH, tid);
147 } else {
148 path = StringPrintf("/proc/%d/task/%d", pid, tid);
149 }
150 return access(path.c_str(), F_OK) == 0;
151 }
152
153 bool GetTidsByPidWithFunc(const int pid, std::vector<int>& tids, std::function<bool(int)> const& func)
154 {
155 std::vector<std::string> files;
156 if (ReadDirFilesByPid(pid, files)) {
157 for (size_t i = 0; i < files.size(); ++i) {
158 pid_t tid = atoi(files[i].c_str());
159 if (tid == 0) {
160 continue;
161 }
162 tids.push_back(tid);
163
164 if (func != nullptr) {
165 func(tid);
166 }
167 }
168 }
169 return (tids.size() > 0);
170 }
171
172 bool GetTidsByPid(const int pid, std::vector<int>& tids, std::vector<int>& nstids)
173 {
174 struct ProcInfo procInfo;
175 (void)GetProcStatusByPid(pid, procInfo);
176
177 std::function<bool(int)> func = nullptr;
178 if (procInfo.ns) {
179 func = [&](int tid) {
180 pid_t nstid = tid;
181 TidToNstid(pid, tid, nstid);
182 nstids.push_back(nstid);
183 return true;
184 };
185 }
186 bool ret = GetTidsByPidWithFunc(pid, tids, func);
187 if (ret && !procInfo.ns) {
188 nstids = tids;
189 }
190 return (nstids.size() > 0);
191 }
192
193 void ReadThreadName(const int tid, std::string& str)
194 {
195 std::string path = StringPrintf("/proc/%d/comm", tid);
196 std::string name;
197 OHOS::HiviewDFX::LoadStringFromFile(path, name);
198 TrimAndDupStr(name, str);
199 }
200
201 void ReadThreadNameByPidAndTid(const int pid, const int tid, std::string& str)
202 {
203 std::string path = StringPrintf("/proc/%d/task/%d/comm", pid, tid);
204 std::string name;
205 OHOS::HiviewDFX::LoadStringFromFile(path, name);
206 TrimAndDupStr(name, str);
207 }
208
209 void ReadProcessName(const int pid, std::string& str)
210 {
211 std::string path;
212 #if is_ohos
213 if (pid == getprocpid()) {
214 #else
215 if (pid == getpid()) {
216 #endif
217 path = std::string(PROC_SELF_CMDLINE_PATH);
218 } else {
219 path = StringPrintf("/proc/%d/cmdline", pid);
220 }
221 std::string name;
222 OHOS::HiviewDFX::LoadStringFromFile(path, name);
223 std::cout << name << std::endl;
224 TrimAndDupStr(name, str);
225 }
226
227 void ReadProcessStatus(std::string& result, const int pid)
228 {
229 std::string path = StringPrintf("/proc/%d/status", pid);
230 if (access(path.c_str(), F_OK) != 0) {
231 result.append(StringPrintf("Failed to access path(%s), errno(%d).\n", path.c_str(), errno));
232 return;
233 }
234 std::string content;
235 OHOS::HiviewDFX::LoadStringFromFile(path, content);
236 if (!content.empty()) {
237 std::string str = StringPrintf("Process status:\n%s\n", content.c_str());
238 result.append(str);
239 }
240 }
241
242 void ReadProcessWchan(std::string& result, const int pid, bool onlyPid, bool withThreadName)
243 {
244 std::string path = StringPrintf("/proc/%d/wchan", pid);
245 if (access(path.c_str(), F_OK) != 0) {
246 result.append(StringPrintf("Failed to access path(%s), errno(%d).\n", path.c_str(), errno));
247 return;
248 }
249 std::ostringstream ss;
250 std::string content;
251 OHOS::HiviewDFX::LoadStringFromFile(path, content);
252 if (!content.empty()) {
253 ss << "Process wchan:\n";
254 ss << StringPrintf("%s\n", content.c_str());
255 }
256 if (onlyPid) {
257 result.append(ss.str());
258 return;
259 }
260 ss << "\nProcess threads wchan:\n";
261 ss << "=======================================\n";
262 bool flag = false;
263 std::string comm = "";
264 std::string wchan = "";
265 std::string taskPath = StringPrintf("/proc/%d/task", pid);
266 std::vector<std::string> files;
267 flag = ReadDirFiles(taskPath, files);
268 for (size_t i = 0; i < files.size(); ++i) {
269 std::string tidStr = files[i];
270 std::string commPath = StringPrintf("%s/%s/comm", taskPath.c_str(), tidStr.c_str());
271 std::string wchanPath = StringPrintf("%s/%s/wchan", taskPath.c_str(), tidStr.c_str());
272 OHOS::HiviewDFX::LoadStringFromFile(commPath, comm);
273 OHOS::HiviewDFX::LoadStringFromFile(wchanPath, wchan);
274 if (!comm.empty() && !wchan.empty()) {
275 flag = true;
276 if (withThreadName) {
277 ss << "Tid:" << tidStr << ", Name:" << comm;
278 }
279 ss << "wchan:" << wchan << std::endl;
280 }
281 }
282
283 if (!flag) {
284 ss << "Failed to access path: " << taskPath << std::endl;
285 }
286 ss << "=======================================\n";
287 result.append(ss.str());
288 }
289
290 void ReadThreadWchan(std::string& result, const int tid, bool withThreadName)
291 {
292 std::ostringstream ss;
293 if (withThreadName) {
294 std::string threadName;
295 ReadThreadName(tid, threadName);
296 ss << "Tid:" << tid << ", Name:" << threadName << std::endl;
297 }
298 std::string wchanPath = StringPrintf("%s/%d/wchan", PROC_SELF_TASK_PATH, tid);
299 std::string wchan;
300 if (OHOS::HiviewDFX::LoadStringFromFile(wchanPath, wchan)) {
301 ss << "wchan:" << wchan << std::endl;
302 } else {
303 ss << "Load thread wchan failed." << std::endl;
304 }
305 result = ss.str();
306 }
307 } // namespace HiviewDFX
308 } // namespace OHOS
309