1174910Srwatson/*-
2174910Srwatson * Copyright (c) 2007 Robert N. M. Watson
3174910Srwatson * All rights reserved.
4174910Srwatson *
5174910Srwatson * Redistribution and use in source and binary forms, with or without
6174910Srwatson * modification, are permitted provided that the following conditions
7174910Srwatson * are met:
8174910Srwatson * 1. Redistributions of source code must retain the above copyright
9174910Srwatson *    notice, this list of conditions and the following disclaimer.
10174910Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174910Srwatson *    notice, this list of conditions and the following disclaimer in the
12174910Srwatson *    documentation and/or other materials provided with the distribution.
13174910Srwatson *
14174910Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174910Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174910Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174910Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174910Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174910Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174910Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174910Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174910Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174910Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174910Srwatson * SUCH DAMAGE.
25174910Srwatson */
26174910Srwatson
27174910Srwatson/*
28174910Srwatson * DDB capture support: capture kernel debugger output into a fixed-size
29174910Srwatson * buffer for later dumping to disk or extraction from user space.
30174910Srwatson */
31174910Srwatson
32174910Srwatson#include <sys/cdefs.h>
33174910Srwatson__FBSDID("$FreeBSD$");
34174910Srwatson
35175694Srwatson#include "opt_ddb.h"
36175694Srwatson
37174910Srwatson#include <sys/param.h>
38174910Srwatson#include <sys/conf.h>
39174910Srwatson#include <sys/kernel.h>
40174910Srwatson#include <sys/kerneldump.h>
41174910Srwatson#include <sys/malloc.h>
42174910Srwatson#include <sys/msgbuf.h>
43174910Srwatson#include <sys/priv.h>
44174910Srwatson#include <sys/sx.h>
45174910Srwatson#include <sys/sysctl.h>
46174910Srwatson#include <sys/systm.h>
47174910Srwatson
48174910Srwatson#include <ddb/ddb.h>
49174910Srwatson#include <ddb/db_lex.h>
50174910Srwatson
51174910Srwatson/*
52174910Srwatson * While it would be desirable to use a small block-sized buffer and dump
53174910Srwatson * incrementally to disk in fixed-size blocks, it's not possible to enter
54174910Srwatson * kernel dumper routines without restarting the kernel, which is undesirable
55174910Srwatson * in the midst of debugging.  Instead, we maintain a large static global
56174910Srwatson * buffer that we fill from DDB's output routines.
57175694Srwatson *
58175694Srwatson * We enforce an invariant at runtime that buffer sizes are even multiples of
59175694Srwatson * the textdump block size, which is a design choice that we might want to
60175694Srwatson * reconsider.
61174910Srwatson */
62175684Srwatsonstatic MALLOC_DEFINE(M_DDB_CAPTURE, "ddb_capture", "DDB capture buffer");
63174910Srwatson
64175694Srwatson#ifndef DDB_CAPTURE_DEFAULTBUFSIZE
65175684Srwatson#define	DDB_CAPTURE_DEFAULTBUFSIZE	48*1024
66175694Srwatson#endif
67175694Srwatson#ifndef DDB_CAPTURE_MAXBUFSIZE
68175699Srwatson#define	DDB_CAPTURE_MAXBUFSIZE	5*1024*1024
69175694Srwatson#endif
70175684Srwatson#define	DDB_CAPTURE_FILENAME	"ddb.txt"	/* Captured DDB output. */
71174910Srwatson
72174910Srwatsonstatic char *db_capture_buf;
73175684Srwatsonstatic u_int db_capture_bufsize = DDB_CAPTURE_DEFAULTBUFSIZE;
74175684Srwatsonstatic u_int db_capture_maxbufsize = DDB_CAPTURE_MAXBUFSIZE; /* Read-only. */
75174910Srwatsonstatic u_int db_capture_bufoff;		/* Next location to write in buffer. */
76174921Srwatsonstatic u_int db_capture_bufpadding;	/* Amount of zero padding. */
77174910Srwatsonstatic int db_capture_inpager;		/* Suspend capture in pager. */
78174910Srwatsonstatic int db_capture_inprogress;	/* DDB capture currently in progress. */
79174910Srwatson
80174910Srwatsonstruct sx db_capture_sx;		/* Lock against user thread races. */
81174910SrwatsonSX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx");
82174910Srwatson
83174910Srwatsonstatic SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0,
84174910Srwatson    "DDB capture options");
85174910Srwatson
86178492SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bufoff, CTLFLAG_RD,
87174910Srwatson    &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer");
88174910Srwatson
89174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD,
90174910Srwatson    &db_capture_maxbufsize, 0,
91174910Srwatson    "Maximum value for debug.ddb.capture.bufsize");
92174910Srwatson
93217326SmdfSYSCTL_INT(_debug_ddb_capture, OID_AUTO, inprogress, CTLFLAG_RD,
94178492Srwatson    &db_capture_inprogress, 0, "DDB output capture in progress");
95178492Srwatson
96174910Srwatson/*
97175694Srwatson * Boot-time allocation of the DDB capture buffer, if any.  Force all buffer
98175694Srwatson * sizes, including the maximum size, to be rounded to block sizes.
99174921Srwatson */
100174910Srwatsonstatic void
101174910Srwatsondb_capture_sysinit(__unused void *dummy)
102174910Srwatson{
103174910Srwatson
104174910Srwatson	TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
105175694Srwatson	db_capture_maxbufsize = roundup(db_capture_maxbufsize,
106175694Srwatson	    TEXTDUMP_BLOCKSIZE);
107174921Srwatson	db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE);
108175694Srwatson	if (db_capture_bufsize > db_capture_maxbufsize)
109175694Srwatson		db_capture_bufsize = db_capture_maxbufsize;
110174910Srwatson	if (db_capture_bufsize != 0)
111175684Srwatson		db_capture_buf = malloc(db_capture_bufsize, M_DDB_CAPTURE,
112174910Srwatson		    M_WAITOK);
113174910Srwatson}
114174910SrwatsonSYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
115174910Srwatson    NULL);
116174910Srwatson
117174910Srwatson/*
118174910Srwatson * Run-time adjustment of the capture buffer.
119174910Srwatson */
120174910Srwatsonstatic int
121174910Srwatsonsysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
122174910Srwatson{
123174910Srwatson	u_int len, size;
124174910Srwatson	char *buf;
125174910Srwatson	int error;
126174910Srwatson
127174910Srwatson	size = db_capture_bufsize;
128174910Srwatson	error = sysctl_handle_int(oidp, &size, 0, req);
129174910Srwatson	if (error || req->newptr == NULL)
130174910Srwatson		return (error);
131174921Srwatson	size = roundup(size, TEXTDUMP_BLOCKSIZE);
132175694Srwatson	if (size > db_capture_maxbufsize)
133174910Srwatson		return (EINVAL);
134174910Srwatson	sx_xlock(&db_capture_sx);
135174910Srwatson	if (size != 0) {
136174910Srwatson		/*
137174910Srwatson		 * Potentially the buffer is quite large, so if we can't
138174910Srwatson		 * allocate it, fail rather than waiting.
139174910Srwatson		 */
140175684Srwatson		buf = malloc(size, M_DDB_CAPTURE, M_NOWAIT);
141174910Srwatson		if (buf == NULL) {
142174910Srwatson			sx_xunlock(&db_capture_sx);
143174910Srwatson			return (ENOMEM);
144174910Srwatson		}
145174910Srwatson		len = min(db_capture_bufoff, size);
146174910Srwatson	} else {
147174910Srwatson		buf = NULL;
148174910Srwatson		len = 0;
149174910Srwatson	}
150174910Srwatson	if (db_capture_buf != NULL && buf != NULL)
151174910Srwatson		bcopy(db_capture_buf, buf, len);
152174910Srwatson	if (db_capture_buf != NULL)
153175684Srwatson		free(db_capture_buf, M_DDB_CAPTURE);
154174910Srwatson	db_capture_bufoff = len;
155174910Srwatson	db_capture_buf = buf;
156174910Srwatson	db_capture_bufsize = size;
157174910Srwatson	sx_xunlock(&db_capture_sx);
158174910Srwatson
159174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
160174910Srwatson	    ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
161175694Srwatson	KASSERT(db_capture_bufsize <= db_capture_maxbufsize,
162174910Srwatson	    ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
163174910Srwatson
164174910Srwatson	return (0);
165174910Srwatson}
166174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW,
167174910Srwatson    0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
168174910Srwatson    "Size of DDB capture buffer");
169174910Srwatson
170174910Srwatson/*
171174910Srwatson * Sysctl to read out the capture buffer from userspace.  We require
172174910Srwatson * privilege as sensitive process/memory information may be accessed.
173174910Srwatson */
174174910Srwatsonstatic int
175174910Srwatsonsysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
176174910Srwatson{
177174910Srwatson	int error;
178174910Srwatson	char ch;
179174910Srwatson
180174910Srwatson	error = priv_check(req->td, PRIV_DDB_CAPTURE);
181174910Srwatson	if (error)
182174910Srwatson		return (error);
183174910Srwatson
184174910Srwatson	sx_slock(&db_capture_sx);
185174910Srwatson	error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
186174910Srwatson	sx_sunlock(&db_capture_sx);
187174910Srwatson	if (error)
188174910Srwatson		return (error);
189174910Srwatson	ch = '\0';
190174910Srwatson	return (SYSCTL_OUT(req, &ch, sizeof(ch)));
191174910Srwatson}
192174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD,
193174910Srwatson    NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data");
194174910Srwatson
195174910Srwatson/*
196174910Srwatson * Routines for capturing DDB output into a fixed-size buffer.  These are
197174910Srwatson * invoked from DDB's input and output routines.  If we hit the limit on the
198174910Srwatson * buffer, we simply drop further data.
199174910Srwatson */
200174910Srwatsonvoid
201174910Srwatsondb_capture_write(char *buffer, u_int buflen)
202174910Srwatson{
203174910Srwatson	u_int len;
204174910Srwatson
205174910Srwatson	if (db_capture_inprogress == 0 || db_capture_inpager)
206174910Srwatson		return;
207174910Srwatson	len = min(buflen, db_capture_bufsize - db_capture_bufoff);
208174910Srwatson	bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
209174910Srwatson	db_capture_bufoff += len;
210174910Srwatson
211174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
212174910Srwatson	    ("db_capture_write: bufoff > bufsize"));
213174910Srwatson}
214174910Srwatson
215174910Srwatsonvoid
216174910Srwatsondb_capture_writech(char ch)
217174910Srwatson{
218174910Srwatson
219174910Srwatson	return (db_capture_write(&ch, sizeof(ch)));
220174910Srwatson}
221174910Srwatson
222174910Srwatsonvoid
223174910Srwatsondb_capture_enterpager(void)
224174910Srwatson{
225174910Srwatson
226174910Srwatson	db_capture_inpager = 1;
227174910Srwatson}
228174910Srwatson
229174910Srwatsonvoid
230174910Srwatsondb_capture_exitpager(void)
231174910Srwatson{
232174910Srwatson
233174910Srwatson	db_capture_inpager = 0;
234174910Srwatson}
235174910Srwatson
236174910Srwatson/*
237174921Srwatson * Zero out any bytes left in the last block of the DDB capture buffer.  This
238174921Srwatson * is run shortly before writing the blocks to disk, rather than when output
239174921Srwatson * capture is stopped, in order to avoid injecting nul's into the middle of
240174921Srwatson * output.
241174921Srwatson */
242174921Srwatsonstatic void
243174921Srwatsondb_capture_zeropad(void)
244174921Srwatson{
245174921Srwatson	u_int len;
246174921Srwatson
247174921Srwatson	len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize -
248174921Srwatson	    db_capture_bufoff) % TEXTDUMP_BLOCKSIZE);
249174921Srwatson	bzero(db_capture_buf + db_capture_bufoff, len);
250174921Srwatson	db_capture_bufpadding = len;
251174921Srwatson}
252174921Srwatson
253174921Srwatson/*
254174910Srwatson * Reset capture state, which flushes buffers.
255174910Srwatson */
256174910Srwatsonstatic void
257174910Srwatsondb_capture_reset(void)
258174910Srwatson{
259174910Srwatson
260174910Srwatson	db_capture_inprogress = 0;
261174910Srwatson	db_capture_bufoff = 0;
262174921Srwatson	db_capture_bufpadding = 0;
263174910Srwatson}
264174910Srwatson
265174910Srwatson/*
266174910Srwatson * Start capture.  Only one session is allowed at any time, but we may
267174910Srwatson * continue a previous session, so the buffer isn't reset.
268174910Srwatson */
269174910Srwatsonstatic void
270174910Srwatsondb_capture_start(void)
271174910Srwatson{
272174910Srwatson
273174910Srwatson	if (db_capture_inprogress) {
274174910Srwatson		db_printf("Capture already started\n");
275174910Srwatson		return;
276174910Srwatson	}
277174910Srwatson	db_capture_inprogress = 1;
278174910Srwatson}
279174910Srwatson
280174910Srwatson/*
281174921Srwatson * Terminate DDB output capture--real work is deferred to db_capture_dump,
282174921Srwatson * which executes outside of the DDB context.  We don't zero pad here because
283174921Srwatson * capture may be started again before the dump takes place.
284174910Srwatson */
285174910Srwatsonstatic void
286174910Srwatsondb_capture_stop(void)
287174910Srwatson{
288174910Srwatson
289174910Srwatson	if (db_capture_inprogress == 0) {
290174910Srwatson		db_printf("Capture not started\n");
291174910Srwatson		return;
292174910Srwatson	}
293174910Srwatson	db_capture_inprogress = 0;
294174910Srwatson}
295174910Srwatson
296174921Srwatson/*
297174921Srwatson * Dump DDB(4) captured output (and resets capture buffers).
298174921Srwatson */
299174921Srwatsonvoid
300174921Srwatsondb_capture_dump(struct dumperinfo *di)
301174921Srwatson{
302174921Srwatson	u_int offset;
303174921Srwatson
304174921Srwatson	if (db_capture_bufoff == 0)
305174921Srwatson		return;
306174921Srwatson
307174921Srwatson	db_capture_zeropad();
308175684Srwatson	textdump_mkustar(textdump_block_buffer, DDB_CAPTURE_FILENAME,
309174921Srwatson	    db_capture_bufoff);
310174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
311174921Srwatson	for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding;
312174921Srwatson	    offset += TEXTDUMP_BLOCKSIZE)
313174921Srwatson		(void)textdump_writenextblock(di, db_capture_buf + offset);
314174921Srwatson	db_capture_bufoff = 0;
315174921Srwatson	db_capture_bufpadding = 0;
316174921Srwatson}
317174921Srwatson
318174910Srwatson/*-
319174910Srwatson * DDB(4) command to manage capture:
320174910Srwatson *
321174910Srwatson * capture on          - start DDB output capture
322174910Srwatson * capture off         - stop DDB output capture
323174910Srwatson * capture reset       - reset DDB capture buffer (also stops capture)
324174910Srwatson * capture status      - print DDB output capture status
325174910Srwatson */
326174910Srwatsonstatic void
327174910Srwatsondb_capture_usage(void)
328174910Srwatson{
329174910Srwatson
330174910Srwatson	db_error("capture [on|off|reset|status]\n");
331174910Srwatson}
332174910Srwatson
333174910Srwatsonvoid
334174910Srwatsondb_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
335174910Srwatson    char *modif)
336174910Srwatson{
337174910Srwatson	int t;
338174910Srwatson
339174910Srwatson	t = db_read_token();
340174910Srwatson	if (t != tIDENT) {
341174910Srwatson		db_capture_usage();
342174910Srwatson		return;
343174910Srwatson	}
344174910Srwatson	if (db_read_token() != tEOL)
345174910Srwatson		db_error("?\n");
346174910Srwatson	if (strcmp(db_tok_string, "on") == 0)
347174910Srwatson		db_capture_start();
348174910Srwatson	else if (strcmp(db_tok_string, "off") == 0)
349174910Srwatson		db_capture_stop();
350174910Srwatson	else if (strcmp(db_tok_string, "reset") == 0)
351174910Srwatson		db_capture_reset();
352174910Srwatson	else if (strcmp(db_tok_string, "status") == 0) {
353174910Srwatson		db_printf("%u/%u bytes used\n", db_capture_bufoff,
354174910Srwatson		    db_capture_bufsize);
355174910Srwatson		if (db_capture_inprogress)
356174910Srwatson			db_printf("capture is on\n");
357174910Srwatson		else
358174910Srwatson			db_printf("capture is off\n");
359174910Srwatson	} else
360174910Srwatson		db_capture_usage();
361174910Srwatson}
362