]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/asf/asf_kvm.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / usr.sbin / asf / asf_kvm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 The FreeBSD Project
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>  /* for <sys/linker.h> with _KERNEL defined */
35 #include <err.h>
36 #include <fcntl.h>
37 #include <kvm.h>
38 #include <limits.h>
39 #include <nlist.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #define _KERNEL
45 #include <sys/linker.h>
46 #undef  _KERNEL
47
48 #include "asf.h"
49
50 /* Name of the head of the linker file list in /sys/kern/kern_linker.c */
51 #define LINKER_HEAD     "linker_files"
52
53 /*
54  * Get the list of linker files using kvm(3).
55  * Can work with a live kernel as well as with a crash dump.
56  */
57 void
58 asf_kvm(const char *kernfile, const char *corefile)
59 {
60         char errbuf[LINE_MAX];
61         char name[PATH_MAX];
62         kvm_t *kd;
63         struct nlist nl[2];
64         struct linker_file lf;
65         linker_file_list_t linker_files;
66         ssize_t n;
67         void *kp;
68
69         kd = kvm_openfiles(kernfile, corefile, NULL, O_RDONLY, errbuf);
70         if (kd == NULL)
71                 errx(2, "open kernel memory: %s", errbuf);
72
73         /*
74          * Locate the head of the linker file list using kernel symbols.
75          */
76         strcpy(name, LINKER_HEAD);
77         nl[0].n_name = name; /* can't use LINKER_HEAD here because it's const */
78         nl[1].n_name = NULL; /* terminate the array for kvm_nlist() */
79         switch (kvm_nlist(kd, nl)) {
80         case 0:
81                 break;
82         case -1:
83                 warnx("%s: %s", LINKER_HEAD, kvm_geterr(kd));
84                 kvm_close(kd);
85                 exit(2);
86         default:
87                 kvm_close(kd);
88                 errx(2, "%s: symbol not found", LINKER_HEAD);
89         }
90
91         /*
92          * Read the head of the linker file list from kernel memory.
93          */
94         n = kvm_read(kd, nl[0].n_value, &linker_files, sizeof(linker_files));
95         if (n == -1)
96                 goto read_err;
97         if (n != sizeof(linker_files)) {
98                 kvm_close(kd);
99                 errx(2, "%s: short read", LINKER_HEAD);
100         }
101
102         /*
103          * Traverse the linker file list starting at its head.
104          */
105         for (kp = linker_files.tqh_first; kp; kp = lf.link.tqe_next) {
106                 /* Read a linker file structure */
107                 n = kvm_read(kd, (u_long)kp, &lf, sizeof(lf));
108                 if (n == -1)
109                         goto read_err;
110                 if (n != sizeof(lf)) {
111                         kvm_close(kd);
112                         errx(2, "kvm: short read");
113                 }
114                 /* Read the name of the file stored separately */
115                 bzero(name, sizeof(name));
116                 n = kvm_read(kd, (u_long)lf.filename, name, sizeof(name) - 1);
117                 if (n == -1)
118                         goto read_err;
119                 if (strcmp(name, KERNFILE) == 0)
120                         continue;
121                 /* Add this file to our list of linker files */
122                 kfile_add(name, lf.address);
123         }
124         kvm_close(kd);
125         return;
126
127 read_err:       /* A common error case */
128         warnx("read kernel memory: %s", kvm_geterr(kd));
129         kvm_close(kd);
130         exit(2);
131 }