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