xref: /freebsd/sbin/ddb/ddb_capture.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Robert N. M. Watson
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 #include <sys/types.h>
31 #include <sys/sysctl.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <kvm.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43 
44 #include "ddb.h"
45 
46 /*
47  * Interface with the ddb(4) capture buffer of a live kernel using sysctl, or
48  * for a crash dump using libkvm.
49  */
50 #define	SYSCTL_DDB_CAPTURE_BUFOFF	"debug.ddb.capture.bufoff"
51 #define	SYSCTL_DDB_CAPTURE_BUFSIZE	"debug.ddb.capture.bufsize"
52 #define	SYSCTL_DDB_CAPTURE_MAXBUFSIZE	"debug.ddb.capture.maxbufsize"
53 #define	SYSCTL_DDB_CAPTURE_DATA		"debug.ddb.capture.data"
54 #define	SYSCTL_DDB_CAPTURE_INPROGRESS	"debug.ddb.capture.inprogress"
55 
56 static struct nlist namelist[] = {
57 #define X_DB_CAPTURE_BUF	0
58 	{ .n_name = "_db_capture_buf" },
59 #define X_DB_CAPTURE_BUFSIZE	1
60 	{ .n_name = "_db_capture_bufsize" },
61 #define X_DB_CAPTURE_MAXBUFSIZE	2
62 	{ .n_name = "_db_capture_maxbufsize" },
63 #define X_DB_CAPTURE_BUFOFF	3
64 	{ .n_name = "_db_capture_bufoff" },
65 #define	X_DB_CAPTURE_INPROGRESS	4
66 	{ .n_name = "_db_capture_inprogress" },
67 	{ .n_name = "" },
68 };
69 
70 static int
71 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
72     size_t offset)
73 {
74 	ssize_t ret;
75 
76 	ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
77 	    size);
78 	if (ret < 0 || (size_t)ret != size)
79 		return (-1);
80 	return (0);
81 }
82 
83 static int
84 kread_symbol(kvm_t *kvm, int read_index, void *address, size_t size,
85     size_t offset)
86 {
87 	ssize_t ret;
88 
89 	ret = kvm_read(kvm, namelist[read_index].n_value + offset, address, size);
90 	if (ret < 0 || (size_t)ret != size)
91 		return (-1);
92 	return (0);
93 }
94 
95 static void
96 ddb_capture_print_kvm(kvm_t *kvm)
97 {
98 	u_int db_capture_bufoff;
99 	char *buffer, *db_capture_buf;
100 
101 	if (kread_symbol(kvm, X_DB_CAPTURE_BUF, &db_capture_buf,
102 	    sizeof(db_capture_buf), 0) < 0)
103 		errx(-1, "kvm: unable to read db_capture_buf");
104 
105 	if (kread_symbol(kvm, X_DB_CAPTURE_BUFOFF, &db_capture_bufoff,
106 	    sizeof(db_capture_bufoff), 0) < 0)
107 		errx(-1, "kvm: unable to read db_capture_bufoff");
108 
109 	buffer = malloc(db_capture_bufoff + 1);
110 	if (buffer == NULL)
111 		err(-1, "malloc: db_capture_bufoff (%u)",
112 		    db_capture_bufoff);
113 	bzero(buffer, db_capture_bufoff + 1);
114 
115 	if (kread(kvm, db_capture_buf, buffer, db_capture_bufoff, 0) < 0)
116 		errx(-1, "kvm: unable to read buffer");
117 
118 	printf("%s\n", buffer);
119 	free(buffer);
120 }
121 
122 static void
123 ddb_capture_print_sysctl(void)
124 {
125 	size_t buflen, len;
126 	char *buffer;
127 	int ret;
128 
129 repeat:
130 	if (sysctlbyname(SYSCTL_DDB_CAPTURE_DATA, NULL, &buflen, NULL, 0) < 0)
131 		err(EX_OSERR, "sysctl: %s", SYSCTL_DDB_CAPTURE_DATA);
132 	if (buflen == 0)
133 		return;
134 	buffer = malloc(buflen);
135 	if (buffer == NULL)
136 		err(EX_OSERR, "malloc");
137 	bzero(buffer, buflen);
138 	len = buflen;
139 	ret = sysctlbyname(SYSCTL_DDB_CAPTURE_DATA, buffer, &len, NULL, 0);
140 	if (ret < 0 && errno != ENOMEM)
141 		err(EX_OSERR, "sysctl: %s", SYSCTL_DDB_CAPTURE_DATA);
142 	if (ret < 0) {
143 		free(buffer);
144 		goto repeat;
145 	}
146 
147 	printf("%s\n", buffer);
148 	free(buffer);
149 }
150 
151 static void
152 ddb_capture_status_kvm(kvm_t *kvm)
153 {
154 	u_int db_capture_bufoff, db_capture_bufsize, db_capture_inprogress;
155 
156 	if (kread_symbol(kvm, X_DB_CAPTURE_BUFOFF, &db_capture_bufoff,
157 	    sizeof(db_capture_bufoff), 0) < 0)
158 		errx(-1, "kvm: unable to read db_capture_bufoff");
159 	if (kread_symbol(kvm, X_DB_CAPTURE_BUFSIZE, &db_capture_bufsize,
160 	    sizeof(db_capture_bufsize), 0) < 0)
161 		errx(-1, "kvm: unable to read db_capture_bufsize");
162 	if (kread_symbol(kvm, X_DB_CAPTURE_INPROGRESS,
163 	    &db_capture_inprogress, sizeof(db_capture_inprogress), 0) < 0)
164 		err(-1, "kvm: unable to read db_capture_inprogress");
165 	printf("%u/%u bytes used\n", db_capture_bufoff, db_capture_bufsize);
166 	if (db_capture_inprogress)
167 		printf("capture is on\n");
168 	else
169 		printf("capture is off\n");
170 
171 }
172 
173 static void
174 ddb_capture_status_sysctl(void)
175 {
176 	u_int db_capture_bufoff, db_capture_bufsize, db_capture_inprogress;
177 	size_t len;
178 
179 	len = sizeof(db_capture_bufoff);
180 	if (sysctlbyname(SYSCTL_DDB_CAPTURE_BUFOFF, &db_capture_bufoff, &len,
181 	    NULL, 0) < 0)
182 		err(EX_OSERR, "sysctl: %s", SYSCTL_DDB_CAPTURE_BUFOFF);
183 	len = sizeof(db_capture_bufoff);
184 	if (sysctlbyname(SYSCTL_DDB_CAPTURE_BUFSIZE, &db_capture_bufsize,
185 	    &len, NULL, 0) < 0)
186 		err(EX_OSERR, "sysctl: %s", SYSCTL_DDB_CAPTURE_BUFSIZE);
187 	len = sizeof(db_capture_inprogress);
188 	if (sysctlbyname(SYSCTL_DDB_CAPTURE_INPROGRESS,
189 	    &db_capture_inprogress, &len, NULL, 0) < 0)
190 		err(EX_OSERR, "sysctl: %s", SYSCTL_DDB_CAPTURE_INPROGRESS);
191 	printf("%u/%u bytes used\n", db_capture_bufoff, db_capture_bufsize);
192 	if (db_capture_inprogress)
193 		printf("capture is on\n");
194 	else
195 		printf("capture is off\n");
196 }
197 
198 void
199 ddb_capture(int argc, char *argv[])
200 {
201 	char *mflag, *nflag, errbuf[_POSIX2_LINE_MAX];
202 	kvm_t *kvm;
203 	int ch;
204 
205 	mflag = NULL;
206 	nflag = NULL;
207 	kvm = NULL;
208 	while ((ch = getopt(argc, argv, "M:N:")) != -1) {
209 		switch (ch) {
210 		case 'M':
211 			mflag = optarg;
212 			break;
213 
214 		case 'N':
215 			nflag = optarg;
216 			break;
217 
218 		default:
219 			usage();
220 		}
221 	}
222 	argc -= optind;
223 	argv += optind;
224 
225 	if (argc != 1)
226 		usage();
227 
228 	if (mflag != NULL) {
229 		kvm = kvm_openfiles(nflag, mflag, NULL, O_RDONLY, errbuf);
230 		if (kvm == NULL)
231 			errx(-1, "ddb_capture: kvm_openfiles: %s", errbuf);
232 		if (kvm_nlist(kvm, namelist) != 0)
233 			errx(-1, "ddb_capture: kvm_nlist");
234 	} else if (nflag != NULL)
235 		usage();
236 	if (strcmp(argv[0], "print") == 0) {
237 		if (kvm != NULL)
238 			ddb_capture_print_kvm(kvm);
239 		else
240 			ddb_capture_print_sysctl();
241 	} else if (strcmp(argv[0], "status") == 0) {
242 		if (kvm != NULL)
243 			ddb_capture_status_kvm(kvm);
244 		else
245 			ddb_capture_status_sysctl();
246 	} else
247 		usage();
248 }
249