2 * Copyright (c) 2007 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Kernel text-dump support: allow a series of text files to be written to
29 * the dump partition for later recovery, including captured DDB output, the
30 * kernel configuration, message buffer, panic message, etc. This allows for
31 * a more compact representation of critical debugging information than
32 * traditional binary dumps, as well as allowing dump information to be used
33 * without access to kernel symbols, source code, etc.
38 * Crash dumps are aligned to the end of the dump or swap partition in order
39 * to minimize the chances of swap duing fsck eating into the dump. However,
40 * unlike a memory dump, we don't know the size of the textdump a priori, so
41 * can't just write it out sequentially in order from a known starting point
42 * calculated with respect to the end of the partition. In order to address
43 * this, we actually write out the textdump in reverse block order, allowing
44 * us to directly align it to the end of the partition and then write out the
45 * dump header and trailer before and after it once done. savecore(8) must
46 * know to reverse the order of the blocks in order to produce a readable
49 * Data is written out in the 'tar' file format, as it provides the facility
50 * to write data incrementally as a stream without reference to previous
56 * - Allow subsytems to register to submit files for inclusion in the text
57 * dump in a generic way.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include "opt_config.h"
65 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/kerneldump.h>
69 #include <sys/msgbuf.h>
70 #include <sys/sysctl.h>
71 #include <sys/systm.h>
74 #include <ddb/db_lex.h>
76 static SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0,
77 "DDB textdump options");
80 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This is
81 * to protect us from metadata and metadata from us.
83 #define SIZEOF_METADATA (64*1024)
86 * Data is written out as a series of files in the ustar tar format. ustar
87 * is a simple streamed format consiting of a series of files prefixed with
88 * headers, and all padded to 512-byte block boundaries, which maps
89 * conveniently to our requirements.
92 char uh_filename[100];
100 char uh_linkfile[100];
107 char uh_filenameprefix[155];
112 * Various size assertions -- pretty much everything must be one block in
115 CTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE);
116 CTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE);
119 * Is a textdump scheduled? If so, the shutdown code will invoke our dumpsys
120 * routine instead of the machine-dependent kernel dump routine.
122 int textdump_pending;
123 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW,
124 &textdump_pending, 0,
125 "Perform textdump instead of regular kernel dump.");
128 * Various constants for tar headers and contents.
130 #define TAR_USER "root"
131 #define TAR_GROUP "wheel"
134 #define TAR_MODE "0600"
135 #define TAR_USTAR "ustar"
137 #define TAR_CONFIG_FILENAME "config.txt" /* Kernel configuration. */
138 #define TAR_MSGBUF_FILENAME "msgbuf.txt" /* Kernel messsage buffer. */
139 #define TAR_PANIC_FILENAME "panic.txt" /* Panic message. */
140 #define TAR_VERSION_FILENAME "version.txt" /* Kernel version. */
143 * Configure which files will be dumped.
145 #ifdef INCLUDE_CONFIG_FILE
146 static int textdump_do_config = 1;
147 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW,
148 &textdump_do_config, 0, "Dump kernel configuration in textdump");
151 static int textdump_do_ddb = 1;
152 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW,
153 &textdump_do_ddb, 0, "Dump DDB captured output in textdump");
155 static int textdump_do_msgbuf = 1;
156 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW,
157 &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump");
159 static int textdump_do_panic = 1;
160 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW,
161 &textdump_do_panic, 0, "Dump kernel panic message in textdump");
163 static int textdump_do_version = 1;
164 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW,
165 &textdump_do_version, 0, "Dump kernel version string in textdump");
168 * State related to incremental writing of blocks to disk.
170 static off_t textdump_offset; /* Offset of next sequential write. */
171 static int textdump_error; /* Carried write error, if any. */
174 * Statically allocate space to prepare block-sized headers and data.
176 char textdump_block_buffer[TEXTDUMP_BLOCKSIZE];
177 static struct kerneldumpheader kdh;
180 * Text dumps are prefixed with a normal kernel dump header but with a
181 * different magic number to allow them to be uniquely identified.
184 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver,
185 uint64_t dumplen, uint32_t blksz)
188 bzero(kdh, sizeof(*kdh));
189 strncpy(kdh->magic, TEXTDUMPMAGIC, sizeof(kdh->magic));
190 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
191 kdh->version = htod32(KERNELDUMPVERSION);
192 kdh->architectureversion = htod32(archver);
193 kdh->dumplength = htod64(dumplen);
194 kdh->dumptime = htod64(time_second);
195 kdh->blocksize = htod32(blksz);
196 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
197 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
198 if (panicstr != NULL)
199 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
200 kdh->parity = kerneldump_parity(kdh);
204 * Calculate and fill in the checksum for a tar header.
207 ustar_checksum(struct ustar_header *uhp)
212 for (i = 0; i < sizeof(uhp->uh_sum); i++)
213 uhp->uh_sum[i] = ' ';
215 for (i = 0; i < sizeof(*uhp); i++)
216 sum += ((u_char *)uhp)[i];
217 snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum);
221 * Each file in the tarball has a block-sized header with its name and other,
222 * largely hard-coded, properties.
225 textdump_mkustar(char *block_buffer, const char *filename, u_int size)
227 struct ustar_header *uhp;
229 uhp = (struct ustar_header *)block_buffer;
230 bzero(uhp, sizeof(*uhp));
231 strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename));
232 strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode));
233 snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size);
234 strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner));
235 strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group));
236 strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner));
237 strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group));
238 snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo",
239 (unsigned long)time_second);
241 strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar));
246 * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to
247 * the space between di->mediaoffset and di->mediaoffset + di->mediasize. It
248 * accepts an offset relative to di->mediaoffset. If we're carrying any
249 * error from previous I/O, return that error and don't continue to try to
250 * write. Most writers ignore the error and forge ahead on the basis that
251 * there's not much you can do.
254 textdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer)
258 return (textdump_error);
259 if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize)
261 if (offset < SIZEOF_METADATA)
263 textdump_error = di->dumper(di->priv, buffer, 0, offset +
264 di->mediaoffset, TEXTDUMP_BLOCKSIZE);
265 return (textdump_error);
269 * Interfaces to save and restore the dump offset, so that printers can go
270 * back to rewrite a header if required, while avoiding their knowing about
271 * the global layout of the blocks.
274 textdump_saveoff(off_t *offsetp)
277 *offsetp = textdump_offset;
281 textdump_restoreoff(off_t offset)
284 textdump_offset = offset;
288 * Interface to write the "next block" relative to the current offset; since
289 * we write backwards from the end of the partition, we subtract, but there's
290 * no reason for the caller to know this.
293 textdump_writenextblock(struct dumperinfo *di, char *buffer)
297 error = textdump_writeblock(di, textdump_offset, buffer);
298 textdump_offset -= TEXTDUMP_BLOCKSIZE;
302 #ifdef INCLUDE_CONFIG_FILE
303 extern char kernconfstring[];
306 * Dump kernel configuration.
309 textdump_dump_config(struct dumperinfo *di)
311 u_int count, fullblocks, len;
313 len = strlen(kernconfstring);
314 textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len);
315 (void)textdump_writenextblock(di, textdump_block_buffer);
318 * Write out all full blocks directly from the string, and handle any
319 * left-over bits by copying it to out to the local buffer and
322 fullblocks = len / TEXTDUMP_BLOCKSIZE;
323 for (count = 0; count < fullblocks; count++)
324 (void)textdump_writenextblock(di, kernconfstring + count *
326 if (len % TEXTDUMP_BLOCKSIZE != 0) {
327 bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE);
328 bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE,
329 textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE);
330 (void)textdump_writenextblock(di, textdump_block_buffer);
333 #endif /* INCLUDE_CONFIG_FILE */
336 * Dump kernel message buffer.
339 textdump_dump_msgbuf(struct dumperinfo *di)
341 off_t end_offset, tarhdr_offset;
342 u_int i, len, offset, seq, total_len;
346 * Write out a dummy tar header to advance the offset; we'll rewrite
347 * it later once we know the true size.
349 textdump_saveoff(&tarhdr_offset);
350 textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0);
351 (void)textdump_writenextblock(di, textdump_block_buffer);
354 * Copy out the data in small chunks, but don't copy nuls that may be
355 * present if the message buffer has not yet completely filled at
360 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
361 while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
362 for (i = 0; i < len; i++) {
365 textdump_block_buffer[offset] = buf[i];
367 if (offset != sizeof(textdump_block_buffer))
369 (void)textdump_writenextblock(di,
370 textdump_block_buffer);
375 total_len += offset; /* Without the zero-padding. */
377 bzero(textdump_block_buffer + offset,
378 sizeof(textdump_block_buffer) - offset);
379 (void)textdump_writenextblock(di, textdump_block_buffer);
383 * Rewrite tar header to reflect how much was actually written.
385 textdump_saveoff(&end_offset);
386 textdump_restoreoff(tarhdr_offset);
387 textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME,
389 (void)textdump_writenextblock(di, textdump_block_buffer);
390 textdump_restoreoff(end_offset);
394 textdump_dump_panic(struct dumperinfo *di)
399 * Write out tar header -- we store up to one block of panic message.
401 len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE);
402 textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len);
403 (void)textdump_writenextblock(di, textdump_block_buffer);
406 * Zero-pad the panic string and write out block.
408 bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
409 bcopy(panicstr, textdump_block_buffer, len);
410 (void)textdump_writenextblock(di, textdump_block_buffer);
414 textdump_dump_version(struct dumperinfo *di)
419 * Write out tar header -- at most one block of version information.
421 len = min(strlen(version), TEXTDUMP_BLOCKSIZE);
422 textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len);
423 (void)textdump_writenextblock(di, textdump_block_buffer);
426 * Zero pad the version string and write out block.
428 bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
429 bcopy(version, textdump_block_buffer, len);
430 (void)textdump_writenextblock(di, textdump_block_buffer);
434 * Commit text dump to disk.
437 textdump_dumpsys(struct dumperinfo *di)
439 off_t dumplen, trailer_offset;
441 if (di->blocksize != TEXTDUMP_BLOCKSIZE) {
442 printf("Dump partition block size (%ju) not textdump "
443 "block size (%ju)", (uintmax_t)di->blocksize,
444 (uintmax_t)TEXTDUMP_BLOCKSIZE);
449 * We don't know a priori how large the dump will be, but we do know
450 * that we need to reserve space for metadata and that we need two
451 * dump headers. Also leave room for one ustar header and one block
454 if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) {
455 printf("Insufficient space on dump partition.\n");
461 * Position the start of the dump so that we'll write the kernel dump
462 * trailer immediately before the end of the partition, and then work
463 * our way back. We will rewrite this header later to reflect the
464 * true size if things go well.
466 textdump_offset = di->mediasize - sizeof(kdh);
467 textdump_saveoff(&trailer_offset);
468 mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE);
469 (void)textdump_writenextblock(di, (char *)&kdh);
472 * Write a series of files in ustar format.
476 #ifdef INCLUDE_CONFIG_FILE
477 if (textdump_do_config)
478 textdump_dump_config(di);
480 if (textdump_do_msgbuf)
481 textdump_dump_msgbuf(di);
482 if (textdump_do_panic && panicstr != NULL)
483 textdump_dump_panic(di);
484 if (textdump_do_version)
485 textdump_dump_version(di);
488 * Now that we know the true size, we can write out the header, then
489 * seek back to the end and rewrite the trailer with the correct
492 dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE);
493 mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, dumplen,
495 (void)textdump_writenextblock(di, (char *)&kdh);
496 textdump_restoreoff(trailer_offset);
497 (void)textdump_writenextblock(di, (char *)&kdh);
500 * Terminate the dump, report any errors, and clear the pending flag.
502 if (textdump_error == 0)
503 (void)di->dumper(di->priv, NULL, 0, 0, 0);
504 if (textdump_error == ENOSPC)
505 printf("Insufficient space on dump partition\n");
506 else if (textdump_error != 0)
507 printf("Error %d writing dump\n", textdump_error);
509 printf("Textdump complete.\n");
510 textdump_pending = 0;
514 * DDB(4) command to manage textdumps:
516 * textdump set - request a textdump
517 * textdump status - print DDB output textdump status
518 * textdump unset - clear textdump request
521 db_textdump_usage(void)
524 db_printf("textdump [unset|set|status]\n");
528 db_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
538 if (db_read_token() != tEOL) {
542 if (strcmp(db_tok_string, "set") == 0) {
543 textdump_pending = 1;
544 db_printf("textdump set\n");
545 } else if (strcmp(db_tok_string, "status") == 0) {
546 if (textdump_pending)
547 db_printf("textdump is set\n");
549 db_printf("textdump is not set\n");
550 } else if (strcmp(db_tok_string, "unset") == 0) {
551 textdump_pending = 0;
552 db_printf("textdump unset\n");