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