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 /*- 28618c7db3SRobert Watson * Kernel text-dump support: allow a series of text files to be written to 29618c7db3SRobert Watson * the dump partition for later recovery, including captured DDB output, the 30618c7db3SRobert Watson * kernel configuration, message buffer, panic message, etc. This allows for 31618c7db3SRobert Watson * a more compact representation of critical debugging information than 32618c7db3SRobert Watson * traditional binary dumps, as well as allowing dump information to be used 33618c7db3SRobert Watson * without 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 * 49618c7db3SRobert Watson * Data is written out in the 'tar' file format, as it provides the facility 50618c7db3SRobert Watson * to write data incrementally as a stream without reference to previous 51618c7db3SRobert Watson * files. 52618c7db3SRobert Watson * 53618c7db3SRobert Watson * TODO 54618c7db3SRobert Watson * ---- 55618c7db3SRobert Watson * 56618c7db3SRobert Watson * - Allow subsytems to register to submit files for inclusion in the text 57618c7db3SRobert Watson * dump in a generic way. 58618c7db3SRobert Watson */ 59618c7db3SRobert Watson 60618c7db3SRobert Watson #include <sys/cdefs.h> 61618c7db3SRobert Watson __FBSDID("$FreeBSD$"); 62618c7db3SRobert Watson 63618c7db3SRobert Watson #include "opt_config.h" 64618c7db3SRobert Watson 65618c7db3SRobert Watson #include <sys/param.h> 66618c7db3SRobert Watson #include <sys/conf.h> 67618c7db3SRobert Watson #include <sys/kernel.h> 68618c7db3SRobert Watson #include <sys/kerneldump.h> 69618c7db3SRobert Watson #include <sys/msgbuf.h> 70618c7db3SRobert Watson #include <sys/sysctl.h> 71618c7db3SRobert Watson #include <sys/systm.h> 72618c7db3SRobert Watson 73618c7db3SRobert Watson #include <ddb/ddb.h> 74618c7db3SRobert Watson #include <ddb/db_lex.h> 75618c7db3SRobert Watson 76618c7db3SRobert Watson static SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0, 77618c7db3SRobert Watson "DDB textdump options"); 78618c7db3SRobert Watson 79618c7db3SRobert Watson /* 80618c7db3SRobert Watson * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is 81618c7db3SRobert Watson * to protect us from metadata and metadata from us. 82618c7db3SRobert Watson */ 83618c7db3SRobert Watson #define SIZEOF_METADATA (64*1024) 84618c7db3SRobert Watson 85618c7db3SRobert Watson /* 86618c7db3SRobert Watson * Data is written out as a series of files in the ustar tar format. ustar 87618c7db3SRobert Watson * is a simple streamed format consiting of a series of files prefixed with 88618c7db3SRobert Watson * headers, and all padded to 512-byte block boundaries, which maps 89618c7db3SRobert Watson * conveniently to our requirements. 90618c7db3SRobert Watson */ 91618c7db3SRobert Watson struct ustar_header { 92618c7db3SRobert Watson char uh_filename[100]; 93618c7db3SRobert Watson char uh_mode[8]; 94618c7db3SRobert Watson char uh_tar_owner[8]; 95618c7db3SRobert Watson char uh_tar_group[8]; 96618c7db3SRobert Watson char uh_size[12]; 97618c7db3SRobert Watson char uh_mtime[12]; 98618c7db3SRobert Watson char uh_sum[8]; 99618c7db3SRobert Watson char uh_type; 100618c7db3SRobert Watson char uh_linkfile[100]; 101618c7db3SRobert Watson char uh_ustar[6]; 102618c7db3SRobert Watson char uh_version[2]; 103618c7db3SRobert Watson char uh_owner[32]; 104618c7db3SRobert Watson char uh_group[32]; 105618c7db3SRobert Watson char uh_major[8]; 106618c7db3SRobert Watson char uh_minor[8]; 107618c7db3SRobert Watson char uh_filenameprefix[155]; 108618c7db3SRobert Watson char uh_zeropad[12]; 109618c7db3SRobert Watson } __packed; 110618c7db3SRobert Watson 111618c7db3SRobert Watson /* 112618c7db3SRobert Watson * Various size assertions -- pretty much everything must be one block in 113618c7db3SRobert Watson * size. 114618c7db3SRobert Watson */ 115618c7db3SRobert Watson CTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE); 116618c7db3SRobert Watson CTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE); 117618c7db3SRobert Watson 118618c7db3SRobert Watson /* 119618c7db3SRobert Watson * Is a textdump scheduled? If so, the shutdown code will invoke our dumpsys 120618c7db3SRobert Watson * routine instead of the machine-dependent kernel dump routine. 121618c7db3SRobert Watson */ 122618c7db3SRobert Watson int textdump_pending; 123618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW, 124618c7db3SRobert Watson &textdump_pending, 0, 125618c7db3SRobert Watson "Perform textdump instead of regular kernel dump."); 126618c7db3SRobert Watson 127618c7db3SRobert Watson /* 128618c7db3SRobert Watson * Various constants for tar headers and contents. 129618c7db3SRobert Watson */ 130618c7db3SRobert Watson #define TAR_USER "root" 131618c7db3SRobert Watson #define TAR_GROUP "wheel" 132618c7db3SRobert Watson #define TAR_UID "0" 133618c7db3SRobert Watson #define TAR_GID "0" 134618c7db3SRobert Watson #define TAR_MODE "0600" 135618c7db3SRobert Watson #define TAR_USTAR "ustar" 136618c7db3SRobert Watson 137618c7db3SRobert Watson #define TAR_CONFIG_FILENAME "config.txt" /* Kernel configuration. */ 138618c7db3SRobert Watson #define TAR_MSGBUF_FILENAME "msgbuf.txt" /* Kernel messsage buffer. */ 139618c7db3SRobert Watson #define TAR_PANIC_FILENAME "panic.txt" /* Panic message. */ 140618c7db3SRobert Watson #define TAR_VERSION_FILENAME "version.txt" /* Kernel version. */ 141618c7db3SRobert Watson 142618c7db3SRobert Watson /* 143618c7db3SRobert Watson * Configure which files will be dumped. 144618c7db3SRobert Watson */ 145618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 146618c7db3SRobert Watson static int textdump_do_config = 1; 147618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW, 148618c7db3SRobert Watson &textdump_do_config, 0, "Dump kernel configuration in textdump"); 149618c7db3SRobert Watson #endif 150618c7db3SRobert Watson 151618c7db3SRobert Watson static int textdump_do_ddb = 1; 152618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW, 153618c7db3SRobert Watson &textdump_do_ddb, 0, "Dump DDB captured output in textdump"); 154618c7db3SRobert Watson 155618c7db3SRobert Watson static int textdump_do_msgbuf = 1; 156618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW, 157618c7db3SRobert Watson &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump"); 158618c7db3SRobert Watson 159618c7db3SRobert Watson static int textdump_do_panic = 1; 160618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW, 161618c7db3SRobert Watson &textdump_do_panic, 0, "Dump kernel panic message in textdump"); 162618c7db3SRobert Watson 163618c7db3SRobert Watson static int textdump_do_version = 1; 164618c7db3SRobert Watson SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW, 165618c7db3SRobert Watson &textdump_do_version, 0, "Dump kernel version string in textdump"); 166618c7db3SRobert Watson 167618c7db3SRobert Watson /* 168618c7db3SRobert Watson * State related to incremental writing of blocks to disk. 169618c7db3SRobert Watson */ 170618c7db3SRobert Watson static off_t textdump_offset; /* Offset of next sequential write. */ 171618c7db3SRobert Watson static int textdump_error; /* Carried write error, if any. */ 172618c7db3SRobert Watson 173618c7db3SRobert Watson /* 174618c7db3SRobert Watson * Statically allocate space to prepare block-sized headers and data. 175618c7db3SRobert Watson */ 176618c7db3SRobert Watson char textdump_block_buffer[TEXTDUMP_BLOCKSIZE]; 177618c7db3SRobert Watson static struct kerneldumpheader kdh; 178618c7db3SRobert Watson 179618c7db3SRobert Watson /* 180618c7db3SRobert Watson * Text dumps are prefixed with a normal kernel dump header but with a 181618c7db3SRobert Watson * different magic number to allow them to be uniquely identified. 182618c7db3SRobert Watson */ 183618c7db3SRobert Watson static void 184618c7db3SRobert Watson mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, 185618c7db3SRobert Watson uint64_t dumplen, uint32_t blksz) 186618c7db3SRobert Watson { 187618c7db3SRobert Watson 188618c7db3SRobert Watson bzero(kdh, sizeof(*kdh)); 189618c7db3SRobert Watson strncpy(kdh->magic, TEXTDUMPMAGIC, sizeof(kdh->magic)); 190618c7db3SRobert Watson strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 191618c7db3SRobert Watson kdh->version = htod32(KERNELDUMPVERSION); 192618c7db3SRobert Watson kdh->architectureversion = htod32(archver); 193618c7db3SRobert Watson kdh->dumplength = htod64(dumplen); 194618c7db3SRobert Watson kdh->dumptime = htod64(time_second); 195618c7db3SRobert Watson kdh->blocksize = htod32(blksz); 196618c7db3SRobert Watson strncpy(kdh->hostname, hostname, sizeof(kdh->hostname)); 197618c7db3SRobert Watson strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 198618c7db3SRobert Watson if (panicstr != NULL) 199618c7db3SRobert Watson strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 200618c7db3SRobert Watson kdh->parity = kerneldump_parity(kdh); 201618c7db3SRobert Watson } 202618c7db3SRobert Watson 203618c7db3SRobert Watson /* 204618c7db3SRobert Watson * Calculate and fill in the checksum for a tar header. 205618c7db3SRobert Watson */ 206618c7db3SRobert Watson static void 207618c7db3SRobert Watson ustar_checksum(struct ustar_header *uhp) 208618c7db3SRobert Watson { 209618c7db3SRobert Watson u_int sum; 210618c7db3SRobert Watson int i; 211618c7db3SRobert Watson 212618c7db3SRobert Watson for (i = 0; i < sizeof(uhp->uh_sum); i++) 213618c7db3SRobert Watson uhp->uh_sum[i] = ' '; 214618c7db3SRobert Watson sum = 0; 215618c7db3SRobert Watson for (i = 0; i < sizeof(*uhp); i++) 216618c7db3SRobert Watson sum += ((u_char *)uhp)[i]; 217618c7db3SRobert Watson snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum); 218618c7db3SRobert Watson } 219618c7db3SRobert Watson 220618c7db3SRobert Watson /* 221618c7db3SRobert Watson * Each file in the tarball has a block-sized header with its name and other, 222618c7db3SRobert Watson * largely hard-coded, properties. 223618c7db3SRobert Watson */ 224618c7db3SRobert Watson void 225618c7db3SRobert Watson textdump_mkustar(char *block_buffer, const char *filename, u_int size) 226618c7db3SRobert Watson { 227618c7db3SRobert Watson struct ustar_header *uhp; 228618c7db3SRobert Watson 229618c7db3SRobert Watson uhp = (struct ustar_header *)block_buffer; 230618c7db3SRobert Watson bzero(uhp, sizeof(*uhp)); 231618c7db3SRobert Watson strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename)); 232618c7db3SRobert Watson strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode)); 233618c7db3SRobert Watson snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size); 234618c7db3SRobert Watson strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner)); 235618c7db3SRobert Watson strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group)); 236618c7db3SRobert Watson strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner)); 237618c7db3SRobert Watson strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group)); 238618c7db3SRobert Watson snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo", 239618c7db3SRobert Watson (unsigned long)time_second); 240618c7db3SRobert Watson uhp->uh_type = 0; 241618c7db3SRobert Watson strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar)); 242618c7db3SRobert Watson ustar_checksum(uhp); 243618c7db3SRobert Watson } 244618c7db3SRobert Watson 245618c7db3SRobert Watson /* 246618c7db3SRobert Watson * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to 247618c7db3SRobert Watson * the space between di->mediaoffset and di->mediaoffset + di->mediasize. It 248618c7db3SRobert Watson * accepts an offset relative to di->mediaoffset. If we're carrying any 249618c7db3SRobert Watson * error from previous I/O, return that error and don't continue to try to 250618c7db3SRobert Watson * write. Most writers ignore the error and forge ahead on the basis that 251618c7db3SRobert Watson * there's not much you can do. 252618c7db3SRobert Watson */ 253618c7db3SRobert Watson static int 254618c7db3SRobert Watson textdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer) 255618c7db3SRobert Watson { 256618c7db3SRobert Watson 257618c7db3SRobert Watson if (textdump_error) 258618c7db3SRobert Watson return (textdump_error); 259618c7db3SRobert Watson if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize) 260618c7db3SRobert Watson return (EIO); 261618c7db3SRobert Watson if (offset < SIZEOF_METADATA) 262618c7db3SRobert Watson return (ENOSPC); 263618c7db3SRobert Watson textdump_error = di->dumper(di->priv, buffer, 0, offset + 264618c7db3SRobert Watson di->mediaoffset, TEXTDUMP_BLOCKSIZE); 265618c7db3SRobert Watson return (textdump_error); 266618c7db3SRobert Watson } 267618c7db3SRobert Watson 268618c7db3SRobert Watson /* 269618c7db3SRobert Watson * Interfaces to save and restore the dump offset, so that printers can go 270618c7db3SRobert Watson * back to rewrite a header if required, while avoiding their knowing about 271618c7db3SRobert Watson * the global layout of the blocks. 272618c7db3SRobert Watson */ 273618c7db3SRobert Watson void 274618c7db3SRobert Watson textdump_saveoff(off_t *offsetp) 275618c7db3SRobert Watson { 276618c7db3SRobert Watson 277618c7db3SRobert Watson *offsetp = textdump_offset; 278618c7db3SRobert Watson } 279618c7db3SRobert Watson 280618c7db3SRobert Watson void 281618c7db3SRobert Watson textdump_restoreoff(off_t offset) 282618c7db3SRobert Watson { 283618c7db3SRobert Watson 284618c7db3SRobert Watson textdump_offset = offset; 285618c7db3SRobert Watson } 286618c7db3SRobert Watson 287618c7db3SRobert Watson /* 288618c7db3SRobert Watson * Interface to write the "next block" relative to the current offset; since 289618c7db3SRobert Watson * we write backwards from the end of the partition, we subtract, but there's 290618c7db3SRobert Watson * no reason for the caller to know this. 291618c7db3SRobert Watson */ 292618c7db3SRobert Watson int 293618c7db3SRobert Watson textdump_writenextblock(struct dumperinfo *di, char *buffer) 294618c7db3SRobert Watson { 295618c7db3SRobert Watson int error; 296618c7db3SRobert Watson 297618c7db3SRobert Watson error = textdump_writeblock(di, textdump_offset, buffer); 298618c7db3SRobert Watson textdump_offset -= TEXTDUMP_BLOCKSIZE; 299618c7db3SRobert Watson return (error); 300618c7db3SRobert Watson } 301618c7db3SRobert Watson 302618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 303618c7db3SRobert Watson extern char kernconfstring[]; 304618c7db3SRobert Watson 305618c7db3SRobert Watson /* 306618c7db3SRobert Watson * Dump kernel configuration. 307618c7db3SRobert Watson */ 308618c7db3SRobert Watson static void 309618c7db3SRobert Watson textdump_dump_config(struct dumperinfo *di) 310618c7db3SRobert Watson { 311618c7db3SRobert Watson u_int count, fullblocks, len; 312618c7db3SRobert Watson 313618c7db3SRobert Watson len = strlen(kernconfstring); 314618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len); 315618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 316618c7db3SRobert Watson 317618c7db3SRobert Watson /* 318618c7db3SRobert Watson * Write out all full blocks directly from the string, and handle any 319618c7db3SRobert Watson * left-over bits by copying it to out to the local buffer and 320618c7db3SRobert Watson * zero-padding it. 321618c7db3SRobert Watson */ 322618c7db3SRobert Watson fullblocks = len / TEXTDUMP_BLOCKSIZE; 323618c7db3SRobert Watson for (count = 0; count < fullblocks; count++) 324618c7db3SRobert Watson (void)textdump_writenextblock(di, kernconfstring + count * 325618c7db3SRobert Watson TEXTDUMP_BLOCKSIZE); 326618c7db3SRobert Watson if (len % TEXTDUMP_BLOCKSIZE != 0) { 327618c7db3SRobert Watson bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE); 328618c7db3SRobert Watson bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE, 329618c7db3SRobert Watson textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE); 330618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 331618c7db3SRobert Watson } 332618c7db3SRobert Watson } 333618c7db3SRobert Watson #endif /* INCLUDE_CONFIG_FILE */ 334618c7db3SRobert Watson 335618c7db3SRobert Watson /* 336618c7db3SRobert Watson * Dump kernel message buffer. 337618c7db3SRobert Watson */ 338618c7db3SRobert Watson static void 339618c7db3SRobert Watson textdump_dump_msgbuf(struct dumperinfo *di) 340618c7db3SRobert Watson { 341618c7db3SRobert Watson off_t end_offset, tarhdr_offset; 342618c7db3SRobert Watson u_int i, len, offset, seq, total_len; 343618c7db3SRobert Watson char buf[16]; 344618c7db3SRobert Watson 345618c7db3SRobert Watson /* 346618c7db3SRobert Watson * Write out a dummy tar header to advance the offset; we'll rewrite 347618c7db3SRobert Watson * it later once we know the true size. 348618c7db3SRobert Watson */ 349618c7db3SRobert Watson textdump_saveoff(&tarhdr_offset); 350618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0); 351618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 352618c7db3SRobert Watson 353618c7db3SRobert Watson /* 354618c7db3SRobert Watson * Copy out the data in small chunks, but don't copy nuls that may be 355618c7db3SRobert Watson * present if the message buffer has not yet completely filled at 356618c7db3SRobert Watson * least once. 357618c7db3SRobert Watson */ 358618c7db3SRobert Watson total_len = 0; 359618c7db3SRobert Watson offset = 0; 360618c7db3SRobert Watson msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 361618c7db3SRobert Watson while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 362618c7db3SRobert Watson for (i = 0; i < len; i++) { 363618c7db3SRobert Watson if (buf[i] == '\0') 364618c7db3SRobert Watson continue; 365618c7db3SRobert Watson textdump_block_buffer[offset] = buf[i]; 366618c7db3SRobert Watson offset++; 367618c7db3SRobert Watson if (offset != sizeof(textdump_block_buffer)) 368618c7db3SRobert Watson continue; 369618c7db3SRobert Watson (void)textdump_writenextblock(di, 370618c7db3SRobert Watson textdump_block_buffer); 371618c7db3SRobert Watson total_len += offset; 372618c7db3SRobert Watson offset = 0; 373618c7db3SRobert Watson } 374618c7db3SRobert Watson } 375618c7db3SRobert Watson total_len += offset; /* Without the zero-padding. */ 376618c7db3SRobert Watson if (offset != 0) { 377618c7db3SRobert Watson bzero(textdump_block_buffer + offset, 378618c7db3SRobert Watson sizeof(textdump_block_buffer) - offset); 379618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 380618c7db3SRobert Watson } 381618c7db3SRobert Watson 382618c7db3SRobert Watson /* 383618c7db3SRobert Watson * Rewrite tar header to reflect how much was actually written. 384618c7db3SRobert Watson */ 385618c7db3SRobert Watson textdump_saveoff(&end_offset); 386618c7db3SRobert Watson textdump_restoreoff(tarhdr_offset); 387618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 388618c7db3SRobert Watson total_len); 389618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 390618c7db3SRobert Watson textdump_restoreoff(end_offset); 391618c7db3SRobert Watson } 392618c7db3SRobert Watson 393618c7db3SRobert Watson static void 394618c7db3SRobert Watson textdump_dump_panic(struct dumperinfo *di) 395618c7db3SRobert Watson { 396618c7db3SRobert Watson u_int len; 397618c7db3SRobert Watson 398618c7db3SRobert Watson /* 399618c7db3SRobert Watson * Write out tar header -- we store up to one block of panic message. 400618c7db3SRobert Watson */ 401618c7db3SRobert Watson len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE); 402618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len); 403618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 404618c7db3SRobert Watson 405618c7db3SRobert Watson /* 406618c7db3SRobert Watson * Zero-pad the panic string and write out block. 407618c7db3SRobert Watson */ 408618c7db3SRobert Watson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 409618c7db3SRobert Watson bcopy(panicstr, textdump_block_buffer, len); 410618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 411618c7db3SRobert Watson } 412618c7db3SRobert Watson 413618c7db3SRobert Watson static void 414618c7db3SRobert Watson textdump_dump_version(struct dumperinfo *di) 415618c7db3SRobert Watson { 416618c7db3SRobert Watson u_int len; 417618c7db3SRobert Watson 418618c7db3SRobert Watson /* 419618c7db3SRobert Watson * Write out tar header -- at most one block of version information. 420618c7db3SRobert Watson */ 421618c7db3SRobert Watson len = min(strlen(version), TEXTDUMP_BLOCKSIZE); 422618c7db3SRobert Watson textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len); 423618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 424618c7db3SRobert Watson 425618c7db3SRobert Watson /* 426618c7db3SRobert Watson * Zero pad the version string and write out block. 427618c7db3SRobert Watson */ 428618c7db3SRobert Watson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 429618c7db3SRobert Watson bcopy(version, textdump_block_buffer, len); 430618c7db3SRobert Watson (void)textdump_writenextblock(di, textdump_block_buffer); 431618c7db3SRobert Watson } 432618c7db3SRobert Watson 433618c7db3SRobert Watson /* 434618c7db3SRobert Watson * Commit text dump to disk. 435618c7db3SRobert Watson */ 436618c7db3SRobert Watson void 437618c7db3SRobert Watson textdump_dumpsys(struct dumperinfo *di) 438618c7db3SRobert Watson { 439618c7db3SRobert Watson off_t dumplen, trailer_offset; 440618c7db3SRobert Watson 441618c7db3SRobert Watson if (di->blocksize != TEXTDUMP_BLOCKSIZE) { 442618c7db3SRobert Watson printf("Dump partition block size (%ju) not textdump " 443618c7db3SRobert Watson "block size (%ju)", (uintmax_t)di->blocksize, 444618c7db3SRobert Watson (uintmax_t)TEXTDUMP_BLOCKSIZE); 445618c7db3SRobert Watson return; 446618c7db3SRobert Watson } 447618c7db3SRobert Watson 448618c7db3SRobert Watson /* 449618c7db3SRobert Watson * We don't know a priori how large the dump will be, but we do know 450618c7db3SRobert Watson * that we need to reserve space for metadata and that we need two 451618c7db3SRobert Watson * dump headers. Also leave room for one ustar header and one block 452618c7db3SRobert Watson * of data. 453618c7db3SRobert Watson */ 454618c7db3SRobert Watson if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) { 455618c7db3SRobert Watson printf("Insufficient space on dump partition.\n"); 456618c7db3SRobert Watson return; 457618c7db3SRobert Watson } 458618c7db3SRobert Watson textdump_error = 0; 459618c7db3SRobert Watson 460618c7db3SRobert Watson /* 461618c7db3SRobert Watson * Position the start of the dump so that we'll write the kernel dump 462618c7db3SRobert Watson * trailer immediately before the end of the partition, and then work 463618c7db3SRobert Watson * our way back. We will rewrite this header later to reflect the 464618c7db3SRobert Watson * true size if things go well. 465618c7db3SRobert Watson */ 466618c7db3SRobert Watson textdump_offset = di->mediasize - sizeof(kdh); 467618c7db3SRobert Watson textdump_saveoff(&trailer_offset); 468618c7db3SRobert Watson mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); 469618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 470618c7db3SRobert Watson 471618c7db3SRobert Watson /* 472618c7db3SRobert Watson * Write a series of files in ustar format. 473618c7db3SRobert Watson */ 474618c7db3SRobert Watson if (textdump_do_ddb) 475618c7db3SRobert Watson db_capture_dump(di); 476618c7db3SRobert Watson #ifdef INCLUDE_CONFIG_FILE 477618c7db3SRobert Watson if (textdump_do_config) 478618c7db3SRobert Watson textdump_dump_config(di); 479618c7db3SRobert Watson #endif 480618c7db3SRobert Watson if (textdump_do_msgbuf) 481618c7db3SRobert Watson textdump_dump_msgbuf(di); 482618c7db3SRobert Watson if (textdump_do_panic && panicstr != NULL) 483618c7db3SRobert Watson textdump_dump_panic(di); 484618c7db3SRobert Watson if (textdump_do_version) 485618c7db3SRobert Watson textdump_dump_version(di); 486618c7db3SRobert Watson 487618c7db3SRobert Watson /* 488618c7db3SRobert Watson * Now that we know the true size, we can write out the header, then 489618c7db3SRobert Watson * seek back to the end and rewrite the trailer with the correct 490618c7db3SRobert Watson * size. 491618c7db3SRobert Watson */ 492618c7db3SRobert Watson dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE); 493618c7db3SRobert Watson mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, dumplen, 494618c7db3SRobert Watson TEXTDUMP_BLOCKSIZE); 495618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 496618c7db3SRobert Watson textdump_restoreoff(trailer_offset); 497618c7db3SRobert Watson (void)textdump_writenextblock(di, (char *)&kdh); 498618c7db3SRobert Watson 499618c7db3SRobert Watson /* 500618c7db3SRobert Watson * Terminate the dump, report any errors, and clear the pending flag. 501618c7db3SRobert Watson */ 502618c7db3SRobert Watson if (textdump_error == 0) 503618c7db3SRobert Watson (void)di->dumper(di->priv, NULL, 0, 0, 0); 504618c7db3SRobert Watson if (textdump_error == ENOSPC) 505618c7db3SRobert Watson printf("Insufficient space on dump partition\n"); 506618c7db3SRobert Watson else if (textdump_error != 0) 507618c7db3SRobert Watson printf("Error %d writing dump\n", textdump_error); 508618c7db3SRobert Watson else 509618c7db3SRobert Watson printf("Textdump complete.\n"); 510618c7db3SRobert Watson textdump_pending = 0; 511618c7db3SRobert Watson } 512618c7db3SRobert Watson 513618c7db3SRobert Watson /*- 514618c7db3SRobert Watson * DDB(4) command to manage textdumps: 515618c7db3SRobert Watson * 516618c7db3SRobert Watson * textdump set - request a textdump 517618c7db3SRobert Watson * textdump status - print DDB output textdump status 518618c7db3SRobert Watson * textdump unset - clear textdump request 519618c7db3SRobert Watson */ 520618c7db3SRobert Watson static void 521618c7db3SRobert Watson db_textdump_usage(void) 522618c7db3SRobert Watson { 523618c7db3SRobert Watson 524618c7db3SRobert Watson db_printf("textdump [unset|set|status]\n"); 525618c7db3SRobert Watson } 526618c7db3SRobert Watson 527618c7db3SRobert Watson void 528618c7db3SRobert Watson db_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 529618c7db3SRobert Watson char *modif) 530618c7db3SRobert Watson { 531618c7db3SRobert Watson int t; 532618c7db3SRobert Watson 533618c7db3SRobert Watson t = db_read_token(); 534618c7db3SRobert Watson if (t != tIDENT) { 535618c7db3SRobert Watson db_textdump_usage(); 536618c7db3SRobert Watson return; 537618c7db3SRobert Watson } 538618c7db3SRobert Watson if (db_read_token() != tEOL) { 539618c7db3SRobert Watson db_textdump_usage(); 540618c7db3SRobert Watson return; 541618c7db3SRobert Watson } 542618c7db3SRobert Watson if (strcmp(db_tok_string, "set") == 0) { 543618c7db3SRobert Watson textdump_pending = 1; 544618c7db3SRobert Watson db_printf("textdump set\n"); 545618c7db3SRobert Watson } else if (strcmp(db_tok_string, "status") == 0) { 546618c7db3SRobert Watson if (textdump_pending) 547618c7db3SRobert Watson db_printf("textdump is set\n"); 548618c7db3SRobert Watson else 549618c7db3SRobert Watson db_printf("textdump is not set\n"); 550618c7db3SRobert Watson } else if (strcmp(db_tok_string, "unset") == 0) { 551618c7db3SRobert Watson textdump_pending = 0; 552618c7db3SRobert Watson db_printf("textdump unset\n"); 553618c7db3SRobert Watson } else 554618c7db3SRobert Watson db_textdump_usage(); 555618c7db3SRobert Watson } 556