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