1618c7db3SRobert Watson /*- 2618c7db3SRobert Watson * Copyright (c) 2007 Robert N. M. Watson 3618c7db3SRobert Watson * All rights reserved. 4618c7db3SRobert Watson * 5618c7db3SRobert Watson * Redistribution and use in source and binary forms, with or without 6618c7db3SRobert Watson * modification, are permitted provided that the following conditions 7618c7db3SRobert Watson * are met: 8618c7db3SRobert Watson * 1. Redistributions of source code must retain the above copyright 9618c7db3SRobert Watson * notice, this list of conditions and the following disclaimer. 10618c7db3SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 11618c7db3SRobert Watson * notice, this list of conditions and the following disclaimer in the 12618c7db3SRobert Watson * documentation and/or other materials provided with the distribution. 13618c7db3SRobert Watson * 14618c7db3SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15618c7db3SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16618c7db3SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17618c7db3SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18618c7db3SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19618c7db3SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20618c7db3SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21618c7db3SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22618c7db3SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23618c7db3SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24618c7db3SRobert Watson * SUCH DAMAGE. 25618c7db3SRobert Watson */ 26618c7db3SRobert Watson 27618c7db3SRobert Watson /*- 289b0fce60SRobert Watson * Kernel text-dump support: write a series of text files to the dump 299b0fce60SRobert Watson * partition for later recovery, including captured DDB output, kernel 309b0fce60SRobert Watson * configuration, message buffer, and panic message. This allows for a more 319b0fce60SRobert Watson * compact representation of critical debugging information than traditional 329b0fce60SRobert Watson * binary dumps, as well as allowing dump information to be used without 339b0fce60SRobert Watson * access to kernel symbols, source code, etc. 34618c7db3SRobert Watson * 35618c7db3SRobert Watson * Storage Layout 36618c7db3SRobert Watson * -------------- 37618c7db3SRobert Watson * 38618c7db3SRobert Watson * Crash dumps are aligned to the end of the dump or swap partition in order 39618c7db3SRobert Watson * to minimize the chances of swap duing fsck eating into the dump. However, 40618c7db3SRobert Watson * unlike a memory dump, we don't know the size of the textdump a priori, so 41618c7db3SRobert Watson * can't just write it out sequentially in order from a known starting point 42618c7db3SRobert Watson * calculated with respect to the end of the partition. In order to address 43618c7db3SRobert Watson * this, we actually write out the textdump in reverse block order, allowing 44618c7db3SRobert Watson * us to directly align it to the end of the partition and then write out the 45618c7db3SRobert Watson * dump header and trailer before and after it once done. savecore(8) must 46618c7db3SRobert Watson * know to reverse the order of the blocks in order to produce a readable 47618c7db3SRobert Watson * file. 48618c7db3SRobert Watson * 499b0fce60SRobert Watson * Data is written out in the ustar file format so that we can write data 509b0fce60SRobert Watson * incrementally as a stream without reference to previous files. 51618c7db3SRobert Watson * 52618c7db3SRobert Watson * TODO 53618c7db3SRobert Watson * ---- 54618c7db3SRobert Watson * 55618c7db3SRobert Watson * - Allow subsytems to register to submit files for inclusion in the text 56618c7db3SRobert Watson * dump in a generic way. 57618c7db3SRobert Watson */ 58618c7db3SRobert Watson 59618c7db3SRobert Watson #include <sys/cdefs.h> 60618c7db3SRobert Watson __FBSDID("$FreeBSD$"); 61618c7db3SRobert Watson 62618c7db3SRobert Watson #include "opt_config.h" 63618c7db3SRobert Watson 64*21d748a9SAlfred Perlstein #include "opt_ddb.h" 65*21d748a9SAlfred Perlstein 66618c7db3SRobert Watson #include <sys/param.h> 67618c7db3SRobert Watson #include <sys/conf.h> 68618c7db3SRobert Watson #include <sys/kernel.h> 69618c7db3SRobert Watson #include <sys/kerneldump.h> 70618c7db3SRobert Watson #include <sys/msgbuf.h> 71618c7db3SRobert Watson #include <sys/sysctl.h> 72618c7db3SRobert Watson #include <sys/systm.h> 73618c7db3SRobert Watson 74618c7db3SRobert Watson #include <ddb/ddb.h> 75618c7db3SRobert Watson #include <ddb/db_lex.h> 76618c7db3SRobert Watson 77618c7db3SRobert Watson static SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0, 78618c7db3SRobert Watson "DDB textdump options"); 79618c7db3SRobert Watson 80618c7db3SRobert Watson /* 81618c7db3SRobert Watson * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is 82618c7db3SRobert Watson * to protect us from metadata and metadata from us. 83618c7db3SRobert Watson */ 84618c7db3SRobert Watson #define SIZEOF_METADATA (64*1024) 85618c7db3SRobert Watson 86618c7db3SRobert Watson /* 87618c7db3SRobert Watson * Data is written out as a series of files in the ustar tar format. ustar 88618c7db3SRobert Watson * is a simple streamed format consiting of a series of files prefixed with 89618c7db3SRobert Watson * headers, and all padded to 512-byte block boundaries, which maps 90618c7db3SRobert Watson * conveniently to our requirements. 91618c7db3SRobert Watson */ 92618c7db3SRobert Watson struct ustar_header { 93618c7db3SRobert Watson char uh_filename[100]; 94618c7db3SRobert Watson char uh_mode[8]; 95618c7db3SRobert Watson char uh_tar_owner[8]; 96618c7db3SRobert Watson char uh_tar_group[8]; 97618c7db3SRobert Watson char uh_size[12]; 98618c7db3SRobert Watson char uh_mtime[12]; 99618c7db3SRobert Watson char uh_sum[8]; 100618c7db3SRobert Watson char uh_type; 101618c7db3SRobert Watson char uh_linkfile[100]; 102618c7db3SRobert Watson char uh_ustar[6]; 103618c7db3SRobert Watson char uh_version[2]; 104618c7db3SRobert Watson char uh_owner[32]; 105618c7db3SRobert Watson char uh_group[32]; 106618c7db3SRobert Watson char uh_major[8]; 107618c7db3SRobert Watson char uh_minor[8]; 108618c7db3SRobert Watson char uh_filenameprefix[155]; 109618c7db3SRobert Watson char uh_zeropad[12]; 110618c7db3SRobert Watson } __packed; 111618c7db3SRobert Watson 112618c7db3SRobert Watson /* 113618c7db3SRobert Watson * Various size assertions -- pretty much everything must be one block in 114618c7db3SRobert Watson * size. 115618c7db3SRobert Watson */ 116618c7db3SRobert Watson CTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE); 117618c7db3SRobert Watson CTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE); 118618c7db3SRobert Watson 119618c7db3SRobert Watson /* 120618c7db3SRobert Watson * Is a textdump scheduled? If so, the shutdown code will invoke our dumpsys 121618c7db3SRobert Watson * routine instead of the machine-dependent kernel dump routine. 122618c7db3SRobert Watson */ 123*21d748a9SAlfred Perlstein #ifdef TEXTDUMP_PREFERRED 124*21d748a9SAlfred Perlstein int textdump_pending = 1; 125*21d748a9SAlfred Perlstein #else 126*21d748a9SAlfred Perlstein int textdump_pending = 0; 127*21d748a9SAlfred Perlstein #endif 128618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW, 129618c7db3SRobert Watson &textdump_pending, 0, 130618c7db3SRobert Watson "Perform textdump instead of regular kernel dump."); 131618c7db3SRobert Watson 132618c7db3SRobert Watson /* 133618c7db3SRobert Watson * Various constants for tar headers and contents. 134618c7db3SRobert Watson */ 135618c7db3SRobert Watson #define TAR_USER "root" 136618c7db3SRobert Watson #define TAR_GROUP "wheel" 137618c7db3SRobert Watson #define TAR_UID "0" 138618c7db3SRobert Watson #define TAR_GID "0" 139618c7db3SRobert Watson #define TAR_MODE "0600" 140618c7db3SRobert Watson #define TAR_USTAR "ustar" 141618c7db3SRobert Watson 142618c7db3SRobert Watson #define TAR_CONFIG_FILENAME "config.txt" /* Kernel configuration. */ 143618c7db3SRobert Watson #define TAR_MSGBUF_FILENAME "msgbuf.txt" /* Kernel messsage buffer. */ 144618c7db3SRobert Watson #define TAR_PANIC_FILENAME "panic.txt" /* Panic message. */ 145618c7db3SRobert Watson #define TAR_VERSION_FILENAME "version.txt" /* Kernel version. */ 146618c7db3SRobert Watson 147618c7db3SRobert Watson /* 148618c7db3SRobert Watson * Configure which files will be dumped. 149618c7db3SRobert Watson */ 150618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 151618c7db3SRobert Watson static int textdump_do_config = 1; 152618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW, 153618c7db3SRobert Watson &textdump_do_config, 0, "Dump kernel configuration in textdump"); 154618c7db3SRobert Watson #endif 155618c7db3SRobert Watson 156618c7db3SRobert Watson static int textdump_do_ddb = 1; 157618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW, 158618c7db3SRobert Watson &textdump_do_ddb, 0, "Dump DDB captured output in textdump"); 159618c7db3SRobert Watson 160618c7db3SRobert Watson static int textdump_do_msgbuf = 1; 161618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW, 162618c7db3SRobert Watson &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump"); 163618c7db3SRobert Watson 164618c7db3SRobert Watson static int textdump_do_panic = 1; 165618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW, 166618c7db3SRobert Watson &textdump_do_panic, 0, "Dump kernel panic message in textdump"); 167618c7db3SRobert Watson 168618c7db3SRobert Watson static int textdump_do_version = 1; 169618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW, 170618c7db3SRobert Watson &textdump_do_version, 0, "Dump kernel version string in textdump"); 171618c7db3SRobert Watson 172618c7db3SRobert Watson /* 173618c7db3SRobert Watson * State related to incremental writing of blocks to disk. 174618c7db3SRobert Watson */ 175618c7db3SRobert Watson static off_t textdump_offset; /* Offset of next sequential write. */ 176618c7db3SRobert Watson static int textdump_error; /* Carried write error, if any. */ 177618c7db3SRobert Watson 178618c7db3SRobert Watson /* 179618c7db3SRobert Watson * Statically allocate space to prepare block-sized headers and data. 180618c7db3SRobert Watson */ 181618c7db3SRobert Watson char textdump_block_buffer[TEXTDUMP_BLOCKSIZE]; 182618c7db3SRobert Watson static struct kerneldumpheader kdh; 183618c7db3SRobert Watson 184618c7db3SRobert Watson /* 1859b0fce60SRobert Watson * Calculate and fill in the checksum for a ustar header. 186618c7db3SRobert Watson */ 187618c7db3SRobert Watson static void 188618c7db3SRobert Watson ustar_checksum(struct ustar_header *uhp) 189618c7db3SRobert Watson { 190618c7db3SRobert Watson u_int sum; 191618c7db3SRobert Watson int i; 192618c7db3SRobert Watson 193618c7db3SRobert Watson for (i = 0; i < sizeof(uhp->uh_sum); i++) 194618c7db3SRobert Watson uhp->uh_sum[i] = ' '; 195618c7db3SRobert Watson sum = 0; 196618c7db3SRobert Watson for (i = 0; i < sizeof(*uhp); i++) 197618c7db3SRobert Watson sum += ((u_char *)uhp)[i]; 198618c7db3SRobert Watson snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum); 199618c7db3SRobert Watson } 200618c7db3SRobert Watson 201618c7db3SRobert Watson /* 202618c7db3SRobert Watson * Each file in the tarball has a block-sized header with its name and other, 203618c7db3SRobert Watson * largely hard-coded, properties. 204618c7db3SRobert Watson */ 205618c7db3SRobert Watson void 206618c7db3SRobert Watson textdump_mkustar(char *block_buffer, const char *filename, u_int size) 207618c7db3SRobert Watson { 208618c7db3SRobert Watson struct ustar_header *uhp; 209618c7db3SRobert Watson 210*21d748a9SAlfred Perlstein #ifdef TEXTDUMP_VERBOSE 211*21d748a9SAlfred Perlstein if (textdump_error == 0) 212*21d748a9SAlfred Perlstein printf("textdump: creating '%s'.\n", filename); 213*21d748a9SAlfred Perlstein #endif 214618c7db3SRobert Watson uhp = (struct ustar_header *)block_buffer; 215618c7db3SRobert Watson bzero(uhp, sizeof(*uhp)); 216618c7db3SRobert Watson strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename)); 217618c7db3SRobert Watson strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode)); 218618c7db3SRobert Watson snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size); 219618c7db3SRobert Watson strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner)); 220618c7db3SRobert Watson strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group)); 221618c7db3SRobert Watson strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner)); 222618c7db3SRobert Watson strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group)); 223618c7db3SRobert Watson snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo", 224618c7db3SRobert Watson (unsigned long)time_second); 225618c7db3SRobert Watson uhp->uh_type = 0; 226618c7db3SRobert Watson strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar)); 227618c7db3SRobert Watson ustar_checksum(uhp); 228618c7db3SRobert Watson } 229618c7db3SRobert Watson 230618c7db3SRobert Watson /* 231618c7db3SRobert Watson * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to 232618c7db3SRobert Watson * the space between di->mediaoffset and di->mediaoffset + di->mediasize. It 233618c7db3SRobert Watson * accepts an offset relative to di->mediaoffset. If we're carrying any 234618c7db3SRobert Watson * error from previous I/O, return that error and don't continue to try to 235618c7db3SRobert Watson * write. Most writers ignore the error and forge ahead on the basis that 236618c7db3SRobert Watson * there's not much you can do. 237618c7db3SRobert Watson */ 238618c7db3SRobert Watson static int 239618c7db3SRobert Watson textdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer) 240618c7db3SRobert Watson { 241618c7db3SRobert Watson 242618c7db3SRobert Watson if (textdump_error) 243618c7db3SRobert Watson return (textdump_error); 244618c7db3SRobert Watson if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize) 245618c7db3SRobert Watson return (EIO); 246618c7db3SRobert Watson if (offset < SIZEOF_METADATA) 247618c7db3SRobert Watson return (ENOSPC); 248990132f0SRobert Watson textdump_error = dump_write(di, buffer, 0, offset + di->mediaoffset, 249990132f0SRobert Watson TEXTDUMP_BLOCKSIZE); 250*21d748a9SAlfred Perlstein if (textdump_error) 251*21d748a9SAlfred Perlstein printf("textdump_writeblock: offset %jd, error %d\n", (intmax_t)offset, 252*21d748a9SAlfred Perlstein textdump_error); 253618c7db3SRobert Watson return (textdump_error); 254618c7db3SRobert Watson } 255618c7db3SRobert Watson 256618c7db3SRobert Watson /* 257618c7db3SRobert Watson * Interfaces to save and restore the dump offset, so that printers can go 258618c7db3SRobert Watson * back to rewrite a header if required, while avoiding their knowing about 259618c7db3SRobert Watson * the global layout of the blocks. 2609b0fce60SRobert Watson * 2619b0fce60SRobert Watson * If we ever want to support writing textdumps to tape or other 2629b0fce60SRobert Watson * stream-oriented target, we'll need to remove this. 263618c7db3SRobert Watson */ 264618c7db3SRobert Watson void 265618c7db3SRobert Watson textdump_saveoff(off_t *offsetp) 266618c7db3SRobert Watson { 267618c7db3SRobert Watson 268618c7db3SRobert Watson *offsetp = textdump_offset; 269618c7db3SRobert Watson } 270618c7db3SRobert Watson 271618c7db3SRobert Watson void 272618c7db3SRobert Watson textdump_restoreoff(off_t offset) 273618c7db3SRobert Watson { 274618c7db3SRobert Watson 275618c7db3SRobert Watson textdump_offset = offset; 276618c7db3SRobert Watson } 277618c7db3SRobert Watson 278618c7db3SRobert Watson /* 279618c7db3SRobert Watson * Interface to write the "next block" relative to the current offset; since 280618c7db3SRobert Watson * we write backwards from the end of the partition, we subtract, but there's 281618c7db3SRobert Watson * no reason for the caller to know this. 282618c7db3SRobert Watson */ 283618c7db3SRobert Watson int 284618c7db3SRobert Watson textdump_writenextblock(struct dumperinfo *di, char *buffer) 285618c7db3SRobert Watson { 286618c7db3SRobert Watson int error; 287618c7db3SRobert Watson 288618c7db3SRobert Watson error = textdump_writeblock(di, textdump_offset, buffer); 289618c7db3SRobert Watson textdump_offset -= TEXTDUMP_BLOCKSIZE; 290618c7db3SRobert Watson return (error); 291618c7db3SRobert Watson } 292618c7db3SRobert Watson 293618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 294618c7db3SRobert Watson extern char kernconfstring[]; 295618c7db3SRobert Watson 296618c7db3SRobert Watson /* 297618c7db3SRobert Watson * Dump kernel configuration. 298618c7db3SRobert Watson */ 299618c7db3SRobert Watson static void 300618c7db3SRobert Watson textdump_dump_config(struct dumperinfo *di) 301618c7db3SRobert Watson { 302618c7db3SRobert Watson u_int count, fullblocks, len; 303618c7db3SRobert Watson 304618c7db3SRobert Watson len = strlen(kernconfstring); 305618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len); 306618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 307618c7db3SRobert Watson 308618c7db3SRobert Watson /* 309618c7db3SRobert Watson * Write out all full blocks directly from the string, and handle any 310618c7db3SRobert Watson * left-over bits by copying it to out to the local buffer and 311618c7db3SRobert Watson * zero-padding it. 312618c7db3SRobert Watson */ 313618c7db3SRobert Watson fullblocks = len / TEXTDUMP_BLOCKSIZE; 314618c7db3SRobert Watson for (count = 0; count < fullblocks; count++) 315618c7db3SRobert Watson (void)textdump_writenextblock(di, kernconfstring + count * 316618c7db3SRobert Watson TEXTDUMP_BLOCKSIZE); 317618c7db3SRobert Watson if (len % TEXTDUMP_BLOCKSIZE != 0) { 318618c7db3SRobert Watson bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE); 319618c7db3SRobert Watson bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE, 320618c7db3SRobert Watson textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE); 321618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 322618c7db3SRobert Watson } 323618c7db3SRobert Watson } 324618c7db3SRobert Watson #endif /* INCLUDE_CONFIG_FILE */ 325618c7db3SRobert Watson 326618c7db3SRobert Watson /* 327618c7db3SRobert Watson * Dump kernel message buffer. 328618c7db3SRobert Watson */ 329618c7db3SRobert Watson static void 330618c7db3SRobert Watson textdump_dump_msgbuf(struct dumperinfo *di) 331618c7db3SRobert Watson { 332618c7db3SRobert Watson off_t end_offset, tarhdr_offset; 333618c7db3SRobert Watson u_int i, len, offset, seq, total_len; 334618c7db3SRobert Watson char buf[16]; 335618c7db3SRobert Watson 336618c7db3SRobert Watson /* 337618c7db3SRobert Watson * Write out a dummy tar header to advance the offset; we'll rewrite 338618c7db3SRobert Watson * it later once we know the true size. 339618c7db3SRobert Watson */ 340618c7db3SRobert Watson textdump_saveoff(&tarhdr_offset); 341618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0); 342618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 343618c7db3SRobert Watson 344618c7db3SRobert Watson /* 345618c7db3SRobert Watson * Copy out the data in small chunks, but don't copy nuls that may be 346618c7db3SRobert Watson * present if the message buffer has not yet completely filled at 347618c7db3SRobert Watson * least once. 348618c7db3SRobert Watson */ 349618c7db3SRobert Watson total_len = 0; 350618c7db3SRobert Watson offset = 0; 351618c7db3SRobert Watson msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 352618c7db3SRobert Watson while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 353618c7db3SRobert Watson for (i = 0; i < len; i++) { 354618c7db3SRobert Watson if (buf[i] == '\0') 355618c7db3SRobert Watson continue; 356618c7db3SRobert Watson textdump_block_buffer[offset] = buf[i]; 357618c7db3SRobert Watson offset++; 358618c7db3SRobert Watson if (offset != sizeof(textdump_block_buffer)) 359618c7db3SRobert Watson continue; 360618c7db3SRobert Watson (void)textdump_writenextblock(di, 361618c7db3SRobert Watson textdump_block_buffer); 362618c7db3SRobert Watson total_len += offset; 363618c7db3SRobert Watson offset = 0; 364618c7db3SRobert Watson } 365618c7db3SRobert Watson } 366618c7db3SRobert Watson total_len += offset; /* Without the zero-padding. */ 367618c7db3SRobert Watson if (offset != 0) { 368618c7db3SRobert Watson bzero(textdump_block_buffer + offset, 369618c7db3SRobert Watson sizeof(textdump_block_buffer) - offset); 370618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 371618c7db3SRobert Watson } 372618c7db3SRobert Watson 373618c7db3SRobert Watson /* 374618c7db3SRobert Watson * Rewrite tar header to reflect how much was actually written. 375618c7db3SRobert Watson */ 376618c7db3SRobert Watson textdump_saveoff(&end_offset); 377618c7db3SRobert Watson textdump_restoreoff(tarhdr_offset); 378618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 379618c7db3SRobert Watson total_len); 380618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 381618c7db3SRobert Watson textdump_restoreoff(end_offset); 382618c7db3SRobert Watson } 383618c7db3SRobert Watson 384618c7db3SRobert Watson static void 385618c7db3SRobert Watson textdump_dump_panic(struct dumperinfo *di) 386618c7db3SRobert Watson { 387618c7db3SRobert Watson u_int len; 388618c7db3SRobert Watson 389618c7db3SRobert Watson /* 390618c7db3SRobert Watson * Write out tar header -- we store up to one block of panic message. 391618c7db3SRobert Watson */ 392618c7db3SRobert Watson len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE); 393618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len); 394618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 395618c7db3SRobert Watson 396618c7db3SRobert Watson /* 397618c7db3SRobert Watson * Zero-pad the panic string and write out block. 398618c7db3SRobert Watson */ 399618c7db3SRobert Watson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 400618c7db3SRobert Watson bcopy(panicstr, textdump_block_buffer, len); 401618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 402618c7db3SRobert Watson } 403618c7db3SRobert Watson 404618c7db3SRobert Watson static void 405618c7db3SRobert Watson textdump_dump_version(struct dumperinfo *di) 406618c7db3SRobert Watson { 407618c7db3SRobert Watson u_int len; 408618c7db3SRobert Watson 409618c7db3SRobert Watson /* 410618c7db3SRobert Watson * Write out tar header -- at most one block of version information. 411618c7db3SRobert Watson */ 412618c7db3SRobert Watson len = min(strlen(version), TEXTDUMP_BLOCKSIZE); 413618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len); 414618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 415618c7db3SRobert Watson 416618c7db3SRobert Watson /* 417618c7db3SRobert Watson * Zero pad the version string and write out block. 418618c7db3SRobert Watson */ 419618c7db3SRobert Watson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 420618c7db3SRobert Watson bcopy(version, textdump_block_buffer, len); 421618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 422618c7db3SRobert Watson } 423618c7db3SRobert Watson 424618c7db3SRobert Watson /* 425618c7db3SRobert Watson * Commit text dump to disk. 426618c7db3SRobert Watson */ 427618c7db3SRobert Watson void 428618c7db3SRobert Watson textdump_dumpsys(struct dumperinfo *di) 429618c7db3SRobert Watson { 430618c7db3SRobert Watson off_t dumplen, trailer_offset; 431618c7db3SRobert Watson 432618c7db3SRobert Watson if (di->blocksize != TEXTDUMP_BLOCKSIZE) { 433618c7db3SRobert Watson printf("Dump partition block size (%ju) not textdump " 434618c7db3SRobert Watson "block size (%ju)", (uintmax_t)di->blocksize, 435618c7db3SRobert Watson (uintmax_t)TEXTDUMP_BLOCKSIZE); 436618c7db3SRobert Watson return; 437618c7db3SRobert Watson } 438618c7db3SRobert Watson 439618c7db3SRobert Watson /* 440618c7db3SRobert Watson * We don't know a priori how large the dump will be, but we do know 441618c7db3SRobert Watson * that we need to reserve space for metadata and that we need two 442618c7db3SRobert Watson * dump headers. Also leave room for one ustar header and one block 443618c7db3SRobert Watson * of data. 444618c7db3SRobert Watson */ 445618c7db3SRobert Watson if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) { 446*21d748a9SAlfred Perlstein printf("Insufficient space on dump partition for minimal textdump.\n"); 447618c7db3SRobert Watson return; 448618c7db3SRobert Watson } 449618c7db3SRobert Watson textdump_error = 0; 450618c7db3SRobert Watson 451618c7db3SRobert Watson /* 452618c7db3SRobert Watson * Position the start of the dump so that we'll write the kernel dump 453618c7db3SRobert Watson * trailer immediately before the end of the partition, and then work 454618c7db3SRobert Watson * our way back. We will rewrite this header later to reflect the 455618c7db3SRobert Watson * true size if things go well. 456618c7db3SRobert Watson */ 457618c7db3SRobert Watson textdump_offset = di->mediasize - sizeof(kdh); 458618c7db3SRobert Watson textdump_saveoff(&trailer_offset); 459e6592ee5SPeter Wemm mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); 460618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 461618c7db3SRobert Watson 462618c7db3SRobert Watson /* 463618c7db3SRobert Watson * Write a series of files in ustar format. 464618c7db3SRobert Watson */ 465618c7db3SRobert Watson if (textdump_do_ddb) 466618c7db3SRobert Watson db_capture_dump(di); 467618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 468618c7db3SRobert Watson if (textdump_do_config) 469618c7db3SRobert Watson textdump_dump_config(di); 470618c7db3SRobert Watson #endif 471618c7db3SRobert Watson if (textdump_do_msgbuf) 472618c7db3SRobert Watson textdump_dump_msgbuf(di); 473618c7db3SRobert Watson if (textdump_do_panic && panicstr != NULL) 474618c7db3SRobert Watson textdump_dump_panic(di); 475618c7db3SRobert Watson if (textdump_do_version) 476618c7db3SRobert Watson textdump_dump_version(di); 477618c7db3SRobert Watson 478618c7db3SRobert Watson /* 479618c7db3SRobert Watson * Now that we know the true size, we can write out the header, then 480618c7db3SRobert Watson * seek back to the end and rewrite the trailer with the correct 481618c7db3SRobert Watson * size. 482618c7db3SRobert Watson */ 483618c7db3SRobert Watson dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE); 484e6592ee5SPeter Wemm mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, dumplen, 485618c7db3SRobert Watson TEXTDUMP_BLOCKSIZE); 486618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 487618c7db3SRobert Watson textdump_restoreoff(trailer_offset); 488618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 489618c7db3SRobert Watson 490618c7db3SRobert Watson /* 491618c7db3SRobert Watson * Terminate the dump, report any errors, and clear the pending flag. 492618c7db3SRobert Watson */ 493618c7db3SRobert Watson if (textdump_error == 0) 494990132f0SRobert Watson (void)dump_write(di, NULL, 0, 0, 0); 495618c7db3SRobert Watson if (textdump_error == ENOSPC) 496*21d748a9SAlfred Perlstein printf("Textdump: Insufficient space on dump partition\n"); 497618c7db3SRobert Watson else if (textdump_error != 0) 498*21d748a9SAlfred Perlstein printf("Textdump: Error %d writing dump\n", textdump_error); 499618c7db3SRobert Watson else 500618c7db3SRobert Watson printf("Textdump complete.\n"); 501618c7db3SRobert Watson textdump_pending = 0; 502618c7db3SRobert Watson } 503618c7db3SRobert Watson 504618c7db3SRobert Watson /*- 505618c7db3SRobert Watson * DDB(4) command to manage textdumps: 506618c7db3SRobert Watson * 507618c7db3SRobert Watson * textdump set - request a textdump 508618c7db3SRobert Watson * textdump status - print DDB output textdump status 509618c7db3SRobert Watson * textdump unset - clear textdump request 510618c7db3SRobert Watson */ 511618c7db3SRobert Watson static void 512618c7db3SRobert Watson db_textdump_usage(void) 513618c7db3SRobert Watson { 514618c7db3SRobert Watson 515*21d748a9SAlfred Perlstein db_printf("textdump [unset|set|status|dump]\n"); 516618c7db3SRobert Watson } 517618c7db3SRobert Watson 518618c7db3SRobert Watson void 519618c7db3SRobert Watson db_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 520618c7db3SRobert Watson char *modif) 521618c7db3SRobert Watson { 522618c7db3SRobert Watson int t; 523618c7db3SRobert Watson 524618c7db3SRobert Watson t = db_read_token(); 525618c7db3SRobert Watson if (t != tIDENT) { 526618c7db3SRobert Watson db_textdump_usage(); 527618c7db3SRobert Watson return; 528618c7db3SRobert Watson } 529618c7db3SRobert Watson if (db_read_token() != tEOL) { 530618c7db3SRobert Watson db_textdump_usage(); 531618c7db3SRobert Watson return; 532618c7db3SRobert Watson } 533618c7db3SRobert Watson if (strcmp(db_tok_string, "set") == 0) { 534618c7db3SRobert Watson textdump_pending = 1; 535618c7db3SRobert Watson db_printf("textdump set\n"); 536618c7db3SRobert Watson } else if (strcmp(db_tok_string, "status") == 0) { 537618c7db3SRobert Watson if (textdump_pending) 538618c7db3SRobert Watson db_printf("textdump is set\n"); 539618c7db3SRobert Watson else 540618c7db3SRobert Watson db_printf("textdump is not set\n"); 541618c7db3SRobert Watson } else if (strcmp(db_tok_string, "unset") == 0) { 542618c7db3SRobert Watson textdump_pending = 0; 543618c7db3SRobert Watson db_printf("textdump unset\n"); 544*21d748a9SAlfred Perlstein } else if (strcmp(db_tok_string, "dump") == 0) { 545*21d748a9SAlfred Perlstein textdump_pending = 1; 546*21d748a9SAlfred Perlstein doadump(TRUE); 547*21d748a9SAlfred Perlstein } else { 548618c7db3SRobert Watson db_textdump_usage(); 549618c7db3SRobert Watson } 550*21d748a9SAlfred Perlstein } 551