db_capture.c revision 174910
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 174910 2007-12-25 23:06:51Z rwatson $");
34174910Srwatson
35174910Srwatson#include <sys/param.h>
36174910Srwatson#include <sys/conf.h>
37174910Srwatson#include <sys/kernel.h>
38174910Srwatson#include <sys/kerneldump.h>
39174910Srwatson#include <sys/malloc.h>
40174910Srwatson#include <sys/msgbuf.h>
41174910Srwatson#include <sys/priv.h>
42174910Srwatson#include <sys/sx.h>
43174910Srwatson#include <sys/sysctl.h>
44174910Srwatson#include <sys/systm.h>
45174910Srwatson
46174910Srwatson#include <ddb/ddb.h>
47174910Srwatson#include <ddb/db_lex.h>
48174910Srwatson
49174910Srwatson/*
50174910Srwatson * While it would be desirable to use a small block-sized buffer and dump
51174910Srwatson * incrementally to disk in fixed-size blocks, it's not possible to enter
52174910Srwatson * kernel dumper routines without restarting the kernel, which is undesirable
53174910Srwatson * in the midst of debugging.  Instead, we maintain a large static global
54174910Srwatson * buffer that we fill from DDB's output routines.
55174910Srwatson */
56174910Srwatsonstatic MALLOC_DEFINE(M_DB_CAPTURE, "db_capture", "DDB capture buffer");
57174910Srwatson
58174910Srwatson#define	DB_CAPTURE_DEFAULTBUFSIZE	48*1024
59174910Srwatson#define	DB_CAPTURE_MAXBUFSIZE	512*1024
60174910Srwatson
61174910Srwatsonstatic char *db_capture_buf;
62174910Srwatsonstatic u_int db_capture_bufsize = DB_CAPTURE_DEFAULTBUFSIZE;
63174910Srwatsonstatic u_int db_capture_maxbufsize = DB_CAPTURE_MAXBUFSIZE; /* Read-only. */
64174910Srwatsonstatic u_int db_capture_bufoff;		/* Next location to write in buffer. */
65174910Srwatsonstatic int db_capture_inpager;		/* Suspend capture in pager. */
66174910Srwatsonstatic int db_capture_inprogress;	/* DDB capture currently in progress. */
67174910Srwatson
68174910Srwatsonstruct sx db_capture_sx;		/* Lock against user thread races. */
69174910SrwatsonSX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx");
70174910Srwatson
71174910Srwatsonstatic SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0,
72174910Srwatson    "DDB capture options");
73174910Srwatson
74174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD,
75174910Srwatson    &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer");
76174910Srwatson
77174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD,
78174910Srwatson    &db_capture_maxbufsize, 0,
79174910Srwatson    "Maximum value for debug.ddb.capture.bufsize");
80174910Srwatson
81174910Srwatson/*
82174910Srwatson * Boot-time allocation of the DDB capture buffer, if any.
83174910Srwatson */
84174910Srwatsonstatic void
85174910Srwatsondb_capture_sysinit(__unused void *dummy)
86174910Srwatson{
87174910Srwatson
88174910Srwatson	TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
89174910Srwatson	if (db_capture_bufsize > DB_CAPTURE_MAXBUFSIZE)
90174910Srwatson		db_capture_bufsize = DB_CAPTURE_MAXBUFSIZE;
91174910Srwatson
92174910Srwatson	if (db_capture_bufsize != 0)
93174910Srwatson		db_capture_buf = malloc(db_capture_bufsize, M_DB_CAPTURE,
94174910Srwatson		    M_WAITOK);
95174910Srwatson}
96174910SrwatsonSYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
97174910Srwatson    NULL);
98174910Srwatson
99174910Srwatson/*
100174910Srwatson * Run-time adjustment of the capture buffer.
101174910Srwatson */
102174910Srwatsonstatic int
103174910Srwatsonsysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
104174910Srwatson{
105174910Srwatson	u_int len, size;
106174910Srwatson	char *buf;
107174910Srwatson	int error;
108174910Srwatson
109174910Srwatson	size = db_capture_bufsize;
110174910Srwatson	error = sysctl_handle_int(oidp, &size, 0, req);
111174910Srwatson	if (error || req->newptr == NULL)
112174910Srwatson		return (error);
113174910Srwatson	if (size > DB_CAPTURE_MAXBUFSIZE)
114174910Srwatson		return (EINVAL);
115174910Srwatson
116174910Srwatson	sx_xlock(&db_capture_sx);
117174910Srwatson	if (size != 0) {
118174910Srwatson		/*
119174910Srwatson		 * Potentially the buffer is quite large, so if we can't
120174910Srwatson		 * allocate it, fail rather than waiting.
121174910Srwatson		 */
122174910Srwatson		buf = malloc(size, M_DB_CAPTURE, M_NOWAIT);
123174910Srwatson		if (buf == NULL) {
124174910Srwatson			sx_xunlock(&db_capture_sx);
125174910Srwatson			return (ENOMEM);
126174910Srwatson		}
127174910Srwatson		len = min(db_capture_bufoff, size);
128174910Srwatson	} else {
129174910Srwatson		buf = NULL;
130174910Srwatson		len = 0;
131174910Srwatson	}
132174910Srwatson	if (db_capture_buf != NULL && buf != NULL)
133174910Srwatson		bcopy(db_capture_buf, buf, len);
134174910Srwatson	if (db_capture_buf != NULL)
135174910Srwatson		free(db_capture_buf, M_DB_CAPTURE);
136174910Srwatson	db_capture_bufoff = len;
137174910Srwatson	db_capture_buf = buf;
138174910Srwatson	db_capture_bufsize = size;
139174910Srwatson	sx_xunlock(&db_capture_sx);
140174910Srwatson
141174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
142174910Srwatson	    ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
143174910Srwatson	KASSERT(db_capture_bufsize <= DB_CAPTURE_MAXBUFSIZE,
144174910Srwatson	    ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
145174910Srwatson
146174910Srwatson	return (0);
147174910Srwatson}
148174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW,
149174910Srwatson    0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
150174910Srwatson    "Size of DDB capture buffer");
151174910Srwatson
152174910Srwatson/*
153174910Srwatson * Sysctl to read out the capture buffer from userspace.  We require
154174910Srwatson * privilege as sensitive process/memory information may be accessed.
155174910Srwatson */
156174910Srwatsonstatic int
157174910Srwatsonsysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
158174910Srwatson{
159174910Srwatson	int error;
160174910Srwatson	char ch;
161174910Srwatson
162174910Srwatson	error = priv_check(req->td, PRIV_DDB_CAPTURE);
163174910Srwatson	if (error)
164174910Srwatson		return (error);
165174910Srwatson
166174910Srwatson	sx_slock(&db_capture_sx);
167174910Srwatson	error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
168174910Srwatson	sx_sunlock(&db_capture_sx);
169174910Srwatson	if (error)
170174910Srwatson		return (error);
171174910Srwatson	ch = '\0';
172174910Srwatson	return (SYSCTL_OUT(req, &ch, sizeof(ch)));
173174910Srwatson}
174174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD,
175174910Srwatson    NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data");
176174910Srwatson
177174910Srwatson/*
178174910Srwatson * Routines for capturing DDB output into a fixed-size buffer.  These are
179174910Srwatson * invoked from DDB's input and output routines.  If we hit the limit on the
180174910Srwatson * buffer, we simply drop further data.
181174910Srwatson */
182174910Srwatsonvoid
183174910Srwatsondb_capture_write(char *buffer, u_int buflen)
184174910Srwatson{
185174910Srwatson	u_int len;
186174910Srwatson
187174910Srwatson	if (db_capture_inprogress == 0 || db_capture_inpager)
188174910Srwatson		return;
189174910Srwatson	len = min(buflen, db_capture_bufsize - db_capture_bufoff);
190174910Srwatson	bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
191174910Srwatson	db_capture_bufoff += len;
192174910Srwatson
193174910Srwatson	KASSERT(db_capture_bufoff <= db_capture_bufsize,
194174910Srwatson	    ("db_capture_write: bufoff > bufsize"));
195174910Srwatson}
196174910Srwatson
197174910Srwatsonvoid
198174910Srwatsondb_capture_writech(char ch)
199174910Srwatson{
200174910Srwatson
201174910Srwatson	return (db_capture_write(&ch, sizeof(ch)));
202174910Srwatson}
203174910Srwatson
204174910Srwatsonvoid
205174910Srwatsondb_capture_enterpager(void)
206174910Srwatson{
207174910Srwatson
208174910Srwatson	db_capture_inpager = 1;
209174910Srwatson}
210174910Srwatson
211174910Srwatsonvoid
212174910Srwatsondb_capture_exitpager(void)
213174910Srwatson{
214174910Srwatson
215174910Srwatson	db_capture_inpager = 0;
216174910Srwatson}
217174910Srwatson
218174910Srwatson/*
219174910Srwatson * Reset capture state, which flushes buffers.
220174910Srwatson */
221174910Srwatsonstatic void
222174910Srwatsondb_capture_reset(void)
223174910Srwatson{
224174910Srwatson
225174910Srwatson	db_capture_inprogress = 0;
226174910Srwatson	db_capture_bufoff = 0;
227174910Srwatson}
228174910Srwatson
229174910Srwatson/*
230174910Srwatson * Start capture.  Only one session is allowed at any time, but we may
231174910Srwatson * continue a previous session, so the buffer isn't reset.
232174910Srwatson */
233174910Srwatsonstatic void
234174910Srwatsondb_capture_start(void)
235174910Srwatson{
236174910Srwatson
237174910Srwatson	if (db_capture_inprogress) {
238174910Srwatson		db_printf("Capture already started\n");
239174910Srwatson		return;
240174910Srwatson	}
241174910Srwatson	db_capture_inprogress = 1;
242174910Srwatson}
243174910Srwatson
244174910Srwatson/*
245174910Srwatson * Terminate DDB output capture.
246174910Srwatson */
247174910Srwatsonstatic void
248174910Srwatsondb_capture_stop(void)
249174910Srwatson{
250174910Srwatson
251174910Srwatson	if (db_capture_inprogress == 0) {
252174910Srwatson		db_printf("Capture not started\n");
253174910Srwatson		return;
254174910Srwatson	}
255174910Srwatson	db_capture_inprogress = 0;
256174910Srwatson}
257174910Srwatson
258174910Srwatson/*-
259174910Srwatson * DDB(4) command to manage capture:
260174910Srwatson *
261174910Srwatson * capture on          - start DDB output capture
262174910Srwatson * capture off         - stop DDB output capture
263174910Srwatson * capture reset       - reset DDB capture buffer (also stops capture)
264174910Srwatson * capture status      - print DDB output capture status
265174910Srwatson */
266174910Srwatsonstatic void
267174910Srwatsondb_capture_usage(void)
268174910Srwatson{
269174910Srwatson
270174910Srwatson	db_error("capture [on|off|reset|status]\n");
271174910Srwatson}
272174910Srwatson
273174910Srwatsonvoid
274174910Srwatsondb_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
275174910Srwatson    char *modif)
276174910Srwatson{
277174910Srwatson	int t;
278174910Srwatson
279174910Srwatson	t = db_read_token();
280174910Srwatson	if (t != tIDENT) {
281174910Srwatson		db_capture_usage();
282174910Srwatson		return;
283174910Srwatson	}
284174910Srwatson	if (db_read_token() != tEOL)
285174910Srwatson		db_error("?\n");
286174910Srwatson	if (strcmp(db_tok_string, "on") == 0)
287174910Srwatson		db_capture_start();
288174910Srwatson	else if (strcmp(db_tok_string, "off") == 0)
289174910Srwatson		db_capture_stop();
290174910Srwatson	else if (strcmp(db_tok_string, "reset") == 0)
291174910Srwatson		db_capture_reset();
292174910Srwatson	else if (strcmp(db_tok_string, "status") == 0) {
293174910Srwatson		db_printf("%u/%u bytes used\n", db_capture_bufoff,
294174910Srwatson		    db_capture_bufsize);
295174910Srwatson		if (db_capture_inprogress)
296174910Srwatson			db_printf("capture is on\n");
297174910Srwatson		else
298174910Srwatson			db_printf("capture is off\n");
299174910Srwatson	} else
300174910Srwatson		db_capture_usage();
301174910Srwatson}
302