Index: head/usr.bin/procstat/Makefile =================================================================== --- head/usr.bin/procstat/Makefile (revision 287485) +++ head/usr.bin/procstat/Makefile (revision 287486) @@ -1,22 +1,22 @@ # $FreeBSD$ PROG= procstat MAN= procstat.1 SRCS= procstat.c \ procstat_args.c \ procstat_auxv.c \ procstat_basic.c \ procstat_bin.c \ procstat_cred.c \ procstat_cs.c \ procstat_files.c \ procstat_kstack.c \ procstat_rlimit.c \ procstat_rusage.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c -LIBADD+= util procstat +LIBADD+= procstat xo util sbuf .include Index: head/usr.bin/procstat/procstat.1 =================================================================== --- head/usr.bin/procstat/procstat.1 (revision 287485) +++ head/usr.bin/procstat/procstat.1 (revision 287486) @@ -1,551 +1,562 @@ .\"- .\" Copyright (c) 2007-2009 Robert N. M. Watson .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd May 18, 2015 +.Dd September 5, 2015 .Dt PROCSTAT 1 .Os .Sh NAME .Nm procstat .Nd get detailed process information .Sh SYNOPSIS .Nm +.Op Fl -libxo .Op Fl CHhn .Op Fl w Ar interval .Op Fl b | c | e | f | i | j | k | l | r | s | S | t | v | x .Op Fl a | Ar pid | Ar core ... .Sh DESCRIPTION The .Nm utility displays detailed information about the processes identified by the .Ar pid arguments, or if the .Fl a flag is used, all processes. It can also display information extracted from a process core file, if the core file is specified as the argument. .Pp By default, basic process statistics are printed; one of the following options may be specified in order to select more detailed process information for printing: .Bl -tag -width indent +.It Fl -libxo +Generate output via +.Xr libxo 3 +in a selection of different human and machine readable formats. +See +.Xr xo_parse_args 3 +for details on command line arguments. .It Fl b Display binary information for the process. .It Fl c Display command line arguments for the process. .It Fl e Display environment variables for the process. .It Fl f Display file descriptor information for the process. .It Fl i Display signal pending and disposition information for the process. .It Fl j Display signal pending and blocked information for the process's threads. .It Fl k Display the stacks of kernel threads in the process, excluding stacks of threads currently running on a CPU and threads with stacks swapped to disk. If the flag is repeated, function offsets as well as function names are printed. .It Fl l Display resource limits for the process. .It Fl r Display resource usage information for the process. .It Fl s Display security credential information for the process. .It Fl S Display the cpuset information for the thread. .It Fl t Display thread information for the process. .It Fl v Display virtual memory mappings for the process. .It Fl x Display ELF auxiliary vector for the process. .El .Pp All options generate output in the format of a table, the first field of which is the process ID to which the row of information corresponds. The .Fl h flag may be used to suppress table headers. .Pp The .Fl w flag may be used to specify a wait interval at which to repeat the printing of the requested process information. If the .Fl w flag is not specified, the output will not repeat. .Pp The .Fl C flag requests the printing of additional capability information in the file descriptor view. .Pp The .Fl H flag may be used to request per-thread statistics rather than per-process statistics for some options. For those options, the second field in the table will list the thread ID to which the row of information corresponds. The .Fl H flag is implied for the .Fl S mode. .Pp Information for VM, file descriptor, and cpuset options is available only to the owner of a process or the superuser. A cpuset value displayed as -1 means that the information is either invalid or not available. .Ss Binary Information Display the process ID, command, and path to the process binary: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It OSREL osreldate for process binary .It PATH path to process binary (if available) .El .Ss Command Line Arguments Display the process ID, command, and command line arguments: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It ARGS command line arguments (if available) .El .Ss Environment Variables Display the process ID, command, and environment variables: .Pp .Bl -tag -width "ENVIRONMENT" -compact .It PID process ID .It COMM command .It ENVIRONMENT environment variables (if available) .El .Ss File Descriptors Display detailed information about each file descriptor referenced by a process, including the process ID, command, file descriptor number, and per-file descriptor object information, such as object type and file system path. By default, the following information will be printed: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It FD file descriptor number or cwd/root/jail .It T file descriptor type .It V vnode type .It FLAGS file descriptor flags .It REF file descriptor reference count .It OFFSET file descriptor offset .It PRO network protocol .It NAME file path or socket addresses (if available) .El .Pp The following file descriptor types may be displayed: .Pp .Bl -tag -width X -compact .It c crypto .It e POSIX semaphore .It f fifo .It h shared memory .It k kqueue .It m message queue .It p pipe .It s socket .It t pseudo-terminal master .It v vnode .El .Pp The following vnode types may be displayed: .Pp .Bl -tag -width X -compact .It - not a vnode .It b block device .It c character device .It d directory .It f fifo .It l symbolic link .It r regular file .It s socket .It x revoked device .El .Pp The following file descriptor flags may be displayed: .Pp .Bl -tag -width X -compact .It r read .It w write .It a append .It s async .It f fsync .It n non-blocking .It d direct I/O .It l lock held .El .Pp If the .Fl C flag is specified, the vnode type, reference count, and offset fields will be omitted, and a new capabilities field will be included listing capabilities, as described in .Xr cap_rights_limit 2 , present for each capability descriptor. .Ss Signal Disposition Information Display signal pending and disposition for a process: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It SIG signal name .It FLAGS process signal disposition details, three symbols .Bl -tag -width X -compact .It P if signal is pending in the global process queue, - otherwise .It I if signal delivery disposition is SIGIGN, - otherwise .It C if signal delivery is to catch it, - otherwise .El .El .Pp If .Fl n switch is given, the signal numbers are shown instead of signal names. .Ss Thread Signal Information Display signal pending and blocked for a process's threads: .Pp .Bl -tag -width indent -compact .It PID process ID .It TID thread ID .It COMM command .It SIG signal name .It FLAGS thread signal delivery status, two symbols .Bl -tag -width X -compact .It P if signal is pending for the thread, - otherwise .It B if signal is blocked in the thread signal mask, - if not blocked .El .El .Pp The .Fl n switch has the same effect as for the .Fl i switch: the signal numbers are shown instead of signal names. .Ss Kernel Thread Stacks Display kernel thread stacks for a process, allowing further interpretation of thread wait channels. If the .Fl k flag is repeated, function offsets, not just function names, are printed. .Pp This feature requires .Cd "options STACK" or .Cd "options DDB" to be compiled into the kernel. .Pp .Bl -tag -width indent -compact .It PID process ID .It TID thread ID .It COMM command .It TDNAME thread name .It KSTACK kernel thread call stack .El .Ss Resource Limits Display resource limits for a process: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It RLIMIT resource limit name .It SOFT soft limit .It HARD hard limit .El .Ss Resource Usage Display resource usage for a process. If the .Fl H flag is specified, resource usage for individual threads is displayed instead. .Pp .Bl -tag -width "RESOURCE" -compact .It PID process ID .It TID thread ID .Po if .Fl H is specified .Pc .It COMM command .It RESOURCE resource name .It VALUE current usage .El .Ss Security Credentials Display process credential information: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It EUID effective user ID .It RUID real user ID .It SVUID saved user ID .It EGID effective group ID .It RGID real group ID .It SVGID saved group ID .It UMASK file creation mode mask .It FLAGS credential flags .It GROUPS group set .El .Pp The following credential flags may be displayed: .Pp .Bl -tag -width X -compact .It C capability mode .El .Ss Thread Information Display per-thread information, including process ID, per-thread ID, name, CPU, and execution state: .Pp .Bl -tag -width indent -compact .It PID process ID .It TID thread ID .It COMM command .It TDNAME thread name .It CPU current or most recent CPU run on .It PRI thread priority .It STATE thread state .It WCHAN thread wait channel .El .Ss Virtual Memory Mappings Display process virtual memory mappings, including addresses, mapping meta-data, and mapped object information: .Pp .Bl -tag -width indent -compact .It PID process ID .It START starting address of mapping .It END ending address of mapping .It PRT protection flags .It RES resident pages .It PRES private resident pages .It REF reference count .It SHD shadow page count .It FLAG mapping flags .It TP VM object type .El .Pp The following protection flags may be displayed: .Pp .Bl -tag -width X -compact .It r read .It w write .It x execute .El .Pp The following VM object types may be displayed: .Pp .Bl -tag -width XX -compact .It -- none .It dd dead .It df default .It dv device .It md device with managed pages .Pq GEM/TTM .It ph physical .It sg scatter/gather .It sw swap .It vn vnode .El .Pp The following mapping flags may be displayed: .Pp .Bl -tag -width X -compact .It C copy-on-write .It N needs copy .It S one or more superpage mappings are used .It D grows down (top-down stack) .It U grows up (bottom-up stack) .El .Ss ELF Auxiliary Vector Display ELF auxiliary vector values: .Pp .Bl -tag -width indent -compact .It PID process ID .It COMM command .It AUXV auxiliary vector name .It VALUE auxiliary vector value .El .Sh EXIT STATUS .Ex -std .Sh SEE ALSO .Xr fstat 1 , .Xr ps 1 , .Xr sockstat 1 , .Xr cap_enter 2 , .Xr cap_rights_limit 2 , .Xr libprocstat 3 , +.Xr libxo 3 , +.Xr xo_parse_args 3 , .Xr ddb 4 , .Xr stack 9 .Sh AUTHORS -.An Robert N M Watson +.An Robert N M Watson Aq Mt rwatson@FreeBSD.org . +.br +.Xr libxo 3 +support was added by +.An -nosplit +Allan Jude +.Aq Mt allanjude@FreeBSD.org . .Sh BUGS -Some field values may include spaces, which limits the extent to which the -output of -.Nm -may be mechanically parsed. -.Pp The display of open file or memory mapping pathnames is implemented using the kernel's name cache. If a file system does not use the name cache, or the path to a file is not in the cache, a path will not be displayed. .Pp .Nm currently supports extracting data only from a live kernel, and not from kernel crash dumps. Index: head/usr.bin/procstat/procstat.c =================================================================== --- head/usr.bin/procstat/procstat.c (revision 287485) +++ head/usr.bin/procstat/procstat.c (revision 287486) @@ -1,308 +1,349 @@ /*- * Copyright (c) 2007, 2011 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include "procstat.h" static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, rflag; static int sflag, tflag, vflag, xflag, Sflag; int hflag, nflag, Cflag, Hflag; static void usage(void) { - fprintf(stderr, "usage: procstat [-CHhn] [-M core] [-N system] " - "[-w interval] \n"); - fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | " - "-l | -r | -s | -S | -t | -v | -x]\n"); - fprintf(stderr, " [-a | pid | core ...]\n"); + xo_error("usage: procstat [-CHhn] [-M core] [-N system] " + "[-w interval]\n" + " [-b | -c | -e | -f | -i | -j | -k | " + "-l | -r | -s | -S | -t | -v | -x]\n" + " [-a | pid | core ...]\n"); + xo_finish(); exit(EX_USAGE); } static void procstat(struct procstat *prstat, struct kinfo_proc *kipp) { + char *pidstr = NULL; + asprintf(&pidstr, "%d", kipp->ki_pid); + if (pidstr == NULL) + xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()"); + xo_open_container(pidstr); + if (bflag) procstat_bin(prstat, kipp); else if (cflag) procstat_args(prstat, kipp); else if (eflag) procstat_env(prstat, kipp); else if (fflag) procstat_files(prstat, kipp); else if (iflag) procstat_sigs(prstat, kipp); else if (jflag) procstat_threads_sigs(prstat, kipp); else if (kflag) procstat_kstack(prstat, kipp, kflag); else if (lflag) procstat_rlimit(prstat, kipp); else if (rflag) procstat_rusage(prstat, kipp); else if (sflag) procstat_cred(prstat, kipp); else if (tflag) procstat_threads(prstat, kipp); else if (vflag) procstat_vm(prstat, kipp); else if (xflag) procstat_auxv(prstat, kipp); else if (Sflag) procstat_cs(prstat, kipp); else procstat_basic(kipp); + + xo_close_container(pidstr); + free(pidstr); } /* * Sort processes first by pid and then tid. */ static int kinfo_proc_compare(const void *a, const void *b) { int i; i = ((const struct kinfo_proc *)a)->ki_pid - ((const struct kinfo_proc *)b)->ki_pid; if (i != 0) return (i); i = ((const struct kinfo_proc *)a)->ki_tid - ((const struct kinfo_proc *)b)->ki_tid; return (i); } void kinfo_proc_sort(struct kinfo_proc *kipp, int count) { qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); } int main(int argc, char *argv[]) { int ch, interval, tmp; int i; struct kinfo_proc *p; struct procstat *prstat, *cprstat; long l; pid_t pid; char *dummy; char *nlistf, *memf; + const char *xocontainer; int cnt; interval = 0; memf = nlistf = NULL; + argc = xo_parse_args(argc, argv); + xocontainer = "basic"; + while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrsStvw:x")) != -1) { switch (ch) { case 'C': Cflag++; break; case 'H': Hflag++; break; case 'M': memf = optarg; break; case 'N': nlistf = optarg; break; case 'S': Sflag++; + xocontainer = "cs"; break; case 'a': aflag++; break; case 'b': bflag++; + xocontainer = "binary"; break; case 'c': cflag++; + xocontainer = "arguments"; break; case 'e': eflag++; + xocontainer = "environment"; break; case 'f': fflag++; + xocontainer = "files"; break; case 'i': iflag++; + xocontainer = "signals"; break; case 'j': jflag++; + xocontainer = "thread_signals"; break; case 'k': kflag++; + xocontainer = "kstack"; break; case 'l': lflag++; + xocontainer = "rlimit"; break; case 'n': nflag++; break; case 'h': hflag++; break; case 'r': rflag++; + xocontainer = "rusage"; break; case 's': sflag++; + xocontainer = "credentials"; break; case 't': tflag++; + xocontainer = "threads"; break; case 'v': vflag++; + xocontainer = "vm"; break; case 'w': l = strtol(optarg, &dummy, 10); if (*dummy != '\0') usage(); if (l < 1 || l > INT_MAX) usage(); interval = l; break; case 'x': xflag++; + xocontainer = "auxv"; break; case '?': default: usage(); } } argc -= optind; argv += optind; /* We require that either 0 or 1 mode flags be set. */ tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) + lflag + rflag + sflag + tflag + vflag + xflag + Sflag; if (!(tmp == 0 || tmp == 1)) usage(); /* We allow -k to be specified up to twice, but not more. */ if (kflag > 2) usage(); /* Must specify either the -a flag or a list of pids. */ if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0)) usage(); /* Only allow -C with -f. */ if (Cflag && !fflag) usage(); if (memf != NULL) prstat = procstat_open_kvm(nlistf, memf); else prstat = procstat_open_sysctl(); if (prstat == NULL) - errx(1, "procstat_open()"); + xo_errx(1, "procstat_open()"); do { + xo_set_version(PROCSTAT_XO_VERSION); + xo_open_container("procstat"); + xo_open_container(xocontainer); + if (aflag) { p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt); if (p == NULL) - errx(1, "procstat_getprocs()"); + xo_errx(1, "procstat_getprocs()"); kinfo_proc_sort(p, cnt); for (i = 0; i < cnt; i++) { procstat(prstat, &p[i]); /* Suppress header after first process. */ hflag = 1; + xo_flush(); } procstat_freeprocs(prstat, p); } for (i = 0; i < argc; i++) { l = strtol(argv[i], &dummy, 10); if (*dummy == '\0') { if (l < 0) usage(); pid = l; - p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt); + p = procstat_getprocs(prstat, KERN_PROC_PID, + pid, &cnt); if (p == NULL) - errx(1, "procstat_getprocs()"); + xo_errx(1, "procstat_getprocs()"); if (cnt != 0) procstat(prstat, p); procstat_freeprocs(prstat, p); } else { cprstat = procstat_open_core(argv[i]); if (cprstat == NULL) { warnx("procstat_open()"); continue; } p = procstat_getprocs(cprstat, KERN_PROC_PID, -1, &cnt); if (p == NULL) - errx(1, "procstat_getprocs()"); + xo_errx(1, "procstat_getprocs()"); if (cnt != 0) procstat(cprstat, p); procstat_freeprocs(cprstat, p); procstat_close(cprstat); } /* Suppress header after first process. */ hflag = 1; } + + xo_close_container(xocontainer); + xo_close_container("procstat"); + xo_finish(); if (interval) sleep(interval); } while (interval); + procstat_close(prstat); + exit(0); } Index: head/usr.bin/procstat/procstat.h =================================================================== --- head/usr.bin/procstat/procstat.h (revision 287485) +++ head/usr.bin/procstat/procstat.h (revision 287486) @@ -1,54 +1,59 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ +#include + #ifndef PROCSTAT_H #define PROCSTAT_H + +#define PROCSTAT_XO_VERSION "1" extern int hflag, nflag, Cflag, Hflag; struct kinfo_proc; void kinfo_proc_sort(struct kinfo_proc *kipp, int count); void procstat_args(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_auxv(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_basic(struct kinfo_proc *kipp); void procstat_bin(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_cred(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_cs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_env(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_kstack(struct procstat *prstat, struct kinfo_proc *kipp, int kflag); void procstat_rlimit(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_rusage(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_vm(struct procstat *prstat, struct kinfo_proc *kipp); #endif /* !PROCSTAT_H */ Index: head/usr.bin/procstat/procstat_args.c =================================================================== --- head/usr.bin/procstat/procstat_args.c (revision 287485) +++ head/usr.bin/procstat/procstat_args.c (revision 287486) @@ -1,79 +1,96 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include "procstat.h" -static void -do_args(struct procstat *procstat, struct kinfo_proc *kipp, int env) +void +procstat_args(struct procstat *procstat, struct kinfo_proc *kipp) { int i; char **args; if (!hflag) { - printf("%5s %-16s %-53s\n", "PID", "COMM", - env ? "ENVIRONMENT" : "ARGS"); + xo_emit("{T:/%5s %-16s %-53s}\n", "PID", "COMM", "ARGS"); } - args = env ? procstat_getenvv(procstat, kipp, 0) : - procstat_getargv(procstat, kipp, 0); + args = procstat_getargv(procstat, kipp, 0); - printf("%5d %-16s", kipp->ki_pid, kipp->ki_comm); + xo_emit("{k:process_id/%5d/%d} {:command/%-16s/%s}", kipp->ki_pid, + kipp->ki_comm); if (args == NULL) { - printf(" -\n"); + xo_emit(" {d:args/-}\n"); return; } + xo_open_list("arguments"); for (i = 0; args[i] != NULL; i++) - printf(" %s", args[i]); - printf("\n"); + xo_emit(" {l:args/%s}", args[i]); + xo_close_list("arguments"); + xo_emit("\n"); } void -procstat_args(struct procstat *procstat, struct kinfo_proc *kipp) -{ - do_args(procstat, kipp, 0); -} - -void procstat_env(struct procstat *procstat, struct kinfo_proc *kipp) { - do_args(procstat, kipp, 1); + int i; + char **envs; + + if (!hflag) { + xo_emit("{T:/%5s %-16s %-53s}\n", "PID", "COMM", "ENVIRONMENT"); + } + + envs = procstat_getenvv(procstat, kipp, 0); + + xo_emit("{k:process_id/%5d/%d} {:command/%-16s/%s}", kipp->ki_pid, + kipp->ki_comm); + + if (envs == NULL) { + xo_emit(" {d:env/-}\n"); + return; + } + + xo_open_list("environment"); + for (i = 0; envs[i] != NULL; i++) + xo_emit(" {l:env/%s}", envs[i]); + xo_close_list("environment"); + xo_emit("\n"); } Index: head/usr.bin/procstat/procstat_auxv.c =================================================================== --- head/usr.bin/procstat/procstat_auxv.c (revision 287485) +++ head/usr.bin/procstat/procstat_auxv.c (revision 287486) @@ -1,160 +1,189 @@ /*- * Copyright (c) 2011 Mikolaj Golub + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include "procstat.h" -#define PRINT(name, spec, val) \ - printf("%s %-16s " #spec "\n", prefix, #name, (val)) -#define PRINT_UNKNOWN(type, val) \ - printf("%s %16ld %#lx\n", prefix, (long)type, (u_long)(val)) - void procstat_auxv(struct procstat *procstat, struct kinfo_proc *kipp) { Elf_Auxinfo *auxv; u_int count, i; static char prefix[256]; if (!hflag) - printf("%5s %-16s %-16s %-16s\n", "PID", "COMM", "AUXV", "VALUE"); + xo_emit("{T:/%5s %-16s %-16s %-16s}\n", "PID", "COMM", "AUXV", + "VALUE"); + auxv = procstat_getauxv(procstat, kipp, &count); if (auxv == NULL) return; - snprintf(prefix, sizeof(prefix), "%5d %-16s", kipp->ki_pid, + snprintf(prefix, sizeof(prefix), "%5d %-16s", kipp->ki_pid, + kipp->ki_comm); + + xo_emit("{e:process_id/%5d/%d}{e:command/%-16s/%s}", kipp->ki_pid, kipp->ki_comm); + for (i = 0; i < count; i++) { switch(auxv[i].a_type) { case AT_NULL: return; case AT_IGNORE: break; case AT_EXECFD: - PRINT(AT_EXECFD, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_EXECFD/%ld}\n", + prefix, "AT_EXECFD", (long)auxv[i].a_un.a_val); break; case AT_PHDR: - PRINT(AT_PHDR, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_PHDR/%p}\n", + prefix, "AT_PHDR", auxv[i].a_un.a_ptr); break; case AT_PHENT: - PRINT(AT_PHENT, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_PHENT/%ld}\n", + prefix, "AT_PHENT", (long)auxv[i].a_un.a_val); break; case AT_PHNUM: - PRINT(AT_PHNUM, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_PHNUM/%ld}\n", + prefix, "AT_PHNUM", (long)auxv[i].a_un.a_val); break; case AT_PAGESZ: - PRINT(AT_PAGESZ, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_PAGESZ/%ld}\n", + prefix, "AT_PAGESZ", (long)auxv[i].a_un.a_val); break; case AT_BASE: - PRINT(AT_BASE, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_BASE/%p}\n", + prefix, "AT_BASE", auxv[i].a_un.a_ptr); break; case AT_FLAGS: - PRINT(AT_FLAGS, %#lx, (u_long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_FLAGS/%#lx}\n", + prefix, "AT_FLAGS", (u_long)auxv[i].a_un.a_val); break; case AT_ENTRY: - PRINT(AT_ENTRY, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_ENTRY/%p}\n", + prefix, "AT_ENTRY", auxv[i].a_un.a_ptr); break; #ifdef AT_NOTELF case AT_NOTELF: - PRINT(AT_NOTELF, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_NOTELF/%ld}\n", + prefix, "AT_NOTELF", (long)auxv[i].a_un.a_val); break; #endif #ifdef AT_UID case AT_UID: - PRINT(AT_UID, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_UID/%ld}\n", + prefix, "AT_UID", (long)auxv[i].a_un.a_val); break; #endif #ifdef AT_EUID case AT_EUID: - PRINT(AT_EUID, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_EUID/%ld}\n", + prefix, "AT_EUID", (long)auxv[i].a_un.a_val); break; #endif #ifdef AT_GID case AT_GID: - PRINT(AT_GID, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_GID/%ld}\n", + prefix, "AT_GID", (long)auxv[i].a_un.a_val); break; #endif #ifdef AT_EGID case AT_EGID: - PRINT(AT_EGID, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_EGID/%ld}\n", + prefix, "AT_EGID", (long)auxv[i].a_un.a_val); break; #endif case AT_EXECPATH: - PRINT(AT_EXECPATH, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_EXECPATH/%p}\n", + prefix, "AT_EXECPATH", auxv[i].a_un.a_ptr); break; case AT_CANARY: - PRINT(AT_CANARY, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_CANARY/%p}\n", + prefix, "AT_CANARY", auxv[i].a_un.a_ptr); break; case AT_CANARYLEN: - PRINT(AT_CANARYLEN, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_CANARYLEN/%ld}\n", + prefix, "AT_CANARYLEN", (long)auxv[i].a_un.a_val); break; case AT_OSRELDATE: - PRINT(AT_OSRELDATE, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_OSRELDATE/%ld}\n", + prefix, "AT_OSRELDATE", (long)auxv[i].a_un.a_val); break; case AT_NCPUS: - PRINT(AT_NCPUS, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_NCPUS/%ld}\n", + prefix, "AT_NCPUS", (long)auxv[i].a_un.a_val); break; case AT_PAGESIZES: - PRINT(AT_PAGESIZES, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_PAGESIZES/%p}\n", + prefix, "AT_PAGESIZES", auxv[i].a_un.a_ptr); break; case AT_PAGESIZESLEN: - PRINT(AT_PAGESIZESLEN, %ld, (long)auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}" + "{:AT_PAGESIZESLEN/%ld}\n", prefix, + "AT_PAGESIZESLEN", (long)auxv[i].a_un.a_val); break; case AT_STACKPROT: if ((auxv[i].a_un.a_val & VM_PROT_EXECUTE) != 0) - PRINT(AT_STACKPROT, %s, "NONEXECUTABLE"); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}" + "{:AT_STACKPROT/%s}\n", prefix, + "AT_STACKPROT", "NONEXECUTABLE"); else - PRINT(AT_STACKPROT, %s, "EXECUTABLE"); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}" + "{:AT_STACKPROT/%s}\n", prefix, + "AT_STACKPROT", "EXECUTABLE"); break; #ifdef AT_TIMEKEEP case AT_TIMEKEEP: - PRINT(AT_TIMEKEEP, %p, auxv[i].a_un.a_ptr); + xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_TIMEKEEP/%p}\n", + prefix, "AT_TIMEKEEP", auxv[i].a_un.a_ptr); break; #endif default: - PRINT_UNKNOWN(auxv[i].a_type, auxv[i].a_un.a_val); + xo_emit("{dw:/%s}{Lw:/%16ld/%ld}{:UNKNOWN/%#lx}\n", + prefix, auxv[i].a_type, auxv[i].a_un.a_val); break; } } - printf("\n"); + xo_emit("\n"); procstat_freeauxv(procstat, auxv); } Index: head/usr.bin/procstat/procstat_basic.c =================================================================== --- head/usr.bin/procstat/procstat_basic.c (revision 287485) +++ head/usr.bin/procstat/procstat_basic.c (revision 287486) @@ -1,65 +1,68 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include "procstat.h" void procstat_basic(struct kinfo_proc *kipp) { if (!hflag) - printf("%5s %5s %5s %5s %5s %3s %-8s %-9s %-13s %-12s\n", + xo_emit("{T:/%5s %5s %5s %5s %5s %3s %-8s %-9s %-13s %-12s}\n", "PID", "PPID", "PGID", "SID", "TSID", "THR", "LOGIN", "WCHAN", "EMUL", "COMM"); - printf("%5d ", kipp->ki_pid); - printf("%5d ", kipp->ki_ppid); - printf("%5d ", kipp->ki_pgid); - printf("%5d ", kipp->ki_sid); - printf("%5d ", kipp->ki_tsid); - printf("%3d ", kipp->ki_numthreads); - printf("%-8s ", strlen(kipp->ki_login) ? kipp->ki_login : "-"); + xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:parent_process_id/%5d/%d} ", kipp->ki_ppid); + xo_emit("{:process_group_id/%5d/%d} ", kipp->ki_pgid); + xo_emit("{:session_id/%5d/%d} ", kipp->ki_sid); + xo_emit("{:terminal_session_id/%5d/%d} ", kipp->ki_tsid); + xo_emit("{:threads/%3d/%d} ", kipp->ki_numthreads); + xo_emit("{:login/%-8s/%s} ", strlen(kipp->ki_login) ? + kipp->ki_login : "-"); if (kipp->ki_kiflag & KI_LOCKBLOCK) { - printf("*%-8s ", strlen(kipp->ki_lockname) ? + xo_emit("{:lockname/*%-8s/%s} ", strlen(kipp->ki_lockname) ? kipp->ki_lockname : "-"); } else { - printf("%-9s ", strlen(kipp->ki_wmesg) ? + xo_emit("{:wait_channel/%-9s/%s} ", strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-"); } - printf("%-13s ", strcmp(kipp->ki_emul, "null") ? kipp->ki_emul : "-"); - printf("%-12s\n", kipp->ki_comm); + xo_emit("{:emulation/%-13s/%s} ", strcmp(kipp->ki_emul, "null") ? + kipp->ki_emul : "-"); + xo_emit("{:command/%-12s/%s}\n", kipp->ki_comm); } Index: head/usr.bin/procstat/procstat_bin.c =================================================================== --- head/usr.bin/procstat/procstat_bin.c (revision 287485) +++ head/usr.bin/procstat/procstat_bin.c (revision 287486) @@ -1,62 +1,64 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include "procstat.h" void procstat_bin(struct procstat *prstat, struct kinfo_proc *kipp) { int osrel; static char pathname[PATH_MAX]; if (!hflag) - printf("%5s %-16s %8s %s\n", "PID", "COMM", "OSREL", "PATH"); + xo_emit("{T:/%5s %-16s %8s %s}\n", "PID", "COMM", "OSREL", + "PATH"); if (procstat_getpathname(prstat, kipp, pathname, sizeof(pathname)) != 0) return; if (strlen(pathname) == 0) strcpy(pathname, "-"); if (procstat_getosrel(prstat, kipp, &osrel) != 0) return; - printf("%5d ", kipp->ki_pid); - printf("%-16s ", kipp->ki_comm); - printf("%8d ", osrel); - printf("%s\n", pathname); + xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:command/%-16s/%s} ", kipp->ki_comm); + xo_emit("{:osrel/%8d/%d} ", osrel); + xo_emit("{:pathname/%s}\n", pathname); } Index: head/usr.bin/procstat/procstat_cred.c =================================================================== --- head/usr.bin/procstat/procstat_cred.c (revision 287485) +++ head/usr.bin/procstat/procstat_cred.c (revision 287486) @@ -1,99 +1,103 @@ /*- * Copyright (c) 2007-2008 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include "procstat.h" static const char *get_umask(struct procstat *procstat, struct kinfo_proc *kipp); void procstat_cred(struct procstat *procstat, struct kinfo_proc *kipp) { unsigned int i, ngroups; gid_t *groups; if (!hflag) - printf("%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s\n", + xo_emit("{T:/%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s}\n", "PID", "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID", "SVGID", "UMASK", "FLAGS", "GROUPS"); - printf("%5d ", kipp->ki_pid); - printf("%-16s ", kipp->ki_comm); - printf("%5d ", kipp->ki_uid); - printf("%5d ", kipp->ki_ruid); - printf("%5d ", kipp->ki_svuid); - printf("%5d ", kipp->ki_groups[0]); - printf("%5d ", kipp->ki_rgid); - printf("%5d ", kipp->ki_svgid); - printf("%5s ", get_umask(procstat, kipp)); - printf("%s", kipp->ki_cr_flags & CRED_FLAG_CAPMODE ? "C" : "-"); - printf(" "); + xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:command/%-16s/%s} ", kipp->ki_comm); + xo_emit("{:uid/%5d} ", kipp->ki_uid); + xo_emit("{:ruid/%5d} ", kipp->ki_ruid); + xo_emit("{:svuid/%5d} ", kipp->ki_svuid); + xo_emit("{:group/%5d} ", kipp->ki_groups[0]); + xo_emit("{:rgid/%5d} ", kipp->ki_rgid); + xo_emit("{:svgid/%5d} ", kipp->ki_svgid); + xo_emit("{:umask/%5s} ", get_umask(procstat, kipp)); + xo_emit("{:cr_flags/%s}", kipp->ki_cr_flags & CRED_FLAG_CAPMODE ? + "C" : "-"); + xo_emit("{P: }"); groups = NULL; /* * We may have too many groups to fit in kinfo_proc's statically * sized storage. If that occurs, attempt to retrieve them using * libprocstat. */ if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW) groups = procstat_getgroups(procstat, kipp, &ngroups); if (groups == NULL) { ngroups = kipp->ki_ngroups; groups = kipp->ki_groups; } + xo_open_list("groups"); for (i = 0; i < ngroups; i++) - printf("%s%d", (i > 0) ? "," : "", groups[i]); + xo_emit("{D:/%s}{l:groups/%d}", (i > 0) ? "," : "", groups[i]); if (groups != kipp->ki_groups) procstat_freegroups(procstat, groups); - printf("\n"); + xo_close_list("groups"); + xo_emit("\n"); } static const char * get_umask(struct procstat *procstat, struct kinfo_proc *kipp) { u_short fd_cmask; static char umask[4]; if (procstat_getumask(procstat, kipp, &fd_cmask) == 0) { snprintf(umask, 4, "%03o", fd_cmask); return (umask); } else { return ("-"); } } Index: head/usr.bin/procstat/procstat_cs.c =================================================================== --- head/usr.bin/procstat/procstat_cs.c (revision 287485) +++ head/usr.bin/procstat/procstat_cs.c (revision 287486) @@ -1,108 +1,119 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include +#include #include #include #include #include #include #include #include #include #include "procstat.h" void procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp) { cpusetid_t cs; cpuset_t mask; struct kinfo_proc *kip; + struct sbuf *cpusetbuf; unsigned int count, i; int once, twice, lastcpu, cpu; if (!hflag) - printf("%5s %6s %-16s %-16s %2s %4s %-7s\n", "PID", + xo_emit("{T:/%5s %6s %-16s %-16s %2s %4s %-7s}\n", "PID", "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK"); kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &count); if (kip == NULL) return; kinfo_proc_sort(kip, count); for (i = 0; i < count; i++) { kipp = &kip[i]; - printf("%5d ", kipp->ki_pid); - printf("%6d ", kipp->ki_tid); - printf("%-16s ", strlen(kipp->ki_comm) ? + xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid); + xo_emit("{:command/%-16s/%s} ", strlen(kipp->ki_comm) ? kipp->ki_comm : "-"); - printf("%-16s ", (strlen(kipp->ki_tdname) && + xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) && (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? kipp->ki_tdname : "-"); if (kipp->ki_oncpu != 255) - printf("%3d ", kipp->ki_oncpu); + xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu); else if (kipp->ki_lastcpu != 255) - printf("%3d ", kipp->ki_lastcpu); + xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu); else - printf("%3s ", "-"); + xo_emit("{:cpu/%3s/%s} ", "-"); if (cpuset_getid(CPU_LEVEL_CPUSET, CPU_WHICH_TID, kipp->ki_tid, &cs) != 0) { cs = CPUSET_INVALID; } - printf("%4d ", cs); + xo_emit("{:cpu_set_id/%4d/%d} ", cs); if ((cs != CPUSET_INVALID) && (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, kipp->ki_tid, sizeof(mask), &mask) == 0)) { lastcpu = -1; once = 0; twice = 0; + cpusetbuf = sbuf_new_auto(); for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { if (CPU_ISSET(cpu, &mask)) { if (once == 0) { - printf("%d", cpu); + sbuf_printf(cpusetbuf, "%d", + cpu); once = 1; } else if (cpu == lastcpu + 1) { twice = 1; } else if (twice == 1) { - printf("-%d,%d", lastcpu, cpu); + sbuf_printf(cpusetbuf, "-%d,%d", + lastcpu, cpu); twice = 0; } else - printf(",%d", cpu); + sbuf_printf(cpusetbuf, ",%d", + cpu); lastcpu = cpu; } } if (once && twice) - printf("-%d", lastcpu); + sbuf_printf(cpusetbuf, "-%d", lastcpu); + if (sbuf_finish(cpusetbuf) != 0) + xo_err(1, "Could not generate output"); + xo_emit("{:cpu_set/%s}", sbuf_data(cpusetbuf)); + sbuf_delete(cpusetbuf); } - printf("\n"); + xo_emit("\n"); } procstat_freeprocs(procstat, kip); } Index: head/usr.bin/procstat/procstat_files.c =================================================================== --- head/usr.bin/procstat/procstat_files.c (revision 287485) +++ head/usr.bin/procstat/procstat_files.c (revision 287486) @@ -1,508 +1,576 @@ /*- * Copyright (c) 2007-2011 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "procstat.h" static const char * protocol_to_string(int domain, int type, int protocol) { switch (domain) { case AF_INET: case AF_INET6: switch (protocol) { case IPPROTO_TCP: return ("TCP"); case IPPROTO_UDP: return ("UDP"); case IPPROTO_ICMP: return ("ICM"); case IPPROTO_RAW: return ("RAW"); case IPPROTO_SCTP: return ("SCT"); case IPPROTO_DIVERT: return ("IPD"); default: return ("IP?"); } case AF_LOCAL: switch (type) { case SOCK_STREAM: return ("UDS"); case SOCK_DGRAM: return ("UDD"); default: return ("UD?"); } default: return ("?"); } } static void addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen) { char buffer2[INET6_ADDRSTRLEN]; struct sockaddr_in6 *sin6; struct sockaddr_in *sin; struct sockaddr_un *sun; switch (ss->ss_family) { case AF_LOCAL: sun = (struct sockaddr_un *)ss; if (strlen(sun->sun_path) == 0) strlcpy(buffer, "-", buflen); else strlcpy(buffer, sun->sun_path, buflen); break; case AF_INET: sin = (struct sockaddr_in *)ss; snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); break; case AF_INET6: sin6 = (struct sockaddr_in6 *)ss; if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2, sizeof(buffer2)) != NULL) snprintf(buffer, buflen, "%s.%d", buffer2, ntohs(sin6->sin6_port)); else strlcpy(buffer, "-", buflen); break; default: strlcpy(buffer, "", buflen); break; } } -static void -print_address(struct sockaddr_storage *ss) -{ - char addr[PATH_MAX]; - - addr_to_string(ss, addr, sizeof(addr)); - printf("%s", addr); -} - static struct cap_desc { uint64_t cd_right; const char *cd_desc; } cap_desc[] = { /* General file I/O. */ { CAP_READ, "rd" }, { CAP_WRITE, "wr" }, { CAP_SEEK, "se" }, { CAP_MMAP, "mm" }, { CAP_CREATE, "cr" }, { CAP_FEXECVE, "fe" }, { CAP_FSYNC, "fy" }, { CAP_FTRUNCATE, "ft" }, /* VFS methods. */ { CAP_FCHDIR, "cd" }, { CAP_FCHFLAGS, "cf" }, { CAP_FCHMOD, "cm" }, { CAP_FCHOWN, "cn" }, { CAP_FCNTL, "fc" }, { CAP_FLOCK, "fl" }, { CAP_FPATHCONF, "fp" }, { CAP_FSCK, "fk" }, { CAP_FSTAT, "fs" }, { CAP_FSTATFS, "sf" }, { CAP_FUTIMES, "fu" }, { CAP_LINKAT_SOURCE, "ls" }, { CAP_LINKAT_TARGET, "lt" }, { CAP_MKDIRAT, "md" }, { CAP_MKFIFOAT, "mf" }, { CAP_MKNODAT, "mn" }, { CAP_RENAMEAT_SOURCE, "rs" }, { CAP_RENAMEAT_TARGET, "rt" }, { CAP_SYMLINKAT, "sl" }, { CAP_UNLINKAT, "un" }, /* Lookups - used to constrain *at() calls. */ { CAP_LOOKUP, "lo" }, /* Extended attributes. */ { CAP_EXTATTR_GET, "eg" }, { CAP_EXTATTR_SET, "es" }, { CAP_EXTATTR_DELETE, "ed" }, { CAP_EXTATTR_LIST, "el" }, /* Access Control Lists. */ { CAP_ACL_GET, "ag" }, { CAP_ACL_SET, "as" }, { CAP_ACL_DELETE, "ad" }, { CAP_ACL_CHECK, "ac" }, /* Socket operations. */ { CAP_ACCEPT, "at" }, { CAP_BIND, "bd" }, { CAP_CONNECT, "co" }, { CAP_GETPEERNAME, "pn" }, { CAP_GETSOCKNAME, "sn" }, { CAP_GETSOCKOPT, "gs" }, { CAP_LISTEN, "ln" }, { CAP_PEELOFF, "pf" }, { CAP_SETSOCKOPT, "ss" }, { CAP_SHUTDOWN, "sh" }, /* Mandatory Access Control. */ { CAP_MAC_GET, "mg" }, { CAP_MAC_SET, "ms" }, /* Methods on semaphores. */ { CAP_SEM_GETVALUE, "sg" }, { CAP_SEM_POST, "sp" }, { CAP_SEM_WAIT, "sw" }, /* Event monitoring and posting. */ { CAP_EVENT, "ev" }, { CAP_KQUEUE_EVENT, "ke" }, { CAP_KQUEUE_CHANGE, "kc" }, /* Strange and powerful rights that should not be given lightly. */ { CAP_IOCTL, "io" }, { CAP_TTYHOOK, "ty" }, /* Process management via process descriptors. */ { CAP_PDGETPID, "pg" }, { CAP_PDWAIT, "pw" }, { CAP_PDKILL, "pk" }, /* * Rights that allow to use bindat(2) and connectat(2) syscalls on a * directory descriptor. */ { CAP_BINDAT, "ba" }, { CAP_CONNECTAT, "ca" }, /* Aliases and defines that combine multiple rights. */ { CAP_PREAD, "prd" }, { CAP_PWRITE, "pwr" }, { CAP_MMAP_R, "mmr" }, { CAP_MMAP_W, "mmw" }, { CAP_MMAP_X, "mmx" }, { CAP_MMAP_RW, "mrw" }, { CAP_MMAP_RX, "mrx" }, { CAP_MMAP_WX, "mwx" }, { CAP_MMAP_RWX, "mma" }, { CAP_RECV, "re" }, { CAP_SEND, "sd" }, { CAP_SOCK_CLIENT, "scl" }, { CAP_SOCK_SERVER, "ssr" }, }; static const u_int cap_desc_count = sizeof(cap_desc) / sizeof(cap_desc[0]); static u_int width_capability(cap_rights_t *rightsp) { u_int count, i, width; count = 0; width = 0; for (i = 0; i < cap_desc_count; i++) { if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) { width += strlen(cap_desc[i].cd_desc); if (count) width++; count++; } } return (width); } static void print_capability(cap_rights_t *rightsp, u_int capwidth) { u_int count, i, width; count = 0; width = 0; for (i = width_capability(rightsp); i < capwidth; i++) { if (i != 0) - printf(" "); + xo_emit(" "); else - printf("-"); + xo_emit("-"); } + xo_open_list("capabilities"); for (i = 0; i < cap_desc_count; i++) { if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) { - printf("%s%s", count ? "," : "", cap_desc[i].cd_desc); + xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "", + cap_desc[i].cd_desc); width += strlen(cap_desc[i].cd_desc); if (count) width++; count++; } } + xo_close_list("capabilities"); } void procstat_files(struct procstat *procstat, struct kinfo_proc *kipp) { struct sockstat sock; struct filestat_list *head; struct filestat *fst; const char *str; struct vnstat vn; u_int capwidth, width; int error; + char src_addr[PATH_MAX]; + char dst_addr[PATH_MAX]; /* * To print the header in capability mode, we need to know the width * of the widest capability string. Even if we get no processes * back, we will print the header, so we defer aborting due to a lack * of processes until after the header logic. */ capwidth = 0; head = procstat_getfiles(procstat, kipp, 0); if (head != NULL && Cflag) { STAILQ_FOREACH(fst, head, next) { width = width_capability(&fst->fs_cap_rights); if (width > capwidth) capwidth = width; } if (capwidth < strlen("CAPABILITIES")) capwidth = strlen("CAPABILITIES"); } if (!hflag) { if (Cflag) - printf("%5s %-16s %5s %1s %-8s %-*s " - "%-3s %-12s\n", "PID", "COMM", "FD", "T", + xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s " + "%-3s %-12s}\n", "PID", "COMM", "FD", "T", "FLAGS", capwidth, "CAPABILITIES", "PRO", "NAME"); else - printf("%5s %-16s %5s %1s %1s %-8s " - "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T", + xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s " + "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); } if (head == NULL) return; + xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); + xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); + xo_open_list("files"); STAILQ_FOREACH(fst, head, next) { - printf("%5d ", kipp->ki_pid); - printf("%-16s ", kipp->ki_comm); + xo_open_instance("files"); + xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); if (fst->fs_uflags & PS_FST_UFLAG_CTTY) - printf(" ctty "); + xo_emit("{P: }{:fd/%s} ", "ctty"); else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) - printf(" cwd "); + xo_emit("{P: }{:fd/%s} ", "cwd"); else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) - printf(" jail "); + xo_emit("{P: }{:fd/%s} ", "jail"); else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) - printf(" root "); + xo_emit("{P: }{:fd/%s} ", "root"); else if (fst->fs_uflags & PS_FST_UFLAG_TEXT) - printf(" text "); + xo_emit("{P: }{:fd/%s} ", "text"); else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) - printf("trace "); + xo_emit("{:fd/%s} ", "trace"); else - printf("%5d ", fst->fs_fd); + xo_emit("{:fd/%5d} ", fst->fs_fd); switch (fst->fs_type) { case PS_FST_TYPE_VNODE: str = "v"; + xo_emit("{eq:fd_type/vnode}"); break; case PS_FST_TYPE_SOCKET: str = "s"; + xo_emit("{eq:fd_type/socket}"); break; case PS_FST_TYPE_PIPE: str = "p"; + xo_emit("{eq:fd_type/pipe}"); break; case PS_FST_TYPE_FIFO: str = "f"; + xo_emit("{eq:fd_type/fifo}"); break; case PS_FST_TYPE_KQUEUE: str = "k"; + xo_emit("{eq:fd_type/kqueue}"); break; case PS_FST_TYPE_CRYPTO: str = "c"; + xo_emit("{eq:fd_type/crypto}"); break; case PS_FST_TYPE_MQUEUE: str = "m"; + xo_emit("{eq:fd_type/mqueue}"); break; case PS_FST_TYPE_SHM: str = "h"; + xo_emit("{eq:fd_type/shm}"); break; case PS_FST_TYPE_PTS: str = "t"; + xo_emit("{eq:fd_type/pts}"); break; case PS_FST_TYPE_SEM: str = "e"; + xo_emit("{eq:fd_type/sem}"); break; case PS_FST_TYPE_NONE: + str = "?"; + xo_emit("{eq:fd_type/none}"); + break; + case PS_FST_TYPE_UNKNOWN: default: str = "?"; + xo_emit("{eq:fd_type/unknown}"); break; } - printf("%1s ", str); + xo_emit("{d:fd_type/%1s/%s} ", str); if (!Cflag) { str = "-"; if (fst->fs_type == PS_FST_TYPE_VNODE) { error = procstat_get_vnode_info(procstat, fst, &vn, NULL); switch (vn.vn_type) { case PS_FST_VTYPE_VREG: str = "r"; + xo_emit("{eq:vode_type/regular}"); break; case PS_FST_VTYPE_VDIR: str = "d"; + xo_emit("{eq:vode_type/directory}"); break; case PS_FST_VTYPE_VBLK: str = "b"; + xo_emit("{eq:vode_type/block}"); break; case PS_FST_VTYPE_VCHR: str = "c"; + xo_emit("{eq:vode_type/character}"); break; case PS_FST_VTYPE_VLNK: str = "l"; + xo_emit("{eq:vode_type/link}"); break; case PS_FST_VTYPE_VSOCK: str = "s"; + xo_emit("{eq:vode_type/socket}"); break; case PS_FST_VTYPE_VFIFO: str = "f"; + xo_emit("{eq:vode_type/fifo}"); break; case PS_FST_VTYPE_VBAD: str = "x"; + xo_emit("{eq:vode_type/revoked_device}"); break; case PS_FST_VTYPE_VNON: + str = "?"; + xo_emit("{eq:vode_type/non}"); + break; + case PS_FST_VTYPE_UNKNOWN: default: str = "?"; + xo_emit("{eq:vode_type/unknown}"); break; } } - printf("%1s ", str); + xo_emit("{d:vnode_type/%1s/%s} ", str); } - printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-"); - printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-"); - printf(" "); + + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ? + "r" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ? + "w" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ? + "a" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? + "s" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ? + "f" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? + "n" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? + "d" : "-"); + xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? + "l" : "-"); + xo_emit(" "); + xo_open_list("fd_flags"); + if (fst->fs_fflags & PS_FST_FFLAG_READ) + xo_emit("{elq:fd_flags/read}"); + if (fst->fs_fflags & PS_FST_FFLAG_WRITE) + xo_emit("{elq:fd_flags/write}"); + if (fst->fs_fflags & PS_FST_FFLAG_APPEND) + xo_emit("{elq:fd_flags/append}"); + if (fst->fs_fflags & PS_FST_FFLAG_ASYNC) + xo_emit("{elq:fd_flags/async}"); + if (fst->fs_fflags & PS_FST_FFLAG_SYNC) + xo_emit("{elq:fd_flags/fsync}"); + if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK) + xo_emit("{elq:fd_flags/nonblocking}"); + if (fst->fs_fflags & PS_FST_FFLAG_DIRECT) + xo_emit("{elq:fd_flags/direct_io}"); + if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK) + xo_emit("{elq:fd_flags/lock_held}"); + xo_close_list("fd_flags"); + if (!Cflag) { if (fst->fs_ref_count > -1) - printf("%3d ", fst->fs_ref_count); + xo_emit("{:ref_count/%3d/%d} ", + fst->fs_ref_count); else - printf("%3c ", '-'); + xo_emit("{q:ref_count/%3c/%c} ", '-'); if (fst->fs_offset > -1) - printf("%7jd ", (intmax_t)fst->fs_offset); + xo_emit("{:offset/%7jd/%jd} ", + (intmax_t)fst->fs_offset); else - printf("%7c ", '-'); + xo_emit("{q:offset/%7c/%c} ", '-'); } if (Cflag) { print_capability(&fst->fs_cap_rights, capwidth); - printf(" "); + xo_emit(" "); } switch (fst->fs_type) { case PS_FST_TYPE_SOCKET: - error = procstat_get_socket_info(procstat, fst, &sock, NULL); + error = procstat_get_socket_info(procstat, fst, &sock, + NULL); if (error != 0) break; - printf("%-3s ", + xo_emit("{:protocol/%-3s/%s} ", protocol_to_string(sock.dom_family, sock.type, sock.proto)); /* * While generally we like to print two addresses, * local and peer, for sockets, it turns out to be * more useful to print the first non-nul address for * local sockets, as typically they aren't bound and * connected, and the path strings can get long. */ if (sock.dom_family == AF_LOCAL) { struct sockaddr_un *sun = (struct sockaddr_un *)&sock.sa_local; if (sun->sun_path[0] != 0) - print_address(&sock.sa_local); + addr_to_string(&sock.sa_local, + src_addr, sizeof(src_addr)); else - print_address(&sock.sa_peer); + addr_to_string(&sock.sa_peer, + src_addr, sizeof(src_addr)); + xo_emit("{:path/%s}", src_addr); } else { - print_address(&sock.sa_local); - printf(" "); - print_address(&sock.sa_peer); + addr_to_string(&sock.sa_local, src_addr, + sizeof(src_addr)); + addr_to_string(&sock.sa_peer, dst_addr, + sizeof(dst_addr)); + xo_emit("{:path/%s %s}", src_addr, dst_addr); } break; default: - printf("%-3s ", "-"); - printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-"); + xo_emit("{:protocol/%-3s/%s} ", "-"); + xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ? + fst->fs_path : "-"); } - printf("\n"); + xo_emit("\n"); + xo_close_instance("files"); } + xo_close_list("files"); procstat_freefiles(procstat, head); } Index: head/usr.bin/procstat/procstat_kstack.c =================================================================== --- head/usr.bin/procstat/procstat_kstack.c (revision 287485) +++ head/usr.bin/procstat/procstat_kstack.c (revision 287486) @@ -1,206 +1,246 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include "procstat.h" /* * Walk the stack trace provided by the kernel and reduce it to what we * actually want to print. This involves stripping true instruction pointers, * frame numbers, and carriage returns as generated by stack(9). If -kk is * specified, print the function and offset, otherwise just the function. */ enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF }; static enum trace_state kstack_nextstate(enum trace_state ts) { switch (ts) { case TS_FRAMENUM: return (TS_PC); case TS_PC: return (TS_AT); case TS_AT: return (TS_FUNC); case TS_FUNC: return (TS_OFF); case TS_OFF: return TS_FRAMENUM; default: errx(-1, "kstack_nextstate"); } } static void kstack_cleanup(const char *old, char *new, int kflag) { enum trace_state old_ts, ts; const char *cp_old; char *cp_new; ts = TS_FRAMENUM; for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) { switch (*cp_old) { case ' ': case '\n': case '+': old_ts = ts; ts = kstack_nextstate(old_ts); if (old_ts == TS_OFF) { *cp_new = ' '; cp_new++; } if (kflag > 1 && old_ts == TS_FUNC) { *cp_new = '+'; cp_new++; } continue; } if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) { *cp_new = *cp_old; cp_new++; } } *cp_new = '\0'; } +static void +kstack_cleanup_encoded(const char *old, char *new, int kflag) +{ + enum trace_state old_ts, ts; + const char *cp_old; + char *cp_new, *cp_loop, *cp_tofree, *cp_line; + + ts = TS_FRAMENUM; + if (kflag == 1) { + for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) { + switch (*cp_old) { + case '\n': + *cp_new = *cp_old; + cp_new++; + case ' ': + case '+': + old_ts = ts; + ts = kstack_nextstate(old_ts); + continue; + } + if (ts == TS_FUNC) { + *cp_new = *cp_old; + cp_new++; + } + } + cp_tofree = cp_loop = strdup(new); + } else + cp_tofree = cp_loop = strdup(old); + while ((cp_line = strsep(&cp_loop, "\n")) != NULL) { + if (strlen(cp_line) != 0 && *cp_line != 127) + xo_emit("{le:token/%s}", cp_line); + } + *cp_new = '\0'; + free(cp_tofree); +} + /* * Sort threads by tid. */ static int kinfo_kstack_compare(const void *a, const void *b) { return ((const struct kinfo_kstack *)a)->kkst_tid - ((const struct kinfo_kstack *)b)->kkst_tid; } static void kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count) { qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare); } void procstat_kstack(struct procstat *procstat, struct kinfo_proc *kipp, int kflag) { struct kinfo_kstack *kkstp, *kkstp_free; struct kinfo_proc *kip, *kip_free; - char trace[KKST_MAXLEN]; + char trace[KKST_MAXLEN], encoded_trace[KKST_MAXLEN]; unsigned int i, j; unsigned int kip_count, kstk_count; if (!hflag) - printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM", + xo_emit("{T:/%5s %6s %-16s %-16s %-29s}\n", "PID", "TID", "COMM", "TDNAME", "KSTACK"); kkstp = kkstp_free = procstat_getkstack(procstat, kipp, &kstk_count); if (kkstp == NULL) return; /* * We need to re-query for thread information, so don't use *kipp. */ kip = kip_free = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &kip_count); if (kip == NULL) { procstat_freekstack(procstat, kkstp_free); return; } kinfo_kstack_sort(kkstp, kstk_count); for (i = 0; i < kstk_count; i++) { kkstp = &kkstp_free[i]; /* * Look up the specific thread using its tid so we can * display the per-thread command line. */ kipp = NULL; for (j = 0; j < kip_count; j++) { kipp = &kip_free[j]; if (kkstp->kkst_tid == kipp->ki_tid) break; } if (kipp == NULL) continue; - printf("%5d ", kipp->ki_pid); - printf("%6d ", kkstp->kkst_tid); - printf("%-16s ", kipp->ki_comm); - printf("%-16s ", (strlen(kipp->ki_tdname) && + xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:thread_id/%6d/%d} ", kkstp->kkst_tid); + xo_emit("{:command/%-16s/%s} ", kipp->ki_comm); + xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) && (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? kipp->ki_tdname : "-"); switch (kkstp->kkst_state) { case KKST_STATE_RUNNING: - printf("%-29s\n", ""); + xo_emit("{:state/%-29s/%s}\n", ""); continue; case KKST_STATE_SWAPPED: - printf("%-29s\n", ""); + xo_emit("{:state/%-29s/%s}\n", ""); continue; case KKST_STATE_STACKOK: break; default: - printf("%-29s\n", ""); + xo_emit("{:state/%-29s/%s}\n", ""); continue; } /* * The kernel generates a trace with carriage returns between * entries, but for a more compact view, we convert carriage * returns to spaces. */ kstack_cleanup(kkstp->kkst_trace, trace, kflag); - printf("%-29s\n", trace); + xo_open_list("trace"); + kstack_cleanup_encoded(kkstp->kkst_trace, encoded_trace, kflag); + xo_close_list("trace"); + xo_emit("{d:trace/%-29s}\n", trace); } procstat_freekstack(procstat, kkstp_free); procstat_freeprocs(procstat, kip_free); } Index: head/usr.bin/procstat/procstat_rlimit.c =================================================================== --- head/usr.bin/procstat/procstat_rlimit.c (revision 287485) +++ head/usr.bin/procstat/procstat_rlimit.c (revision 287486) @@ -1,107 +1,126 @@ /*- * Copyright (c) 2011 Mikolaj Golub + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "procstat.h" static struct { const char *name; const char *suffix; } rlimit_param[14] = { {"cputime", "sec"}, {"filesize", "B "}, {"datasize", "B "}, {"stacksize", "B "}, {"coredumpsize", "B "}, {"memoryuse", "B "}, {"memorylocked", "B "}, {"maxprocesses", " "}, {"openfiles", " "}, {"sbsize", "B "}, {"vmemoryuse", "B "}, {"pseudo-terminals", " "}, {"swapuse", "B "}, {"kqueues", " "}, }; #if RLIM_NLIMITS > 14 #error "Resource limits have grown. Add new entries to rlimit_param[]." #endif static const char * humanize_rlimit(int indx, rlim_t limit) { static char buf[14]; int scale; if (limit == RLIM_INFINITY) return ("infinity "); scale = humanize_number(buf, sizeof(buf) - 1, (int64_t)limit, rlimit_param[indx].suffix, HN_AUTOSCALE | HN_GETSCALE, HN_DECIMAL); (void)humanize_number(buf, sizeof(buf) - 1, (int64_t)limit, rlimit_param[indx].suffix, HN_AUTOSCALE, HN_DECIMAL); /* Pad with one space if there is no suffix prefix. */ if (scale == 0) sprintf(buf + strlen(buf), " "); return (buf); } void procstat_rlimit(struct procstat *prstat, struct kinfo_proc *kipp) { struct rlimit rlimit; int i; if (!hflag) { - printf("%5s %-16s %-16s %16s %16s\n", + xo_emit("{T:/%5s %-16s %-16s %16s %16s}\n", "PID", "COMM", "RLIMIT", "SOFT ", "HARD "); } + xo_emit("{ek:process_id/%5d}{e:command/%-16s/%s}", kipp->ki_pid, + kipp->ki_comm); for (i = 0; i < RLIM_NLIMITS; i++) { if (procstat_getrlimit(prstat, kipp, i, &rlimit) == -1) return; - printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm, + xo_emit("{dk:process_id/%5d} {d:command/%-16s} " + "{d:rlimit_param/%-16s} ", kipp->ki_pid, kipp->ki_comm, rlimit_param[i].name); - printf("%16s ", humanize_rlimit(i, rlimit.rlim_cur)); - printf("%16s\n", humanize_rlimit(i, rlimit.rlim_max)); + + xo_open_container(rlimit_param[i].name); + if (rlimit.rlim_cur == RLIM_INFINITY) + xo_emit("{e:soft_limit/infinity}"); + else + xo_emit("{e:soft_limit/%U}", rlimit.rlim_cur); + + if (rlimit.rlim_max == RLIM_INFINITY) + xo_emit("{e:hard_limit/infinity}"); + else + xo_emit("{e:hard_limit/%U}", rlimit.rlim_max); + xo_close_container(rlimit_param[i].name); + + xo_emit("{d:rlim_cur/%16s} ", + humanize_rlimit(i, rlimit.rlim_cur)); + xo_emit("{d:rlim_max/%16s}\n", + humanize_rlimit(i, rlimit.rlim_max)); } } Index: head/usr.bin/procstat/procstat_rusage.c =================================================================== --- head/usr.bin/procstat/procstat_rusage.c (revision 287485) +++ head/usr.bin/procstat/procstat_rusage.c (revision 287486) @@ -1,161 +1,197 @@ /*- * Copyright (c) 2012 Hudson River Trading LLC * Written by: John H. Baldwin + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include +#include #include #include "procstat.h" static struct { const char *ri_name; bool ri_humanize; int ri_scale; } rusage_info[] = { { "maximum RSS", true, 1 }, { "integral shared memory", true, 1 }, { "integral unshared data", true, 1 }, { "integral unshared stack", true, 1 }, { "page reclaims", false, 0 }, { "page faults", false, 0 }, { "swaps", false, 0 }, { "block reads", false, 0 }, { "block writes", false, 0 }, { "messages sent", false, 0 }, { "messages received", false, 0 }, { "signals received", false, 0 }, { "voluntary context switches", false, 0 }, { "involuntary context switches", false, 0 } }; /* xxx days hh:mm:ss.uuuuuu */ static const char * format_time(struct timeval *tv) { static char buffer[32]; int days, hours, minutes, seconds, used; minutes = tv->tv_sec / 60; seconds = tv->tv_sec % 60; hours = minutes / 60; minutes %= 60; days = hours / 24; hours %= 24; used = 0; if (days == 1) used += snprintf(buffer, sizeof(buffer), "1 day "); else if (days > 0) used += snprintf(buffer, sizeof(buffer), "%u days ", days); - snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u ", + snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u", hours, minutes, seconds, (unsigned int)tv->tv_usec); return (buffer); } static const char * format_value(long value, bool humanize, int scale) { static char buffer[14]; if (scale != 0) value <<= scale * 10; if (humanize) humanize_number(buffer, sizeof(buffer), value, "B", scale, HN_DECIMAL); else snprintf(buffer, sizeof(buffer), "%ld ", value); return (buffer); } static void print_prefix(struct kinfo_proc *kipp) { - printf("%5d ", kipp->ki_pid); + xo_emit("{d:process_id/%5d/%d} ", kipp->ki_pid); if (Hflag) - printf("%6d ", kipp->ki_tid); - printf("%-16s ", kipp->ki_comm); + xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); + xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); } static void print_rusage(struct kinfo_proc *kipp) { long *lp; unsigned int i; + char *field, *threadid; print_prefix(kipp); - printf("%-14s %32s\n", "user time", + xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "user time", format_time(&kipp->ki_rusage.ru_utime)); print_prefix(kipp); - printf("%-14s %32s\n", "system time", + xo_emit("{d:resource/%-14s} {d:usage/%29s}{P: }\n", "system time", format_time(&kipp->ki_rusage.ru_stime)); + + if (Hflag) { + asprintf(&threadid, "%d", kipp->ki_tid); + if (threadid == NULL) + xo_errc(1, ENOMEM, + "Failed to allocate memory in print_rusage()"); + xo_open_container(threadid); + xo_emit("{e:thread_id/%d}", kipp->ki_tid); + } else { + xo_emit("{e:process_id/%d}", kipp->ki_pid); + xo_emit("{e:command/%s}", kipp->ki_comm); + } + xo_emit("{e:user time/%s}", format_time(&kipp->ki_rusage.ru_utime)); + xo_emit("{e:system time/%s}", format_time(&kipp->ki_rusage.ru_stime)); + lp = &kipp->ki_rusage.ru_maxrss; for (i = 0; i < nitems(rusage_info); i++) { print_prefix(kipp); - printf("%-32s %14s\n", rusage_info[i].ri_name, + asprintf(&field, "{e:%s/%%D}", rusage_info[i].ri_name); + if (field == NULL) + xo_errc(1, ENOMEM, + "Failed to allocate memory in print_rusage()"); + xo_emit(field, *lp); + free(field); + xo_emit("{d:resource/%-32s} {d:usage/%14s}\n", + rusage_info[i].ri_name, format_value(*lp, rusage_info[i].ri_humanize, - rusage_info[i].ri_scale)); + rusage_info[i].ri_scale)); lp++; } + if (Hflag) { + xo_close_container(threadid); + free(threadid); + } } void procstat_rusage(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; unsigned int count, i; if (!hflag) { - printf("%5s ", "PID"); + xo_emit("{d:ta/%5s} ", "PID"); if (Hflag) - printf("%6s ", "TID"); - printf("%-16s %-32s %14s\n", "COMM", "RESOURCE", + xo_emit("{d:tb/%6s} ", "TID"); + xo_emit("{d:tc/%-16s %-32s %14s}\n", "COMM", "RESOURCE", "VALUE "); } if (!Hflag) { print_rusage(kipp); return; } + xo_emit("{e:process_id/%d}", kipp->ki_pid); + xo_emit("{e:command/%s}", kipp->ki_comm); + xo_open_container("threads"); + kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &count); if (kip == NULL) return; kinfo_proc_sort(kip, count); - for (i = 0; i < count; i++) + for (i = 0; i < count; i++) { print_rusage(&kip[i]); + } + + xo_close_container("threads"); procstat_freeprocs(procstat, kip); } Index: head/usr.bin/procstat/procstat_sigs.c =================================================================== --- head/usr.bin/procstat/procstat_sigs.c (revision 287485) +++ head/usr.bin/procstat/procstat_sigs.c (revision 287486) @@ -1,120 +1,180 @@ /*- * Copyright (c) 2010 Konstantin Belousov + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include "procstat.h" static void procstat_print_signame(int sig) { char name[12]; int i; if (!nflag && sig < sys_nsig) { strlcpy(name, sys_signame[sig], sizeof(name)); for (i = 0; name[i] != 0; i++) name[i] = toupper(name[i]); - printf("%-7s ", name); + xo_emit("{d:signal/%-7s/%s} ", name); + xo_open_container(name); + } else { + xo_emit("{d:signal/%-7d/%d} ", sig); + snprintf(name, 12, "%d", sig); + xo_open_container(name); + } +} + +static void +procstat_close_signame(int sig) +{ + char name[12]; + int i; + + if (!nflag && sig < sys_nsig) { + strlcpy(name, sys_signame[sig], sizeof(name)); + for (i = 0; name[i] != 0; i++) + name[i] = toupper(name[i]); + xo_close_container(name); } else - printf("%-7d ", sig); + snprintf(name, 12, "%d", sig); + xo_close_container(name); } static void procstat_print_sig(const sigset_t *set, int sig, char flag) { - - printf("%c", sigismember(set, sig) ? flag : '-'); + xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-'); + switch (flag) { + case 'B': + xo_emit("{en:mask/%s}", sigismember(set, sig) ? + "true" : "false"); + break; + case 'C': + xo_emit("{en:catch/%s}", sigismember(set, sig) ? + "true" : "false"); + break; + case 'P': + xo_emit("{en:list/%s}", sigismember(set, sig) ? + "true" : "false"); + break; + case 'I': + xo_emit("{en:ignore/%s}", sigismember(set, sig) ? + "true" : "false"); + break; + default: + xo_emit("{en:unknown/%s}", sigismember(set, sig) ? + "true" : "false"); + break; + } } void procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) { int j; - pid_t pid; - pid = kipp->ki_pid; if (!hflag) - printf("%5s %-16s %-7s %4s\n", "PID", "COMM", "SIG", "FLAGS"); + xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG", + "FLAGS"); + xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); + xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); + xo_open_container("signals"); for (j = 1; j <= _SIG_MAXSIG; j++) { - printf("%5d ", pid); - printf("%-16s ", kipp->ki_comm); + xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); procstat_print_signame(j); - printf(" "); + xo_emit(" "); procstat_print_sig(&kipp->ki_siglist, j, 'P'); procstat_print_sig(&kipp->ki_sigignore, j, 'I'); procstat_print_sig(&kipp->ki_sigcatch, j, 'C'); - printf("\n"); + procstat_close_signame(j); + xo_emit("\n"); } + xo_close_container("signals"); } void procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; - pid_t pid; int j; unsigned int count, i; + char *threadid; - pid = kipp->ki_pid; if (!hflag) - printf("%5s %6s %-16s %-7s %4s\n", "PID", "TID", "COMM", + xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM", "SIG", "FLAGS"); kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, - pid, &count); + kipp->ki_pid, &count); if (kip == NULL) return; + xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); + xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); + xo_open_container("threads"); kinfo_proc_sort(kip, count); for (i = 0; i < count; i++) { kipp = &kip[i]; + asprintf(&threadid, "%d", kipp->ki_tid); + if (threadid == NULL) + xo_errc(1, ENOMEM, "Failed to allocate memory in " + "procstat_threads_sigs()"); + xo_open_container(threadid); + xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid); + xo_open_container("signals"); for (j = 1; j <= _SIG_MAXSIG; j++) { - printf("%5d ", pid); - printf("%6d ", kipp->ki_tid); - printf("%-16s ", kipp->ki_comm); + xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); + xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); procstat_print_signame(j); - printf(" "); + xo_emit(" "); procstat_print_sig(&kipp->ki_siglist, j, 'P'); procstat_print_sig(&kipp->ki_sigmask, j, 'B'); - printf("\n"); + procstat_close_signame(j); + xo_emit("\n"); } + xo_close_container("signals"); + xo_close_container(threadid); + free(threadid); } + xo_close_container("threads"); procstat_freeprocs(procstat, kip); } Index: head/usr.bin/procstat/procstat_threads.c =================================================================== --- head/usr.bin/procstat/procstat_threads.c (revision 287485) +++ head/usr.bin/procstat/procstat_threads.c (revision 287486) @@ -1,118 +1,134 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include "procstat.h" void procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; unsigned int count, i; const char *str; + char *threadid; if (!hflag) - printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", + xo_emit("{T:/%5s %6s %-16s %-16s %2s %4s %-7s %-9s}\n", "PID", "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); + xo_emit("{ek:process_id/%d}", kipp->ki_pid); + xo_emit("{e:command/%s}", strlen(kipp->ki_comm) ? + kipp->ki_comm : "-"); + xo_open_container("threads"); + kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &count); if (kip == NULL) return; kinfo_proc_sort(kip, count); for (i = 0; i < count; i++) { kipp = &kip[i]; - printf("%5d ", kipp->ki_pid); - printf("%6d ", kipp->ki_tid); - printf("%-16s ", strlen(kipp->ki_comm) ? + asprintf(&threadid, "%d", kipp->ki_tid); + if (threadid == NULL) + xo_errc(1, ENOMEM, "Failed to allocate memory in " + "procstat_threads()"); + xo_open_container(threadid); + xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); + xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid); + xo_emit("{d:command/%-16s/%s} ", strlen(kipp->ki_comm) ? kipp->ki_comm : "-"); - printf("%-16s ", (strlen(kipp->ki_tdname) && + xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) && (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? kipp->ki_tdname : "-"); if (kipp->ki_oncpu != 255) - printf("%3d ", kipp->ki_oncpu); + xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu); else if (kipp->ki_lastcpu != 255) - printf("%3d ", kipp->ki_lastcpu); + xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu); else - printf("%3s ", "-"); - printf("%4d ", kipp->ki_pri.pri_level); + xo_emit("{:cpu/%3s/%s} ", "-"); + xo_emit("{:priority/%4d/%d} ", kipp->ki_pri.pri_level); switch (kipp->ki_stat) { case SRUN: str = "run"; break; case SSTOP: str = "stop"; break; case SSLEEP: str = "sleep"; break; case SLOCK: str = "lock"; break; case SWAIT: str = "wait"; break; case SZOMB: str = "zomb"; break; case SIDL: str = "idle"; break; default: str = "??"; break; } - printf("%-7s ", str); + xo_emit("{:run_state/%-7s/%s} ", str); if (kipp->ki_kiflag & KI_LOCKBLOCK) { - printf("*%-8s ", strlen(kipp->ki_lockname) ? + xo_emit("{:lock_name/*%-8s/%s} ", + strlen(kipp->ki_lockname) ? kipp->ki_lockname : "-"); } else { - printf("%-9s ", strlen(kipp->ki_wmesg) ? - kipp->ki_wmesg : "-"); + xo_emit("{:wait_channel/%-9s/%s} ", + strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-"); } - printf("\n"); + xo_close_container(threadid); + free(threadid); + xo_emit("\n"); } + xo_close_container("threads"); procstat_freeprocs(procstat, kip); } Index: head/usr.bin/procstat/procstat_vm.c =================================================================== --- head/usr.bin/procstat/procstat_vm.c (revision 287485) +++ head/usr.bin/procstat/procstat_vm.c (revision 287486) @@ -1,115 +1,164 @@ /*- * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2015 Allan Jude * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include "procstat.h" void procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_vmentry *freep, *kve; int ptrwidth; int i, cnt; - const char *str; + const char *str, *lstr; ptrwidth = 2*sizeof(void *) + 2; if (!hflag) - printf("%5s %*s %*s %3s %4s %4s %3s %3s %-4s %-2s %-s\n", + xo_emit("{T:/%5s %*s %*s %3s %4s %4s %3s %3s %-4s %-2s %-s}\n", "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES", "PRES", "REF", "SHD", "FLAG", "TP", "PATH"); + xo_emit("{ek:process_id/%d}", kipp->ki_pid); + freep = procstat_getvmmap(procstat, kipp, &cnt); if (freep == NULL) return; + xo_open_list("vm"); for (i = 0; i < cnt; i++) { + xo_open_instance("vm"); kve = &freep[i]; - printf("%5d ", kipp->ki_pid); - printf("%#*jx ", ptrwidth, (uintmax_t)kve->kve_start); - printf("%#*jx ", ptrwidth, (uintmax_t)kve->kve_end); - printf("%s", kve->kve_protection & KVME_PROT_READ ? "r" : "-"); - printf("%s", kve->kve_protection & KVME_PROT_WRITE ? "w" : "-"); - printf("%s ", kve->kve_protection & KVME_PROT_EXEC ? "x" : "-"); - printf("%4d ", kve->kve_resident); - printf("%4d ", kve->kve_private_resident); - printf("%3d ", kve->kve_ref_count); - printf("%3d ", kve->kve_shadow_count); - printf("%-1s", kve->kve_flags & KVME_FLAG_COW ? "C" : "-"); - printf("%-1s", kve->kve_flags & KVME_FLAG_NEEDS_COPY ? "N" : - "-"); - printf("%-1s", kve->kve_flags & KVME_FLAG_SUPER ? "S" : "-"); - printf("%-1s ", kve->kve_flags & KVME_FLAG_GROWS_UP ? "U" : - kve->kve_flags & KVME_FLAG_GROWS_DOWN ? "D" : "-"); + xo_emit("{dk:process_id/%5d} ", kipp->ki_pid); + xo_emit("{d:kve_start/%#*jx} ", ptrwidth, + (uintmax_t)kve->kve_start); + xo_emit("{d:kve_end/%#*jx} ", ptrwidth, + (uintmax_t)kve->kve_end); + xo_emit("{e:kve_start/%#jx}", (uintmax_t)kve->kve_start); + xo_emit("{e:kve_end/%#jx}", (uintmax_t)kve->kve_end); + xo_emit("{d:read/%s}", kve->kve_protection & KVME_PROT_READ ? + "r" : "-"); + xo_emit("{d:write/%s}", kve->kve_protection & KVME_PROT_WRITE ? + "w" : "-"); + xo_emit("{d:exec/%s} ", kve->kve_protection & KVME_PROT_EXEC ? + "x" : "-"); + xo_open_container("kve_protection"); + xo_emit("{en:read/%s}", kve->kve_protection & KVME_PROT_READ ? + "true" : "false"); + xo_emit("{en:write/%s}", kve->kve_protection & KVME_PROT_WRITE ? + "true" : "false"); + xo_emit("{en:exec/%s}", kve->kve_protection & KVME_PROT_EXEC ? + "true" : "false"); + xo_close_container("kve_protection"); + xo_emit("{:kve_resident/%4d/%d} ", kve->kve_resident); + xo_emit("{:kve_private_resident/%4d/%d} ", + kve->kve_private_resident); + xo_emit("{:kve_ref_count/%3d/%d} ", kve->kve_ref_count); + xo_emit("{:kve_shadow_count/%3d/%d} ", kve->kve_shadow_count); + xo_emit("{d:copy_on_write/%-1s}", kve->kve_flags & + KVME_FLAG_COW ? "C" : "-"); + xo_emit("{d:need_copy/%-1s}", kve->kve_flags & + KVME_FLAG_NEEDS_COPY ? "N" : "-"); + xo_emit("{d:super_pages/%-1s}", kve->kve_flags & + KVME_FLAG_SUPER ? "S" : "-"); + xo_emit("{d:grows_down/%-1s} ", kve->kve_flags & + KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags & + KVME_FLAG_GROWS_DOWN ? "D" : "-"); + xo_open_container("kve_flags"); + xo_emit("{en:copy_on_write/%s}", kve->kve_flags & + KVME_FLAG_COW ? "true" : "false"); + xo_emit("{en:needs_copy/%s}", kve->kve_flags & + KVME_FLAG_NEEDS_COPY ? "true" : "false"); + xo_emit("{en:super_pages/%s}", kve->kve_flags & + KVME_FLAG_SUPER ? "true" : "false"); + xo_emit("{en:grows_up/%s}", kve->kve_flags & + KVME_FLAG_GROWS_UP ? "true" : "false"); + xo_emit("{en:grows_down/%s}", kve->kve_flags & + KVME_FLAG_GROWS_DOWN ? "true" : "false"); + xo_close_container("kve_flags"); switch (kve->kve_type) { case KVME_TYPE_NONE: str = "--"; + lstr = "none"; break; case KVME_TYPE_DEFAULT: str = "df"; + lstr = "default"; break; case KVME_TYPE_VNODE: str = "vn"; + lstr = "vnode"; break; case KVME_TYPE_SWAP: str = "sw"; + lstr = "swap"; break; case KVME_TYPE_DEVICE: str = "dv"; + lstr = "device"; break; case KVME_TYPE_PHYS: str = "ph"; + lstr = "physical"; break; case KVME_TYPE_DEAD: str = "dd"; + lstr = "dead"; break; case KVME_TYPE_SG: str = "sg"; + lstr = "scatter/gather"; break; case KVME_TYPE_MGTDEVICE: str = "md"; + lstr = "managed_device"; break; case KVME_TYPE_UNKNOWN: default: str = "??"; + lstr = "unknown"; break; } - printf("%-2s ", str); - printf("%-s\n", kve->kve_path); + xo_emit("{d:kve_type/%-2s} ", str); + xo_emit("{e:kve_type/%s}", lstr); + xo_emit("{:kve_path/%-s/%s}\n", kve->kve_path); + xo_close_instance("vm"); } + xo_close_list("vm"); free(freep); }