xref: /freebsd/lib/libkvm/kvm.c (revision bfe691b2f75de2224c7ceb304ebcdef2b42d4179)
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 (_kvm_initvtop(kd) < 0)
223 		goto failed;
224 	return (kd);
225 failed:
226 	/*
227 	 * Copy out the error if doing sane error semantics.
228 	 */
229 	if (errout != 0)
230 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
231 	(void)kvm_close(kd);
232 	return (0);
233 }
234 
235 kvm_t *
236 kvm_openfiles(uf, mf, sf, flag, errout)
237 	const char *uf;
238 	const char *mf;
239 	const char *sf __unused;
240 	int flag;
241 	char *errout;
242 {
243 	kvm_t *kd;
244 
245 	if ((kd = malloc(sizeof(*kd))) == NULL) {
246 		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
247 		return (0);
248 	}
249 	memset(kd, 0, sizeof(*kd));
250 	kd->program = 0;
251 	return (_kvm_open(kd, uf, mf, flag, errout));
252 }
253 
254 kvm_t *
255 kvm_open(uf, mf, sf, flag, errstr)
256 	const char *uf;
257 	const char *mf;
258 	const char *sf __unused;
259 	int flag;
260 	const char *errstr;
261 {
262 	kvm_t *kd;
263 
264 	if ((kd = malloc(sizeof(*kd))) == NULL) {
265 		if (errstr != NULL)
266 			(void)fprintf(stderr, "%s: %s\n",
267 				      errstr, strerror(errno));
268 		return (0);
269 	}
270 	memset(kd, 0, sizeof(*kd));
271 	kd->program = errstr;
272 	return (_kvm_open(kd, uf, mf, flag, NULL));
273 }
274 
275 int
276 kvm_close(kd)
277 	kvm_t *kd;
278 {
279 	int error = 0;
280 
281 	if (kd->pmfd >= 0)
282 		error |= close(kd->pmfd);
283 	if (kd->vmfd >= 0)
284 		error |= close(kd->vmfd);
285 	if (kd->nlfd >= 0)
286 		error |= close(kd->nlfd);
287 	if (kd->vmst)
288 		_kvm_freevtop(kd);
289 	if (kd->procbase != 0)
290 		free((void *)kd->procbase);
291 	if (kd->argv != 0)
292 		free((void *)kd->argv);
293 	free((void *)kd);
294 
295 	return (0);
296 }
297 
298 int
299 kvm_nlist(kd, nl)
300 	kvm_t *kd;
301 	struct nlist *nl;
302 {
303 	struct nlist *p;
304 	int nvalid;
305 	struct kld_sym_lookup lookup;
306 	int error;
307 
308 	/*
309 	 * If we can't use the kld symbol lookup, revert to the
310 	 * slow library call.
311 	 */
312 	if (!ISALIVE(kd))
313 		return (__fdnlist(kd->nlfd, nl));
314 
315 	/*
316 	 * We can use the kld lookup syscall.  Go through each nlist entry
317 	 * and look it up with a kldsym(2) syscall.
318 	 */
319 	nvalid = 0;
320 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
321 		lookup.version = sizeof(lookup);
322 		lookup.symname = p->n_name;
323 		lookup.symvalue = 0;
324 		lookup.symsize = 0;
325 
326 		if (lookup.symname[0] == '_')
327 			lookup.symname++;
328 
329 		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
330 			p->n_type = N_TEXT;
331 			p->n_other = 0;
332 			p->n_desc = 0;
333 			p->n_value = lookup.symvalue;
334 			++nvalid;
335 			/* lookup.symsize */
336 		}
337 	}
338 	/*
339 	 * Return the number of entries that weren't found. If they exist,
340 	 * also fill internal error buffer.
341 	 */
342 	error = ((p - nl) - nvalid);
343 	if (error)
344 		_kvm_syserr(kd, kd->program, "kvm_nlist");
345 	return (error);
346 }
347 
348 ssize_t
349 kvm_read(kd, kva, buf, len)
350 	kvm_t *kd;
351 	u_long kva;
352 	void *buf;
353 	size_t len;
354 {
355 	int cc;
356 	char *cp;
357 
358 	if (ISALIVE(kd)) {
359 		/*
360 		 * We're using /dev/kmem.  Just read straight from the
361 		 * device and let the active kernel do the address translation.
362 		 */
363 		errno = 0;
364 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
365 			_kvm_err(kd, 0, "invalid address (%x)", kva);
366 			return (-1);
367 		}
368 		cc = read(kd->vmfd, buf, len);
369 		if (cc < 0) {
370 			_kvm_syserr(kd, 0, "kvm_read");
371 			return (-1);
372 		} else if (cc < len)
373 			_kvm_err(kd, kd->program, "short read");
374 		return (cc);
375 	} else {
376 		cp = buf;
377 		while (len > 0) {
378 			off_t pa;
379 
380 			cc = _kvm_kvatop(kd, kva, &pa);
381 			if (cc == 0)
382 				return (-1);
383 			if (cc > len)
384 				cc = len;
385 			errno = 0;
386 			if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
387 				_kvm_syserr(kd, 0, _PATH_MEM);
388 				break;
389 			}
390 			cc = read(kd->pmfd, cp, cc);
391 			if (cc < 0) {
392 				_kvm_syserr(kd, kd->program, "kvm_read");
393 				break;
394 			}
395 			/*
396 			 * If kvm_kvatop returns a bogus value or our core
397 			 * file is truncated, we might wind up seeking beyond
398 			 * the end of the core file in which case the read will
399 			 * return 0 (EOF).
400 			 */
401 			if (cc == 0)
402 				break;
403 			cp += cc;
404 			kva += cc;
405 			len -= cc;
406 		}
407 		return (cp - (char *)buf);
408 	}
409 	/* NOTREACHED */
410 }
411 
412 ssize_t
413 kvm_write(kd, kva, buf, len)
414 	kvm_t *kd;
415 	u_long kva;
416 	const void *buf;
417 	size_t len;
418 {
419 	int cc;
420 
421 	if (ISALIVE(kd)) {
422 		/*
423 		 * Just like kvm_read, only we write.
424 		 */
425 		errno = 0;
426 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
427 			_kvm_err(kd, 0, "invalid address (%x)", kva);
428 			return (-1);
429 		}
430 		cc = write(kd->vmfd, buf, len);
431 		if (cc < 0) {
432 			_kvm_syserr(kd, 0, "kvm_write");
433 			return (-1);
434 		} else if (cc < len)
435 			_kvm_err(kd, kd->program, "short write");
436 		return (cc);
437 	} else {
438 		_kvm_err(kd, kd->program,
439 		    "kvm_write not implemented for dead kernels");
440 		return (-1);
441 	}
442 	/* NOTREACHED */
443 }
444