xref: /freebsd/lib/libkvm/kvm.c (revision 2f6a179eb910129fb812c1ad1bdc300da1203dc0)
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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
40 #endif
41 #endif /* LIBC_SCCS and not lint */
42 
43 #include <sys/param.h>
44 #include <sys/user.h>
45 #include <sys/proc.h>
46 #include <sys/ioctl.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/linker.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 
54 #include <machine/vmparam.h>
55 
56 #include <ctype.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 /* from src/lib/libc/gen/nlist.c */
70 int __fdnlist(int, struct nlist *);
71 
72 char *
73 kvm_geterr(kd)
74 	kvm_t *kd;
75 {
76 	return (kd->errbuf);
77 }
78 
79 #include <stdarg.h>
80 
81 /*
82  * Report an error using printf style arguments.  "program" is kd->program
83  * on hard errors, and 0 on soft errors, so that under sun error emulation,
84  * only hard errors are printed out (otherwise, programs like gdb will
85  * generate tons of error messages when trying to access bogus pointers).
86  */
87 void
88 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
89 {
90 	va_list ap;
91 
92 	va_start(ap, fmt);
93 	if (program != NULL) {
94 		(void)fprintf(stderr, "%s: ", program);
95 		(void)vfprintf(stderr, fmt, ap);
96 		(void)fputc('\n', stderr);
97 	} else
98 		(void)vsnprintf(kd->errbuf,
99 		    sizeof(kd->errbuf), (char *)fmt, ap);
100 
101 	va_end(ap);
102 }
103 
104 void
105 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
106 {
107 	va_list ap;
108 	int n;
109 
110 	va_start(ap, fmt);
111 	if (program != NULL) {
112 		(void)fprintf(stderr, "%s: ", program);
113 		(void)vfprintf(stderr, fmt, ap);
114 		(void)fprintf(stderr, ": %s\n", strerror(errno));
115 	} else {
116 		char *cp = kd->errbuf;
117 
118 		(void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
119 		n = strlen(cp);
120 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
121 		    strerror(errno));
122 	}
123 	va_end(ap);
124 }
125 
126 void *
127 _kvm_malloc(kd, n)
128 	kvm_t *kd;
129 	size_t n;
130 {
131 	void *p;
132 
133 	if ((p = calloc(n, sizeof(char))) == NULL)
134 		_kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
135 			 n, strerror(errno));
136 	return (p);
137 }
138 
139 static kvm_t *
140 _kvm_open(kd, uf, mf, flag, errout)
141 	kvm_t *kd;
142 	const char *uf;
143 	const char *mf;
144 	int flag;
145 	char *errout;
146 {
147 	struct stat st;
148 
149 	kd->vmfd = -1;
150 	kd->pmfd = -1;
151 	kd->nlfd = -1;
152 	kd->vmst = 0;
153 	kd->procbase = 0;
154 	kd->argspc = 0;
155 	kd->argv = 0;
156 
157 	if (uf == 0)
158 		uf = getbootfile();
159 	else if (strlen(uf) >= MAXPATHLEN) {
160 		_kvm_err(kd, kd->program, "exec file name too long");
161 		goto failed;
162 	}
163 	if (flag & ~O_RDWR) {
164 		_kvm_err(kd, kd->program, "bad flags arg");
165 		goto failed;
166 	}
167 	if (mf == 0)
168 		mf = _PATH_MEM;
169 
170 	if ((kd->pmfd = open(mf, flag, 0)) < 0) {
171 		_kvm_syserr(kd, kd->program, "%s", mf);
172 		goto failed;
173 	}
174 	if (fstat(kd->pmfd, &st) < 0) {
175 		_kvm_syserr(kd, kd->program, "%s", mf);
176 		goto failed;
177 	}
178 	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
179 		errno = EINVAL;
180 		_kvm_syserr(kd, kd->program, "empty file");
181 		goto failed;
182 	}
183 	if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
184 		_kvm_syserr(kd, kd->program, "%s", mf);
185 		goto failed;
186 	}
187 	if (S_ISCHR(st.st_mode)) {
188 		/*
189 		 * If this is a character special device, then check that
190 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
191 		 * make it work for either /dev/mem or /dev/kmem -- in either
192 		 * case you're working with a live kernel.)
193 		 */
194 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
195 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
196 			return (kd);
197 		} else if (strcmp(mf, _PATH_MEM) == 0) {
198 			if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
199 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
200 				goto failed;
201 			}
202 			if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
203 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
204 				goto failed;
205 			}
206 			return (kd);
207 		}
208 	}
209 	/*
210 	 * This is a crash dump.
211 	 * Initialize the virtual address translation machinery,
212 	 * but first setup the namelist fd.
213 	 */
214 	if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
215 		_kvm_syserr(kd, kd->program, "%s", uf);
216 		goto failed;
217 	}
218 	if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
219 		_kvm_syserr(kd, kd->program, "%s", uf);
220 		goto failed;
221 	}
222 	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
223 		kd->rawdump = 1;
224 	if (_kvm_initvtop(kd) < 0)
225 		goto failed;
226 	return (kd);
227 failed:
228 	/*
229 	 * Copy out the error if doing sane error semantics.
230 	 */
231 	if (errout != 0)
232 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
233 	(void)kvm_close(kd);
234 	return (0);
235 }
236 
237 kvm_t *
238 kvm_openfiles(uf, mf, sf, flag, errout)
239 	const char *uf;
240 	const char *mf;
241 	const char *sf __unused;
242 	int flag;
243 	char *errout;
244 {
245 	kvm_t *kd;
246 
247 	if ((kd = malloc(sizeof(*kd))) == NULL) {
248 		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
249 		return (0);
250 	}
251 	memset(kd, 0, sizeof(*kd));
252 	kd->program = 0;
253 	return (_kvm_open(kd, uf, mf, flag, errout));
254 }
255 
256 kvm_t *
257 kvm_open(uf, mf, sf, flag, errstr)
258 	const char *uf;
259 	const char *mf;
260 	const char *sf __unused;
261 	int flag;
262 	const char *errstr;
263 {
264 	kvm_t *kd;
265 
266 	if ((kd = malloc(sizeof(*kd))) == NULL) {
267 		if (errstr != NULL)
268 			(void)fprintf(stderr, "%s: %s\n",
269 				      errstr, strerror(errno));
270 		return (0);
271 	}
272 	memset(kd, 0, sizeof(*kd));
273 	kd->program = errstr;
274 	return (_kvm_open(kd, uf, mf, flag, NULL));
275 }
276 
277 int
278 kvm_close(kd)
279 	kvm_t *kd;
280 {
281 	int error = 0;
282 
283 	if (kd->pmfd >= 0)
284 		error |= close(kd->pmfd);
285 	if (kd->vmfd >= 0)
286 		error |= close(kd->vmfd);
287 	if (kd->nlfd >= 0)
288 		error |= close(kd->nlfd);
289 	if (kd->vmst)
290 		_kvm_freevtop(kd);
291 	if (kd->procbase != 0)
292 		free((void *)kd->procbase);
293 	if (kd->argbuf != 0)
294 		free((void *) kd->argbuf);
295 	if (kd->argspc != 0)
296 		free((void *) kd->argspc);
297 	if (kd->argv != 0)
298 		free((void *)kd->argv);
299 	free((void *)kd);
300 
301 	return (0);
302 }
303 
304 int
305 kvm_nlist(kd, nl)
306 	kvm_t *kd;
307 	struct nlist *nl;
308 {
309 	struct nlist *p;
310 	int nvalid;
311 	struct kld_sym_lookup lookup;
312 	int error;
313 
314 	/*
315 	 * If we can't use the kld symbol lookup, revert to the
316 	 * slow library call.
317 	 */
318 	if (!ISALIVE(kd))
319 		return (__fdnlist(kd->nlfd, nl));
320 
321 	/*
322 	 * We can use the kld lookup syscall.  Go through each nlist entry
323 	 * and look it up with a kldsym(2) syscall.
324 	 */
325 	nvalid = 0;
326 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
327 		lookup.version = sizeof(lookup);
328 		lookup.symname = p->n_name;
329 		lookup.symvalue = 0;
330 		lookup.symsize = 0;
331 
332 		if (lookup.symname[0] == '_')
333 			lookup.symname++;
334 
335 		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
336 			p->n_type = N_TEXT;
337 			p->n_other = 0;
338 			p->n_desc = 0;
339 			p->n_value = lookup.symvalue;
340 			++nvalid;
341 			/* lookup.symsize */
342 		}
343 	}
344 	/*
345 	 * Return the number of entries that weren't found. If they exist,
346 	 * also fill internal error buffer.
347 	 */
348 	error = ((p - nl) - nvalid);
349 	if (error)
350 		_kvm_syserr(kd, kd->program, "kvm_nlist");
351 	return (error);
352 }
353 
354 ssize_t
355 kvm_read(kd, kva, buf, len)
356 	kvm_t *kd;
357 	u_long kva;
358 	void *buf;
359 	size_t len;
360 {
361 	int cc;
362 	char *cp;
363 
364 	if (ISALIVE(kd)) {
365 		/*
366 		 * We're using /dev/kmem.  Just read straight from the
367 		 * device and let the active kernel do the address translation.
368 		 */
369 		errno = 0;
370 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
371 			_kvm_err(kd, 0, "invalid address (%x)", kva);
372 			return (-1);
373 		}
374 		cc = read(kd->vmfd, buf, len);
375 		if (cc < 0) {
376 			_kvm_syserr(kd, 0, "kvm_read");
377 			return (-1);
378 		} else if (cc < len)
379 			_kvm_err(kd, kd->program, "short read");
380 		return (cc);
381 	} else {
382 		cp = buf;
383 		while (len > 0) {
384 			off_t pa;
385 
386 			cc = _kvm_kvatop(kd, kva, &pa);
387 			if (cc == 0)
388 				return (-1);
389 			if (cc > len)
390 				cc = len;
391 			errno = 0;
392 			if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
393 				_kvm_syserr(kd, 0, _PATH_MEM);
394 				break;
395 			}
396 			cc = read(kd->pmfd, cp, cc);
397 			if (cc < 0) {
398 				_kvm_syserr(kd, kd->program, "kvm_read");
399 				break;
400 			}
401 			/*
402 			 * If kvm_kvatop returns a bogus value or our core
403 			 * file is truncated, we might wind up seeking beyond
404 			 * the end of the core file in which case the read will
405 			 * return 0 (EOF).
406 			 */
407 			if (cc == 0)
408 				break;
409 			cp += cc;
410 			kva += cc;
411 			len -= cc;
412 		}
413 		return (cp - (char *)buf);
414 	}
415 	/* NOTREACHED */
416 }
417 
418 ssize_t
419 kvm_write(kd, kva, buf, len)
420 	kvm_t *kd;
421 	u_long kva;
422 	const void *buf;
423 	size_t len;
424 {
425 	int cc;
426 
427 	if (ISALIVE(kd)) {
428 		/*
429 		 * Just like kvm_read, only we write.
430 		 */
431 		errno = 0;
432 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
433 			_kvm_err(kd, 0, "invalid address (%x)", kva);
434 			return (-1);
435 		}
436 		cc = write(kd->vmfd, buf, len);
437 		if (cc < 0) {
438 			_kvm_syserr(kd, 0, "kvm_write");
439 			return (-1);
440 		} else if (cc < len)
441 			_kvm_err(kd, kd->program, "short write");
442 		return (cc);
443 	} else {
444 		_kvm_err(kd, kd->program,
445 		    "kvm_write not implemented for dead kernels");
446 		return (-1);
447 	}
448 	/* NOTREACHED */
449 }
450