From e6592ee55c70841f91ba307ed5da17d12cd809bd Mon Sep 17 00:00:00 2001 From: Peter Wemm Date: Wed, 1 Oct 2008 22:08:53 +0000 Subject: [PATCH] Collect N identical (or near identical) mkdumpheader() implementations into one, as threatened in the comment. Textdump magic can be passed in. --- sys/amd64/amd64/dump_machdep.c | 23 +---------------------- sys/amd64/amd64/minidump_machdep.c | 23 +---------------------- sys/arm/arm/dump_machdep.c | 23 +---------------------- sys/ddb/db_textdump.c | 28 ++-------------------------- sys/i386/i386/dump_machdep.c | 23 +---------------------- sys/i386/i386/minidump_machdep.c | 23 +---------------------- sys/ia64/ia64/dump_machdep.c | 23 +---------------------- sys/kern/kern_shutdown.c | 22 ++++++++++++++++++++++ sys/sparc64/sparc64/dump_machdep.c | 23 +---------------------- sys/sun4v/sun4v/dump_machdep.c | 23 +---------------------- sys/sys/kerneldump.h | 5 +++++ 11 files changed, 37 insertions(+), 202 deletions(-) diff --git a/sys/amd64/amd64/dump_machdep.c b/sys/amd64/amd64/dump_machdep.c index 08d32d5ab7d9..c07f0cae5945 100644 --- a/sys/amd64/amd64/dump_machdep.c +++ b/sys/amd64/amd64/dump_machdep.c @@ -105,27 +105,6 @@ md_pa_next(struct md_pa *mdp) return (mdp); } -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -326,7 +305,7 @@ dumpsys(struct dumperinfo *di) dumplo = di->mediaoffset + di->mediasize - dumpsize; dumplo -= sizeof(kdh) * 2; - mkdumpheader(&kdh, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize); printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20, ehdr.e_phnum); diff --git a/sys/amd64/amd64/minidump_machdep.c b/sys/amd64/amd64/minidump_machdep.c index d3c4309ced96..ec6727c5c243 100644 --- a/sys/amd64/amd64/minidump_machdep.c +++ b/sys/amd64/amd64/minidump_machdep.c @@ -81,27 +81,6 @@ is_dumpable(vm_paddr_t pa) return (0); } -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8) static int @@ -284,7 +263,7 @@ minidumpsys(struct dumperinfo *di) mdhdr.dmapbase = DMAP_MIN_ADDRESS; mdhdr.dmapend = DMAP_MAX_ADDRESS; - mkdumpheader(&kdh, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize); printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576); printf("Dumping %llu MB:", (long long)dumpsize >> 20); diff --git a/sys/arm/arm/dump_machdep.c b/sys/arm/arm/dump_machdep.c index 391bc401b1e5..87943c251801 100644 --- a/sys/arm/arm/dump_machdep.c +++ b/sys/arm/arm/dump_machdep.c @@ -103,27 +103,6 @@ md_pa_next(struct md_pa *mdp) return (mdp); } -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -325,7 +304,7 @@ dumpsys(struct dumperinfo *di) dumplo = di->mediaoffset + di->mediasize - dumpsize; dumplo -= sizeof(kdh) * 2; - mkdumpheader(&kdh, KERNELDUMP_ARM_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize, di->blocksize); printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20, ehdr.e_phnum); diff --git a/sys/ddb/db_textdump.c b/sys/ddb/db_textdump.c index dd48acc372d8..e9069620d94e 100644 --- a/sys/ddb/db_textdump.c +++ b/sys/ddb/db_textdump.c @@ -176,30 +176,6 @@ static int textdump_error; /* Carried write error, if any. */ char textdump_block_buffer[TEXTDUMP_BLOCKSIZE]; static struct kerneldumpheader kdh; -/* - * Text dumps are prefixed with a normal kernel dump header but with a - * different magic number to allow them to be uniquely identified. - */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, - uint64_t dumplen, uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, TEXTDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - /* * Calculate and fill in the checksum for a ustar header. */ @@ -468,7 +444,7 @@ textdump_dumpsys(struct dumperinfo *di) */ textdump_offset = di->mediasize - sizeof(kdh); textdump_saveoff(&trailer_offset); - mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); + mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, 0, TEXTDUMP_BLOCKSIZE); (void)textdump_writenextblock(di, (char *)&kdh); /* @@ -493,7 +469,7 @@ textdump_dumpsys(struct dumperinfo *di) * size. */ dumplen = trailer_offset - (textdump_offset + TEXTDUMP_BLOCKSIZE); - mkdumpheader(&kdh, KERNELDUMP_TEXT_VERSION, dumplen, + mkdumpheader(&kdh, TEXTDUMPMAGIC, KERNELDUMP_TEXT_VERSION, dumplen, TEXTDUMP_BLOCKSIZE); (void)textdump_writenextblock(di, (char *)&kdh); textdump_restoreoff(trailer_offset); diff --git a/sys/i386/i386/dump_machdep.c b/sys/i386/i386/dump_machdep.c index b9a18e32f4d3..4522dc3f7fb4 100644 --- a/sys/i386/i386/dump_machdep.c +++ b/sys/i386/i386/dump_machdep.c @@ -105,27 +105,6 @@ md_pa_next(struct md_pa *mdp) return (mdp); } -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -326,7 +305,7 @@ dumpsys(struct dumperinfo *di) dumplo = di->mediaoffset + di->mediasize - dumpsize; dumplo -= sizeof(kdh) * 2; - mkdumpheader(&kdh, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize); printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20, ehdr.e_phnum); diff --git a/sys/i386/i386/minidump_machdep.c b/sys/i386/i386/minidump_machdep.c index 61d28d353482..2daab3b1f465 100644 --- a/sys/i386/i386/minidump_machdep.c +++ b/sys/i386/i386/minidump_machdep.c @@ -79,27 +79,6 @@ is_dumpable(vm_paddr_t pa) return (0); } -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8) static int @@ -280,7 +259,7 @@ minidumpsys(struct dumperinfo *di) mdhdr.paemode = 1; #endif - mkdumpheader(&kdh, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize); printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576); printf("Dumping %llu MB:", (long long)dumpsize >> 20); diff --git a/sys/ia64/ia64/dump_machdep.c b/sys/ia64/ia64/dump_machdep.c index a2dfe45bdcf5..db73271d0d0d 100644 --- a/sys/ia64/ia64/dump_machdep.c +++ b/sys/ia64/ia64/dump_machdep.c @@ -60,27 +60,6 @@ static off_t dumplo, fileofs; static char buffer[DEV_BSIZE]; static size_t fragsz; -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -261,7 +240,7 @@ dumpsys(struct dumperinfo *di) dumplo = di->mediaoffset + di->mediasize - dumpsize; dumplo -= sizeof(kdh) * 2; - mkdumpheader(&kdh, KERNELDUMP_IA64_VERSION, dumpsize, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_IA64_VERSION, dumpsize, di->blocksize); printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20, ehdr.e_phnum); diff --git a/sys/kern/kern_shutdown.c b/sys/kern/kern_shutdown.c index e6d4f4466f2a..238abc7198d6 100644 --- a/sys/kern/kern_shutdown.c +++ b/sys/kern/kern_shutdown.c @@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -64,6 +65,7 @@ __FBSDID("$FreeBSD$"); #include /* smp_active */ #include #include +#include #include @@ -686,3 +688,23 @@ dumpsys(struct dumperinfo *di __unused) printf("Kernel dumps not implemented on this architecture\n"); } #endif + +void +mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, + uint64_t dumplen, uint32_t blksz) +{ + + bzero(kdh, sizeof(*kdh)); + strncpy(kdh->magic, magic, sizeof(kdh->magic)); + strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); + kdh->version = htod32(KERNELDUMPVERSION); + kdh->architectureversion = htod32(archver); + kdh->dumplength = htod64(dumplen); + kdh->dumptime = htod64(time_second); + kdh->blocksize = htod32(blksz); + strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); + strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); + if (panicstr != NULL) + strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); + kdh->parity = kerneldump_parity(kdh); +} diff --git a/sys/sparc64/sparc64/dump_machdep.c b/sys/sparc64/sparc64/dump_machdep.c index 19e94ccd39a7..5a90175a2aca 100644 --- a/sys/sparc64/sparc64/dump_machdep.c +++ b/sys/sparc64/sparc64/dump_machdep.c @@ -56,27 +56,6 @@ static vm_size_t fragsz; #define MAXDUMPSZ (MAXDUMPPGS << PAGE_SHIFT) -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -191,7 +170,7 @@ dumpsys(struct dumperinfo *di) /* Determine dump offset on device. */ dumplo = di->mediaoffset + di->mediasize - totsize; - mkdumpheader(&kdh, KERNELDUMP_SPARC64_VERSION, size, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_SPARC64_VERSION, size, di->blocksize); printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg); diff --git a/sys/sun4v/sun4v/dump_machdep.c b/sys/sun4v/sun4v/dump_machdep.c index f3acdc66c0d7..3a5d134ab368 100644 --- a/sys/sun4v/sun4v/dump_machdep.c +++ b/sys/sun4v/sun4v/dump_machdep.c @@ -57,27 +57,6 @@ static vm_size_t fragsz; #define MAXDUMPSZ (MAXDUMPPGS << PAGE_SHIFT) -/* XXX should be MI */ -static void -mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen, - uint32_t blksz) -{ - - bzero(kdh, sizeof(*kdh)); - strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic)); - strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); - kdh->version = htod32(KERNELDUMPVERSION); - kdh->architectureversion = htod32(archver); - kdh->dumplength = htod64(dumplen); - kdh->dumptime = htod64(time_second); - kdh->blocksize = htod32(blksz); - strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname)); - strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); - if (panicstr != NULL) - strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); - kdh->parity = kerneldump_parity(kdh); -} - static int buf_write(struct dumperinfo *di, char *ptr, size_t sz) { @@ -194,7 +173,7 @@ dumpsys(struct dumperinfo *di) /* Determine dump offset on device. */ dumplo = di->mediaoffset + di->mediasize - totsize; - mkdumpheader(&kdh, KERNELDUMP_SPARC64_VERSION, size, di->blocksize); + mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_SPARC64_VERSION, size, di->blocksize); printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg); diff --git a/sys/sys/kerneldump.h b/sys/sys/kerneldump.h index f9f851bd2dbd..3f3eacd83148 100644 --- a/sys/sys/kerneldump.h +++ b/sys/sys/kerneldump.h @@ -98,4 +98,9 @@ kerneldump_parity(struct kerneldumpheader *kdhp) return (parity); } +#ifdef _KERNEL +void mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, + uint64_t dumplen, uint32_t blksz); +#endif + #endif /* _SYS_KERNELDUMP_H */