Home | History | Annotate | Download | only in libkvm
History log of /src/lib/libkvm/kvm_proc.c
RevisionDateAuthorComments
 1.100  15-Dec-2024  christos remove unused.
 1.99  10-Aug-2023  mrg avoid various use-after-free issues.

create a ptrdiff_t offset between the start of an allocation region and
some interesting pointer, so it can be adjusted with this offset after
realloc() returns.

found by GCC 12.
 1.98  19-Apr-2022  rillig lib: remove CONSTCOND comment

Since 2021-01-31, lint doesn't need it anymore for the common pattern of
'do ... while (0)'.
 1.97  10-Jan-2022  christos read the ps arguments from the correct place.
 1.96  10-Jan-2022  christos Get rid of usrstack/USRSTACK. Document that the old version of getargv is
broken because of ASLR.
 1.95  19-Jul-2021  christos Match the declaration in <sys/kauth.h>
 1.94  11-Dec-2019  ad Redo previous more conventionally. Requested by kre@.
 1.93  10-Dec-2019  ad Mask out always zero bits off pg->phys_addr.
 1.92  04-Apr-2016  christos branches: 1.92.16;
some ports need <sys/wait.h>
 1.91  04-Apr-2016  christos catch up with p_xstat split.
 1.90  19-Feb-2014  dsl Remove the #include <sys/user.h> from all of libkvm.
sys/user.h is a stub that just #includes sys/pcb.h.
There are no 'struct pcb' anywhere in here, so I'm extremely doubtful
any of the builds will fail.
OTOH it might be relying on a header that pcb.h includes.
In any case i386 and amd64 build.
 1.89  05-Jun-2012  martin branches: 1.89.2;
Measure kinfo_proc2::p_vm_vsize in pages, as it was always documented.
This value seems to never have been used anywhere.
This makes it consistent with it's cousin p_vm_msize (which is in pages as
well and has several uses).
 1.88  12-Nov-2010  uebayasi branches: 1.88.6; 1.88.8;
Fix build.
 1.87  26-Sep-2010  jym Define KREAD() inside kvm_private.h, for reusability.
 1.86  20-Sep-2010  jym Change kvm_pa2off() and kvm_kvatop() prototypes (private to kvm(3)):

-int _kvm_kvatop(kvm_t *, u_long, u_long *);
-off_t _kvm_pa2off(kvm_t *, u_long);
+int _kvm_kvatop(kvm_t *, vaddr_t, paddr_t *);
+off_t _kvm_pa2off(kvm_t *, paddr_t);

Basically, use vaddr_t for VA and paddr_t for PA. In addition, for variables
representing addresses, use paddr_t or vaddr_t, depending on the context.

For most arches, vaddr_t and paddr_t are equivalent to unsigned long. However,
the change was needed for exotic situations, like i386 PAE, were unsigned long
is not suitable for PA which are 64 bits long. As this required a complete
change of the function prototypes, all arches had to be adapted accordingly.

Core files from before this commit should still work with the new code; I did
not see any direct dependency between core's structure and kvatop/pa2off.

The change was compile tested for all arches, as it impacts all of them.

See also:

http://mail-index.netbsd.org/current-users/2010/09/07/msg014249.html
 1.85  19-Sep-2010  jym Ansify and KNF all functions within kvm(3). No objection on current-users@.

Used as ground for u_long => vaddr_t/paddr_t replacements in kvm(3)
private functions.

Compile tested for all arches. See also:

http://mail-index.netbsd.org/current-users/2010/09/07/msg014249.html
 1.84  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.83  16-May-2009  yamt kvm_getprocs actually works with KVM_NO_FILES.
 1.82  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.81  28-Dec-2008  christos branches: 1.81.2;
tdev is unsigned
 1.80  28-Dec-2008  christos cast dev_t's to int32_t because XXX: sysctl can't change size. This should
be not a problem since tty's live in the low part of the dev range.
 1.79  29-Nov-2008  cegger check error case for kvm_getlwps or SIGSEGV occurs due to dereferecing a NULL pointer.
return NULL if there are no threads or proceed further through all loops.
Fixes PR 40004. Ok rmind@
 1.78  28-Apr-2008  martin branches: 1.78.6;
Remove clause 3 and 4 from TNF licenses
 1.77  05-Feb-2008  elad branches: 1.77.4;
PR/37962: Andrew Doran: libkvm is busted due to kauth_cred redefinition

Sync structure with kernel version.
 1.76  15-Jan-2008  ad Handle reading from raw disk devices.
 1.75  22-Dec-2007  yamt sync with kernel.
 1.74  06-Nov-2007  ad Catch up with scheduling changes in the kernel.
 1.73  09-Jul-2007  ad branches: 1.73.4;
'true' is a C99 keyword.
 1.72  07-Jul-2007  christos treat ESRCH as a soft error. From Anon Ymous
 1.71  06-Jul-2007  christos - Fix kvm_getlwps not to spit errors if we lose the race:
- if the process does not exist anymore, return NULL
- if the process has created more lwps retry
XXX[1]: We should fix sysctl_kern_lwp() to return the number of lwps it
needs in oldlenp when ENOMEM. The we can avoid calling sysctl
twice (by starting let's say with always 1 lwp, or 10).
XXX[2]: We should fix kvm_getlwps to never spit errors.
- Silence new lint warnings
 1.70  17-May-2007  christos fix for yamt-idlelwp
 1.69  01-May-2007  dsl Set kp2p->p_ru = 0 in the kproc2 structure.
It was only ever non-zero for zombies, and now doesn't exist.
The 'pstats' field has been valid for zombies for a while (it isn't in
the uarea any more).
Fixes part of build breakage.
 1.68  24-Feb-2007  christos Revert the kauth_impl.h change. Elad is going to maintain this. Asked by core@
 1.67  18-Feb-2007  dsl Update only user code that uses the types of the e_[pu]cred members
of struct kinfo_proc.
 1.66  09-Feb-2007  ad Sync with kernel changes introduced by merging the newlock2 branch.
 1.65  06-Feb-2007  elad PR/35506: Chuck Silvers: ps doesn't work on crash dumps, libkvm doesn't
know about kauth

Implemented _kvm_convertcred() to handle copying kauth_cred_t to userspace
and converting it to the appropriate 'struct eproc' members ('pcred' and
'ucred' respectively).

Okay mlelstv@.
 1.64  28-Jan-2007  chs fill in p_stat for crashdumps like the kernel does for the sysctl.
 1.63  31-Dec-2006  yamt kvm_getproc2: retry on ENOMEM.
it happens when the number of processes are changed in the mean time.
 1.62  11-May-2006  yamt branches: 1.62.4;
#include a necessary header directly, rather than via user.h.
 1.61  16-Feb-2006  christos 1. Eliminate some unnecessary to kvm_{m,re}alloc.
2. Don't malloc/free procbase/procbase2/lwpbase continuously. Keep track
of the size, and only do it if necessary.
3. Write a macro to malloc/realloc and set the size of members so that it
is done correctly. Previous open coded version in kvm_file.c always
set the length, which is incorrect.
4. Remove bogus check against INT_MAX.
5. use NULL to initialize pointers instead of 0.
 1.60  30-Jul-2005  yamt as swap is now optional, ENXIO on /dev/drum is normal.
 1.59  11-May-2005  jmc Fix fallout from vm_anon changes
 1.58  12-May-2004  toshii Fix typos.
 1.57  19-Feb-2004  atatat Use new PTRTOUINT64() macro instead of local PTRTOINT64() macro.
 1.56  17-Nov-2003  christos zero out nl, from Todd Miller. thanks!
 1.55  29-Sep-2003  wiz available, not avaliable. From miod@openbsd.
 1.54  07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22280, verified by myself.
 1.53  28-Mar-2003  christos remove scanning of deadproc, since zombproc should contain all the processes
in deadproc according to the comment in kern_proc.c. deadproc is not a
struct proclist anymore, so it would require more work to scan anyway, and
this is unnecessary.
 1.52  20-Mar-2003  ross fix LP64 and other warnings
 1.51  19-Mar-2003  dsl Alternative pid/proc allocater, removes all searches associated with pid
lookup and allocation, and any dependency on NPROC or MAXUSERS.
NO_PID changed to -1 (and renamed NO_PGID) to remove artificial limit
on PID_MAX.
As discussed on tech-kern.
 1.50  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.49  26-Feb-2003  enami Use _kvm_freeprocs() rather than free() so that kvm_close() won't
free already free'ed region.
 1.48  26-Feb-2003  enami Cosmetic changes.
 1.47  02-Feb-2003  christos delint (unsigned long long) -> (unsigned long) casts.
 1.46  18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.45  24-Mar-2001  jdolecek branches: 1.45.2;
kvm_doargv2(): make sure the string ends with '\0'; sysctl() might not
properly end it if the data would be longer than the supplied
buffer

This fixes lib/11467 by SAITOH Masanobu and lib/12389, maybe also
lib/12439.
 1.44  24-Mar-2001  jdolecek Minor fix to kvm_doargv2() - if the kd->argv is reallocated, reinitialize
'ap' too. This doesn't seem to have any effect either way; seems like the
condition is never met?
 1.43  22-Dec-2000  jdolecek adapt to latest struct proc, struct sigacts, struct sigctx changes
 1.42  05-Oct-2000  enami Make this file compiles on alpha.
 1.41  04-Oct-2000  sommerfeld format-string audit cleanups
 1.40  16-Jul-2000  christos revert part of previous; u_long -> uintptr_t now that uintptr_t is u_long.
 1.39  16-Jul-2000  christos de-lint. Most important problem was the cast from pointer to uintptr_t to
u_int64_t; changed the uintptr_t to u_long.
 1.38  29-Jun-2000  mrg <vm/vm.h> -> <uvm/uvm_extern.h>
 1.37  26-Jun-2000  mrg remove redundant vm includes
 1.36  04-Jun-2000  tron branches: 1.36.2;
Use "NULL" instead of "0" in pointer assignments and comparisons.
 1.35  27-May-2000  thorpej branches: 1.35.2;
p_schedflags doesn't exist anymore; just set it to zero for now,
although we'll be able to get the correct info through kmem
groveling later.
 1.34  26-May-2000  simonb Add kvm interface to the new sysctls:
kvm_getproc2() -> sysctl(KERN_PROC2)
kvm_getargv2() -> sysctl(KERN_PROC_ARGS, KERN_PROC_ARGV)
kvm_getenvv2() -> sysctl(KERN_PROC_ARGS, KERN_PROC_ENV)
Add new KVM_NO_FILES flag to kvm_open*() - set up enough state to
use the above calls without having to open any kernel files.

XXX: kvm_getprocs.3 and kvm_open.3 to be updated soon.
 1.33  15-Apr-2000  simonb Fill in e_sid when building the eproc from a crash dump.
 1.32  15-Jan-2000  chs in kvm_getprocs(), check for error from kvm_deadprocs().
 1.31  02-Jul-1999  simonb More trailing white space.
 1.30  24-Mar-1999  mrg branches: 1.30.2;
completely remove Mach VM support. all that is left is the all the
header files as UVM still uses (most of) these.
 1.29  25-Jan-1999  mrg catch up with the UVM amap changes.. tested by nathanw@mit.edu, thanks.
 1.28  27-Sep-1998  christos Remove lint
 1.27  09-Sep-1998  thorpej Adjust for the reaper process.

XXX Would really like to use proclists[] here, but not right now.
 1.26  15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.25  10-Aug-1998  perry bzero->memset, bcopy->memcpy, bcmp->memcmp
 1.24  30-Jun-1998  thorpej Use pread(2) and pwrite(2) rather than lseek(2)/{read,write}(2).
 1.23  12-Feb-1998  chs add UVM support.
 1.22  11-Feb-1998  mrg while it is still kinda broken, at least let it compile -DUVM...
 1.21  03-Feb-1998  perry remove obsolete register declarations
 1.20  15-Aug-1997  drochner Fix compiler warnings.
 1.19  15-Aug-1997  mikel use <sys/cdefs.h> __RCSID() macro
 1.18  12-Aug-1997  gwr Instead of: USRSTACK, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS
Use these: kd->usrstack, kd->min_uva, kd->max_uva
 1.17  20-Jun-1997  mikel fix some printf() formats
 1.16  18-Mar-1996  thorpej RCS id police.
 1.15  04-Jul-1995  cgd slight type cleanup. add some prototypes for internal functions,
some 'const' poisoning, some lvalue lossage.
 1.14  25-May-1995  mycroft Fix up the last argv pointer correctly when terminating early.
 1.13  18-May-1995  mycroft Fix one more reallocing bug.
 1.12  18-May-1995  mycroft Fix up another pointer if we realloc the argument list.
 1.11  16-May-1995  mycroft Update copyright.
 1.10  16-May-1995  mycroft Use the new ps_strings format. Find the arguments using their
addresses in the argv array, rather than guessing based on the location
of NULs.
 1.9  12-Jan-1995  pk The sparc port no longer needs special casing.
 1.8  09-Jan-1995  mycroft Eliminate _kvm_uvatop(), in favor of using the machine-independent VM structures.
 1.7  19-Oct-1994  cgd fix type of variables, add headers, protos, where necessary.
 1.6  18-Sep-1994  mycroft Move the swap page buffer into struct __kvm. Cache the page size during
kvm_open(). Fix an existing memory leak.
 1.5  18-Sep-1994  deraadt Don't use NBPG/PGOFSET/PGSHIFT. Use getpagesize() and calculate them.
 1.4  30-Aug-1994  mycroft Update to match include files.
 1.3  13-Aug-1994  mycroft Don't return garbage if there are no argument and/or environment strings.
 1.2  23-May-1994  mycroft branches: 1.2.2;
Deal with shadow objects correctly.
 1.1  09-May-1994  cgd branches: 1.1.1;
Initial revision
 1.1.1.1  09-May-1994  cgd new libkvm
 1.2.2.1  13-Aug-1994  mycroft update from trunk
 1.30.2.2  08-Oct-2000  he Pull up revision 1.42 (via patch, requested by he):
Fix compilation problem after format string audit fixes.
 1.30.2.1  04-Oct-2000  he Pull up revision 1.41 (via patch, requested by sommerfeld):
Format string audit.
 1.35.2.1  23-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.36.2.1  30-Mar-2001  he Pull up revision 1.45 (requested by jdolecek):
Properly '\0'-terminate data from sysctl() which might not end
properly if data would be longer than requested. Fixes PR#11467,
PR#12389 and possibly PR#12439.
 1.45.2.5  09-May-2002  nathanw Fill in new kinfo_proc2 flags from core.
 1.45.2.4  09-May-2002  nathanw Use kvm_read() to get l.l_wmesg from a core file.
 1.45.2.3  23-Apr-2002  nathanw Freeing the kinfo_lwp structure is centralized.
 1.45.2.2  23-Apr-2002  nathanw Make libkvm able to cope with a LWPified kernel.

Add a kvm_getlwps() function to retrieve the LWPs of a specific process.
 1.45.2.1  24-Mar-2001  nathanw file kvm_proc.c was added on branch nathanw_sa on 2002-04-23 20:10:20 +0000
 1.62.4.2  16-Feb-2007  riz Pull up following revision(s) (requested by chs in ticket #421):
lib/libkvm/kvm_proc.c: revision 1.64
fill in p_stat for crashdumps like the kernel does for the sysctl.
 1.62.4.1  11-Feb-2007  tron Pull up following revision(s) (requested by elad in ticket #415):
lib/libkvm/kvm_proc.c: revision 1.65
PR/35506: Chuck Silvers: ps doesn't work on crash dumps, libkvm doesn't
know about kauth
Implemented _kvm_convertcred() to handle copying kauth_cred_t to userspace
and converting it to the appropriate 'struct eproc' members ('pcred' and
'ucred' respectively).
Okay mlelstv@.
 1.73.4.3  23-Mar-2008  matt sync with HEAD
 1.73.4.2  09-Jan-2008  matt sync with HEAD
 1.73.4.1  06-Nov-2007  matt sync with HEAD
 1.77.4.1  18-May-2008  yamt sync with head.
 1.78.6.2  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.78.6.1  29-Nov-2008  bouyer Pull up following revision(s) (requested by cegger in ticket #146):
lib/libkvm/kvm_proc.c: revision 1.79
check error case for kvm_getlwps or SIGSEGV occurs due to dereferecing a
NULL pointer.
return NULL if there are no threads or proceed further through all loops.
Fixes PR 40004. Ok rmind@
 1.81.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.88.8.1  12-Jun-2012  riz Pull up following revision(s) (requested by martin in ticket #310):
sys/kern/kern_proc.c: revision 1.184
lib/libkvm/kvm_proc.c: revision 1.89
Measure kinfo_proc2::p_vm_vsize in pages, as it was always documented.
This value seems to never have been used anywhere.
This makes it consistent with it's cousin p_vm_msize (which is in pages as
well and has several uses).
 1.88.6.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.88.6.1  30-Oct-2012  yamt sync with head
 1.89.2.1  20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.92.16.1  08-Apr-2020  martin Merge changes from current as of 20200406

RSS XML Feed