kvm.c revision 1.70.2.2 1 1.70.2.2 nathanw /* $NetBSD: kvm.c,v 1.70.2.2 2002/04/23 20:10:19 nathanw Exp $ */
2 1.70.2.2 nathanw
3 1.70.2.2 nathanw /*-
4 1.70.2.2 nathanw * Copyright (c) 1989, 1992, 1993
5 1.70.2.2 nathanw * The Regents of the University of California. All rights reserved.
6 1.70.2.2 nathanw *
7 1.70.2.2 nathanw * This code is derived from software developed by the Computer Systems
8 1.70.2.2 nathanw * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 1.70.2.2 nathanw * BG 91-66 and contributed to Berkeley.
10 1.70.2.2 nathanw *
11 1.70.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.70.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.70.2.2 nathanw * are met:
14 1.70.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.70.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.70.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.70.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.70.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.70.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.70.2.2 nathanw * must display the following acknowledgement:
21 1.70.2.2 nathanw * This product includes software developed by the University of
22 1.70.2.2 nathanw * California, Berkeley and its contributors.
23 1.70.2.2 nathanw * 4. Neither the name of the University nor the names of its contributors
24 1.70.2.2 nathanw * may be used to endorse or promote products derived from this software
25 1.70.2.2 nathanw * without specific prior written permission.
26 1.70.2.2 nathanw *
27 1.70.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.70.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.70.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.70.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.70.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.70.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.70.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.70.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.70.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.70.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.70.2.2 nathanw * SUCH DAMAGE.
38 1.70.2.2 nathanw */
39 1.70.2.2 nathanw
40 1.70.2.2 nathanw #include <sys/cdefs.h>
41 1.70.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
42 1.70.2.2 nathanw #if 0
43 1.70.2.2 nathanw static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
44 1.70.2.2 nathanw #else
45 1.70.2.2 nathanw __RCSID("$NetBSD: kvm.c,v 1.70.2.2 2002/04/23 20:10:19 nathanw Exp $");
46 1.70.2.2 nathanw #endif
47 1.70.2.2 nathanw #endif /* LIBC_SCCS and not lint */
48 1.70.2.2 nathanw
49 1.70.2.2 nathanw #include <sys/param.h>
50 1.70.2.2 nathanw #include <sys/user.h>
51 1.70.2.2 nathanw #include <sys/lwp.h>
52 1.70.2.2 nathanw #include <sys/proc.h>
53 1.70.2.2 nathanw #include <sys/ioctl.h>
54 1.70.2.2 nathanw #include <sys/stat.h>
55 1.70.2.2 nathanw #include <sys/sysctl.h>
56 1.70.2.2 nathanw
57 1.70.2.2 nathanw #include <sys/core.h>
58 1.70.2.2 nathanw #include <sys/exec_aout.h>
59 1.70.2.2 nathanw #include <sys/kcore.h>
60 1.70.2.2 nathanw
61 1.70.2.2 nathanw #include <uvm/uvm_extern.h>
62 1.70.2.2 nathanw
63 1.70.2.2 nathanw #include <ctype.h>
64 1.70.2.2 nathanw #include <db.h>
65 1.70.2.2 nathanw #include <fcntl.h>
66 1.70.2.2 nathanw #include <limits.h>
67 1.70.2.2 nathanw #include <nlist.h>
68 1.70.2.2 nathanw #include <paths.h>
69 1.70.2.2 nathanw #include <stdio.h>
70 1.70.2.2 nathanw #include <stdlib.h>
71 1.70.2.2 nathanw #include <string.h>
72 1.70.2.2 nathanw #include <unistd.h>
73 1.70.2.2 nathanw #include <kvm.h>
74 1.70.2.2 nathanw
75 1.70.2.2 nathanw #include "kvm_private.h"
76 1.70.2.2 nathanw
77 1.70.2.2 nathanw static int kvm_dbopen __P((kvm_t *));
78 1.70.2.2 nathanw static int _kvm_get_header __P((kvm_t *));
79 1.70.2.2 nathanw static kvm_t *_kvm_open __P((kvm_t *, const char *, const char *,
80 1.70.2.2 nathanw const char *, int, char *));
81 1.70.2.2 nathanw static int clear_gap __P((kvm_t *, FILE *, int));
82 1.70.2.2 nathanw static off_t Lseek __P((kvm_t *, int, off_t, int));
83 1.70.2.2 nathanw static ssize_t Pread __P((kvm_t *, int, void *, size_t, off_t));
84 1.70.2.2 nathanw
85 1.70.2.2 nathanw char *
86 1.70.2.2 nathanw kvm_geterr(kd)
87 1.70.2.2 nathanw kvm_t *kd;
88 1.70.2.2 nathanw {
89 1.70.2.2 nathanw return (kd->errbuf);
90 1.70.2.2 nathanw }
91 1.70.2.2 nathanw
92 1.70.2.2 nathanw #if __STDC__
93 1.70.2.2 nathanw #include <stdarg.h>
94 1.70.2.2 nathanw #else
95 1.70.2.2 nathanw #include <varargs.h>
96 1.70.2.2 nathanw #endif
97 1.70.2.2 nathanw
98 1.70.2.2 nathanw /*
99 1.70.2.2 nathanw * Report an error using printf style arguments. "program" is kd->program
100 1.70.2.2 nathanw * on hard errors, and 0 on soft errors, so that under sun error emulation,
101 1.70.2.2 nathanw * only hard errors are printed out (otherwise, programs like gdb will
102 1.70.2.2 nathanw * generate tons of error messages when trying to access bogus pointers).
103 1.70.2.2 nathanw */
104 1.70.2.2 nathanw void
105 1.70.2.2 nathanw #if __STDC__
106 1.70.2.2 nathanw _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
107 1.70.2.2 nathanw #else
108 1.70.2.2 nathanw _kvm_err(kd, program, fmt, va_alist)
109 1.70.2.2 nathanw kvm_t *kd;
110 1.70.2.2 nathanw char *program, *fmt;
111 1.70.2.2 nathanw va_dcl
112 1.70.2.2 nathanw #endif
113 1.70.2.2 nathanw {
114 1.70.2.2 nathanw va_list ap;
115 1.70.2.2 nathanw
116 1.70.2.2 nathanw #ifdef __STDC__
117 1.70.2.2 nathanw va_start(ap, fmt);
118 1.70.2.2 nathanw #else
119 1.70.2.2 nathanw va_start(ap);
120 1.70.2.2 nathanw #endif
121 1.70.2.2 nathanw if (program != NULL) {
122 1.70.2.2 nathanw (void)fprintf(stderr, "%s: ", program);
123 1.70.2.2 nathanw (void)vfprintf(stderr, fmt, ap);
124 1.70.2.2 nathanw (void)fputc('\n', stderr);
125 1.70.2.2 nathanw } else
126 1.70.2.2 nathanw (void)vsnprintf(kd->errbuf,
127 1.70.2.2 nathanw sizeof(kd->errbuf), fmt, ap);
128 1.70.2.2 nathanw
129 1.70.2.2 nathanw va_end(ap);
130 1.70.2.2 nathanw }
131 1.70.2.2 nathanw
132 1.70.2.2 nathanw void
133 1.70.2.2 nathanw #if __STDC__
134 1.70.2.2 nathanw _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
135 1.70.2.2 nathanw #else
136 1.70.2.2 nathanw _kvm_syserr(kd, program, fmt, va_alist)
137 1.70.2.2 nathanw kvm_t *kd;
138 1.70.2.2 nathanw char *program, *fmt;
139 1.70.2.2 nathanw va_dcl
140 1.70.2.2 nathanw #endif
141 1.70.2.2 nathanw {
142 1.70.2.2 nathanw va_list ap;
143 1.70.2.2 nathanw size_t n;
144 1.70.2.2 nathanw
145 1.70.2.2 nathanw #if __STDC__
146 1.70.2.2 nathanw va_start(ap, fmt);
147 1.70.2.2 nathanw #else
148 1.70.2.2 nathanw va_start(ap);
149 1.70.2.2 nathanw #endif
150 1.70.2.2 nathanw if (program != NULL) {
151 1.70.2.2 nathanw (void)fprintf(stderr, "%s: ", program);
152 1.70.2.2 nathanw (void)vfprintf(stderr, fmt, ap);
153 1.70.2.2 nathanw (void)fprintf(stderr, ": %s\n", strerror(errno));
154 1.70.2.2 nathanw } else {
155 1.70.2.2 nathanw char *cp = kd->errbuf;
156 1.70.2.2 nathanw
157 1.70.2.2 nathanw (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
158 1.70.2.2 nathanw n = strlen(cp);
159 1.70.2.2 nathanw (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
160 1.70.2.2 nathanw strerror(errno));
161 1.70.2.2 nathanw }
162 1.70.2.2 nathanw va_end(ap);
163 1.70.2.2 nathanw }
164 1.70.2.2 nathanw
165 1.70.2.2 nathanw void *
166 1.70.2.2 nathanw _kvm_malloc(kd, n)
167 1.70.2.2 nathanw kvm_t *kd;
168 1.70.2.2 nathanw size_t n;
169 1.70.2.2 nathanw {
170 1.70.2.2 nathanw void *p;
171 1.70.2.2 nathanw
172 1.70.2.2 nathanw if ((p = malloc(n)) == NULL)
173 1.70.2.2 nathanw _kvm_err(kd, kd->program, "%s", strerror(errno));
174 1.70.2.2 nathanw return (p);
175 1.70.2.2 nathanw }
176 1.70.2.2 nathanw
177 1.70.2.2 nathanw /*
178 1.70.2.2 nathanw * Wrapper around the lseek(2) system call; calls _kvm_syserr() for us
179 1.70.2.2 nathanw * in the event of emergency.
180 1.70.2.2 nathanw */
181 1.70.2.2 nathanw static off_t
182 1.70.2.2 nathanw Lseek(kd, fd, offset, whence)
183 1.70.2.2 nathanw kvm_t *kd;
184 1.70.2.2 nathanw int fd;
185 1.70.2.2 nathanw off_t offset;
186 1.70.2.2 nathanw int whence;
187 1.70.2.2 nathanw {
188 1.70.2.2 nathanw off_t off;
189 1.70.2.2 nathanw
190 1.70.2.2 nathanw errno = 0;
191 1.70.2.2 nathanw
192 1.70.2.2 nathanw if ((off = lseek(fd, offset, whence)) == -1 && errno != 0) {
193 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "Lseek");
194 1.70.2.2 nathanw return ((off_t)-1);
195 1.70.2.2 nathanw }
196 1.70.2.2 nathanw return (off);
197 1.70.2.2 nathanw }
198 1.70.2.2 nathanw
199 1.70.2.2 nathanw /*
200 1.70.2.2 nathanw * Wrapper around the pread(2) system call; calls _kvm_syserr() for us
201 1.70.2.2 nathanw * in the event of emergency.
202 1.70.2.2 nathanw */
203 1.70.2.2 nathanw static ssize_t
204 1.70.2.2 nathanw Pread(kd, fd, buf, nbytes, offset)
205 1.70.2.2 nathanw kvm_t *kd;
206 1.70.2.2 nathanw int fd;
207 1.70.2.2 nathanw void *buf;
208 1.70.2.2 nathanw size_t nbytes;
209 1.70.2.2 nathanw off_t offset;
210 1.70.2.2 nathanw {
211 1.70.2.2 nathanw ssize_t rv;
212 1.70.2.2 nathanw
213 1.70.2.2 nathanw errno = 0;
214 1.70.2.2 nathanw
215 1.70.2.2 nathanw if ((rv = pread(fd, buf, nbytes, offset)) != nbytes &&
216 1.70.2.2 nathanw errno != 0)
217 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "Pread");
218 1.70.2.2 nathanw return (rv);
219 1.70.2.2 nathanw }
220 1.70.2.2 nathanw
221 1.70.2.2 nathanw static kvm_t *
222 1.70.2.2 nathanw _kvm_open(kd, uf, mf, sf, flag, errout)
223 1.70.2.2 nathanw kvm_t *kd;
224 1.70.2.2 nathanw const char *uf;
225 1.70.2.2 nathanw const char *mf;
226 1.70.2.2 nathanw const char *sf;
227 1.70.2.2 nathanw int flag;
228 1.70.2.2 nathanw char *errout;
229 1.70.2.2 nathanw {
230 1.70.2.2 nathanw struct stat st;
231 1.70.2.2 nathanw int ufgiven;
232 1.70.2.2 nathanw
233 1.70.2.2 nathanw kd->db = 0;
234 1.70.2.2 nathanw kd->pmfd = -1;
235 1.70.2.2 nathanw kd->vmfd = -1;
236 1.70.2.2 nathanw kd->swfd = -1;
237 1.70.2.2 nathanw kd->nlfd = -1;
238 1.70.2.2 nathanw kd->alive = KVM_ALIVE_DEAD;
239 1.70.2.2 nathanw kd->procbase = 0;
240 1.70.2.2 nathanw kd->procbase2 = 0;
241 1.70.2.2 nathanw kd->lwpbase = 0;
242 1.70.2.2 nathanw kd->nbpg = getpagesize();
243 1.70.2.2 nathanw kd->swapspc = 0;
244 1.70.2.2 nathanw kd->argspc = 0;
245 1.70.2.2 nathanw kd->arglen = 0;
246 1.70.2.2 nathanw kd->argbuf = 0;
247 1.70.2.2 nathanw kd->argv = 0;
248 1.70.2.2 nathanw kd->vmst = 0;
249 1.70.2.2 nathanw kd->vm_page_buckets = 0;
250 1.70.2.2 nathanw kd->kcore_hdr = 0;
251 1.70.2.2 nathanw kd->cpu_dsize = 0;
252 1.70.2.2 nathanw kd->cpu_data = 0;
253 1.70.2.2 nathanw kd->dump_off = 0;
254 1.70.2.2 nathanw
255 1.70.2.2 nathanw if (flag & KVM_NO_FILES) {
256 1.70.2.2 nathanw kd->alive = KVM_ALIVE_SYSCTL;
257 1.70.2.2 nathanw return(kd);
258 1.70.2.2 nathanw }
259 1.70.2.2 nathanw
260 1.70.2.2 nathanw /*
261 1.70.2.2 nathanw * Call the MD open hook. This sets:
262 1.70.2.2 nathanw * usrstack, min_uva, max_uva
263 1.70.2.2 nathanw */
264 1.70.2.2 nathanw if (_kvm_mdopen(kd)) {
265 1.70.2.2 nathanw _kvm_err(kd, kd->program, "md init failed");
266 1.70.2.2 nathanw goto failed;
267 1.70.2.2 nathanw }
268 1.70.2.2 nathanw
269 1.70.2.2 nathanw ufgiven = (uf != NULL);
270 1.70.2.2 nathanw if (!ufgiven)
271 1.70.2.2 nathanw uf = _PATH_UNIX;
272 1.70.2.2 nathanw else if (strlen(uf) >= MAXPATHLEN) {
273 1.70.2.2 nathanw _kvm_err(kd, kd->program, "exec file name too long");
274 1.70.2.2 nathanw goto failed;
275 1.70.2.2 nathanw }
276 1.70.2.2 nathanw if (flag & ~O_RDWR) {
277 1.70.2.2 nathanw _kvm_err(kd, kd->program, "bad flags arg");
278 1.70.2.2 nathanw goto failed;
279 1.70.2.2 nathanw }
280 1.70.2.2 nathanw if (mf == 0)
281 1.70.2.2 nathanw mf = _PATH_MEM;
282 1.70.2.2 nathanw if (sf == 0)
283 1.70.2.2 nathanw sf = _PATH_DRUM;
284 1.70.2.2 nathanw
285 1.70.2.2 nathanw if ((kd->pmfd = open(mf, flag, 0)) < 0) {
286 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", mf);
287 1.70.2.2 nathanw goto failed;
288 1.70.2.2 nathanw }
289 1.70.2.2 nathanw if (fstat(kd->pmfd, &st) < 0) {
290 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", mf);
291 1.70.2.2 nathanw goto failed;
292 1.70.2.2 nathanw }
293 1.70.2.2 nathanw if (S_ISCHR(st.st_mode)) {
294 1.70.2.2 nathanw /*
295 1.70.2.2 nathanw * If this is a character special device, then check that
296 1.70.2.2 nathanw * it's /dev/mem. If so, open kmem too. (Maybe we should
297 1.70.2.2 nathanw * make it work for either /dev/mem or /dev/kmem -- in either
298 1.70.2.2 nathanw * case you're working with a live kernel.)
299 1.70.2.2 nathanw */
300 1.70.2.2 nathanw if (strcmp(mf, _PATH_MEM) != 0) { /* XXX */
301 1.70.2.2 nathanw _kvm_err(kd, kd->program,
302 1.70.2.2 nathanw "%s: not physical memory device", mf);
303 1.70.2.2 nathanw goto failed;
304 1.70.2.2 nathanw }
305 1.70.2.2 nathanw if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
306 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
307 1.70.2.2 nathanw goto failed;
308 1.70.2.2 nathanw }
309 1.70.2.2 nathanw kd->alive = KVM_ALIVE_FILES;
310 1.70.2.2 nathanw if ((kd->swfd = open(sf, flag, 0)) < 0) {
311 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", sf);
312 1.70.2.2 nathanw goto failed;
313 1.70.2.2 nathanw }
314 1.70.2.2 nathanw /*
315 1.70.2.2 nathanw * Open kvm nlist database. We only try to use
316 1.70.2.2 nathanw * the pre-built database if the namelist file name
317 1.70.2.2 nathanw * pointer is NULL. If the database cannot or should
318 1.70.2.2 nathanw * not be opened, open the namelist argument so we
319 1.70.2.2 nathanw * revert to slow nlist() calls.
320 1.70.2.2 nathanw */
321 1.70.2.2 nathanw if ((ufgiven || kvm_dbopen(kd) < 0) &&
322 1.70.2.2 nathanw (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
323 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", uf);
324 1.70.2.2 nathanw goto failed;
325 1.70.2.2 nathanw }
326 1.70.2.2 nathanw } else {
327 1.70.2.2 nathanw /*
328 1.70.2.2 nathanw * This is a crash dump.
329 1.70.2.2 nathanw * Initialize the virtual address translation machinery,
330 1.70.2.2 nathanw * but first setup the namelist fd.
331 1.70.2.2 nathanw */
332 1.70.2.2 nathanw if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
333 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "%s", uf);
334 1.70.2.2 nathanw goto failed;
335 1.70.2.2 nathanw }
336 1.70.2.2 nathanw
337 1.70.2.2 nathanw /*
338 1.70.2.2 nathanw * If there is no valid core header, fail silently here.
339 1.70.2.2 nathanw * The address translations however will fail without
340 1.70.2.2 nathanw * header. Things can be made to run by calling
341 1.70.2.2 nathanw * kvm_dump_mkheader() before doing any translation.
342 1.70.2.2 nathanw */
343 1.70.2.2 nathanw if (_kvm_get_header(kd) == 0) {
344 1.70.2.2 nathanw if (_kvm_initvtop(kd) < 0)
345 1.70.2.2 nathanw goto failed;
346 1.70.2.2 nathanw }
347 1.70.2.2 nathanw }
348 1.70.2.2 nathanw return (kd);
349 1.70.2.2 nathanw failed:
350 1.70.2.2 nathanw /*
351 1.70.2.2 nathanw * Copy out the error if doing sane error semantics.
352 1.70.2.2 nathanw */
353 1.70.2.2 nathanw if (errout != 0)
354 1.70.2.2 nathanw (void)strncpy(errout, kd->errbuf, _POSIX2_LINE_MAX - 1);
355 1.70.2.2 nathanw (void)kvm_close(kd);
356 1.70.2.2 nathanw return (0);
357 1.70.2.2 nathanw }
358 1.70.2.2 nathanw
359 1.70.2.2 nathanw /*
360 1.70.2.2 nathanw * The kernel dump file (from savecore) contains:
361 1.70.2.2 nathanw * kcore_hdr_t kcore_hdr;
362 1.70.2.2 nathanw * kcore_seg_t cpu_hdr;
363 1.70.2.2 nathanw * (opaque) cpu_data; (size is cpu_hdr.c_size)
364 1.70.2.2 nathanw * kcore_seg_t mem_hdr;
365 1.70.2.2 nathanw * (memory) mem_data; (size is mem_hdr.c_size)
366 1.70.2.2 nathanw *
367 1.70.2.2 nathanw * Note: khdr is padded to khdr.c_hdrsize;
368 1.70.2.2 nathanw * cpu_hdr and mem_hdr are padded to khdr.c_seghdrsize
369 1.70.2.2 nathanw */
370 1.70.2.2 nathanw static int
371 1.70.2.2 nathanw _kvm_get_header(kd)
372 1.70.2.2 nathanw kvm_t *kd;
373 1.70.2.2 nathanw {
374 1.70.2.2 nathanw kcore_hdr_t kcore_hdr;
375 1.70.2.2 nathanw kcore_seg_t cpu_hdr;
376 1.70.2.2 nathanw kcore_seg_t mem_hdr;
377 1.70.2.2 nathanw size_t offset;
378 1.70.2.2 nathanw ssize_t sz;
379 1.70.2.2 nathanw
380 1.70.2.2 nathanw /*
381 1.70.2.2 nathanw * Read the kcore_hdr_t
382 1.70.2.2 nathanw */
383 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, &kcore_hdr, sizeof(kcore_hdr), (off_t)0);
384 1.70.2.2 nathanw if (sz != sizeof(kcore_hdr))
385 1.70.2.2 nathanw return (-1);
386 1.70.2.2 nathanw
387 1.70.2.2 nathanw /*
388 1.70.2.2 nathanw * Currently, we only support dump-files made by the current
389 1.70.2.2 nathanw * architecture...
390 1.70.2.2 nathanw */
391 1.70.2.2 nathanw if ((CORE_GETMAGIC(kcore_hdr) != KCORE_MAGIC) ||
392 1.70.2.2 nathanw (CORE_GETMID(kcore_hdr) != MID_MACHINE))
393 1.70.2.2 nathanw return (-1);
394 1.70.2.2 nathanw
395 1.70.2.2 nathanw /*
396 1.70.2.2 nathanw * Currently, we only support exactly 2 segments: cpu-segment
397 1.70.2.2 nathanw * and data-segment in exactly that order.
398 1.70.2.2 nathanw */
399 1.70.2.2 nathanw if (kcore_hdr.c_nseg != 2)
400 1.70.2.2 nathanw return (-1);
401 1.70.2.2 nathanw
402 1.70.2.2 nathanw /*
403 1.70.2.2 nathanw * Save away the kcore_hdr. All errors after this
404 1.70.2.2 nathanw * should do a to "goto fail" to deallocate things.
405 1.70.2.2 nathanw */
406 1.70.2.2 nathanw kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr));
407 1.70.2.2 nathanw memcpy(kd->kcore_hdr, &kcore_hdr, sizeof(kcore_hdr));
408 1.70.2.2 nathanw offset = kcore_hdr.c_hdrsize;
409 1.70.2.2 nathanw
410 1.70.2.2 nathanw /*
411 1.70.2.2 nathanw * Read the CPU segment header
412 1.70.2.2 nathanw */
413 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), (off_t)offset);
414 1.70.2.2 nathanw if (sz != sizeof(cpu_hdr))
415 1.70.2.2 nathanw goto fail;
416 1.70.2.2 nathanw if ((CORE_GETMAGIC(cpu_hdr) != KCORESEG_MAGIC) ||
417 1.70.2.2 nathanw (CORE_GETFLAG(cpu_hdr) != CORE_CPU))
418 1.70.2.2 nathanw goto fail;
419 1.70.2.2 nathanw offset += kcore_hdr.c_seghdrsize;
420 1.70.2.2 nathanw
421 1.70.2.2 nathanw /*
422 1.70.2.2 nathanw * Read the CPU segment DATA.
423 1.70.2.2 nathanw */
424 1.70.2.2 nathanw kd->cpu_dsize = cpu_hdr.c_size;
425 1.70.2.2 nathanw kd->cpu_data = _kvm_malloc(kd, cpu_hdr.c_size);
426 1.70.2.2 nathanw if (kd->cpu_data == NULL)
427 1.70.2.2 nathanw goto fail;
428 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size, (off_t)offset);
429 1.70.2.2 nathanw if (sz != cpu_hdr.c_size)
430 1.70.2.2 nathanw goto fail;
431 1.70.2.2 nathanw offset += cpu_hdr.c_size;
432 1.70.2.2 nathanw
433 1.70.2.2 nathanw /*
434 1.70.2.2 nathanw * Read the next segment header: data segment
435 1.70.2.2 nathanw */
436 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, &mem_hdr, sizeof(mem_hdr), (off_t)offset);
437 1.70.2.2 nathanw if (sz != sizeof(mem_hdr))
438 1.70.2.2 nathanw goto fail;
439 1.70.2.2 nathanw offset += kcore_hdr.c_seghdrsize;
440 1.70.2.2 nathanw
441 1.70.2.2 nathanw if ((CORE_GETMAGIC(mem_hdr) != KCORESEG_MAGIC) ||
442 1.70.2.2 nathanw (CORE_GETFLAG(mem_hdr) != CORE_DATA))
443 1.70.2.2 nathanw goto fail;
444 1.70.2.2 nathanw
445 1.70.2.2 nathanw kd->dump_off = offset;
446 1.70.2.2 nathanw return (0);
447 1.70.2.2 nathanw
448 1.70.2.2 nathanw fail:
449 1.70.2.2 nathanw if (kd->kcore_hdr != NULL) {
450 1.70.2.2 nathanw free(kd->kcore_hdr);
451 1.70.2.2 nathanw kd->kcore_hdr = NULL;
452 1.70.2.2 nathanw }
453 1.70.2.2 nathanw if (kd->cpu_data != NULL) {
454 1.70.2.2 nathanw free(kd->cpu_data);
455 1.70.2.2 nathanw kd->cpu_data = NULL;
456 1.70.2.2 nathanw kd->cpu_dsize = 0;
457 1.70.2.2 nathanw }
458 1.70.2.2 nathanw return (-1);
459 1.70.2.2 nathanw }
460 1.70.2.2 nathanw
461 1.70.2.2 nathanw /*
462 1.70.2.2 nathanw * The format while on the dump device is: (new format)
463 1.70.2.2 nathanw * kcore_seg_t cpu_hdr;
464 1.70.2.2 nathanw * (opaque) cpu_data; (size is cpu_hdr.c_size)
465 1.70.2.2 nathanw * kcore_seg_t mem_hdr;
466 1.70.2.2 nathanw * (memory) mem_data; (size is mem_hdr.c_size)
467 1.70.2.2 nathanw */
468 1.70.2.2 nathanw int
469 1.70.2.2 nathanw kvm_dump_mkheader(kd, dump_off)
470 1.70.2.2 nathanw kvm_t *kd;
471 1.70.2.2 nathanw off_t dump_off;
472 1.70.2.2 nathanw {
473 1.70.2.2 nathanw kcore_seg_t cpu_hdr;
474 1.70.2.2 nathanw size_t hdr_size;
475 1.70.2.2 nathanw ssize_t sz;
476 1.70.2.2 nathanw
477 1.70.2.2 nathanw if (kd->kcore_hdr != NULL) {
478 1.70.2.2 nathanw _kvm_err(kd, kd->program, "already has a dump header");
479 1.70.2.2 nathanw return (-1);
480 1.70.2.2 nathanw }
481 1.70.2.2 nathanw if (ISALIVE(kd)) {
482 1.70.2.2 nathanw _kvm_err(kd, kd->program, "don't use on live kernel");
483 1.70.2.2 nathanw return (-1);
484 1.70.2.2 nathanw }
485 1.70.2.2 nathanw
486 1.70.2.2 nathanw /*
487 1.70.2.2 nathanw * Validate new format crash dump
488 1.70.2.2 nathanw */
489 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), dump_off);
490 1.70.2.2 nathanw if (sz != sizeof(cpu_hdr))
491 1.70.2.2 nathanw return (-1);
492 1.70.2.2 nathanw if ((CORE_GETMAGIC(cpu_hdr) != KCORE_MAGIC)
493 1.70.2.2 nathanw || (CORE_GETMID(cpu_hdr) != MID_MACHINE)) {
494 1.70.2.2 nathanw _kvm_err(kd, 0, "invalid magic in cpu_hdr");
495 1.70.2.2 nathanw return (0);
496 1.70.2.2 nathanw }
497 1.70.2.2 nathanw hdr_size = ALIGN(sizeof(cpu_hdr));
498 1.70.2.2 nathanw
499 1.70.2.2 nathanw /*
500 1.70.2.2 nathanw * Read the CPU segment.
501 1.70.2.2 nathanw */
502 1.70.2.2 nathanw kd->cpu_dsize = cpu_hdr.c_size;
503 1.70.2.2 nathanw kd->cpu_data = _kvm_malloc(kd, kd->cpu_dsize);
504 1.70.2.2 nathanw if (kd->cpu_data == NULL)
505 1.70.2.2 nathanw goto fail;
506 1.70.2.2 nathanw sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size,
507 1.70.2.2 nathanw dump_off + hdr_size);
508 1.70.2.2 nathanw if (sz != cpu_hdr.c_size)
509 1.70.2.2 nathanw goto fail;
510 1.70.2.2 nathanw hdr_size += kd->cpu_dsize;
511 1.70.2.2 nathanw
512 1.70.2.2 nathanw /*
513 1.70.2.2 nathanw * Leave phys mem pointer at beginning of memory data
514 1.70.2.2 nathanw */
515 1.70.2.2 nathanw kd->dump_off = dump_off + hdr_size;
516 1.70.2.2 nathanw if (Lseek(kd, kd->pmfd, kd->dump_off, SEEK_SET) == -1)
517 1.70.2.2 nathanw goto fail;
518 1.70.2.2 nathanw
519 1.70.2.2 nathanw /*
520 1.70.2.2 nathanw * Create a kcore_hdr.
521 1.70.2.2 nathanw */
522 1.70.2.2 nathanw kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr_t));
523 1.70.2.2 nathanw if (kd->kcore_hdr == NULL)
524 1.70.2.2 nathanw goto fail;
525 1.70.2.2 nathanw
526 1.70.2.2 nathanw kd->kcore_hdr->c_hdrsize = ALIGN(sizeof(kcore_hdr_t));
527 1.70.2.2 nathanw kd->kcore_hdr->c_seghdrsize = ALIGN(sizeof(kcore_seg_t));
528 1.70.2.2 nathanw kd->kcore_hdr->c_nseg = 2;
529 1.70.2.2 nathanw CORE_SETMAGIC(*(kd->kcore_hdr), KCORE_MAGIC, MID_MACHINE,0);
530 1.70.2.2 nathanw
531 1.70.2.2 nathanw /*
532 1.70.2.2 nathanw * Now that we have a valid header, enable translations.
533 1.70.2.2 nathanw */
534 1.70.2.2 nathanw if (_kvm_initvtop(kd) == 0)
535 1.70.2.2 nathanw /* Success */
536 1.70.2.2 nathanw return (hdr_size);
537 1.70.2.2 nathanw
538 1.70.2.2 nathanw fail:
539 1.70.2.2 nathanw if (kd->kcore_hdr != NULL) {
540 1.70.2.2 nathanw free(kd->kcore_hdr);
541 1.70.2.2 nathanw kd->kcore_hdr = NULL;
542 1.70.2.2 nathanw }
543 1.70.2.2 nathanw if (kd->cpu_data != NULL) {
544 1.70.2.2 nathanw free(kd->cpu_data);
545 1.70.2.2 nathanw kd->cpu_data = NULL;
546 1.70.2.2 nathanw kd->cpu_dsize = 0;
547 1.70.2.2 nathanw }
548 1.70.2.2 nathanw return (-1);
549 1.70.2.2 nathanw }
550 1.70.2.2 nathanw
551 1.70.2.2 nathanw static int
552 1.70.2.2 nathanw clear_gap(kd, fp, size)
553 1.70.2.2 nathanw kvm_t *kd;
554 1.70.2.2 nathanw FILE *fp;
555 1.70.2.2 nathanw int size;
556 1.70.2.2 nathanw {
557 1.70.2.2 nathanw if (size <= 0) /* XXX - < 0 should never happen */
558 1.70.2.2 nathanw return (0);
559 1.70.2.2 nathanw while (size-- > 0) {
560 1.70.2.2 nathanw if (fputc(0, fp) == EOF) {
561 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "clear_gap");
562 1.70.2.2 nathanw return (-1);
563 1.70.2.2 nathanw }
564 1.70.2.2 nathanw }
565 1.70.2.2 nathanw return (0);
566 1.70.2.2 nathanw }
567 1.70.2.2 nathanw
568 1.70.2.2 nathanw /*
569 1.70.2.2 nathanw * Write the dump header info to 'fp'. Note that we can't use fseek(3) here
570 1.70.2.2 nathanw * because 'fp' might be a file pointer obtained by zopen().
571 1.70.2.2 nathanw */
572 1.70.2.2 nathanw int
573 1.70.2.2 nathanw kvm_dump_wrtheader(kd, fp, dumpsize)
574 1.70.2.2 nathanw kvm_t *kd;
575 1.70.2.2 nathanw FILE *fp;
576 1.70.2.2 nathanw int dumpsize;
577 1.70.2.2 nathanw {
578 1.70.2.2 nathanw kcore_seg_t seghdr;
579 1.70.2.2 nathanw long offset;
580 1.70.2.2 nathanw int gap;
581 1.70.2.2 nathanw
582 1.70.2.2 nathanw if (kd->kcore_hdr == NULL || kd->cpu_data == NULL) {
583 1.70.2.2 nathanw _kvm_err(kd, kd->program, "no valid dump header(s)");
584 1.70.2.2 nathanw return (-1);
585 1.70.2.2 nathanw }
586 1.70.2.2 nathanw
587 1.70.2.2 nathanw /*
588 1.70.2.2 nathanw * Write the generic header
589 1.70.2.2 nathanw */
590 1.70.2.2 nathanw offset = 0;
591 1.70.2.2 nathanw if (fwrite((void*)kd->kcore_hdr, sizeof(kcore_hdr_t), 1, fp) == 0) {
592 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
593 1.70.2.2 nathanw return (-1);
594 1.70.2.2 nathanw }
595 1.70.2.2 nathanw offset += kd->kcore_hdr->c_hdrsize;
596 1.70.2.2 nathanw gap = kd->kcore_hdr->c_hdrsize - sizeof(kcore_hdr_t);
597 1.70.2.2 nathanw if (clear_gap(kd, fp, gap) == -1)
598 1.70.2.2 nathanw return (-1);
599 1.70.2.2 nathanw
600 1.70.2.2 nathanw /*
601 1.70.2.2 nathanw * Write the cpu header
602 1.70.2.2 nathanw */
603 1.70.2.2 nathanw CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_CPU);
604 1.70.2.2 nathanw seghdr.c_size = ALIGN(kd->cpu_dsize);
605 1.70.2.2 nathanw if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) == 0) {
606 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
607 1.70.2.2 nathanw return (-1);
608 1.70.2.2 nathanw }
609 1.70.2.2 nathanw offset += kd->kcore_hdr->c_seghdrsize;
610 1.70.2.2 nathanw gap = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
611 1.70.2.2 nathanw if (clear_gap(kd, fp, gap) == -1)
612 1.70.2.2 nathanw return (-1);
613 1.70.2.2 nathanw
614 1.70.2.2 nathanw if (fwrite((void*)kd->cpu_data, kd->cpu_dsize, 1, fp) == 0) {
615 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
616 1.70.2.2 nathanw return (-1);
617 1.70.2.2 nathanw }
618 1.70.2.2 nathanw offset += seghdr.c_size;
619 1.70.2.2 nathanw gap = seghdr.c_size - kd->cpu_dsize;
620 1.70.2.2 nathanw if (clear_gap(kd, fp, gap) == -1)
621 1.70.2.2 nathanw return (-1);
622 1.70.2.2 nathanw
623 1.70.2.2 nathanw /*
624 1.70.2.2 nathanw * Write the actual dump data segment header
625 1.70.2.2 nathanw */
626 1.70.2.2 nathanw CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_DATA);
627 1.70.2.2 nathanw seghdr.c_size = dumpsize;
628 1.70.2.2 nathanw if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) == 0) {
629 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
630 1.70.2.2 nathanw return (-1);
631 1.70.2.2 nathanw }
632 1.70.2.2 nathanw offset += kd->kcore_hdr->c_seghdrsize;
633 1.70.2.2 nathanw gap = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
634 1.70.2.2 nathanw if (clear_gap(kd, fp, gap) == -1)
635 1.70.2.2 nathanw return (-1);
636 1.70.2.2 nathanw
637 1.70.2.2 nathanw return (int)offset;
638 1.70.2.2 nathanw }
639 1.70.2.2 nathanw
640 1.70.2.2 nathanw kvm_t *
641 1.70.2.2 nathanw kvm_openfiles(uf, mf, sf, flag, errout)
642 1.70.2.2 nathanw const char *uf;
643 1.70.2.2 nathanw const char *mf;
644 1.70.2.2 nathanw const char *sf;
645 1.70.2.2 nathanw int flag;
646 1.70.2.2 nathanw char *errout;
647 1.70.2.2 nathanw {
648 1.70.2.2 nathanw kvm_t *kd;
649 1.70.2.2 nathanw
650 1.70.2.2 nathanw if ((kd = malloc(sizeof(*kd))) == NULL) {
651 1.70.2.2 nathanw (void)strncpy(errout, strerror(errno), _POSIX2_LINE_MAX - 1);
652 1.70.2.2 nathanw return (0);
653 1.70.2.2 nathanw }
654 1.70.2.2 nathanw kd->program = 0;
655 1.70.2.2 nathanw return (_kvm_open(kd, uf, mf, sf, flag, errout));
656 1.70.2.2 nathanw }
657 1.70.2.2 nathanw
658 1.70.2.2 nathanw kvm_t *
659 1.70.2.2 nathanw kvm_open(uf, mf, sf, flag, program)
660 1.70.2.2 nathanw const char *uf;
661 1.70.2.2 nathanw const char *mf;
662 1.70.2.2 nathanw const char *sf;
663 1.70.2.2 nathanw int flag;
664 1.70.2.2 nathanw const char *program;
665 1.70.2.2 nathanw {
666 1.70.2.2 nathanw kvm_t *kd;
667 1.70.2.2 nathanw
668 1.70.2.2 nathanw if ((kd = malloc(sizeof(*kd))) == NULL && program != NULL) {
669 1.70.2.2 nathanw (void)fprintf(stderr, "%s: %s\n", program, strerror(errno));
670 1.70.2.2 nathanw return (0);
671 1.70.2.2 nathanw }
672 1.70.2.2 nathanw kd->program = program;
673 1.70.2.2 nathanw return (_kvm_open(kd, uf, mf, sf, flag, NULL));
674 1.70.2.2 nathanw }
675 1.70.2.2 nathanw
676 1.70.2.2 nathanw int
677 1.70.2.2 nathanw kvm_close(kd)
678 1.70.2.2 nathanw kvm_t *kd;
679 1.70.2.2 nathanw {
680 1.70.2.2 nathanw int error = 0;
681 1.70.2.2 nathanw
682 1.70.2.2 nathanw if (kd->pmfd >= 0)
683 1.70.2.2 nathanw error |= close(kd->pmfd);
684 1.70.2.2 nathanw if (kd->vmfd >= 0)
685 1.70.2.2 nathanw error |= close(kd->vmfd);
686 1.70.2.2 nathanw if (kd->nlfd >= 0)
687 1.70.2.2 nathanw error |= close(kd->nlfd);
688 1.70.2.2 nathanw if (kd->swfd >= 0)
689 1.70.2.2 nathanw error |= close(kd->swfd);
690 1.70.2.2 nathanw if (kd->db != 0)
691 1.70.2.2 nathanw error |= (kd->db->close)(kd->db);
692 1.70.2.2 nathanw if (kd->vmst)
693 1.70.2.2 nathanw _kvm_freevtop(kd);
694 1.70.2.2 nathanw kd->cpu_dsize = 0;
695 1.70.2.2 nathanw if (kd->cpu_data != NULL)
696 1.70.2.2 nathanw free((void *)kd->cpu_data);
697 1.70.2.2 nathanw if (kd->kcore_hdr != NULL)
698 1.70.2.2 nathanw free((void *)kd->kcore_hdr);
699 1.70.2.2 nathanw if (kd->procbase != 0)
700 1.70.2.2 nathanw free((void *)kd->procbase);
701 1.70.2.2 nathanw if (kd->procbase2 != 0)
702 1.70.2.2 nathanw free((void *)kd->procbase2);
703 1.70.2.2 nathanw if (kd->lwpbase != 0)
704 1.70.2.2 nathanw free((void *)kd->lwpbase);
705 1.70.2.2 nathanw if (kd->swapspc != 0)
706 1.70.2.2 nathanw free((void *)kd->swapspc);
707 1.70.2.2 nathanw if (kd->argspc != 0)
708 1.70.2.2 nathanw free((void *)kd->argspc);
709 1.70.2.2 nathanw if (kd->argbuf != 0)
710 1.70.2.2 nathanw free((void *)kd->argbuf);
711 1.70.2.2 nathanw if (kd->argv != 0)
712 1.70.2.2 nathanw free((void *)kd->argv);
713 1.70.2.2 nathanw free((void *)kd);
714 1.70.2.2 nathanw
715 1.70.2.2 nathanw return (0);
716 1.70.2.2 nathanw }
717 1.70.2.2 nathanw
718 1.70.2.2 nathanw /*
719 1.70.2.2 nathanw * Set up state necessary to do queries on the kernel namelist
720 1.70.2.2 nathanw * data base. If the data base is out-of-data/incompatible with
721 1.70.2.2 nathanw * given executable, set up things so we revert to standard nlist call.
722 1.70.2.2 nathanw * Only called for live kernels. Return 0 on success, -1 on failure.
723 1.70.2.2 nathanw */
724 1.70.2.2 nathanw static int
725 1.70.2.2 nathanw kvm_dbopen(kd)
726 1.70.2.2 nathanw kvm_t *kd;
727 1.70.2.2 nathanw {
728 1.70.2.2 nathanw DBT rec;
729 1.70.2.2 nathanw size_t dbversionlen;
730 1.70.2.2 nathanw struct nlist nitem;
731 1.70.2.2 nathanw char dbversion[_POSIX2_LINE_MAX];
732 1.70.2.2 nathanw char kversion[_POSIX2_LINE_MAX];
733 1.70.2.2 nathanw
734 1.70.2.2 nathanw kd->db = dbopen(_PATH_KVMDB, O_RDONLY, 0, DB_HASH, NULL);
735 1.70.2.2 nathanw if (kd->db == 0)
736 1.70.2.2 nathanw return (-1);
737 1.70.2.2 nathanw /*
738 1.70.2.2 nathanw * read version out of database
739 1.70.2.2 nathanw */
740 1.70.2.2 nathanw rec.data = VRS_KEY;
741 1.70.2.2 nathanw rec.size = sizeof(VRS_KEY) - 1;
742 1.70.2.2 nathanw if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
743 1.70.2.2 nathanw goto close;
744 1.70.2.2 nathanw if (rec.data == 0 || rec.size > sizeof(dbversion))
745 1.70.2.2 nathanw goto close;
746 1.70.2.2 nathanw
747 1.70.2.2 nathanw memcpy(dbversion, rec.data, rec.size);
748 1.70.2.2 nathanw dbversionlen = rec.size;
749 1.70.2.2 nathanw /*
750 1.70.2.2 nathanw * Read version string from kernel memory.
751 1.70.2.2 nathanw * Since we are dealing with a live kernel, we can call kvm_read()
752 1.70.2.2 nathanw * at this point.
753 1.70.2.2 nathanw */
754 1.70.2.2 nathanw rec.data = VRS_SYM;
755 1.70.2.2 nathanw rec.size = sizeof(VRS_SYM) - 1;
756 1.70.2.2 nathanw if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
757 1.70.2.2 nathanw goto close;
758 1.70.2.2 nathanw if (rec.data == 0 || rec.size != sizeof(struct nlist))
759 1.70.2.2 nathanw goto close;
760 1.70.2.2 nathanw memcpy(&nitem, rec.data, sizeof(nitem));
761 1.70.2.2 nathanw if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
762 1.70.2.2 nathanw dbversionlen)
763 1.70.2.2 nathanw goto close;
764 1.70.2.2 nathanw /*
765 1.70.2.2 nathanw * If they match, we win - otherwise clear out kd->db so
766 1.70.2.2 nathanw * we revert to slow nlist().
767 1.70.2.2 nathanw */
768 1.70.2.2 nathanw if (memcmp(dbversion, kversion, dbversionlen) == 0)
769 1.70.2.2 nathanw return (0);
770 1.70.2.2 nathanw close:
771 1.70.2.2 nathanw (void)(kd->db->close)(kd->db);
772 1.70.2.2 nathanw kd->db = 0;
773 1.70.2.2 nathanw
774 1.70.2.2 nathanw return (-1);
775 1.70.2.2 nathanw }
776 1.70.2.2 nathanw
777 1.70.2.2 nathanw int
778 1.70.2.2 nathanw kvm_nlist(kd, nl)
779 1.70.2.2 nathanw kvm_t *kd;
780 1.70.2.2 nathanw struct nlist *nl;
781 1.70.2.2 nathanw {
782 1.70.2.2 nathanw struct nlist *p;
783 1.70.2.2 nathanw int nvalid, rv;
784 1.70.2.2 nathanw
785 1.70.2.2 nathanw /*
786 1.70.2.2 nathanw * If we can't use the data base, revert to the
787 1.70.2.2 nathanw * slow library call.
788 1.70.2.2 nathanw */
789 1.70.2.2 nathanw if (kd->db == 0) {
790 1.70.2.2 nathanw rv = __fdnlist(kd->nlfd, nl);
791 1.70.2.2 nathanw if (rv == -1)
792 1.70.2.2 nathanw _kvm_err(kd, 0, "bad namelist");
793 1.70.2.2 nathanw return (rv);
794 1.70.2.2 nathanw }
795 1.70.2.2 nathanw
796 1.70.2.2 nathanw /*
797 1.70.2.2 nathanw * We can use the kvm data base. Go through each nlist entry
798 1.70.2.2 nathanw * and look it up with a db query.
799 1.70.2.2 nathanw */
800 1.70.2.2 nathanw nvalid = 0;
801 1.70.2.2 nathanw for (p = nl; p->n_name && p->n_name[0]; ++p) {
802 1.70.2.2 nathanw int len;
803 1.70.2.2 nathanw DBT rec;
804 1.70.2.2 nathanw
805 1.70.2.2 nathanw if ((len = strlen(p->n_name)) > 4096) {
806 1.70.2.2 nathanw /* sanity */
807 1.70.2.2 nathanw _kvm_err(kd, kd->program, "symbol too large");
808 1.70.2.2 nathanw return (-1);
809 1.70.2.2 nathanw }
810 1.70.2.2 nathanw rec.data = (char *)p->n_name;
811 1.70.2.2 nathanw rec.size = len;
812 1.70.2.2 nathanw
813 1.70.2.2 nathanw /*
814 1.70.2.2 nathanw * Make sure that n_value = 0 when the symbol isn't found
815 1.70.2.2 nathanw */
816 1.70.2.2 nathanw p->n_value = 0;
817 1.70.2.2 nathanw
818 1.70.2.2 nathanw if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
819 1.70.2.2 nathanw continue;
820 1.70.2.2 nathanw if (rec.data == 0 || rec.size != sizeof(struct nlist))
821 1.70.2.2 nathanw continue;
822 1.70.2.2 nathanw ++nvalid;
823 1.70.2.2 nathanw /*
824 1.70.2.2 nathanw * Avoid alignment issues.
825 1.70.2.2 nathanw */
826 1.70.2.2 nathanw (void)memcpy(&p->n_type, &((struct nlist *)rec.data)->n_type,
827 1.70.2.2 nathanw sizeof(p->n_type));
828 1.70.2.2 nathanw (void)memcpy(&p->n_value, &((struct nlist *)rec.data)->n_value,
829 1.70.2.2 nathanw sizeof(p->n_value));
830 1.70.2.2 nathanw }
831 1.70.2.2 nathanw /*
832 1.70.2.2 nathanw * Return the number of entries that weren't found.
833 1.70.2.2 nathanw */
834 1.70.2.2 nathanw return ((p - nl) - nvalid);
835 1.70.2.2 nathanw }
836 1.70.2.2 nathanw
837 1.70.2.2 nathanw int kvm_dump_inval(kd)
838 1.70.2.2 nathanw kvm_t *kd;
839 1.70.2.2 nathanw {
840 1.70.2.2 nathanw struct nlist nl[2];
841 1.70.2.2 nathanw u_long pa, val;
842 1.70.2.2 nathanw
843 1.70.2.2 nathanw if (ISALIVE(kd)) {
844 1.70.2.2 nathanw _kvm_err(kd, kd->program, "clearing dump on live kernel");
845 1.70.2.2 nathanw return (-1);
846 1.70.2.2 nathanw }
847 1.70.2.2 nathanw nl[0].n_name = "_dumpmag";
848 1.70.2.2 nathanw nl[1].n_name = NULL;
849 1.70.2.2 nathanw
850 1.70.2.2 nathanw if (kvm_nlist(kd, nl) == -1) {
851 1.70.2.2 nathanw _kvm_err(kd, 0, "bad namelist");
852 1.70.2.2 nathanw return (-1);
853 1.70.2.2 nathanw }
854 1.70.2.2 nathanw if (_kvm_kvatop(kd, (u_long)nl[0].n_value, &pa) == 0)
855 1.70.2.2 nathanw return (-1);
856 1.70.2.2 nathanw
857 1.70.2.2 nathanw errno = 0;
858 1.70.2.2 nathanw val = 0;
859 1.70.2.2 nathanw if (pwrite(kd->pmfd, (void *) &val, sizeof(val),
860 1.70.2.2 nathanw _kvm_pa2off(kd, pa)) == -1) {
861 1.70.2.2 nathanw _kvm_syserr(kd, 0, "cannot invalidate dump - pwrite");
862 1.70.2.2 nathanw return (-1);
863 1.70.2.2 nathanw }
864 1.70.2.2 nathanw return (0);
865 1.70.2.2 nathanw }
866 1.70.2.2 nathanw
867 1.70.2.2 nathanw ssize_t
868 1.70.2.2 nathanw kvm_read(kd, kva, buf, len)
869 1.70.2.2 nathanw kvm_t *kd;
870 1.70.2.2 nathanw u_long kva;
871 1.70.2.2 nathanw void *buf;
872 1.70.2.2 nathanw size_t len;
873 1.70.2.2 nathanw {
874 1.70.2.2 nathanw int cc;
875 1.70.2.2 nathanw void *cp;
876 1.70.2.2 nathanw
877 1.70.2.2 nathanw if (ISKMEM(kd)) {
878 1.70.2.2 nathanw /*
879 1.70.2.2 nathanw * We're using /dev/kmem. Just read straight from the
880 1.70.2.2 nathanw * device and let the active kernel do the address translation.
881 1.70.2.2 nathanw */
882 1.70.2.2 nathanw errno = 0;
883 1.70.2.2 nathanw cc = pread(kd->vmfd, buf, len, (off_t)kva);
884 1.70.2.2 nathanw if (cc < 0) {
885 1.70.2.2 nathanw _kvm_syserr(kd, 0, "kvm_read");
886 1.70.2.2 nathanw return (-1);
887 1.70.2.2 nathanw } else if (cc < len)
888 1.70.2.2 nathanw _kvm_err(kd, kd->program, "short read");
889 1.70.2.2 nathanw return (cc);
890 1.70.2.2 nathanw } else if (ISSYSCTL(kd)) {
891 1.70.2.2 nathanw _kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
892 1.70.2.2 nathanw "can't use kvm_read");
893 1.70.2.2 nathanw return (-1);
894 1.70.2.2 nathanw } else {
895 1.70.2.2 nathanw if ((kd->kcore_hdr == NULL) || (kd->cpu_data == NULL)) {
896 1.70.2.2 nathanw _kvm_err(kd, kd->program, "no valid dump header");
897 1.70.2.2 nathanw return (-1);
898 1.70.2.2 nathanw }
899 1.70.2.2 nathanw cp = buf;
900 1.70.2.2 nathanw while (len > 0) {
901 1.70.2.2 nathanw u_long pa;
902 1.70.2.2 nathanw off_t foff;
903 1.70.2.2 nathanw
904 1.70.2.2 nathanw cc = _kvm_kvatop(kd, kva, &pa);
905 1.70.2.2 nathanw if (cc == 0)
906 1.70.2.2 nathanw return (-1);
907 1.70.2.2 nathanw if (cc > len)
908 1.70.2.2 nathanw cc = len;
909 1.70.2.2 nathanw foff = _kvm_pa2off(kd, pa);
910 1.70.2.2 nathanw errno = 0;
911 1.70.2.2 nathanw cc = pread(kd->pmfd, cp, (size_t)cc, foff);
912 1.70.2.2 nathanw if (cc < 0) {
913 1.70.2.2 nathanw _kvm_syserr(kd, kd->program, "kvm_read");
914 1.70.2.2 nathanw break;
915 1.70.2.2 nathanw }
916 1.70.2.2 nathanw /*
917 1.70.2.2 nathanw * If kvm_kvatop returns a bogus value or our core
918 1.70.2.2 nathanw * file is truncated, we might wind up seeking beyond
919 1.70.2.2 nathanw * the end of the core file in which case the read will
920 1.70.2.2 nathanw * return 0 (EOF).
921 1.70.2.2 nathanw */
922 1.70.2.2 nathanw if (cc == 0)
923 1.70.2.2 nathanw break;
924 1.70.2.2 nathanw cp = (char *)cp + cc;
925 1.70.2.2 nathanw kva += cc;
926 1.70.2.2 nathanw len -= cc;
927 1.70.2.2 nathanw }
928 1.70.2.2 nathanw return ((char *)cp - (char *)buf);
929 1.70.2.2 nathanw }
930 1.70.2.2 nathanw /* NOTREACHED */
931 1.70.2.2 nathanw }
932 1.70.2.2 nathanw
933 1.70.2.2 nathanw ssize_t
934 1.70.2.2 nathanw kvm_write(kd, kva, buf, len)
935 1.70.2.2 nathanw kvm_t *kd;
936 1.70.2.2 nathanw u_long kva;
937 1.70.2.2 nathanw const void *buf;
938 1.70.2.2 nathanw size_t len;
939 1.70.2.2 nathanw {
940 1.70.2.2 nathanw int cc;
941 1.70.2.2 nathanw
942 1.70.2.2 nathanw if (ISKMEM(kd)) {
943 1.70.2.2 nathanw /*
944 1.70.2.2 nathanw * Just like kvm_read, only we write.
945 1.70.2.2 nathanw */
946 1.70.2.2 nathanw errno = 0;
947 1.70.2.2 nathanw cc = pwrite(kd->vmfd, buf, len, (off_t)kva);
948 1.70.2.2 nathanw if (cc < 0) {
949 1.70.2.2 nathanw _kvm_syserr(kd, 0, "kvm_write");
950 1.70.2.2 nathanw return (-1);
951 1.70.2.2 nathanw } else if (cc < len)
952 1.70.2.2 nathanw _kvm_err(kd, kd->program, "short write");
953 1.70.2.2 nathanw return (cc);
954 1.70.2.2 nathanw } else if (ISSYSCTL(kd)) {
955 1.70.2.2 nathanw _kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
956 1.70.2.2 nathanw "can't use kvm_write");
957 1.70.2.2 nathanw return (-1);
958 1.70.2.2 nathanw } else {
959 1.70.2.2 nathanw _kvm_err(kd, kd->program,
960 1.70.2.2 nathanw "kvm_write not implemented for dead kernels");
961 1.70.2.2 nathanw return (-1);
962 1.70.2.2 nathanw }
963 1.70.2.2 nathanw /* NOTREACHED */
964 1.70.2.2 nathanw }
965