2009-03-04 04:32:55 +01:00
|
|
|
#include <ctype.h>
|
2014-12-29 21:24:25 +01:00
|
|
|
#include <dirent.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <fcntl.h>
|
2015-03-10 05:10:10 +01:00
|
|
|
#include <inttypes.h>
|
2014-12-29 21:24:25 +01:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-12-29 21:24:25 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-10-27 19:53:22 +01:00
|
|
|
#include <cutils/sched_policy.h>
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static char *nexttoksep(char **strp, char *sep)
|
|
|
|
{
|
|
|
|
char *p = strsep(strp,sep);
|
|
|
|
return (p == 0) ? "" : p;
|
|
|
|
}
|
|
|
|
static char *nexttok(char **strp)
|
|
|
|
{
|
|
|
|
return nexttoksep(strp, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SHOW_PRIO 1
|
|
|
|
#define SHOW_TIME 2
|
2009-10-27 19:53:22 +01:00
|
|
|
#define SHOW_POLICY 4
|
2010-08-19 02:26:26 +02:00
|
|
|
#define SHOW_CPU 8
|
2012-01-13 14:53:56 +01:00
|
|
|
#define SHOW_MACLABEL 16
|
2013-10-25 17:13:46 +02:00
|
|
|
#define SHOW_NUMERIC_UID 32
|
2014-05-15 02:29:21 +02:00
|
|
|
#define SHOW_ABI 64
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-03-10 02:36:23 +01:00
|
|
|
#if __LP64__
|
|
|
|
#define PC_WIDTH 10 /* Realistically, the top bits will be 0, so don't waste space. */
|
|
|
|
#else
|
|
|
|
#define PC_WIDTH (2*sizeof(uintptr_t))
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static int display_flags = 0;
|
2015-01-30 02:20:47 +01:00
|
|
|
static int ppid_filter = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-05-15 00:07:08 +02:00
|
|
|
static void print_exe_abi(int pid);
|
|
|
|
|
2015-10-30 23:03:30 +01:00
|
|
|
static int ps_line(int pid, int tid)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
char statline[1024];
|
|
|
|
char cmdline[1024];
|
2012-01-13 14:53:56 +01:00
|
|
|
char macline[1024];
|
2009-03-04 04:32:55 +01:00
|
|
|
char user[32];
|
|
|
|
struct stat stats;
|
2015-10-30 23:03:30 +01:00
|
|
|
int r;
|
2009-03-04 04:32:55 +01:00
|
|
|
char *ptr, *name, *state;
|
2014-05-08 18:31:43 +02:00
|
|
|
int ppid;
|
2015-03-10 02:36:23 +01:00
|
|
|
unsigned rss, vss;
|
|
|
|
uintptr_t eip;
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned utime, stime;
|
2010-08-19 02:26:26 +02:00
|
|
|
int prio, nice, rtprio, sched, psr;
|
2009-03-04 04:32:55 +01:00
|
|
|
struct passwd *pw;
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
sprintf(statline, "/proc/%d", pid);
|
|
|
|
stat(statline, &stats);
|
|
|
|
|
|
|
|
if(tid) {
|
|
|
|
sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);
|
|
|
|
cmdline[0] = 0;
|
2012-01-13 14:53:56 +01:00
|
|
|
snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
sprintf(statline, "/proc/%d/stat", pid);
|
2012-01-13 14:53:56 +01:00
|
|
|
sprintf(cmdline, "/proc/%d/cmdline", pid);
|
|
|
|
snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
|
2015-10-30 23:03:30 +01:00
|
|
|
int fd = open(cmdline, O_RDONLY);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(fd == 0) {
|
|
|
|
r = 0;
|
|
|
|
} else {
|
|
|
|
r = read(fd, cmdline, 1023);
|
|
|
|
close(fd);
|
|
|
|
if(r < 0) r = 0;
|
|
|
|
}
|
|
|
|
cmdline[r] = 0;
|
|
|
|
}
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2015-10-30 23:03:30 +01:00
|
|
|
int fd = open(statline, O_RDONLY);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(fd == 0) return -1;
|
|
|
|
r = read(fd, statline, 1023);
|
|
|
|
close(fd);
|
|
|
|
if(r < 0) return -1;
|
|
|
|
statline[r] = 0;
|
|
|
|
|
|
|
|
ptr = statline;
|
|
|
|
nexttok(&ptr); // skip pid
|
|
|
|
ptr++; // skip "("
|
|
|
|
|
|
|
|
name = ptr;
|
|
|
|
ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',
|
|
|
|
*ptr++ = '\0'; // and null-terminate name.
|
|
|
|
|
|
|
|
ptr++; // skip " "
|
|
|
|
state = nexttok(&ptr);
|
|
|
|
ppid = atoi(nexttok(&ptr));
|
|
|
|
nexttok(&ptr); // pgrp
|
|
|
|
nexttok(&ptr); // sid
|
2014-05-08 18:31:43 +02:00
|
|
|
nexttok(&ptr); // tty
|
2009-03-04 04:32:55 +01:00
|
|
|
nexttok(&ptr); // tpgid
|
|
|
|
nexttok(&ptr); // flags
|
|
|
|
nexttok(&ptr); // minflt
|
|
|
|
nexttok(&ptr); // cminflt
|
|
|
|
nexttok(&ptr); // majflt
|
|
|
|
nexttok(&ptr); // cmajflt
|
|
|
|
#if 1
|
|
|
|
utime = atoi(nexttok(&ptr));
|
|
|
|
stime = atoi(nexttok(&ptr));
|
|
|
|
#else
|
|
|
|
nexttok(&ptr); // utime
|
|
|
|
nexttok(&ptr); // stime
|
|
|
|
#endif
|
|
|
|
nexttok(&ptr); // cutime
|
|
|
|
nexttok(&ptr); // cstime
|
|
|
|
prio = atoi(nexttok(&ptr));
|
|
|
|
nice = atoi(nexttok(&ptr));
|
|
|
|
nexttok(&ptr); // threads
|
|
|
|
nexttok(&ptr); // itrealvalue
|
|
|
|
nexttok(&ptr); // starttime
|
|
|
|
vss = strtoul(nexttok(&ptr), 0, 10); // vsize
|
|
|
|
rss = strtoul(nexttok(&ptr), 0, 10); // rss
|
|
|
|
nexttok(&ptr); // rlim
|
|
|
|
nexttok(&ptr); // startcode
|
|
|
|
nexttok(&ptr); // endcode
|
|
|
|
nexttok(&ptr); // startstack
|
|
|
|
nexttok(&ptr); // kstkesp
|
|
|
|
eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip
|
|
|
|
nexttok(&ptr); // signal
|
|
|
|
nexttok(&ptr); // blocked
|
|
|
|
nexttok(&ptr); // sigignore
|
|
|
|
nexttok(&ptr); // sigcatch
|
2015-03-10 02:36:23 +01:00
|
|
|
nexttok(&ptr); // wchan
|
2009-03-04 04:32:55 +01:00
|
|
|
nexttok(&ptr); // nswap
|
|
|
|
nexttok(&ptr); // cnswap
|
|
|
|
nexttok(&ptr); // exit signal
|
2010-08-19 02:26:26 +02:00
|
|
|
psr = atoi(nexttok(&ptr)); // processor
|
2009-03-04 04:32:55 +01:00
|
|
|
rtprio = atoi(nexttok(&ptr)); // rt_priority
|
|
|
|
sched = atoi(nexttok(&ptr)); // scheduling policy
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2014-05-08 18:31:43 +02:00
|
|
|
nexttok(&ptr); // tty
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(tid != 0) {
|
|
|
|
ppid = pid;
|
|
|
|
pid = tid;
|
|
|
|
}
|
|
|
|
|
|
|
|
pw = getpwuid(stats.st_uid);
|
2013-10-25 17:13:46 +02:00
|
|
|
if(pw == 0 || (display_flags & SHOW_NUMERIC_UID)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
sprintf(user,"%d",(int)stats.st_uid);
|
|
|
|
} else {
|
|
|
|
strcpy(user,pw->pw_name);
|
|
|
|
}
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2015-01-30 02:20:47 +01:00
|
|
|
if(ppid_filter != 0 && ppid != ppid_filter) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 23:03:30 +01:00
|
|
|
if (display_flags & SHOW_MACLABEL) {
|
|
|
|
fd = open(macline, O_RDONLY);
|
|
|
|
strcpy(macline, "-");
|
|
|
|
if (fd >= 0) {
|
|
|
|
r = read(fd, macline, sizeof(macline)-1);
|
|
|
|
close(fd);
|
|
|
|
if (r > 0)
|
|
|
|
macline[r] = 0;
|
2014-05-15 00:07:08 +02:00
|
|
|
}
|
2015-10-30 23:16:45 +01:00
|
|
|
printf("%-30s ", macline);
|
2015-10-30 23:03:30 +01:00
|
|
|
}
|
2009-10-27 19:53:22 +01:00
|
|
|
|
2015-10-30 23:03:30 +01:00
|
|
|
printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
|
|
|
|
if (display_flags & SHOW_CPU)
|
|
|
|
printf(" %-2d", psr);
|
|
|
|
if (display_flags & SHOW_PRIO)
|
|
|
|
printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
|
|
|
|
if (display_flags & SHOW_POLICY) {
|
|
|
|
SchedPolicy p;
|
|
|
|
if (get_sched_policy(pid, &p) < 0)
|
|
|
|
printf(" un ");
|
|
|
|
else
|
|
|
|
printf(" %.2s ", get_sched_policy_name(p));
|
|
|
|
}
|
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "/proc/%d/wchan", pid);
|
|
|
|
char wchan[10];
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
ssize_t wchan_len = read(fd, wchan, sizeof(wchan));
|
|
|
|
if (wchan_len == -1) {
|
|
|
|
wchan[wchan_len = 0] = '\0';
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-10-30 23:03:30 +01:00
|
|
|
close(fd);
|
|
|
|
printf(" %10.*s %0*" PRIxPTR " %s ", (int) wchan_len, wchan, (int) PC_WIDTH, eip, state);
|
|
|
|
if (display_flags & SHOW_ABI) {
|
|
|
|
print_exe_abi(pid);
|
|
|
|
}
|
|
|
|
printf("%s", cmdline[0] ? cmdline : name);
|
|
|
|
if(display_flags&SHOW_TIME)
|
|
|
|
printf(" (u:%d, s:%d)", utime, stime);
|
|
|
|
|
|
|
|
printf("\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:07:08 +02:00
|
|
|
static void print_exe_abi(int pid)
|
|
|
|
{
|
|
|
|
int fd, r;
|
|
|
|
char exeline[1024];
|
|
|
|
|
|
|
|
sprintf(exeline, "/proc/%d/exe", pid);
|
|
|
|
fd = open(exeline, O_RDONLY);
|
|
|
|
if(fd == 0) {
|
|
|
|
printf(" ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r = read(fd, exeline, 5 /* 4 byte ELFMAG + 1 byte EI_CLASS */);
|
|
|
|
close(fd);
|
|
|
|
if(r < 0) {
|
|
|
|
printf(" ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp("\177ELF", exeline, 4) != 0) {
|
|
|
|
printf("?? ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (exeline[4]) {
|
|
|
|
case 1:
|
|
|
|
printf("32 ");
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
printf("64 ");
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
printf("?? ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-10-30 23:03:30 +01:00
|
|
|
void ps_threads(int pid)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
char tmp[128];
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
sprintf(tmp,"/proc/%d/task",pid);
|
|
|
|
d = opendir(tmp);
|
|
|
|
if(d == 0) return;
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
while((de = readdir(d)) != 0){
|
|
|
|
if(isdigit(de->d_name[0])){
|
|
|
|
int tid = atoi(de->d_name);
|
|
|
|
if(tid == pid) continue;
|
2015-10-30 23:03:30 +01:00
|
|
|
ps_line(pid, tid);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:13:46 +02:00
|
|
|
closedir(d);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ps_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
int pidfilter = 0;
|
|
|
|
int threads = 0;
|
2013-10-25 17:13:46 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
d = opendir("/proc");
|
|
|
|
if(d == 0) return -1;
|
|
|
|
|
|
|
|
while(argc > 1){
|
|
|
|
if(!strcmp(argv[1],"-t")) {
|
|
|
|
threads = 1;
|
2013-10-25 17:13:46 +02:00
|
|
|
} else if(!strcmp(argv[1],"-n")) {
|
|
|
|
display_flags |= SHOW_NUMERIC_UID;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(argv[1],"-x")) {
|
|
|
|
display_flags |= SHOW_TIME;
|
2012-01-13 14:53:56 +01:00
|
|
|
} else if(!strcmp(argv[1], "-Z")) {
|
|
|
|
display_flags |= SHOW_MACLABEL;
|
2009-10-27 19:53:22 +01:00
|
|
|
} else if(!strcmp(argv[1],"-P")) {
|
|
|
|
display_flags |= SHOW_POLICY;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(argv[1],"-p")) {
|
|
|
|
display_flags |= SHOW_PRIO;
|
2010-08-19 02:26:26 +02:00
|
|
|
} else if(!strcmp(argv[1],"-c")) {
|
|
|
|
display_flags |= SHOW_CPU;
|
2014-05-15 00:07:08 +02:00
|
|
|
} else if(!strcmp(argv[1],"--abi")) {
|
|
|
|
display_flags |= SHOW_ABI;
|
2015-01-30 02:20:47 +01:00
|
|
|
} else if(!strcmp(argv[1],"--ppid")) {
|
|
|
|
ppid_filter = atoi(argv[2]);
|
2015-10-30 23:03:30 +01:00
|
|
|
if (ppid_filter == 0) {
|
|
|
|
fprintf(stderr, "bad ppid '%s'\n", argv[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-30 02:20:47 +01:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-10-30 23:03:30 +01:00
|
|
|
pidfilter = atoi(argv[1]);
|
|
|
|
if (pidfilter == 0) {
|
|
|
|
fprintf(stderr, "bad pid '%s'\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2012-01-13 14:53:56 +01:00
|
|
|
if (display_flags & SHOW_MACLABEL) {
|
2015-10-30 23:16:45 +01:00
|
|
|
printf("LABEL ");
|
2012-01-13 14:53:56 +01:00
|
|
|
}
|
2015-10-30 23:16:45 +01:00
|
|
|
printf("USER PID PPID VSIZE RSS %s%s %sWCHAN %*s %sNAME\n",
|
|
|
|
(display_flags&SHOW_CPU)?"CPU ":"",
|
|
|
|
(display_flags&SHOW_PRIO)?"PRIO NICE RTPRI SCHED ":"",
|
|
|
|
(display_flags&SHOW_POLICY)?"PCY " : "",
|
|
|
|
(int) PC_WIDTH, "PC",
|
|
|
|
(display_flags&SHOW_ABI)?"ABI " : "");
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
while((de = readdir(d)) != 0){
|
|
|
|
if(isdigit(de->d_name[0])){
|
|
|
|
int pid = atoi(de->d_name);
|
|
|
|
if(!pidfilter || (pidfilter == pid)) {
|
2015-10-30 23:03:30 +01:00
|
|
|
ps_line(pid, 0);
|
|
|
|
if(threads) ps_threads(pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|