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 defined(is_ohos) && 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 defined(is_ohos) && 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 defined(is_ohos) && 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::string path;
156 #if defined(is_ohos) && is_ohos
157 if (pid == getprocpid()) {
158 #else
159 if (pid == getpid()) {
160 #endif
161 path = std::string(PROC_SELF_TASK_PATH);
162 } else {
163 path = StringPrintf("/proc/%d/task", pid);
164 }
165
166 std::vector<std::string> files;
167 if (ReadDirFiles(path, files)) {
168 for (size_t i = 0; i < files.size(); ++i) {
169 pid_t tid = atoi(files[i].c_str());
170 if (tid == 0) {
171 continue;
172 }
173 tids.emplace_back(tid);
174
175 if (func != nullptr) {
176 func(tid);
177 }
178 }
179 }
180 return (tids.size() > 0);
181 }
182
183 bool GetTidsByPid(const int pid, std::vector<int>& tids, std::vector<int>& nstids)
184 {
185 struct ProcInfo procInfo;
186 (void)GetProcStatusByPid(pid, procInfo);
187
188 std::function<bool(int)> func = nullptr;
189 if (procInfo.ns) {
190 func = [&](int tid) {
191 pid_t nstid = tid;
192 TidToNstid(pid, tid, nstid);
193 nstids.emplace_back(nstid);
194 return true;
195 };
196 }
197 bool ret = GetTidsByPidWithFunc(pid, tids, func);
198 if (ret && !procInfo.ns) {
199 nstids = tids;
200 }
201 return (nstids.size() > 0);
202 }
203
204 std::string GetStacktraceHeader()
205 {
206 pid_t pid = getprocpid();
207 std::ostringstream ss;
208 ss << "" << std::endl << "Timestamp:" << GetCurrentTimeStr();
209 ss << "Pid:" << pid << std::endl;
210 ss << "Uid:" << getuid() << std::endl;
211 std::string processName;
212 ReadProcessName(pid, processName);
213 std::string processNameNoNul;
214 for (char c : processName) {
215 if (c != '\0') {
216 processNameNoNul += c;
217 } else {
218 break;
219 }
220 }
221 ss << "Process name::" << processNameNoNul << std::endl;
222 return ss.str();
223 }
224
225 void ReadThreadName(const int tid, std::string& str)
226 {
227 std::string path = StringPrintf("/proc/%d/comm", tid);
228 std::string name;
229 OHOS::HiviewDFX::LoadStringFromFile(path, name);
230 TrimAndDupStr(name, str);
231 }
232
233 void ReadThreadNameByPidAndTid(const int pid, const int tid, std::string& str)
234 {
235 std::string path = StringPrintf("/proc/%d/task/%d/comm", pid, tid);
236 std::string name;
237 OHOS::HiviewDFX::LoadStringFromFile(path, name);
238 TrimAndDupStr(name, str);
239 }
240
241 void ReadProcessName(const int pid, std::string& str)
242 {
243 std::string path;
244 #if defined(is_ohos) && is_ohos
245 if (pid == getprocpid()) {
246 #else
247 if (pid == getpid()) {
248 #endif
249 path = std::string(PROC_SELF_CMDLINE_PATH);
250 } else {
251 path = StringPrintf("/proc/%d/cmdline", pid);
252 }
253 std::string name;
254 OHOS::HiviewDFX::LoadStringFromFile(path, name);
255 TrimAndDupStr(name, str);
256 }
257
258 void ReadProcessStatus(std::string& result, const int pid)
259 {
260 std::string path = StringPrintf("/proc/%d/status", pid);
261 if (access(path.c_str(), F_OK) != 0) {
262 result.append(StringPrintf("Failed to access path(%s), errno(%d).\n", path.c_str(), errno));
263 return;
264 }
265 std::string content;
266 OHOS::HiviewDFX::LoadStringFromFile(path, content);
267 if (!content.empty()) {
268 std::string str = StringPrintf("Process status:\n%s\n", content.c_str());
269 result.append(str);
270 }
271 }
272
273 void ReadProcessWchan(std::string& result, const int pid, bool onlyPid, bool withThreadName)
274 {
275 std::string path = StringPrintf("/proc/%d/wchan", pid);
276 if (access(path.c_str(), F_OK) != 0) {
277 result.append(StringPrintf("Failed to access path(%s), errno(%d).\n", path.c_str(), errno));
278 return;
279 }
280 std::ostringstream ss;
281 std::string content;
282 OHOS::HiviewDFX::LoadStringFromFile(path, content);
283 if (!content.empty()) {
284 ss << "Process wchan:\n";
285 ss << StringPrintf("%s\n", content.c_str());
286 }
287 if (onlyPid) {
288 result.append(ss.str());
289 return;
290 }
291 ss << "\nProcess threads wchan:\n";
292 ss << "=======================================\n";
293 bool flag = false;
294 std::string comm = "";
295 std::string wchan = "";
296 std::string taskPath = StringPrintf("/proc/%d/task", pid);
297 std::vector<std::string> files;
298 flag = ReadDirFiles(taskPath, files);
299 for (size_t i = 0; i < files.size(); ++i) {
300 std::string tidStr = files[i];
301 std::string commPath = StringPrintf("%s/%s/comm", taskPath.c_str(), tidStr.c_str());
302 std::string wchanPath = StringPrintf("%s/%s/wchan", taskPath.c_str(), tidStr.c_str());
303 OHOS::HiviewDFX::LoadStringFromFile(commPath, comm);
304 OHOS::HiviewDFX::LoadStringFromFile(wchanPath, wchan);
305 if (!comm.empty() && !wchan.empty()) {
306 flag = true;
307 if (withThreadName) {
308 ss << "Tid:" << tidStr << ", Name:" << comm;
309 }
310 ss << "wchan:" << wchan << std::endl;
311 }
312 }
313
314 if (!flag) {
315 ss << "Failed to access path: " << taskPath << std::endl;
316 }
317 ss << "=======================================\n";
318 result.append(ss.str());
319 }
320
321 void ReadThreadWchan(std::string& result, const int tid, bool withThreadName)
322 {
323 std::ostringstream ss;
324 if (withThreadName) {
325 std::string threadName;
326 ReadThreadName(tid, threadName);
327 ss << "Tid:" << tid << ", Name:" << threadName << std::endl;
328 }
329 std::string wchanPath = StringPrintf("%s/%d/wchan", PROC_SELF_TASK_PATH, tid);
330 std::string wchan;
331 if (OHOS::HiviewDFX::LoadStringFromFile(wchanPath, wchan)) {
332 ss << "wchan:" << wchan << std::endl;
333 } else {
334 ss << "Load thread wchan failed." << std::endl;
335 }
336 result = ss.str();
337 }
338 } // namespace HiviewDFX
339 } // namespace OHOS
340