db_capture.c revision 175684
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 175684 2008-01-26 13:55:52Z 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 */ 56175684Srwatsonstatic MALLOC_DEFINE(M_DDB_CAPTURE, "ddb_capture", "DDB capture buffer"); 57174910Srwatson 58175684Srwatson#define DDB_CAPTURE_DEFAULTBUFSIZE 48*1024 59175684Srwatson#define DDB_CAPTURE_MAXBUFSIZE 512*1024 60175684Srwatson#define DDB_CAPTURE_FILENAME "ddb.txt" /* Captured DDB output. */ 61174910Srwatson 62174910Srwatsonstatic char *db_capture_buf; 63175684Srwatsonstatic u_int db_capture_bufsize = DDB_CAPTURE_DEFAULTBUFSIZE; 64175684Srwatsonstatic u_int db_capture_maxbufsize = DDB_CAPTURE_MAXBUFSIZE; /* Read-only. */ 65174910Srwatsonstatic u_int db_capture_bufoff; /* Next location to write in buffer. */ 66174921Srwatsonstatic u_int db_capture_bufpadding; /* Amount of zero padding. */ 67174910Srwatsonstatic int db_capture_inpager; /* Suspend capture in pager. */ 68174910Srwatsonstatic int db_capture_inprogress; /* DDB capture currently in progress. */ 69174910Srwatson 70174910Srwatsonstruct sx db_capture_sx; /* Lock against user thread races. */ 71174910SrwatsonSX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx"); 72174910Srwatson 73174910Srwatsonstatic SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0, 74174910Srwatson "DDB capture options"); 75174910Srwatson 76174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD, 77174910Srwatson &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer"); 78174910Srwatson 79174910SrwatsonSYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD, 80174910Srwatson &db_capture_maxbufsize, 0, 81174910Srwatson "Maximum value for debug.ddb.capture.bufsize"); 82174910Srwatson 83174910Srwatson/* 84174921Srwatson * Various compile-time assertions: defaults must be even multiples of 85174921Srwatson * textdump block size. We also perform run-time checking of 86174921Srwatson * user-configurable values. 87174921Srwatson */ 88175684SrwatsonCTASSERT(DDB_CAPTURE_DEFAULTBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 89175684SrwatsonCTASSERT(DDB_CAPTURE_MAXBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 90174921Srwatson 91174921Srwatson/* 92174910Srwatson * Boot-time allocation of the DDB capture buffer, if any. 93174910Srwatson */ 94174910Srwatsonstatic void 95174910Srwatsondb_capture_sysinit(__unused void *dummy) 96174910Srwatson{ 97174910Srwatson 98174910Srwatson TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize); 99174921Srwatson db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE); 100175684Srwatson if (db_capture_bufsize > DDB_CAPTURE_MAXBUFSIZE) 101175684Srwatson db_capture_bufsize = DDB_CAPTURE_MAXBUFSIZE; 102174910Srwatson if (db_capture_bufsize != 0) 103175684Srwatson db_capture_buf = malloc(db_capture_bufsize, M_DDB_CAPTURE, 104174910Srwatson M_WAITOK); 105174910Srwatson} 106174910SrwatsonSYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit, 107174910Srwatson NULL); 108174910Srwatson 109174910Srwatson/* 110174910Srwatson * Run-time adjustment of the capture buffer. 111174910Srwatson */ 112174910Srwatsonstatic int 113174910Srwatsonsysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS) 114174910Srwatson{ 115174910Srwatson u_int len, size; 116174910Srwatson char *buf; 117174910Srwatson int error; 118174910Srwatson 119174910Srwatson size = db_capture_bufsize; 120174910Srwatson error = sysctl_handle_int(oidp, &size, 0, req); 121174910Srwatson if (error || req->newptr == NULL) 122174910Srwatson return (error); 123174921Srwatson size = roundup(size, TEXTDUMP_BLOCKSIZE); 124175684Srwatson if (size > DDB_CAPTURE_MAXBUFSIZE) 125174910Srwatson return (EINVAL); 126174910Srwatson sx_xlock(&db_capture_sx); 127174910Srwatson if (size != 0) { 128174910Srwatson /* 129174910Srwatson * Potentially the buffer is quite large, so if we can't 130174910Srwatson * allocate it, fail rather than waiting. 131174910Srwatson */ 132175684Srwatson buf = malloc(size, M_DDB_CAPTURE, M_NOWAIT); 133174910Srwatson if (buf == NULL) { 134174910Srwatson sx_xunlock(&db_capture_sx); 135174910Srwatson return (ENOMEM); 136174910Srwatson } 137174910Srwatson len = min(db_capture_bufoff, size); 138174910Srwatson } else { 139174910Srwatson buf = NULL; 140174910Srwatson len = 0; 141174910Srwatson } 142174910Srwatson if (db_capture_buf != NULL && buf != NULL) 143174910Srwatson bcopy(db_capture_buf, buf, len); 144174910Srwatson if (db_capture_buf != NULL) 145175684Srwatson free(db_capture_buf, M_DDB_CAPTURE); 146174910Srwatson db_capture_bufoff = len; 147174910Srwatson db_capture_buf = buf; 148174910Srwatson db_capture_bufsize = size; 149174910Srwatson sx_xunlock(&db_capture_sx); 150174910Srwatson 151174910Srwatson KASSERT(db_capture_bufoff <= db_capture_bufsize, 152174910Srwatson ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize")); 153175684Srwatson KASSERT(db_capture_bufsize <= DDB_CAPTURE_MAXBUFSIZE, 154174910Srwatson ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize")); 155174910Srwatson 156174910Srwatson return (0); 157174910Srwatson} 158174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW, 159174910Srwatson 0, 0, sysctl_debug_ddb_capture_bufsize, "IU", 160174910Srwatson "Size of DDB capture buffer"); 161174910Srwatson 162174910Srwatson/* 163174910Srwatson * Sysctl to read out the capture buffer from userspace. We require 164174910Srwatson * privilege as sensitive process/memory information may be accessed. 165174910Srwatson */ 166174910Srwatsonstatic int 167174910Srwatsonsysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS) 168174910Srwatson{ 169174910Srwatson int error; 170174910Srwatson char ch; 171174910Srwatson 172174910Srwatson error = priv_check(req->td, PRIV_DDB_CAPTURE); 173174910Srwatson if (error) 174174910Srwatson return (error); 175174910Srwatson 176174910Srwatson sx_slock(&db_capture_sx); 177174910Srwatson error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff); 178174910Srwatson sx_sunlock(&db_capture_sx); 179174910Srwatson if (error) 180174910Srwatson return (error); 181174910Srwatson ch = '\0'; 182174910Srwatson return (SYSCTL_OUT(req, &ch, sizeof(ch))); 183174910Srwatson} 184174910SrwatsonSYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD, 185174910Srwatson NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data"); 186174910Srwatson 187174910Srwatson/* 188174910Srwatson * Routines for capturing DDB output into a fixed-size buffer. These are 189174910Srwatson * invoked from DDB's input and output routines. If we hit the limit on the 190174910Srwatson * buffer, we simply drop further data. 191174910Srwatson */ 192174910Srwatsonvoid 193174910Srwatsondb_capture_write(char *buffer, u_int buflen) 194174910Srwatson{ 195174910Srwatson u_int len; 196174910Srwatson 197174910Srwatson if (db_capture_inprogress == 0 || db_capture_inpager) 198174910Srwatson return; 199174910Srwatson len = min(buflen, db_capture_bufsize - db_capture_bufoff); 200174910Srwatson bcopy(buffer, db_capture_buf + db_capture_bufoff, len); 201174910Srwatson db_capture_bufoff += len; 202174910Srwatson 203174910Srwatson KASSERT(db_capture_bufoff <= db_capture_bufsize, 204174910Srwatson ("db_capture_write: bufoff > bufsize")); 205174910Srwatson} 206174910Srwatson 207174910Srwatsonvoid 208174910Srwatsondb_capture_writech(char ch) 209174910Srwatson{ 210174910Srwatson 211174910Srwatson return (db_capture_write(&ch, sizeof(ch))); 212174910Srwatson} 213174910Srwatson 214174910Srwatsonvoid 215174910Srwatsondb_capture_enterpager(void) 216174910Srwatson{ 217174910Srwatson 218174910Srwatson db_capture_inpager = 1; 219174910Srwatson} 220174910Srwatson 221174910Srwatsonvoid 222174910Srwatsondb_capture_exitpager(void) 223174910Srwatson{ 224174910Srwatson 225174910Srwatson db_capture_inpager = 0; 226174910Srwatson} 227174910Srwatson 228174910Srwatson/* 229174921Srwatson * Zero out any bytes left in the last block of the DDB capture buffer. This 230174921Srwatson * is run shortly before writing the blocks to disk, rather than when output 231174921Srwatson * capture is stopped, in order to avoid injecting nul's into the middle of 232174921Srwatson * output. 233174921Srwatson */ 234174921Srwatsonstatic void 235174921Srwatsondb_capture_zeropad(void) 236174921Srwatson{ 237174921Srwatson u_int len; 238174921Srwatson 239174921Srwatson len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize - 240174921Srwatson db_capture_bufoff) % TEXTDUMP_BLOCKSIZE); 241174921Srwatson bzero(db_capture_buf + db_capture_bufoff, len); 242174921Srwatson db_capture_bufpadding = len; 243174921Srwatson} 244174921Srwatson 245174921Srwatson/* 246174910Srwatson * Reset capture state, which flushes buffers. 247174910Srwatson */ 248174910Srwatsonstatic void 249174910Srwatsondb_capture_reset(void) 250174910Srwatson{ 251174910Srwatson 252174910Srwatson db_capture_inprogress = 0; 253174910Srwatson db_capture_bufoff = 0; 254174921Srwatson db_capture_bufpadding = 0; 255174910Srwatson} 256174910Srwatson 257174910Srwatson/* 258174910Srwatson * Start capture. Only one session is allowed at any time, but we may 259174910Srwatson * continue a previous session, so the buffer isn't reset. 260174910Srwatson */ 261174910Srwatsonstatic void 262174910Srwatsondb_capture_start(void) 263174910Srwatson{ 264174910Srwatson 265174910Srwatson if (db_capture_inprogress) { 266174910Srwatson db_printf("Capture already started\n"); 267174910Srwatson return; 268174910Srwatson } 269174910Srwatson db_capture_inprogress = 1; 270174910Srwatson} 271174910Srwatson 272174910Srwatson/* 273174921Srwatson * Terminate DDB output capture--real work is deferred to db_capture_dump, 274174921Srwatson * which executes outside of the DDB context. We don't zero pad here because 275174921Srwatson * capture may be started again before the dump takes place. 276174910Srwatson */ 277174910Srwatsonstatic void 278174910Srwatsondb_capture_stop(void) 279174910Srwatson{ 280174910Srwatson 281174910Srwatson if (db_capture_inprogress == 0) { 282174910Srwatson db_printf("Capture not started\n"); 283174910Srwatson return; 284174910Srwatson } 285174910Srwatson db_capture_inprogress = 0; 286174910Srwatson} 287174910Srwatson 288174921Srwatson/* 289174921Srwatson * Dump DDB(4) captured output (and resets capture buffers). 290174921Srwatson */ 291174921Srwatsonvoid 292174921Srwatsondb_capture_dump(struct dumperinfo *di) 293174921Srwatson{ 294174921Srwatson u_int offset; 295174921Srwatson 296174921Srwatson if (db_capture_bufoff == 0) 297174921Srwatson return; 298174921Srwatson 299174921Srwatson db_capture_zeropad(); 300175684Srwatson textdump_mkustar(textdump_block_buffer, DDB_CAPTURE_FILENAME, 301174921Srwatson db_capture_bufoff); 302174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 303174921Srwatson for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding; 304174921Srwatson offset += TEXTDUMP_BLOCKSIZE) 305174921Srwatson (void)textdump_writenextblock(di, db_capture_buf + offset); 306174921Srwatson db_capture_bufoff = 0; 307174921Srwatson db_capture_bufpadding = 0; 308174921Srwatson} 309174921Srwatson 310174910Srwatson/*- 311174910Srwatson * DDB(4) command to manage capture: 312174910Srwatson * 313174910Srwatson * capture on - start DDB output capture 314174910Srwatson * capture off - stop DDB output capture 315174910Srwatson * capture reset - reset DDB capture buffer (also stops capture) 316174910Srwatson * capture status - print DDB output capture status 317174910Srwatson */ 318174910Srwatsonstatic void 319174910Srwatsondb_capture_usage(void) 320174910Srwatson{ 321174910Srwatson 322174910Srwatson db_error("capture [on|off|reset|status]\n"); 323174910Srwatson} 324174910Srwatson 325174910Srwatsonvoid 326174910Srwatsondb_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 327174910Srwatson char *modif) 328174910Srwatson{ 329174910Srwatson int t; 330174910Srwatson 331174910Srwatson t = db_read_token(); 332174910Srwatson if (t != tIDENT) { 333174910Srwatson db_capture_usage(); 334174910Srwatson return; 335174910Srwatson } 336174910Srwatson if (db_read_token() != tEOL) 337174910Srwatson db_error("?\n"); 338174910Srwatson if (strcmp(db_tok_string, "on") == 0) 339174910Srwatson db_capture_start(); 340174910Srwatson else if (strcmp(db_tok_string, "off") == 0) 341174910Srwatson db_capture_stop(); 342174910Srwatson else if (strcmp(db_tok_string, "reset") == 0) 343174910Srwatson db_capture_reset(); 344174910Srwatson else if (strcmp(db_tok_string, "status") == 0) { 345174910Srwatson db_printf("%u/%u bytes used\n", db_capture_bufoff, 346174910Srwatson db_capture_bufsize); 347174910Srwatson if (db_capture_inprogress) 348174910Srwatson db_printf("capture is on\n"); 349174910Srwatson else 350174910Srwatson db_printf("capture is off\n"); 351174910Srwatson } else 352174910Srwatson db_capture_usage(); 353174910Srwatson} 354