db_capture.c revision 174921
1272343Sngie/*- 2272343Sngie * Copyright (c) 2007 Robert N. M. Watson 3272343Sngie * All rights reserved. 4272343Sngie * 5272343Sngie * Redistribution and use in source and binary forms, with or without 6272343Sngie * modification, are permitted provided that the following conditions 7272343Sngie * are met: 8272343Sngie * 1. Redistributions of source code must retain the above copyright 9272343Sngie * notice, this list of conditions and the following disclaimer. 10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer in the 12272343Sngie * documentation and/or other materials provided with the distribution. 13272343Sngie * 14272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17272343Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24272343Sngie * SUCH DAMAGE. 25272343Sngie */ 26272343Sngie 27272343Sngie/* 28272343Sngie * DDB capture support: capture kernel debugger output into a fixed-size 29272343Sngie * buffer for later dumping to disk or extraction from user space. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <sys/cdefs.h> 33272343Sngie__FBSDID("$FreeBSD: head/sys/ddb/db_capture.c 174921 2007-12-26 11:32:33Z rwatson $"); 34272343Sngie 35272343Sngie#include <sys/param.h> 36272343Sngie#include <sys/conf.h> 37272343Sngie#include <sys/kernel.h> 38272343Sngie#include <sys/kerneldump.h> 39272343Sngie#include <sys/malloc.h> 40272343Sngie#include <sys/msgbuf.h> 41272343Sngie#include <sys/priv.h> 42272343Sngie#include <sys/sx.h> 43272343Sngie#include <sys/sysctl.h> 44272343Sngie#include <sys/systm.h> 45272343Sngie 46272343Sngie#include <ddb/ddb.h> 47272343Sngie#include <ddb/db_lex.h> 48272343Sngie 49272343Sngie/* 50272343Sngie * While it would be desirable to use a small block-sized buffer and dump 51272343Sngie * incrementally to disk in fixed-size blocks, it's not possible to enter 52272343Sngie * kernel dumper routines without restarting the kernel, which is undesirable 53272343Sngie * in the midst of debugging. Instead, we maintain a large static global 54272343Sngie * buffer that we fill from DDB's output routines. 55272343Sngie */ 56272343Sngiestatic MALLOC_DEFINE(M_DB_CAPTURE, "db_capture", "DDB capture buffer"); 57272343Sngie 58272343Sngie#define DB_CAPTURE_DEFAULTBUFSIZE 48*1024 59272343Sngie#define DB_CAPTURE_MAXBUFSIZE 512*1024 60272343Sngie#define DB_CAPTURE_FILENAME "ddb.txt" /* Captured DDB output. */ 61 62static char *db_capture_buf; 63static u_int db_capture_bufsize = DB_CAPTURE_DEFAULTBUFSIZE; 64static u_int db_capture_maxbufsize = DB_CAPTURE_MAXBUFSIZE; /* Read-only. */ 65static u_int db_capture_bufoff; /* Next location to write in buffer. */ 66static u_int db_capture_bufpadding; /* Amount of zero padding. */ 67static int db_capture_inpager; /* Suspend capture in pager. */ 68static int db_capture_inprogress; /* DDB capture currently in progress. */ 69 70struct sx db_capture_sx; /* Lock against user thread races. */ 71SX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx"); 72 73static SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0, 74 "DDB capture options"); 75 76SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD, 77 &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer"); 78 79SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, maxbufsize, CTLFLAG_RD, 80 &db_capture_maxbufsize, 0, 81 "Maximum value for debug.ddb.capture.bufsize"); 82 83/* 84 * Various compile-time assertions: defaults must be even multiples of 85 * textdump block size. We also perform run-time checking of 86 * user-configurable values. 87 */ 88CTASSERT(DB_CAPTURE_DEFAULTBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 89CTASSERT(DB_CAPTURE_MAXBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 90 91/* 92 * Boot-time allocation of the DDB capture buffer, if any. 93 */ 94static void 95db_capture_sysinit(__unused void *dummy) 96{ 97 98 TUNABLE_INT_FETCH("debug.ddb.capture.bufsize", &db_capture_bufsize); 99 db_capture_bufsize = roundup(db_capture_bufsize, TEXTDUMP_BLOCKSIZE); 100 if (db_capture_bufsize > DB_CAPTURE_MAXBUFSIZE) 101 db_capture_bufsize = DB_CAPTURE_MAXBUFSIZE; 102 if (db_capture_bufsize != 0) 103 db_capture_buf = malloc(db_capture_bufsize, M_DB_CAPTURE, 104 M_WAITOK); 105} 106SYSINIT(db_capture, SI_SUB_DDB_SERVICES, SI_ORDER_ANY, db_capture_sysinit, 107 NULL); 108 109/* 110 * Run-time adjustment of the capture buffer. 111 */ 112static int 113sysctl_debug_ddb_capture_bufsize(SYSCTL_HANDLER_ARGS) 114{ 115 u_int len, size; 116 char *buf; 117 int error; 118 119 size = db_capture_bufsize; 120 error = sysctl_handle_int(oidp, &size, 0, req); 121 if (error || req->newptr == NULL) 122 return (error); 123 size = roundup(size, TEXTDUMP_BLOCKSIZE); 124 if (size > DB_CAPTURE_MAXBUFSIZE) 125 return (EINVAL); 126 sx_xlock(&db_capture_sx); 127 if (size != 0) { 128 /* 129 * Potentially the buffer is quite large, so if we can't 130 * allocate it, fail rather than waiting. 131 */ 132 buf = malloc(size, M_DB_CAPTURE, M_NOWAIT); 133 if (buf == NULL) { 134 sx_xunlock(&db_capture_sx); 135 return (ENOMEM); 136 } 137 len = min(db_capture_bufoff, size); 138 } else { 139 buf = NULL; 140 len = 0; 141 } 142 if (db_capture_buf != NULL && buf != NULL) 143 bcopy(db_capture_buf, buf, len); 144 if (db_capture_buf != NULL) 145 free(db_capture_buf, M_DB_CAPTURE); 146 db_capture_bufoff = len; 147 db_capture_buf = buf; 148 db_capture_bufsize = size; 149 sx_xunlock(&db_capture_sx); 150 151 KASSERT(db_capture_bufoff <= db_capture_bufsize, 152 ("sysctl_debug_ddb_capture_bufsize: bufoff > bufsize")); 153 KASSERT(db_capture_bufsize <= DB_CAPTURE_MAXBUFSIZE, 154 ("sysctl_debug_ddb_capture_maxbufsize: bufsize > maxbufsize")); 155 156 return (0); 157} 158SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, bufsize, CTLTYPE_UINT|CTLFLAG_RW, 159 0, 0, sysctl_debug_ddb_capture_bufsize, "IU", 160 "Size of DDB capture buffer"); 161 162/* 163 * Sysctl to read out the capture buffer from userspace. We require 164 * privilege as sensitive process/memory information may be accessed. 165 */ 166static int 167sysctl_debug_ddb_capture_data(SYSCTL_HANDLER_ARGS) 168{ 169 int error; 170 char ch; 171 172 error = priv_check(req->td, PRIV_DDB_CAPTURE); 173 if (error) 174 return (error); 175 176 sx_slock(&db_capture_sx); 177 error = SYSCTL_OUT(req, db_capture_buf, db_capture_bufoff); 178 sx_sunlock(&db_capture_sx); 179 if (error) 180 return (error); 181 ch = '\0'; 182 return (SYSCTL_OUT(req, &ch, sizeof(ch))); 183} 184SYSCTL_PROC(_debug_ddb_capture, OID_AUTO, data, CTLTYPE_STRING | CTLFLAG_RD, 185 NULL, 0, sysctl_debug_ddb_capture_data, "A", "DDB capture data"); 186 187/* 188 * Routines for capturing DDB output into a fixed-size buffer. These are 189 * invoked from DDB's input and output routines. If we hit the limit on the 190 * buffer, we simply drop further data. 191 */ 192void 193db_capture_write(char *buffer, u_int buflen) 194{ 195 u_int len; 196 197 if (db_capture_inprogress == 0 || db_capture_inpager) 198 return; 199 len = min(buflen, db_capture_bufsize - db_capture_bufoff); 200 bcopy(buffer, db_capture_buf + db_capture_bufoff, len); 201 db_capture_bufoff += len; 202 203 KASSERT(db_capture_bufoff <= db_capture_bufsize, 204 ("db_capture_write: bufoff > bufsize")); 205} 206 207void 208db_capture_writech(char ch) 209{ 210 211 return (db_capture_write(&ch, sizeof(ch))); 212} 213 214void 215db_capture_enterpager(void) 216{ 217 218 db_capture_inpager = 1; 219} 220 221void 222db_capture_exitpager(void) 223{ 224 225 db_capture_inpager = 0; 226} 227 228/* 229 * Zero out any bytes left in the last block of the DDB capture buffer. This 230 * is run shortly before writing the blocks to disk, rather than when output 231 * capture is stopped, in order to avoid injecting nul's into the middle of 232 * output. 233 */ 234static void 235db_capture_zeropad(void) 236{ 237 u_int len; 238 239 len = min(TEXTDUMP_BLOCKSIZE, (db_capture_bufsize - 240 db_capture_bufoff) % TEXTDUMP_BLOCKSIZE); 241 bzero(db_capture_buf + db_capture_bufoff, len); 242 db_capture_bufpadding = len; 243} 244 245/* 246 * Reset capture state, which flushes buffers. 247 */ 248static void 249db_capture_reset(void) 250{ 251 252 db_capture_inprogress = 0; 253 db_capture_bufoff = 0; 254 db_capture_bufpadding = 0; 255} 256 257/* 258 * Start capture. Only one session is allowed at any time, but we may 259 * continue a previous session, so the buffer isn't reset. 260 */ 261static void 262db_capture_start(void) 263{ 264 265 if (db_capture_inprogress) { 266 db_printf("Capture already started\n"); 267 return; 268 } 269 db_capture_inprogress = 1; 270} 271 272/* 273 * Terminate DDB output capture--real work is deferred to db_capture_dump, 274 * which executes outside of the DDB context. We don't zero pad here because 275 * capture may be started again before the dump takes place. 276 */ 277static void 278db_capture_stop(void) 279{ 280 281 if (db_capture_inprogress == 0) { 282 db_printf("Capture not started\n"); 283 return; 284 } 285 db_capture_inprogress = 0; 286} 287 288/* 289 * Dump DDB(4) captured output (and resets capture buffers). 290 */ 291void 292db_capture_dump(struct dumperinfo *di) 293{ 294 u_int offset; 295 296 if (db_capture_bufoff == 0) 297 return; 298 299 db_capture_zeropad(); 300 textdump_mkustar(textdump_block_buffer, DB_CAPTURE_FILENAME, 301 db_capture_bufoff); 302 (void)textdump_writenextblock(di, textdump_block_buffer); 303 for (offset = 0; offset < db_capture_bufoff + db_capture_bufpadding; 304 offset += TEXTDUMP_BLOCKSIZE) 305 (void)textdump_writenextblock(di, db_capture_buf + offset); 306 db_capture_bufoff = 0; 307 db_capture_bufpadding = 0; 308} 309 310/*- 311 * DDB(4) command to manage capture: 312 * 313 * capture on - start DDB output capture 314 * capture off - stop DDB output capture 315 * capture reset - reset DDB capture buffer (also stops capture) 316 * capture status - print DDB output capture status 317 */ 318static void 319db_capture_usage(void) 320{ 321 322 db_error("capture [on|off|reset|status]\n"); 323} 324 325void 326db_capture_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 327 char *modif) 328{ 329 int t; 330 331 t = db_read_token(); 332 if (t != tIDENT) { 333 db_capture_usage(); 334 return; 335 } 336 if (db_read_token() != tEOL) 337 db_error("?\n"); 338 if (strcmp(db_tok_string, "on") == 0) 339 db_capture_start(); 340 else if (strcmp(db_tok_string, "off") == 0) 341 db_capture_stop(); 342 else if (strcmp(db_tok_string, "reset") == 0) 343 db_capture_reset(); 344 else if (strcmp(db_tok_string, "status") == 0) { 345 db_printf("%u/%u bytes used\n", db_capture_bufoff, 346 db_capture_bufsize); 347 if (db_capture_inprogress) 348 db_printf("capture is on\n"); 349 else 350 db_printf("capture is off\n"); 351 } else 352 db_capture_usage(); 353} 354