1174921Srwatson/*- 2174921Srwatson * Copyright (c) 2007 Robert N. M. Watson 3174921Srwatson * All rights reserved. 4174921Srwatson * 5174921Srwatson * Redistribution and use in source and binary forms, with or without 6174921Srwatson * modification, are permitted provided that the following conditions 7174921Srwatson * are met: 8174921Srwatson * 1. Redistributions of source code must retain the above copyright 9174921Srwatson * notice, this list of conditions and the following disclaimer. 10174921Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11174921Srwatson * notice, this list of conditions and the following disclaimer in the 12174921Srwatson * documentation and/or other materials provided with the distribution. 13174921Srwatson * 14174921Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15174921Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16174921Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17174921Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18174921Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19174921Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20174921Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21174921Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22174921Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23174921Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24174921Srwatson * SUCH DAMAGE. 25174921Srwatson */ 26174921Srwatson 27174921Srwatson/*- 28175199Srwatson * Kernel text-dump support: write a series of text files to the dump 29175199Srwatson * partition for later recovery, including captured DDB output, kernel 30175199Srwatson * configuration, message buffer, and panic message. This allows for a more 31175199Srwatson * compact representation of critical debugging information than traditional 32175199Srwatson * binary dumps, as well as allowing dump information to be used without 33175199Srwatson * access to kernel symbols, source code, etc. 34174921Srwatson * 35174921Srwatson * Storage Layout 36174921Srwatson * -------------- 37174921Srwatson * 38174921Srwatson * Crash dumps are aligned to the end of the dump or swap partition in order 39174921Srwatson * to minimize the chances of swap duing fsck eating into the dump. However, 40174921Srwatson * unlike a memory dump, we don't know the size of the textdump a priori, so 41174921Srwatson * can't just write it out sequentially in order from a known starting point 42174921Srwatson * calculated with respect to the end of the partition. In order to address 43174921Srwatson * this, we actually write out the textdump in reverse block order, allowing 44174921Srwatson * us to directly align it to the end of the partition and then write out the 45174921Srwatson * dump header and trailer before and after it once done. savecore(8) must 46174921Srwatson * know to reverse the order of the blocks in order to produce a readable 47174921Srwatson * file. 48174921Srwatson * 49175199Srwatson * Data is written out in the ustar file format so that we can write data 50175199Srwatson * incrementally as a stream without reference to previous files. 51174921Srwatson * 52174921Srwatson * TODO 53174921Srwatson * ---- 54174921Srwatson * 55174921Srwatson * - Allow subsytems to register to submit files for inclusion in the text 56174921Srwatson * dump in a generic way. 57174921Srwatson */ 58174921Srwatson 59174921Srwatson#include <sys/cdefs.h> 60174921Srwatson__FBSDID("$FreeBSD$"); 61174921Srwatson 62174921Srwatson#include "opt_config.h" 63174921Srwatson 64242424Salfred#include "opt_ddb.h" 65242424Salfred 66174921Srwatson#include <sys/param.h> 67174921Srwatson#include <sys/conf.h> 68174921Srwatson#include <sys/kernel.h> 69174921Srwatson#include <sys/kerneldump.h> 70174921Srwatson#include <sys/msgbuf.h> 71174921Srwatson#include <sys/sysctl.h> 72174921Srwatson#include <sys/systm.h> 73174921Srwatson 74174921Srwatson#include <ddb/ddb.h> 75174921Srwatson#include <ddb/db_lex.h> 76174921Srwatson 77174921Srwatsonstatic SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0, 78174921Srwatson "DDB textdump options"); 79174921Srwatson 80174921Srwatson/* 81174921Srwatson * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is 82174921Srwatson * to protect us from metadata and metadata from us. 83174921Srwatson */ 84174921Srwatson#define SIZEOF_METADATA (64*1024) 85174921Srwatson 86174921Srwatson/* 87174921Srwatson * Data is written out as a series of files in the ustar tar format. ustar 88174921Srwatson * is a simple streamed format consiting of a series of files prefixed with 89174921Srwatson * headers, and all padded to 512-byte block boundaries, which maps 90174921Srwatson * conveniently to our requirements. 91174921Srwatson */ 92174921Srwatsonstruct ustar_header { 93174921Srwatson char uh_filename[100]; 94174921Srwatson char uh_mode[8]; 95174921Srwatson char uh_tar_owner[8]; 96174921Srwatson char uh_tar_group[8]; 97174921Srwatson char uh_size[12]; 98174921Srwatson char uh_mtime[12]; 99174921Srwatson char uh_sum[8]; 100174921Srwatson char uh_type; 101174921Srwatson char uh_linkfile[100]; 102174921Srwatson char uh_ustar[6]; 103174921Srwatson char uh_version[2]; 104174921Srwatson char uh_owner[32]; 105174921Srwatson char uh_group[32]; 106174921Srwatson char uh_major[8]; 107174921Srwatson char uh_minor[8]; 108174921Srwatson char uh_filenameprefix[155]; 109174921Srwatson char uh_zeropad[12]; 110174921Srwatson} __packed; 111174921Srwatson 112174921Srwatson/* 113174921Srwatson * Various size assertions -- pretty much everything must be one block in 114174921Srwatson * size. 115174921Srwatson */ 116174921SrwatsonCTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE); 117174921SrwatsonCTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE); 118174921Srwatson 119174921Srwatson/* 120174921Srwatson * Is a textdump scheduled? If so, the shutdown code will invoke our dumpsys 121174921Srwatson * routine instead of the machine-dependent kernel dump routine. 122174921Srwatson */ 123242424Salfred#ifdef TEXTDUMP_PREFERRED 124242424Salfredint textdump_pending = 1; 125242424Salfred#else 126242424Salfredint textdump_pending = 0; 127242424Salfred#endif 128174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW, 129174921Srwatson &textdump_pending, 0, 130174921Srwatson "Perform textdump instead of regular kernel dump."); 131174921Srwatson 132174921Srwatson/* 133174921Srwatson * Various constants for tar headers and contents. 134174921Srwatson */ 135174921Srwatson#define TAR_USER "root" 136174921Srwatson#define TAR_GROUP "wheel" 137174921Srwatson#define TAR_UID "0" 138174921Srwatson#define TAR_GID "0" 139174921Srwatson#define TAR_MODE "0600" 140174921Srwatson#define TAR_USTAR "ustar" 141174921Srwatson 142174921Srwatson#define TAR_CONFIG_FILENAME "config.txt" /* Kernel configuration. */ 143174921Srwatson#define TAR_MSGBUF_FILENAME "msgbuf.txt" /* Kernel messsage buffer. */ 144174921Srwatson#define TAR_PANIC_FILENAME "panic.txt" /* Panic message. */ 145174921Srwatson#define TAR_VERSION_FILENAME "version.txt" /* Kernel version. */ 146174921Srwatson 147174921Srwatson/* 148174921Srwatson * Configure which files will be dumped. 149174921Srwatson */ 150174921Srwatson#ifdef INCLUDE_CONFIG_FILE 151174921Srwatsonstatic int textdump_do_config = 1; 152174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW, 153174921Srwatson &textdump_do_config, 0, "Dump kernel configuration in textdump"); 154174921Srwatson#endif 155174921Srwatson 156174921Srwatsonstatic int textdump_do_ddb = 1; 157174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW, 158174921Srwatson &textdump_do_ddb, 0, "Dump DDB captured output in textdump"); 159174921Srwatson 160174921Srwatsonstatic int textdump_do_msgbuf = 1; 161174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW, 162174921Srwatson &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump"); 163174921Srwatson 164174921Srwatsonstatic int textdump_do_panic = 1; 165174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW, 166174921Srwatson &textdump_do_panic, 0, "Dump kernel panic message in textdump"); 167174921Srwatson 168174921Srwatsonstatic int textdump_do_version = 1; 169174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW, 170174921Srwatson &textdump_do_version, 0, "Dump kernel version string in textdump"); 171174921Srwatson 172174921Srwatson/* 173174921Srwatson * State related to incremental writing of blocks to disk. 174174921Srwatson */ 175174921Srwatsonstatic off_t textdump_offset; /* Offset of next sequential write. */ 176174921Srwatsonstatic int textdump_error; /* Carried write error, if any. */ 177174921Srwatson 178174921Srwatson/* 179174921Srwatson * Statically allocate space to prepare block-sized headers and data. 180174921Srwatson */ 181174921Srwatsonchar textdump_block_buffer[TEXTDUMP_BLOCKSIZE]; 182174921Srwatsonstatic struct kerneldumpheader kdh; 183174921Srwatson 184174921Srwatson/* 185175199Srwatson * Calculate and fill in the checksum for a ustar header. 186174921Srwatson */ 187174921Srwatsonstatic void 188174921Srwatsonustar_checksum(struct ustar_header *uhp) 189174921Srwatson{ 190174921Srwatson u_int sum; 191174921Srwatson int i; 192174921Srwatson 193174921Srwatson for (i = 0; i < sizeof(uhp->uh_sum); i++) 194174921Srwatson uhp->uh_sum[i] = ' '; 195174921Srwatson sum = 0; 196174921Srwatson for (i = 0; i < sizeof(*uhp); i++) 197174921Srwatson sum += ((u_char *)uhp)[i]; 198174921Srwatson snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum); 199174921Srwatson} 200174921Srwatson 201174921Srwatson/* 202174921Srwatson * Each file in the tarball has a block-sized header with its name and other, 203174921Srwatson * largely hard-coded, properties. 204174921Srwatson */ 205174921Srwatsonvoid 206174921Srwatsontextdump_mkustar(char *block_buffer, const char *filename, u_int size) 207174921Srwatson{ 208174921Srwatson struct ustar_header *uhp; 209174921Srwatson 210242424Salfred#ifdef TEXTDUMP_VERBOSE 211242424Salfred if (textdump_error == 0) 212242424Salfred printf("textdump: creating '%s'.\n", filename); 213242424Salfred#endif 214174921Srwatson uhp = (struct ustar_header *)block_buffer; 215174921Srwatson bzero(uhp, sizeof(*uhp)); 216174921Srwatson strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename)); 217174921Srwatson strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode)); 218174921Srwatson snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size); 219174921Srwatson strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner)); 220174921Srwatson strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group)); 221174921Srwatson strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner)); 222174921Srwatson strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group)); 223174921Srwatson snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo", 224174921Srwatson (unsigned long)time_second); 225174921Srwatson uhp->uh_type = 0; 226174921Srwatson strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar)); 227174921Srwatson ustar_checksum(uhp); 228174921Srwatson} 229174921Srwatson 230174921Srwatson/* 231174921Srwatson * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to 232174921Srwatson * the space between di->mediaoffset and di->mediaoffset + di->mediasize. It 233174921Srwatson * accepts an offset relative to di->mediaoffset. If we're carrying any 234174921Srwatson * error from previous I/O, return that error and don't continue to try to 235174921Srwatson * write. Most writers ignore the error and forge ahead on the basis that 236174921Srwatson * there's not much you can do. 237174921Srwatson */ 238174921Srwatsonstatic int 239174921Srwatsontextdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer) 240174921Srwatson{ 241174921Srwatson 242174921Srwatson if (textdump_error) 243174921Srwatson return (textdump_error); 244174921Srwatson if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize) 245174921Srwatson return (EIO); 246174921Srwatson if (offset < SIZEOF_METADATA) 247174921Srwatson return (ENOSPC); 248175858Srwatson textdump_error = dump_write(di, buffer, 0, offset + di->mediaoffset, 249175858Srwatson TEXTDUMP_BLOCKSIZE); 250242424Salfred if (textdump_error) 251242424Salfred printf("textdump_writeblock: offset %jd, error %d\n", (intmax_t)offset, 252242424Salfred textdump_error); 253174921Srwatson return (textdump_error); 254174921Srwatson} 255174921Srwatson 256174921Srwatson/* 257174921Srwatson * Interfaces to save and restore the dump offset, so that printers can go 258174921Srwatson * back to rewrite a header if required, while avoiding their knowing about 259174921Srwatson * the global layout of the blocks. 260175199Srwatson * 261175199Srwatson * If we ever want to support writing textdumps to tape or other 262175199Srwatson * stream-oriented target, we'll need to remove this. 263174921Srwatson */ 264174921Srwatsonvoid 265174921Srwatsontextdump_saveoff(off_t *offsetp) 266174921Srwatson{ 267174921Srwatson 268174921Srwatson *offsetp = textdump_offset; 269174921Srwatson} 270174921Srwatson 271174921Srwatsonvoid 272174921Srwatsontextdump_restoreoff(off_t offset) 273174921Srwatson{ 274174921Srwatson 275174921Srwatson textdump_offset = offset; 276174921Srwatson} 277174921Srwatson 278174921Srwatson/* 279174921Srwatson * Interface to write the "next block" relative to the current offset; since 280174921Srwatson * we write backwards from the end of the partition, we subtract, but there's 281174921Srwatson * no reason for the caller to know this. 282174921Srwatson */ 283174921Srwatsonint 284174921Srwatsontextdump_writenextblock(struct dumperinfo *di, char *buffer) 285174921Srwatson{ 286174921Srwatson int error; 287174921Srwatson 288174921Srwatson error = textdump_writeblock(di, textdump_offset, buffer); 289174921Srwatson textdump_offset -= TEXTDUMP_BLOCKSIZE; 290174921Srwatson return (error); 291174921Srwatson} 292174921Srwatson 293174921Srwatson#ifdef INCLUDE_CONFIG_FILE 294174921Srwatsonextern char kernconfstring[]; 295174921Srwatson 296174921Srwatson/* 297174921Srwatson * Dump kernel configuration. 298174921Srwatson */ 299174921Srwatsonstatic void 300174921Srwatsontextdump_dump_config(struct dumperinfo *di) 301174921Srwatson{ 302174921Srwatson u_int count, fullblocks, len; 303174921Srwatson 304174921Srwatson len = strlen(kernconfstring); 305174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len); 306174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 307174921Srwatson 308174921Srwatson /* 309174921Srwatson * Write out all full blocks directly from the string, and handle any 310174921Srwatson * left-over bits by copying it to out to the local buffer and 311174921Srwatson * zero-padding it. 312174921Srwatson */ 313174921Srwatson fullblocks = len / TEXTDUMP_BLOCKSIZE; 314174921Srwatson for (count = 0; count < fullblocks; count++) 315174921Srwatson (void)textdump_writenextblock(di, kernconfstring + count * 316174921Srwatson TEXTDUMP_BLOCKSIZE); 317174921Srwatson if (len % TEXTDUMP_BLOCKSIZE != 0) { 318174921Srwatson bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE); 319174921Srwatson bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE, 320174921Srwatson textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE); 321174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 322174921Srwatson } 323174921Srwatson} 324174921Srwatson#endif /* INCLUDE_CONFIG_FILE */ 325174921Srwatson 326174921Srwatson/* 327174921Srwatson * Dump kernel message buffer. 328174921Srwatson */ 329174921Srwatsonstatic void 330174921Srwatsontextdump_dump_msgbuf(struct dumperinfo *di) 331174921Srwatson{ 332174921Srwatson off_t end_offset, tarhdr_offset; 333174921Srwatson u_int i, len, offset, seq, total_len; 334174921Srwatson char buf[16]; 335174921Srwatson 336174921Srwatson /* 337174921Srwatson * Write out a dummy tar header to advance the offset; we'll rewrite 338174921Srwatson * it later once we know the true size. 339174921Srwatson */ 340174921Srwatson textdump_saveoff(&tarhdr_offset); 341174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0); 342174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 343174921Srwatson 344174921Srwatson /* 345174921Srwatson * Copy out the data in small chunks, but don't copy nuls that may be 346174921Srwatson * present if the message buffer has not yet completely filled at 347174921Srwatson * least once. 348174921Srwatson */ 349174921Srwatson total_len = 0; 350174921Srwatson offset = 0; 351174921Srwatson msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 352174921Srwatson while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 353174921Srwatson for (i = 0; i < len; i++) { 354174921Srwatson if (buf[i] == '\0') 355174921Srwatson continue; 356174921Srwatson textdump_block_buffer[offset] = buf[i]; 357174921Srwatson offset++; 358174921Srwatson if (offset != sizeof(textdump_block_buffer)) 359174921Srwatson continue; 360174921Srwatson (void)textdump_writenextblock(di, 361174921Srwatson textdump_block_buffer); 362174921Srwatson total_len += offset; 363174921Srwatson offset = 0; 364174921Srwatson } 365174921Srwatson } 366174921Srwatson total_len += offset; /* Without the zero-padding. */ 367174921Srwatson if (offset != 0) { 368174921Srwatson bzero(textdump_block_buffer + offset, 369174921Srwatson sizeof(textdump_block_buffer) - offset); 370174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 371174921Srwatson } 372174921Srwatson 373174921Srwatson /* 374174921Srwatson * Rewrite tar header to reflect how much was actually written. 375174921Srwatson */ 376174921Srwatson textdump_saveoff(&end_offset); 377174921Srwatson textdump_restoreoff(tarhdr_offset); 378174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 379174921Srwatson total_len); 380174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 381174921Srwatson textdump_restoreoff(end_offset); 382174921Srwatson} 383174921Srwatson 384174921Srwatsonstatic void 385174921Srwatsontextdump_dump_panic(struct dumperinfo *di) 386174921Srwatson{ 387174921Srwatson u_int len; 388174921Srwatson 389174921Srwatson /* 390174921Srwatson * Write out tar header -- we store up to one block of panic message. 391174921Srwatson */ 392174921Srwatson len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE); 393174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len); 394174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 395174921Srwatson 396174921Srwatson /* 397174921Srwatson * Zero-pad the panic string and write out block. 398174921Srwatson */ 399174921Srwatson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 400174921Srwatson bcopy(panicstr, textdump_block_buffer, len); 401174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 402174921Srwatson} 403174921Srwatson 404174921Srwatsonstatic void 405174921Srwatsontextdump_dump_version(struct dumperinfo *di) 406174921Srwatson{ 407174921Srwatson u_int len; 408174921Srwatson 409174921Srwatson /* 410174921Srwatson * Write out tar header -- at most one block of version information. 411174921Srwatson */ 412174921Srwatson len = min(strlen(version), TEXTDUMP_BLOCKSIZE); 413174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len); 414174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 415174921Srwatson 416174921Srwatson /* 417174921Srwatson * Zero pad the version string and write out block. 418174921Srwatson */ 419174921Srwatson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 420174921Srwatson bcopy(version, textdump_block_buffer, len); 421174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 422174921Srwatson} 423174921Srwatson 424174921Srwatson/* 425174921Srwatson * Commit text dump to disk. 426174921Srwatson */ 427174921Srwatsonvoid 428174921Srwatsontextdump_dumpsys(struct dumperinfo *di) 429174921Srwatson{ 430174921Srwatson off_t dumplen, trailer_offset; 431174921Srwatson 432174921Srwatson if (di->blocksize != TEXTDUMP_BLOCKSIZE) { 433174921Srwatson printf("Dump partition block size (%ju) not textdump " 434174921Srwatson "block size (%ju)", (uintmax_t)di->blocksize, 435174921Srwatson (uintmax_t)TEXTDUMP_BLOCKSIZE); 436174921Srwatson return; 437174921Srwatson } 438174921Srwatson 439174921Srwatson /* 440174921Srwatson * We don't know a priori how large the dump will be, but we do know 441174921Srwatson * that we need to reserve space for metadata and that we need two 442174921Srwatson * dump headers. Also leave room for one ustar header and one block 443174921Srwatson * of data. 444174921Srwatson */ 445174921Srwatson if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) { 446242424Salfred printf("Insufficient space on dump partition for minimal textdump.\n"); 447174921Srwatson return; 448174921Srwatson } 449174921Srwatson textdump_error = 0; 450174921Srwatson 451174921Srwatson /* 452174921Srwatson * Position the start of the dump so that we'll write the kernel dump 453174921Srwatson * trailer immediately before the end of the partition, and then work 454174921Srwatson * our way back. We will rewrite this header later to reflect the 455174921Srwatson * true size if things go well. 456174921Srwatson */ 457174921Srwatson textdump_offset = di->mediasize - sizeof(kdh); 458174921Srwatson textdump_saveoff(&trailer_offset); 459183527Speter mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); 460174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 461174921Srwatson 462174921Srwatson /* 463174921Srwatson * Write a series of files in ustar format. 464174921Srwatson */ 465174921Srwatson if (textdump_do_ddb) 466174921Srwatson db_capture_dump(di); 467174921Srwatson#ifdef INCLUDE_CONFIG_FILE 468174921Srwatson if (textdump_do_config) 469174921Srwatson textdump_dump_config(di); 470174921Srwatson#endif 471174921Srwatson if (textdump_do_msgbuf) 472174921Srwatson textdump_dump_msgbuf(di); 473174921Srwatson if (textdump_do_panic && panicstr != NULL) 474174921Srwatson textdump_dump_panic(di); 475174921Srwatson if (textdump_do_version) 476174921Srwatson textdump_dump_version(di); 477174921Srwatson 478174921Srwatson /* 479174921Srwatson * Now that we know the true size, we can write out the header, then 480174921Srwatson * seek back to the end and rewrite the trailer with the correct 481174921Srwatson * size. 482174921Srwatson */ 483174921Srwatson dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE); 484183527Speter mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, dumplen, 485174921Srwatson TEXTDUMP_BLOCKSIZE); 486174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 487174921Srwatson textdump_restoreoff(trailer_offset); 488174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 489174921Srwatson 490174921Srwatson /* 491174921Srwatson * Terminate the dump, report any errors, and clear the pending flag. 492174921Srwatson */ 493174921Srwatson if (textdump_error == 0) 494175858Srwatson (void)dump_write(di, NULL, 0, 0, 0); 495174921Srwatson if (textdump_error == ENOSPC) 496242424Salfred printf("Textdump: Insufficient space on dump partition\n"); 497174921Srwatson else if (textdump_error != 0) 498242424Salfred printf("Textdump: Error %d writing dump\n", textdump_error); 499174921Srwatson else 500174921Srwatson printf("Textdump complete.\n"); 501174921Srwatson textdump_pending = 0; 502174921Srwatson} 503174921Srwatson 504174921Srwatson/*- 505174921Srwatson * DDB(4) command to manage textdumps: 506174921Srwatson * 507174921Srwatson * textdump set - request a textdump 508174921Srwatson * textdump status - print DDB output textdump status 509174921Srwatson * textdump unset - clear textdump request 510174921Srwatson */ 511174921Srwatsonstatic void 512174921Srwatsondb_textdump_usage(void) 513174921Srwatson{ 514174921Srwatson 515242424Salfred db_printf("textdump [unset|set|status|dump]\n"); 516174921Srwatson} 517174921Srwatson 518174921Srwatsonvoid 519174921Srwatsondb_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 520174921Srwatson char *modif) 521174921Srwatson{ 522174921Srwatson int t; 523174921Srwatson 524174921Srwatson t = db_read_token(); 525174921Srwatson if (t != tIDENT) { 526174921Srwatson db_textdump_usage(); 527174921Srwatson return; 528174921Srwatson } 529174921Srwatson if (db_read_token() != tEOL) { 530174921Srwatson db_textdump_usage(); 531174921Srwatson return; 532174921Srwatson } 533174921Srwatson if (strcmp(db_tok_string, "set") == 0) { 534174921Srwatson textdump_pending = 1; 535174921Srwatson db_printf("textdump set\n"); 536174921Srwatson } else if (strcmp(db_tok_string, "status") == 0) { 537174921Srwatson if (textdump_pending) 538174921Srwatson db_printf("textdump is set\n"); 539174921Srwatson else 540174921Srwatson db_printf("textdump is not set\n"); 541174921Srwatson } else if (strcmp(db_tok_string, "unset") == 0) { 542174921Srwatson textdump_pending = 0; 543174921Srwatson db_printf("textdump unset\n"); 544242424Salfred } else if (strcmp(db_tok_string, "dump") == 0) { 545242424Salfred textdump_pending = 1; 546242424Salfred doadump(TRUE); 547242424Salfred } else { 548174921Srwatson db_textdump_usage(); 549242424Salfred } 550174921Srwatson} 551