xref: /csrg-svn/usr.bin/systat/fetch.c (revision 15151)
115131Ssam #ifndef lint
2*15151Ssam static char sccsid[] = "@(#)fetch.c	1.3 (Lucasfilm) 10/02/83";
315131Ssam #endif
415131Ssam 
515131Ssam #include "systat.h"
615131Ssam 
715131Ssam long
815131Ssam getw(loc)
915131Ssam         int loc;
1015131Ssam {
1115131Ssam         long word;
1215131Ssam 
1315131Ssam         lseek(kmem, loc, L_SET);
1415131Ssam         if (read(kmem, &word, sizeof (word)) != sizeof (word))
1515131Ssam                 printf("Error reading kmem at %x\n", loc);
1615131Ssam         return (word);
1715131Ssam }
1815131Ssam 
1915131Ssam char *
2015131Ssam getpname(pid, mproc)
2115131Ssam         int pid;
2215131Ssam         register struct proc *mproc;
2315131Ssam {
2415131Ssam         register struct procs *pp;
2515131Ssam         register char *namp;
2615131Ssam         register int j;
2715131Ssam         char *getcmd();
2815131Ssam 
2915131Ssam         pp = procs;
3015131Ssam         for (j = numprocs - 1; j >= 0; j--) {
3115131Ssam                 if (pp->pid == pid)
3215131Ssam                         return (pp->cmd);
3315131Ssam                 pp++;
3415131Ssam         }
3515131Ssam         if (j < 0) {
3615131Ssam                 if (numprocs < 200) {
3715131Ssam                         pp = &procs[numprocs];
3815131Ssam                         namp = strncpy(pp->cmd, getcmd(pid, mproc), 15);
3915131Ssam                         pp->cmd[15] = 0;
4015131Ssam                         pp->pid = pid;
4115131Ssam                         numprocs++;
4215131Ssam                 } else
4315131Ssam                         namp = getcmd(pid, mproc);
4415131Ssam         }
4515131Ssam         return (namp);
4615131Ssam }
4715131Ssam 
4815131Ssam union {
4915131Ssam         struct  user user;
5015131Ssam         char    upages[UPAGES][NBPG];
5115131Ssam } user;
5215131Ssam #define u       user.user
5315131Ssam 
5415131Ssam char *
5515131Ssam getcmd(pid, mproc)
5615131Ssam         int pid;
5715131Ssam         register struct proc *mproc;
5815131Ssam {
5915131Ssam         static char cmd[30];
6015131Ssam 
6115146Ssam         if (mproc == NULL || mproc->p_stat == SZOMB)
6215146Ssam 		return ("");
6315146Ssam 	if (pid == 1)
6415146Ssam 		return ("swapper");
6515146Ssam 	if (pid == 2)
6615146Ssam 		return ("pagedaemon");
6715146Ssam         if (mproc->p_flag&(SSYS|SWEXIT))
6815131Ssam                 return ("");
6915146Ssam         if (getu(mproc) == 0)
7015146Ssam 		return ("???");
7115131Ssam         (void) strncpy(cmd, u.u_comm, sizeof (cmd));
7215131Ssam         return (cmd);
7315131Ssam }
7415131Ssam 
75*15151Ssam static	int argaddr;
76*15151Ssam static	int pcbpf;
77*15151Ssam 
7815131Ssam getu(mproc)
7915131Ssam         register struct proc *mproc;
8015131Ssam {
8115131Ssam         struct pte *pteaddr, apte;
8215131Ssam         struct pte arguutl[UPAGES+CLSIZE];
8315131Ssam         register int i;
8415131Ssam         int ncl, size;
8515131Ssam 
8615131Ssam         size = sizeof (struct user);
8715131Ssam         if ((mproc->p_flag & SLOAD) == 0) {
8815131Ssam                 if (swap < 0)
8915131Ssam                         return (0);
9015131Ssam                 (void) lseek(swap, (long)dtob(mproc->p_swaddr), L_SET);
9115131Ssam                 if (read(swap, (char *)&user.user, size) != size) {
9215146Ssam 			error("cant read u for pid %d", mproc->p_pid);
9315131Ssam                         return (0);
9415131Ssam                 }
9515131Ssam                 pcbpf = 0;
9615131Ssam                 argaddr = 0;
9715131Ssam                 return (1);
9815131Ssam         }
9915131Ssam         pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
10015131Ssam         klseek(kmem, (long)pteaddr, L_SET);
10115131Ssam         if (read(kmem, (char *)&apte, sizeof (apte)) != sizeof (apte)) {
10215146Ssam                 error("cant read indir pte to get u for pid %d", mproc->p_pid);
10315131Ssam                 return (0);
10415131Ssam         }
10515131Ssam         klseek(mem,
10615131Ssam             (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
10715131Ssam                 L_SET);
10815131Ssam         if (read(mem, (char *)arguutl, sizeof (arguutl)) != sizeof (arguutl)) {
10915146Ssam                 error("cant read page table for u of pid %d", mproc->p_pid);
11015131Ssam                 return (0);
11115131Ssam         }
11215131Ssam         if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
11315131Ssam                 argaddr = ctob(arguutl[0].pg_pfnum);
11415131Ssam         else
11515131Ssam                 argaddr = 0;
11615131Ssam         pcbpf = arguutl[CLSIZE].pg_pfnum;
11715131Ssam         ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
11815131Ssam         while (--ncl >= 0) {
11915131Ssam                 i = ncl * CLSIZE;
12015131Ssam                 klseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), L_SET);
12115131Ssam                 if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
12215146Ssam                         error("cant read page %d of u of pid %d\n",
12315146Ssam                             arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid);
12415146Ssam                         return (0);
12515131Ssam                 }
12615131Ssam         }
12715131Ssam         return (1);
12815131Ssam }
12915131Ssam 
13015131Ssam klseek(fd, loc, off)
13115131Ssam         int fd;
13215131Ssam         long loc;
13315131Ssam         int off;
13415131Ssam {
13515131Ssam 
13615131Ssam         (void) lseek(fd, (long)loc, off);
13715131Ssam }
138