1.\" $OpenBSD: kvm_open.3,v 1.6 2000/03/04 15:29:56 aaron 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. All advertising materials mentioning features or use of this software 20.\" must display the following acknowledgement: 21.\" This product includes software developed by the University of 22.\" California, Berkeley and its contributors. 23.\" 4. Neither the name of the University nor the names of its contributors 24.\" may be used to endorse or promote products derived from this software 25.\" without specific prior written permission. 26.\" 27.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37.\" SUCH DAMAGE. 38.\" 39.\" @(#)kvm_open.3 8.3 (Berkeley) 4/19/94 40.\" 41.Dd April 19, 1994 42.Dt KVM_OPEN 3 43.Os 44.Sh NAME 45.Nm kvm_open , 46.Nm kvm_openfiles , 47.Nm kvm_close 48.Nd initialize kernel virtual memory access 49.Sh SYNOPSIS 50.Fd #include <fcntl.h> 51.Fd #include <kvm.h> 52.Ft kvm_t * 53.Fn kvm_open "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "const char *errstr" 54.Ft kvm_t * 55.Fn kvm_openfiles "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "char *errbuf" 56.Ft int 57.Fn kvm_close "kvm_t *kd" 58.Sh DESCRIPTION 59The functions 60.Fn kvm_open 61and 62.Fn kvm_openfiles 63return a descriptor used to access kernel virtual memory 64via the 65.Xr kvm 3 66library routines. 67Both active kernels and crash dumps are accessible through this interface. 68.Pp 69.Fa execfile 70is the executable image of the kernel being examined. 71This file must contain a symbol table. 72If this argument is 73.Dv NULL , 74the currently running system is assumed, 75which is indicated by 76.Dv _PATH_KSYMS , 77if it exists, otherwise 78.Dv _PATH_UNIX 79is used. 80Both are defined in 81.Aq Pa paths.h . 82.Pp 83.Fa corefile 84is the kernel memory device file. 85It can be either 86.Pa /dev/mem 87or a crash dump core generated by 88.Xr savecore 8 . 89If 90.Fa corefile 91is 92.Dv NULL , 93the default indicated by 94.Dv _PATH_MEM 95from 96.Aq Pa paths.h 97is used. 98.Pp 99.Fa swapfile 100should indicate the swap device. 101If 102.Dv NULL , 103.Dv _PATH_DRUM 104from 105.Aq Pa paths.h 106is used. 107.Pp 108The 109.Fa flags 110argument indicates read/write access as in 111.Xr open 2 112and applies only to the core file. 113Only 114.Dv O_RDONLY , 115.Dv O_WRONLY , 116and 117.Dv O_RDWR 118are permitted. 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 BUGS 192There should not be two open calls. 193The ill-defined error semantics of the Sun library and the desire to have 194a backward-compatible library for BSD left little choice. 195.Sh SEE ALSO 196.Xr open 2 , 197.Xr kvm 3 , 198.Xr kvm_getargv 3 , 199.Xr kvm_getenvv 3 , 200.Xr kvm_geterr 3 , 201.Xr kvm_getprocs 3 , 202.Xr kvm_nlist 3 , 203.Xr kvm_read 3 , 204.Xr kvm_write 3 205