---
name: "kvm_close"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/kvm_close"
---

# kvm\_close(3)

initialize kernel virtual memory access

## NAME

**kvm\_open**, **kvm\_open2**, **kvm\_openfiles**, **kvm\_close** – initialize kernel virtual memory access

## LIBRARY

Kernel Data Access Library (libkvm, -lkvm)

## SYNOPSIS

**#include \<fcntl.h\>**

**#include \<kvm.h\>**

*kvm\_t* *\**\

**kvm\_open**(*const char \*execfile*, *const char \*corefile*, *const char \*swapfile*, *int flags*, *const char \*errstr*);

*kvm\_t* *\**\

**kvm\_open2**(*const char \*execfile*, *const char \*corefile*, *int flags*, *char \*errbuf*, *int (\*resolver)(const char \*name, kvaddr\_t \*addr)*);

*kvm\_t* *\**\

**kvm\_openfiles**(*const char \*execfile*, *const char \*corefile*, *const char \*swapfile*, *int flags*, *char \*errbuf*);

*int*\

**kvm\_close**(*kvm\_t \*kd*);

## DESCRIPTION

The functions **kvm\_open**(), **kvm\_open2**(), and **kvm\_openfiles**() return a descriptor used to access kernel virtual memory via the [kvm(3)](/kvm/3.md) library routines. Both active kernels and crash dumps are accessible through this interface.

The *execfile* argument is the executable image of the kernel being examined. This file must contain a symbol table. If this argument is NULL, the currently running system is assumed, as determined from [getbootfile(3)](/getbootfile/3.md).

The *corefile* argument is the kernel memory device file. It can be either */dev/mem* or a crash dump core generated by [savecore(8)](/savecore/8.md). If *corefile* is NULL, the default indicated by \_PATH\_MEM from ⟨paths.h⟩ is used. It can also be set to a special value */dev/null* by utilities like [ps(1)](/ps/1.md) that do not directly access kernel memory.

The *swapfile* argument is currently unused.

The *flags* argument indicates read/write access as in [open(2)](/open/2.md) and applies only to the core file. Only O\_RDONLY, O\_WRONLY, and O\_RDWR are permitted.

The **kvm** library provides two different error reporting mechanisms. One provides backward compatibility with the SunOS kvm library, while the other provides an improved error reporting framework. The mechanism used by a descriptor is determined by the function used to open the descriptor.

The **kvm\_open**() function is the Sun kvm compatible open call. Here, the *errstr* argument indicates how errors should be handled. If it is NULL, no errors are reported and the application cannot know the specific nature of the failed kvm call. If it is not NULL, errors are printed to stderr with *errstr* prepended to the message, as in [perror(3)](/perror/3.md). Normally, the name of the program is used here. The string is assumed to persist at least until the corresponding **kvm\_close**() call.

The **kvm\_open2**() and **kvm\_openfiles**() functions provide BSD style error reporting. Here, error messages are not printed out by the library. Instead, the application obtains the error message corresponding to the most recent kvm library call using **kvm\_geterr**() (see [kvm\_geterr(3)](/kvm_geterr/3.md)). The results are undefined if the most recent kvm call did not produce an error. Since **kvm\_geterr**() requires a kvm descriptor, but the open routines return NULL on failure, **kvm\_geterr**() cannot be used to get the error message if open fails. Thus, **kvm\_open2**() and **kvm\_openfiles**() will place any error message in the *errbuf* argument. This buffer should be \_POSIX2\_LINE\_MAX characters large (from \<limits.h\>).

The *resolver* argument points to a function used by the **kvm** library to map symbol names to kernel virtual addresses. When the *resolver* function is called, *name* specifies the requested symbol name. If the function is able to resolve the name to an address, the address should be set in *addr* and the function should return zero. If the function is not able to resolve the name to an address, it should return a non-zero value. When opening a native kernel image, *resolver* may be set to NULL to use an internal function to resolve symbol names. Non-native kernel images (such as when cross-debugging a crash dump) require a valid *resolver*.

## RETURN VALUES

The **kvm\_open**(), **kvm\_open2**(), and **kvm\_openfiles**() functions return a descriptor to be used in all subsequent kvm library calls. The library is fully re-entrant. On failure, NULL is returned, in which case **kvm\_open2**() and **kvm\_openfiles**() write the error message into *errbuf*.

The **kvm\_close()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The **kvm\_close**() function may fail and set the global variable *errno* for any of the errors specified for [close(2)](/close/2.md).

The **kvm\_close**() function may also fail and set *errno* if:

- \[EINVAL\]

  The value passed via *kd* was NULL.

## NOTE

Full memory dumps taken on 13.x (excluding 13.0) and 14.x amd64 kernels will cause both **kvm\_open**() and **kvm\_open2**() to fail since they do not provide sufficient information to figure out where in physical memory the kernel was loaded. Full memory dumps have to be explicitly enabled by setting the *debug.minidump* [sysctl(8)](/sysctl/8.md) to 0.

## SEE ALSO

[close(2)](/close/2.md), [open(2)](/open/2.md), [kvm(3)](/kvm/3.md), [kvm\_getargv(3)](/kvm_getargv/3.md), [kvm\_getenvv(3)](/kvm_getenvv/3.md), [kvm\_geterr(3)](/kvm_geterr/3.md), [kvm\_getprocs(3)](/kvm_getprocs/3.md), [kvm\_native(3)](/kvm_native/3.md), [kvm\_nlist(3)](/kvm_nlist/3.md), [kvm\_read(3)](/kvm_read/3.md), [kvm\_write(3)](/kvm_write/3.md), [kmem(4)](/kmem/4.md), [mem(4)](/mem/4.md), [dumpon(8)](/dumpon/8.md)

## BUGS

There should not be three open calls. The ill-defined error semantics of the Sun library and the desire to have a backward-compatible library for BSD left little choice.

## HISTORY

The **kvm\_open2**() function first appeared in FreeBSD 11.0.
