xref: /netbsd-src/lib/libkvm/kvm_open.3 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: kvm_open.3,v 1.14 2003/08/07 16:44:37 agc 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. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)kvm_open.3	8.3 (Berkeley) 4/19/94
35.\"
36.Dd April 19, 1994
37.Dt KVM_OPEN 3
38.Os
39.Sh NAME
40.Nm kvm_open ,
41.Nm kvm_openfiles ,
42.Nm kvm_close
43.Nd initialize kernel virtual memory access
44.Sh LIBRARY
45.Lb libkvm
46.Sh SYNOPSIS
47.In fcntl.h
48.In kvm.h
49.br
50.Ft kvm_t *
51.Fn kvm_open "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "const char *errstr"
52.Ft kvm_t *
53.Fn kvm_openfiles "const char *execfile" "const char *corefile" "char *swapfile" "int flags" "char *errbuf"
54.Ft int
55.Fn kvm_close "kvm_t *kd"
56.Sh DESCRIPTION
57The functions
58.Fn kvm_open
59and
60.Fn kvm_openfiles
61return a descriptor used to access kernel virtual memory
62via the
63.Xr kvm 3
64library routines.
65Both active kernels and crash dumps are accessible
66through this interface.
67.Pp
68.Fa execfile
69is the executable image of the kernel being examined.
70This file must contain a symbol table.
71If this argument is
72.Dv NULL ,
73the currently running system is assumed,
74which is indicated by
75.Dv _PATH_UNIX
76in
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.
109The only permitted flags from
110.Xr open 2
111are
112.Dv O_RDONLY ,
113.Dv O_WRONLY ,
114and
115.Dv O_RDWR .
116.Pp
117As a special case, a
118.Fa flags
119argument of
120.Dv KVM_NO_FILES
121will initialize the
122.Xr kvm 3
123library for use on active kernels only using
124.Xr sysctl 3
125for retrieving kernel data and ignores the
126.Fa execfile ,
127.Fa corefile
128and
129.Fa swapfile
130arguments.
131Only a small subset of the
132.Xr kvm 3
133library functions are available using this method.
134These are 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.
148Here, the
149.Fa errstr
150argument indicates how errors should be handled.
151If it is
152.Dv NULL ,
153no errors are reported and the application cannot know the
154specific nature of the failed kvm call.
155If it is not
156.Dv NULL ,
157errors are printed to stderr with
158.Fa errstr
159prepended to the message, as in
160.Xr perror 3 .
161Normally, the name of the program is used here.
162The string is assumed to persist at least until the corresponding
163.Fn kvm_close
164call.
165.Pp
166The
167.Fn kvm_openfiles
168function provides
169.Bx
170style error reporting.
171Here, error messages are not printed out by the library.
172Instead, the application obtains the error message
173corresponding to the most recent kvm library call using
174.Fn kvm_geterr
175(see
176.Xr kvm_geterr 3 ) .
177The results are undefined if the most recent kvm call did not produce
178an error.
179Since
180.Fn kvm_geterr
181requires a kvm descriptor, but the open routines return
182.Dv NULL
183on failure,
184.Fn kvm_geterr
185cannot be used to get the error message if open fails.
186Thus,
187.Fn kvm_openfiles
188will place any error message in the
189.Fa errbuf
190argument.
191This buffer should be _POSIX2_LINE_MAX characters large (from
192.Aq Pa limits.h ) .
193.Sh RETURN VALUES
194The
195.Fn kvm_open
196and
197.Fn kvm_openfiles
198functions both return a descriptor to be used
199in all subsequent kvm library calls.
200The library is fully re-entrant.
201On failure,
202.Dv NULL
203is returned, in which case
204.Fn kvm_openfiles
205writes the error message into
206.Fa errbuf .
207.Pp
208The
209.Fn kvm_close
210function returns 0 on success and -1 on failure.
211.Sh SEE ALSO
212.Xr open 2 ,
213.Xr kvm 3 ,
214.Xr kvm_getargv 3 ,
215.Xr kvm_getenvv 3 ,
216.Xr kvm_geterr 3 ,
217.Xr kvm_getprocs 3 ,
218.Xr kvm_nlist 3 ,
219.Xr kvm_read 3 ,
220.Xr kvm_write 3
221.Sh BUGS
222There should not be two open calls.
223The ill-defined error semantics of the Sun library
224and the desire to have a backward-compatible library for
225.Bx
226left little choice.
227