2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * DDB capture support: capture kernel debugger output into a fixed-size
31 * buffer for later dumping to disk or extraction from user space.
34 #include <sys/cdefs.h>
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/malloc.h>
42 #include <sys/msgbuf.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
49 #include <ddb/db_lex.h>
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.
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
62 static MALLOC_DEFINE(M_DDB_CAPTURE, "ddb_capture", "DDB capture buffer");
64 #ifndef DDB_CAPTURE_DEFAULTBUFSIZE
65 #define DDB_CAPTURE_DEFAULTBUFSIZE 48*1024
67 #ifndef DDB_CAPTURE_MAXBUFSIZE
68 #define DDB_CAPTURE_MAXBUFSIZE 5*1024*1024
70 #define DDB_CAPTURE_FILENAME "ddb.txt" /* Captured DDB output. */
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. */
80 struct sx db_capture_sx; /* Lock against user thread races. */
81 SX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx");
83 static SYSCTL_NODE(_debug_ddb, OID_AUTO, capture,
84 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
85 "DDB capture options");
87 SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bufoff, CTLFLAG_RD,
88 &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer");
90 SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD,
91 &db_capture_maxbufsize, 0,
92 "Maximum value for debug.ddb.capture.bufsize");
94 SYSCTL_INT(_debug_ddb_capture, OID_AUTO, inprogress, CTLFLAG_RD,
95 &db_capture_inprogress, 0, "DDB output capture in progress");
98 * Boot-time allocation of the DDB capture buffer, if any. Force all buffer
99 * sizes, including the maximum size, to be rounded to block sizes.
102 db_capture_sysinit(__unused void *dummy)
105 TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
106 db_capture_maxbufsize = roundup(db_capture_maxbufsize,
108 db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE);
109 if (db_capture_bufsize > db_capture_maxbufsize)
110 db_capture_bufsize = db_capture_maxbufsize;
111 if (db_capture_bufsize != 0)
112 db_capture_buf = malloc(db_capture_bufsize, M_DDB_CAPTURE,
115 SYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
119 * Run-time adjustment of the capture buffer.
122 sysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
128 size = db_capture_bufsize;
129 error = sysctl_handle_int(oidp, &size, 0, req);
130 if (error || req->newptr == NULL)
132 size = roundup(size, TEXTDUMP_BLOCKSIZE);
133 if (size > db_capture_maxbufsize)
135 sx_xlock(&db_capture_sx);
138 * Potentially the buffer is quite large, so if we can't
139 * allocate it, fail rather than waiting.
141 buf = malloc(size, M_DDB_CAPTURE, M_NOWAIT);
143 sx_xunlock(&db_capture_sx);
146 len = min(db_capture_bufoff, size);
151 if (db_capture_buf != NULL && buf != NULL)
152 bcopy(db_capture_buf, buf, len);
153 if (db_capture_buf != NULL)
154 free(db_capture_buf, M_DDB_CAPTURE);
155 db_capture_bufoff = len;
156 db_capture_buf = buf;
157 db_capture_bufsize = size;
158 sx_xunlock(&db_capture_sx);
160 KASSERT(db_capture_bufoff <= db_capture_bufsize,
161 ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
162 KASSERT(db_capture_bufsize <= db_capture_maxbufsize,
163 ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
167 SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize,
168 CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
169 0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
170 "Size of DDB capture buffer");
173 * Sysctl to read out the capture buffer from userspace. We require
174 * privilege as sensitive process/memory information may be accessed.
177 sysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
182 error = priv_check(req->td, PRIV_DDB_CAPTURE);
186 sx_slock(&db_capture_sx);
187 error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
188 sx_sunlock(&db_capture_sx);
192 return (SYSCTL_OUT(req, &ch, sizeof(ch)));
194 SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data,
195 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
196 sysctl_debug_ddb_capture_data, "A",
200 * Routines for capturing DDB output into a fixed-size buffer. These are
201 * invoked from DDB's input and output routines. If we hit the limit on the
202 * buffer, we simply drop further data.
205 db_capture_write(char *buffer, u_int buflen)
209 if (db_capture_inprogress == 0 || db_capture_inpager)
211 len = min(buflen, db_capture_bufsize - db_capture_bufoff);
212 bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
213 db_capture_bufoff += len;
215 KASSERT(db_capture_bufoff <= db_capture_bufsize,
216 ("db_capture_write: bufoff > bufsize"));
220 db_capture_writech(char ch)
223 return (db_capture_write(&ch, sizeof(ch)));
227 db_capture_enterpager(void)
230 db_capture_inpager = 1;
234 db_capture_exitpager(void)
237 db_capture_inpager = 0;
241 * Zero out any bytes left in the last block of the DDB capture buffer. This
242 * is run shortly before writing the blocks to disk, rather than when output
243 * capture is stopped, in order to avoid injecting nul's into the middle of
247 db_capture_zeropad(void)
251 len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize -
252 db_capture_bufoff) % TEXTDUMP_BLOCKSIZE);
253 bzero(db_capture_buf + db_capture_bufoff, len);
254 db_capture_bufpadding = len;
258 * Reset capture state, which flushes buffers.
261 db_capture_reset(void)
264 db_capture_inprogress = 0;
265 db_capture_bufoff = 0;
266 db_capture_bufpadding = 0;
270 * Start capture. Only one session is allowed at any time, but we may
271 * continue a previous session, so the buffer isn't reset.
274 db_capture_start(void)
277 if (db_capture_inprogress) {
278 db_printf("Capture already started\n");
281 db_capture_inprogress = 1;
285 * Terminate DDB output capture--real work is deferred to db_capture_dump,
286 * which executes outside of the DDB context. We don't zero pad here because
287 * capture may be started again before the dump takes place.
290 db_capture_stop(void)
293 if (db_capture_inprogress == 0) {
294 db_printf("Capture not started\n");
297 db_capture_inprogress = 0;
301 * Dump DDB(4) captured output (and resets capture buffers).
304 db_capture_dump(struct dumperinfo *di)
308 if (db_capture_bufoff == 0)
311 db_capture_zeropad();
312 textdump_mkustar(textdump_block_buffer, DDB_CAPTURE_FILENAME,
314 (void)textdump_writenextblock(di, textdump_block_buffer);
315 for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding;
316 offset += TEXTDUMP_BLOCKSIZE)
317 (void)textdump_writenextblock(di, db_capture_buf + offset);
318 db_capture_bufoff = 0;
319 db_capture_bufpadding = 0;
323 * DDB(4) command to manage capture:
325 * capture on - start DDB output capture
326 * capture off - stop DDB output capture
327 * capture reset - reset DDB capture buffer (also stops capture)
328 * capture status - print DDB output capture status
331 db_capture_usage(void)
334 db_error("capture [on|off|reset|status]\n");
338 db_capture_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
347 if (db_read_token() != tEOL)
349 if (strcmp(db_tok_string, "on") == 0)
351 else if (strcmp(db_tok_string, "off") == 0)
353 else if (strcmp(db_tok_string, "reset") == 0)
355 else if (strcmp(db_tok_string, "status") == 0) {
356 db_printf("%u/%u bytes used\n", db_capture_bufoff,
358 if (db_capture_inprogress)
359 db_printf("capture is on\n");
361 db_printf("capture is off\n");