Some miscellanous "ps" fixes and improvements

guy at gorodish.UUCP guy at gorodish.UUCP
Fri Jan 30 10:20:26 AEST 1987


> 	5) It might be useful to print the time a process started in at
> 	   least one of the formats; again, this suggestion and the code
> 	   implementing this (printing the start date/time in the "u" format)
> 	   came from Rick Adams.

Except that *his* code actually *set* "ap->a_start".  Here's a
corrected version of the "diff"s.

*** /arch/4.3/usr/src/bin/ps.c	Thu May  8 12:49:00 1986
--- ps.c	Thu Jan 29 16:19:24 1987
***************
*** 91,96 ****
--- 91,98 ----
  #define X_CMAP		27
  	"_buffers",
  #define X_BUFFERS	28
+ 	"_boottime",
+ #define X_BOOTTIME	29
  	""
  };
  
***************
*** 117,122 ****
--- 119,125 ----
  	dev_t	a_ttyd;
  	time_t	a_cpu;
  	size_t	a_maxrss;
+ 	struct timeval a_start;
  };
  
  char	*lhdr;
***************
*** 143,148 ****
--- 146,153 ----
  struct	proc proc[NPROC];		/* a few, for less syscalls */
  struct	proc *mproc;
  struct	text *text;
+ struct	timeval	boottime;
+ time_t	now;
  
  union {
  	struct	user user;
***************
*** 157,163 ****
  #endif
  
  int	chkpid = -1;
! int	aflg, cflg, eflg, gflg, kflg, lflg, nflg, sflg,
  	uflg, vflg, xflg, Uflg;
  int	nchans;				/* total # of wait channels */
  char	*tptr;
--- 162,168 ----
  #endif
  
  int	chkpid = -1;
! int	aflg, cflg, eflg, gflg, kflg, lflg, nflg, rflg, sflg,
  	uflg, vflg, xflg, Uflg;
  int	nchans;				/* total # of wait channels */
  char	*tptr;
***************
*** 224,229 ****
--- 229,249 ----
  	"calimit",
  	NULL
  };
+ /*
+  * names listed here get mapped -- this is because only a guru will
+  * necessarily know that something waiting on "selwait" is waiting
+  * for a select to finish
+  */
+ struct wchan_map {
+ 	char	*map_from;
+ 	char	*map_to;
+ } wchan_map_list[] = {
+ 	{ "proc", "child" },
+ 	{ "u", "pause" },
+ 	{ "selwait", "select" },
+ 	{ "mbutl", "socket" },
+ 	{ NULL, NULL },
+ };
  
  int	npr;
  
***************
*** 289,294 ****
--- 309,317 ----
  		case 'n':
  			nflg++;
  			break;
+ 		case 'r':
+ 			rflg++;
+ 			break;
  		case 's':
  			sflg++;
  			break;
***************
*** 338,347 ****
--- 361,373 ----
  	openfiles(argc, argv);
  	getkvars(argc, argv);
  	uid = getuid();
+ 	(void) time(&now);
  	printhdr();
  	procp = getw(nl[X_PROC].n_value);
  	nproc = getw(nl[X_NPROC].n_value);
  	savcom = (struct savcom *)calloc((unsigned) nproc, sizeof (*savcom));
+ 	klseek(kmem, (long)nl[X_BOOTTIME].n_value, 0);
+ 	read(kmem, &boottime, sizeof (boottime));
  	for (i=0; i<nproc; i += NPROC) {
  		klseek(kmem, (long)procp, 0);
  		j = nproc - i;
***************
*** 374,379 ****
--- 400,408 ----
  				     mproc->p_stat == SSTOP))
  				continue;
  			}
+ 			if (rflg && !(mproc->p_stat == SRUN
+ 				|| mproc->p_pri < PZERO))
+ 				continue;
  			save();
  		}
  	}
***************
*** 413,419 ****
  
  	klseek(kmem, (long)loc, 0);
  	if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
! 		printf("error reading kmem at %x\n", loc);
  	return (word);
  }
  
--- 442,448 ----
  
  	klseek(kmem, (long)loc, 0);
  	if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
! 		printf("error reading %s at %x\n", kmemf, loc);
  	return (word);
  }
  
***************
*** 444,449 ****
--- 473,479 ----
  	setgid(getgid());
  	setuid(getuid());
  	if ((fp = fopen(psdb, "w")) == NULL) {
+ 		fprintf(stderr, "ps: ");
  		perror(psdb);
  		exit(1);
  	} else
***************
*** 482,487 ****
--- 512,518 ----
  	if ((fp = fopen(psdb, "r")) == NULL) {
  		if (errno == ENOENT)
  			return (0);
+ 		fprintf(stderr, "ps: ");
  		perror(psdb);
  		exit(1);
  	}
***************
*** 539,544 ****
--- 570,576 ----
  		kmemf = argc > 2 ? argv[2] : "/vmcore";
  	kmem = open(kmemf, 0);
  	if (kmem < 0) {
+ 		fprintf(stderr, "ps: ");
  		perror(kmemf);
  		exit(1);
  	}
***************
*** 549,554 ****
--- 581,587 ----
  		memf = "/dev/mem";
  		mem = open(memf, 0);
  		if (mem < 0) {
+ 			fprintf(stderr, "ps: ");
  			perror(memf);
  			exit(1);
  		}
***************
*** 557,562 ****
--- 590,596 ----
  		swapf = argc>3 ? argv[3]: "/dev/drum";
  		swap = open(swapf, 0);
  		if (swap < 0) {
+ 			fprintf(stderr, "ps: ");
  			perror(swapf);
  			exit(1);
  		}
***************
*** 588,594 ****
  	}
  
  	if (nl[0].n_type == 0) {
! 		fprintf(stderr, "%s: No namelist\n", nlistf);
  		exit(1);
  	}
  	if (kflg) {
--- 622,628 ----
  	}
  
  	if (nl[0].n_type == 0) {
! 		fprintf(stderr, "ps: %s: No namelist\n", nlistf);
  		exit(1);
  	}
  	if (kflg) {
***************
*** 599,605 ****
  		Sysmap = (struct pte *)
  			calloc((unsigned) Syssize, sizeof (struct pte));
  		if (Sysmap == NULL) {
! 			fprintf(stderr, "Out of space for Sysmap\n");
  			exit(1);
  		}
  		addr = (long) nl[X_SYSMAP].n_value;
--- 633,639 ----
  		Sysmap = (struct pte *)
  			calloc((unsigned) Syssize, sizeof (struct pte));
  		if (Sysmap == NULL) {
! 			fprintf(stderr, "ps: Out of space for Sysmap\n");
  			exit(1);
  		}
  		addr = (long) nl[X_SYSMAP].n_value;
***************
*** 636,642 ****
  		text = (struct text *)
  			calloc((unsigned) ntext, sizeof (struct text));
  		if (text == 0) {
! 			fprintf(stderr, "no room for text table\n");
  			exit(1);
  		}
  		atext = (struct text *)getw(nl[X_TEXT].n_value);
--- 670,676 ----
  		text = (struct text *)
  			calloc((unsigned) ntext, sizeof (struct text));
  		if (text == 0) {
! 			fprintf(stderr, "ps: no room for text table\n");
  			exit(1);
  		}
  		atext = (struct text *)getw(nl[X_TEXT].n_value);
***************
*** 743,754 ****
  	struct lttys *lt;
  
  	if (chdir("/dev") < 0) {
! 		perror("/dev");
  		exit(1);
  	}
  	dialbase = -1;
  	if ((df = opendir(".")) == NULL) {
! 		fprintf(stderr, "Can't open . in /dev\n");
  		exit(1);
  	}
  	while ((dbuf = readdir(df)) != NULL) 
--- 777,789 ----
  	struct lttys *lt;
  
  	if (chdir("/dev") < 0) {
! 		perror("ps: /dev");
  		exit(1);
  	}
  	dialbase = -1;
  	if ((df = opendir(".")) == NULL) {
! 		fprintf(stderr, "ps: ");
! 		perror("Can't open . in /dev");
  		exit(1);
  	}
  	while ((dbuf = readdir(df)) != NULL) 
***************
*** 907,914 ****
  	struct stat stb;
  	int x;
  
! 	if (u.u_ttyp == 0)
  		return("?");
  	x = u.u_ttyd & 017;
  	for (dp = &allttys[cand[x]]; dp != &allttys[-1];
  	     dp = &allttys[dp->cand]) {
--- 942,951 ----
  	struct stat stb;
  	int x;
  
! 	if (u.u_ttyp == 0) {
! 		u.u_ttyd = -1;
  		return("?");
+ 	}
  	x = u.u_ttyd & 017;
  	for (dp = &allttys[cand[x]]; dp != &allttys[-1];
  	     dp = &allttys[dp->cand]) {
***************
*** 950,964 ****
  	register struct text *xp;
  	char *ttyp, *cmdp;
  
! 	if (mproc->p_stat != SZOMB && getu() == 0)
! 		return;
! 	ttyp = gettty();
  	if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2))
  		return;
  	sp = &savcom[npr];
  	cmdp = getcmd();
- 	if (cmdp == 0)
- 		return;
  	sp->ap = ap = (struct asav *)calloc(1, sizeof (struct asav));
  	sp->ap->a_cmdp = cmdp;
  #define e(a,b) ap->a = mproc->b
--- 987,1002 ----
  	register struct text *xp;
  	char *ttyp, *cmdp;
  
! 	if (mproc->p_stat != SZOMB) {
! 		if (getu() == 0)
! 			return;
! 		ttyp = gettty();
! 	} else
! 		ttyp = "?";	/* zombies are not attached to terminals */
  	if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2))
  		return;
  	sp = &savcom[npr];
  	cmdp = getcmd();
  	sp->ap = ap = (struct asav *)calloc(1, sizeof (struct asav));
  	sp->ap->a_cmdp = cmdp;
  #define e(a,b) ap->a = mproc->b
***************
*** 982,987 ****
--- 1020,1026 ----
  			ap->a_txtrss = xp->x_rssize;
  			ap->a_xccount = xp->x_ccount;
  		}
+ 		ap->a_start = u.u_start;
  	}
  #undef e
  	ap->a_maxrss = mproc->p_maxrss;
***************
*** 1070,1076 ****
  			return (0);
  		(void) lseek(swap, (long)dtob(mproc->p_swaddr), 0);
  		if (read(swap, (char *)&user.user, size) != size) {
! 			fprintf(stderr, "ps: cant read u for pid %d from %s\n",
  			    mproc->p_pid, swapf);
  			return (0);
  		}
--- 1109,1115 ----
  			return (0);
  		(void) lseek(swap, (long)dtob(mproc->p_swaddr), 0);
  		if (read(swap, (char *)&user.user, size) != size) {
! 			fprintf(stderr, "ps: can't read u for pid %d from %s\n",
  			    mproc->p_pid, swapf);
  			return (0);
  		}
***************
*** 1081,1087 ****
  	pteaddr = &Usrptmap[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
  	klseek(kmem, (long)pteaddr, 0);
  	if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
! 		printf("ps: cant read indir pte to get u for pid %d from %s\n",
  		    mproc->p_pid, kmemf);
  		return (0);
  	}
--- 1120,1126 ----
  	pteaddr = &Usrptmap[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
  	klseek(kmem, (long)pteaddr, 0);
  	if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
! 		printf("ps: can't read indir pte to get u for pid %d from %s\n",
  		    mproc->p_pid, kmemf);
  		return (0);
  	}
***************
*** 1089,1095 ****
  	    (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
  		0);
  	if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
! 		printf("ps: cant read page table for u of pid %d from %s\n",
  		    mproc->p_pid, memf);
  		return (0);
  	}
--- 1128,1134 ----
  	    (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
  		0);
  	if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
! 		printf("ps: can't read page table for u of pid %d from %s\n",
  		    mproc->p_pid, memf);
  		return (0);
  	}
***************
*** 1103,1109 ****
  		i = ncl * CLSIZE;
  		lseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
  		if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
! 			printf("ps: cant read page %d of u of pid %d from %s\n",
  			    arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
  			return(0);
  		}
--- 1142,1148 ----
  		i = ncl * CLSIZE;
  		lseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
  		if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
! 			printf("ps: can't read page %d of u of pid %d from %s\n",
  			    arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
  			return(0);
  		}
***************
*** 1233,1243 ****
  }
  
  char	*uhdr =
! "%s   PID %%CPU %%MEM   SZ  RSS TT STAT  TIME";
  upr(sp)
  	struct savcom *sp;
  {
  	register struct asav *ap = sp->ap;
  	int vmsize, rmsize;
  
  	vmsize = pgtok((ap->a_size + ap->a_tsiz));
--- 1272,1283 ----
  }
  
  char	*uhdr =
! "%s   PID %%CPU %%MEM   SZ  RSS TT STAT START  TIME";
  upr(sp)
  	struct savcom *sp;
  {
  	register struct asav *ap = sp->ap;
+ 	char *cp;
  	int vmsize, rmsize;
  
  	vmsize = pgtok((ap->a_size + ap->a_tsiz));
***************
*** 1253,1258 ****
--- 1293,1305 ----
  	putchar(' ');
  	ptty(ap->a_tty);
  	printf(" %4.4s", state(ap));
+ 	if (ap->a_start.tv_sec == 0)
+ 		ap->a_start = boottime;
+ 	cp = ctime(&ap->a_start.tv_sec);
+ 	if ((now - ap->a_start.tv_sec) > 60*60*24)
+ 		printf("%.7s", cp+3);
+ 	else
+ 		printf("%.6s ", cp+10);
  	ptime(ap);
  }
  
***************
*** 1488,1504 ****
  	newloc = loc & ~0xc0000000;
  	p = btop(newloc);
  	if ((loc & 0xc0000000) == 0) {
! 		fprintf(stderr, "Vtophys: translating non-kernel address\n");
  		return((off_t) -1);
  	}
  	if (p >= Syssize) {
! 		fprintf(stderr, "Vtophys: page out of bound (%d>=%d)\n",
  			p, Syssize);
  		return((off_t) -1);
  	}
  	if (Sysmap[p].pg_v == 0
  	&& (Sysmap[p].pg_fod || Sysmap[p].pg_pfnum == 0)) {
! 		fprintf(stderr, "Vtophys: page not valid\n");
  		return((off_t) -1);
  	}
  	loc = (long) (ptob(Sysmap[p].pg_pfnum) + (loc & PGOFSET));
--- 1535,1551 ----
  	newloc = loc & ~0xc0000000;
  	p = btop(newloc);
  	if ((loc & 0xc0000000) == 0) {
! 		fprintf(stderr, "ps: Vtophys: translating non-kernel address\n");
  		return((off_t) -1);
  	}
  	if (p >= Syssize) {
! 		fprintf(stderr, "ps: Vtophys: page out of bound (%d>=%d)\n",
  			p, Syssize);
  		return((off_t) -1);
  	}
  	if (Sysmap[p].pg_v == 0
  	&& (Sysmap[p].pg_fod || Sysmap[p].pg_pfnum == 0)) {
! 		fprintf(stderr, "ps: Vtophys: page not valid\n");
  		return((off_t) -1);
  	}
  	loc = (long) (ptob(Sysmap[p].pg_pfnum) + (loc & PGOFSET));
***************
*** 1540,1546 ****
  	struct nlist *list;
  {
  	register struct nlist *p, *q;
- 	register char *s1, *s2;
  	register n, m;
  	int maxlen, nreq;
  	FILE *f;
--- 1587,1592 ----
***************
*** 1605,1612 ****
  			if (!nflg)
  				addchan(&nambuf[1], (caddr_t) q->n_value);
  			for (p = list; p->n_un.n_name && p->n_un.n_name[0]; p++) {
- 				s1 = p->n_un.n_name;
- 				s2 = nambuf;
  				if (strcmp(p->n_un.n_name, nambuf) == 0) {
  					p->n_value = q->n_value;
  					p->n_type = q->n_type;
--- 1651,1656 ----
***************
*** 1634,1639 ****
--- 1678,1684 ----
  	static int left = 0;
  	register struct wchan *wp;
  	register char **p;
+ 	register struct wchan_map *mp;
  
  	for (p = wchan_stop_list; *p; p++) {
  		if (**p != *name)	/* quick check first */
***************
*** 1640,1645 ****
--- 1685,1696 ----
  			continue;
  		if (strncmp(name, *p, WNAMESIZ) == 0)
  			return;		/* if found, don't add */
+ 	}
+ 	for (mp = wchan_map_list; mp->map_from; mp++) {
+ 		if (*(mp->map_from) != *name)	/* quick check first */
+ 			continue;
+ 		if (strncmp(name, mp->map_from, WNAMESIZ) == 0)
+ 			name = mp->map_to;	/* if found, remap */
  	}
  	if (left == 0) {
  		if (wchanhd) {



More information about the Comp.bugs.4bsd.ucb-fixes mailing list