blob: 3b3c8a1ae93b1f5559e7f8408fa1fe2711a2c898 [file] [log] [blame]
Elliott Hughes0badbd62014-12-29 12:24:25 -08001#include <ctype.h>
2#include <dirent.h>
3#include <fcntl.h>
Elliott Hughes8ac94d32015-03-09 21:10:10 -07004#include <inttypes.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -08005#include <pwd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006#include <stdio.h>
7#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009#include <sys/stat.h>
10#include <sys/types.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -080011#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012
San Mehat39274412009-10-27 11:53:22 -070013#include <cutils/sched_policy.h>
14
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015static char *nexttoksep(char **strp, char *sep)
16{
17 char *p = strsep(strp,sep);
18 return (p == 0) ? "" : p;
19}
20static char *nexttok(char **strp)
21{
22 return nexttoksep(strp, " ");
23}
24
25#define SHOW_PRIO 1
26#define SHOW_TIME 2
San Mehat39274412009-10-27 11:53:22 -070027#define SHOW_POLICY 4
Dmitry Shmidt8b37c912010-08-18 17:26:26 -070028#define SHOW_CPU 8
Stephen Smalley8290d102012-01-13 08:53:56 -050029#define SHOW_MACLABEL 16
Marco Nelissen377cb2a2013-10-25 08:13:46 -070030#define SHOW_NUMERIC_UID 32
Kenny Root7c015852014-05-14 17:29:21 -070031#define SHOW_ABI 64
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Chris Dearman08b34662015-03-09 18:36:23 -070033#if __LP64__
34#define PC_WIDTH 10 /* Realistically, the top bits will be 0, so don't waste space. */
35#else
36#define PC_WIDTH (2*sizeof(uintptr_t))
37#endif
38
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039static int display_flags = 0;
Elliott Hughesb5fc3132015-01-29 17:20:47 -080040static int ppid_filter = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Kenny Root8f197e62014-05-14 15:07:08 -070042static void print_exe_abi(int pid);
43
Elliott Hughes5dc119a2015-10-30 15:03:30 -070044static int ps_line(int pid, int tid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045{
46 char statline[1024];
47 char cmdline[1024];
Stephen Smalley8290d102012-01-13 08:53:56 -050048 char macline[1024];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 char user[32];
50 struct stat stats;
Elliott Hughes5dc119a2015-10-30 15:03:30 -070051 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052 char *ptr, *name, *state;
Mark Salyzynaa907762014-05-08 09:31:43 -070053 int ppid;
Chris Dearman08b34662015-03-09 18:36:23 -070054 unsigned rss, vss;
55 uintptr_t eip;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 unsigned utime, stime;
Dmitry Shmidt8b37c912010-08-18 17:26:26 -070057 int prio, nice, rtprio, sched, psr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058 struct passwd *pw;
Marco Nelissen377cb2a2013-10-25 08:13:46 -070059
George Burgess IVe7aa2b22016-03-02 14:02:55 -080060 snprintf(statline, sizeof(statline), "/proc/%d", tid ? tid : pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 stat(statline, &stats);
62
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -070063 if (tid) {
George Burgess IVe7aa2b22016-03-02 14:02:55 -080064 snprintf(statline, sizeof(statline), "/proc/%d/task/%d/stat", pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065 cmdline[0] = 0;
Stephen Smalley8290d102012-01-13 08:53:56 -050066 snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 } else {
George Burgess IVe7aa2b22016-03-02 14:02:55 -080068 snprintf(statline, sizeof(statline), "/proc/%d/stat", pid);
69 snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
Stephen Smalley8290d102012-01-13 08:53:56 -050070 snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
Elliott Hughes5dc119a2015-10-30 15:03:30 -070071 int fd = open(cmdline, O_RDONLY);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -070072 if (fd == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 r = 0;
74 } else {
75 r = read(fd, cmdline, 1023);
76 close(fd);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -070077 if (r < 0) r = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078 }
79 cmdline[r] = 0;
80 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -070081
Elliott Hughes5dc119a2015-10-30 15:03:30 -070082 int fd = open(statline, O_RDONLY);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -070083 if (fd == 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 r = read(fd, statline, 1023);
85 close(fd);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -070086 if (r < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 statline[r] = 0;
88
89 ptr = statline;
90 nexttok(&ptr); // skip pid
91 ptr++; // skip "("
92
93 name = ptr;
94 ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',
95 *ptr++ = '\0'; // and null-terminate name.
96
97 ptr++; // skip " "
98 state = nexttok(&ptr);
99 ppid = atoi(nexttok(&ptr));
100 nexttok(&ptr); // pgrp
101 nexttok(&ptr); // sid
Mark Salyzynaa907762014-05-08 09:31:43 -0700102 nexttok(&ptr); // tty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 nexttok(&ptr); // tpgid
104 nexttok(&ptr); // flags
105 nexttok(&ptr); // minflt
106 nexttok(&ptr); // cminflt
107 nexttok(&ptr); // majflt
108 nexttok(&ptr); // cmajflt
109#if 1
110 utime = atoi(nexttok(&ptr));
111 stime = atoi(nexttok(&ptr));
112#else
113 nexttok(&ptr); // utime
114 nexttok(&ptr); // stime
115#endif
116 nexttok(&ptr); // cutime
117 nexttok(&ptr); // cstime
118 prio = atoi(nexttok(&ptr));
119 nice = atoi(nexttok(&ptr));
120 nexttok(&ptr); // threads
121 nexttok(&ptr); // itrealvalue
122 nexttok(&ptr); // starttime
123 vss = strtoul(nexttok(&ptr), 0, 10); // vsize
124 rss = strtoul(nexttok(&ptr), 0, 10); // rss
125 nexttok(&ptr); // rlim
126 nexttok(&ptr); // startcode
127 nexttok(&ptr); // endcode
128 nexttok(&ptr); // startstack
129 nexttok(&ptr); // kstkesp
130 eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip
131 nexttok(&ptr); // signal
132 nexttok(&ptr); // blocked
133 nexttok(&ptr); // sigignore
134 nexttok(&ptr); // sigcatch
Chris Dearman08b34662015-03-09 18:36:23 -0700135 nexttok(&ptr); // wchan
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 nexttok(&ptr); // nswap
137 nexttok(&ptr); // cnswap
138 nexttok(&ptr); // exit signal
Dmitry Shmidt8b37c912010-08-18 17:26:26 -0700139 psr = atoi(nexttok(&ptr)); // processor
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 rtprio = atoi(nexttok(&ptr)); // rt_priority
141 sched = atoi(nexttok(&ptr)); // scheduling policy
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700142
Mark Salyzynaa907762014-05-08 09:31:43 -0700143 nexttok(&ptr); // tty
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700144
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700145 if (tid != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 ppid = pid;
147 pid = tid;
148 }
149
150 pw = getpwuid(stats.st_uid);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700151 if (pw == 0 || (display_flags & SHOW_NUMERIC_UID)) {
152 snprintf(user, sizeof(user), "%d", (int)stats.st_uid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 } else {
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700154 strcpy(user, pw->pw_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700156
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700157 if (ppid_filter != 0 && ppid != ppid_filter) {
Elliott Hughesb5fc3132015-01-29 17:20:47 -0800158 return 0;
159 }
160
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700161 if (display_flags & SHOW_MACLABEL) {
162 fd = open(macline, O_RDONLY);
163 strcpy(macline, "-");
164 if (fd >= 0) {
165 r = read(fd, macline, sizeof(macline)-1);
166 close(fd);
167 if (r > 0)
168 macline[r] = 0;
Stephen Smalley8290d102012-01-13 08:53:56 -0500169 }
Elliott Hughesd2e26c02015-10-30 15:16:45 -0700170 printf("%-30s ", macline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 }
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700172
173 printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
174 if (display_flags & SHOW_CPU)
175 printf(" %-2d", psr);
176 if (display_flags & SHOW_PRIO)
177 printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
178 if (display_flags & SHOW_POLICY) {
179 SchedPolicy p;
180 if (get_sched_policy(pid, &p) < 0)
181 printf(" un ");
182 else
183 printf(" %.2s ", get_sched_policy_name(p));
184 }
185 char path[PATH_MAX];
186 snprintf(path, sizeof(path), "/proc/%d/wchan", pid);
187 char wchan[10];
188 fd = open(path, O_RDONLY);
189 ssize_t wchan_len = read(fd, wchan, sizeof(wchan));
190 if (wchan_len == -1) {
191 wchan[wchan_len = 0] = '\0';
192 }
193 close(fd);
194 printf(" %10.*s %0*" PRIxPTR " %s ", (int) wchan_len, wchan, (int) PC_WIDTH, eip, state);
195 if (display_flags & SHOW_ABI) {
196 print_exe_abi(pid);
197 }
198 printf("%s", cmdline[0] ? cmdline : name);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700199 if (display_flags & SHOW_TIME)
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700200 printf(" (u:%d, s:%d)", utime, stime);
201
202 printf("\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 return 0;
204}
205
Kenny Root8f197e62014-05-14 15:07:08 -0700206static void print_exe_abi(int pid)
207{
208 int fd, r;
209 char exeline[1024];
210
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800211 snprintf(exeline, sizeof(exeline), "/proc/%d/exe", pid);
Kenny Root8f197e62014-05-14 15:07:08 -0700212 fd = open(exeline, O_RDONLY);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700213 if (fd == 0) {
Kenny Root8f197e62014-05-14 15:07:08 -0700214 printf(" ");
215 return;
216 }
217 r = read(fd, exeline, 5 /* 4 byte ELFMAG + 1 byte EI_CLASS */);
218 close(fd);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700219 if (r < 0) {
Kenny Root8f197e62014-05-14 15:07:08 -0700220 printf(" ");
221 return;
222 }
223 if (memcmp("\177ELF", exeline, 4) != 0) {
224 printf("?? ");
225 return;
226 }
227 switch (exeline[4]) {
228 case 1:
229 printf("32 ");
230 return;
231 case 2:
232 printf("64 ");
233 return;
234 default:
235 printf("?? ");
236 return;
237 }
238}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700240void ps_threads(int pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241{
242 char tmp[128];
243 DIR *d;
244 struct dirent *de;
245
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800246 snprintf(tmp,sizeof(tmp),"/proc/%d/task",pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 d = opendir(tmp);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700248 if (d == 0) return;
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700249
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700250 while ((de = readdir(d)) != 0) {
251 if (isdigit(de->d_name[0])) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 int tid = atoi(de->d_name);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700253 if (tid == pid) continue;
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700254 ps_line(pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 }
256 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700257 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
260int ps_main(int argc, char **argv)
261{
262 DIR *d;
263 struct dirent *de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 int pidfilter = 0;
265 int threads = 0;
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700266
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700267 while (argc > 1) {
268 if (!strcmp(argv[1], "-t")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 threads = 1;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700270 } else if (!strcmp(argv[1], "-n")) {
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700271 display_flags |= SHOW_NUMERIC_UID;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700272 } else if (!strcmp(argv[1], "-x")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 display_flags |= SHOW_TIME;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700274 } else if (!strcmp(argv[1], "-Z")) {
Stephen Smalley8290d102012-01-13 08:53:56 -0500275 display_flags |= SHOW_MACLABEL;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700276 } else if (!strcmp(argv[1], "-P")) {
San Mehat39274412009-10-27 11:53:22 -0700277 display_flags |= SHOW_POLICY;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700278 } else if (!strcmp(argv[1], "-p")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 display_flags |= SHOW_PRIO;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700280 } else if (!strcmp(argv[1], "-c")) {
Dmitry Shmidt8b37c912010-08-18 17:26:26 -0700281 display_flags |= SHOW_CPU;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700282 } else if (!strcmp(argv[1], "--abi")) {
Kenny Root8f197e62014-05-14 15:07:08 -0700283 display_flags |= SHOW_ABI;
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700284 } else if (!strcmp(argv[1], "--ppid")) {
Jorge Lucangeli Obesc5e7f732016-04-25 15:53:09 -0700285 if (argc < 3) {
286 /* Bug 26554285: Use printf because some apps require at least
287 * one line of output to stdout even for errors.
288 */
289 printf("no ppid\n");
290 return 1;
291 }
Elliott Hughesb5fc3132015-01-29 17:20:47 -0800292 ppid_filter = atoi(argv[2]);
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700293 if (ppid_filter == 0) {
Christopher Ferris78e08062016-01-28 16:08:08 -0800294 /* Bug 26554285: Use printf because some apps require at least
295 * one line of output to stdout even for errors.
296 */
297 printf("bad ppid '%s'\n", argv[2]);
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700298 return 1;
299 }
Elliott Hughesb5fc3132015-01-29 17:20:47 -0800300 argc--;
301 argv++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 } else {
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700303 pidfilter = atoi(argv[1]);
304 if (pidfilter == 0) {
Christopher Ferris78e08062016-01-28 16:08:08 -0800305 /* Bug 26554285: Use printf because some apps require at least
306 * one line of output to stdout even for errors.
307 */
308 printf("bad pid '%s'\n", argv[1]);
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700309 return 1;
310 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 }
312 argc--;
313 argv++;
314 }
315
Stephen Smalley8290d102012-01-13 08:53:56 -0500316 if (display_flags & SHOW_MACLABEL) {
Elliott Hughesd2e26c02015-10-30 15:16:45 -0700317 printf("LABEL ");
Stephen Smalley8290d102012-01-13 08:53:56 -0500318 }
Elliott Hughesd2e26c02015-10-30 15:16:45 -0700319 printf("USER PID PPID VSIZE RSS %s%s %sWCHAN %*s %sNAME\n",
320 (display_flags&SHOW_CPU)?"CPU ":"",
321 (display_flags&SHOW_PRIO)?"PRIO NICE RTPRI SCHED ":"",
322 (display_flags&SHOW_POLICY)?"PCY " : "",
323 (int) PC_WIDTH, "PC",
324 (display_flags&SHOW_ABI)?"ABI " : "");
325
Christopher Ferris78e08062016-01-28 16:08:08 -0800326 d = opendir("/proc");
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700327 if (d == 0) return -1;
Christopher Ferris78e08062016-01-28 16:08:08 -0800328
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700329 while ((de = readdir(d)) != 0) {
330 if (isdigit(de->d_name[0])) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 int pid = atoi(de->d_name);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700332 if (!pidfilter || (pidfilter == pid)) {
Elliott Hughes5dc119a2015-10-30 15:03:30 -0700333 ps_line(pid, 0);
Jorge Lucangeli Obes5c1b3af2016-04-25 15:38:05 -0700334 if (threads) ps_threads(pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 }
336 }
337 }
338 closedir(d);
339 return 0;
340}