1 /*- 2 * Copyright (c) 2007 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * DDB capture support: capture kernel debugger output into a fixed-size 29 * buffer for later dumping to disk or extraction from user space. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/conf.h> 37 #include <sys/kernel.h> 38 #include <sys/kerneldump.h> 39 #include <sys/malloc.h> 40 #include <sys/msgbuf.h> 41 #include <sys/priv.h> 42 #include <sys/sx.h> 43 #include <sys/sysctl.h> 44 #include <sys/systm.h> 45 46 #include <ddb/ddb.h> 47 #include <ddb/db_lex.h> 48 49 /* 50 * While it would be desirable to use a small block-sized buffer and dump 51 * incrementally to disk in fixed-size blocks, it's not possible to enter 52 * kernel dumper routines without restarting the kernel, which is undesirable 53 * in the midst of debugging. Instead, we maintain a large static global 54 * buffer that we fill from DDB's output routines. 55 */ 56 static MALLOC_DEFINE(M_DB_CAPTURE, "db_capture", "DDB capture buffer"); 57 58 #define DB_CAPTURE_DEFAULTBUFSIZE 48*1024 59 #define DB_CAPTURE_MAXBUFSIZE 512*1024 60 #define DB_CAPTURE_FILENAME "ddb.txt" /* Captured DDB output. */ 61 62 static char *db_capture_buf; 63 static u_int db_capture_bufsize = DB_CAPTURE_DEFAULTBUFSIZE; 64 static u_int db_capture_maxbufsize = DB_CAPTURE_MAXBUFSIZE; /* Read-only. */ 65 static u_int db_capture_bufoff; /* Next location to write in buffer. */ 66 static u_int db_capture_bufpadding; /* Amount of zero padding. */ 67 static int db_capture_inpager; /* Suspend capture in pager. */ 68 static int db_capture_inprogress; /* DDB capture currently in progress. */ 69 70 struct sx db_capture_sx; /* Lock against user thread races. */ 71 SX_SYSINIT(db_capture_sx, &db_capture_sx, "db_capture_sx"); 72 73 static SYSCTL_NODE(_debug_ddb, OID_AUTO, capture, CTLFLAG_RW, 0, 74 "DDB capture options"); 75 76 SYSCTL_UINT(_debug_ddb_capture, OID_AUTO, bytes, CTLFLAG_RD, 77 &db_capture_bufoff, 0, "Bytes of data in DDB capture buffer"); 78 79 SYSCTL_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 */ 88 CTASSERT(DB_CAPTURE_DEFAULTBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 89 CTASSERT(DB_CAPTURE_MAXBUFSIZE % TEXTDUMP_BLOCKSIZE == 0); 90 91 /* 92 * Boot-time allocation of the DDB capture buffer, if any. 93 */ 94 static void 95 db_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 } 106 SYSINIT(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 */ 112 static int 113 sysctl_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 } 158 SYSCTL_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 */ 166 static int 167 sysctl_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 } 184 SYSCTL_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 */ 192 void 193 db_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 207 void 208 db_capture_writech(char ch) 209 { 210 211 return (db_capture_write(&ch, sizeof(ch))); 212 } 213 214 void 215 db_capture_enterpager(void) 216 { 217 218 db_capture_inpager = 1; 219 } 220 221 void 222 db_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 */ 234 static void 235 db_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 */ 248 static void 249 db_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 */ 261 static void 262 db_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 */ 277 static void 278 db_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 */ 291 void 292 db_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 */ 318 static void 319 db_capture_usage(void) 320 { 321 322 db_error("capture [on|off|reset|status]\n"); 323 } 324 325 void 326 db_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