xref: /freebsd/lib/libkvm/kvm_open.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1.\" Copyright (c) 1992, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software developed by the Computer Systems
5.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
6.\" BG 91-66 and contributed to Berkeley.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd March 20, 2017
33.Dt KVM_OPEN 3
34.Os
35.Sh NAME
36.Nm kvm_open ,
37.Nm kvm_open2 ,
38.Nm kvm_openfiles ,
39.Nm kvm_close
40.Nd initialize kernel virtual memory access
41.Sh LIBRARY
42.Lb libkvm
43.Sh SYNOPSIS
44.In fcntl.h
45.In kvm.h
46.Ft kvm_t *
47.Fn kvm_open "const char *execfile" "const char *corefile" "const char *swapfile" "int flags" "const char *errstr"
48.Ft kvm_t *
49.Fo kvm_open2
50.Fa "const char *execfile"
51.Fa "const char *corefile"
52.Fa "int flags"
53.Fa "char *errbuf"
54.Fa "int (*resolver)(const char *name, kvaddr_t *addr)"
55.Fc
56.Ft kvm_t *
57.Fn kvm_openfiles "const char *execfile" "const char *corefile" "const 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 ,
63.Fn kvm_open2 ,
64and
65.Fn kvm_openfiles
66return a descriptor used to access kernel virtual memory
67via the
68.Xr kvm 3
69library routines.
70Both active kernels and crash dumps are accessible
71through this interface.
72.Pp
73The
74.Fa execfile
75argument is the executable image of the kernel being examined.
76This file must contain a symbol table.
77If this argument is
78.Dv NULL ,
79the currently running system is assumed,
80as determined from
81.Xr getbootfile 3 .
82.Pp
83The
84.Fa corefile
85argument is the kernel memory device file.
86It can be either
87.Pa /dev/mem
88or a crash dump core generated by
89.Xr savecore 8 .
90If
91.Fa corefile
92is
93.Dv NULL ,
94the default indicated by
95.Dv _PATH_MEM
96from
97.In paths.h
98is used.
99It can also be set to a special value
100.Pa /dev/null
101by utilities like
102.Xr ps 1
103that do not directly access kernel memory.
104.Pp
105The
106.Fa swapfile
107argument is currently unused.
108.Pp
109The
110.Fa flags
111argument indicates read/write access as in
112.Xr open 2
113and applies only to the core file.
114Only
115.Dv O_RDONLY ,
116.Dv O_WRONLY ,
117and
118.Dv O_RDWR
119are permitted.
120.Pp
121The
122.Nm kvm
123library provides two different error reporting mechanisms.
124One provides backward compatibility with the SunOS kvm library, while the
125other provides an improved error reporting framework.
126The mechanism used by a descriptor is determined by the function used to
127open the descriptor.
128.Pp
129The
130.Fn kvm_open
131function is the Sun kvm compatible open call.
132Here, the
133.Fa errstr
134argument indicates how errors should be handled.
135If it is
136.Dv NULL ,
137no errors are reported and the application cannot know the
138specific nature of the failed kvm call.
139If it is not
140.Dv NULL ,
141errors are printed to
142.Dv stderr
143with
144.Fa errstr
145prepended to the message, as in
146.Xr perror 3 .
147Normally, the name of the program is used here.
148The string is assumed to persist at least until the corresponding
149.Fn kvm_close
150call.
151.Pp
152The
153.Fn kvm_open2
154and
155.Fn kvm_openfiles
156functions provide
157.Bx
158style error reporting.
159Here, error messages are not printed out by the library.
160Instead, the application obtains the error message
161corresponding to the most recent kvm library call using
162.Fn kvm_geterr
163(see
164.Xr kvm_geterr 3 ) .
165The results are undefined if the most recent kvm call did not produce
166an error.
167Since
168.Fn kvm_geterr
169requires a kvm descriptor, but the open routines return
170.Dv NULL
171on failure,
172.Fn kvm_geterr
173cannot be used to get the error message if open fails.
174Thus,
175.Fn kvm_open2
176and
177.Fn kvm_openfiles
178will place any error message in the
179.Fa errbuf
180argument.
181This buffer should be _POSIX2_LINE_MAX characters large (from
182<limits.h>).
183.Pp
184The
185.Fa resolver
186argument points to a function used by the
187.Nm kvm
188library to map symbol names to kernel virtual addresses.
189When the
190.Fa resolver
191function is called,
192.Fa name
193specifies the requested symbol name.
194If the function is able to resolve the name to an address,
195the address should be set in
196.Fa addr
197and the function should return zero.
198If the function is not able to resolve the name to an address,
199it should return a non-zero value.
200When opening a native kernel image,
201.Fa resolver
202may be set to
203.Dv NULL
204to use an internal function to resolve symbol names.
205Non-native kernel images
206.Pq such as when cross-debugging a crash dump
207require a valid
208.Fa resolver .
209.Sh RETURN VALUES
210The
211.Fn kvm_open ,
212.Fn kvm_open2 ,
213and
214.Fn kvm_openfiles
215functions return a descriptor to be used
216in all subsequent kvm library calls.
217The library is fully re-entrant.
218On failure,
219.Dv NULL
220is returned, in which case
221.Fn kvm_open2
222and
223.Fn kvm_openfiles
224write the error message into
225.Fa errbuf .
226.Pp
227.Rv -std kvm_close
228.Sh ERRORS
229The
230.Fn kvm_close
231function may fail and set the global variable
232.Va errno
233for any of the errors specified for
234.Xr close 2 .
235.Pp
236The
237.Fn kvm_close
238function may also fail and set
239.Va errno
240if:
241.Bl -tag -width Er
242.It Bq Er EINVAL
243The value passed via
244.Fa kd
245was
246.Dv NULL .
247.El
248.Sh SEE ALSO
249.Xr close 2 ,
250.Xr open 2 ,
251.Xr kvm 3 ,
252.Xr kvm_getargv 3 ,
253.Xr kvm_getenvv 3 ,
254.Xr kvm_geterr 3 ,
255.Xr kvm_getprocs 3 ,
256.Xr kvm_native 3 ,
257.Xr kvm_nlist 3 ,
258.Xr kvm_read 3 ,
259.Xr kvm_write 3 ,
260.Xr kmem 4 ,
261.Xr mem 4
262.Sh BUGS
263There should not be three open calls.
264The ill-defined error semantics
265of the Sun library and the desire to have a backward-compatible library
266for
267.Bx
268left little choice.
269.Sh HISTORY
270The
271.Fn kvm_open2
272function first appeared in
273.Fx 11.0 .
274