2 * Copyright (c) 2006 The FreeBSD Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/queue.h> /* for <sys/linker.h> with _KERNEL defined */
42 #include <sys/linker.h>
47 /* Name of the head of the linker file list in /sys/kern/kern_linker.c */
48 #define LINKER_HEAD "linker_files"
51 * Get the list of linker files using kvm(3).
52 * Can work with a live kernel as well as with a crash dump.
55 asf_kvm(const char *kernfile, const char *corefile)
57 char errbuf[LINE_MAX];
61 struct linker_file lf;
62 linker_file_list_t linker_files;
66 kd = kvm_openfiles(kernfile, corefile, NULL, O_RDONLY, errbuf);
68 errx(2, "open kernel memory: %s", errbuf);
71 * Locate the head of the linker file list using kernel symbols.
73 strcpy(name, LINKER_HEAD);
74 nl[0].n_name = name; /* can't use LINKER_HEAD here because it's const */
75 nl[1].n_name = NULL; /* terminate the array for kvm_nlist() */
76 switch (kvm_nlist(kd, nl)) {
80 warnx("%s: %s", LINKER_HEAD, kvm_geterr(kd));
85 errx(2, "%s: symbol not found", LINKER_HEAD);
89 * Read the head of the linker file list from kernel memory.
91 n = kvm_read(kd, nl[0].n_value, &linker_files, sizeof(linker_files));
94 if (n != sizeof(linker_files)) {
96 errx(2, "%s: short read", LINKER_HEAD);
100 * Traverse the linker file list starting at its head.
102 for (kp = linker_files.tqh_first; kp; kp = lf.link.tqe_next) {
103 /* Read a linker file structure */
104 n = kvm_read(kd, (u_long)kp, &lf, sizeof(lf));
107 if (n != sizeof(lf)) {
109 errx(2, "kvm: short read");
111 /* Read the name of the file stored separately */
112 bzero(name, sizeof(name));
113 n = kvm_read(kd, (u_long)lf.filename, name, sizeof(name) - 1);
116 if (strcmp(name, KERNFILE) == 0)
118 /* Add this file to our list of linker files */
119 kfile_add(name, lf.address);
124 read_err: /* A common error case */
125 warnx("read kernel memory: %s", kvm_geterr(kd));