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