kvm.c revision 1.38 1 /*-
2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
40 #endif /* LIBC_SCCS and not lint */
41
42 #include <sys/param.h>
43 #include <sys/user.h>
44 #include <sys/proc.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/sysctl.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/swap_pager.h>
52
53 #include <machine/vmparam.h>
54
55 #include <ctype.h>
56 #include <db.h>
57 #include <fcntl.h>
58 #include <kvm.h>
59 #include <limits.h>
60 #include <nlist.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include "kvm_private.h"
68
69 static int kvm_dbopen __P((kvm_t *, const char *));
70
71 char *
72 kvm_geterr(kd)
73 kvm_t *kd;
74 {
75 return (kd->errbuf);
76 }
77
78 #if __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 /*
85 * Report an error using printf style arguments. "program" is kd->program
86 * on hard errors, and 0 on soft errors, so that under sun error emulation,
87 * only hard errors are printed out (otherwise, programs like gdb will
88 * generate tons of error messages when trying to access bogus pointers).
89 */
90 void
91 #if __STDC__
92 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
93 #else
94 _kvm_err(kd, program, fmt, va_alist)
95 kvm_t *kd;
96 char *program, *fmt;
97 va_dcl
98 #endif
99 {
100 va_list ap;
101
102 #ifdef __STDC__
103 va_start(ap, fmt);
104 #else
105 va_start(ap);
106 #endif
107 if (program != NULL) {
108 (void)fprintf(stderr, "%s: ", program);
109 (void)vfprintf(stderr, fmt, ap);
110 (void)fputc('\n', stderr);
111 } else
112 (void)vsnprintf(kd->errbuf,
113 sizeof(kd->errbuf), (char *)fmt, ap);
114
115 va_end(ap);
116 }
117
118 void
119 #if __STDC__
120 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
121 #else
122 _kvm_syserr(kd, program, fmt, va_alist)
123 kvm_t *kd;
124 char *program, *fmt;
125 va_dcl
126 #endif
127 {
128 va_list ap;
129 register int n;
130
131 #if __STDC__
132 va_start(ap, fmt);
133 #else
134 va_start(ap);
135 #endif
136 if (program != NULL) {
137 (void)fprintf(stderr, "%s: ", program);
138 (void)vfprintf(stderr, fmt, ap);
139 (void)fprintf(stderr, ": %s\n", strerror(errno));
140 } else {
141 register char *cp = kd->errbuf;
142
143 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
144 n = strlen(cp);
145 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
146 strerror(errno));
147 }
148 va_end(ap);
149 }
150
151 void *
152 _kvm_malloc(kd, n)
153 register kvm_t *kd;
154 register size_t n;
155 {
156 void *p;
157
158 if ((p = malloc(n)) == NULL)
159 _kvm_err(kd, kd->program, strerror(errno));
160 return (p);
161 }
162
163 static kvm_t *
164 _kvm_open(kd, uf, mf, sf, flag, errout)
165 register kvm_t *kd;
166 const char *uf;
167 const char *mf;
168 const char *sf;
169 int flag;
170 char *errout;
171 {
172 struct stat st;
173
174 kd->db = 0;
175 kd->pmfd = -1;
176 kd->vmfd = -1;
177 kd->swfd = -1;
178 kd->nlfd = -1;
179 kd->procbase = 0;
180 kd->nbpg = getpagesize();
181 kd->swapspc = 0;
182 kd->argspc = 0;
183 kd->argbuf = 0;
184 kd->argv = 0;
185 kd->vmst = 0;
186 kd->vm_page_buckets = 0;
187
188 if (uf == 0)
189 uf = _PATH_UNIX;
190 else if (strlen(uf) >= MAXPATHLEN) {
191 _kvm_err(kd, kd->program, "exec file name too long");
192 goto failed;
193 }
194 if (flag & ~O_RDWR) {
195 _kvm_err(kd, kd->program, "bad flags arg");
196 goto failed;
197 }
198 if (mf == 0)
199 mf = _PATH_MEM;
200 if (sf == 0)
201 sf = _PATH_DRUM;
202
203 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
204 _kvm_syserr(kd, kd->program, "%s", mf);
205 goto failed;
206 }
207 if (fstat(kd->pmfd, &st) < 0) {
208 _kvm_syserr(kd, kd->program, "%s", mf);
209 goto failed;
210 }
211 if (S_ISCHR(st.st_mode)) {
212 /*
213 * If this is a character special device, then check that
214 * it's /dev/mem. If so, open kmem too. (Maybe we should
215 * make it work for either /dev/mem or /dev/kmem -- in either
216 * case you're working with a live kernel.)
217 */
218 if (strcmp(mf, _PATH_MEM) != 0) { /* XXX */
219 _kvm_err(kd, kd->program,
220 "%s: not physical memory device", mf);
221 goto failed;
222 }
223 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
224 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
225 goto failed;
226 }
227 if ((kd->swfd = open(sf, flag, 0)) < 0) {
228 _kvm_syserr(kd, kd->program, "%s", sf);
229 goto failed;
230 }
231 /*
232 * Open kvm nlist database. We go ahead and do this
233 * here so that we don't have to hold on to the vmunix
234 * path name. Since a kvm application will surely do
235 * a kvm_nlist(), this probably won't be a wasted effort.
236 * If the database cannot be opened, open the namelist
237 * argument so we revert to slow nlist() calls.
238 */
239 if (kvm_dbopen(kd, uf) < 0 &&
240 (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
241 _kvm_syserr(kd, kd->program, "%s", uf);
242 goto failed;
243 }
244 } else {
245 /*
246 * This is a crash dump.
247 * Initalize the virtual address translation machinery,
248 * but first setup the namelist fd.
249 */
250 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
251 _kvm_syserr(kd, kd->program, "%s", uf);
252 goto failed;
253 }
254 if (_kvm_initvtop(kd) < 0)
255 goto failed;
256 }
257 return (kd);
258 failed:
259 /*
260 * Copy out the error if doing sane error semantics.
261 */
262 if (errout != 0)
263 strcpy(errout, kd->errbuf);
264 (void)kvm_close(kd);
265 return (0);
266 }
267
268 kvm_t *
269 kvm_openfiles(uf, mf, sf, flag, errout)
270 const char *uf;
271 const char *mf;
272 const char *sf;
273 int flag;
274 char *errout;
275 {
276 register kvm_t *kd;
277
278 if ((kd = malloc(sizeof(*kd))) == NULL) {
279 (void)strcpy(errout, strerror(errno));
280 return (0);
281 }
282 kd->program = 0;
283 return (_kvm_open(kd, uf, mf, sf, flag, errout));
284 }
285
286 kvm_t *
287 kvm_open(uf, mf, sf, flag, program)
288 const char *uf;
289 const char *mf;
290 const char *sf;
291 int flag;
292 const char *program;
293 {
294 register kvm_t *kd;
295
296 if ((kd = malloc(sizeof(*kd))) == NULL && program != NULL) {
297 (void)fprintf(stderr, "%s: %s\n", strerror(errno));
298 return (0);
299 }
300 kd->program = program;
301 return (_kvm_open(kd, uf, mf, sf, flag, NULL));
302 }
303
304 int
305 kvm_close(kd)
306 kvm_t *kd;
307 {
308 register int error = 0;
309
310 if (kd->pmfd >= 0)
311 error |= close(kd->pmfd);
312 if (kd->vmfd >= 0)
313 error |= close(kd->vmfd);
314 if (kd->nlfd >= 0)
315 error |= close(kd->nlfd);
316 if (kd->swfd >= 0)
317 error |= close(kd->swfd);
318 if (kd->db != 0)
319 error |= (kd->db->close)(kd->db);
320 if (kd->vmst)
321 _kvm_freevtop(kd);
322 if (kd->procbase != 0)
323 free((void *)kd->procbase);
324 if (kd->swapspc != 0)
325 free((void *)kd->swapspc);
326 if (kd->argspc != 0)
327 free((void *)kd->argspc);
328 if (kd->argbuf != 0)
329 free((void *)kd->argbuf);
330 if (kd->argv != 0)
331 free((void *)kd->argv);
332 free((void *)kd);
333
334 return (0);
335 }
336
337 /*
338 * Set up state necessary to do queries on the kernel namelist
339 * data base. If the data base is out-of-data/incompatible with
340 * given executable, set up things so we revert to standard nlist call.
341 * Only called for live kernels. Return 0 on success, -1 on failure.
342 */
343 static int
344 kvm_dbopen(kd, uf)
345 kvm_t *kd;
346 const char *uf;
347 {
348 char *cp;
349 DBT rec;
350 int dbversionlen;
351 struct nlist nitem;
352 char dbversion[_POSIX2_LINE_MAX];
353 char kversion[_POSIX2_LINE_MAX];
354 char dbname[MAXPATHLEN];
355
356 if ((cp = rindex(uf, '/')) != 0)
357 uf = cp + 1;
358
359 (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
360 kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL);
361 if (kd->db == 0)
362 return (-1);
363 /*
364 * read version out of database
365 */
366 rec.data = VRS_KEY;
367 rec.size = sizeof(VRS_KEY) - 1;
368 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
369 goto close;
370 if (rec.data == 0 || rec.size > sizeof(dbversion))
371 goto close;
372
373 bcopy(rec.data, dbversion, rec.size);
374 dbversionlen = rec.size;
375 /*
376 * Read version string from kernel memory.
377 * Since we are dealing with a live kernel, we can call kvm_read()
378 * at this point.
379 */
380 rec.data = VRS_SYM;
381 rec.size = sizeof(VRS_SYM) - 1;
382 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
383 goto close;
384 if (rec.data == 0 || rec.size != sizeof(struct nlist))
385 goto close;
386 bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem));
387 if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
388 dbversionlen)
389 goto close;
390 /*
391 * If they match, we win - otherwise clear out kd->db so
392 * we revert to slow nlist().
393 */
394 if (bcmp(dbversion, kversion, dbversionlen) == 0)
395 return (0);
396 close:
397 (void)(kd->db->close)(kd->db);
398 kd->db = 0;
399
400 return (-1);
401 }
402
403 int
404 kvm_nlist(kd, nl)
405 kvm_t *kd;
406 struct nlist *nl;
407 {
408 register struct nlist *p;
409 register int nvalid;
410
411 /*
412 * If we can't use the data base, revert to the
413 * slow library call.
414 */
415 if (kd->db == 0)
416 return (__fdnlist(kd->nlfd, nl));
417
418 /*
419 * We can use the kvm data base. Go through each nlist entry
420 * and look it up with a db query.
421 */
422 nvalid = 0;
423 for (p = nl; p->n_name && p->n_name[0]; ++p) {
424 register int len;
425 DBT rec;
426
427 if ((len = strlen(p->n_name)) > 4096) {
428 /* sanity */
429 _kvm_err(kd, kd->program, "symbol too large");
430 return (-1);
431 }
432 rec.data = p->n_name;
433 rec.size = len;
434 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
435 continue;
436 if (rec.data == 0 || rec.size != sizeof(struct nlist))
437 continue;
438 ++nvalid;
439 /*
440 * Avoid alignment issues.
441 */
442 bcopy((char *)&((struct nlist *)rec.data)->n_type,
443 (char *)&p->n_type,
444 sizeof(p->n_type));
445 bcopy((char *)&((struct nlist *)rec.data)->n_value,
446 (char *)&p->n_value,
447 sizeof(p->n_value));
448 }
449 /*
450 * Return the number of entries that weren't found.
451 */
452 return ((p - nl) - nvalid);
453 }
454
455 ssize_t
456 kvm_read(kd, kva, buf, len)
457 kvm_t *kd;
458 register u_long kva;
459 register void *buf;
460 register size_t len;
461 {
462 register int cc;
463 register void *cp;
464
465 if (ISALIVE(kd)) {
466 /*
467 * We're using /dev/kmem. Just read straight from the
468 * device and let the active kernel do the address translation.
469 */
470 errno = 0;
471 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
472 _kvm_err(kd, 0, "invalid address (%x)", kva);
473 return (0);
474 }
475 cc = read(kd->vmfd, buf, len);
476 if (cc < 0) {
477 _kvm_syserr(kd, 0, "kvm_read");
478 return (0);
479 } else if (cc < len)
480 _kvm_err(kd, kd->program, "short read");
481 return (cc);
482 } else {
483 cp = buf;
484 while (len > 0) {
485 u_long pa;
486
487 cc = _kvm_kvatop(kd, kva, &pa);
488 if (cc == 0)
489 return (0);
490 if (cc > len)
491 cc = len;
492 errno = 0;
493 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
494 _kvm_syserr(kd, 0, _PATH_MEM);
495 break;
496 }
497 cc = read(kd->pmfd, cp, cc);
498 if (cc < 0) {
499 _kvm_syserr(kd, kd->program, "kvm_read");
500 break;
501 }
502 /*
503 * If kvm_kvatop returns a bogus value or our core
504 * file is truncated, we might wind up seeking beyond
505 * the end of the core file in which case the read will
506 * return 0 (EOF).
507 */
508 if (cc == 0)
509 break;
510 (char *)cp += cc;
511 kva += cc;
512 len -= cc;
513 }
514 return ((char *)cp - (char *)buf);
515 }
516 /* NOTREACHED */
517 }
518
519 ssize_t
520 kvm_write(kd, kva, buf, len)
521 kvm_t *kd;
522 register u_long kva;
523 register const void *buf;
524 register size_t len;
525 {
526 register int cc;
527
528 if (ISALIVE(kd)) {
529 /*
530 * Just like kvm_read, only we write.
531 */
532 errno = 0;
533 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
534 _kvm_err(kd, 0, "invalid address (%x)", kva);
535 return (0);
536 }
537 cc = write(kd->vmfd, buf, len);
538 if (cc < 0) {
539 _kvm_syserr(kd, 0, "kvm_write");
540 return (0);
541 } else if (cc < len)
542 _kvm_err(kd, kd->program, "short write");
543 return (cc);
544 } else {
545 _kvm_err(kd, kd->program,
546 "kvm_write not implemented for dead kernels");
547 return (0);
548 }
549 /* NOTREACHED */
550 }
551