db_textdump.c revision 183527
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: head/sys/ddb/db_textdump.c 183527 2008-10-01 22:08:53Z peter $");
61174921Srwatson
62174921Srwatson#include "opt_config.h"
63174921Srwatson
64174921Srwatson#include <sys/param.h>
65174921Srwatson#include <sys/conf.h>
66174921Srwatson#include <sys/kernel.h>
67174921Srwatson#include <sys/kerneldump.h>
68174921Srwatson#include <sys/msgbuf.h>
69174921Srwatson#include <sys/sysctl.h>
70174921Srwatson#include <sys/systm.h>
71181803Sbz#include <sys/vimage.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/*
180175199Srwatson * Calculate and fill in the checksum for a ustar header.
181174921Srwatson */
182174921Srwatsonstatic void
183174921Srwatsonustar_checksum(struct ustar_header *uhp)
184174921Srwatson{
185174921Srwatson	u_int sum;
186174921Srwatson	int i;
187174921Srwatson
188174921Srwatson	for (i = 0; i < sizeof(uhp->uh_sum); i++)
189174921Srwatson		uhp->uh_sum[i] = ' ';
190174921Srwatson	sum = 0;
191174921Srwatson	for (i = 0; i < sizeof(*uhp); i++)
192174921Srwatson		sum += ((u_char *)uhp)[i];
193174921Srwatson	snprintf(uhp->uh_sum, sizeof(uhp->uh_sum), "%6o", sum);
194174921Srwatson}
195174921Srwatson
196174921Srwatson/*
197174921Srwatson * Each file in the tarball has a block-sized header with its name and other,
198174921Srwatson * largely hard-coded, properties.
199174921Srwatson */
200174921Srwatsonvoid
201174921Srwatsontextdump_mkustar(char *block_buffer, const char *filename, u_int size)
202174921Srwatson{
203174921Srwatson	struct ustar_header *uhp;
204174921Srwatson
205174921Srwatson	uhp = (struct ustar_header *)block_buffer;
206174921Srwatson	bzero(uhp, sizeof(*uhp));
207174921Srwatson	strlcpy(uhp->uh_filename, filename, sizeof(uhp->uh_filename));
208174921Srwatson	strlcpy(uhp->uh_mode, TAR_MODE, sizeof(uhp->uh_mode));
209174921Srwatson	snprintf(uhp->uh_size, sizeof(uhp->uh_size), "%o", size);
210174921Srwatson	strlcpy(uhp->uh_tar_owner, TAR_UID, sizeof(uhp->uh_tar_owner));
211174921Srwatson	strlcpy(uhp->uh_tar_group, TAR_GID, sizeof(uhp->uh_tar_group));
212174921Srwatson	strlcpy(uhp->uh_owner, TAR_USER, sizeof(uhp->uh_owner));
213174921Srwatson	strlcpy(uhp->uh_group, TAR_GROUP, sizeof(uhp->uh_group));
214174921Srwatson	snprintf(uhp->uh_mtime, sizeof(uhp->uh_mtime), "%lo",
215174921Srwatson	    (unsigned long)time_second);
216174921Srwatson	uhp->uh_type = 0;
217174921Srwatson	strlcpy(uhp->uh_ustar, TAR_USTAR, sizeof(uhp->uh_ustar));
218174921Srwatson	ustar_checksum(uhp);
219174921Srwatson}
220174921Srwatson
221174921Srwatson/*
222174921Srwatson * textdump_writeblock() writes TEXTDUMP_BLOCKSIZE-sized blocks of data to
223174921Srwatson * the space between di->mediaoffset and di->mediaoffset + di->mediasize.  It
224174921Srwatson * accepts an offset relative to di->mediaoffset.  If we're carrying any
225174921Srwatson * error from previous I/O, return that error and don't continue to try to
226174921Srwatson * write.  Most writers ignore the error and forge ahead on the basis that
227174921Srwatson * there's not much you can do.
228174921Srwatson */
229174921Srwatsonstatic int
230174921Srwatsontextdump_writeblock(struct dumperinfo *di, off_t offset, char *buffer)
231174921Srwatson{
232174921Srwatson
233174921Srwatson	if (textdump_error)
234174921Srwatson		return (textdump_error);
235174921Srwatson	if (offset + TEXTDUMP_BLOCKSIZE > di->mediasize)
236174921Srwatson		return (EIO);
237174921Srwatson	if (offset < SIZEOF_METADATA)
238174921Srwatson		return (ENOSPC);
239175858Srwatson	textdump_error = dump_write(di, buffer, 0, offset + di->mediaoffset,
240175858Srwatson	    TEXTDUMP_BLOCKSIZE);
241174921Srwatson	return (textdump_error);
242174921Srwatson}
243174921Srwatson
244174921Srwatson/*
245174921Srwatson * Interfaces to save and restore the dump offset, so that printers can go
246174921Srwatson * back to rewrite a header if required, while avoiding their knowing about
247174921Srwatson * the global layout of the blocks.
248175199Srwatson *
249175199Srwatson * If we ever want to support writing textdumps to tape or other
250175199Srwatson * stream-oriented target, we'll need to remove this.
251174921Srwatson */
252174921Srwatsonvoid
253174921Srwatsontextdump_saveoff(off_t *offsetp)
254174921Srwatson{
255174921Srwatson
256174921Srwatson	*offsetp = textdump_offset;
257174921Srwatson}
258174921Srwatson
259174921Srwatsonvoid
260174921Srwatsontextdump_restoreoff(off_t offset)
261174921Srwatson{
262174921Srwatson
263174921Srwatson	textdump_offset = offset;
264174921Srwatson}
265174921Srwatson
266174921Srwatson/*
267174921Srwatson * Interface to write the "next block" relative to the current offset; since
268174921Srwatson * we write backwards from the end of the partition, we subtract, but there's
269174921Srwatson * no reason for the caller to know this.
270174921Srwatson */
271174921Srwatsonint
272174921Srwatsontextdump_writenextblock(struct dumperinfo *di, char *buffer)
273174921Srwatson{
274174921Srwatson	int error;
275174921Srwatson
276174921Srwatson	error = textdump_writeblock(di, textdump_offset, buffer);
277174921Srwatson	textdump_offset -= TEXTDUMP_BLOCKSIZE;
278174921Srwatson	return (error);
279174921Srwatson}
280174921Srwatson
281174921Srwatson#ifdef INCLUDE_CONFIG_FILE
282174921Srwatsonextern char kernconfstring[];
283174921Srwatson
284174921Srwatson/*
285174921Srwatson * Dump kernel configuration.
286174921Srwatson */
287174921Srwatsonstatic void
288174921Srwatsontextdump_dump_config(struct dumperinfo *di)
289174921Srwatson{
290174921Srwatson	u_int count, fullblocks, len;
291174921Srwatson
292174921Srwatson	len = strlen(kernconfstring);
293174921Srwatson	textdump_mkustar(textdump_block_buffer, TAR_CONFIG_FILENAME, len);
294174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
295174921Srwatson
296174921Srwatson	/*
297174921Srwatson	 * Write out all full blocks directly from the string, and handle any
298174921Srwatson	 * left-over bits by copying it to out to the local buffer and
299174921Srwatson	 * zero-padding it.
300174921Srwatson	 */
301174921Srwatson	fullblocks = len / TEXTDUMP_BLOCKSIZE;
302174921Srwatson	for (count = 0; count < fullblocks; count++)
303174921Srwatson		(void)textdump_writenextblock(di, kernconfstring + count *
304174921Srwatson		    TEXTDUMP_BLOCKSIZE);
305174921Srwatson	if (len % TEXTDUMP_BLOCKSIZE != 0) {
306174921Srwatson		bzero(textdump_block_buffer, TEXTDUMP_BLOCKSIZE);
307174921Srwatson		bcopy(kernconfstring + count * TEXTDUMP_BLOCKSIZE,
308174921Srwatson		    textdump_block_buffer, len % TEXTDUMP_BLOCKSIZE);
309174921Srwatson		(void)textdump_writenextblock(di, textdump_block_buffer);
310174921Srwatson	}
311174921Srwatson}
312174921Srwatson#endif /* INCLUDE_CONFIG_FILE */
313174921Srwatson
314174921Srwatson/*
315174921Srwatson * Dump kernel message buffer.
316174921Srwatson */
317174921Srwatsonstatic void
318174921Srwatsontextdump_dump_msgbuf(struct dumperinfo *di)
319174921Srwatson{
320174921Srwatson	off_t end_offset, tarhdr_offset;
321174921Srwatson	u_int i, len, offset, seq, total_len;
322174921Srwatson	char buf[16];
323174921Srwatson
324174921Srwatson	/*
325174921Srwatson	 * Write out a dummy tar header to advance the offset; we'll rewrite
326174921Srwatson	 * it later once we know the true size.
327174921Srwatson	 */
328174921Srwatson	textdump_saveoff(&tarhdr_offset);
329174921Srwatson	textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME, 0);
330174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
331174921Srwatson
332174921Srwatson	/*
333174921Srwatson	 * Copy out the data in small chunks, but don't copy nuls that may be
334174921Srwatson	 * present if the message buffer has not yet completely filled at
335174921Srwatson	 * least once.
336174921Srwatson	 */
337174921Srwatson	total_len = 0;
338174921Srwatson	offset = 0;
339174921Srwatson        msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
340174921Srwatson        while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
341174921Srwatson		for (i = 0; i < len; i++) {
342174921Srwatson			if (buf[i] == '\0')
343174921Srwatson				continue;
344174921Srwatson			textdump_block_buffer[offset] = buf[i];
345174921Srwatson			offset++;
346174921Srwatson			if (offset != sizeof(textdump_block_buffer))
347174921Srwatson				continue;
348174921Srwatson			(void)textdump_writenextblock(di,
349174921Srwatson			    textdump_block_buffer);
350174921Srwatson			total_len += offset;
351174921Srwatson			offset = 0;
352174921Srwatson		}
353174921Srwatson        }
354174921Srwatson	total_len += offset;	/* Without the zero-padding. */
355174921Srwatson	if (offset != 0) {
356174921Srwatson		bzero(textdump_block_buffer + offset,
357174921Srwatson		    sizeof(textdump_block_buffer) - offset);
358174921Srwatson		(void)textdump_writenextblock(di, textdump_block_buffer);
359174921Srwatson	}
360174921Srwatson
361174921Srwatson	/*
362174921Srwatson	 * Rewrite tar header to reflect how much was actually written.
363174921Srwatson	 */
364174921Srwatson	textdump_saveoff(&end_offset);
365174921Srwatson	textdump_restoreoff(tarhdr_offset);
366174921Srwatson	textdump_mkustar(textdump_block_buffer, TAR_MSGBUF_FILENAME,
367174921Srwatson	    total_len);
368174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
369174921Srwatson	textdump_restoreoff(end_offset);
370174921Srwatson}
371174921Srwatson
372174921Srwatsonstatic void
373174921Srwatsontextdump_dump_panic(struct dumperinfo *di)
374174921Srwatson{
375174921Srwatson	u_int len;
376174921Srwatson
377174921Srwatson	/*
378174921Srwatson	 * Write out tar header -- we store up to one block of panic message.
379174921Srwatson	 */
380174921Srwatson	len = min(strlen(panicstr), TEXTDUMP_BLOCKSIZE);
381174921Srwatson	textdump_mkustar(textdump_block_buffer, TAR_PANIC_FILENAME, len);
382174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
383174921Srwatson
384174921Srwatson	/*
385174921Srwatson	 * Zero-pad the panic string and write out block.
386174921Srwatson	 */
387174921Srwatson	bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
388174921Srwatson	bcopy(panicstr, textdump_block_buffer, len);
389174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
390174921Srwatson}
391174921Srwatson
392174921Srwatsonstatic void
393174921Srwatsontextdump_dump_version(struct dumperinfo *di)
394174921Srwatson{
395174921Srwatson	u_int len;
396174921Srwatson
397174921Srwatson	/*
398174921Srwatson	 * Write out tar header -- at most one block of version information.
399174921Srwatson	 */
400174921Srwatson	len = min(strlen(version), TEXTDUMP_BLOCKSIZE);
401174921Srwatson	textdump_mkustar(textdump_block_buffer, TAR_VERSION_FILENAME, len);
402174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
403174921Srwatson
404174921Srwatson	/*
405174921Srwatson	 * Zero pad the version string and write out block.
406174921Srwatson	 */
407174921Srwatson	bzero(textdump_block_buffer, sizeof(textdump_block_buffer));
408174921Srwatson	bcopy(version, textdump_block_buffer, len);
409174921Srwatson	(void)textdump_writenextblock(di, textdump_block_buffer);
410174921Srwatson}
411174921Srwatson
412174921Srwatson/*
413174921Srwatson * Commit text dump to disk.
414174921Srwatson */
415174921Srwatsonvoid
416174921Srwatsontextdump_dumpsys(struct dumperinfo *di)
417174921Srwatson{
418174921Srwatson	off_t dumplen, trailer_offset;
419174921Srwatson
420174921Srwatson	if (di->blocksize != TEXTDUMP_BLOCKSIZE) {
421174921Srwatson		printf("Dump partition block size (%ju) not textdump "
422174921Srwatson		    "block size (%ju)", (uintmax_t)di->blocksize,
423174921Srwatson		    (uintmax_t)TEXTDUMP_BLOCKSIZE);
424174921Srwatson		return;
425174921Srwatson	}
426174921Srwatson
427174921Srwatson	/*
428174921Srwatson	 * We don't know a priori how large the dump will be, but we do know
429174921Srwatson	 * that we need to reserve space for metadata and that we need two
430174921Srwatson	 * dump headers.  Also leave room for one ustar header and one block
431174921Srwatson	 * of data.
432174921Srwatson	 */
433174921Srwatson	if (di->mediasize < SIZEOF_METADATA + 2 * sizeof(kdh)) {
434174921Srwatson		printf("Insufficient space on dump partition.\n");
435174921Srwatson		return;
436174921Srwatson	}
437174921Srwatson	textdump_error = 0;
438174921Srwatson
439174921Srwatson	/*
440174921Srwatson	 * Position the start of the dump so that we'll write the kernel dump
441174921Srwatson	 * trailer immediately before the end of the partition, and then work
442174921Srwatson	 * our way back.  We will rewrite this header later to reflect the
443174921Srwatson	 * true size if things go well.
444174921Srwatson	 */
445174921Srwatson	textdump_offset = di->mediasize - sizeof(kdh);
446174921Srwatson	textdump_saveoff(&trailer_offset);
447183527Speter	mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE);
448174921Srwatson	(void)textdump_writenextblock(di, (char *)&kdh);
449174921Srwatson
450174921Srwatson	/*
451174921Srwatson	 * Write a series of files in ustar format.
452174921Srwatson	 */
453174921Srwatson	if (textdump_do_ddb)
454174921Srwatson		db_capture_dump(di);
455174921Srwatson#ifdef INCLUDE_CONFIG_FILE
456174921Srwatson	if (textdump_do_config)
457174921Srwatson		textdump_dump_config(di);
458174921Srwatson#endif
459174921Srwatson	if (textdump_do_msgbuf)
460174921Srwatson		textdump_dump_msgbuf(di);
461174921Srwatson	if (textdump_do_panic && panicstr != NULL)
462174921Srwatson		textdump_dump_panic(di);
463174921Srwatson	if (textdump_do_version)
464174921Srwatson		textdump_dump_version(di);
465174921Srwatson
466174921Srwatson	/*
467174921Srwatson	 * Now that we know the true size, we can write out the header, then
468174921Srwatson	 * seek back to the end and rewrite the trailer with the correct
469174921Srwatson	 * size.
470174921Srwatson	 */
471174921Srwatson	dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE);
472183527Speter	mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, dumplen,
473174921Srwatson	    TEXTDUMP_BLOCKSIZE);
474174921Srwatson	(void)textdump_writenextblock(di, (char *)&kdh);
475174921Srwatson	textdump_restoreoff(trailer_offset);
476174921Srwatson	(void)textdump_writenextblock(di, (char *)&kdh);
477174921Srwatson
478174921Srwatson	/*
479174921Srwatson	 * Terminate the dump, report any errors, and clear the pending flag.
480174921Srwatson	 */
481174921Srwatson	if (textdump_error == 0)
482175858Srwatson		(void)dump_write(di, NULL, 0, 0, 0);
483174921Srwatson	if (textdump_error == ENOSPC)
484174921Srwatson		printf("Insufficient space on dump partition\n");
485174921Srwatson	else if (textdump_error != 0)
486174921Srwatson		printf("Error %d writing dump\n", textdump_error);
487174921Srwatson	else
488174921Srwatson		printf("Textdump complete.\n");
489174921Srwatson	textdump_pending = 0;
490174921Srwatson}
491174921Srwatson
492174921Srwatson/*-
493174921Srwatson * DDB(4) command to manage textdumps:
494174921Srwatson *
495174921Srwatson * textdump set        - request a textdump
496174921Srwatson * textdump status     - print DDB output textdump status
497174921Srwatson * textdump unset      - clear textdump request
498174921Srwatson */
499174921Srwatsonstatic void
500174921Srwatsondb_textdump_usage(void)
501174921Srwatson{
502174921Srwatson
503174921Srwatson	db_printf("textdump [unset|set|status]\n");
504174921Srwatson}
505174921Srwatson
506174921Srwatsonvoid
507174921Srwatsondb_textdump_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
508174921Srwatson    char *modif)
509174921Srwatson{
510174921Srwatson	int t;
511174921Srwatson
512174921Srwatson	t = db_read_token();
513174921Srwatson	if (t != tIDENT) {
514174921Srwatson		db_textdump_usage();
515174921Srwatson		return;
516174921Srwatson	}
517174921Srwatson	if (db_read_token() != tEOL) {
518174921Srwatson		db_textdump_usage();
519174921Srwatson		return;
520174921Srwatson	}
521174921Srwatson	if (strcmp(db_tok_string, "set") == 0) {
522174921Srwatson		textdump_pending = 1;
523174921Srwatson		db_printf("textdump set\n");
524174921Srwatson	} else if (strcmp(db_tok_string, "status") == 0) {
525174921Srwatson		if (textdump_pending)
526174921Srwatson			db_printf("textdump is set\n");
527174921Srwatson		else
528174921Srwatson			db_printf("textdump is not set\n");
529174921Srwatson	} else if (strcmp(db_tok_string, "unset") == 0) {
530174921Srwatson		textdump_pending = 0;
531174921Srwatson		db_printf("textdump unset\n");
532174921Srwatson	} else
533174921Srwatson		db_textdump_usage();
534174921Srwatson}
535