Add a new DDB(4) facility, output capture. Input and output from DDB may be
captured to a memory buffer for later inspection using sysctl(8), or in the future, to a textdump. A new DDB command, "capture", is added, which accepts arguments "on", "off", "reset", and "status". A new DDB sysctl tree, debug.ddb.capture, is added, which can be used to resize the capture buffer and extract buffer contents. MFC after: 3 months
This commit is contained in:
parent
45044461a8
commit
086fec574e
@ -350,6 +350,7 @@ crypto/sha2/sha2.c optional crypto | geom_bde | ipsec | random | \
|
||||
sctp
|
||||
ddb/db_access.c optional ddb
|
||||
ddb/db_break.c optional ddb
|
||||
ddb/db_capture.c optional ddb
|
||||
ddb/db_command.c optional ddb
|
||||
ddb/db_examine.c optional ddb
|
||||
ddb/db_expr.c optional ddb
|
||||
|
301
sys/ddb/db_capture.c
Normal file
301
sys/ddb/db_capture.c
Normal file
@ -0,0 +1,301 @@
|
||||
/*-
|
||||
* Copyright (c) 2007 Robert N. M. Watson
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DDB capture support: capture kernel debugger output into a fixed-size
|
||||
* buffer for later dumping to disk or extraction from user space.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/kerneldump.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/msgbuf.h>
|
||||
#include <sys/priv.h>
|
||||
#include <sys/sx.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include <ddb/ddb.h>
|
||||
#include <ddb/db_lex.h>
|
||||
|
||||
/*
|
||||
* While it would be desirable to use a small block-sized buffer and dump
|
||||
* incrementally to disk in fixed-size blocks, it's not possible to enter
|
||||
* kernel dumper routines without restarting the kernel, which is undesirable
|
||||
* in the midst of debugging. Instead, we maintain a large static global
|
||||
* buffer that we fill from DDB's output routines.
|
||||
*/
|
||||
static MALLOC_DEFINE(M_DB_CAPTURE, "db_capture", "DDB capture buffer");
|
||||
|
||||
#define DB_CAPTURE_DEFAULTBUFSIZE 48*1024
|
||||
#define DB_CAPTURE_MAXBUFSIZE 512*1024
|
||||
|
||||
static char *db_capture_buf;
|
||||
static u_int db_capture_bufsize = DB_CAPTURE_DEFAULTBUFSIZE;
|
||||
static u_int db_capture_maxbufsize = DB_CAPTURE_MAXBUFSIZE; /* Read-only. */
|
||||
static u_int db_capture_bufoff; /* Next location to write in buffer. */
|
||||
static int db_capture_inpager; /* Suspend capture in pager. */
|
||||
static int db_capture_inprogress; /* DDB capture currently in progress. */
|
||||
|
||||
struct sx db_capture_sx; /* Lock against user thread races. */
|
||||
SX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx");
|
||||
|
||||
static SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0,
|
||||
"DDB capture options");
|
||||
|
||||
SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD,
|
||||
&db_capture_bufoff, 0, "Bytes of data in DDB capture buffer");
|
||||
|
||||
SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD,
|
||||
&db_capture_maxbufsize, 0,
|
||||
"Maximum value for debug.ddb.capture.bufsize");
|
||||
|
||||
/*
|
||||
* Boot-time allocation of the DDB capture buffer, if any.
|
||||
*/
|
||||
static void
|
||||
db_capture_sysinit(__unused void *dummy)
|
||||
{
|
||||
|
||||
TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize);
|
||||
if (db_capture_bufsize > DB_CAPTURE_MAXBUFSIZE)
|
||||
db_capture_bufsize = DB_CAPTURE_MAXBUFSIZE;
|
||||
|
||||
if (db_capture_bufsize != 0)
|
||||
db_capture_buf = malloc(db_capture_bufsize, M_DB_CAPTURE,
|
||||
M_WAITOK);
|
||||
}
|
||||
SYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit,
|
||||
NULL);
|
||||
|
||||
/*
|
||||
* Run-time adjustment of the capture buffer.
|
||||
*/
|
||||
static int
|
||||
sysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
u_int len, size;
|
||||
char *buf;
|
||||
int error;
|
||||
|
||||
size = db_capture_bufsize;
|
||||
error = sysctl_handle_int(oidp, &size, 0, req);
|
||||
if (error || req->newptr == NULL)
|
||||
return (error);
|
||||
if (size > DB_CAPTURE_MAXBUFSIZE)
|
||||
return (EINVAL);
|
||||
|
||||
sx_xlock(&db_capture_sx);
|
||||
if (size != 0) {
|
||||
/*
|
||||
* Potentially the buffer is quite large, so if we can't
|
||||
* allocate it, fail rather than waiting.
|
||||
*/
|
||||
buf = malloc(size, M_DB_CAPTURE, M_NOWAIT);
|
||||
if (buf == NULL) {
|
||||
sx_xunlock(&db_capture_sx);
|
||||
return (ENOMEM);
|
||||
}
|
||||
len = min(db_capture_bufoff, size);
|
||||
} else {
|
||||
buf = NULL;
|
||||
len = 0;
|
||||
}
|
||||
if (db_capture_buf != NULL && buf != NULL)
|
||||
bcopy(db_capture_buf, buf, len);
|
||||
if (db_capture_buf != NULL)
|
||||
free(db_capture_buf, M_DB_CAPTURE);
|
||||
db_capture_bufoff = len;
|
||||
db_capture_buf = buf;
|
||||
db_capture_bufsize = size;
|
||||
sx_xunlock(&db_capture_sx);
|
||||
|
||||
KASSERT(db_capture_bufoff <= db_capture_bufsize,
|
||||
("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize"));
|
||||
KASSERT(db_capture_bufsize <= DB_CAPTURE_MAXBUFSIZE,
|
||||
("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize"));
|
||||
|
||||
return (0);
|
||||
}
|
||||
SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW,
|
||||
0, 0, sysctl_debug_ddb_capture_bufsize, "IU",
|
||||
"Size of DDB capture buffer");
|
||||
|
||||
/*
|
||||
* Sysctl to read out the capture buffer from userspace. We require
|
||||
* privilege as sensitive process/memory information may be accessed.
|
||||
*/
|
||||
static int
|
||||
sysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
int error;
|
||||
char ch;
|
||||
|
||||
error = priv_check(req->td, PRIV_DDB_CAPTURE);
|
||||
if (error)
|
||||
return (error);
|
||||
|
||||
sx_slock(&db_capture_sx);
|
||||
error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff);
|
||||
sx_sunlock(&db_capture_sx);
|
||||
if (error)
|
||||
return (error);
|
||||
ch = '\0';
|
||||
return (SYSCTL_OUT(req, &ch, sizeof(ch)));
|
||||
}
|
||||
SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD,
|
||||
NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data");
|
||||
|
||||
/*
|
||||
* Routines for capturing DDB output into a fixed-size buffer. These are
|
||||
* invoked from DDB's input and output routines. If we hit the limit on the
|
||||
* buffer, we simply drop further data.
|
||||
*/
|
||||
void
|
||||
db_capture_write(char *buffer, u_int buflen)
|
||||
{
|
||||
u_int len;
|
||||
|
||||
if (db_capture_inprogress == 0 || db_capture_inpager)
|
||||
return;
|
||||
len = min(buflen, db_capture_bufsize - db_capture_bufoff);
|
||||
bcopy(buffer, db_capture_buf + db_capture_bufoff, len);
|
||||
db_capture_bufoff += len;
|
||||
|
||||
KASSERT(db_capture_bufoff <= db_capture_bufsize,
|
||||
("db_capture_write: bufoff > bufsize"));
|
||||
}
|
||||
|
||||
void
|
||||
db_capture_writech(char ch)
|
||||
{
|
||||
|
||||
return (db_capture_write(&ch, sizeof(ch)));
|
||||
}
|
||||
|
||||
void
|
||||
db_capture_enterpager(void)
|
||||
{
|
||||
|
||||
db_capture_inpager = 1;
|
||||
}
|
||||
|
||||
void
|
||||
db_capture_exitpager(void)
|
||||
{
|
||||
|
||||
db_capture_inpager = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reset capture state, which flushes buffers.
|
||||
*/
|
||||
static void
|
||||
db_capture_reset(void)
|
||||
{
|
||||
|
||||
db_capture_inprogress = 0;
|
||||
db_capture_bufoff = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Start capture. Only one session is allowed at any time, but we may
|
||||
* continue a previous session, so the buffer isn't reset.
|
||||
*/
|
||||
static void
|
||||
db_capture_start(void)
|
||||
{
|
||||
|
||||
if (db_capture_inprogress) {
|
||||
db_printf("Capture already started\n");
|
||||
return;
|
||||
}
|
||||
db_capture_inprogress = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Terminate DDB output capture.
|
||||
*/
|
||||
static void
|
||||
db_capture_stop(void)
|
||||
{
|
||||
|
||||
if (db_capture_inprogress == 0) {
|
||||
db_printf("Capture not started\n");
|
||||
return;
|
||||
}
|
||||
db_capture_inprogress = 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* DDB(4) command to manage capture:
|
||||
*
|
||||
* capture on - start DDB output capture
|
||||
* capture off - stop DDB output capture
|
||||
* capture reset - reset DDB capture buffer (also stops capture)
|
||||
* capture status - print DDB output capture status
|
||||
*/
|
||||
static void
|
||||
db_capture_usage(void)
|
||||
{
|
||||
|
||||
db_error("capture [on|off|reset|status]\n");
|
||||
}
|
||||
|
||||
void
|
||||
db_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
|
||||
char *modif)
|
||||
{
|
||||
int t;
|
||||
|
||||
t = db_read_token();
|
||||
if (t != tIDENT) {
|
||||
db_capture_usage();
|
||||
return;
|
||||
}
|
||||
if (db_read_token() != tEOL)
|
||||
db_error("?\n");
|
||||
if (strcmp(db_tok_string, "on") == 0)
|
||||
db_capture_start();
|
||||
else if (strcmp(db_tok_string, "off") == 0)
|
||||
db_capture_stop();
|
||||
else if (strcmp(db_tok_string, "reset") == 0)
|
||||
db_capture_reset();
|
||||
else if (strcmp(db_tok_string, "status") == 0) {
|
||||
db_printf("%u/%u bytes used\n", db_capture_bufoff,
|
||||
db_capture_bufsize);
|
||||
if (db_capture_inprogress)
|
||||
db_printf("capture is on\n");
|
||||
else
|
||||
db_printf("capture is off\n");
|
||||
} else
|
||||
db_capture_usage();
|
||||
}
|
@ -141,6 +141,7 @@ static struct command db_commands[] = {
|
||||
{ "kill", db_kill, CS_OWN, 0 },
|
||||
{ "watchdog", db_watchdog, 0, 0 },
|
||||
{ "thread", db_set_thread, CS_OWN, 0 },
|
||||
{ "capture", db_capture_cmd, CS_OWN, 0 },
|
||||
{ (char *)0, }
|
||||
};
|
||||
|
||||
|
@ -323,6 +323,7 @@ db_readline(lstart, lsize)
|
||||
while (!db_inputchar(cngetc()))
|
||||
continue;
|
||||
|
||||
db_capture_write(lstart, db_le - db_lbuf_start);
|
||||
db_printf("\n"); /* synch output position */
|
||||
*db_le = 0;
|
||||
|
||||
|
@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
|
||||
#include <sys/pcpu.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <machine/kdb.h>
|
||||
#include <machine/pcb.h>
|
||||
@ -45,6 +46,8 @@ __FBSDID("$FreeBSD$");
|
||||
#include <ddb/db_command.h>
|
||||
#include <ddb/db_sym.h>
|
||||
|
||||
SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings");
|
||||
|
||||
static dbbe_init_f db_init;
|
||||
static dbbe_trap_f db_trap;
|
||||
static dbbe_trace_f db_trace_self_wrapper;
|
||||
|
@ -90,11 +90,13 @@ db_force_whitespace()
|
||||
if (next_tab <= db_output_position) {
|
||||
while (last_print < next_tab) { /* DON'T send a tab!!! */
|
||||
cnputc(' ');
|
||||
db_capture_writech(' ');
|
||||
last_print++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cnputc(' ');
|
||||
db_capture_writech(' ');
|
||||
last_print++;
|
||||
}
|
||||
}
|
||||
@ -137,12 +139,14 @@ db_putchar(c, arg)
|
||||
*/
|
||||
db_force_whitespace();
|
||||
cnputc(c);
|
||||
db_capture_writech(c);
|
||||
db_output_position++;
|
||||
db_last_non_space = db_output_position;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
/* Newline */
|
||||
cnputc(c);
|
||||
db_capture_writech(c);
|
||||
db_output_position = 0;
|
||||
db_last_non_space = 0;
|
||||
db_check_interrupt();
|
||||
@ -155,6 +159,7 @@ db_putchar(c, arg)
|
||||
else if (c == '\r') {
|
||||
/* Return */
|
||||
cnputc(c);
|
||||
db_capture_writech(c);
|
||||
db_output_position = 0;
|
||||
db_last_non_space = 0;
|
||||
db_check_interrupt();
|
||||
@ -170,6 +175,7 @@ db_putchar(c, arg)
|
||||
else if (c == '\007') {
|
||||
/* bell */
|
||||
cnputc(c);
|
||||
/* No need to beep in a log: db_capture_writech(c); */
|
||||
}
|
||||
/* other characters are assumed non-printing */
|
||||
}
|
||||
@ -206,6 +212,7 @@ db_pager(void)
|
||||
{
|
||||
int c, done;
|
||||
|
||||
db_capture_enterpager();
|
||||
db_printf("--More--\r");
|
||||
done = 0;
|
||||
while (!done) {
|
||||
@ -249,6 +256,7 @@ db_pager(void)
|
||||
db_force_whitespace();
|
||||
db_printf("\r");
|
||||
db_newlines = 0;
|
||||
db_capture_exitpager();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -37,6 +37,10 @@
|
||||
#ifndef _DDB_DDB_H_
|
||||
#define _DDB_DDB_H_
|
||||
|
||||
#ifdef SYSCTL_DECL
|
||||
SYSCTL_DECL(_debug_ddb);
|
||||
#endif
|
||||
|
||||
#include <machine/db_machdep.h> /* type definitions */
|
||||
|
||||
#ifndef DB_MAXARGS
|
||||
@ -126,6 +130,7 @@ int db_value_of_name(const char *name, db_expr_t *valuep);
|
||||
int db_write_bytes(vm_offset_t addr, size_t size, char *data);
|
||||
|
||||
db_cmdfcn_t db_breakpoint_cmd;
|
||||
db_cmdfcn_t db_capture_cmd;
|
||||
db_cmdfcn_t db_continue_cmd;
|
||||
db_cmdfcn_t db_delete_cmd;
|
||||
db_cmdfcn_t db_deletehwatch_cmd;
|
||||
@ -168,4 +173,14 @@ struct command {
|
||||
struct command_table *more; /* another level of command */
|
||||
};
|
||||
|
||||
/*
|
||||
* Interface between DDB and the DDB output capture facility.
|
||||
*/
|
||||
struct dumperinfo;
|
||||
void db_capture_dump(struct dumperinfo *di);
|
||||
void db_capture_enterpager(void);
|
||||
void db_capture_exitpager(void);
|
||||
void db_capture_write(char *buffer, u_int buflen);
|
||||
void db_capture_writech(char ch);
|
||||
|
||||
#endif /* !_DDB_DDB_H_ */
|
||||
|
Loading…
x
Reference in New Issue
Block a user