Commit | Line | Data |
---|---|---|
3d9f6a35 | 1 | #ifndef lint |
e8807835 | 2 | static char *sccsid = "@(#)ps.c 4.27 (Berkeley) %G%"; |
3d9f6a35 KM |
3 | #endif |
4 | ||
aafc2e2f | 5 | /* |
3d9f6a35 | 6 | * ps |
aafc2e2f | 7 | */ |
aafc2e2f BJ |
8 | #include <stdio.h> |
9 | #include <ctype.h> | |
10 | #include <nlist.h> | |
11 | #include <pwd.h> | |
12 | #include <sys/param.h> | |
13 | #include <sys/tty.h> | |
b38b1b3a | 14 | #include <sys/dir.h> |
aafc2e2f BJ |
15 | #include <sys/user.h> |
16 | #include <sys/proc.h> | |
0bc29a7d | 17 | #include <machine/pte.h> |
aafc2e2f BJ |
18 | #include <sys/vm.h> |
19 | #include <sys/text.h> | |
20 | #include <sys/stat.h> | |
066ec2ef | 21 | #include <sys/mbuf.h> |
7f0fa372 | 22 | #include <math.h> |
e8807835 | 23 | #include <errno.h> |
aafc2e2f BJ |
24 | |
25 | struct nlist nl[] = { | |
26 | { "_proc" }, | |
27 | #define X_PROC 0 | |
28 | { "_Usrptmap" }, | |
e8807835 | 29 | #define X_USRPTMAP 1 |
aafc2e2f BJ |
30 | { "_usrpt" }, |
31 | #define X_USRPT 2 | |
32 | { "_text" }, | |
33 | #define X_TEXT 3 | |
34 | { "_nswap" }, | |
35 | #define X_NSWAP 4 | |
c67277e8 BJ |
36 | { "_maxslp" }, |
37 | #define X_MAXSLP 5 | |
7f0fa372 BJ |
38 | { "_ccpu" }, |
39 | #define X_CCPU 6 | |
0f0e0052 BJ |
40 | { "_ecmx" }, |
41 | #define X_ECMX 7 | |
bdb1f95f BJ |
42 | { "_nproc" }, |
43 | #define X_NPROC 8 | |
44 | { "_ntext" }, | |
45 | #define X_NTEXT 9 | |
b38b1b3a SL |
46 | { "_dmmin" }, |
47 | #define X_DMMIN 10 | |
48 | { "_dmmax" }, | |
49 | #define X_DMMAX 11 | |
e8807835 RC |
50 | { "_Sysmap" }, |
51 | #define X_SYSMAP 12 | |
52 | { "_Syssize" }, | |
53 | #define X_SYSSIZE 13 | |
066ec2ef | 54 | { "" }, |
aafc2e2f BJ |
55 | }; |
56 | ||
57 | struct savcom { | |
58 | union { | |
59 | struct lsav *lp; | |
60 | float u_pctcpu; | |
61 | struct vsav *vp; | |
62 | int s_ssiz; | |
066ec2ef | 63 | } s_un; |
aafc2e2f | 64 | struct asav *ap; |
bdb1f95f | 65 | } *savcom; |
aafc2e2f BJ |
66 | |
67 | struct asav { | |
68 | char *a_cmdp; | |
69 | int a_flag; | |
c67277e8 | 70 | short a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time; |
0f0e0052 BJ |
71 | size_t a_size, a_rss, a_tsiz, a_txtrss; |
72 | short a_xccount; | |
3d9f6a35 | 73 | char a_tty[MAXNAMLEN+1]; |
aafc2e2f | 74 | dev_t a_ttyd; |
c67277e8 | 75 | time_t a_cpu; |
79de1ed3 | 76 | size_t a_maxrss; |
aafc2e2f BJ |
77 | }; |
78 | ||
79 | char *lhdr; | |
aafc2e2f BJ |
80 | struct lsav { |
81 | short l_ppid; | |
c67277e8 | 82 | char l_cpu; |
aafc2e2f BJ |
83 | int l_addr; |
84 | caddr_t l_wchan; | |
85 | }; | |
86 | ||
87 | char *uhdr; | |
aafc2e2f | 88 | char *shdr; |
aafc2e2f BJ |
89 | |
90 | char *vhdr; | |
aafc2e2f | 91 | struct vsav { |
0f0e0052 BJ |
92 | u_int v_majflt; |
93 | size_t v_swrss, v_txtswrss; | |
94 | float v_pctcpu; | |
aafc2e2f BJ |
95 | }; |
96 | ||
e8807835 RC |
97 | #define NPROC 16 |
98 | ||
99 | struct proc proc[NPROC]; /* a few, for less syscalls */ | |
aafc2e2f BJ |
100 | struct proc *mproc; |
101 | struct text *text; | |
102 | ||
103 | union { | |
104 | struct user user; | |
105 | char upages[UPAGES][NBPG]; | |
106 | } user; | |
107 | #define u user.user | |
108 | ||
e8807835 RC |
109 | #ifndef PSFILE |
110 | char *psdb = "/etc/psdatabase"; | |
111 | #else | |
112 | char *psdb = PSFILE; | |
113 | #endif | |
aafc2e2f BJ |
114 | |
115 | int chkpid; | |
066ec2ef | 116 | int aflg, cflg, eflg, gflg, kflg, lflg, sflg, |
e8807835 | 117 | uflg, vflg, xflg, Uflg; |
aafc2e2f | 118 | char *tptr; |
e8807835 | 119 | char *gettty(), *getcmd(), *getname(), *savestr(), *state(); |
3d9f6a35 | 120 | char *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat(); |
e8807835 | 121 | char *strncpy(), *index(), *ttyname(), mytty[16]; |
3d9f6a35 | 122 | long lseek(); |
e8807835 | 123 | off_t vtophys(); |
0f0e0052 | 124 | double pcpu(), pmem(); |
aafc2e2f | 125 | int pscomp(); |
c67277e8 | 126 | int nswap, maxslp; |
bdb1f95f | 127 | struct text *atext; |
7f0fa372 | 128 | double ccpu; |
0f0e0052 | 129 | int ecmx; |
e8807835 | 130 | struct pte *Usrptmap, *usrpt; |
066ec2ef | 131 | int nproc, ntext; |
b38b1b3a | 132 | int dmmin, dmmax; |
e8807835 RC |
133 | struct pte *Sysmap; |
134 | int Syssize; | |
135 | ||
136 | #ifndef MAXTTYS | |
137 | #define MAXTTYS 256 | |
138 | #endif | |
139 | ||
140 | int nttys; | |
aafc2e2f BJ |
141 | |
142 | struct ttys { | |
aafc2e2f | 143 | dev_t ttyd; |
aafc2e2f | 144 | struct ttys *cand; |
e8807835 RC |
145 | char name[MAXNAMLEN+1]; |
146 | } allttys[MAXTTYS], *cand[16]; | |
aafc2e2f | 147 | |
aafc2e2f BJ |
148 | int npr; |
149 | ||
150 | int cmdstart; | |
151 | int twidth; | |
152 | char *kmemf, *memf, *swapf, *nlistf; | |
25659ede | 153 | int kmem, mem, swap = -1; |
1fb6a666 | 154 | int rawcpu, sumcpu; |
aafc2e2f BJ |
155 | |
156 | int pcbpf; | |
157 | int argaddr; | |
158 | extern char _sobuf[]; | |
159 | ||
72db1f59 | 160 | #define pgtok(a) ((a)/(1024/NBPG)) |
066ec2ef | 161 | |
aafc2e2f BJ |
162 | main(argc, argv) |
163 | char **argv; | |
164 | { | |
165 | register int i, j; | |
166 | register char *ap; | |
167 | int uid; | |
168 | off_t procp; | |
169 | ||
aafc2e2f | 170 | twidth = 80; |
aafc2e2f BJ |
171 | argc--, argv++; |
172 | if (argc > 0) { | |
173 | ap = argv[0]; | |
174 | while (*ap) switch (*ap++) { | |
175 | ||
0f0e0052 | 176 | case 'C': |
066ec2ef | 177 | rawcpu++; |
0f0e0052 | 178 | break; |
1fb6a666 BJ |
179 | case 'S': |
180 | sumcpu++; | |
181 | break; | |
e8807835 RC |
182 | |
183 | case 'U': | |
184 | Uflg++; | |
185 | break; | |
186 | ||
aafc2e2f BJ |
187 | case 'a': |
188 | aflg++; | |
189 | break; | |
190 | case 'c': | |
066ec2ef | 191 | cflg = !cflg; |
aafc2e2f BJ |
192 | break; |
193 | case 'e': | |
066ec2ef | 194 | eflg++; |
aafc2e2f BJ |
195 | break; |
196 | case 'g': | |
066ec2ef | 197 | gflg++; |
aafc2e2f BJ |
198 | break; |
199 | case 'k': | |
066ec2ef | 200 | kflg++; |
aafc2e2f BJ |
201 | break; |
202 | case 'l': | |
066ec2ef | 203 | lflg++; |
aafc2e2f BJ |
204 | break; |
205 | case 's': | |
066ec2ef | 206 | sflg++; |
aafc2e2f BJ |
207 | break; |
208 | case 't': | |
209 | if (*ap) | |
210 | tptr = ap; | |
e13b2716 CL |
211 | else if ((tptr = ttyname(2)) != 0) { |
212 | strcpy(mytty, tptr); | |
213 | if ((tptr = index(mytty,'y')) != 0) | |
214 | tptr++; | |
215 | } | |
066ec2ef | 216 | aflg++; |
aafc2e2f | 217 | gflg++; |
066ec2ef | 218 | if (tptr && *tptr == '?') |
aafc2e2f BJ |
219 | xflg++; |
220 | while (*ap) | |
221 | ap++; | |
222 | break; | |
133a0b3b | 223 | case 'u': |
066ec2ef | 224 | uflg++; |
aafc2e2f BJ |
225 | break; |
226 | case 'v': | |
227 | cflg = 1; | |
066ec2ef | 228 | vflg++; |
aafc2e2f BJ |
229 | break; |
230 | case 'w': | |
231 | if (twidth == 80) | |
232 | twidth = 132; | |
233 | else | |
234 | twidth = BUFSIZ; | |
235 | break; | |
236 | case 'x': | |
237 | xflg++; | |
238 | break; | |
239 | default: | |
240 | if (!isdigit(ap[-1])) | |
241 | break; | |
242 | chkpid = atoi(--ap); | |
243 | *ap = 0; | |
066ec2ef | 244 | aflg++; |
aafc2e2f BJ |
245 | xflg++; |
246 | break; | |
247 | } | |
248 | } | |
249 | openfiles(argc, argv); | |
250 | getkvars(argc, argv); | |
aafc2e2f BJ |
251 | uid = getuid(); |
252 | printhdr(); | |
bdb1f95f BJ |
253 | procp = getw(nl[X_PROC].n_value); |
254 | nproc = getw(nl[X_NPROC].n_value); | |
e8807835 RC |
255 | savcom = (struct savcom *)calloc((unsigned) nproc, sizeof (*savcom)); |
256 | for (i=0; i<nproc; i += NPROC) { | |
3d9f6a35 | 257 | klseek(kmem, (long)procp, 0); |
bdb1f95f | 258 | j = nproc - i; |
e8807835 RC |
259 | if (j > NPROC) |
260 | j = NPROC; | |
aafc2e2f | 261 | j *= sizeof (struct proc); |
25659ede | 262 | if (read(kmem, (char *)proc, j) != j) { |
aafc2e2f | 263 | cantread("proc table", kmemf); |
25659ede BJ |
264 | exit(1); |
265 | } | |
aafc2e2f BJ |
266 | procp += j; |
267 | for (j = j / sizeof (struct proc) - 1; j >= 0; j--) { | |
268 | mproc = &proc[j]; | |
269 | if (mproc->p_stat == 0 || | |
270 | mproc->p_pgrp == 0 && xflg == 0) | |
c67277e8 | 271 | continue; |
aafc2e2f | 272 | if (tptr == 0 && gflg == 0 && xflg == 0 && |
6e515893 | 273 | mproc->p_ppid == 1) |
aafc2e2f | 274 | continue; |
e8807835 RC |
275 | if (uid != mproc->p_uid && aflg==0) |
276 | continue; | |
277 | if (chkpid != 0 && chkpid != mproc->p_pid) | |
aafc2e2f BJ |
278 | continue; |
279 | if (vflg && gflg == 0 && xflg == 0) { | |
280 | if (mproc->p_stat == SZOMB || | |
281 | mproc->p_flag&SWEXIT) | |
282 | continue; | |
283 | if (mproc->p_slptime > MAXSLP && | |
284 | (mproc->p_stat == SSLEEP || | |
285 | mproc->p_stat == SSTOP)) | |
286 | continue; | |
287 | } | |
288 | save(); | |
289 | } | |
290 | } | |
e8807835 | 291 | qsort((char *) savcom, npr, sizeof(savcom[0]), pscomp); |
aafc2e2f BJ |
292 | for (i=0; i<npr; i++) { |
293 | register struct savcom *sp = &savcom[i]; | |
294 | if (lflg) | |
295 | lpr(sp); | |
296 | else if (vflg) | |
297 | vpr(sp); | |
298 | else if (uflg) | |
299 | upr(sp); | |
300 | else | |
301 | spr(sp); | |
302 | if (sp->ap->a_flag & SWEXIT) | |
303 | printf(" <exiting>"); | |
304 | else if (sp->ap->a_stat == SZOMB) | |
305 | printf(" <defunct>"); | |
306 | else if (sp->ap->a_pid == 0) | |
307 | printf(" swapper"); | |
308 | else if (sp->ap->a_pid == 2) | |
309 | printf(" pagedaemon"); | |
d4e18729 | 310 | else if (sp->ap->a_pid == 3 && sp->ap->a_flag & SSYS) |
3bbec3da | 311 | printf(" ip input"); |
aafc2e2f BJ |
312 | else |
313 | printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp); | |
314 | printf("\n"); | |
315 | } | |
316 | exit(npr == 0); | |
317 | } | |
318 | ||
bdb1f95f | 319 | getw(loc) |
3d9f6a35 | 320 | unsigned long loc; |
bdb1f95f | 321 | { |
e8807835 | 322 | int word; |
bdb1f95f | 323 | |
3d9f6a35 KM |
324 | klseek(kmem, (long)loc, 0); |
325 | if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word)) | |
bdb1f95f BJ |
326 | printf("error reading kmem at %x\n", loc); |
327 | return (word); | |
328 | } | |
329 | ||
6e515893 | 330 | klseek(fd, loc, off) |
3d9f6a35 KM |
331 | int fd; |
332 | long loc; | |
333 | int off; | |
6e515893 | 334 | { |
e8807835 RC |
335 | if (kflg) { |
336 | if ((loc = vtophys(loc)) == -1) | |
337 | return; | |
338 | } | |
3d9f6a35 | 339 | (void) lseek(fd, (long)loc, off); |
6e515893 BJ |
340 | } |
341 | ||
e8807835 RC |
342 | writepsdb(unixname) |
343 | char *unixname; | |
344 | { | |
345 | int nllen; | |
346 | register FILE *fp; | |
347 | ||
348 | setuid(getuid()); | |
349 | if ((fp = fopen(psdb, "w")) == NULL) { | |
350 | perror(psdb); | |
351 | exit(1); | |
352 | } else | |
353 | fchmod(fileno(fp), 0644); | |
354 | nllen = sizeof nl / sizeof (struct nlist); | |
355 | fwrite((char *) &nllen, sizeof nllen, 1, fp); | |
356 | fwrite((char *) nl, sizeof (struct nlist), nllen, fp); | |
357 | fwrite((char *) &nttys, sizeof nttys, 1, fp); | |
358 | fwrite((char *) allttys, sizeof (struct ttys), nttys, fp); | |
359 | fwrite(unixname, strlen(unixname) + 1, 1, fp); | |
360 | fclose(fp); | |
361 | } | |
362 | ||
363 | readpsdb(unixname) | |
364 | char *unixname; | |
365 | { | |
366 | int nllen; | |
367 | register i; | |
368 | register FILE *fp; | |
369 | char unamebuf[BUFSIZ]; | |
370 | char *p = unamebuf; | |
371 | extern int errno; | |
372 | ||
373 | if ((fp = fopen(psdb, "r")) == NULL) { | |
374 | if (errno == ENOENT) | |
375 | return (0); | |
376 | perror(psdb); | |
377 | exit(1); | |
378 | } | |
379 | ||
380 | fread((char *) &nllen, sizeof nllen, 1, fp); | |
381 | fread((char *) nl, sizeof (struct nlist), nllen, fp); | |
382 | fread((char *) &nttys, sizeof nttys, 1, fp); | |
383 | fread((char *) allttys, sizeof (struct ttys), nttys, fp); | |
384 | while ((*p = getc(fp)) != '\0') | |
385 | p++; | |
386 | return (strcmp(unixname, unamebuf) == 0); | |
387 | } | |
388 | ||
aafc2e2f BJ |
389 | openfiles(argc, argv) |
390 | char **argv; | |
391 | { | |
392 | ||
25659ede | 393 | kmemf = "/dev/kmem"; |
aafc2e2f | 394 | if (kflg) |
25659ede | 395 | kmemf = argc > 2 ? argv[2] : "/vmcore"; |
aafc2e2f BJ |
396 | kmem = open(kmemf, 0); |
397 | if (kmem < 0) { | |
398 | perror(kmemf); | |
399 | exit(1); | |
400 | } | |
401 | if (kflg) { | |
402 | mem = kmem; | |
403 | memf = kmemf; | |
404 | } else { | |
25659ede | 405 | memf = "/dev/mem"; |
aafc2e2f BJ |
406 | mem = open(memf, 0); |
407 | if (mem < 0) { | |
408 | perror(memf); | |
409 | exit(1); | |
410 | } | |
411 | } | |
25659ede BJ |
412 | if (kflg == 0 || argc > 3) { |
413 | swapf = argc>3 ? argv[3]: "/dev/drum"; | |
414 | swap = open(swapf, 0); | |
415 | if (swap < 0) { | |
416 | perror(swapf); | |
417 | exit(1); | |
418 | } | |
aafc2e2f BJ |
419 | } |
420 | } | |
421 | ||
422 | getkvars(argc, argv) | |
423 | char **argv; | |
424 | { | |
aafc2e2f | 425 | |
25659ede | 426 | nlistf = argc > 1 ? argv[1] : "/vmunix"; |
e8807835 RC |
427 | if (Uflg) { |
428 | nlist(nlistf, nl); | |
429 | getdev(); | |
430 | writepsdb(nlistf); | |
431 | exit (0); | |
432 | } else if (!readpsdb(nlistf)) { | |
433 | if (!kflg) | |
434 | nl[X_SYSMAP].n_name = ""; | |
435 | nlist(nlistf, nl); | |
436 | getdev(); | |
437 | } | |
438 | ||
aafc2e2f BJ |
439 | if (nl[0].n_type == 0) { |
440 | fprintf(stderr, "%s: No namelist\n", nlistf); | |
441 | exit(1); | |
442 | } | |
e8807835 RC |
443 | if (kflg) { |
444 | /* We must do the sys map first because klseek uses it */ | |
445 | long addr; | |
446 | ||
447 | Syssize = nl[X_SYSSIZE].n_value; | |
448 | Sysmap = (struct pte *) | |
449 | calloc((unsigned) Syssize, sizeof (struct pte)); | |
450 | if (Sysmap == NULL) { | |
451 | fprintf(stderr, "Out of space for Sysmap\n"); | |
452 | exit(1); | |
453 | } | |
454 | addr = (long) nl[X_SYSMAP].n_value; | |
455 | addr &= ~0x80000000; | |
456 | (void) lseek(kmem, addr, 0); | |
457 | read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte)); | |
458 | } | |
459 | usrpt = (struct pte *)nl[X_USRPT].n_value; | |
460 | Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value; | |
6e515893 | 461 | klseek(kmem, (long)nl[X_NSWAP].n_value, 0); |
3d9f6a35 | 462 | if (read(kmem, (char *)&nswap, sizeof (nswap)) != sizeof (nswap)) { |
aafc2e2f BJ |
463 | cantread("nswap", kmemf); |
464 | exit(1); | |
465 | } | |
6e515893 | 466 | klseek(kmem, (long)nl[X_MAXSLP].n_value, 0); |
3d9f6a35 | 467 | if (read(kmem, (char *)&maxslp, sizeof (maxslp)) != sizeof (maxslp)) { |
c67277e8 BJ |
468 | cantread("maxslp", kmemf); |
469 | exit(1); | |
470 | } | |
6e515893 | 471 | klseek(kmem, (long)nl[X_CCPU].n_value, 0); |
3d9f6a35 | 472 | if (read(kmem, (char *)&ccpu, sizeof (ccpu)) != sizeof (ccpu)) { |
7f0fa372 BJ |
473 | cantread("ccpu", kmemf); |
474 | exit(1); | |
475 | } | |
6e515893 | 476 | klseek(kmem, (long)nl[X_ECMX].n_value, 0); |
3d9f6a35 | 477 | if (read(kmem, (char *)&ecmx, sizeof (ecmx)) != sizeof (ecmx)) { |
0f0e0052 BJ |
478 | cantread("ecmx", kmemf); |
479 | exit(1); | |
480 | } | |
481 | if (uflg || vflg) { | |
bdb1f95f | 482 | ntext = getw(nl[X_NTEXT].n_value); |
e8807835 RC |
483 | text = (struct text *) |
484 | calloc((unsigned) ntext, sizeof (struct text)); | |
aafc2e2f BJ |
485 | if (text == 0) { |
486 | fprintf(stderr, "no room for text table\n"); | |
487 | exit(1); | |
488 | } | |
bdb1f95f | 489 | atext = (struct text *)getw(nl[X_TEXT].n_value); |
3d9f6a35 | 490 | klseek(kmem, (long)atext, 0); |
bdb1f95f BJ |
491 | if (read(kmem, (char *)text, ntext * sizeof (struct text)) |
492 | != ntext * sizeof (struct text)) { | |
aafc2e2f BJ |
493 | cantread("text table", kmemf); |
494 | exit(1); | |
495 | } | |
496 | } | |
b38b1b3a SL |
497 | dmmin = getw(nl[X_DMMIN].n_value); |
498 | dmmax = getw(nl[X_DMMAX].n_value); | |
aafc2e2f BJ |
499 | } |
500 | ||
501 | printhdr() | |
502 | { | |
503 | char *hdr; | |
504 | ||
505 | if (sflg+lflg+vflg+uflg > 1) { | |
506 | fprintf(stderr, "ps: specify only one of s,l,v and u\n"); | |
507 | exit(1); | |
508 | } | |
066ec2ef | 509 | hdr = lflg ? lhdr : |
133a0b3b | 510 | (vflg ? vhdr : |
066ec2ef | 511 | (uflg ? uhdr : shdr)); |
aafc2e2f | 512 | if (lflg+vflg+uflg+sflg == 0) |
c67277e8 | 513 | hdr += strlen("SSIZ "); |
aafc2e2f BJ |
514 | cmdstart = strlen(hdr); |
515 | printf("%s COMMAND\n", hdr); | |
3d9f6a35 | 516 | (void) fflush(stdout); |
aafc2e2f BJ |
517 | } |
518 | ||
519 | cantread(what, fromwhat) | |
520 | char *what, *fromwhat; | |
521 | { | |
522 | ||
25659ede | 523 | fprintf(stderr, "ps: error reading %s from %s\n", what, fromwhat); |
aafc2e2f BJ |
524 | } |
525 | ||
3d9f6a35 | 526 | struct direct *dbuf; |
aafc2e2f BJ |
527 | int dialbase; |
528 | ||
529 | getdev() | |
530 | { | |
3d9f6a35 | 531 | register DIR *df; |
aafc2e2f | 532 | |
e8807835 RC |
533 | if (chdir("/dev") < 0) { |
534 | perror("/dev"); | |
535 | exit(1); | |
536 | } | |
aafc2e2f | 537 | dialbase = -1; |
3d9f6a35 | 538 | if ((df = opendir(".")) == NULL) { |
aafc2e2f BJ |
539 | fprintf(stderr, "Can't open . in /dev\n"); |
540 | exit(1); | |
541 | } | |
3d9f6a35 KM |
542 | while ((dbuf = readdir(df)) != NULL) |
543 | maybetty(); | |
544 | closedir(df); | |
aafc2e2f BJ |
545 | } |
546 | ||
547 | /* | |
548 | * Attempt to avoid stats by guessing minor device | |
549 | * numbers from tty names. Console is known, | |
550 | * know that r(hp|up|mt) are unlikely as are different mem's, | |
551 | * floppy, null, tty, etc. | |
552 | */ | |
553 | maybetty() | |
554 | { | |
3d9f6a35 | 555 | register char *cp = dbuf->d_name; |
aafc2e2f BJ |
556 | register struct ttys *dp; |
557 | int x; | |
558 | struct stat stb; | |
559 | ||
560 | switch (cp[0]) { | |
561 | ||
562 | case 'c': | |
563 | if (!strcmp(cp, "console")) { | |
564 | x = 0; | |
565 | goto donecand; | |
566 | } | |
567 | /* cu[la]? are possible!?! don't rule them out */ | |
568 | break; | |
569 | ||
570 | case 'd': | |
571 | if (!strcmp(cp, "drum")) | |
3d9f6a35 | 572 | return; |
aafc2e2f BJ |
573 | break; |
574 | ||
575 | case 'f': | |
576 | if (!strcmp(cp, "floppy")) | |
3d9f6a35 | 577 | return; |
aafc2e2f BJ |
578 | break; |
579 | ||
580 | case 'k': | |
581 | cp++; | |
582 | if (*cp == 'U') | |
583 | cp++; | |
584 | goto trymem; | |
585 | ||
586 | case 'r': | |
587 | cp++; | |
588 | if (*cp == 'r' || *cp == 'u' || *cp == 'h') | |
589 | cp++; | |
590 | #define is(a,b) cp[0] == 'a' && cp[1] == 'b' | |
591 | if (is(r,p) || is(u,p) || is(r,k) || is(r,m) || is(m,t)) { | |
592 | cp += 2; | |
593 | if (isdigit(*cp) && cp[2] == 0) | |
3d9f6a35 | 594 | return; |
aafc2e2f BJ |
595 | } |
596 | break; | |
597 | ||
598 | case 'm': | |
599 | trymem: | |
600 | if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0) | |
3d9f6a35 | 601 | return; |
3bbec3da | 602 | if (cp[0] == 'm' && cp[1] == 't') |
3d9f6a35 | 603 | return; |
aafc2e2f BJ |
604 | break; |
605 | ||
606 | case 'n': | |
607 | if (!strcmp(cp, "null")) | |
3d9f6a35 | 608 | return; |
aafc2e2f BJ |
609 | break; |
610 | ||
611 | case 'v': | |
612 | if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) && | |
613 | cp[3] == 0) | |
3d9f6a35 | 614 | return; |
aafc2e2f BJ |
615 | break; |
616 | } | |
3d9f6a35 | 617 | cp = dbuf->d_name + dbuf->d_namlen - 1; |
aafc2e2f BJ |
618 | x = 0; |
619 | if (cp[-1] == 'd') { | |
620 | if (dialbase == -1) { | |
621 | if (stat("ttyd0", &stb) == 0) | |
622 | dialbase = stb.st_rdev & 017; | |
623 | else | |
624 | dialbase = -2; | |
625 | } | |
626 | if (dialbase == -2) | |
627 | x = 0; | |
628 | else | |
629 | x = 11; | |
630 | } | |
3d9f6a35 | 631 | if (cp > dbuf->d_name && isdigit(cp[-1]) && isdigit(*cp)) |
aafc2e2f BJ |
632 | x += 10 * (cp[-1] - ' ') + cp[0] - '0'; |
633 | else if (*cp >= 'a' && *cp <= 'f') | |
634 | x += 10 + *cp - 'a'; | |
635 | else if (isdigit(*cp)) | |
636 | x += *cp - '0'; | |
637 | else | |
638 | x = -1; | |
639 | donecand: | |
e8807835 RC |
640 | if (nttys >= MAXTTYS) { |
641 | fprintf(stderr, "ps: tty table overflow\n"); | |
642 | exit(1); | |
643 | } | |
644 | dp = &allttys[nttys++]; | |
3d9f6a35 | 645 | (void) strcpy(dp->name, dbuf->d_name); |
e8807835 RC |
646 | if (Uflg) { |
647 | if (stat(dp->name, &stb) == 0 && | |
648 | (stb.st_mode&S_IFMT)==S_IFCHR) | |
649 | dp->ttyd = x = stb.st_rdev; | |
650 | else { | |
651 | nttys--; | |
652 | return; | |
653 | } | |
654 | } else | |
655 | dp->ttyd = -1; | |
aafc2e2f BJ |
656 | if (x == -1) |
657 | return; | |
658 | x &= 017; | |
659 | dp->cand = cand[x]; | |
660 | cand[x] = dp; | |
661 | } | |
662 | ||
663 | char * | |
664 | gettty() | |
665 | { | |
666 | register char *p; | |
667 | register struct ttys *dp; | |
668 | struct stat stb; | |
669 | int x; | |
670 | ||
671 | if (u.u_ttyp == 0) | |
672 | return("?"); | |
673 | x = u.u_ttyd & 017; | |
674 | for (dp = cand[x]; dp; dp = dp->cand) { | |
675 | if (dp->ttyd == -1) { | |
c67277e8 BJ |
676 | if (stat(dp->name, &stb) == 0 && |
677 | (stb.st_mode&S_IFMT)==S_IFCHR) | |
aafc2e2f BJ |
678 | dp->ttyd = stb.st_rdev; |
679 | else | |
680 | dp->ttyd = -2; | |
681 | } | |
682 | if (dp->ttyd == u.u_ttyd) | |
683 | goto found; | |
684 | } | |
685 | /* ick */ | |
e8807835 | 686 | for (dp = allttys; dp < &allttys[nttys]; dp++) { |
aafc2e2f | 687 | if (dp->ttyd == -1) { |
3bbec3da BJ |
688 | if (stat(dp->name, &stb) == 0 && |
689 | (stb.st_mode&S_IFMT)==S_IFCHR) | |
aafc2e2f BJ |
690 | dp->ttyd = stb.st_rdev; |
691 | else | |
692 | dp->ttyd = -2; | |
693 | } | |
694 | if (dp->ttyd == u.u_ttyd) | |
695 | goto found; | |
696 | } | |
697 | return ("?"); | |
698 | found: | |
699 | p = dp->name; | |
700 | if (p[0]=='t' && p[1]=='t' && p[2]=='y') | |
701 | p += 3; | |
702 | return (p); | |
703 | } | |
704 | ||
705 | save() | |
706 | { | |
707 | register struct savcom *sp; | |
708 | register struct asav *ap; | |
709 | register char *cp; | |
0f0e0052 | 710 | register struct text *xp; |
aafc2e2f BJ |
711 | char *ttyp, *cmdp; |
712 | ||
713 | if (mproc->p_stat != SZOMB && getu() == 0) | |
714 | return; | |
715 | ttyp = gettty(); | |
133a0b3b | 716 | if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2)) |
aafc2e2f BJ |
717 | return; |
718 | sp = &savcom[npr]; | |
719 | cmdp = getcmd(); | |
720 | if (cmdp == 0) | |
721 | return; | |
e8807835 | 722 | sp->ap = ap = (struct asav *)calloc(1, sizeof (struct asav)); |
aafc2e2f BJ |
723 | sp->ap->a_cmdp = cmdp; |
724 | #define e(a,b) ap->a = mproc->b | |
725 | e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice); | |
0f0e0052 | 726 | e(a_uid, p_uid); e(a_pid, p_pid); e(a_pri, p_pri); |
c67277e8 | 727 | e(a_slptime, p_slptime); e(a_time, p_time); |
aafc2e2f BJ |
728 | ap->a_tty[0] = ttyp[0]; |
729 | ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' '; | |
730 | if (ap->a_stat == SZOMB) { | |
b1198826 | 731 | ap->a_cpu = 0; |
aafc2e2f BJ |
732 | } else { |
733 | ap->a_size = mproc->p_dsize + mproc->p_ssize; | |
0f0e0052 | 734 | e(a_rss, p_rssize); |
aafc2e2f | 735 | ap->a_ttyd = u.u_ttyd; |
b1198826 | 736 | ap->a_cpu = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec; |
1fb6a666 | 737 | if (sumcpu) |
b1198826 | 738 | ap->a_cpu += u.u_cru.ru_utime.tv_sec + u.u_cru.ru_stime.tv_sec; |
b2867a35 | 739 | if (mproc->p_textp && text) { |
bdb1f95f | 740 | xp = &text[mproc->p_textp - atext]; |
0f0e0052 BJ |
741 | ap->a_tsiz = xp->x_size; |
742 | ap->a_txtrss = xp->x_rssize; | |
743 | ap->a_xccount = xp->x_ccount; | |
744 | } | |
aafc2e2f | 745 | } |
0f0e0052 | 746 | #undef e |
79de1ed3 | 747 | ap->a_maxrss = mproc->p_maxrss; |
aafc2e2f BJ |
748 | if (lflg) { |
749 | register struct lsav *lp; | |
750 | ||
e8807835 RC |
751 | sp->s_un.lp = lp = (struct lsav *) |
752 | calloc(1, sizeof (struct lsav)); | |
aafc2e2f | 753 | #define e(a,b) lp->a = mproc->b |
c67277e8 | 754 | e(l_ppid, p_ppid); e(l_cpu, p_cpu); |
aafc2e2f BJ |
755 | if (ap->a_stat != SZOMB) |
756 | e(l_wchan, p_wchan); | |
757 | #undef e | |
758 | lp->l_addr = pcbpf; | |
759 | } else if (vflg) { | |
760 | register struct vsav *vp; | |
aafc2e2f | 761 | |
e8807835 RC |
762 | sp->s_un.vp = vp = (struct vsav *) |
763 | calloc(1, sizeof (struct vsav)); | |
aafc2e2f | 764 | #define e(a,b) vp->a = mproc->b |
aafc2e2f | 765 | if (ap->a_stat != SZOMB) { |
7f0fa372 | 766 | e(v_swrss, p_swrss); |
b1198826 | 767 | vp->v_majflt = u.u_ru.ru_majflt; |
0f0e0052 | 768 | if (mproc->p_textp) |
aafc2e2f | 769 | vp->v_txtswrss = xp->x_swrss; |
aafc2e2f | 770 | } |
7f0fa372 | 771 | vp->v_pctcpu = pcpu(); |
aafc2e2f | 772 | #undef e |
066ec2ef SL |
773 | } else if (uflg) |
774 | sp->s_un.u_pctcpu = pcpu(); | |
775 | else if (sflg) { | |
aafc2e2f BJ |
776 | if (ap->a_stat != SZOMB) { |
777 | for (cp = (char *)u.u_stack; | |
bdb1f95f | 778 | cp < &user.upages[UPAGES][0]; ) |
aafc2e2f BJ |
779 | if (*cp++) |
780 | break; | |
066ec2ef | 781 | sp->s_un.s_ssiz = (&user.upages[UPAGES][0] - cp); |
aafc2e2f BJ |
782 | } |
783 | } | |
133a0b3b | 784 | |
aafc2e2f BJ |
785 | npr++; |
786 | } | |
787 | ||
0f0e0052 BJ |
788 | double |
789 | pmem(ap) | |
790 | register struct asav *ap; | |
791 | { | |
792 | double fracmem; | |
793 | int szptudot; | |
794 | ||
795 | if ((ap->a_flag&SLOAD) == 0) | |
796 | fracmem = 0.0; | |
797 | else { | |
798 | szptudot = UPAGES + clrnd(ctopt(ap->a_size+ap->a_tsiz)); | |
799 | fracmem = ((float)ap->a_rss+szptudot)/CLSIZE/ecmx; | |
800 | if (ap->a_xccount) | |
801 | fracmem += ((float)ap->a_txtrss)/CLSIZE/ | |
802 | ap->a_xccount/ecmx; | |
803 | } | |
804 | return (100.0 * fracmem); | |
805 | } | |
806 | ||
7f0fa372 BJ |
807 | double |
808 | pcpu() | |
809 | { | |
0f0e0052 | 810 | time_t time; |
7f0fa372 | 811 | |
0f0e0052 BJ |
812 | time = mproc->p_time; |
813 | if (time == 0 || (mproc->p_flag&SLOAD) == 0) | |
7f0fa372 | 814 | return (0.0); |
0f0e0052 BJ |
815 | if (rawcpu) |
816 | return (100.0 * mproc->p_pctcpu); | |
817 | return (100.0 * mproc->p_pctcpu / (1.0 - exp(time * log(ccpu)))); | |
7f0fa372 BJ |
818 | } |
819 | ||
aafc2e2f BJ |
820 | getu() |
821 | { | |
f700a7ee | 822 | struct pte *pteaddr, apte; |
f700a7ee | 823 | struct pte arguutl[UPAGES+CLSIZE]; |
aafc2e2f BJ |
824 | register int i; |
825 | int ncl, size; | |
826 | ||
827 | size = sflg ? ctob(UPAGES) : sizeof (struct user); | |
828 | if ((mproc->p_flag & SLOAD) == 0) { | |
25659ede BJ |
829 | if (swap < 0) |
830 | return (0); | |
066ec2ef | 831 | (void) lseek(swap, (long)dtob(mproc->p_swaddr), 0); |
aafc2e2f BJ |
832 | if (read(swap, (char *)&user.user, size) != size) { |
833 | fprintf(stderr, "ps: cant read u for pid %d from %s\n", | |
834 | mproc->p_pid, swapf); | |
835 | return (0); | |
836 | } | |
837 | pcbpf = 0; | |
838 | argaddr = 0; | |
839 | return (1); | |
840 | } | |
e8807835 | 841 | pteaddr = &Usrptmap[btokmx(mproc->p_p0br) + mproc->p_szpt - 1]; |
3d9f6a35 | 842 | klseek(kmem, (long)pteaddr, 0); |
aafc2e2f BJ |
843 | if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) { |
844 | printf("ps: cant read indir pte to get u for pid %d from %s\n", | |
e8807835 | 845 | mproc->p_pid, kmemf); |
aafc2e2f BJ |
846 | return (0); |
847 | } | |
e8807835 | 848 | lseek(mem, |
3d9f6a35 KM |
849 | (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte), |
850 | 0); | |
aafc2e2f BJ |
851 | if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) { |
852 | printf("ps: cant read page table for u of pid %d from %s\n", | |
e8807835 | 853 | mproc->p_pid, memf); |
aafc2e2f BJ |
854 | return (0); |
855 | } | |
856 | if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum) | |
857 | argaddr = ctob(arguutl[0].pg_pfnum); | |
858 | else | |
859 | argaddr = 0; | |
860 | pcbpf = arguutl[CLSIZE].pg_pfnum; | |
861 | ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE); | |
862 | while (--ncl >= 0) { | |
863 | i = ncl * CLSIZE; | |
e8807835 | 864 | lseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), 0); |
aafc2e2f BJ |
865 | if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) { |
866 | printf("ps: cant read page %d of u of pid %d from %s\n", | |
867 | arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf); | |
868 | return(0); | |
869 | } | |
870 | } | |
871 | return (1); | |
872 | } | |
873 | ||
874 | char * | |
875 | getcmd() | |
876 | { | |
066ec2ef | 877 | char cmdbuf[CLSIZE*NBPG]; |
aafc2e2f BJ |
878 | union { |
879 | char argc[CLSIZE*NBPG]; | |
880 | int argi[CLSIZE*NBPG/sizeof (int)]; | |
881 | } argspac; | |
882 | register char *cp; | |
883 | register int *ip; | |
884 | char c; | |
885 | int nbad; | |
886 | struct dblock db; | |
25659ede | 887 | char *file; |
aafc2e2f BJ |
888 | |
889 | if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT)) | |
890 | return (""); | |
891 | if (cflg) { | |
3d9f6a35 | 892 | (void) strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm)); |
aafc2e2f BJ |
893 | return (savestr(cmdbuf)); |
894 | } | |
895 | if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) { | |
25659ede BJ |
896 | if (swap < 0) |
897 | goto retucomm; | |
aafc2e2f | 898 | vstodb(0, CLSIZE, &u.u_smap, &db, 1); |
066ec2ef | 899 | (void) lseek(swap, (long)dtob(db.db_base), 0); |
aafc2e2f BJ |
900 | if (read(swap, (char *)&argspac, sizeof(argspac)) |
901 | != sizeof(argspac)) | |
902 | goto bad; | |
25659ede | 903 | file = swapf; |
aafc2e2f | 904 | } else { |
e8807835 | 905 | lseek(mem, (long)argaddr, 0); |
aafc2e2f BJ |
906 | if (read(mem, (char *)&argspac, sizeof (argspac)) |
907 | != sizeof (argspac)) | |
908 | goto bad; | |
25659ede | 909 | file = memf; |
aafc2e2f BJ |
910 | } |
911 | ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)]; | |
912 | ip -= 2; /* last arg word and .long 0 */ | |
913 | while (*--ip) | |
914 | if (ip == argspac.argi) | |
915 | goto retucomm; | |
916 | *(char *)ip = ' '; | |
917 | ip++; | |
918 | nbad = 0; | |
919 | for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) { | |
920 | c = *cp & 0177; | |
921 | if (c == 0) | |
922 | *cp = ' '; | |
923 | else if (c < ' ' || c > 0176) { | |
924 | if (++nbad >= 5*(eflg+1)) { | |
925 | *cp++ = ' '; | |
926 | break; | |
927 | } | |
928 | *cp = '?'; | |
929 | } else if (eflg == 0 && c == '=') { | |
930 | while (*--cp != ' ') | |
931 | if (cp <= (char *)ip) | |
932 | break; | |
933 | break; | |
934 | } | |
935 | } | |
936 | *cp = 0; | |
937 | while (*--cp == ' ') | |
938 | *cp = 0; | |
939 | cp = (char *)ip; | |
3d9f6a35 | 940 | (void) strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp); |
aafc2e2f | 941 | if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') { |
3d9f6a35 KM |
942 | (void) strcat(cmdbuf, " ("); |
943 | (void) strncat(cmdbuf, u.u_comm, sizeof(u.u_comm)); | |
944 | (void) strcat(cmdbuf, ")"); | |
aafc2e2f | 945 | } |
aafc2e2f BJ |
946 | return (savestr(cmdbuf)); |
947 | ||
948 | bad: | |
25659ede BJ |
949 | fprintf(stderr, "ps: error locating command name for pid %d from %s\n", |
950 | mproc->p_pid, file); | |
aafc2e2f | 951 | retucomm: |
3d9f6a35 KM |
952 | (void) strcpy(cmdbuf, " ("); |
953 | (void) strncat(cmdbuf, u.u_comm, sizeof (u.u_comm)); | |
954 | (void) strcat(cmdbuf, ")"); | |
aafc2e2f BJ |
955 | return (savestr(cmdbuf)); |
956 | } | |
957 | ||
958 | char *lhdr = | |
066ec2ef | 959 | " F UID PID PPID CP PRI NI ADDR SZ RSS WCHAN STAT TT TIME"; |
aafc2e2f BJ |
960 | lpr(sp) |
961 | struct savcom *sp; | |
962 | { | |
963 | register struct asav *ap = sp->ap; | |
066ec2ef | 964 | register struct lsav *lp = sp->s_un.lp; |
aafc2e2f | 965 | |
066ec2ef | 966 | printf("%7x%4d%6u%6u%3d%4d%3d%5x%4d%5d", |
c67277e8 BJ |
967 | ap->a_flag, ap->a_uid, |
968 | ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO, | |
066ec2ef | 969 | ap->a_nice-NZERO, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss)); |
aafc2e2f | 970 | printf(lp->l_wchan ? " %5x" : " ", (int)lp->l_wchan&0xfffff); |
0f0e0052 | 971 | printf(" %4.4s ", state(ap)); |
aafc2e2f BJ |
972 | ptty(ap->a_tty); |
973 | ptime(ap); | |
974 | } | |
975 | ||
976 | ptty(tp) | |
977 | char *tp; | |
978 | { | |
979 | ||
c67277e8 | 980 | printf("%-2.2s", tp); |
aafc2e2f BJ |
981 | } |
982 | ||
983 | ptime(ap) | |
984 | struct asav *ap; | |
985 | { | |
986 | ||
b1198826 | 987 | printf("%3ld:%02ld", ap->a_cpu / 60, ap->a_cpu % 60); |
aafc2e2f BJ |
988 | } |
989 | ||
990 | char *uhdr = | |
0f0e0052 | 991 | "USER PID %CPU %MEM SZ RSS TT STAT TIME"; |
aafc2e2f BJ |
992 | upr(sp) |
993 | struct savcom *sp; | |
994 | { | |
995 | register struct asav *ap = sp->ap; | |
0f0e0052 BJ |
996 | int vmsize, rmsize; |
997 | ||
066ec2ef SL |
998 | vmsize = pgtok((ap->a_size + ap->a_tsiz)); |
999 | rmsize = pgtok(ap->a_rss); | |
0f0e0052 | 1000 | if (ap->a_xccount) |
066ec2ef | 1001 | rmsize += pgtok(ap->a_txtrss/ap->a_xccount); |
0f0e0052 | 1002 | printf("%-8.8s %5d%5.1f%5.1f%5d%5d", |
066ec2ef | 1003 | getname(ap->a_uid), ap->a_pid, sp->s_un.u_pctcpu, pmem(ap), |
0f0e0052 | 1004 | vmsize, rmsize); |
c67277e8 | 1005 | putchar(' '); |
aafc2e2f | 1006 | ptty(ap->a_tty); |
0f0e0052 | 1007 | printf(" %4.4s", state(ap)); |
aafc2e2f BJ |
1008 | ptime(ap); |
1009 | } | |
1010 | ||
1011 | char *vhdr = | |
066ec2ef | 1012 | " SIZE PID TT STAT TIME SL RE PAGEIN SIZE RSS LIM TSIZ TRS %CPU %MEM"+5; |
aafc2e2f BJ |
1013 | vpr(sp) |
1014 | struct savcom *sp; | |
1015 | { | |
066ec2ef | 1016 | register struct vsav *vp = sp->s_un.vp; |
aafc2e2f | 1017 | register struct asav *ap = sp->ap; |
c67277e8 | 1018 | |
0f0e0052 | 1019 | printf("%5u ", ap->a_pid); |
c67277e8 | 1020 | ptty(ap->a_tty); |
0f0e0052 | 1021 | printf(" %4.4s", state(ap)); |
c67277e8 | 1022 | ptime(ap); |
79de1ed3 BJ |
1023 | printf("%3d%3d%7d%5d%5d", |
1024 | ap->a_slptime > 99 ? 99 : ap-> a_slptime, | |
1025 | ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt, | |
066ec2ef SL |
1026 | pgtok(ap->a_size), pgtok(ap->a_rss)); |
1027 | if (ap->a_maxrss == (RLIM_INFINITY/NBPG)) | |
8fb17030 | 1028 | printf(" xx"); |
79de1ed3 | 1029 | else |
066ec2ef | 1030 | printf("%5d", pgtok(ap->a_maxrss)); |
79de1ed3 | 1031 | printf("%5d%4d%5.1f%5.1f", |
066ec2ef | 1032 | pgtok(ap->a_tsiz), pgtok(ap->a_txtrss), vp->v_pctcpu, pmem(ap)); |
c67277e8 BJ |
1033 | } |
1034 | ||
1035 | char *shdr = | |
0f0e0052 | 1036 | "SSIZ PID TT STAT TIME"; |
c67277e8 BJ |
1037 | spr(sp) |
1038 | struct savcom *sp; | |
1039 | { | |
1040 | register struct asav *ap = sp->ap; | |
1041 | ||
1042 | if (sflg) | |
066ec2ef | 1043 | printf("%4d ", sp->s_un.s_ssiz); |
c67277e8 BJ |
1044 | printf("%5u", ap->a_pid); |
1045 | putchar(' '); | |
1046 | ptty(ap->a_tty); | |
0f0e0052 | 1047 | printf(" %4.4s", state(ap)); |
c67277e8 BJ |
1048 | ptime(ap); |
1049 | } | |
1050 | ||
1051 | char * | |
1052 | state(ap) | |
1053 | register struct asav *ap; | |
1054 | { | |
1055 | char stat, load, nice, anom; | |
1056 | static char res[5]; | |
aafc2e2f BJ |
1057 | |
1058 | switch (ap->a_stat) { | |
1059 | ||
aafc2e2f | 1060 | case SSTOP: |
c67277e8 BJ |
1061 | stat = 'T'; |
1062 | break; | |
1063 | ||
1064 | case SSLEEP: | |
1065 | if (ap->a_pri >= PZERO) | |
1066 | if (ap->a_slptime >= MAXSLP) | |
1067 | stat = 'I'; | |
1068 | else | |
1069 | stat = 'S'; | |
aafc2e2f BJ |
1070 | else if (ap->a_flag & SPAGE) |
1071 | stat = 'P'; | |
1072 | else | |
1073 | stat = 'D'; | |
1074 | break; | |
1075 | ||
c67277e8 | 1076 | case SWAIT: |
aafc2e2f BJ |
1077 | case SRUN: |
1078 | case SIDL: | |
c67277e8 BJ |
1079 | stat = 'R'; |
1080 | break; | |
1081 | ||
1082 | case SZOMB: | |
1083 | stat = 'Z'; | |
aafc2e2f | 1084 | break; |
c67277e8 BJ |
1085 | |
1086 | default: | |
1087 | stat = '?'; | |
aafc2e2f | 1088 | } |
79de1ed3 | 1089 | load = ap->a_flag & SLOAD ? (ap->a_rss>ap->a_maxrss ? '>' : ' ') : 'W'; |
0f0e0052 BJ |
1090 | if (ap->a_nice < NZERO) |
1091 | nice = '<'; | |
1092 | else if (ap->a_nice > NZERO) | |
1093 | nice = 'N'; | |
1094 | else | |
1095 | nice = ' '; | |
8fb17030 | 1096 | anom = (ap->a_flag&SUANOM) ? 'A' : ((ap->a_flag&SSEQL) ? 'S' : ' '); |
c67277e8 BJ |
1097 | res[0] = stat; res[1] = load; res[2] = nice; res[3] = anom; |
1098 | return (res); | |
aafc2e2f BJ |
1099 | } |
1100 | ||
1101 | /* | |
1102 | * Given a base/size pair in virtual swap area, | |
1103 | * return a physical base/size pair which is the | |
1104 | * (largest) initial, physically contiguous block. | |
1105 | */ | |
1106 | vstodb(vsbase, vssize, dmp, dbp, rev) | |
1107 | register int vsbase; | |
1108 | int vssize; | |
1109 | struct dmap *dmp; | |
1110 | register struct dblock *dbp; | |
1111 | { | |
b38b1b3a | 1112 | register int blk = dmmin; |
aafc2e2f BJ |
1113 | register swblk_t *ip = dmp->dm_map; |
1114 | ||
066ec2ef SL |
1115 | vsbase = ctod(vsbase); |
1116 | vssize = ctod(vssize); | |
aafc2e2f BJ |
1117 | if (vsbase < 0 || vsbase + vssize > dmp->dm_size) |
1118 | panic("vstodb"); | |
1119 | while (vsbase >= blk) { | |
1120 | vsbase -= blk; | |
b38b1b3a | 1121 | if (blk < dmmax) |
aafc2e2f BJ |
1122 | blk *= 2; |
1123 | ip++; | |
1124 | } | |
1125 | if (*ip <= 0 || *ip + blk > nswap) | |
1126 | panic("vstodb *ip"); | |
1127 | dbp->db_size = min(vssize, blk - vsbase); | |
1128 | dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase); | |
1129 | } | |
1130 | ||
1131 | /*ARGSUSED*/ | |
1132 | panic(cp) | |
1133 | char *cp; | |
1134 | { | |
1135 | ||
1136 | #ifdef DEBUG | |
1137 | printf("%s\n", cp); | |
1138 | #endif | |
1139 | } | |
1140 | ||
1141 | min(a, b) | |
1142 | { | |
1143 | ||
1144 | return (a < b ? a : b); | |
1145 | } | |
1146 | ||
1147 | pscomp(s1, s2) | |
1148 | struct savcom *s1, *s2; | |
1149 | { | |
1150 | register int i; | |
1151 | ||
066ec2ef SL |
1152 | if (uflg) |
1153 | return (s2->s_un.u_pctcpu > s1->s_un.u_pctcpu ? 1 : -1); | |
aafc2e2f BJ |
1154 | if (vflg) |
1155 | return (vsize(s2) - vsize(s1)); | |
1156 | i = s1->ap->a_ttyd - s2->ap->a_ttyd; | |
1157 | if (i == 0) | |
1158 | i = s1->ap->a_pid - s2->ap->a_pid; | |
1159 | return (i); | |
1160 | } | |
1161 | ||
1162 | vsize(sp) | |
1163 | struct savcom *sp; | |
1164 | { | |
1165 | register struct asav *ap = sp->ap; | |
066ec2ef | 1166 | register struct vsav *vp = sp->s_un.vp; |
aafc2e2f BJ |
1167 | |
1168 | if (ap->a_flag & SLOAD) | |
1169 | return (ap->a_rss + | |
0f0e0052 BJ |
1170 | ap->a_txtrss / (ap->a_xccount ? ap->a_xccount : 1)); |
1171 | return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss)); | |
aafc2e2f BJ |
1172 | } |
1173 | ||
60b23435 CL |
1174 | #define NMAX 8 /* sizeof loginname (should be sizeof (utmp.ut_name)) */ |
1175 | #define NUID 2048 /* must not be a multiple of 5 */ | |
aafc2e2f | 1176 | |
60b23435 CL |
1177 | struct nametable { |
1178 | char nt_name[NMAX+1]; | |
1179 | int nt_uid; | |
1180 | } nametable[NUID]; | |
1181 | ||
1182 | struct nametable * | |
1183 | findslot(uid) | |
e8807835 | 1184 | int uid; |
60b23435 CL |
1185 | { |
1186 | register struct nametable *n, *start; | |
1187 | ||
1188 | /* | |
1189 | * find the uid or an empty slot. | |
1190 | * return NULL if neither found. | |
1191 | */ | |
1192 | ||
1193 | n = start = nametable + (uid % (NUID - 20)); | |
1194 | while (n->nt_name[0] && n->nt_uid != uid) { | |
1195 | if ((n += 5) >= &nametable[NUID]) | |
1196 | n -= NUID; | |
1197 | if (n == start) | |
1198 | return((struct nametable *)NULL); | |
1199 | } | |
1200 | return(n); | |
1201 | } | |
aafc2e2f | 1202 | |
aafc2e2f BJ |
1203 | char * |
1204 | getname(uid) | |
1205 | { | |
60b23435 CL |
1206 | register struct passwd *pw; |
1207 | static init = 0; | |
1208 | struct passwd *getpwent(); | |
1209 | register struct nametable *n; | |
1210 | ||
1211 | /* | |
1212 | * find uid in hashed table; add it if not found. | |
1213 | * return pointer to name. | |
1214 | */ | |
1215 | ||
1216 | if ((n = findslot(uid)) == NULL) | |
1217 | return((char *)NULL); | |
1218 | ||
1219 | if (n->nt_name[0]) /* occupied? */ | |
1220 | return(n->nt_name); | |
1221 | ||
1222 | switch (init) { | |
1223 | case 0: | |
1224 | setpwent(); | |
1225 | init = 1; | |
1226 | /* intentional fall-thru */ | |
1227 | case 1: | |
1228 | while (pw = getpwent()) { | |
1229 | if (pw->pw_uid < 0) | |
1230 | continue; | |
1231 | if ((n = findslot(pw->pw_uid)) == NULL) { | |
1232 | endpwent(); | |
1233 | init = 2; | |
1234 | return((char *)NULL); | |
1235 | } | |
1236 | if (n->nt_name[0]) | |
1237 | continue; /* duplicate, not uid */ | |
1238 | strncpy(n->nt_name, pw->pw_name, NMAX); | |
1239 | n->nt_uid = pw->pw_uid; | |
1240 | if (pw->pw_uid == uid) | |
1241 | return (n->nt_name); | |
1242 | } | |
1243 | endpwent(); | |
1244 | init = 2; | |
1245 | /* intentional fall-thru */ | |
1246 | case 2: | |
1247 | return ((char *)NULL); | |
aafc2e2f | 1248 | } |
e8807835 | 1249 | /* NOTREACHED */ |
aafc2e2f BJ |
1250 | } |
1251 | ||
1252 | char * | |
1253 | savestr(cp) | |
1254 | char *cp; | |
1255 | { | |
e8807835 | 1256 | register unsigned len; |
aafc2e2f BJ |
1257 | register char *dp; |
1258 | ||
1259 | len = strlen(cp); | |
e8807835 | 1260 | dp = (char *)calloc(len+1, sizeof (char)); |
3d9f6a35 | 1261 | (void) strcpy(dp, cp); |
aafc2e2f BJ |
1262 | return (dp); |
1263 | } | |
e8807835 RC |
1264 | |
1265 | /* | |
1266 | * This routine was stolen from adb to simulate memory management | |
1267 | * on the VAX. | |
1268 | */ | |
1269 | off_t | |
1270 | vtophys(loc) | |
1271 | long loc; | |
1272 | { | |
1273 | register p; | |
1274 | off_t newloc; | |
1275 | ||
1276 | newloc = loc & ~0xc0000000; | |
1277 | p = btop(newloc); | |
1278 | if ((loc & 0xc0000000) == 0) { | |
1279 | fprintf(stderr, "Vtophys: translating non-kernel address\n"); | |
1280 | return((off_t) -1); | |
1281 | } | |
1282 | if (p >= Syssize) { | |
1283 | fprintf(stderr, "Vtophys: page out of bound (%d>=%d)\n", | |
1284 | p, Syssize); | |
1285 | return((off_t) -1); | |
1286 | } | |
1287 | if (Sysmap[p].pg_v == 0 | |
1288 | && (Sysmap[p].pg_fod || Sysmap[p].pg_pfnum == 0)) { | |
1289 | fprintf(stderr, "Vtophys: page not valid\n"); | |
1290 | return((off_t) -1); | |
1291 | } | |
1292 | loc = (long) (ptob(Sysmap[p].pg_pfnum) + (loc & PGOFSET)); | |
1293 | return(loc); | |
1294 | } |