db_capture.c revision 175694
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: head/sys/ddb/db_capture.c 175694 2008-01-26 22:32:23Z rwatson $");
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
68175684Srwatson#define	DDB_CAPTURE_MAXBUFSIZE	512*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
86174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, 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
93174910Srwatson/*
94175694Srwatson * Boot-time allocation of the DDB capture buffer, if any.  Force all buffer
95175694Srwatson * sizes, including the maximum size, to be rounded to block sizes.
96174921Srwatson */
97174910Srwatsonstatic void
98174910Srwatsondb_capture_sysinit(__unused void *dummy)
99174910Srwatson{
100174910Srwatson
101174910Srwatson	TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
102175694Srwatson	db_capture_maxbufsize = roundup(db_capture_maxbufsize,
103175694Srwatson	    TEXTDUMP_BLOCKSIZE);
104174921Srwatson	db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE);
105175694Srwatson	if (db_capture_bufsize > db_capture_maxbufsize)
106175694Srwatson		db_capture_bufsize = db_capture_maxbufsize;
107174910Srwatson	if (db_capture_bufsize != 0)
108175684Srwatson		db_capture_buf = malloc(db_capture_bufsize, M_DDB_CAPTURE,
109174910Srwatson		    M_WAITOK);
110174910Srwatson}
111174910SrwatsonSYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
112174910Srwatson    NULL);
113174910Srwatson
114174910Srwatson/*
115174910Srwatson * Run-time adjustment of the capture buffer.
116174910Srwatson */
117174910Srwatsonstatic int
118174910Srwatsonsysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
119174910Srwatson{
120174910Srwatson	u_int len, size;
121174910Srwatson	char *buf;
122174910Srwatson	int error;
123174910Srwatson
124174910Srwatson	size = db_capture_bufsize;
125174910Srwatson	error = sysctl_handle_int(oidp, &size, 0, req);
126174910Srwatson	if (error || req->newptr == NULL)
127174910Srwatson		return (error);
128174921Srwatson	size = roundup(size, TEXTDUMP_BLOCKSIZE);
129175694Srwatson	if (size > db_capture_maxbufsize)
130174910Srwatson		return (EINVAL);
131174910Srwatson	sx_xlock(&db_capture_sx);
132174910Srwatson	if (size != 0) {
133174910Srwatson		/*
134174910Srwatson		 * Potentially the buffer is quite large, so if we can't
135174910Srwatson		 * allocate it, fail rather than waiting.
136174910Srwatson		 */
137175684Srwatson		buf = malloc(size, M_DDB_CAPTURE, M_NOWAIT);
138174910Srwatson		if (buf == NULL) {
139174910Srwatson			sx_xunlock(&db_capture_sx);
140174910Srwatson			return (ENOMEM);
141174910Srwatson		}
142174910Srwatson		len = min(db_capture_bufoff, size);
143174910Srwatson	} else {
144174910Srwatson		buf = NULL;
145174910Srwatson		len = 0;
146174910Srwatson	}
147174910Srwatson	if (db_capture_buf != NULL && buf != NULL)
148174910Srwatson		bcopy(db_capture_buf, buf, len);
149174910Srwatson	if (db_capture_buf != NULL)
150175684Srwatson		free(db_capture_buf, M_DDB_CAPTURE);
151174910Srwatson	db_capture_bufoff = len;
152174910Srwatson	db_capture_buf = buf;
153174910Srwatson	db_capture_bufsize = size;
154174910Srwatson	sx_xunlock(&db_capture_sx);
155174910Srwatson
156174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
157174910Srwatson	    ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
158175694Srwatson	KASSERT(db_capture_bufsize <= db_capture_maxbufsize,
159174910Srwatson	    ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
160174910Srwatson
161174910Srwatson	return (0);
162174910Srwatson}
163174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW,
164174910Srwatson    0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
165174910Srwatson    "Size of DDB capture buffer");
166174910Srwatson
167174910Srwatson/*
168174910Srwatson * Sysctl to read out the capture buffer from userspace.  We require
169174910Srwatson * privilege as sensitive process/memory information may be accessed.
170174910Srwatson */
171174910Srwatsonstatic int
172174910Srwatsonsysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
173174910Srwatson{
174174910Srwatson	int error;
175174910Srwatson	char ch;
176174910Srwatson
177174910Srwatson	error = priv_check(req->td, PRIV_DDB_CAPTURE);
178174910Srwatson	if (error)
179174910Srwatson		return (error);
180174910Srwatson
181174910Srwatson	sx_slock(&db_capture_sx);
182174910Srwatson	error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
183174910Srwatson	sx_sunlock(&db_capture_sx);
184174910Srwatson	if (error)
185174910Srwatson		return (error);
186174910Srwatson	ch = '\0';
187174910Srwatson	return (SYSCTL_OUT(req, &ch, sizeof(ch)));
188174910Srwatson}
189174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD,
190174910Srwatson    NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data");
191174910Srwatson
192174910Srwatson/*
193174910Srwatson * Routines for capturing DDB output into a fixed-size buffer.  These are
194174910Srwatson * invoked from DDB's input and output routines.  If we hit the limit on the
195174910Srwatson * buffer, we simply drop further data.
196174910Srwatson */
197174910Srwatsonvoid
198174910Srwatsondb_capture_write(char *buffer, u_int buflen)
199174910Srwatson{
200174910Srwatson	u_int len;
201174910Srwatson
202174910Srwatson	if (db_capture_inprogress == 0 || db_capture_inpager)
203174910Srwatson		return;
204174910Srwatson	len = min(buflen, db_capture_bufsize - db_capture_bufoff);
205174910Srwatson	bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
206174910Srwatson	db_capture_bufoff += len;
207174910Srwatson
208174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
209174910Srwatson	    ("db_capture_write: bufoff > bufsize"));
210174910Srwatson}
211174910Srwatson
212174910Srwatsonvoid
213174910Srwatsondb_capture_writech(char ch)
214174910Srwatson{
215174910Srwatson
216174910Srwatson	return (db_capture_write(&ch, sizeof(ch)));
217174910Srwatson}
218174910Srwatson
219174910Srwatsonvoid
220174910Srwatsondb_capture_enterpager(void)
221174910Srwatson{
222174910Srwatson
223174910Srwatson	db_capture_inpager = 1;
224174910Srwatson}
225174910Srwatson
226174910Srwatsonvoid
227174910Srwatsondb_capture_exitpager(void)
228174910Srwatson{
229174910Srwatson
230174910Srwatson	db_capture_inpager = 0;
231174910Srwatson}
232174910Srwatson
233174910Srwatson/*
234174921Srwatson * Zero out any bytes left in the last block of the DDB capture buffer.  This
235174921Srwatson * is run shortly before writing the blocks to disk, rather than when output
236174921Srwatson * capture is stopped, in order to avoid injecting nul's into the middle of
237174921Srwatson * output.
238174921Srwatson */
239174921Srwatsonstatic void
240174921Srwatsondb_capture_zeropad(void)
241174921Srwatson{
242174921Srwatson	u_int len;
243174921Srwatson
244174921Srwatson	len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize -
245174921Srwatson	    db_capture_bufoff) % TEXTDUMP_BLOCKSIZE);
246174921Srwatson	bzero(db_capture_buf + db_capture_bufoff, len);
247174921Srwatson	db_capture_bufpadding = len;
248174921Srwatson}
249174921Srwatson
250174921Srwatson/*
251174910Srwatson * Reset capture state, which flushes buffers.
252174910Srwatson */
253174910Srwatsonstatic void
254174910Srwatsondb_capture_reset(void)
255174910Srwatson{
256174910Srwatson
257174910Srwatson	db_capture_inprogress = 0;
258174910Srwatson	db_capture_bufoff = 0;
259174921Srwatson	db_capture_bufpadding = 0;
260174910Srwatson}
261174910Srwatson
262174910Srwatson/*
263174910Srwatson * Start capture.  Only one session is allowed at any time, but we may
264174910Srwatson * continue a previous session, so the buffer isn't reset.
265174910Srwatson */
266174910Srwatsonstatic void
267174910Srwatsondb_capture_start(void)
268174910Srwatson{
269174910Srwatson
270174910Srwatson	if (db_capture_inprogress) {
271174910Srwatson		db_printf("Capture already started\n");
272174910Srwatson		return;
273174910Srwatson	}
274174910Srwatson	db_capture_inprogress = 1;
275174910Srwatson}
276174910Srwatson
277174910Srwatson/*
278174921Srwatson * Terminate DDB output capture--real work is deferred to db_capture_dump,
279174921Srwatson * which executes outside of the DDB context.  We don't zero pad here because
280174921Srwatson * capture may be started again before the dump takes place.
281174910Srwatson */
282174910Srwatsonstatic void
283174910Srwatsondb_capture_stop(void)
284174910Srwatson{
285174910Srwatson
286174910Srwatson	if (db_capture_inprogress == 0) {
287174910Srwatson		db_printf("Capture not started\n");
288174910Srwatson		return;
289174910Srwatson	}
290174910Srwatson	db_capture_inprogress = 0;
291174910Srwatson}
292174910Srwatson
293174921Srwatson/*
294174921Srwatson * Dump DDB(4) captured output (and resets capture buffers).
295174921Srwatson */
296174921Srwatsonvoid
297174921Srwatsondb_capture_dump(struct dumperinfo *di)
298174921Srwatson{
299174921Srwatson	u_int offset;
300174921Srwatson
301174921Srwatson	if (db_capture_bufoff == 0)
302174921Srwatson		return;
303174921Srwatson
304174921Srwatson	db_capture_zeropad();
305175684Srwatson	textdump_mkustar(textdump_block_buffer, DDB_CAPTURE_FILENAME,
306174921Srwatson	    db_capture_bufoff);
307174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
308174921Srwatson	for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding;
309174921Srwatson	    offset += TEXTDUMP_BLOCKSIZE)
310174921Srwatson		(void)textdump_writenextblock(di, db_capture_buf + offset);
311174921Srwatson	db_capture_bufoff = 0;
312174921Srwatson	db_capture_bufpadding = 0;
313174921Srwatson}
314174921Srwatson
315174910Srwatson/*-
316174910Srwatson * DDB(4) command to manage capture:
317174910Srwatson *
318174910Srwatson * capture on          - start DDB output capture
319174910Srwatson * capture off         - stop DDB output capture
320174910Srwatson * capture reset       - reset DDB capture buffer (also stops capture)
321174910Srwatson * capture status      - print DDB output capture status
322174910Srwatson */
323174910Srwatsonstatic void
324174910Srwatsondb_capture_usage(void)
325174910Srwatson{
326174910Srwatson
327174910Srwatson	db_error("capture [on|off|reset|status]\n");
328174910Srwatson}
329174910Srwatson
330174910Srwatsonvoid
331174910Srwatsondb_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
332174910Srwatson    char *modif)
333174910Srwatson{
334174910Srwatson	int t;
335174910Srwatson
336174910Srwatson	t = db_read_token();
337174910Srwatson	if (t != tIDENT) {
338174910Srwatson		db_capture_usage();
339174910Srwatson		return;
340174910Srwatson	}
341174910Srwatson	if (db_read_token() != tEOL)
342174910Srwatson		db_error("?\n");
343174910Srwatson	if (strcmp(db_tok_string, "on") == 0)
344174910Srwatson		db_capture_start();
345174910Srwatson	else if (strcmp(db_tok_string, "off") == 0)
346174910Srwatson		db_capture_stop();
347174910Srwatson	else if (strcmp(db_tok_string, "reset") == 0)
348174910Srwatson		db_capture_reset();
349174910Srwatson	else if (strcmp(db_tok_string, "status") == 0) {
350174910Srwatson		db_printf("%u/%u bytes used\n", db_capture_bufoff,
351174910Srwatson		    db_capture_bufsize);
352174910Srwatson		if (db_capture_inprogress)
353174910Srwatson			db_printf("capture is on\n");
354174910Srwatson		else
355174910Srwatson			db_printf("capture is off\n");
356174910Srwatson	} else
357174910Srwatson		db_capture_usage();
358174910Srwatson}
359