aboutsummaryrefslogtreecommitdiff
path: root/lib/libkvm/kvm.c
Commit message (Collapse)AuthorAgeFilesLines
* libkvm: Plug couple of memory leaks and check possible calloc(3) failureJung-uk Kim2021-03-061-0/+7
| | | | | | | | | | | | | First, r204494 introduced dpcpu_off in struct __kvm and it was allocated from _kvm_dpcpu_init() but it was not free(3)'ed from kvm_close(3). Second, r291406 introduced kvm_nlist2(3) and converted kvm_nlist(3) to use the new function but it did not free the temporary buffer. Also, check possible calloc(3) failure while I am in the neighborhood. Differential Revision: https://reviews.freebsd.org/D29019 (cherry picked from commit 645eaa2ccaed6eea801d07d6a092974fc1713896) (cherry picked from commit 483c6da3a20b2064cd655f7cb19e6b98dee677ff)
* Use __SCCSID for SCCS IDs in libkvm sources.John Baldwin2018-05-221-6/+1
| | | | | | | | | | | | | | Rather than using #ifdef's around a static char array, use the existing helper macro from <sys/cdefs.h> for SCCS IDs. To preserve existing behavior, add -DNO__SCCSID to CFLAGS to not include SCCS IDs in the built library by default. Reviewed by: brooks, dab (older version) Reviewed by: rgrimes Differential Revision: https://reviews.freebsd.org/D15459 Notes: svn path=/head/; revision=334053
* General further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-201-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
* libkvm: add kvm_walk_pages API.Will Andrews2017-11-111-0/+15
| | | | | | | | | | | | | | | | | | | | This API allows callers to enumerate all known pages, including any direct map & kernel map virtual addresses, physical addresses, size, offset into the core, & protection configured. For architectures that support direct map addresses, also generate pages for any direct map only addresses that are not associated with kernel map addresses. Fix page size portability issue left behind from previous kvm page table lookup interface. Reviewed by: jhb Sponsored by: Backtrace I/O Differential Revision: https://reviews.freebsd.org/D12279 Notes: svn path=/head/; revision=325728
* kvm_write: fix -Wcast-qual warning in pointer arithmetic argumentEnji Cooper2017-03-211-1/+1
| | | | | | | | | | | | | | Cast buf to `const char *` when doing arithmetic operation to match `cp`'s type [1]. Differential Revision: D10082 MFC after: 1 week Reviewed by: cem (earlier diff), vangyzen Submitted by: cem [1] Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=315688
* kvm_geterr: handle `kd` == NULL in a deterministic/graceful mannerEnji Cooper2017-03-211-0/+5
| | | | | | | | | | | | | | | | | | | Return a NUL string instead of just working by accident with kvm_geterr(3) when MALLOC_PRODUCTION is disabled (I didn't confirm the MALLOC_PRODUCTION being enabled path). Document the new explicit return behavior for kvm_geterr(3), as well as the previous implicit behavior, i.e., the buffer attached to returned via kvm_geterr(3) would be empty if a previous error hadn't been stored in `kd`. Differential Revision: D10022 MFC after: 1 week Reviewed by: vangyzen Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=315686
* Handle kd == NULL gracefully with kvm_close(3)Enji Cooper2017-03-201-0/+4
| | | | | | | | | | | | | | | Don't segfault in kvm_close(3) if provided a NULL pointer. Instead, return -1 and set errno to EINVAL. Document this new behavior explicitly. MFC after: 1 week Reviewed by: vangyzen Sponsored by: Dell EMC Isilon Differential Revision: D10065 Notes: svn path=/head/; revision=315647
* kvm_close(3): return `error` instead of blindly returning `0`Enji Cooper2017-03-201-1/+1
| | | | | | | | | | | | | | | `error` is the accumulated error from previous close(2) calls. This bug has been present since the libcall's import from 4.4BSD Lite (r1573). MFC after: 1 week Noticed by: vangyzen (D10022) Relnotes: yes Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=315603
* Return NULL instead of 0 on failure in _kvm_open, kvm_open{,2,files}Enji Cooper2017-03-161-4/+4
| | | | | | | | | | | | | This is being done for the following reasons: - kvm_open(3), etc says they will return NULL. - NULL by definition is (void*)0 per POSIX, but can be redefined, depending on the compiler, etc. MFC after: 1 week Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=315360
* Renumber copyright clause 4Warner Losh2017-02-281-1/+1
| | | | | | | | | | | | Renumber cluase 4 to 3, per what everybody else did when BSD granted them permission to remove clause 3. My insistance on keeping the same numbering for legal reasons is too pedantic, so give up on that point. Submitted by: Jan Schaumann <jschauma@stevens.edu> Pull Request: https://github.com/freebsd/freebsd/pull/96 Notes: svn path=/head/; revision=314436
* libkvm: support access to vmm guest memory, allow writes to fwmem and vmmAndriy Gapon2016-12-271-7/+47
| | | | | | | | | | | | | | | | | | | | | | This change consists of two parts: - allow libkvm to recognize /dev/vmm/* character devices as devices that provide access to the physical memory of a system (similarly to /dev/fwmem*) - allow libkvm to recognize that /dev/vmm/* and /dev/fwmem* devices provide access to the physical memory of live remote systems and, thus, the memory is writable As a result, it should be possible to run commands like $ kgdb -w /path/to/kernel /dev/fwmem0.0 $ kgdb /path/to/kernel /dev/vmm/guest Reviewed by: kib, jhb MFC after: 2 weeks Relnotes: yes Sponsored by: Panzura Differential Revision: https://reviews.freebsd.org/D8679 Notes: svn path=/head/; revision=310630
* libkvm: Improve physical address lookup scaling.Will Andrews2016-07-181-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | Instead of using a hash table to convert physical page addresses to offsets in the sparse page array, cache the number of bits set for each 4MB chunk of physical pages. Upon lookup, find the nearest cached population count, then add/subtract the number of bits from that point to the page's PTE bit. Then multiply by page size and add to the sparse page map's base offset. This replaces O(n) worst-case lookup with O(1) (plus a small number of bits to scan in the bitmap). Also, for a 128GB system, a typical kernel core of about 8GB will now only require ~4.5MB of RAM for this approach instead of ~48MB as with the hash table. More concretely, /usr/sbin/crashinfo against the same core improves from a max RSS of 188MB and wall time of 43.72s (33.25 user 2.94 sys) to 135MB and 9.43s (2.58 user 1.47 sys). Running "thread apply all bt" in kgdb has a similar RSS improvement, and wall time drops from 4.44s to 1.93s. Reviewed by: jhb Sponsored by: Backtrace I/O Notes: svn path=/head/; revision=302976
* libkvm: Put private kvm routines in kvm_private.c.Will Andrews2016-07-181-463/+0
| | | | Notes: svn path=/head/; revision=302974
* lib: minor spelling fixes in comments.Pedro F. Giffuni2016-05-011-2/+2
| | | | | | | No functional change. Notes: svn path=/head/; revision=298896
* Fix up pointer issues with lib/libkvmEnji Cooper2016-04-221-9/+13
| | | | | | | | | | | | | | | | | | | In particular, - avoid dereferencing NULL pointers - test pointers against NULL, not 0 - test for errout == NULL in the top-level functions (kvm_open, kvm_openfiles, kvm_open2, etc) - Replace a realloc and free on failure with reallocf Found with: devel/cocchinelle Differential Revision: https://reviews.freebsd.org/D5954 MFC after: 1 week Reviewed by: jhb Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=298485
* Add support to libkvm for reading vmcores from other architectures.John Baldwin2015-11-271-54/+333
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Add a kvaddr_type to represent kernel virtual addresses instead of unsigned long. - Add a struct kvm_nlist which is a stripped down version of struct nlist that uses kvaddr_t for n_value. - Add a kvm_native() routine that returns true if an open kvm descriptor is for a native kernel and memory image. - Add a kvm_open2() function similar to kvm_openfiles(). It drops the unused 'swapfile' argument and adds a new function pointer argument for a symbol resolving function. Native kernels still use _fdnlist() from libc to resolve symbols if a resolver function is not supplied, but cross kernels require a resolver. - Add a kvm_nlist2() function similar to kvm_nlist() except that it uses struct kvm_nlist instead of struct nlist. - Add a kvm_read2() function similar to kvm_read() except that it uses kvaddr_t instead of unsigned long for the kernel virtual address. - Add a new kvm_arch switch of routines needed by a vmcore backend. Each backend is responsible for implementing kvm_read2() for a given vmcore format. - Use libelf to read headers from ELF kernels and cores (except for powerpc cores). - Add internal helper routines for the common page offset hash table used by the minidump backends. - Port all of the existing kvm backends to implement a kvm_arch switch and to be cross-friendly by using private constants instead of ones that vary by platform (e.g. PAGE_SIZE). Static assertions are present when a given backend is compiled natively to ensure the private constants match the real ones. - Enable all of the existing vmcore backends on all platforms. This means that libkvm on any platform should be able to perform KVA translation and read data from a vmcore of any platform. Tested on: amd64, i386, sparc64 (marius) Differential Revision: https://reviews.freebsd.org/D3341 Notes: svn path=/head/; revision=291406
* Remove trailing whitespace.John Baldwin2015-11-261-1/+1
| | | | Notes: svn path=/head/; revision=291372
* Allow building a cross libkvm by setting TARGET_ARCH. The library soMarcel Moolenaar2013-12-281-2/+37
| | | | | | | | | | | | | | | produced will be called libkvm-${ARCH} instead of libkvm. This allows installing it alongside the native version. For symbol lookups, use ps_pglobal_lookup() instead of __fdnlist() when building a cross libkvm. It is assumed that the cross tool that uses the cross libkvm also provides an implementation for this proc_services function. Note that this commit does not change any of the architecture-specific code for cross-compilation. Notes: svn path=/head/; revision=260022
* libkvm: Make second /dev/null file descriptor close-on-exec as well.Jilles Tjoelker2013-05-041-1/+1
| | | | | | | MFC after: 1 week Notes: svn path=/head/; revision=250231
* libkvm: Use O_CLOEXEC instead of separate fcntl(F_SETFD) call.Jilles Tjoelker2013-05-041-15/+4
| | | | | | | MFC after: 1 week Notes: svn path=/head/; revision=250230
* libkvm: fix logic inversion introduced with last commitUlrich Spörlein2011-02-021-2/+2
| | | | | | | | Reported by: Brandon Gooch <jamesbrandongooch@gmail.com> Pointy hat to: uqs Notes: svn path=/head/; revision=218196
* libkvm code janitoringUlrich Spörlein2011-01-231-91/+64
| | | | | | | | | | | | | | | - make WARNS=6 clean for archs w/o strict alignment requirments - add const, ANSIfy, remove unused vars, cast types for comparison - thanks to differing definitions of VM_MIN_ADDRESS across our archs, we need to trick the compiler to not complain about signedness. We could either fix VM_MIN_ADDRESS to always be a simple integer or make the check conditional on $ARCH. Closes PRs: kern/42386, kern/83364 Reviewed by: bde Notes: svn path=/head/; revision=217744
* Similar to sys/net/vnet.h, define the linker set name for sys/sys/pcpu.hDimitry Andric2010-11-141-3/+4
| | | | | | | as a macro, and use it instead of literal strings. Notes: svn path=/head/; revision=215315
* A first cut at teaching libkvm how to deal with dynamic per-CPU storageRobert Watson2010-03-011-3/+20
| | | | | | | | | | | | | | | | | | | | (DPCPU): A new API, kvm_dpcpu_setcpu(3), selects the active CPU for the purposes of DPCPU. Calls to kvm_nlist(3) will automatically translate DPCPU symbols and return a pointer to the current CPU's version of the data. Consumers needing to read the same symbol on several CPUs will invoke a series of setcpu/nlist calls, one per CPU of interest. This addition makes it possible for tools like netstat(1) to query the values of DPCPU variables during crashdump analysis, and is based on similar code handling virtualized global variables. MFC after: 1 week Sponsored by: Juniper Networks, Inc. Notes: svn path=/head/; revision=204494
* Make libkvm work on live systems and crashdumps with andBjoern A. Zeeb2009-07-231-8/+164
| | | | | | | | | | | | | | | | | | | | | without VIMAGE virtualization in the kernel. If we cannot resolve a symbol try to see if we can find it with prefix of the virtualized subsystem, currently only "vnet_entry" by identifying either the vnet of the current process for a live system or the vnet of proc0 (or of dumptid if compiled in a non-default way). The way this is done currently allows us to only touch libkvm but no single application. Once we are going to virtualize more subsystems we will have to review this decision for better scaling. Submitted by: rwatson (initial version of kvm_vnet.c, lots of ideas) Reviewed by: rwatson Approved by: re (kib) Notes: svn path=/head/; revision=195838
* Reduce code duplication: use calloc() intead of malloc()Xin LI2008-10-171-4/+2
| | | | | | | and memset afterward. Notes: svn path=/head/; revision=183986
* Plug memory leaks that is observed when argbuf or argspc is used in theXin LI2008-01-121-0/+4
| | | | | | | | | | | context. Submitted by: Michal Vranek <michal.vranek seznam cz> PR: bin/118380 MFC after: 1 month Notes: svn path=/head/; revision=175243
* Re-enable raw dump format support on i386 and amd64 for /dev/fwmem.Hidetoshi Shimokawa2007-06-151-0/+2
| | | | Notes: svn path=/head/; revision=170772
* Remove the advertising clause. UCB did this some time ago, but theseWarner Losh2007-01-081-4/+0
| | | | | | | | | files were never updated to reflect that. MFC After: 2 days Notes: svn path=/head/; revision=165888
* Don't forget to set internal error message in kvm_nlist().Wojciech A. Koszek2006-09-201-2/+7
| | | | | | | Approved by: cognet (mentor) Notes: svn path=/head/; revision=162462
* Validate that the supplied file is not empty before trying mmap(2) itChristian S.J. Peron2006-01-151-0/+5
| | | | | | | | | | | and access the pages associated with it. Submitted by: Wojciech A. Koszek PR: bin/91411 MFC after: 1 week Notes: svn path=/head/; revision=154404
* Support crash dumps > 4GB in size on 32 bit platforms. _kvm_kvatop()Peter Wemm2005-06-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | returned an lseek offset in a "u_long *" value, which can't express >4GB offsets on 32 bit machines (eg: PAE). Change to "off_t *" for all. Support ELF crashdumps on i386 and amd64. Support PAE crashdumps on i386. This is done by auto-detecting the presence of the IdlePDPT which means that PAE is active. I used Marcel's _kvm_pa2off strategy and ELF header reader for ELF support on amd64. Paul Saab ported the amd64 changes to i386 and we implemented the PAE support from there. Note that gdb6 in the src tree uses whatever libkvm supports. If you want to debug an old crash dump, you might want to keep an old libkvm.so handy and use LD_PRELOAD or the like. This does not detect the old raw dump format. Approved by: re Notes: svn path=/head/; revision=147672
* Avoid assignments to cast expressions.Stefan Farfeleder2004-06-081-3/+3
| | | | | | | | Reviewed by: md5 Approved by: das (mentor) Notes: svn path=/head/; revision=130246
* Allow to specify a character special device as a core file.Hidetoshi Shimokawa2003-10-291-21/+18
| | | | | | | | | | | | | | | | | This enable us to use /dev/fwmem* as a core file. e.g. ps -M /dev/fwmem0.0 -N kernel.debug dmesg -M /dev/fwmem0.0 -N kernel.debug gdb -k -c /dev/fwmem0.0 kernel.debug You need to set target EUI64 in hw.firewire.fwmem.eui64_hi/lo before opening the device. On the target arch, (PCI) bus address must be equivalent to physical address. (We cannot use this for sparc64 because of IOMMU.) No objection in: -audit Notes: svn path=/head/; revision=121678
* Remove unnecssary <vm/swap_pager.h> includes.Poul-Henning Kamp2003-07-311-1/+0
| | | | | | | | These were probably not cleaned up back in whatever murky past these files were split into separate files. Notes: svn path=/head/; revision=118284
* In kvm_openfiles/kvm_open, mark the file descriptors as close-on-exec.Jacques Vidrine2002-09-111-0/+12
| | | | | | | | Applications can not do this themselves, as the descriptors are hidden behind the opaque `kvm_t' type. Notes: svn path=/head/; revision=103224
* Assume __STDC__, remove non-__STDC__ code.Alfred Perlstein2002-05-281-26/+0
| | | | | | | Submitted by: keramida Notes: svn path=/head/; revision=97407
* Remove __P() usage.David E. O'Brien2002-03-211-1/+1
| | | | Notes: svn path=/head/; revision=92917
* Remove 'register' keyword.David E. O'Brien2002-03-211-19/+19
| | | | Notes: svn path=/head/; revision=92913
* Finish cleanup in kvm.c revisions 1.10 and 1.11 -- mark sf (swapfile)Ruslan Ermilov2002-01-221-2/+2
| | | | | | | | | | | | argument to kvm_open() and kvm_openfiles() as unused. BSD didn't read swap since kvm.c CSRG revision 5.21 (u-area is pageable under new VM. no need to read from swap.) The old !NEWVM code was removed in CSRG revision 5.23 (~ten years ago). Notes: svn path=/head/; revision=89640
* Compensate for "Compensate for header dethreading" by backing it out.Bruce Evans2001-10-101-2/+0
| | | | Notes: svn path=/head/; revision=84768
* Implement __FBSDID()Matthew Dillon2001-09-161-3/+3
| | | | Notes: svn path=/head/; revision=83551
* Compensate for header dethreading.Mark Murray2001-05-011-0/+2
| | | | Notes: svn path=/head/; revision=76176
* Limit the amount of data copied to the error buffer to _POSIX2_LINE_MAX.Kris Kennaway2000-08-041-2/+2
| | | | | | | This is the documented size which the user-provided buffer must be. Notes: svn path=/head/; revision=64233
* Change the return value of kvm_read/kvm_write to be -1 on error, toJacques Vidrine2000-03-311-6/+6
| | | | | | | | | | | match the documented interface. Previously it returned 0 on error. PR: bin/10511 Notes: svn path=/head/; revision=58870
* -Wall, which caught a real bug where buflen wasn't being set properly.David E. O'Brien2000-03-271-0/+3
| | | | Notes: svn path=/head/; revision=58642
* Use kldsym(2) to lookup symbol values. This avoids the kvm_mkdb jugglingPeter Wemm1999-12-271-112/+21
| | | | | | | | | and is module aware. Yes, this means that kvm_nlist(3) will find symbols in loaded modules. The emulation of the nlist struct is pretty crude but seems to work well enough for all the users in the tree that I found. Notes: svn path=/head/; revision=55127
* Delete the sf (swapfile) arg to an internal function that used to point toPeter Wemm1999-10-111-6/+8
| | | | | | | | /dev/drum but has not been used for a LONG time. Add $FreeBSD$ Notes: svn path=/head/; revision=52117
* Don't open the swap file. The open descriptor for it hasn't been usedBruce Evans1999-08-251-8/+0
| | | | | | | | | for over 5 years since we switched to using procfs for kvm_uread(). This cleanup was motivated by recent breakage of the default swap file (/dev/drum) when swapon() has not been called. Notes: svn path=/head/; revision=50331
* Allow /dev/null as path for the "/dev/mem" file, and assume that peoplePoul-Henning Kamp1998-06-301-10/+14
| | | | | | | | | | know what they're doing if they do that. This will allow ps to use the kvm_proc.c bits without having access to /dev/mem. Fix kvm_proc.c to not need /dev/mem for access to argv/envp Notes: svn path=/head/; revision=37316