db_textdump.c revision 174921
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/*- 28174921Srwatson * Kernel text-dump support: allow a series of text files to be written to 29174921Srwatson * the dump partition for later recovery, including captured DDB output, the 30174921Srwatson * kernel configuration, message buffer, panic message, etc. This allows for 31174921Srwatson * a more compact representation of critical debugging information than 32174921Srwatson * traditional binary dumps, as well as allowing dump information to be used 33174921Srwatson * without 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 * 49174921Srwatson * Data is written out in the 'tar' file format, as it provides the facility 50174921Srwatson * to write data incrementally as a stream without reference to previous 51174921Srwatson * files. 52174921Srwatson * 53174921Srwatson * TODO 54174921Srwatson * ---- 55174921Srwatson * 56174921Srwatson * - Allow subsytems to register to submit files for inclusion in the text 57174921Srwatson * dump in a generic way. 58174921Srwatson */ 59174921Srwatson 60174921Srwatson#include <sys/cdefs.h> 61174921Srwatson__FBSDID("$FreeBSD: head/sys/ddb/db_textdump.c 174921 2007-12-26 11:32:33Z rwatson $"); 62174921Srwatson 63174921Srwatson#include "opt_config.h" 64174921Srwatson 65174921Srwatson#include <sys/param.h> 66174921Srwatson#include <sys/conf.h> 67174921Srwatson#include <sys/kernel.h> 68174921Srwatson#include <sys/kerneldump.h> 69174921Srwatson#include <sys/msgbuf.h> 70174921Srwatson#include <sys/sysctl.h> 71174921Srwatson#include <sys/systm.h> 72174921Srwatson 73174921Srwatson#include <ddb/ddb.h> 74174921Srwatson#include <ddb/db_lex.h> 75174921Srwatson 76174921Srwatsonstatic SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0, 77174921Srwatson "DDB textdump options"); 78174921Srwatson 79174921Srwatson/* 80174921Srwatson * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is 81174921Srwatson * to protect us from metadata and metadata from us. 82174921Srwatson */ 83174921Srwatson#define SIZEOF_METADATA (64*1024) 84174921Srwatson 85174921Srwatson/* 86174921Srwatson * Data is written out as a series of files in the ustar tar format. ustar 87174921Srwatson * is a simple streamed format consiting of a series of files prefixed with 88174921Srwatson * headers, and all padded to 512-byte block boundaries, which maps 89174921Srwatson * conveniently to our requirements. 90174921Srwatson */ 91174921Srwatsonstruct ustar_header { 92174921Srwatson char uh_filename[100]; 93174921Srwatson char uh_mode[8]; 94174921Srwatson char uh_tar_owner[8]; 95174921Srwatson char uh_tar_group[8]; 96174921Srwatson char uh_size[12]; 97174921Srwatson char uh_mtime[12]; 98174921Srwatson char uh_sum[8]; 99174921Srwatson char uh_type; 100174921Srwatson char uh_linkfile[100]; 101174921Srwatson char uh_ustar[6]; 102174921Srwatson char uh_version[2]; 103174921Srwatson char uh_owner[32]; 104174921Srwatson char uh_group[32]; 105174921Srwatson char uh_major[8]; 106174921Srwatson char uh_minor[8]; 107174921Srwatson char uh_filenameprefix[155]; 108174921Srwatson char uh_zeropad[12]; 109174921Srwatson} __packed; 110174921Srwatson 111174921Srwatson/* 112174921Srwatson * Various size assertions -- pretty much everything must be one block in 113174921Srwatson * size. 114174921Srwatson */ 115174921SrwatsonCTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE); 116174921SrwatsonCTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE); 117174921Srwatson 118174921Srwatson/* 119174921Srwatson * Is a textdump scheduled? If so, the shutdown code will invoke our dumpsys 120174921Srwatson * routine instead of the machine-dependent kernel dump routine. 121174921Srwatson */ 122174921Srwatsonint textdump_pending; 123174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW, 124174921Srwatson &textdump_pending, 0, 125174921Srwatson "Perform textdump instead of regular kernel dump."); 126174921Srwatson 127174921Srwatson/* 128174921Srwatson * Various constants for tar headers and contents. 129174921Srwatson */ 130174921Srwatson#define TAR_USER "root" 131174921Srwatson#define TAR_GROUP "wheel" 132174921Srwatson#define TAR_UID "0" 133174921Srwatson#define TAR_GID "0" 134174921Srwatson#define TAR_MODE "0600" 135174921Srwatson#define TAR_USTAR "ustar" 136174921Srwatson 137174921Srwatson#define TAR_CONFIG_FILENAME "config.txt" /* Kernel configuration. */ 138174921Srwatson#define TAR_MSGBUF_FILENAME "msgbuf.txt" /* Kernel messsage buffer. */ 139174921Srwatson#define TAR_PANIC_FILENAME "panic.txt" /* Panic message. */ 140174921Srwatson#define TAR_VERSION_FILENAME "version.txt" /* Kernel version. */ 141174921Srwatson 142174921Srwatson/* 143174921Srwatson * Configure which files will be dumped. 144174921Srwatson */ 145174921Srwatson#ifdef INCLUDE_CONFIG_FILE 146174921Srwatsonstatic int textdump_do_config = 1; 147174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW, 148174921Srwatson &textdump_do_config, 0, "Dump kernel configuration in textdump"); 149174921Srwatson#endif 150174921Srwatson 151174921Srwatsonstatic int textdump_do_ddb = 1; 152174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW, 153174921Srwatson &textdump_do_ddb, 0, "Dump DDB captured output in textdump"); 154174921Srwatson 155174921Srwatsonstatic int textdump_do_msgbuf = 1; 156174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW, 157174921Srwatson &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump"); 158174921Srwatson 159174921Srwatsonstatic int textdump_do_panic = 1; 160174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW, 161174921Srwatson &textdump_do_panic, 0, "Dump kernel panic message in textdump"); 162174921Srwatson 163174921Srwatsonstatic int textdump_do_version = 1; 164174921SrwatsonSYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW, 165174921Srwatson &textdump_do_version, 0, "Dump kernel version string in textdump"); 166174921Srwatson 167174921Srwatson/* 168174921Srwatson * State related to incremental writing of blocks to disk. 169174921Srwatson */ 170174921Srwatsonstatic off_t textdump_offset; /* Offset of next sequential write. */ 171174921Srwatsonstatic int textdump_error; /* Carried write error, if any. */ 172174921Srwatson 173174921Srwatson/* 174174921Srwatson * Statically allocate space to prepare block-sized headers and data. 175174921Srwatson */ 176174921Srwatsonchar textdump_block_buffer[TEXTDUMP_BLOCKSIZE]; 177174921Srwatsonstatic struct kerneldumpheader kdh; 178174921Srwatson 179174921Srwatson/* 180174921Srwatson * Text dumps are prefixed with a normal kernel dump header but with a 181174921Srwatson * different magic number to allow them to be uniquely identified. 182174921Srwatson */ 183174921Srwatsonstatic void 184174921Srwatsonmkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, 185174921Srwatson uint64_t dumplen, uint32_t blksz) 186174921Srwatson{ 187174921Srwatson 188174921Srwatson bzero(kdh, sizeof(*kdh)); 189174921Srwatson strncpy(kdh->magic, TEXTDUMPMAGIC, sizeof(kdh->magic)); 190174921Srwatson strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 191174921Srwatson kdh->version = htod32(KERNELDUMPVERSION); 192174921Srwatson kdh->architectureversion = htod32(archver); 193174921Srwatson kdh->dumplength = htod64(dumplen); 194174921Srwatson kdh->dumptime = htod64(time_second); 195174921Srwatson kdh->blocksize = htod32(blksz); 196174921Srwatson strncpy(kdh->hostname, hostname, sizeof(kdh->hostname)); 197174921Srwatson strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 198174921Srwatson if (panicstr != NULL) 199174921Srwatson strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 200174921Srwatson kdh->parity = kerneldump_parity(kdh); 201174921Srwatson} 202174921Srwatson 203174921Srwatson/* 204174921Srwatson * Calculate and fill in the checksum for a tar header. 205174921Srwatson */ 206174921Srwatsonstatic void 207174921Srwatsonustar_checksum(struct ustar_header *uhp) 208174921Srwatson{ 209174921Srwatson u_int sum; 210174921Srwatson int i; 211174921Srwatson 212174921Srwatson for (i = 0; i < sizeof(uhp->uh_sum); i++) 213174921Srwatson uhp->uh_sum[i] = ' '; 214174921Srwatson sum = 0; 215174921Srwatson for (i = 0; i < sizeof(*uhp); i++) 216174921Srwatson sum += ((u_char *)uhp)[i]; 217174921Srwatson snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum); 218174921Srwatson} 219174921Srwatson 220174921Srwatson/* 221174921Srwatson * Each file in the tarball has a block-sized header with its name and other, 222174921Srwatson * largely hard-coded, properties. 223174921Srwatson */ 224174921Srwatsonvoid 225174921Srwatsontextdump_mkustar(char *block_buffer, const char *filename, u_int size) 226174921Srwatson{ 227174921Srwatson struct ustar_header *uhp; 228174921Srwatson 229174921Srwatson uhp = (struct ustar_header *)block_buffer; 230174921Srwatson bzero(uhp, sizeof(*uhp)); 231174921Srwatson strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename)); 232174921Srwatson strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode)); 233174921Srwatson snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size); 234174921Srwatson strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner)); 235174921Srwatson strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group)); 236174921Srwatson strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner)); 237174921Srwatson strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group)); 238174921Srwatson snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo", 239174921Srwatson (unsigned long)time_second); 240174921Srwatson uhp->uh_type = 0; 241174921Srwatson strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar)); 242174921Srwatson ustar_checksum(uhp); 243174921Srwatson} 244174921Srwatson 245174921Srwatson/* 246174921Srwatson * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to 247174921Srwatson * the space between di->mediaoffset and di->mediaoffset + di->mediasize. It 248174921Srwatson * accepts an offset relative to di->mediaoffset. If we're carrying any 249174921Srwatson * error from previous I/O, return that error and don't continue to try to 250174921Srwatson * write. Most writers ignore the error and forge ahead on the basis that 251174921Srwatson * there's not much you can do. 252174921Srwatson */ 253174921Srwatsonstatic int 254174921Srwatsontextdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer) 255174921Srwatson{ 256174921Srwatson 257174921Srwatson if (textdump_error) 258174921Srwatson return (textdump_error); 259174921Srwatson if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize) 260174921Srwatson return (EIO); 261174921Srwatson if (offset < SIZEOF_METADATA) 262174921Srwatson return (ENOSPC); 263174921Srwatson textdump_error = di->dumper(di->priv, buffer, 0, offset + 264174921Srwatson di->mediaoffset, TEXTDUMP_BLOCKSIZE); 265174921Srwatson return (textdump_error); 266174921Srwatson} 267174921Srwatson 268174921Srwatson/* 269174921Srwatson * Interfaces to save and restore the dump offset, so that printers can go 270174921Srwatson * back to rewrite a header if required, while avoiding their knowing about 271174921Srwatson * the global layout of the blocks. 272174921Srwatson */ 273174921Srwatsonvoid 274174921Srwatsontextdump_saveoff(off_t *offsetp) 275174921Srwatson{ 276174921Srwatson 277174921Srwatson *offsetp = textdump_offset; 278174921Srwatson} 279174921Srwatson 280174921Srwatsonvoid 281174921Srwatsontextdump_restoreoff(off_t offset) 282174921Srwatson{ 283174921Srwatson 284174921Srwatson textdump_offset = offset; 285174921Srwatson} 286174921Srwatson 287174921Srwatson/* 288174921Srwatson * Interface to write the "next block" relative to the current offset; since 289174921Srwatson * we write backwards from the end of the partition, we subtract, but there's 290174921Srwatson * no reason for the caller to know this. 291174921Srwatson */ 292174921Srwatsonint 293174921Srwatsontextdump_writenextblock(struct dumperinfo *di, char *buffer) 294174921Srwatson{ 295174921Srwatson int error; 296174921Srwatson 297174921Srwatson error = textdump_writeblock(di, textdump_offset, buffer); 298174921Srwatson textdump_offset -= TEXTDUMP_BLOCKSIZE; 299174921Srwatson return (error); 300174921Srwatson} 301174921Srwatson 302174921Srwatson#ifdef INCLUDE_CONFIG_FILE 303174921Srwatsonextern char kernconfstring[]; 304174921Srwatson 305174921Srwatson/* 306174921Srwatson * Dump kernel configuration. 307174921Srwatson */ 308174921Srwatsonstatic void 309174921Srwatsontextdump_dump_config(struct dumperinfo *di) 310174921Srwatson{ 311174921Srwatson u_int count, fullblocks, len; 312174921Srwatson 313174921Srwatson len = strlen(kernconfstring); 314174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len); 315174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 316174921Srwatson 317174921Srwatson /* 318174921Srwatson * Write out all full blocks directly from the string, and handle any 319174921Srwatson * left-over bits by copying it to out to the local buffer and 320174921Srwatson * zero-padding it. 321174921Srwatson */ 322174921Srwatson fullblocks = len / TEXTDUMP_BLOCKSIZE; 323174921Srwatson for (count = 0; count < fullblocks; count++) 324174921Srwatson (void)textdump_writenextblock(di, kernconfstring + count * 325174921Srwatson TEXTDUMP_BLOCKSIZE); 326174921Srwatson if (len % TEXTDUMP_BLOCKSIZE != 0) { 327174921Srwatson bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE); 328174921Srwatson bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE, 329174921Srwatson textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE); 330174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 331174921Srwatson } 332174921Srwatson} 333174921Srwatson#endif /* INCLUDE_CONFIG_FILE */ 334174921Srwatson 335174921Srwatson/* 336174921Srwatson * Dump kernel message buffer. 337174921Srwatson */ 338174921Srwatsonstatic void 339174921Srwatsontextdump_dump_msgbuf(struct dumperinfo *di) 340174921Srwatson{ 341174921Srwatson off_t end_offset, tarhdr_offset; 342174921Srwatson u_int i, len, offset, seq, total_len; 343174921Srwatson char buf[16]; 344174921Srwatson 345174921Srwatson /* 346174921Srwatson * Write out a dummy tar header to advance the offset; we'll rewrite 347174921Srwatson * it later once we know the true size. 348174921Srwatson */ 349174921Srwatson textdump_saveoff(&tarhdr_offset); 350174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0); 351174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 352174921Srwatson 353174921Srwatson /* 354174921Srwatson * Copy out the data in small chunks, but don't copy nuls that may be 355174921Srwatson * present if the message buffer has not yet completely filled at 356174921Srwatson * least once. 357174921Srwatson */ 358174921Srwatson total_len = 0; 359174921Srwatson offset = 0; 360174921Srwatson msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 361174921Srwatson while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) { 362174921Srwatson for (i = 0; i < len; i++) { 363174921Srwatson if (buf[i] == '\0') 364174921Srwatson continue; 365174921Srwatson textdump_block_buffer[offset] = buf[i]; 366174921Srwatson offset++; 367174921Srwatson if (offset != sizeof(textdump_block_buffer)) 368174921Srwatson continue; 369174921Srwatson (void)textdump_writenextblock(di, 370174921Srwatson textdump_block_buffer); 371174921Srwatson total_len += offset; 372174921Srwatson offset = 0; 373174921Srwatson } 374174921Srwatson } 375174921Srwatson total_len += offset; /* Without the zero-padding. */ 376174921Srwatson if (offset != 0) { 377174921Srwatson bzero(textdump_block_buffer + offset, 378174921Srwatson sizeof(textdump_block_buffer) - offset); 379174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 380174921Srwatson } 381174921Srwatson 382174921Srwatson /* 383174921Srwatson * Rewrite tar header to reflect how much was actually written. 384174921Srwatson */ 385174921Srwatson textdump_saveoff(&end_offset); 386174921Srwatson textdump_restoreoff(tarhdr_offset); 387174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 388174921Srwatson total_len); 389174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 390174921Srwatson textdump_restoreoff(end_offset); 391174921Srwatson} 392174921Srwatson 393174921Srwatsonstatic void 394174921Srwatsontextdump_dump_panic(struct dumperinfo *di) 395174921Srwatson{ 396174921Srwatson u_int len; 397174921Srwatson 398174921Srwatson /* 399174921Srwatson * Write out tar header -- we store up to one block of panic message. 400174921Srwatson */ 401174921Srwatson len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE); 402174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len); 403174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 404174921Srwatson 405174921Srwatson /* 406174921Srwatson * Zero-pad the panic string and write out block. 407174921Srwatson */ 408174921Srwatson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 409174921Srwatson bcopy(panicstr, textdump_block_buffer, len); 410174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 411174921Srwatson} 412174921Srwatson 413174921Srwatsonstatic void 414174921Srwatsontextdump_dump_version(struct dumperinfo *di) 415174921Srwatson{ 416174921Srwatson u_int len; 417174921Srwatson 418174921Srwatson /* 419174921Srwatson * Write out tar header -- at most one block of version information. 420174921Srwatson */ 421174921Srwatson len = min(strlen(version), TEXTDUMP_BLOCKSIZE); 422174921Srwatson textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len); 423174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 424174921Srwatson 425174921Srwatson /* 426174921Srwatson * Zero pad the version string and write out block. 427174921Srwatson */ 428174921Srwatson bzero(textdump_block_buffer, sizeof(textdump_block_buffer)); 429174921Srwatson bcopy(version, textdump_block_buffer, len); 430174921Srwatson (void)textdump_writenextblock(di, textdump_block_buffer); 431174921Srwatson} 432174921Srwatson 433174921Srwatson/* 434174921Srwatson * Commit text dump to disk. 435174921Srwatson */ 436174921Srwatsonvoid 437174921Srwatsontextdump_dumpsys(struct dumperinfo *di) 438174921Srwatson{ 439174921Srwatson off_t dumplen, trailer_offset; 440174921Srwatson 441174921Srwatson if (di->blocksize != TEXTDUMP_BLOCKSIZE) { 442174921Srwatson printf("Dump partition block size (%ju) not textdump " 443174921Srwatson "block size (%ju)", (uintmax_t)di->blocksize, 444174921Srwatson (uintmax_t)TEXTDUMP_BLOCKSIZE); 445174921Srwatson return; 446174921Srwatson } 447174921Srwatson 448174921Srwatson /* 449174921Srwatson * We don't know a priori how large the dump will be, but we do know 450174921Srwatson * that we need to reserve space for metadata and that we need two 451174921Srwatson * dump headers. Also leave room for one ustar header and one block 452174921Srwatson * of data. 453174921Srwatson */ 454174921Srwatson if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) { 455174921Srwatson printf("Insufficient space on dump partition.\n"); 456174921Srwatson return; 457174921Srwatson } 458174921Srwatson textdump_error = 0; 459174921Srwatson 460174921Srwatson /* 461174921Srwatson * Position the start of the dump so that we'll write the kernel dump 462174921Srwatson * trailer immediately before the end of the partition, and then work 463174921Srwatson * our way back. We will rewrite this header later to reflect the 464174921Srwatson * true size if things go well. 465174921Srwatson */ 466174921Srwatson textdump_offset = di->mediasize - sizeof(kdh); 467174921Srwatson textdump_saveoff(&trailer_offset); 468174921Srwatson mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); 469174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 470174921Srwatson 471174921Srwatson /* 472174921Srwatson * Write a series of files in ustar format. 473174921Srwatson */ 474174921Srwatson if (textdump_do_ddb) 475174921Srwatson db_capture_dump(di); 476174921Srwatson#ifdef INCLUDE_CONFIG_FILE 477174921Srwatson if (textdump_do_config) 478174921Srwatson textdump_dump_config(di); 479174921Srwatson#endif 480174921Srwatson if (textdump_do_msgbuf) 481174921Srwatson textdump_dump_msgbuf(di); 482174921Srwatson if (textdump_do_panic && panicstr != NULL) 483174921Srwatson textdump_dump_panic(di); 484174921Srwatson if (textdump_do_version) 485174921Srwatson textdump_dump_version(di); 486174921Srwatson 487174921Srwatson /* 488174921Srwatson * Now that we know the true size, we can write out the header, then 489174921Srwatson * seek back to the end and rewrite the trailer with the correct 490174921Srwatson * size. 491174921Srwatson */ 492174921Srwatson dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE); 493174921Srwatson mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, dumplen, 494174921Srwatson TEXTDUMP_BLOCKSIZE); 495174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 496174921Srwatson textdump_restoreoff(trailer_offset); 497174921Srwatson (void)textdump_writenextblock(di, (char *)&kdh); 498174921Srwatson 499174921Srwatson /* 500174921Srwatson * Terminate the dump, report any errors, and clear the pending flag. 501174921Srwatson */ 502174921Srwatson if (textdump_error == 0) 503174921Srwatson (void)di->dumper(di->priv, NULL, 0, 0, 0); 504174921Srwatson if (textdump_error == ENOSPC) 505174921Srwatson printf("Insufficient space on dump partition\n"); 506174921Srwatson else if (textdump_error != 0) 507174921Srwatson printf("Error %d writing dump\n", textdump_error); 508174921Srwatson else 509174921Srwatson printf("Textdump complete.\n"); 510174921Srwatson textdump_pending = 0; 511174921Srwatson} 512174921Srwatson 513174921Srwatson/*- 514174921Srwatson * DDB(4) command to manage textdumps: 515174921Srwatson * 516174921Srwatson * textdump set - request a textdump 517174921Srwatson * textdump status - print DDB output textdump status 518174921Srwatson * textdump unset - clear textdump request 519174921Srwatson */ 520174921Srwatsonstatic void 521174921Srwatsondb_textdump_usage(void) 522174921Srwatson{ 523174921Srwatson 524174921Srwatson db_printf("textdump [unset|set|status]\n"); 525174921Srwatson} 526174921Srwatson 527174921Srwatsonvoid 528174921Srwatsondb_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 529174921Srwatson char *modif) 530174921Srwatson{ 531174921Srwatson int t; 532174921Srwatson 533174921Srwatson t = db_read_token(); 534174921Srwatson if (t != tIDENT) { 535174921Srwatson db_textdump_usage(); 536174921Srwatson return; 537174921Srwatson } 538174921Srwatson if (db_read_token() != tEOL) { 539174921Srwatson db_textdump_usage(); 540174921Srwatson return; 541174921Srwatson } 542174921Srwatson if (strcmp(db_tok_string, "set") == 0) { 543174921Srwatson textdump_pending = 1; 544174921Srwatson db_printf("textdump set\n"); 545174921Srwatson } else if (strcmp(db_tok_string, "status") == 0) { 546174921Srwatson if (textdump_pending) 547174921Srwatson db_printf("textdump is set\n"); 548174921Srwatson else 549174921Srwatson db_printf("textdump is not set\n"); 550174921Srwatson } else if (strcmp(db_tok_string, "unset") == 0) { 551174921Srwatson textdump_pending = 0; 552174921Srwatson db_printf("textdump unset\n"); 553174921Srwatson } else 554174921Srwatson db_textdump_usage(); 555174921Srwatson} 556