]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/ddb/db_textdump.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / ddb / db_textdump.c
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 /*-
28  * Kernel text-dump support: write a series of text files to the dump
29  * partition for later recovery, including captured DDB output, kernel
30  * configuration, message buffer, and panic message.  This allows for a more
31  * compact representation of critical debugging information than traditional
32  * binary dumps, as well as allowing dump information to be used without
33  * access to kernel symbols, source code, etc.
34  *
35  * Storage Layout
36  * --------------
37  *
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
47  * file.
48  *
49  * Data is written out in the ustar file format so that we can write data
50  * incrementally as a stream without reference to previous files.
51  *
52  * TODO
53  * ----
54  *
55  * - Allow subsytems to register to submit files for inclusion in the text
56  *   dump in a generic way.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 #include "opt_config.h"
63
64 #include <sys/param.h>
65 #include <sys/conf.h>
66 #include <sys/kernel.h>
67 #include <sys/kerneldump.h>
68 #include <sys/msgbuf.h>
69 #include <sys/sysctl.h>
70 #include <sys/systm.h>
71
72 #include <ddb/ddb.h>
73 #include <ddb/db_lex.h>
74
75 static SYSCTL_NODE(_debug_ddb, OID_AUTO, textdump, CTLFLAG_RW, 0,
76     "DDB textdump options");
77
78 /*
79  * Don't touch the first SIZEOF_METADATA bytes on the dump device.  This is
80  * to protect us from metadata and metadata from us.
81  */
82 #define SIZEOF_METADATA         (64*1024)
83
84 /*
85  * Data is written out as a series of files in the ustar tar format.  ustar
86  * is a simple streamed format consiting of a series of files prefixed with
87  * headers, and all padded to 512-byte block boundaries, which maps
88  * conveniently to our requirements.
89  */
90 struct ustar_header {
91         char    uh_filename[100];
92         char    uh_mode[8];
93         char    uh_tar_owner[8];
94         char    uh_tar_group[8];
95         char    uh_size[12];
96         char    uh_mtime[12];
97         char    uh_sum[8];
98         char    uh_type;
99         char    uh_linkfile[100];
100         char    uh_ustar[6];
101         char    uh_version[2];
102         char    uh_owner[32];
103         char    uh_group[32];
104         char    uh_major[8];
105         char    uh_minor[8];
106         char    uh_filenameprefix[155];
107         char    uh_zeropad[12];
108 } __packed;
109
110 /*
111  * Various size assertions -- pretty much everything must be one block in
112  * size.
113  */
114 CTASSERT(sizeof(struct kerneldumpheader) == TEXTDUMP_BLOCKSIZE);
115 CTASSERT(sizeof(struct ustar_header) == TEXTDUMP_BLOCKSIZE);
116
117 /*
118  * Is a textdump scheduled?  If so, the shutdown code will invoke our dumpsys
119  * routine instead of the machine-dependent kernel dump routine.
120  */
121 int     textdump_pending;
122 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, pending, CTLFLAG_RW,
123     &textdump_pending, 0,
124     "Perform textdump instead of regular kernel dump.");
125
126 /*
127  * Various constants for tar headers and contents.
128  */
129 #define TAR_USER        "root"
130 #define TAR_GROUP       "wheel"
131 #define TAR_UID         "0"
132 #define TAR_GID         "0"
133 #define TAR_MODE        "0600"
134 #define TAR_USTAR       "ustar"
135
136 #define TAR_CONFIG_FILENAME     "config.txt"    /* Kernel configuration. */
137 #define TAR_MSGBUF_FILENAME     "msgbuf.txt"    /* Kernel messsage buffer. */
138 #define TAR_PANIC_FILENAME      "panic.txt"     /* Panic message. */
139 #define TAR_VERSION_FILENAME    "version.txt"   /* Kernel version. */
140
141 /*
142  * Configure which files will be dumped.
143  */
144 #ifdef INCLUDE_CONFIG_FILE
145 static int textdump_do_config = 1;
146 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_config, CTLFLAG_RW,
147     &textdump_do_config, 0, "Dump kernel configuration in textdump");
148 #endif
149
150 static int textdump_do_ddb = 1;
151 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_ddb, CTLFLAG_RW,
152     &textdump_do_ddb, 0, "Dump DDB captured output in textdump");
153
154 static int textdump_do_msgbuf = 1;
155 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_msgbuf, CTLFLAG_RW,
156     &textdump_do_msgbuf, 0, "Dump kernel message buffer in textdump");
157
158 static int textdump_do_panic = 1;
159 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_panic, CTLFLAG_RW,
160     &textdump_do_panic, 0, "Dump kernel panic message in textdump");
161
162 static int textdump_do_version = 1;
163 SYSCTL_INT(_debug_ddb_textdump, OID_AUTO, do_version, CTLFLAG_RW,
164     &textdump_do_version, 0, "Dump kernel version string in textdump");
165
166 /*
167  * State related to incremental writing of blocks to disk.
168  */
169 static off_t textdump_offset;           /* Offset of next sequential write. */
170 static int textdump_error;              /* Carried write error, if any. */
171
172 /*
173  * Statically allocate space to prepare block-sized headers and data.
174  */
175 char textdump_block_buffer[TEXTDUMP_BLOCKSIZE];
176 static struct kerneldumpheader kdh;
177
178 /*
179  * Calculate and fill in the checksum for a ustar header.
180  */
181 static void
182 ustar_checksum(struct ustar_header *uhp)
183 {
184         u_int sum;
185         int i;
186
187         for (i = 0; i < sizeof(uhp->uh_sum); i++)
188                 uhp->uh_sum[i] = ' ';
189         sum = 0;
190         for (i = 0; i < sizeof(*uhp); i++)
191                 sum += ((u_char *)uhp)[i];
192         snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum);
193 }
194
195 /*
196  * Each file in the tarball has a block-sized header with its name and other,
197  * largely hard-coded, properties.
198  */
199 void
200 textdump_mkustar(char *block_buffer, const char *filename, u_int size)
201 {
202         struct ustar_header *uhp;
203
204         uhp = (struct ustar_header *)block_buffer;
205         bzero(uhp, sizeof(*uhp));
206         strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename));
207         strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode));
208         snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size);
209         strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner));
210         strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group));
211         strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner));
212         strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group));
213         snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo",
214             (unsigned long)time_second);
215         uhp->uh_type = 0;
216         strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar));
217         ustar_checksum(uhp);
218 }
219
220 /*
221  * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to
222  * the space between di->mediaoffset and di->mediaoffset + di->mediasize.  It
223  * accepts an offset relative to di->mediaoffset.  If we're carrying any
224  * error from previous I/O, return that error and don't continue to try to
225  * write.  Most writers ignore the error and forge ahead on the basis that
226  * there's not much you can do.
227  */
228 static int
229 textdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer)
230 {
231
232         if (textdump_error)
233                 return (textdump_error);
234         if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize)
235                 return (EIO);
236         if (offset < SIZEOF_METADATA)
237                 return (ENOSPC);
238         textdump_error = dump_write(di, buffer, 0, offset + di->mediaoffset,
239             TEXTDUMP_BLOCKSIZE);
240         return (textdump_error);
241 }
242
243 /*
244  * Interfaces to save and restore the dump offset, so that printers can go
245  * back to rewrite a header if required, while avoiding their knowing about
246  * the global layout of the blocks.
247  *
248  * If we ever want to support writing textdumps to tape or other
249  * stream-oriented target, we'll need to remove this.
250  */
251 void
252 textdump_saveoff(off_t *offsetp)
253 {
254
255         *offsetp = textdump_offset;
256 }
257
258 void
259 textdump_restoreoff(off_t offset)
260 {
261
262         textdump_offset = offset;
263 }
264
265 /*
266  * Interface to write the "next block" relative to the current offset; since
267  * we write backwards from the end of the partition, we subtract, but there's
268  * no reason for the caller to know this.
269  */
270 int
271 textdump_writenextblock(struct dumperinfo *di, char *buffer)
272 {
273         int error;
274
275         error = textdump_writeblock(di, textdump_offset, buffer);
276         textdump_offset -= TEXTDUMP_BLOCKSIZE;
277         return (error);
278 }
279
280 #ifdef INCLUDE_CONFIG_FILE
281 extern char kernconfstring[];
282
283 /*
284  * Dump kernel configuration.
285  */
286 static void
287 textdump_dump_config(struct dumperinfo *di)
288 {
289         u_int count, fullblocks, len;
290
291         len = strlen(kernconfstring);
292         textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len);
293         (void)textdump_writenextblock(di, textdump_block_buffer);
294
295         /*
296          * Write out all full blocks directly from the string, and handle any
297          * left-over bits by copying it to out to the local buffer and
298          * zero-padding it.
299          */
300         fullblocks = len / TEXTDUMP_BLOCKSIZE;
301         for (count = 0; count < fullblocks; count++)
302                 (void)textdump_writenextblock(di, kernconfstring + count *
303                     TEXTDUMP_BLOCKSIZE);
304         if (len % TEXTDUMP_BLOCKSIZE != 0) {
305                 bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE);
306                 bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE,
307                     textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE);
308                 (void)textdump_writenextblock(di, textdump_block_buffer);
309         }
310 }
311 #endif /* INCLUDE_CONFIG_FILE */
312
313 /*
314  * Dump kernel message buffer.
315  */
316 static void
317 textdump_dump_msgbuf(struct dumperinfo *di)
318 {
319         off_t end_offset, tarhdr_offset;
320         u_int i, len, offset, seq, total_len;
321         char buf[16];
322
323         /*
324          * Write out a dummy tar header to advance the offset; we'll rewrite
325          * it later once we know the true size.
326          */
327         textdump_saveoff(&tarhdr_offset);
328         textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0);
329         (void)textdump_writenextblock(di, textdump_block_buffer);
330
331         /*
332          * Copy out the data in small chunks, but don't copy nuls that may be
333          * present if the message buffer has not yet completely filled at
334          * least once.
335          */
336         total_len = 0;
337         offset = 0;
338         msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
339         while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
340                 for (i = 0; i < len; i++) {
341                         if (buf[i] == '\0')
342                                 continue;
343                         textdump_block_buffer[offset] = buf[i];
344                         offset++;
345                         if (offset != sizeof(textdump_block_buffer))
346                                 continue;
347                         (void)textdump_writenextblock(di,
348                             textdump_block_buffer);
349                         total_len += offset;
350                         offset = 0;
351                 }
352         }
353         total_len += offset;    /* Without the zero-padding. */
354         if (offset != 0) {
355                 bzero(textdump_block_buffer + offset,
356                     sizeof(textdump_block_buffer) - offset);
357                 (void)textdump_writenextblock(di, textdump_block_buffer);
358         }
359
360         /*
361          * Rewrite tar header to reflect how much was actually written.
362          */
363         textdump_saveoff(&end_offset);
364         textdump_restoreoff(tarhdr_offset);
365         textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME,
366             total_len);
367         (void)textdump_writenextblock(di, textdump_block_buffer);
368         textdump_restoreoff(end_offset);
369 }
370
371 static void
372 textdump_dump_panic(struct dumperinfo *di)
373 {
374         u_int len;
375
376         /*
377          * Write out tar header -- we store up to one block of panic message.
378          */
379         len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE);
380         textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len);
381         (void)textdump_writenextblock(di, textdump_block_buffer);
382
383         /*
384          * Zero-pad the panic string and write out block.
385          */
386         bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
387         bcopy(panicstr, textdump_block_buffer, len);
388         (void)textdump_writenextblock(di, textdump_block_buffer);
389 }
390
391 static void
392 textdump_dump_version(struct dumperinfo *di)
393 {
394         u_int len;
395
396         /*
397          * Write out tar header -- at most one block of version information.
398          */
399         len = min(strlen(version), TEXTDUMP_BLOCKSIZE);
400         textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len);
401         (void)textdump_writenextblock(di, textdump_block_buffer);
402
403         /*
404          * Zero pad the version string and write out block.
405          */
406         bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
407         bcopy(version, textdump_block_buffer, len);
408         (void)textdump_writenextblock(di, textdump_block_buffer);
409 }
410
411 /*
412  * Commit text dump to disk.
413  */
414 void
415 textdump_dumpsys(struct dumperinfo *di)
416 {
417         off_t dumplen, trailer_offset;
418
419         if (di->blocksize != TEXTDUMP_BLOCKSIZE) {
420                 printf("Dump partition block size (%ju) not textdump "
421                     "block size (%ju)", (uintmax_t)di->blocksize,
422                     (uintmax_t)TEXTDUMP_BLOCKSIZE);
423                 return;
424         }
425
426         /*
427          * We don't know a priori how large the dump will be, but we do know
428          * that we need to reserve space for metadata and that we need two
429          * dump headers.  Also leave room for one ustar header and one block
430          * of data.
431          */
432         if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) {
433                 printf("Insufficient space on dump partition.\n");
434                 return;
435         }
436         textdump_error = 0;
437
438         /*
439          * Position the start of the dump so that we'll write the kernel dump
440          * trailer immediately before the end of the partition, and then work
441          * our way back.  We will rewrite this header later to reflect the
442          * true size if things go well.
443          */
444         textdump_offset = di->mediasize - sizeof(kdh);
445         textdump_saveoff(&trailer_offset);
446         mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE);
447         (void)textdump_writenextblock(di, (char *)&kdh);
448
449         /*
450          * Write a series of files in ustar format.
451          */
452         if (textdump_do_ddb)
453                 db_capture_dump(di);
454 #ifdef INCLUDE_CONFIG_FILE
455         if (textdump_do_config)
456                 textdump_dump_config(di);
457 #endif
458         if (textdump_do_msgbuf)
459                 textdump_dump_msgbuf(di);
460         if (textdump_do_panic && panicstr != NULL)
461                 textdump_dump_panic(di);
462         if (textdump_do_version)
463                 textdump_dump_version(di);
464
465         /*
466          * Now that we know the true size, we can write out the header, then
467          * seek back to the end and rewrite the trailer with the correct
468          * size.
469          */
470         dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE);
471         mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, dumplen,
472             TEXTDUMP_BLOCKSIZE);
473         (void)textdump_writenextblock(di, (char *)&kdh);
474         textdump_restoreoff(trailer_offset);
475         (void)textdump_writenextblock(di, (char *)&kdh);
476
477         /*
478          * Terminate the dump, report any errors, and clear the pending flag.
479          */
480         if (textdump_error == 0)
481                 (void)dump_write(di, NULL, 0, 0, 0);
482         if (textdump_error == ENOSPC)
483                 printf("Insufficient space on dump partition\n");
484         else if (textdump_error != 0)
485                 printf("Error %d writing dump\n", textdump_error);
486         else
487                 printf("Textdump complete.\n");
488         textdump_pending = 0;
489 }
490
491 /*-
492  * DDB(4) command to manage textdumps:
493  *
494  * textdump set        - request a textdump
495  * textdump status     - print DDB output textdump status
496  * textdump unset      - clear textdump request
497  */
498 static void
499 db_textdump_usage(void)
500 {
501
502         db_printf("textdump [unset|set|status]\n");
503 }
504
505 void
506 db_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
507     char *modif)
508 {
509         int t;
510
511         t = db_read_token();
512         if (t != tIDENT) {
513                 db_textdump_usage();
514                 return;
515         }
516         if (db_read_token() != tEOL) {
517                 db_textdump_usage();
518                 return;
519         }
520         if (strcmp(db_tok_string, "set") == 0) {
521                 textdump_pending = 1;
522                 db_printf("textdump set\n");
523         } else if (strcmp(db_tok_string, "status") == 0) {
524                 if (textdump_pending)
525                         db_printf("textdump is set\n");
526                 else
527                         db_printf("textdump is not set\n");
528         } else if (strcmp(db_tok_string, "unset") == 0) {
529                 textdump_pending = 0;
530                 db_printf("textdump unset\n");
531         } else
532                 db_textdump_usage();
533 }