xref: /freebsd/sys/ddb/db_capture.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * DDB capture support: capture kernel debugger output into a fixed-size
29  * buffer for later dumping to disk or extraction from user space.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_ddb.h"
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/malloc.h>
42 #include <sys/msgbuf.h>
43 #include <sys/priv.h>
44 #include <sys/sx.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 
48 #include <ddb/ddb.h>
49 #include <ddb/db_lex.h>
50 
51 /*
52  * While it would be desirable to use a small block-sized buffer and dump
53  * incrementally to disk in fixed-size blocks, it's not possible to enter
54  * kernel dumper routines without restarting the kernel, which is undesirable
55  * in the midst of debugging.  Instead, we maintain a large static global
56  * buffer that we fill from DDB's output routines.
57  *
58  * We enforce an invariant at runtime that buffer sizes are even multiples of
59  * the textdump block size, which is a design choice that we might want to
60  * reconsider.
61  */
62 static MALLOC_DEFINE(M_DDB_CAPTURE, "ddb_capture", "DDB capture buffer");
63 
64 #ifndef DDB_CAPTURE_DEFAULTBUFSIZE
65 #define	DDB_CAPTURE_DEFAULTBUFSIZE	48*1024
66 #endif
67 #ifndef DDB_CAPTURE_MAXBUFSIZE
68 #define	DDB_CAPTURE_MAXBUFSIZE	5*1024*1024
69 #endif
70 #define	DDB_CAPTURE_FILENAME	"ddb.txt"	/* Captured DDB output. */
71 
72 static char *db_capture_buf;
73 static u_int db_capture_bufsize = DDB_CAPTURE_DEFAULTBUFSIZE;
74 static u_int db_capture_maxbufsize = DDB_CAPTURE_MAXBUFSIZE; /* Read-only. */
75 static u_int db_capture_bufoff;		/* Next location to write in buffer. */
76 static u_int db_capture_bufpadding;	/* Amount of zero padding. */
77 static int db_capture_inpager;		/* Suspend capture in pager. */
78 static int db_capture_inprogress;	/* DDB capture currently in progress. */
79 
80 struct sx db_capture_sx;		/* Lock against user thread races. */
81 SX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx");
82 
83 static SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0,
84     "DDB capture options");
85 
86 SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD,
87     &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer");
88 
89 SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD,
90     &db_capture_maxbufsize, 0,
91     "Maximum value for debug.ddb.capture.bufsize");
92 
93 /*
94  * Boot-time allocation of the DDB capture buffer, if any.  Force all buffer
95  * sizes, including the maximum size, to be rounded to block sizes.
96  */
97 static void
98 db_capture_sysinit(__unused void *dummy)
99 {
100 
101 	TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
102 	db_capture_maxbufsize = roundup(db_capture_maxbufsize,
103 	    TEXTDUMP_BLOCKSIZE);
104 	db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE);
105 	if (db_capture_bufsize > db_capture_maxbufsize)
106 		db_capture_bufsize = db_capture_maxbufsize;
107 	if (db_capture_bufsize != 0)
108 		db_capture_buf = malloc(db_capture_bufsize, M_DDB_CAPTURE,
109 		    M_WAITOK);
110 }
111 SYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
112     NULL);
113 
114 /*
115  * Run-time adjustment of the capture buffer.
116  */
117 static int
118 sysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
119 {
120 	u_int len, size;
121 	char *buf;
122 	int error;
123 
124 	size = db_capture_bufsize;
125 	error = sysctl_handle_int(oidp, &size, 0, req);
126 	if (error || req->newptr == NULL)
127 		return (error);
128 	size = roundup(size, TEXTDUMP_BLOCKSIZE);
129 	if (size > db_capture_maxbufsize)
130 		return (EINVAL);
131 	sx_xlock(&db_capture_sx);
132 	if (size != 0) {
133 		/*
134 		 * Potentially the buffer is quite large, so if we can't
135 		 * allocate it, fail rather than waiting.
136 		 */
137 		buf = malloc(size, M_DDB_CAPTURE, M_NOWAIT);
138 		if (buf == NULL) {
139 			sx_xunlock(&db_capture_sx);
140 			return (ENOMEM);
141 		}
142 		len = min(db_capture_bufoff, size);
143 	} else {
144 		buf = NULL;
145 		len = 0;
146 	}
147 	if (db_capture_buf != NULL && buf != NULL)
148 		bcopy(db_capture_buf, buf, len);
149 	if (db_capture_buf != NULL)
150 		free(db_capture_buf, M_DDB_CAPTURE);
151 	db_capture_bufoff = len;
152 	db_capture_buf = buf;
153 	db_capture_bufsize = size;
154 	sx_xunlock(&db_capture_sx);
155 
156 	KASSERT(db_capture_bufoff <= db_capture_bufsize,
157 	    ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
158 	KASSERT(db_capture_bufsize <= db_capture_maxbufsize,
159 	    ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
160 
161 	return (0);
162 }
163 SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW,
164     0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
165     "Size of DDB capture buffer");
166 
167 /*
168  * Sysctl to read out the capture buffer from userspace.  We require
169  * privilege as sensitive process/memory information may be accessed.
170  */
171 static int
172 sysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
173 {
174 	int error;
175 	char ch;
176 
177 	error = priv_check(req->td, PRIV_DDB_CAPTURE);
178 	if (error)
179 		return (error);
180 
181 	sx_slock(&db_capture_sx);
182 	error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
183 	sx_sunlock(&db_capture_sx);
184 	if (error)
185 		return (error);
186 	ch = '\0';
187 	return (SYSCTL_OUT(req, &ch, sizeof(ch)));
188 }
189 SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD,
190     NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data");
191 
192 /*
193  * Routines for capturing DDB output into a fixed-size buffer.  These are
194  * invoked from DDB's input and output routines.  If we hit the limit on the
195  * buffer, we simply drop further data.
196  */
197 void
198 db_capture_write(char *buffer, u_int buflen)
199 {
200 	u_int len;
201 
202 	if (db_capture_inprogress == 0 || db_capture_inpager)
203 		return;
204 	len = min(buflen, db_capture_bufsize - db_capture_bufoff);
205 	bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
206 	db_capture_bufoff += len;
207 
208 	KASSERT(db_capture_bufoff <= db_capture_bufsize,
209 	    ("db_capture_write: bufoff > bufsize"));
210 }
211 
212 void
213 db_capture_writech(char ch)
214 {
215 
216 	return (db_capture_write(&ch, sizeof(ch)));
217 }
218 
219 void
220 db_capture_enterpager(void)
221 {
222 
223 	db_capture_inpager = 1;
224 }
225 
226 void
227 db_capture_exitpager(void)
228 {
229 
230 	db_capture_inpager = 0;
231 }
232 
233 /*
234  * Zero out any bytes left in the last block of the DDB capture buffer.  This
235  * is run shortly before writing the blocks to disk, rather than when output
236  * capture is stopped, in order to avoid injecting nul's into the middle of
237  * output.
238  */
239 static void
240 db_capture_zeropad(void)
241 {
242 	u_int len;
243 
244 	len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize -
245 	    db_capture_bufoff) % TEXTDUMP_BLOCKSIZE);
246 	bzero(db_capture_buf + db_capture_bufoff, len);
247 	db_capture_bufpadding = len;
248 }
249 
250 /*
251  * Reset capture state, which flushes buffers.
252  */
253 static void
254 db_capture_reset(void)
255 {
256 
257 	db_capture_inprogress = 0;
258 	db_capture_bufoff = 0;
259 	db_capture_bufpadding = 0;
260 }
261 
262 /*
263  * Start capture.  Only one session is allowed at any time, but we may
264  * continue a previous session, so the buffer isn't reset.
265  */
266 static void
267 db_capture_start(void)
268 {
269 
270 	if (db_capture_inprogress) {
271 		db_printf("Capture already started\n");
272 		return;
273 	}
274 	db_capture_inprogress = 1;
275 }
276 
277 /*
278  * Terminate DDB output capture--real work is deferred to db_capture_dump,
279  * which executes outside of the DDB context.  We don't zero pad here because
280  * capture may be started again before the dump takes place.
281  */
282 static void
283 db_capture_stop(void)
284 {
285 
286 	if (db_capture_inprogress == 0) {
287 		db_printf("Capture not started\n");
288 		return;
289 	}
290 	db_capture_inprogress = 0;
291 }
292 
293 /*
294  * Dump DDB(4) captured output (and resets capture buffers).
295  */
296 void
297 db_capture_dump(struct dumperinfo *di)
298 {
299 	u_int offset;
300 
301 	if (db_capture_bufoff == 0)
302 		return;
303 
304 	db_capture_zeropad();
305 	textdump_mkustar(textdump_block_buffer, DDB_CAPTURE_FILENAME,
306 	    db_capture_bufoff);
307 	(void)textdump_writenextblock(di, textdump_block_buffer);
308 	for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding;
309 	    offset += TEXTDUMP_BLOCKSIZE)
310 		(void)textdump_writenextblock(di, db_capture_buf + offset);
311 	db_capture_bufoff = 0;
312 	db_capture_bufpadding = 0;
313 }
314 
315 /*-
316  * DDB(4) command to manage capture:
317  *
318  * capture on          - start DDB output capture
319  * capture off         - stop DDB output capture
320  * capture reset       - reset DDB capture buffer (also stops capture)
321  * capture status      - print DDB output capture status
322  */
323 static void
324 db_capture_usage(void)
325 {
326 
327 	db_error("capture [on|off|reset|status]\n");
328 }
329 
330 void
331 db_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
332     char *modif)
333 {
334 	int t;
335 
336 	t = db_read_token();
337 	if (t != tIDENT) {
338 		db_capture_usage();
339 		return;
340 	}
341 	if (db_read_token() != tEOL)
342 		db_error("?\n");
343 	if (strcmp(db_tok_string, "on") == 0)
344 		db_capture_start();
345 	else if (strcmp(db_tok_string, "off") == 0)
346 		db_capture_stop();
347 	else if (strcmp(db_tok_string, "reset") == 0)
348 		db_capture_reset();
349 	else if (strcmp(db_tok_string, "status") == 0) {
350 		db_printf("%u/%u bytes used\n", db_capture_bufoff,
351 		    db_capture_bufsize);
352 		if (db_capture_inprogress)
353 			db_printf("capture is on\n");
354 		else
355 			db_printf("capture is off\n");
356 	} else
357 		db_capture_usage();
358 }
359