1.\" $OpenBSD: kvm_open.3,v 1.13 2009/01/21 22:18:00 miod Exp $ 2.\" $NetBSD: kvm_open.3,v 1.2 1996/03/18 22:33:52 thorpej Exp $ 3.\" 4.\" Copyright (c) 1992, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" This code is derived from software developed by the Computer Systems 8.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract 9.\" BG 91-66 and contributed to Berkeley. 10.\" 11.\" Redistribution and use in source and binary forms, with or without 12.\" modification, are permitted provided that the following conditions 13.\" are met: 14.\" 1. Redistributions of source code must retain the above copyright 15.\" notice, this list of conditions and the following disclaimer. 16.\" 2. Redistributions in binary form must reproduce the above copyright 17.\" notice, this list of conditions and the following disclaimer in the 18.\" documentation and/or other materials provided with the distribution. 19.\" 3. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)kvm_open.3 8.3 (Berkeley) 4/19/94 36.\" 37.Dd $Mdocdate: January 21 2009 $ 38.Dt KVM_OPEN 3 39.Os 40.Sh NAME 41.Nm kvm_open , 42.Nm kvm_openfiles , 43.Nm kvm_close 44.Nd initialize kernel virtual memory access 45.Sh SYNOPSIS 46.Fd #include <fcntl.h> 47.Fd #include <kvm.h> 48.Ft kvm_t * 49.Fn kvm_open "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "const char *errstr" 50.Ft kvm_t * 51.Fn kvm_openfiles "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "char *errbuf" 52.Ft int 53.Fn kvm_close "kvm_t *kd" 54.Sh DESCRIPTION 55The functions 56.Fn kvm_open 57and 58.Fn kvm_openfiles 59return a descriptor used to access kernel virtual memory 60via the 61.Xr kvm 3 62library routines. 63Both active kernels and crash dumps are accessible through this interface. 64.Pp 65.Fa execfile 66is the executable image of the kernel being examined. 67This file must contain a symbol table. 68If this argument is 69.Dv NULL , 70the currently running system is assumed, 71which is indicated by 72.Dv _PATH_KSYMS , 73if it exists, otherwise 74.Dv _PATH_UNIX 75is used. 76Both are defined in 77.Aq Pa paths.h . 78.Pp 79.Fa corefile 80is the kernel memory device file. 81It can be either 82.Pa /dev/mem 83or a crash dump core generated by 84.Xr savecore 8 . 85If 86.Fa corefile 87is 88.Dv NULL , 89the default indicated by 90.Dv _PATH_MEM 91from 92.Aq Pa paths.h 93is used. 94.Pp 95.Fa swapfile 96should indicate the swap device. 97If 98.Dv NULL , 99no swap device will be used. 100.Pp 101The 102.Fa flags 103argument indicates read/write access as in 104.Xr open 2 105and applies only to the core file. 106Only 107.Dv O_RDONLY , 108.Dv O_WRONLY , 109and 110.Dv O_RDWR 111are permitted. 112A special value 113.Dv KVM_NO_FILES 114can be specified which will cause no files to be opened and the handle 115can only be used on live kernels on a limited subset of all kvm operations. 116.Pp 117There are two open routines which differ only with respect to 118the error mechanism. 119One provides backward compatibility with the SunOS kvm library, while the 120other provides an improved error reporting framework. 121.Pp 122The 123.Fn kvm_open 124function is the Sun kvm compatible open call. 125Here, the 126.Fa errstr 127argument indicates how errors should be handled. 128If it is 129.Dv NULL , 130no errors are reported and the application cannot know the 131specific nature of the failed kvm call. 132If it is not 133.Dv NULL , 134errors are printed to stderr with 135.Fa errstr 136prepended to the message, as in 137.Xr perror 3 . 138Normally, the name of the program is used here. 139The string is assumed to persist at least until the corresponding 140.Fn kvm_close 141call. 142.Pp 143The 144.Fn kvm_openfiles 145function provides BSD style error reporting. 146Here, error messages are not printed out by the library. 147Instead, the application obtains the error message 148corresponding to the most recent kvm library call using 149.Fn kvm_geterr 150(see 151.Xr kvm_geterr 3 ) . 152The results are undefined if the most recent kvm call did not produce 153an error. 154Since 155.Fn kvm_geterr 156requires a kvm descriptor, but the open routines return 157.Dv NULL 158on failure, 159.Fn kvm_geterr 160cannot be used to get the error message if open fails. 161Thus, 162.Fn kvm_openfiles 163will place any error message in the 164.Fa errbuf 165argument. 166This buffer should be 167.Dv _POSIX2_LINE_MAX 168characters large (from 169.Aq Pa limits.h ) . 170.Sh RETURN VALUES 171The 172.Fn kvm_open 173and 174.Fn kvm_openfiles 175functions both return a descriptor to be used 176in all subsequent kvm library calls. 177The library is fully re-entrant. 178On failure, 179.Dv NULL 180is returned, in which case 181.Fn kvm_openfiles 182writes the error message into 183.Fa errbuf . 184.Pp 185The 186.Fn kvm_close 187function returns 0 on success and \-1 on failure. 188.Sh SEE ALSO 189.Xr open 2 , 190.Xr kvm 3 , 191.Xr kvm_getargv 3 , 192.Xr kvm_getenvv 3 , 193.Xr kvm_geterr 3 , 194.Xr kvm_getprocs 3 , 195.Xr kvm_nlist 3 , 196.Xr kvm_read 3 , 197.Xr kvm_write 3 198.Sh BUGS 199There should not be two open calls. 200The ill-defined error semantics of the Sun library and the desire to have 201a backward-compatible library for BSD left little choice. 202