diff options
Diffstat (limited to 'usr.bin/procstat')
-rw-r--r-- | usr.bin/procstat/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/procstat/procstat_files.c | 35 | ||||
-rw-r--r-- | usr.bin/procstat/procstat_vm.c | 41 |
3 files changed, 16 insertions, 64 deletions
diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile index e3c77e4..6cb5895 100644 --- a/usr.bin/procstat/Makefile +++ b/usr.bin/procstat/Makefile @@ -12,4 +12,8 @@ SRCS= procstat.c \ procstat_threads.c \ procstat_vm.c +LDADD+= -lutil +DPADD+= ${LIBUTIL} +WARNS?= 4 + .include <bsd.prog.mk> diff --git a/usr.bin/procstat/procstat_files.c b/usr.bin/procstat/procstat_files.c index 3667913..1f620f3 100644 --- a/usr.bin/procstat/procstat_files.c +++ b/usr.bin/procstat/procstat_files.c @@ -41,6 +41,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <libutil.h> #include "procstat.h" @@ -134,42 +135,18 @@ void procstat_files(pid_t pid, struct kinfo_proc *kipp) { struct kinfo_file *freep, *kif; - int error, name[4]; - unsigned int i; + int i, cnt; const char *str; - size_t len; if (!hflag) printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_FILEDESC; - name[3] = pid; - - error = sysctl(name, 4, NULL, &len, NULL, 0); - if (error < 0 && errno != ESRCH && errno != EPERM) { - warn("sysctl: kern.proc.filedesc: %d", pid); - return; - } - if (error < 0) - return; - - freep = kif = malloc(len); - if (kif == NULL) - err(-1, "malloc"); - - if (sysctl(name, 4, kif, &len, NULL, 0) < 0) { - warn("sysctl: kern.proc.filedesc %d", pid); - free(freep); - return; - } - - for (i = 0; i < len / sizeof(*kif); i++, kif++) { - if (kif->kf_structsize != sizeof(*kif)) - errx(-1, "kinfo_file mismatch"); + freep = kinfo_getfile(pid, &cnt); + for (i = 0; i < cnt; i++) { + kif = &freep[i]; + printf("%5d ", pid); printf("%-16s ", kipp->ki_comm); switch (kif->kf_fd) { diff --git a/usr.bin/procstat/procstat_vm.c b/usr.bin/procstat/procstat_vm.c index a658382..f7ffe58 100644 --- a/usr.bin/procstat/procstat_vm.c +++ b/usr.bin/procstat/procstat_vm.c @@ -34,6 +34,7 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <libutil.h> #include "procstat.h" @@ -41,10 +42,9 @@ void procstat_vm(pid_t pid, struct kinfo_proc *kipp __unused) { struct kinfo_vmentry *freep, *kve; - int error, name[4], ptrwidth; - unsigned int i; + int ptrwidth; + int i, cnt; const char *str; - size_t len; ptrwidth = 2*sizeof(void *) + 2; if (!hflag) @@ -52,38 +52,9 @@ procstat_vm(pid_t pid, struct kinfo_proc *kipp __unused) "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES", "PRES", "REF", "SHD", "FL", "TP", "PATH"); - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_VMMAP; - name[3] = pid; - - len = 0; - error = sysctl(name, 4, NULL, &len, NULL, 0); - if (error < 0 && errno != ESRCH && errno != EPERM) { - warn("sysctl: kern.proc.vmmap: %d", pid); - return; - } - if (error < 0) - return; - - /* - * Especially if running procstat -sv, we may need room for more - * mappings when printing than were present when we queried, so pad - * out the allocation a bit. - */ - len += sizeof(*kve) * 3; - freep = kve = malloc(len); - if (kve == NULL) - err(-1, "malloc"); - if (sysctl(name, 4, kve, &len, NULL, 0) < 0) { - warn("sysctl: kern.proc.vmmap: %d", pid); - free(freep); - return; - } - - for (i = 0; i < (len / sizeof(*kve)); i++, kve++) { - if (kve->kve_structsize != sizeof(*kve)) - errx(-1, "kinfo_vmentry structure mismatch"); + freep = kinfo_getvmmap(pid, &cnt); + for (i = 0; i < cnt; i++) { + kve = &freep[i]; printf("%5d ", pid); printf("%*p ", ptrwidth, kve->kve_start); printf("%*p ", ptrwidth, kve->kve_end); |