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