Index: head/usr.bin/procstat/Makefile =================================================================== --- head/usr.bin/procstat/Makefile (revision 365879) +++ head/usr.bin/procstat/Makefile (revision 365880) @@ -1,28 +1,38 @@ # $FreeBSD$ .include 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_penv.c \ procstat_ptlwpinfo.c \ + procstat_pwdx.c \ procstat_rlimit.c \ procstat_rusage.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c +MLINKS+= procstat.1 pargs.1 +MLINKS+= procstat.1 penv.1 +MLINKS+= procstat.1 pwdx.1 + LIBADD+= procstat xo util sbuf + +LINKS+= ${BINDIR}/procstat ${BINDIR}/pargs +LINKS+= ${BINDIR}/procstat ${BINDIR}/penv +LINKS+= ${BINDIR}/procstat ${BINDIR}/pwdx HAS_TESTS= SUBDIR.${MK_TESTS}+= tests .include Index: head/usr.bin/procstat/procstat.1 =================================================================== --- head/usr.bin/procstat/procstat.1 (revision 365879) +++ head/usr.bin/procstat/procstat.1 (revision 365880) @@ -1,722 +1,753 @@ .\"- .\" 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 March 4, 2019 +.Dd September 14, 2020 .Dt PROCSTAT 1 .Os .Sh NAME .Nm procstat .Nd get detailed process information .Sh SYNOPSIS .Nm .Op Fl -libxo .Op Fl h .Op Fl M Ar core .Op Fl N Ar system .Op Fl w Ar interval .Ar command .Op Ar pid ... | Ar core ... .Nm .Op Fl -libxo .Fl a .Op Fl h .Op Fl M Ar core .Op Fl N Ar system .Op Fl w Ar interval .Ar command .Nm .Op Fl -libxo .Op Fl h .Op Fl M Ar core .Op Fl N Ar system .Op Fl w Ar interval .Oo .Fl b | .Fl c | .Fl e | .Fl f Oo Fl C Oc | .Fl i Oo Fl n Oc | .Fl j Oo Fl n Oc | .Fl k Oo Fl k Oc | .Fl l | .Fl r Oo Fl H Oc | .Fl s | .Fl S | .Fl t | .Fl v | .Fl x .Oc .Op Ar pid ... | Ar core ... .Nm .Op Fl -libxo .Fl a .Op Fl h .Op Fl M Ar core .Op Fl N Ar system .Op Fl w Ar interval .Oo .Fl b | .Fl c | .Fl e | .Fl f Oo Fl C Oc | .Fl i Oo Fl n Oc | .Fl j Oo Fl n Oc | .Fl k Oo Fl k Oc | .Fl l | .Fl r Oo Fl H Oc | .Fl s | .Fl S | .Fl t | .Fl v | .Fl x .Oc .Nm .Op Fl -libxo .Fl L .Op Fl h .Op Fl M Ar core .Op Fl N Ar system .Op Fl w Ar interval .Ar core ... +.Nm pargs +.Op Fl -libxo +.Ar pid ... +.Nm penv +.Op Fl -libxo +.Ar pid ... +.Nm pwdx +.Op Fl -libxo +.Ar pid ... .Sh DESCRIPTION .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 +The +.Nm pargs , +.Nm penv +and +.Nm pwdx +utilities display the arguments, environment, and current working directory, +respectively of the process specified by +.Ar pid +argument. +They mimic the behavior of Solaris utilities of the same names. +.Pp If the .Fl -libxo flag is specified the output is generated 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. .Pp -The following commands are available: +The following commands are available for +.Nm : .Bl -tag -width indent .It Ar basic Print basic process statistics (this is the default). .It Ar binary | Fl b Display binary information for the process. .Pp Substring commands are accepted. .It Ar argument(s) | Fl c Display command line arguments for the process. .Pp Substring commands are accepted. .It Ar environment | Fl e Display environment variables for the process. .Pp Substring commands are accepted. .It Ar file(s) | Ar fd(s) | Fl f Display file descriptor information for the process. .Pp If the .Fl C subcommand flag is used then additional capability information is printed. .It Ar signal(s) | Fl i Display signal pending and disposition information for the process. .Pp If the .Fl n subcommand option is used, the signal numbers are shown instead of signal names. .Pp Substring commands are accepted. .It Ar tsignal(s) | Fl j Display signal pending and blocked information for the process's threads. .Pp If the .Fl n subcommand option is used, the signal numbers are shown instead of signal names. .Pp Substring commands are accepted. .It Ar kstack | 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. .Pp If the .Fl v subcommand option is used (or the command flag is repeated), function offsets as well as function names are printed. .It Ar rlimit | Fl l Display resource limits for the process. .It Ar ptlwpinfo | Fl L Display LWP info for the process pertaining to its signal driven exit. .It Ar rusage | Fl r Display resource usage information for the process. .Pp If the .Fl v .Pq or Fl H subcommand flag is used then per-thread statistics are printed, rather than per-process statistics. The second field in the table will list the thread ID to which the row of information corresponds. .It Ar credential(s) | Fl s Display security credential information for the process. .Pp Substring commands are accepted. .It Ar cpuset | Ar cs | Fl S Display the cpuset information for the thread. .It Ar thread(s) | Fl t Display thread information for the process. .It Ar vm | Fl v Display virtual memory mappings for the process. .It Ar auxv | Fl x Display ELF auxiliary vector for the process. +.It Ar pargs +Display arguments for the process. +.It Ar penv +Display environment variables for the process. +.It Ar pwdx +Display current working directory 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 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 process descriptor .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. .Pp The following network protocols may be displayed (grouped by address family): .Pp .Dv AF_INET , .Dv AF_INET6 .Pp .Bl -tag -width indent -compact .It ICM .Dv IPPROTO_ICMP ; see .Xr icmp 4 . .It IPD .Dv IPPROTO_DIVERT ; see .Xr divert 4 . .It IP? unknown protocol. .It RAW .Dv IPPROTO_RAW ; see .Xr ip 4 . .It SCT .Dv IPPROTO_SCTP ; see .Xr sctp 4 . .It TCP .Dv IPPROTO_TCP ; see .Xr tcp 4 . .It UDP .Dv IPPROTO_UDP ; see .Xr udp 4 . .El .Pp .Dv AF_LOCAL .Pp .Bl -tag -width indent -compact .It UDD .Dv IPPROTO_UDP ; see .Xr udp 4 . .It UDS .Dv IPPROTO_TCP ; see .Xr tcp 4 . .It UD? unknown protocol. .El .Pp .Bl -tag -width indent -compact .It ? unknown address family. .El .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 -.Dv SIG_IGN; +.Dv SIG_IGN ; - otherwise. .It C if the signal will be caught; - 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) .It W pages in this range are locked by .Xr mlock 2 or .Xr mlockall 2 .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 mlock 2 , .Xr mlockall 2 , .Xr libprocstat 3 , .Xr libxo 3 , .Xr signal 3 , .Xr xo_parse_args 3 , .Xr ddb 4 , .Xr divert 4 , .Xr icmp 4 , .Xr ip 4 , .Xr sctp 4 , .Xr tcp 4 , .Xr udp 4 , .Xr stack 9 .Sh AUTHORS .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 . +.br +.An Juraj Lutter +.Aq Mt juraj@lutter.sk +added the pargs, penv and pwdx functionality. .Sh BUGS 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 365879) +++ head/usr.bin/procstat/procstat.c (revision 365880) @@ -1,564 +1,626 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2007, 2011 Robert N. M. Watson * Copyright (c) 2015 Allan Jude * Copyright (c) 2017 Dell EMC * 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" enum { PS_CMP_NORMAL = 0x00, PS_CMP_PLURAL = 0x01, PS_CMP_SUBSTR = 0x02 }; struct procstat_cmd { const char *command; const char *xocontainer; const char *usage; void (*cmd)(struct procstat *, struct kinfo_proc *); void (*opt)(int, char * const *); int cmp; }; int procstat_opts = 0; static void cmdopt_none(int argc, char * const argv[]); static void cmdopt_verbose(int argc, char * const argv[]); static void cmdopt_signals(int argc, char * const argv[]); static void cmdopt_rusage(int argc, char * const argv[]); static void cmdopt_files(int argc, char * const argv[]); static void cmdopt_cpuset(int argc, char * const argv[]); +static const char *progname; + +/* aliased program parameters and arguments + * - usage field is abused to hold the pointer to the function + * displaying program usage + */ +static const struct procstat_cmd pacmd_table[] = { + /* arguments are the same as for pwdx: pid or core file */ + { "pargs", "args", NULL, &procstat_pargs, &cmdopt_none, + PS_CMP_NORMAL | PS_MODE_COMPAT }, + { "penv", "env", NULL, &procstat_penv, &cmdopt_none, + PS_CMP_NORMAL | PS_MODE_COMPAT }, + { "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none, + PS_CMP_NORMAL | PS_MODE_COMPAT } +}; + +/* procstat parameters and arguments */ static const struct procstat_cmd cmd_table[] = { { "argument", "arguments", NULL, &procstat_args, &cmdopt_none, PS_CMP_PLURAL | PS_CMP_SUBSTR }, { "auxv", "auxv", NULL, &procstat_auxv, &cmdopt_none, PS_CMP_NORMAL }, { "basic", "basic", NULL, &procstat_basic, &cmdopt_none, PS_CMP_NORMAL }, { "binary", "binary", NULL, &procstat_bin, &cmdopt_none, PS_CMP_SUBSTR }, { "cpuset", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL }, { "cs", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL }, { "credential", "credentials", NULL, &procstat_cred, &cmdopt_none, PS_CMP_PLURAL | PS_CMP_SUBSTR }, { "environment", "environment", NULL, &procstat_env, &cmdopt_none, PS_CMP_SUBSTR }, { "fd", "files", "[-C]", &procstat_files, &cmdopt_files, PS_CMP_PLURAL }, { "file", "files", "[-C]", &procstat_files, &cmdopt_files, PS_CMP_PLURAL }, { "kstack", "kstack", "[-v]", &procstat_kstack, &cmdopt_verbose, PS_CMP_NORMAL }, + { "pargs", "args", NULL, &procstat_pargs, &cmdopt_none, + PS_CMP_NORMAL }, + { "penv", "env", NULL, &procstat_penv, &cmdopt_none, + PS_CMP_NORMAL }, { "ptlwpinfo", "ptlwpinfo", NULL, &procstat_ptlwpinfo, &cmdopt_none, PS_CMP_NORMAL }, + { "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none, + PS_CMP_NORMAL }, { "rlimit", "rlimit", NULL, &procstat_rlimit, &cmdopt_none, PS_CMP_NORMAL }, { "rusage", "rusage", "[-Ht]", &procstat_rusage, &cmdopt_rusage, PS_CMP_NORMAL }, { "sigfastblock", "sigfastblock", NULL, &procstat_sigfastblock, &cmdopt_none, PS_CMP_NORMAL }, { "signal", "signals", "[-n]", &procstat_sigs, &cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR }, { "thread", "threads", NULL, &procstat_threads, &cmdopt_none, PS_CMP_PLURAL }, { "tsignal", "thread_signals", "[-n]", &procstat_threads_sigs, &cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR }, { "vm", "vm", NULL, &procstat_vm, &cmdopt_none, PS_CMP_NORMAL } }; static void -usage(void) +usage(const struct procstat_cmd *cmd) { size_t i, l; int multi; - xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]" - " [-w interval] command\n" - " [pid ... | core ...]\n" - " procstat [--libxo] -a [-h] [-M core] [-N system] " - " [-w interval] command\n" - " procstat [--libxo] [-h] [-M core] [-N system]" - " [-w interval]\n" - " [-S | -b | -c | -e | -f [-C] | -i [-n] | " - "-j [-n] | -k [-k] |\n" - " -l | -r [-H] | -s | -t | -v | -x] " - "[pid ... | core ...]\n" - " procstat [--libxo] -a [-h] [-M core] [-N system]" - " [-w interval]\n" - " [-S | -b | -c | -e | -f [-C] | -i [-n] | " - "-j [-n] | -k [-k] |\n" - " -l | -r [-H] | -s | -t | -v | -x]\n" - " procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n" - "Available commands:\n"); - for (i = 0, l = nitems(cmd_table); i < l; i++) { - multi = i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd; - xo_error(" %s%s%s", multi ? "[" : "", - cmd_table[i].command, (cmd_table[i].cmp & PS_CMP_PLURAL) ? - "(s)" : ""); - for (; i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd; - i++) - xo_error(" | %s%s", cmd_table[i + 1].command, - (cmd_table[i].cmp & PS_CMP_PLURAL) ? "(s)" : ""); - if (multi) - xo_error("]"); - if (cmd_table[i].usage != NULL) - xo_error(" %s", cmd_table[i].usage); - xo_error("\n"); + if (cmd == NULL || (cmd->cmp & PS_MODE_COMPAT) == 0) { + xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]" + " [-w interval] command\n" + " [pid ... | core ...]\n" + " procstat [--libxo] -a [-h] [-M core] [-N system] " + " [-w interval] command\n" + " procstat [--libxo] [-h] [-M core] [-N system]" + " [-w interval]\n" + " [-S | -b | -c | -e | -f [-C] | -i [-n] | " + "-j [-n] | -k [-k] |\n" + " -l | -r [-H] | -s | -t | -v | -x] " + "[pid ... | core ...]\n" + " procstat [--libxo] -a [-h] [-M core] [-N system]" + " [-w interval]\n" + " [-S | -b | -c | -e | -f [-C] | -i [-n] | " + "-j [-n] | -k [-k] |\n" + " -l | -r [-H] | -s | -t | -v | -x]\n" + " procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n" + "Available commands:\n"); + for (i = 0, l = nitems(cmd_table); i < l; i++) { + multi = i + 1 < l && cmd_table[i].cmd == + cmd_table[i + 1].cmd; + xo_error(" %s%s%s", multi ? "[" : "", + cmd_table[i].command, (cmd_table[i].cmp & + PS_CMP_PLURAL) ? "(s)" : ""); + for (; i + 1 < l && cmd_table[i].cmd == + cmd_table[i + 1].cmd; i++) + xo_error(" | %s%s", cmd_table[i + 1].command, + (cmd_table[i].cmp & PS_CMP_PLURAL) ? + "(s)" : ""); + if (multi) + xo_error("]"); + if (cmd_table[i].usage != NULL) + xo_error(" %s", cmd_table[i].usage); + xo_error("\n"); + } + } else { + xo_error("usage: %s [--libxo] pid ...\n", progname); } xo_finish(); exit(EX_USAGE); } static void procstat(const struct procstat_cmd *cmd, 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); cmd->cmd(prstat, 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); } const char * kinfo_proc_thread_name(const struct kinfo_proc *kipp) { static char name[MAXCOMLEN+1]; strlcpy(name, kipp->ki_tdname, sizeof(name)); strlcat(name, kipp->ki_moretdname, sizeof(name)); if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) { name[0] = '-'; name[1] = '\0'; } return (name); } static const struct procstat_cmd * +getcmdbyprogname(const char *pprogname) +{ + const char *ca; + size_t i, len; + + if (pprogname == NULL) + return (NULL); + len = strlen(pprogname); + + for (i = 0; i < nitems(pacmd_table); i++) { + ca = pacmd_table[i].command; + if (ca != NULL && strcmp(ca, pprogname) == 0) + return (&pacmd_table[i]); + } + + return (NULL); +} + +static const struct procstat_cmd * getcmd(const char *str) { const struct procstat_cmd *cmd; size_t i, l; int cmp, s; if (str == NULL) return (NULL); cmd = NULL; if ((l = strlen(str)) == 0) return (getcmd("basic")); s = l > 1 && strcasecmp(str + l - 1, "s") == 0; for (i = 0; i < nitems(cmd_table); i++) { /* * After the first match substring matches are disabled, * allowing subsequent full matches to take precedence. */ if (cmd == NULL && (cmd_table[i].cmp & PS_CMP_SUBSTR)) cmp = strncasecmp(str, cmd_table[i].command, l - ((cmd_table[i].cmp & PS_CMP_PLURAL) && s ? 1 : 0)); else if ((cmd_table[i].cmp & PS_CMP_PLURAL) && s && l == strlen(cmd_table[i].command) + 1) cmp = strncasecmp(str, cmd_table[i].command, l - 1); else cmp = strcasecmp(str, cmd_table[i].command); if (cmp == 0) cmd = &cmd_table[i]; } return (cmd); } int main(int argc, char *argv[]) { - int ch, interval; - int i; struct kinfo_proc *p; const struct procstat_cmd *cmd; struct procstat *prstat, *cprstat; + char *dummy, *nlistf, *memf; + const char *xocontainer; long l; pid_t pid; - char *dummy; - char *nlistf, *memf; - int aflag; - int cnt; + int aflag, ch, cnt, i, interval; interval = 0; cmd = NULL; memf = nlistf = NULL; aflag = 0; argc = xo_parse_args(argc, argv); + progname = getprogname(); + cmd = getcmdbyprogname(progname); + while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) { switch (ch) { case 'a': aflag++; break; case 'b': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("binary"); break; case 'C': procstat_opts |= PS_OPT_CAPABILITIES; break; case 'c': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("arguments"); break; case 'e': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("environment"); break; case 'f': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("files"); break; case 'H': procstat_opts |= PS_OPT_PERTHREAD; break; case 'h': procstat_opts |= PS_OPT_NOHEADER; break; case 'i': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("signals"); break; case 'j': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("tsignals"); break; case 'k': if (cmd != NULL && cmd->cmd == procstat_kstack) { if ((procstat_opts & PS_OPT_VERBOSE) != 0) - usage(); + usage(cmd); procstat_opts |= PS_OPT_VERBOSE; } else { if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("kstack"); } break; case 'L': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("ptlwpinfo"); break; case 'l': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("rlimit"); break; case 'M': memf = optarg; break; case 'N': nlistf = optarg; break; case 'n': procstat_opts |= PS_OPT_SIGNUM; break; case 'r': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("rusage"); break; case 'S': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("cpuset"); break; case 's': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("credentials"); break; case 't': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("threads"); break; case 'v': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("vm"); break; case 'w': l = strtol(optarg, &dummy, 10); if (*dummy != '\0') - usage(); + usage(cmd); if (l < 1 || l > INT_MAX) - usage(); + usage(cmd); interval = l; break; case 'x': if (cmd != NULL) - usage(); + usage(cmd); cmd = getcmd("auxv"); break; case '?': default: - usage(); + usage(cmd); } } argc -= optind; argv += optind; - if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) { + if (cmd == NULL && argv[0] != NULL) + cmd = getcmd(argv[0]); + if (cmd != NULL) { if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0) - usage(); + usage(cmd); if (cmd->opt != NULL) { optreset = 1; optind = 1; cmd->opt(argc, argv); - argc -= optind; - argv += optind; + if ((cmd->cmp & PS_MODE_COMPAT) == 0) { + argc -= optind; + argv += optind; + } } else { argc -= 1; argv += 1; } } else { - if (cmd == NULL) - cmd = getcmd("basic"); - if (cmd->cmd != procstat_files && - (procstat_opts & PS_OPT_CAPABILITIES) != 0) - usage(); + cmd = getcmd("basic"); } + if (cmd->cmd != procstat_files && + (procstat_opts & PS_OPT_CAPABILITIES) != 0 && + (cmd->cmp & PS_MODE_COMPAT) == 0) + usage(cmd); /* Must specify either the -a flag or a list of pids. */ if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0)) - usage(); + usage(cmd); if (memf != NULL) prstat = procstat_open_kvm(nlistf, memf); else prstat = procstat_open_sysctl(); if (prstat == NULL) xo_errx(1, "procstat_open()"); do { + xocontainer = cmd->xocontainer != NULL ? cmd->xocontainer : + cmd->command; xo_set_version(PROCSTAT_XO_VERSION); - xo_open_container("procstat"); - xo_open_container(cmd->xocontainer); + xo_open_container(progname); + xo_open_container(xocontainer); if (aflag) { p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt); if (p == NULL) xo_errx(1, "procstat_getprocs()"); kinfo_proc_sort(p, cnt); for (i = 0; i < cnt; i++) { procstat(cmd, prstat, &p[i]); /* Suppress header after first process. */ procstat_opts |= PS_OPT_NOHEADER; 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(); + usage(cmd); pid = l; p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt); if (p == NULL) xo_errx(1, "procstat_getprocs()"); if (cnt != 0) procstat(cmd, prstat, p); procstat_freeprocs(prstat, p); } else { - cprstat = procstat_open_core(argv[i]); - if (cprstat == NULL) { - warnx("procstat_open()"); - continue; + if ((cmd->cmp & PS_MODE_COMPAT) == 0) { + 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) { + xo_errx(1, + "procstat_getprocs()"); + } + if (cnt != 0) + procstat(cmd, cprstat, p); + procstat_freeprocs(cprstat, p); + procstat_close(cprstat); + } else { + usage(cmd); } - p = procstat_getprocs(cprstat, KERN_PROC_PID, - -1, &cnt); - if (p == NULL) - xo_errx(1, "procstat_getprocs()"); - if (cnt != 0) - procstat(cmd, cprstat, p); - procstat_freeprocs(cprstat, p); - procstat_close(cprstat); } - /* Suppress header after first process. */ - procstat_opts |= PS_OPT_NOHEADER; + if ((cmd->cmp & PS_MODE_COMPAT) == 0) { + /* Suppress header after first process. */ + procstat_opts |= PS_OPT_NOHEADER; + } } - xo_close_container(cmd->xocontainer); - xo_close_container("procstat"); + xo_close_container(xocontainer); + xo_close_container(progname); xo_finish(); if (interval) sleep(interval); } while (interval); procstat_close(prstat); exit(0); } void cmdopt_none(int argc, char * const argv[]) { int ch; while ((ch = getopt(argc, argv, "")) != -1) { switch (ch) { case '?': default: - usage(); + usage(NULL); } } } void cmdopt_verbose(int argc, char * const argv[]) { int ch; while ((ch = getopt(argc, argv, "v")) != -1) { switch (ch) { case 'v': procstat_opts |= PS_OPT_VERBOSE; break; case '?': default: - usage(); + usage(NULL); } } } void cmdopt_signals(int argc, char * const argv[]) { int ch; while ((ch = getopt(argc, argv, "n")) != -1) { switch (ch) { case 'n': procstat_opts |= PS_OPT_SIGNUM; break; case '?': default: - usage(); + usage(NULL); } } } void cmdopt_rusage(int argc, char * const argv[]) { int ch; while ((ch = getopt(argc, argv, "Ht")) != -1) { switch (ch) { case 'H': /* FALLTHROUGH */ case 't': procstat_opts |= PS_OPT_PERTHREAD; break; case '?': default: - usage(); + usage(NULL); } } } void cmdopt_files(int argc, char * const argv[]) { int ch; while ((ch = getopt(argc, argv, "C")) != -1) { switch (ch) { case 'C': procstat_opts |= PS_OPT_CAPABILITIES; break; case '?': default: - usage(); + usage(NULL); } } } void cmdopt_cpuset(int argc, char * const argv[]) { procstat_opts |= PS_OPT_PERTHREAD; cmdopt_none(argc, argv); } Index: head/usr.bin/procstat/procstat.h =================================================================== --- head/usr.bin/procstat/procstat.h (revision 365879) +++ head/usr.bin/procstat/procstat.h (revision 365880) @@ -1,77 +1,81 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2007 Robert N. M. Watson * Copyright (c) 2015 Allan Jude * Copyright (c) 2017 Dell EMC * 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" enum { PS_OPT_CAPABILITIES = 0x01, PS_OPT_NOHEADER = 0x02, PS_OPT_PERTHREAD = 0x04, PS_OPT_SIGNUM = 0x08, - PS_OPT_VERBOSE = 0x10 + PS_OPT_VERBOSE = 0x10, + PS_MODE_COMPAT = 0x20, }; #define PS_SUBCOMMAND_OPTS \ (PS_OPT_CAPABILITIES | PS_OPT_SIGNUM | \ PS_OPT_PERTHREAD | PS_OPT_VERBOSE) extern int procstat_opts; struct kinfo_proc; void kinfo_proc_sort(struct kinfo_proc *kipp, int count); const char * kinfo_proc_thread_name(const struct kinfo_proc *kipp); void procstat_args(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_auxv(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_basic(struct procstat *prstat, 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); +void procstat_pargs(struct procstat *prstat, struct kinfo_proc *kipp); +void procstat_penv(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_ptlwpinfo(struct procstat *prstat, struct kinfo_proc *kipp); +void procstat_pwdx(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_rlimit(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_rusage(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_sigfastblock(struct procstat *procstat, 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_kstack.c =================================================================== --- head/usr.bin/procstat/procstat_kstack.c (revision 365879) +++ head/usr.bin/procstat/procstat_kstack.c (revision 365880) @@ -1,249 +1,249 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * 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; + 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_new = '\0'; 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); } 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) { struct kinfo_kstack *kkstp, *kkstp_free; struct kinfo_proc *kip, *kip_free; char trace[KKST_MAXLEN], encoded_trace[KKST_MAXLEN]; unsigned int i, j; unsigned int kip_count, kstk_count; if ((procstat_opts & PS_OPT_NOHEADER) == 0) xo_emit("{T:/%5s %6s %-19s %-19s %-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; xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid); xo_emit("{:thread_id/%6d/%d} ", kkstp->kkst_tid); xo_emit("{:command/%-19s/%s} ", kipp->ki_comm); xo_emit("{:thread_name/%-19s/%s} ", kinfo_proc_thread_name(kipp)); switch (kkstp->kkst_state) { case KKST_STATE_RUNNING: xo_emit("{:state/%-29s/%s}\n", ""); continue; case KKST_STATE_SWAPPED: xo_emit("{:state/%-29s/%s}\n", ""); continue; case KKST_STATE_STACKOK: break; default: 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, (procstat_opts & PS_OPT_VERBOSE) != 0 ? 2 : 1); xo_open_list("trace"); kstack_cleanup_encoded(kkstp->kkst_trace, encoded_trace, (procstat_opts & PS_OPT_VERBOSE) != 0 ? 2 : 1); 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_penv.c =================================================================== --- head/usr.bin/procstat/procstat_penv.c (nonexistent) +++ head/usr.bin/procstat/procstat_penv.c (revision 365880) @@ -0,0 +1,86 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Juraj Lutter + * 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 +#include + +#include "procstat.h" + +void +procstat_pargs(struct procstat *procstat, struct kinfo_proc *kipp) +{ + int i; + char **args; + + args = procstat_getargv(procstat, kipp, 0); + + xo_emit("{k:process_id/%d}: {:command/%s/%s}\n", kipp->ki_pid, + kipp->ki_comm); + + if (args == NULL) { + xo_emit("{d:args/-}\n"); + } else { + for (i = 0; args[i] != NULL; i++) { + xo_emit("{Ld:argv[}{Ld:/%d}{Ldwc:]}{l:argv/%s}\n", + i, args[i]); + } + } +} + +void +procstat_penv(struct procstat *procstat, struct kinfo_proc *kipp) +{ + int i; + char **envs; + + envs = procstat_getenvv(procstat, kipp, 0); + + xo_emit("{k:process_id/%d}: {:command/%s/%s}\n", kipp->ki_pid, + kipp->ki_comm); + + if (envs == NULL) { + xo_emit("{d:env/-}\n"); + } else { + for (i = 0; envs[i] != NULL; i++) { + xo_emit("{Ld:envp[}{Ld:/%d}{Ldwc:]}{l:envp/%s}\n", + i, envs[i]); + } + } +} Property changes on: head/usr.bin/procstat/procstat_penv.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/procstat/procstat_pwdx.c =================================================================== --- head/usr.bin/procstat/procstat_pwdx.c (nonexistent) +++ head/usr.bin/procstat/procstat_pwdx.c (revision 365880) @@ -0,0 +1,70 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Juraj Lutter + * 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 +#include +#include +#include +#include +#include + +#include "procstat.h" + +void +procstat_pwdx(struct procstat *procstat, struct kinfo_proc *kipp) +{ + struct filestat_list *head; + struct filestat *fst; + + head = procstat_getfiles(procstat, kipp, 0); + if (head == NULL) + return; + STAILQ_FOREACH(fst, head, next) { + if ((fst->fs_uflags & PS_FST_UFLAG_CDIR) && + (fst->fs_path != NULL)) { + xo_emit("{k:process_id/%d}{P:: }", kipp->ki_pid); + xo_emit("{:cwd/%s}", fst->fs_path); + xo_emit("\n"); + } + } + procstat_freefiles(procstat, head); +} Property changes on: head/usr.bin/procstat/procstat_pwdx.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property