1.\" $OpenBSD: kvm_open.3,v 1.9 2003/06/02 20:18:41 millert 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 April 19, 1994 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 , 99.Dv _PATH_DRUM 100from 101.Aq Pa paths.h 102is used. 103.Pp 104The 105.Fa flags 106argument indicates read/write access as in 107.Xr open 2 108and applies only to the core file. 109Only 110.Dv O_RDONLY , 111.Dv O_WRONLY , 112and 113.Dv O_RDWR 114are permitted. 115A special value 116.Dv KVM_NO_FILES 117can be specified which will cause no files to be opened and the handle 118can only be used on live kernels on a limited subset of all kvm operations. 119.Pp 120There are two open routines which differ only with respect to 121the error mechanism. 122One provides backward compatibility with the SunOS kvm library, while the 123other provides an improved error reporting framework. 124.Pp 125The 126.Fn kvm_open 127function is the Sun kvm compatible open call. 128Here, the 129.Fa errstr 130argument indicates how errors should be handled. 131If it is 132.Dv NULL , 133no errors are reported and the application cannot know the 134specific nature of the failed kvm call. 135If it is not 136.Dv NULL , 137errors are printed to stderr with 138.Fa errstr 139prepended to the message, as in 140.Xr perror 3 . 141Normally, the name of the program is used here. 142The string is assumed to persist at least until the corresponding 143.Fn kvm_close 144call. 145.Pp 146The 147.Fn kvm_openfiles 148function provides BSD style error reporting. 149Here, error messages are not printed out by the library. 150Instead, the application obtains the error message 151corresponding to the most recent kvm library call using 152.Fn kvm_geterr 153(see 154.Xr kvm_geterr 3 ) . 155The results are undefined if the most recent kvm call did not produce 156an error. 157Since 158.Fn kvm_geterr 159requires a kvm descriptor, but the open routines return 160.Dv NULL 161on failure, 162.Fn kvm_geterr 163cannot be used to get the error message if open fails. 164Thus, 165.Fn kvm_openfiles 166will place any error message in the 167.Fa errbuf 168argument. 169This buffer should be 170.Dv _POSIX2_LINE_MAX 171characters large (from 172.Aq Pa limits.h ) . 173.Sh RETURN VALUES 174The 175.Fn kvm_open 176and 177.Fn kvm_openfiles 178functions both return a descriptor to be used 179in all subsequent kvm library calls. 180The library is fully re-entrant. 181On failure, 182.Dv NULL 183is returned, in which case 184.Fn kvm_openfiles 185writes the error message into 186.Fa errbuf . 187.Pp 188The 189.Fn kvm_close 190function returns 0 on success and \-1 on failure. 191.Sh SEE ALSO 192.Xr open 2 , 193.Xr kvm 3 , 194.Xr kvm_getargv 3 , 195.Xr kvm_getenvv 3 , 196.Xr kvm_geterr 3 , 197.Xr kvm_getprocs 3 , 198.Xr kvm_nlist 3 , 199.Xr kvm_read 3 , 200.Xr kvm_write 3 201.Sh BUGS 202There should not be two open calls. 203The ill-defined error semantics of the Sun library and the desire to have 204a backward-compatible library for BSD left little choice. 205