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