1.\" $OpenBSD: kvm_open.3,v 1.3 1998/08/24 05:46:27 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. 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.br 53.Ft kvm_t * 54.Fn kvm_open "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "const char *errstr" 55.Ft kvm_t * 56.Fn kvm_openfiles "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "char *errbuf" 57.Ft int 58.Fn kvm_close "kvm_t *kd" 59.Sh DESCRIPTION 60The functions 61.Fn kvm_open 62and 63.Fn kvm_openfiles 64return a descriptor used to access kernel virtual memory 65via the 66.Xr kvm 3 67library routines. Both active kernels and crash dumps are accessible 68through this interface. 69.Pp 70.Fa execfile 71is the executable image of the kernel being examined. 72This file must contain a symbol table. 73If this argument is 74.Dv NULL , 75the currently running system is assumed, 76which is indicated by 77.Dv _PATH_KSYMS , 78if it exists, otherwise 79.Dv _PATH_UNIX 80is used. 81Both are defined in <paths.h>. 82.Pp 83.Fa corefile 84is the kernel memory device file. It can be either /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 <paths.h> is used. 94.Pp 95.Fa swapfile 96should indicate the swap device. If 97.Dv NULL , 98.Dv _PATH_DRUM 99from <paths.h> is 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. 112.Pp 113There are two open routines which differ only with respect to 114the error mechanism. 115One provides backward compatibility with the SunOS kvm library, while the 116other provides an improved error reporting framework. 117.Pp 118The 119.Fn kvm_open 120function is the Sun kvm compatible open call. Here, the 121.Fa errstr 122argument indicates how errors should be handled. If it is 123.Dv NULL , 124no errors are reported and the application cannot know the 125specific nature of the failed kvm call. 126If it is not 127.Dv NULL , 128errors are printed to stderr with 129.Fa errstr 130prepended to the message, as in 131.Xr perror 3 . 132Normally, the name of the program is used here. 133The string is assumed to persist at least until the corresponding 134.Fn kvm_close 135call. 136.Pp 137The 138.Fn kvm_openfiles 139function provides BSD style error reporting. 140Here, error messages are not printed out by the library. 141Instead, the application obtains the error message 142corresponding to the most recent kvm library call using 143.Fn kvm_geterr 144(see 145.Xr kvm_geterr 3 ). 146The results are undefined if the most recent kvm call did not produce 147an error. 148Since 149.Fn kvm_geterr 150requires a kvm descriptor, but the open routines return 151.Dv NULL 152on failure, 153.Fn kvm_geterr 154cannot be used to get the error message if open fails. 155Thus, 156.Fn kvm_openfiles 157will place any error message in the 158.Fa errbuf 159argument. This buffer should be _POSIX2_LINE_MAX characters large (from 160<limits.h>). 161.Sh RETURN VALUES 162The 163.Fn kvm_open 164and 165.Fn kvm_openfiles 166functions both return a descriptor to be used 167in all subsequent kvm library calls. 168The library is fully re-entrant. 169On failure, 170.Dv NULL 171is returned, in which case 172.Fn kvm_openfiles 173writes the error message into 174.Fa errbuf . 175.Pp 176The 177.Fn kvm_close 178function returns 0 on success and -1 on failure. 179.Sh BUGS 180There should not be two open calls. The ill-defined error semantics 181of the Sun library and the desire to have a backward-compatible library 182for BSD left little choice. 183.Sh SEE ALSO 184.Xr open 2 , 185.Xr kvm 3 , 186.Xr kvm_getargv 3 , 187.Xr kvm_getenvv 3 , 188.Xr kvm_geterr 3 , 189.Xr kvm_getprocs 3 , 190.Xr kvm_nlist 3 , 191.Xr kvm_read 3 , 192.Xr kvm_write 3 193