Home | History | Annotate | only in /src/bin/ps
History log of /src/bin/ps
RevisionDateAuthorComments
 1.31 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.30 06-Apr-2021  simonb We only need -Wno-format-y2k for print.c .
 1.29 14-Aug-2011  christos branches: 1.29.44;
Document non-literal formats
 1.28 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.27 26-May-2000  simonb branches: 1.27.6;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.26 12-Sep-1999  chs the PMAP_NEW option is gone as the new interface is no longer optional.
 1.25 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.24 24-Mar-1999  mrg completely remove Mach VM support. all that is left is the all the
header files as UVM still uses (most of) these.
 1.23 25-Aug-1998  tv Add CWARNFLAGS+=-Wno-format-y2k.
 1.22 25-Aug-1998  ross Kill the CFLAGS+=-Wno-format-y2k which will go, for now, into bsd.sys.mk
It wasn't working anyway, although that could have been fixed by moving it
down below the .include <bsd.prog.mk> line.

It's not completely clear that this usage of %y is in fact y2k-safe, it
seems safer for now to have a single XXX option in bsd.sys.mk, and we need
to rev this anyway for -Wno-uninitialized
 1.21 21-Aug-1998  tv This needs strftime()'s %y; use -Wno-format-y2k.
 1.20 18-Feb-1998  perry add -DPMAP_NEW if PMAP_NEW is on in mk.conf
 1.19 09-Feb-1998  mrg add -DUVM to CPPFLAGS if defined(UVM)
 1.18 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.17 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.16 26-Oct-1995  jtc LIBMATH -> LIBM (PR# 1688)
 1.15 21-May-1995  mycroft Split out some helper functions again.
 1.14 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 03-Aug-1994  deraadt do not need -I/sys
 1.11 03-Jul-1994  deraadt devname is in libc now
 1.10 17-May-1994  pk Restore "${DESTDIR}" in CFLAGS.
 1.9 12-May-1994  mycroft Need devname.c.
 1.8 09-May-1994  cgd clean up import
 1.7 28-Jan-1994  cgd needs -lkvm, not -lutil
 1.6 14-Aug-1993  mycroft Fix library dependencies.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 17-Jun-1993  mycroft Don't need -I/sys. (Why was it here?)
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.27.6.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.29.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.44.1 21-Apr-2020  martin Sync with HEAD
 1.7 03-Jul-1994  mycroft Clean up deleted files.
 1.6 09-May-1994  cgd clean up import
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 21-Apr-1993  proven Changed hash_open to dbopen.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.39 09-Dec-2017  kamil branches: 1.39.4;
ps: Rename local routine name from uname to usrname

This removes symbol name routine clash with libc's uname(3).
This allows to build ps(1) against LLVM Sanitizers.

Sponsored by <The NetBSD Foundation>
 1.38 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.37 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.36 28-Nov-2016  rin for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.35 20-Apr-2014  dholland branches: 1.35.6;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.34 15-Jan-2014  mlelstv branches: 1.34.2;
Add an LTIME column that prints lwp cputime.
 1.33 31-May-2010  rmind branches: 1.33.6; 1.33.12;
Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.32 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.31 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.30 29-Oct-2006  christos branches: 1.30.8;
Add support for printing the emulation of the process.
 1.29 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.28 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.27 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.26 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.25 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.24 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.23 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.22 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.21 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.20 14-Jul-2001  matt branches: 1.20.2;
Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.19 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.18 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.17 26-May-2000  simonb branches: 1.17.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.16 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.15 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.14 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.13 26-Mar-1999  bgrayson branches: 1.13.2; 1.13.4;
Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.12 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.11 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.10 21-May-1995  mycroft Split out some helper functions again.
 1.9 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 03-Jul-1994  deraadt devname is in libc now
 1.6 16-Jun-1994  pk Declare devname().
 1.5 09-May-1994  cgd clean up import
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.13.2.1 08-Nov-1999  cgd pull up rev 1.14 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.17.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.20.2.3 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.20.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.20.2.1 14-Jul-2001  nathanw file extern.h was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.30.8.2 23-Mar-2008  matt sync with HEAD
 1.30.8.1 06-Nov-2007  matt sync with HEAD
 1.33.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.33.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.34.2.1 10-Aug-2014  tls Rebase.
 1.35.6.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.39.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.39.4.1 21-Apr-2020  martin Sync with HEAD
 1.21 12-Dec-2007  lukem branches: 1.21.70;
use __RCSID()
 1.20 27-Mar-2004  simonb branches: 1.20.22;
Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.19 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.18 14-Feb-2002  enami branches: 1.18.2;
Just use return value of strvis instead of calling strlen.
 1.17 14-Feb-2002  enami Don't define variable as static unnecessarily.
 1.16 23-Mar-2001  enami Always initialize an auto variable.
 1.15 20-Mar-2001  itojun do not update maxlen if realloc fails.
 1.14 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.13 03-Feb-1998  perry branches: 1.13.6;
add <sys/time.h> to fix compiler error
 1.12 09-Jan-1998  perry RCS Id Police.
 1.11 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.10 25-May-1995  mycroft Simplify this just a little.
 1.9 21-May-1995  mycroft Split out some helper functions again.
 1.8 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.7 18-May-1995  mycroft Do the right thing for an empty environment.
 1.6 18-May-1995  mycroft Avoid an extra (gratuitous) pass through strvis(3).
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 08-Jan-1995  mycroft Better fix for the previous.
 1.3 08-Jan-1995  mycroft Fix oversight in previous change, when argument list is sort of empty.
 1.2 18-May-1994  cgd kill extra space, save my sanity
 1.1 09-May-1994  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-Jan-1997  tls Import from 4.4BSD-Lite2
 1.1.1.1 09-May-1994  cgd new ps, to match libkvm
 1.13.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.18.2.2 14-Feb-2002  enami Just use return value of strvis instead of calling strlen.
 1.18.2.1 14-Feb-2002  enami file fmt.c was added on branch nathanw_sa on 2002-02-14 06:57:20 +0000
 1.20.22.1 09-Jan-2008  matt sync with HEAD
 1.21.70.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.70.1 21-Apr-2020  martin Sync with HEAD
 1.60 15-Sep-2021  christos Don't assign v to newvar, so we can still access the original length.
 1.59 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.58 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.57 06-Aug-2019  kamil Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.

These values were disabled since day0 until today.

libkvm(3) users will still receive inappropriate values as RSS statistics
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.56 11-Apr-2018  christos branches: 1.56.2; 1.56.4;
use EXIT_FAILURE instead of 1
 1.55 09-Dec-2017  kamil branches: 1.55.2;
ps: Rename local routine name from uname to usrname

This removes symbol name routine clash with libc's uname(3).
This allows to build ps(1) against LLVM Sanitizers.

Sponsored by <The NetBSD Foundation>
 1.54 15-Jan-2014  mlelstv Add an LTIME column that prints lwp cputime.
 1.53 21-Oct-2009  rmind branches: 1.53.6; 1.53.12;
Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.52 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.51 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.50 29-Oct-2006  christos branches: 1.50.8;
Add support for printing the emulation of the process.
 1.49 16-Oct-2006  christos c99 initializers
 1.48 02-Oct-2006  apb Set the width in the correct data structure. (Fix error in previous.)
 1.47 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.46 18-Mar-2006  christos Coverity CID 1559: Fix memory leak.
 1.45 05-Nov-2005  chs add a keyword for the uarea address. very useful with crashdumps.
 1.44 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.43 16-Nov-2004  christos branches: 1.43.2;
print TTY instead of TT, one question mark instead of two and always the
full tty name.
 1.42 23-Aug-2004  nathanw Add a "laddr" keyword, analagous to "paddr". Useful for finding
struct pcb's.
 1.41 28-Jul-2004  simonb For the "lid" keyword, change ULONG to INT32 since it's a 32bit type.
From Nicolas Joly in PR bin/26452.
 1.40 27-Mar-2004  simonb branches: 1.40.2;
Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.39 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.38 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.37 27-Mar-2004  simonb Change the header for the "tty" keyword "TT" instead of "TTY".

Fixed POSIX.2 conformance problem mentioned in standards/11225 from
Ben Harris.
 1.36 16-Dec-2003  mjl Correct typo in page header for tpgid.
 1.35 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.34 08-Mar-2003  dsl Support -ktt and -ktty, quote "tdev" in comment so table can be sorted.
 1.33 08-Mar-2003  christos XXX: cast tdev to INT32 instead of UINT32 so that that sorting works as
before [NODEV entries come first]
 1.32 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.31 03-Mar-2003  dsl Seems better to display p_sv{u,g}id when asked for it...
 1.30 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.29 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.28 17-Oct-2002  itojun correct use of format string (signedness, %ldd). PR 18679
 1.27 24-Apr-2002  nathanw branches: 1.27.2;
Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.26 08-Jan-2001  itojun branches: 1.26.2;
address PR 11653. checked on alpha.
XXX supports 32bit and 64bit pointers only
 1.25 07-Jun-2000  simonb branches: 1.25.2;
Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.24 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.23 26-May-2000  simonb branches: 1.23.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.22 11-May-2000  mjl Fix problem in option processing, would segfault when parsing "-O,".
Closes PR/10096 by Yuji Yamano using the patch provided.
 1.21 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.20 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.19 03-May-1999  mrg branches: 1.19.4;
limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.18 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.17 17-Feb-1998  thorpej Add a "sid" keyword to print the session ID.
 1.16 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.15 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.14 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.13 30-May-1996  cgd in findvar(), return after warning about unknown keyword so we don't
deref. a null pointer.
 1.12 29-Sep-1995  cgd branches: 1.12.6;
correct the sizes specified for various variables.
 1.11 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.10 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.9 08-May-1995  cgd add a 'holdcnt' keyword and document it.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 09-May-1994  cgd clean up import
 1.6 05-May-1994  cgd struct element foo
 1.5 05-May-1994  cgd update for changed flags
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.6.1 30-May-1996  cgd pull up from trunk:
in findvar(), return after warning about unknown keyword so we don't
deref. a null pointer.
 1.19.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.23.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.25.2.1 18-Oct-2002  itojun bin/ps 1.28 via patch

Correct printf format string.
 1.26.2.4 12-Nov-2002  nathanw Catch up to -current.
 1.26.2.3 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.26.2.2 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.26.2.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.27.2.1 21-Oct-2002  lukem Pull up revision 1.28 (requested by itojun in ticket #922):
correct use of format string (signedness, %ldd). PR 18679
 1.40.2.1 28-Jul-2004  tron Pull up revision 1.41 (requested by simonb in ticket #723):
For the "lid" keyword, change ULONG to INT32 since it's a 32bit type.
From Nicolas Joly in PR bin/26452.
 1.43.2.1 06-Nov-2005  tron Pull up following revision(s) (requested by chs in ticket #936):
bin/ps/keyword.c: revision 1.45
add a keyword for the uarea address. very useful with crashdumps.
 1.50.8.2 23-Mar-2008  matt sync with HEAD
 1.50.8.1 06-Nov-2007  matt sync with HEAD
 1.53.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.53.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.55.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.56.4.1 07-Aug-2019  martin Pull up following revision(s) (requested by kamil in ticket #24):

bin/ps/ps.1: revision 1.110
bin/ps/keyword.c: revision 1.57

Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.
These values were disabled since day0 until today.
libkvm(3) users will still receive inappropriate values as RSS statistics=
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.56.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.56.2.2 21-Apr-2020  martin Sync with HEAD
 1.56.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.28 26-Aug-2020  simonb Don't rely on the USPACE kernel define as a fallback if the vm.uspace
sysctl fails. We've got bigger problems if the sysctl fail anyway.
 1.27 28-Nov-2016  rin branches: 1.27.14;
for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.26 28-Apr-2008  martin branches: 1.26.12; 1.26.46;
Remove clause 3 and 4 from TNF licenses
 1.25 16-Oct-2006  christos branches: 1.25.16;
c99 initializers
 1.24 22-Aug-2004  dsl Use HW_PHYSMEM64 to avoid problem with > 2GB memory (and >4GB as well)
Fixes PR/23717 and PR/24092
 1.23 27-Mar-2004  simonb #define<TAB>
 1.22 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.21 25-Feb-2004  jrf donlist_sysctl() was overwriting maxslp with vm.uspace. Changed
it to use uspace. See PR bin/23713, approved by christos@netbsd.org.
Thanks to joff@stchome.com for the submission.
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.18 14-Jul-2001  matt branches: 1.18.2;
Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.17 08-Jun-2000  simonb Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.16 26-May-2000  simonb branches: 1.16.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.15 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.14 27-Mar-1998  is branches: 1.14.6;
Use the physmem variable to get at the physical memory size (instead of the
avail_end - avail_start difference which is only correct on machines with
contiguous memory).
 1.13 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.12 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.9 25-Nov-1994  mycroft Format police.
 1.8 25-Nov-1994  deraadt print %MEM correctly
 1.7 09-May-1994  cgd clean up import
 1.6 05-May-1994  cgd update for changed flags
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.16.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.18.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.18.2.1 14-Jul-2001  nathanw file nlist.c was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.25.16.1 18-May-2008  yamt sync with head.
 1.26.46.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.26.12.1 27-Dec-2011  matt Use UPAGES in preferences to USPACE.
 1.27.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.14.1 21-Apr-2020  martin Sync with HEAD
 1.138 26-Jan-2022  andvar remove double t from targeted, add missing r to arbitrary
And fix few more typos along the way in comments and man pages.
 1.137 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.136 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.135 17-Apr-2021  maya Remove SCCS workaround. No binary change.
 1.134 06-Apr-2021  christos - dedup code
- add a safe_strftime() to handle error cases
 1.133 06-Apr-2021  simonb Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.132 19-Jun-2019  kamil branches: 1.132.2;
Add a fallback definition of LSDEAD in ps(1)

The symbol is no longer available in headers.

Requested by <mrg>
 1.131 18-Jun-2019  kamil Make LSDEAD usage conditional

LSDEAD is not used since NetBSD-5.0 and will be gone.

The same conditional usage is already in ps.c in the same program.
 1.130 19-Sep-2018  maxv Don't display l_wchan, either there is something in l_wmesg and we display
it, or there's nothing and we print "-".
 1.129 11-Apr-2018  christos branches: 1.129.2;
use EXIT_FAILURE instead of 1
 1.128 09-Dec-2017  kamil branches: 1.128.2;
ps: Rename local routine name from uname to usrname

This removes symbol name routine clash with libc's uname(3).
This allows to build ps(1) against LLVM Sanitizers.

Sponsored by <The NetBSD Foundation>
 1.127 12-Dec-2016  christos branches: 1.127.6;
Handle functions that use the offset from either kinfo_proc2/kinfo_lwp properly.
 1.126 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.125 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.124 28-Nov-2016  rin for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.123 15-Nov-2014  joerg branches: 1.123.2;
Use l_wmesg if the string is not empty. Don't bother checking l_name for
nullness.
 1.122 20-Apr-2014  dholland branches: 1.122.2;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.121 15-Jan-2014  mlelstv branches: 1.121.2;
Add an LTIME column that prints lwp cputime.
 1.120 20-Mar-2012  matt branches: 1.120.2;
Use C89 function definitions
 1.119 13-Feb-2012  wiz Remove unused variable.
From cppcheck via Henning Petersen in PR 46002.
 1.118 13-Jun-2011  dholland branches: 1.118.2;
When converting from pages to kilobytes, cast the return value of
getpagesize() to size_t. For some reason getpagesize() is defined to
return int, and several of the page counts we get come back from the
kernel as int32_t; in LP64 without the cast the byte count will be
computed in a 32-bit value and for large processes will overflow and
become negative... and then remain negative when divided by 1024 to
convert to kilobytes.

Fixes a problem I hit the other day where I saw negative RSS, which
turns out also to be PR 40642.

Note: other logic in here will break down when we first get >2TB
processes... and int32 page counts will break on >8TB processes. But
hopefully we won't see any of that for a few years yet.
 1.117 22-Jan-2011  christos branches: 1.117.2;
fix previous.
 1.116 22-Jan-2011  christos Make printing of lwp flags similar to the process one, identifying (O)nproc
(K)ernel threads and scheduler (a)ctivations.
 1.115 28-Jul-2010  njoly branches: 1.115.2;
Do not check pcpu value against 100.0 but rather 99.95, to properly
deal with value rounding. From apb@.
 1.114 27-Jul-2010  njoly Make %cpu drop the decimal part when reaching 100%, to stay in the 5
expected columns.
 1.113 31-May-2010  rmind Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.112 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.111 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.110 14-Feb-2009  lukem fix -Wsign-compare issues
 1.109 03-Feb-2009  drochner branches: 1.109.2;
make sure the table stays aligned even if no groups are to be displayed
with the "groupnames" option
 1.108 03-Feb-2009  drochner make sure the table stays aligned even if no groups are to be displayed
with the "groups" option
 1.107 28-Dec-2008  christos fix dev_t 64 bit issues.
 1.106 19-Oct-2008  christos branches: 1.106.2;
Use 'O' for LSONPROC like solaris instead of bundling LSIDL, LSRUN, LSONPROC
to 'R'
 1.105 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.104 10-Feb-2008  christos branches: 1.104.4;
Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.103 31-Dec-2007  ad Remove systrace. Ok core@.
 1.102 06-Nov-2007  ad Kill another use of PZERO.
 1.101 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.100 28-Jul-2007  dsl branches: 1.100.4; 1.100.6;
Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.99 17-Feb-2007  pavel Revert the changes done after newlock2 merge, except for the removal of
the now unused LSDEAD state. The current kernel headers are now
source-compatible with pre-newlock2 kernel headers again.

Approved by Andrew Doran.
 1.98 10-Feb-2007  ad Further changes to use compat process flags mapped by sysctl. To be
revisited soon.
 1.97 09-Feb-2007  ad Sync with kernel changes introduced by merging the newlock2 branch.
 1.96 29-Oct-2006  christos branches: 1.96.2; 1.96.4;
Add support for printing the emulation of the process.
 1.95 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.94 27-Jun-2005  christos Add some more const so that this compiles again.
 1.93 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.92 07-Jun-2005  he Appease -Wuninitialized by initializing val and uval to 0.
Marked with XXXGCC for hpcarm (found while building for it).

Reviewed (in a slightly different form) by lukem.
 1.91 16-Nov-2004  christos print TTY instead of TT, one question mark instead of two and always the
full tty name.
 1.90 23-Apr-2004  simonb s/the the/the/ (only in sources that aren't regularly imported from
elsewhere).
 1.89 27-Mar-2004  simonb Gah, fix uninitialised variable with the "etime" change. _Never_ be
happy with something and then edit a single line before committing
without retesting...
 1.88 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.87 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.86 27-Mar-2004  simonb #define<TAB>
 1.85 27-Mar-2004  simonb Use some constants from <tzfile.h>.
 1.84 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.83 27-Mar-2004  simonb Fix grammar in a comment.
 1.82 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.81 24-Feb-2004  wiz Spell interrupt with two rs. From Peter Postma.
 1.80 11-Jan-2004  jdolecek g/c SDEAD
 1.79 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.78 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.77 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.76 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.75 06-Jan-2003  wiz interrupt with two rs.
 1.74 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.73 17-Jun-2002  christos Document some missing flags and add systrace(1) 'x' flag.
 1.72 24-Apr-2002  nathanw Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.71 21-Feb-2002  martin branches: 1.71.2;
Use int32_t for temporary variables (instead of long), that is the same
type as we get the data passed from the kernel. This avoids (missing)
sign extension bugs on LP64 systems and partly takes care of PR 15677.

We now print this values as negative seconds - still wrong, but that
probably is due to the simple way this values are acumulated in the
scheduler, causing negative times when ntpd steps time backwards.
 1.70 21-Jan-2002  jdolecek started(): don't assume that the field width is correct and recompute
it as needed

This fixes bogus output for cases like `ps -ao start=xx`, as reported by
Martin Macok of Undeground.cz on current-users@netbsd.org.
 1.69 24-Aug-2001  lukem fix comment: "[]" are [square] brackets, "<>" are angle brackets
 1.68 24-Aug-2001  enami Print commands within angled brackets only if they are system processes.
Print other commands, like cc, cpp and as, within parentheres.
 1.67 07-Aug-2001  christos Print system processes/kernel threads as [kprocname] as top(1) does.
 1.66 14-Jul-2001  matt Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.65 15-Jan-2001  christos clarify things a bit more (from hubertf)
 1.64 15-Jan-2001  christos Really explain what parenthesized names mean and why we parenthesize names!
 1.63 15-Jan-2001  hubertf Document the meaning of commands shown in parantheses.

Patches submitted by Jeremy C. Reed <reed@reedmedia.net> in PR 11930.
 1.62 09-Jan-2001  itojun KPTR/KPTR24 are using u_int64_t. commented by minoura
 1.61 08-Jan-2001  itojun redo previous (simplify KPTR)
 1.60 08-Jan-2001  itojun backout previous; did not compile on non-alpha
 1.59 08-Jan-2001  itojun simplify KPTR processing.
 1.58 08-Jan-2001  itojun address PR 11653. checked on alpha.
XXX supports 32bit and 64bit pointers only
 1.57 23-Oct-2000  simonb Add a K flag to the state information - K is for kernel thread or system
process (ie, P_SYSTEM set in p_flag). The L flag (locked in core) is
only used for non-P_SYSTEM processes.
 1.56 29-Jun-2000  mrg remove include of <vm/vm.h>
 1.55 08-Jun-2000  simonb branches: 1.55.2;
Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.54 08-Jun-2000  simonb Fix back-to-front assignment in tname() which meant that the column
width would never autosize past the header ("TT") width. Problem
noticed by Geoff Wing.
 1.53 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.52 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.51 26-May-2000  simonb branches: 1.51.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.50 26-May-2000  thorpej Handle SONPROC state.
 1.49 05-Dec-1999  fredb Fix CL* purge fallout.
 1.48 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.47 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.46 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.45 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.44 11-Oct-1999  mrg skip "dty" just like we skip "tty", otherwise we just get "dt" in ps output for all dty's
 1.43 23-Jul-1999  veego branches: 1.43.2;
According to the previous commit the new process state should be SDEAD and
not SDYING, so s/SDYING/SDEAD/.
 1.42 22-Jul-1999  thorpej Update for SDEAD.
 1.41 03-May-1999  mrg limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.40 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.39 28-Jul-1998  mycroft branches: 1.39.2;
Look for the formats used by setproctitle(3) and login shells, and avoiding
outputting the redundant `(comm)'.
 1.38 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.37 01-Apr-1998  kleink Need <time.h> for localtime(), time() and strftime() prototypes.
 1.36 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.35 05-Feb-1998  gwr Begone KERNBASE! (Just print kernel pointers as-is.)
 1.34 03-Aug-1997  mikel use %c not %C for lstart; from tetsuya@secom-sis.co.jp via OpenBSD
 1.33 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.32 19-Mar-1997  mycroft DTRT if e_wmesg is not NUL-terminated.
 1.31 19-Mar-1997  mycroft DTRT if e_login is not NUL-terminated.
 1.30 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.29 15-Aug-1996  explorer return 0.0 cpu usage for zombies. closes pr bin/2693. Perhaps this should
be fixed in exit, so p->p_pctcpu is cleared when the process exits.
 1.28 13-Jul-1996  explorer include <pwd.h> for one function (user_from_uid) and correct one
%*d -> %*ld in a format string. (yes, trying to find the problem with
ps wwwwwwwww1 and core dump, and might as well clean up while here)
 1.27 29-Sep-1995  cgd add code to convert from INT32 and UINT32 to native types.
 1.26 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.25 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.24 07-Jun-1995  cgd typeof(timeval.ts_sec) != time_t
 1.23 25-May-1995  mycroft If there is another field after the command, fill in the command field with
blanks.
 1.22 21-May-1995  mycroft Split out some helper functions again.
 1.21 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.20 18-May-1995  mycroft Avoid an extra (gratuitous) pass through strvis(3).
 1.19 08-May-1995  cgd add the ability to understand and print INT and UINT-sized variables.
 1.18 21-Mar-1995  cgd convert to new RCS id conventions.
 1.17 23-Dec-1994  cgd be careful with addresses.
 1.16 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.15 03-Nov-1994  deraadt kill UPAGES
 1.14 15-Sep-1994  deraadt NBPG -> getpagesize()
 1.13 16-Jun-1994  pk No more P_NOSWAP/P_PHYSIO.
 1.12 09-May-1994  cgd clean up import
 1.11 05-May-1994  cgd struct element foo
 1.10 05-May-1994  cgd update for changed flags
 1.9 14-Aug-1993  mycroft Fix test for zombie process.
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 18-Jun-1993  cgd use strvis on the arguments and environment, so we don't end up in
sed and/or xterm-title-changing hell.
 1.6 02-Jun-1993  cgd make ps do something more sensible with zombie processes
 1.5 01-Jun-1993  cgd fix more-or-less-uninitialized variable...
 1.4 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.39.2.2 08-Nov-1999  cgd pull up revs 1.45-1.46 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.39.2.1 12-Oct-1999  he Pull up revision 1.44 (requested by mrg):
Make various utilities skip "dty" as well as "tty" on terminals,
so that the actually used tty is visiable instead of just 'dt'.
 1.43.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.51.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.55.2.1 27-Jan-2001  jhawk Pull up revisions 1.63-1.65 (requested by hubertf):
Clarify meaning of COMMANDs printed in parentheses. Addresses PR11930.
 1.71.2.7 17-Jan-2003  thorpej Sync with HEAD.
 1.71.2.6 12-Nov-2002  nathanw Catch up to -current.
 1.71.2.5 26-Jul-2002  nathanw Print state letter 'U' for suspended LWPs.
Make sure to print '+' (controlling terminal) character last.
 1.71.2.4 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.71.2.3 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.71.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.71.2.1 21-Feb-2002  nathanw file print.c was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.96.4.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.96.2.1 30-Jul-2007  liamjfoy Pull up following revision(s) (requested by dsl in ticket #798):
bin/ps/print.c: revision 1.100
Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.100.6.2 28-Jul-2007  dsl Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.100.6.1 28-Jul-2007  dsl file print.c was added on branch matt-mips64 on 2007-07-28 17:05:51 +0000
 1.100.4.3 23-Mar-2008  matt sync with HEAD
 1.100.4.2 09-Jan-2008  matt sync with HEAD
 1.100.4.1 06-Nov-2007  matt sync with HEAD
 1.104.4.1 18-May-2008  yamt sync with head.
 1.106.2.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.109.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.115.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.117.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.118.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.118.2.1 17-Apr-2012  yamt sync with head
 1.120.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.121.2.1 10-Aug-2014  tls Rebase.
 1.122.2.1 14-May-2015  snj Pull up following revision(s) (requested by joerg in ticket #758):
bin/ksh/Makefile: revision 1.31
bin/ps/print.c: revision 1.123
external/bsd/iscsi/dist/src/lib/parameters.c: revision 1.4
external/bsd/iscsi/dist/src/lib/target.c: revision 1.9
external/gpl2/lvm2/dist/lib/commands/toolcontext.c: revision 1.8
external/gpl2/lvm2/dist/lib/format1/import-export.c: revision 1.2
gnu/dist/texinfo/makeinfo/xml.c: revision 1.2
sbin/bioctl/bioctl.c: revision 1.16
usr.sbin/isdn/isdnd/msghdl.c: revision 1.12
Drop pointer checks that are always true.
--
Comment out impossible code.
--
Remove tautologic check.
--
Skip vendor if the string is empty.
Use l_wmesg if the string is not empty. Don't bother checking l_name for
nullness.
--
Array can't be null.
--
Show display if it is not empty.
--
Make cast warnings for clang non-fatal.
 1.123.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.127.6.1 06-Apr-2021  martin Pull up following revision(s) (requested by simonb in ticket #1669):

bin/ps/print.c: revision 1.133

Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.128.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.128.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.129.2.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.129.2.3 21-Apr-2020  martin Sync with HEAD
 1.129.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.129.2.1 10-Jun-2019  christos Sync with HEAD
 1.132.2.1 06-Apr-2021  martin Pull up following revision(s) (requested by simonb in ticket #1244):

bin/ps/print.c: revision 1.133

Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.10 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.9 26-May-2000  simonb branches: 1.9.6;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.8 07-Apr-2000  jdolecek Ignore also "self" link when searching for proc info.
Patch sent by NAKAJIMA Yoshihiro in bin/9819
 1.7 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.6 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.5 09-May-1999  thorpej branches: 1.5.2;
KNF.
 1.4 28-Mar-1999  bgrayson branches: 1.4.2;
- Be really paranoid. Any time we open a file that is in /proc, run
fstatfs() on the open file to verify that no intruder has mounted
something on portions of /proc. This will catch, for instance,
"mount_null /tmp /proc/1378". We already do 5 syscalls per
process, so one more won't hurt :) ... and safety is better than
performance when ps is otherwise broken.
- Also added a few close()'s at early returns, to avoid chewing up fd's.
 1.3 27-Mar-1999  bgrayson Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.2 27-Mar-1999  tron Add missing prototype so that this actually compiles.
 1.1 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.4.2.1 08-Nov-1999  cgd pull up revs 1.5-1.7 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.5.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.9.6.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.115 06-Nov-2021  msaitoh s/maxiumum/maximum/
 1.114 14-Sep-2021  christos reflect reality on flags.
 1.113 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.112 04-Jun-2021  wiz Sort options in synopsis.
 1.111 04-Jun-2021  cjep PR standards/11223

Add -G to take a single group argument heading towards POSIX.2 compliance.
Patch from jperkin and reviewed by simonb.
 1.110 06-Aug-2019  kamil branches: 1.110.6;
Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.

These values were disabled since day0 until today.

libkvm(3) users will still receive inappropriate values as RSS statistics
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.109 28-Aug-2017  wiz branches: 1.109.4; 1.109.6;
Bump date for previous. Remove/replace bogus Tn uses.
 1.108 28-Aug-2017  kamil Remove the filesystem tracing feature

This is a legacy interface from 4.4BSD, and it was
introduced to overcome shortcomings of ptrace(2) at that time, which are
no longer relevant (performance). Today /proc/#/ctl offers a narrow
subset of ptrace(2) commands and is not applicable for modern
applications use beyond simplistic tracing scenarios.

This removal will simplify kernel internals. Users will still be able to
use all the other /proc files.

This change won't affect other procfs files neither Linux compat
features within mount_procfs(8). /proc/#/ctl isn't available on Linux.

Remove:
- /proc/#/ctl from mount_procfs(8)
- P_FSTRACE note from the documentation of ps(1)
- /proc/#/ctl and filesystem tracing documentation from mount_procfs(8)
- KAUTH_REQ_PROCESS_PROCFS_CTL documentation from kauth(9)
- source code file miscfs/procfs/procfs_ctl.c
- PFSctl and procfs_doctl() from sys/miscfs/procfs/procfs.h
- KAUTH_REQ_PROCESS_PROCFS_CTL from sys/sys/kauth.h
- PSL_FSTRACE (0x00010000) from sys/sys/proc.h
- P_FSTRACE (0x00010000) from sys/sys/sysctl.h

Reduce code complexity after removal of this functionality.

Update TODO.ptrace accordingly: remove two entries about /proc tracing.

Do not keep legacy notes as comments in the headers about removed
PSL_FSTRACE / P_FSTRACE, as this interface had little number of users
(close or equal to zero).

Proposed on tech-kern@.

All filesystem tracing utility users are encouraged to switch to ptrace(2).

Sponsored by <The NetBSD Foundation>
 1.107 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.106 02-Dec-2016  christos branches: 1.106.6;
provide a tree like display with -d, from FreeBSD
 1.105 10-Aug-2016  sevan Earliest reference in the TUHS archive is v3
Unfortunately only the source code for the compiler is present for that
release. The v2 archive is missing the manuals & only contains a some of the
commands.
Switch to v3 and reference manual category eight, as per r1.32 of
src/bin/ps/ps.1 from OpenBSD.
Heads up from Ingo Schwarze.
 1.104 10-Aug-2016  sevan Document the version ps first appeared.
 1.103 15-Jan-2014  wiz branches: 1.103.8;
Bump date for previous.
 1.102 15-Jan-2014  mlelstv Add an LTIME column that prints lwp cputime.
 1.101 03-Jun-2012  joerg branches: 1.101.2;
Switch device database to cdb(5). Rework ttyname(3) and ttyname_r(3) to
depend on new devname_r(3) as heart. Add /dev/pts magic directly to
devname(3). While it can lead to returning non-existing paths, the
behavior is more consistent that way. Drop caching layer in devname(3),
it doesn't buy anything for the common case of having access to the
database. Teach devname(3) proper fallback behavior of scanning /dev.
Create both old-style and new-style database for now in /etc/rc.d/sysdb.
 1.100 15-Apr-2012  wiz Increase width of table column so it formats more nicely.
 1.99 15-Apr-2012  wiz - choose better argument name for `-U';
- add argument names for options in man page;
- improve wording, punctuation, capitalization.

From Bug Hunting in email.
 1.98 15-Mar-2012  wiz Remove trailing whitespace.
 1.97 15-Mar-2012  christos Fix obsolete example (that referred to letters in process state that are
not set anymore), and clarify their meaning.
 1.96 23-Feb-2012  dholland The 'lstart' column uses strftime %c, not %C. Noted by mrg.
 1.95 27-Jan-2012  dholland branches: 1.95.2;
Minor English improvements for -w, partly from Snader_LB.
 1.94 13-Feb-2011  dholland branches: 1.94.4;
fix typo, from Randolf Richardson in PR 44559
 1.93 14-May-2010  joerg branches: 1.93.2;
Don't break lines in .It, there be dragons in groff.
 1.92 22-Mar-2010  joerg Use .In instead of .Aq Pa for header files
 1.91 21-Oct-2009  wiz Bump date for uarea swap-out removal.
 1.90 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.89 03-Aug-2009  wiz Bump date for previous.
 1.88 03-Aug-2009  dsl Remove reference to P_SYSTRACE.
Fixes PR/41426
(leave defn in sys/sysctl.h since the bit shouldn't be reused!)
 1.87 13-Mar-2009  joerg Fix number of columns in .Bl. Escape %C.
 1.86 19-Oct-2008  wiz branches: 1.86.4;
Improve wording.
 1.85 19-Oct-2008  christos Use 'O' for LSONPROC like solaris instead of bundling LSIDL, LSRUN, LSONPROC
to 'R'
 1.84 26-Sep-2008  wiz Sort options.
Remove systrace reference.
 1.83 26-Sep-2008  apb Add "ps -A" option. This displays information about all processes,
like BSD "ps -ax" or SYSV "ps -ef". It's specified in SUSv3.

Also make some minor clarifications to the man page.
 1.82 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.81 31-Dec-2007  ad Remove systrace. Ok core@.
 1.80 09-Sep-2007  apb Document "emul" keyword. Fixes PR 36935.
Also sort list of keywords, and bump date.
 1.79 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.78 25-Jun-2007  yamt branches: 1.78.4;
remove descriptions of some states. ('A', 'S', '>')
- they are wrong. (madvise is not vadvise)
- they don't exist anymore.
 1.77 09-Oct-2006  wiz Remove trailing whitespace.
Remove a partially duplicated sentence.
 1.76 07-Oct-2006  apb Make it clear that multiple sort keys may be specified.
Closes PR 34271.
 1.75 03-Oct-2006  wiz Avoid .No if easily possible.
 1.74 02-Oct-2006  apb Add some markup missed in previous.
 1.73 02-Oct-2006  apb Document changes to -o and -O options, and improve markup.
 1.72 11-Nov-2005  chs add "uaddr" and improve text for "laddr".
 1.71 29-Dec-2004  skrll branches: 1.71.2;
Add the missing suspended state 'U'.

Bump date.
 1.70 23-Aug-2004  wiz Bump date for laddr.
 1.69 23-Aug-2004  nathanw Add a "laddr" keyword, analagous to "paddr". Useful for finding
struct pcb's.
 1.68 23-Apr-2004  simonb s/the the/the/ (only in sources that aren't regularly imported from
elsewhere).
 1.67 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.66 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.65 13-Feb-2004  wiz Uppercase CPU where appropriate.
 1.64 16-Oct-2003  wiz Add Xr for dev_mkdb(8), per PR 23168. Increase width in FILES section while here.
 1.63 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.62 16-Apr-2003  wiz More mdoc.
 1.61 17-Mar-2003  wiz Update date for recent changes.
 1.60 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.59 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.58 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.57 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.56 01-Oct-2002  wiz Use more mdoc.
 1.55 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.54 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.53 17-Jun-2002  wiz Sort SEE ALSO.
 1.52 17-Jun-2002  christos Document some missing flags and add systrace(1) 'x' flag.
 1.51 24-Apr-2002  wiz "a, b, c, and d", not "a, b, c and d".
 1.50 04-Mar-2002  wiz branches: 1.50.2;
Drop trailing space.
 1.49 04-Mar-2002  ad Xref pkill/pgrep.
 1.48 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.47 20-Dec-2001  wiz Properly quote punctuation.
 1.46 20-Dec-2001  wiz Whitespace nits.
 1.45 20-Aug-2001  wiz precede, not preceed.
 1.44 07-Aug-2001  christos Print system processes/kernel threads as [kprocname] as top(1) does.
 1.43 04-Jul-2001  sommerfeld correct the description of paddr
 1.42 15-Jan-2001  christos clarify things a bit more (from hubertf)
 1.41 15-Jan-2001  christos Really explain what parenthesized names mean and why we parenthesize names!
 1.40 15-Jan-2001  hubertf Document the meaning of commands shown in parantheses.

Patches submitted by Jeremy C. Reed <reed@reedmedia.net> in PR 11930.
 1.39 23-Oct-2000  simonb Add a K flag to the state information - K is for kernel thread or system
process (ie, P_SYSTEM set in p_flag). The L flag (locked in core) is
only used for non-P_SYSTEM processes.
 1.38 28-Aug-2000  nathanw Update description of process flag 0x20000 to match reality:
P_NOCLDWAIT, not P_SSTEP.
 1.37 24-Jun-2000  simonb Remove the 'poip' (pageouts in progress) keyword from the manpage.
From PR bin/8843 by Brian Stark.
 1.36 15-Apr-2000  simonb branches: 1.36.4;
Use tty "?" to select processes with no controlling tty and tty "-" to
select processes with a revoked controlling tty.

Idea from SunOS/Solaris, suggested by Alexis Rosen.
 1.35 10-Apr-2000  chs make -M imply -K. the current system is not likely to be
a reasonable approximation of a crash dump.
 1.34 22-Feb-2000  dbj name database is kvm.db not kvm_netbsd.db
 1.33 04-Dec-1999  hubertf Document -U switch (displays processes belonging to given user/uid).
Reported in PR 8943 by WL <wliao@dura.spc.uchicago.edu>
 1.32 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.31 27-Mar-1999  bgrayson branches: 1.31.6;
Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.30 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.29 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.28 17-Mar-1999  msaitoh ps's information is sorted by controlling terminal and (among processes with
the same controlling terminal) by PID.
 1.27 07-Mar-1999  mycroft Clean up SYNOPSIS formatting.
 1.26 07-Mar-1999  mycroft Fix formatting errors.
 1.25 02-Jan-1999  tron The system namelist database is called "/var/db/kvm.db" not
"/var/db/kvm_netbsd.db". Fixes PR bin/6712 by Brian Grayson.
 1.24 28-Apr-1998  fair change references to vadvise to madvise; remove reference to lisp(1)
 1.23 24-Mar-1998  bouyer Sync with reality: the uprocp keyword doesn't exist.
 1.22 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.21 11-Oct-1997  enami branches: 1.21.2;
Use .Nm "" instead of .Nm ps in SYNOPSIS.
 1.20 10-Oct-1997  lukem all but the first .Nm in SYNOPSIS need to be spelt out. [bin/4256]
 1.19 14-Sep-1997  lukem getopt returns -1 not EOF
cleanup manpage
 1.18 27-Aug-1997  mikel default swap device is /dev/drum, not nonexistent /dev/swap; from
<entropy@tardis.bernstein.com> in PR bin/4042
 1.17 23-Jun-1997  pk Refer to execve(2), add section to `vfork' ref. (almost per PR#3627).
 1.16 21-Mar-1996  jtc fix damage caused by sccs (long ago)
 1.15 08-May-1995  cgd add a 'holdcnt' keyword and document it.
 1.14 07-May-1995  pk Sync process flags with sys/proc.h
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 16-Sep-1994  mycroft Integrate `-c' option, from Arne Juul.
 1.11 06-Aug-1994  jtc vmunix -> netbsd (PR #375)
 1.10 09-May-1994  cgd branches: 1.10.2;
clean up import
 1.9 11-Jan-1994  jtc Fix spelling errors.
 1.8 07-Oct-1993  cgd fix from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>:
don't try to display the resident text size; we can't do that anymore
 1.7 01-Aug-1993  mycroft Add RCS indentifiers.
 1.6 13-Jun-1993  mycroft Fix up FILES sections.
 1.5 13-Jun-1993  mycroft Update file names.
 1.4 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.1 06-Aug-1994  mycroft update from trunk
 1.21.2.1 05-May-1998  mycroft Pull up 1.23, per request of bouyer. Also do 1.24.
 1.31.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.36.4.2 27-Jan-2001  jhawk Pull up revisions 1.40-1.42 (requested by hubertf):
Clarify meaning of COMMANDs printed in parentheses. Addresses PR11930.
 1.36.4.1 25-Jun-2000  simonb Pull up rev 1.37:

Remove the 'poip' (pageouts in progress) keyword from the manpage.
From PR bin/8843 by Brian Stark.
 1.50.2.5 12-Nov-2002  nathanw Catch up to -current.
 1.50.2.4 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.50.2.3 23-Apr-2002  wrstuden Add -s to synopsis.
 1.50.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.50.2.1 04-Mar-2002  nathanw file ps.1 was added on branch nathanw_sa on 2002-04-23 20:41:13 +0000
 1.71.2.1 12-Nov-2005  tron Pull up following revision(s) (requested by chs in ticket #953):
bin/ps/ps.1: revision 1.72
add "uaddr" and improve text for "laddr".
 1.78.4.3 23-Mar-2008  matt sync with HEAD
 1.78.4.2 09-Jan-2008  matt sync with HEAD
 1.78.4.1 06-Nov-2007  matt sync with HEAD
 1.86.4.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.93.2.1 17-Feb-2011  bouyer Sync with HEAD
 1.94.4.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.94.4.2 30-Oct-2012  yamt sync with head
 1.94.4.1 17-Apr-2012  yamt sync with head
 1.95.2.1 23-Jun-2012  riz Pull up following revision(s) (requested by joerg in ticket #328):
lib/libc/gen/devname.c: revision 1.22
distrib/sparc64/instfs/dot.profile: revision 1.7
distrib/i386/cdroms/install.sh: revision 1.3
tests/lib/libcurses/director/director.c: revision 1.10
lib/libc/include/reentrant.h: revision 1.15
distrib/amd64/cdroms/install.sh: revision 1.3
tests/include/t_paths.c: revision 1.12
share/man/man7/hier.7: revision 1.101
etc/rc.d/sysdb: revision 1.24
include/stdlib.h: revision 1.99
tests/lib/libcurses/Makefile: revision 1.3
distrib/sparc64/cdroms/installcd/install.sh: revision 1.3
distrib/i386/cdroms/Makefile.cdrom: revision 1.27
lib/libc/gen/ttyname.c: revision 1.25
usr.bin/tic/Makefile: revision 1.2
tools/compat/Makefile: revision 1.57
lib/libterminfo/compile.c: revision 1.7
lib/libc/gen/ttyname.c: revision 1.26
lib/libterminfo/compile.c: revision 1.8
tools/compat/Makefile: revision 1.59
tools/compat/cdbw.h: revision 1.1
distrib/i386/installimage/install.sh: revision 1.2
lib/libc/include/namespace.h: revision 1.154
usr.bin/tic/tic.c: revision 1.13
usr.bin/tic/tic.c: revision 1.14
usr.bin/tic/tic.c: revision 1.15
distrib/sets/lists/base/mi: revision 1.996
usr.bin/tic/tic.c: revision 1.16
usr.sbin/dev_mkdb/dev_mkdb.8: revision 1.13
usr.bin/tic/tic.c: revision 1.17
usr.bin/tic/tic.c: revision 1.18
tools/compat/cdbr.h: revision 1.1
distrib/sparc64/instfs/list: revision 1.25
usr.bin/tic/tic.c: revision 1.19
lib/libc/cdb/cdbw.c: revision 1.4
lib/libc/cdb/cdbw.3: revision 1.4
lib/libc/cdb/cdbw.3: revision 1.5
share/terminfo/Makefile: revision 1.3
usr.sbin/dev_mkdb/dev_mkdb.c: revision 1.29
usr.sbin/dev_mkdb/Makefile: revision 1.8
lib/libc/gen/ttyname.3: revision 1.23
lib/libc/gen/ttyname.3: revision 1.24
lib/libc/cdb/cdbr.c: revision 1.3
include/cdbw.h: revision 1.2
distrib/sparc64/cdroms/installcd/Makefile: revision 1.15
lib/libc/gen/devname.3: revision 1.12
tools/compat/compat_defs.h: revision 1.85
tools/compat/compat_defs.h: revision 1.86
lib/libterminfo/term_private.h: revision 1.10
lib/libterminfo/term.c: revision 1.14
usr.bin/tic/tic.1: revision 1.10
lib/libterminfo/term.c: revision 1.15
lib/libterminfo/Makefile.hash: revision 1.5
lib/libterminfo/term.c: revision 1.16
distrib/amd64/installimage/install.sh: revision 1.2
share/man/man5/rc.conf.5: revision 1.152
distrib/sets/lists/tests/mi: revision 1.472
distrib/amd64/cdroms/Makefile.cdrom: revision 1.7
include/paths.h: revision 1.41
bin/ps/ps.1: revision 1.101
lib/libterminfo/terminfo.5.in: revision 1.16
usr.bin/tic/tic.c: revision 1.20
lib/libterminfo/terminfo.5.in: revision 1.17
Prefer __printflike.
Use queue(3)
Replace linear lookup with hash table, reducing runtime by 60%.
Use e* from util.h.
One more strdup -> estrdup.
Maintain nalias and nterm on the fly.
Add void for ttyslot prototype.
Use memcmp in place of strncmp, since length is known to no longer than
either of the arguments.
Fix a number of memory leaks. Keep final loop of the cleanup in tic(1)
under #ifdef __VALGRIND__ though.
Add __predict_true / __predict_false definitions.
Add a special seeder for cdbw_output that gives stable results.
Hook up cdbw(3) for the tool build.
Prototype for cdbw_stable_seeder.
Provide a usable thr_once for !_REENTRANT
Switch device database to cdb(5). Rework ttyname(3) and ttyname_r(3) to
depend on new devname_r(3) as heart. Add /dev/pts magic directly to
devname(3). While it can lead to returning non-existing paths, the
behavior is more consistent that way. Drop caching layer in devname(3),
it doesn't buy anything for the common case of having access to the
database. Teach devname(3) proper fallback behavior of scanning /dev.
Create both old-style and new-style database for now in /etc/rc.d/sysdb.
Switch terminfo(3) to cdb(5).
Fix typos.
Make Alan Barrett happy and add ().
Add cdbr(3) support to tools/compat.
Bump date for previous.
Relax consistency to make it possible to match valid entries.
Prefix devname's buffer with /dev/ as ttyname is supposed to return the
full path. Reported by Dave Huang.
 1.101.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.103.8.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.106.6.1 12-Apr-2018  martin Pull up following revision(s) (requested by kamil in ticket #713):

sys/modules/procfs/Makefile: revision 1.4
sys/miscfs/procfs/procfs_vfsops.c: revision 1.98
bin/ps/ps.1: revision 1.108
sys/compat/linux/arch/i386/linux_ptrace.c: revision 1.32
sys/miscfs/procfs/procfs_vnops.c: revision 1.198
sys/kern/sys_ptrace_common.c: revision 1.23
sys/kern/sys_ptrace_common.c: revision 1.24
sbin/mount_procfs/mount_procfs.8: revision 1.36
sys/kern/sys_ptrace_common.c: revision 1.25
sys/kern/sys_ptrace.c: revision 1.5
sys/compat/linux/arch/powerpc/linux_ptrace.c: revision 1.30
sys/sys/proc.h: revision 1.342
sys/kern/sys_ptrace_common.c: revision 1.26
sys/miscfs/procfs/procfs_ctl.c: file removal
sys/kern/sys_ptrace_common.c: revision 1.27
sys/miscfs/procfs/procfs_subr.c: revision 1.109
sys/kern/sys_ptrace_common.c: revision 1.28
sys/secmodel/extensions/secmodel_extensions.c: revision 1.8
sys/kern/sys_ptrace_common.c: revision 1.29
sys/sys/ptrace.h: revision 1.62
sys/compat/netbsd32/netbsd32_signal.c: revision 1.45
share/man/man9/kauth.9: revision 1.109
sys/miscfs/procfs/files.procfs: revision 1.12
sys/compat/netbsd32/netbsd32.h: revision 1.115
sys/miscfs/procfs/procfs.h: revision 1.72
sys/compat/netbsd32/netbsd32_ptrace.c: revision 1.5
sys/kern/kern_sig.c: revision 1.337
sys/sys/kauth.h: revision 1.75
sys/sys/sysctl.h: revision 1.224
sys/kern/sys_ptrace_common.c: revision 1.30
sys/kern/sys_ptrace_common.c: revision 1.31
sys/kern/sys_ptrace_common.c: revision 1.32
sys/kern/sys_ptrace_common.c: revision 1.33
sys/compat/linux/arch/arm/linux_ptrace.c: revision 1.20
sys/kern/sys_ptrace_common.c: revision 1.34
sys/kern/sys_ptrace_common.c: revision 1.36
sys/kern/kern_proc.c: revision 1.207
sys/kern/kern_exit.c: revision 1.269
doc/TODO.ptrace: revision 1.29

Make {s,g}et{db,fp,}regs work again for PK_32 processes
XXX: pullup-8

add disgusting magic to handle compat_netbsd32 as a module.

use process_*reg32 instead of struct *reg32.

Remove the filesystem tracing feature

This is a legacy interface from 4.4BSD, and it was
introduced to overcome shortcomings of ptrace(2) at that time, which are
no longer relevant (performance). Today /proc/#/ctl offers a narrow
subset of ptrace(2) commands and is not applicable for modern
applications use beyond simplistic tracing scenarios.

This removal will simplify kernel internals. Users will still be able to
use all the other /proc files.

This change won't affect other procfs files neither Linux compat
features within mount_procfs(8). /proc/#/ctl isn't available on Linux.

Remove:
- /proc/#/ctl from mount_procfs(8)
- P_FSTRACE note from the documentation of ps(1)
- /proc/#/ctl and filesystem tracing documentation from mount_procfs(8)
- KAUTH_REQ_PROCESS_PROCFS_CTL documentation from kauth(9)
- source code file miscfs/procfs/procfs_ctl.c
- PFSctl and procfs_doctl() from sys/miscfs/procfs/procfs.h
- KAUTH_REQ_PROCESS_PROCFS_CTL from sys/sys/kauth.h
- PSL_FSTRACE (0x00010000) from sys/sys/proc.h
- P_FSTRACE (0x00010000) from sys/sys/sysctl.h

Reduce code complexity after removal of this functionality.

Update TODO.ptrace accordingly: remove two entries about /proc tracing.

Do not keep legacy notes as comments in the headers about removed

PSL_FSTRACE / P_FSTRACE, as this interface had little number of users
(close or equal to zero).
Proposed on tech-kern@.

All filesystem tracing utility users are encouraged to switch to ptrace(2).

Sponsored by <The NetBSD Foundation>

untangle the mess:
- factor out common code
- break each ptrace subcall to its own sub-function
.. more to come ...
- reduce ifdef ugliness by moving it up top.
- factor out PT_IO and make PT_{READ,WRITE}_{I,D} use it
- factor out PT_DUMPCORE
- factor out sendsig code
.. more to come ...

handle siginfo requests for ptrace32

ptrace: Partially undo PT_{READ,WRITE}_{I,D} and unbreak these commands

The refactored code did not work and was generating EFAULT.

Sponsored by <The NetBSD Foundation>

Merge the code back; the problem was that since we are reading/writing
to a kernel address for PT_{READ,WRITE}_{I,D} we need the kernel vmspace.
provide separate read and write functions to accomodate register functions
that need a size argument.

don't ignore error from copyout_piod

Use the proper process (the tracee) to get information about lwps and
registers and the tracer for vmspace.

Add new sysctl(3) entry: security.models.extensions.user_set_dbregs

Model this new sysctl(3) entry after "user_set_cpu_affinity" in the same
level of sysctl(3) switches.

Allow to read unconditionally Debug Registers (no change here). This is
convenient as even if a user of a debugger does not use hardware assisted
watchpoints/breakpoints, a debugger can still prompt these values to store
in an internal cache with context of registers. Reading them should have
no security concerns.

Add a paranoid MI switch that prohibits by default setting these registers
by a regular user (non-superuser). Make this switch disabled by default.
There are enough reserved bits out there to allow using them
unconditionally on hardened hosts.

Features shipped with Debug Registers are optional features in debuggers.
There is no reduction in elementary functionality.

Reviewed by <christos>

Sponsored by <The NetBSD Foundation>
 1.109.6.1 07-Aug-2019  martin Pull up following revision(s) (requested by kamil in ticket #24):

bin/ps/ps.1: revision 1.110
bin/ps/keyword.c: revision 1.57

Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.
These values were disabled since day0 until today.
libkvm(3) users will still receive inappropriate values as RSS statistics=
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.109.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.109.4.2 21-Apr-2020  martin Sync with HEAD
 1.109.4.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.110.6.1 06-Jun-2021  cjep sync with head
 1.98 13-Jul-2025  rillig ps: remove unreachable break statement after return statement

No binary change.
 1.97 14-Sep-2021  christos branches: 1.97.4;
use emalloc and friends, add ktrace flag printing.
 1.96 04-Jun-2021  christos use parsenum like everywhere else in the code.
 1.95 04-Jun-2021  wiz Sort options in usage.
 1.94 04-Jun-2021  cjep PR standards/11223

Add -G to take a single group argument heading towards POSIX.2 compliance.
Patch from jperkin and reviewed by simonb.
 1.93 15-Sep-2019  kamil branches: 1.93.6;
ps(1): Guard freeing the memory of pinfo with __NO_LEAKS.

No more leaks are detected by LSan/NetBSD as of the LLVM snapshot
(clang10svn) from 2019-09-15.
 1.92 11-Sep-2019  kamil Plug memory leak in ps(1)

pinfo is allocated in setpinfo() with calloc(3).

Free it when no longer used, just before the program termination.

Detected with LSan.
 1.91 11-Apr-2018  christos branches: 1.91.2;
kvm_geterr() already contains errno, use errx.
 1.90 11-Apr-2018  christos use EXIT_FAILURE instead of 1
 1.89 12-Jan-2018  kamil branches: 1.89.2;
Fix an unitialized memory read bug in ps(1)

rawcpu of type int, is declared inside main(){} and it can be passed as
uninitialized to setpinfo().
The setpinfo() function has a switch checking the value of rawcpu:

if (!rawcpu)
pi[i].pcpu /= 1.0 - exp(ki[i].p_swtime * log_ccpu);

rawcpu is set to 1 with the command line argument "-C".

-C Change the way the CPU percentage is calculated by using a
"raw" CPU calculation that ignores "resident" time (this
normally has no effect).

Bug reproducible with an invocation: "ps u". It hides with "ps uC".

Initialize rawcpu by default to 0, before the getopt(3) machinery.

Detected with MSan running on NetBSD/amd64.

Sponsored by <The NetBSD Foundation>
 1.88 26-Dec-2016  rin branches: 1.88.6;
simplify logic; there must be no CPU usage when p_swtime is zero
 1.87 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.86 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.85 28-Nov-2016  rin KNF
ok martin
 1.84 05-Sep-2016  sevan Drop main() prototype.
 1.83 16-Jun-2015  christos branches: 1.83.2;
remove \n from error messages
 1.82 28-Jun-2014  dholland Don't print junk errnos. Avoids:
% ps -tfoo
ps: /dev/ttyfoo: Undefined error: 0
Exit 1
 1.81 20-Apr-2014  dholland Don't bother using variables whose value is never changed from the
initialization value.
 1.80 19-Feb-2014  dsl branches: 1.80.2;
Remove some pointless inclusions os sys/user.h
 1.79 15-Jan-2014  mlelstv Make ps -s use LTIME instead of TIME in the default output format.
 1.78 07-May-2012  joerg branches: 1.78.2;
Push logic to convert a ttyname to a device number into its own
function. Improve dealing with ptyfs by explicitly handling missing
pts/%d entries, if the kernel supports the pts device (PR 40813).
 1.77 15-Apr-2012  wiz Choose better argument name for `-U' (sync with man page).
From Bug Hunting in email.
 1.76 29-Aug-2011  joerg branches: 1.76.2; 1.76.4;
Use __dead
 1.75 31-May-2010  rmind Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.74 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.73 14-Feb-2009  lukem fix -Wsign-compare issues
 1.72 28-Dec-2008  christos branches: 1.72.2;
fix dev_t 64 bit issues.
 1.71 26-Sep-2008  wiz branches: 1.71.4;
Sync usage with man page.
 1.70 26-Sep-2008  apb Add "ps -A" option. This displays information about all processes,
like BSD "ps -ax" or SYSV "ps -ef". It's specified in SUSv3.

Also make some minor clarifications to the man page.
 1.69 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.68 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.67 28-Apr-2008  martin branches: 1.67.2;
Remove clause 3 and 4 from TNF licenses
 1.66 10-Mar-2008  dholland branches: 1.66.2;
ps -t? needs to imply -x. Otherwise the non-x logic filters all the results
out. Thanks to der Mouse for prompting me to fix it...
 1.65 10-Feb-2008  christos branches: 1.65.2;
Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.64 24-Oct-2007  yamt make -s alter the default format rather than doing parsefmt.
it allows users to use -o to replace it later.
 1.63 17-Feb-2007  pavel branches: 1.63.4;
Revert the changes done after newlock2 merge, except for the removal of
the now unused LSDEAD state. The current kernel headers are now
source-compatible with pre-newlock2 kernel headers again.

Approved by Andrew Doran.
 1.62 09-Feb-2007  ad Sync with kernel changes introduced by merging the newlock2 branch.
 1.61 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.60 02-Sep-2006  christos factor out the code that does numeric parsing into a function and check for
errors properly.
 1.59 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.58 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.57 11-Feb-2005  christos allow -t ttyp0 -t p0 -t /dev/ttyp0 -t /dev/pts/0
 1.56 29-Nov-2004  atatat Make ps understand "-tpts/4" by assuming that ptyfs is mounted on
/dev/pts and that the '/' immediately preceding the digits does not
need a 'p' in front of it (ick, kludge_oldps_options()).
 1.55 29-Oct-2004  dsl Add (unsigned char) cast to ctype function
 1.54 27-Mar-2004  simonb #define<TAB>
 1.53 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.52 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.51 11-Jan-2004  jdolecek g/c SDEAD
 1.50 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.49 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols)
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- use PRId64 to remove some long long casts
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified
(approved by christos)
 1.48 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.47 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.46 20-Dec-2001  soren branches: 1.46.2;
Sync getopt() / man page with actual getopt options.
 1.45 03-Nov-2001  lukem fix minor WARNS=2
 1.44 30-Jul-2001  wiz Remove duplicate `if'. Noted by Geoff C. Wing in bin/13551.
 1.43 20-Jul-2001  matt Add a small optimizetion in savevars(). break out of the loop once needcomm
is set since setting it multiple times only wastes cpu time.
 1.42 14-Jun-2001  cyber If there were no processes to list, it meant that our criteria failed
and didnt match anything. Dont make our caller think we succeeded.
 1.41 16-Jun-2000  simonb Don't check namelist when choosing to use the sysctl interface.
 1.40 08-Jun-2000  simonb Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.39 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.38 26-May-2000  simonb branches: 1.38.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.37 29-Apr-2000  abs If ps cannot get the terminal size from STDOUT, it tried STDERR and STDIN.
What this does to the POLS when a script that is parsing ps output just happens
to run in a narrow terminal cannot be described in polite company.
If STDOUT is not a tty, leave width at 80.
 1.36 15-Apr-2000  simonb Use tty "?" to select processes with no controlling tty and tty "-" to
select processes with a revoked controlling tty.

Idea from SunOS/Solaris, suggested by Alexis Rosen.
 1.35 10-Apr-2000  chs make -M imply -K. the current system is not likely to be
a reasonable approximation of a crash dump.
 1.34 04-Dec-1999  hubertf Document -U switch (displays processes belonging to given user/uid).
Reported in PR 8943 by WL <wliao@dura.spc.uchicago.edu>
 1.33 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.32 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.31 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.30 15-Oct-1999  jdolecek getkinfo_kvm(): fix bug in previous - handled size_t i as if it would be signed
saveuser(): don't check kd - if we get here, kd is never NULL
 1.29 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.28 27-Mar-1999  bgrayson branches: 1.28.2; 1.28.4;
Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.27 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.26 04-Jan-1999  kim Fixed segfaulting for "ps -T". There is no optarg with -T, so instructions
under label "tty:" need to use ttname instead.
 1.25 28-Jul-1998  mycroft Add a -U option to select by user/uid.
 1.24 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.23 28-Jul-1998  mycroft Delint.
 1.22 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.21 06-Jul-1998  mrg - change setgid kmem programs (that lend themselves to this) so setegid(getgid())
and the top, and then set the effective gid back to kmem around the call to
kvm_openfiles(). this reduces the time group kmem is available.
- for those above that also allow this, setgid(getgid()) after the call to
kvm_openfiles() to fully revoke priviledges.
- some KNF
- use err(3) over fprintf(3) in some places
 1.20 14-Sep-1997  lukem getopt returns -1 not EOF
cleanup manpage
 1.19 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.18 21-Apr-1997  mrg note that strcpy is safe.
 1.17 28-Feb-1997  pk Message buffer passed to kvm_openfiles() should _POSIX2_LINE_MAX long; PR#3266
 1.16 27-Sep-1996  thorpej Fix SEGV when kludging old-style options.
From Greg Stark <gsstark@mit.edu>, PR #2550.
 1.15 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.14 18-May-1995  mycroft Gather the argument and environment lists when we actually want to print them.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 16-Sep-1994  mycroft Integrate `-c' option, from Arne Juul.
 1.11 09-May-1994  cgd clean up import
 1.10 05-May-1994  cgd update for changed flags
 1.9 07-Oct-1993  cgd fix from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>:
don't try to display the resident text size; we can't do that anymore
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 19-Jul-1993  cgd branches: 1.7.2;
from Brian Somers <brian@awfulhak.demon.co.uk>:
The kvm entry isn't updated for command unless 'time' is given,
therefore
ps -o command
outputs blank lines [or, in NetBSD, seg faults]
ps -o time,command
works.
 1.6 11-Jul-1993  cgd fix alien 'w' handling. you really don't want to know...
 1.5 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.2.2 19-Jul-1993  cgd from Brian Somers <brian@awfulhak.demon.co.uk>:
The kvm entry isn't updated for command unless 'time' is given,
therefore
ps -o command
outputs blank lines [or, in NetBSD, seg faults]
ps -o time,command
works.
 1.7.2.1 19-Jul-1993  cgd file ps.c was added on branch netbsd-0-9 on 1993-07-19 11:02:11 +0000
 1.28.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.28.2.2 22-May-2000  he Pull up revision 1.37 (requested by abs):
Only check the terminal size via STDOUT, not STDERR or STDIN.
 1.28.2.1 08-Nov-1999  cgd pull up revs 1.29-1.31 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.38.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.46.2.11 12-Nov-2002  nathanw Catch up to -current.
 1.46.2.10 24-Oct-2002  nathanw Small typo fix.

Treat SSTOP and SACTIVE the same way in pick_representative_lwp(), and
include LSSTOP in the state hierarchy.
 1.46.2.9 26-Jul-2002  nathanw Consider LWPs in LSSUSPENDED as a worst-case for a representative LWP.
 1.46.2.8 06-Jun-2002  nathanw Use the real process state when picking a representative LWP, not the
old-style state that was faked up for us by sysctl.
 1.46.2.7 02-May-2002  nathanw Don't puke on zero-LWP processes (zombies).
 1.46.2.6 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.46.2.5 23-Apr-2002  wrstuden Add -s to synopsis.
 1.46.2.4 23-Apr-2002  nathanw Don't need to free the kinfo_lwp pointer; libkvm does that.
 1.46.2.3 23-Apr-2002  nathanw Don't shadow global symbol "sleep".
 1.46.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.46.2.1 20-Dec-2001  nathanw file ps.c was added on branch nathanw_sa on 2002-04-23 20:41:14 +0000
 1.63.4.2 23-Mar-2008  matt sync with HEAD
 1.63.4.1 06-Nov-2007  matt sync with HEAD
 1.65.2.1 24-Mar-2008  keiichi sync with head.
 1.66.2.2 04-Jun-2008  yamt sync with head
 1.66.2.1 18-May-2008  yamt sync with head.
 1.67.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.67.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.71.4.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.72.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.76.4.1 24-Jun-2012  riz Pull up following revision(s) (requested by dholland in ticket #355):
bin/ps/ps.c: revision 1.78
Push logic to convert a ttyname to a device number into its own
function. Improve dealing with ptyfs by explicitly handling missing
pts/%d entries, if the kernel supports the pts device (PR 40813).
 1.76.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.76.2.2 23-May-2012  yamt sync with head.
 1.76.2.1 17-Apr-2012  yamt sync with head
 1.78.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.80.2.1 10-Aug-2014  tls Rebase.
 1.83.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.88.6.1 03-Feb-2018  snj Pull up following revision(s) (requested by kamil in ticket #515):
bin/ps/ps.c: revision 1.89
Fix an unitialized memory read bug in ps(1)
rawcpu of type int, is declared inside main(){} and it can be passed as
uninitialized to setpinfo().
The setpinfo() function has a switch checking the value of rawcpu:
if (!rawcpu)
pi[i].pcpu /= 1.0 - exp(ki[i].p_swtime * log_ccpu);
rawcpu is set to 1 with the command line argument "-C".
-C Change the way the CPU percentage is calculated by using a
"raw" CPU calculation that ignores "resident" time (this
normally has no effect).
Bug reproducible with an invocation: "ps u". It hides with "ps uC".
Initialize rawcpu by default to 0, before the getopt(3) machinery.
Detected with MSan running on NetBSD/amd64.
Sponsored by <The NetBSD Foundation>
 1.89.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.91.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.91.2.2 21-Apr-2020  martin Sync with HEAD
 1.91.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.93.6.1 06-Jun-2021  cjep sync with head
 1.97.4.1 02-Aug-2025  perseant Sync with HEAD
 1.32 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.31 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.30 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.29 02-Dec-2016  christos branches: 1.29.14;
provide a tree like display with -d, from FreeBSD
 1.28 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.27 20-Apr-2014  dholland branches: 1.27.6;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.26 02-Oct-2006  apb branches: 1.26.40; 1.26.46; 1.26.60;
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.25 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.24 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.23 27-Mar-2004  simonb #define<TAB>
 1.22 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.19 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.18 24-Apr-2002  nathanw Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.17 07-Jun-2000  simonb branches: 1.17.4;
Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.16 26-May-2000  simonb branches: 1.16.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.15 03-May-1999  mrg oops, fix merge botch in previous..
 1.14 03-May-1999  mrg limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.13 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.12 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.11 29-Sep-1995  cgd add INT32 and UINT32 variable type specifiers. some of the variables
that are printed are of fixed size.
 1.10 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.9 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.8 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.7 08-May-1995  cgd add the ability to understand and print INT and UINT-sized variables.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 09-May-1994  cgd clean up import
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.17.4.2 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.17.4.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.26.60.1 10-Aug-2014  tls Rebase.
 1.26.46.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.26.40.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.6.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.29.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.14.1 21-Apr-2020  martin Sync with HEAD

RSS XML Feed