2 * Copyright (c) 1986, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1986, 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)savecore.c 8.3 (Berkeley) 1/2/94
35 * $FreeBSD: src/sbin/savecore/savecore.c,v 1.28.2.14 2005/01/05 09:14:34 maxim Exp $
36 * $DragonFly: src/sbin/savecore/savecore.c,v 1.15 2007/05/19 06:54:29 dillon Exp $
39 #define _KERNEL_STRUCTURES
41 #include <sys/param.h>
43 #undef _KERNEL_STRUCTURES
46 #include <sys/mount.h>
47 #include <sys/syslog.h>
48 #include <sys/sysctl.h>
51 #include <vm/vm_param.h>
64 extern FILE *zopen(const char *fname, const char *mode);
66 #define ok(number) ((number) - kernbase)
68 struct nlist current_nl[] = { /* Namelist for currently running system. */
70 { "_dumplo64", 0, 0, 0, 0 },
72 { "_time_second", 0, 0, 0, 0 },
74 { "_dumpsize", 0, 0, 0, 0 },
76 { "_version", 0, 0, 0, 0 },
78 { "_panicstr", 0, 0, 0, 0 },
80 { "_dumpmag", 0, 0, 0, 0 },
82 { "_kernbase", 0, 0, 0, 0 },
84 { "_Maxmem", 0, 0, 0, 0 },
87 int cursyms[] = { X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
88 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
90 struct nlist dump_nl[] = { /* Name list for dumped system. */
91 { "_dumplo64", 0, 0, 0, 0 }, /* Entries MUST be the same as */
92 { "_time_second", 0, 0, 0, 0 }, /* those in current_nl[]. */
93 { "_dumpsize", 0, 0, 0, 0 },
94 { "_version", 0, 0, 0, 0 },
95 { "_panicstr", 0, 0, 0, 0 },
96 { "_dumpmag", 0, 0, 0, 0 },
97 { "_kernbase", 0, 0, 0, 0 },
98 { "_Maxmem", 0, 0, 0, 0 },
102 /* Types match kernel declarations. */
103 u_long dumpmag; /* magic number in dump */
105 /* Based on kernel variables, but with more convenient types. */
106 off_t dumplo; /* where dump starts on dumpdev */
107 off_t dumpsize; /* amount of memory dumped */
109 char *kernel; /* user-specified kernel */
110 char *savedir; /* directory to save dumps in */
111 char ddname[MAXPATHLEN]; /* name of dump device */
112 dev_t dumpdev; /* dump device */
113 int dumpfd; /* read/write descriptor on char dev */
114 time_t now; /* current date */
115 char panic_mesg[1024]; /* panic message */
116 int panicstr; /* flag: dump was caused by panic */
117 char vers[1024]; /* version of kernel that crashed */
118 char *physmem; /* physmem value used with dumped session */
119 u_int64_t dkdumplo; /* directly specified kernel dumplo value */
121 u_long kernbase; /* offset of kvm to core file */
123 static int clear, compress, force, verbose, directdumplo; /* flags */
124 static int keep; /* keep dump on device */
126 static void check_kmem(void);
127 static int check_space(void);
128 static void clear_dump(void);
129 static void DumpRead(int fd, void *bp, int size, off_t off, int flag);
130 static void DumpWrite(int fd, void *bp, int size, off_t off, int flag);
131 static int dump_exists(void);
132 static void find_dev(dev_t);
133 static int get_crashtime(void);
134 static void get_dumpsize(void);
135 static void kmem_setup(void);
136 static void Lseek(int, off_t, int);
137 static int Open(const char *, int rw);
138 static int Read(int, void *, int);
139 static void save_core(void);
140 static void usage(void);
141 static int verify_dev(char *, dev_t);
142 static void Write(int, void *, int);
143 static void kdumplo_adjust(char *cp, int kmem, u_int64_t *kdumplop);
146 main(int argc, char **argv)
151 openlog("savecore", LOG_PERROR, LOG_DAEMON);
153 while ((ch = getopt(argc, argv, "cD:dfkN:vzP:B:")) != -1)
159 strncpy(ddname, optarg, sizeof(ddname));
161 case 'd': /* Not documented. */
182 dkdumplo = strtouq(optarg, &ep, 10);
184 errx(1, "invalid offset: '%s'", optarg);
194 if (argc != 1 && argc != 2)
209 if (!dump_exists() && !force)
215 syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
217 syslog(LOG_ALERT, "reboot");
221 if ((!get_crashtime() || !check_space()) && !force)
236 const char *dump_sys;
238 u_int64_t kdumplo; /* block number where dump starts on dumpdev */
242 * Some names we need for the currently running system, others for
243 * the system that was running when the dump was made. The values
244 * obtained from the current system are used to look for things in
245 * /dev/kmem that cannot be found in the dump_sys namelist, but are
246 * presumed to be the same (since the disk partitions are probably
249 if ((nlist(getbootfile(), current_nl)) == -1)
250 syslog(LOG_ERR, "%s: nlist: %m", getbootfile());
251 for (i = 0; cursyms[i] != -1; i++)
252 if (current_nl[cursyms[i]].n_value == 0) {
253 syslog(LOG_ERR, "%s: %s not in namelist",
254 getbootfile(), current_nl[cursyms[i]].n_name);
258 dump_sys = kernel ? kernel : getbootfile();
259 if ((nlist(dump_sys, dump_nl)) == -1)
260 syslog(LOG_ERR, "%s: nlist: %m", dump_sys);
261 for (i = 0; dumpsyms[i] != -1; i++)
262 if (dump_nl[dumpsyms[i]].n_value == 0) {
263 syslog(LOG_ERR, "%s: %s not in namelist",
264 dump_sys, dump_nl[dumpsyms[i]].n_name);
268 if (dump_nl[X_KERNBASE].n_value != 0)
269 kernbase = dump_nl[X_KERNBASE].n_value;
274 len = sizeof dumpdev;
275 if (sysctlbyname("kern.dumpdev", &dumpdev, &len, NULL, 0) == -1) {
276 syslog(LOG_ERR, "sysctl: kern.dumpdev: %m");
279 if (dumpdev == NODEV) {
280 syslog(LOG_WARNING, "no core dump (no dumpdev)");
286 kmem = Open(_PATH_KMEM, O_RDONLY);
290 Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET);
291 Read(kmem, &kdumplo, sizeof(kdumplo));
293 kdumplo_adjust(physmem, kmem, &kdumplo);
295 dumplo = kdumplo * DEV_BSIZE;
297 printf("dumplo = %lld (%lld * %d)\n",
298 (long long)dumplo, kdumplo, DEV_BSIZE);
299 Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET);
300 Read(kmem, &dumpmag, sizeof(dumpmag));
301 dumpfd = Open(ddname, O_RDWR);
305 lseek(kmem, (off_t)current_nl[X_VERSION].n_value, SEEK_SET);
306 Read(kmem, vers, sizeof(vers));
307 vers[sizeof(vers) - 1] = '\0';
308 p = strchr(vers, '\n');
312 /* Don't fclose(fp), we use kmem later. */
318 char core_vers[1024], *p;
320 DumpRead(dumpfd, core_vers, sizeof(core_vers),
321 (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
322 core_vers[sizeof(core_vers) - 1] = '\0';
323 p = strchr(core_vers, '\n');
326 if (strcmp(vers, core_vers) && kernel == 0)
328 "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
329 getbootfile(), vers, core_vers);
330 DumpRead(dumpfd, &panicstr, sizeof(panicstr),
331 (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
333 DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
334 (off_t)(dumplo + ok(panicstr)), L_SET);
339 * Clear the magic number in the dump header.
347 DumpWrite(dumpfd, &newdumpmag, sizeof(newdumpmag),
348 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
353 * Check if a dump exists by looking for a magic number in the dump
361 DumpRead(dumpfd, &newdumpmag, sizeof(newdumpmag),
362 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
363 if (newdumpmag != dumpmag) {
365 syslog(LOG_WARNING, "magic number mismatch (%lx != %lx)",
366 newdumpmag, dumpmag);
367 syslog(LOG_WARNING, "no core dump");
373 char buf[1024 * 1024];
374 #define BLOCKSIZE (1<<12)
375 #define BLOCKMASK (~(BLOCKSIZE-1))
378 * Save the core dump.
384 int bounds, ifd, nr, nw;
385 int hs, he = 0; /* start and end of hole */
386 char path[MAXPATHLEN];
390 * Get the current number and update the bounds file. Do the update
391 * now, because may fail later and don't want to overwrite anything.
393 snprintf(path, sizeof(path), "%s/bounds", savedir);
394 if ((fp = fopen(path, "r")) == NULL)
396 if (fgets(buf, sizeof(buf), fp) == NULL) {
398 err1: syslog(LOG_WARNING, "%s: %m", path);
404 if ((fp = fopen(path, "w")) == NULL)
405 syslog(LOG_ERR, "%s: %m", path);
407 fprintf(fp, "%d\n", bounds + 1);
411 /* Create the core file. */
412 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
413 snprintf(path, sizeof(path), "%s/vmcore.%d%s",
414 savedir, bounds, compress ? ".gz" : "");
416 fp = zopen(path, "w");
418 fp = fopen(path, "w");
420 syslog(LOG_ERR, "%s: %m", path);
425 /* Seek to the start of the core. */
426 Lseek(dumpfd, (off_t)dumplo, L_SET);
428 /* Copy the core file. */
429 syslog(LOG_NOTICE, "writing %score to %s",
430 compress ? "compressed " : "", path);
431 for (; dumpsize > 0; dumpsize -= nr) {
432 printf("%6ldK\r", (long)(dumpsize / 1024));
434 nr = read(dumpfd, buf, MIN(dumpsize, sizeof(buf)));
438 "WARNING: EOF on dump device");
440 syslog(LOG_ERR, "%s: %m", ddname);
444 nw = fwrite(buf, 1, nr, fp);
446 for (nw = 0; nw < nr; nw = he) {
447 /* find a contiguous block of zeroes */
448 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
449 for (he = hs; he < nr && buf[he] == 0; ++he)
452 /* is the hole long enough to matter? */
453 if (he >= hs + BLOCKSIZE)
457 /* back down to a block boundary */
461 * 1) Don't go beyond the end of the buffer.
462 * 2) If the end of the buffer is less than
463 * BLOCKSIZE bytes away, we're at the end
464 * of the file, so just grab what's left.
466 if (hs + BLOCKSIZE > nr)
470 * At this point, we have a partial ordering:
471 * nw <= hs <= he <= nr
472 * If hs > nw, buf[nw..hs] contains non-zero data.
473 * If he > hs, buf[hs..he] is all zeroes.
476 if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
479 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
484 syslog(LOG_ERR, "%s: %m", path);
485 err2: syslog(LOG_WARNING,
486 "WARNING: vmcore may be incomplete");
494 /* Copy the kernel. */
495 ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
496 snprintf(path, sizeof(path), "%s/kernel.%d%s",
497 savedir, bounds, compress ? ".gz" : "");
499 fp = zopen(path, "w");
501 fp = fopen(path, "w");
503 syslog(LOG_ERR, "%s: %m", path);
506 syslog(LOG_NOTICE, "writing %skernel to %s",
507 compress ? "compressed " : "", path);
508 while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
509 nw = fwrite(buf, 1, nr, fp);
511 syslog(LOG_ERR, "%s: %m", path);
513 "WARNING: kernel may be incomplete");
518 syslog(LOG_ERR, "%s: %m", kernel ? kernel : getbootfile());
520 "WARNING: kernel may be incomplete");
528 * Verify that the specified device node exists and matches the
532 verify_dev(char *name, dev_t dev)
536 if (lstat(name, &sb) == -1)
538 if (!S_ISCHR(sb.st_mode) || sb.st_rdev != dev)
544 * Find the dump device.
546 * 1) try devname(3); see if it returns something sensible
547 * 2) scan /dev for the desired node
548 * 3) as a last resort, try to create the node we need
557 strcpy(ddname, _PATH_DEV);
558 dnp = ddname + sizeof _PATH_DEV - 1;
559 if ((dn = devname(dev, S_IFCHR)) != NULL) {
561 if (verify_dev(ddname, dev) == 0)
564 if ((d = opendir(_PATH_DEV)) != NULL) {
565 while ((ent = readdir(d))) {
566 strcpy(dnp, ent->d_name);
567 if (verify_dev(ddname, dev) == 0) {
575 if (mknod(ddname, S_IFCHR|S_IRUSR|S_IWUSR, dev) == 0)
577 syslog(LOG_ERR, "can't find device %d/%#x", major(dev), minor(dev));
582 * Extract the date and time of the crash from the dump header, and
583 * make sure it looks sane (within one week of current date and time).
588 time_t dumptime; /* Time the dump was taken. */
590 DumpRead(dumpfd, &dumptime, sizeof(dumptime),
591 (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
594 syslog(LOG_ERR, "dump time is zero");
597 printf("savecore: system went down at %s", ctime(&dumptime));
598 #define LEEWAY (7 * 86400)
599 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
600 printf("dump time is unreasonable\n");
607 * Extract the size of the dump from the dump header.
612 int kdumpsize; /* Number of pages in dump. */
614 /* Read the dump size. */
615 DumpRead(dumpfd, &kdumpsize, sizeof(kdumpsize),
616 (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
617 dumpsize = (off_t)kdumpsize * getpagesize();
621 * Check that sufficient space is available on the disk that holds the
629 off_t minfree, spacefree, totfree, kernelsize, needed;
632 char mybuf[100], path[MAXPATHLEN];
634 tkernel = kernel ? kernel : getbootfile();
635 if (stat(tkernel, &st) < 0) {
636 syslog(LOG_ERR, "%s: %m", tkernel);
639 kernelsize = st.st_blocks * S_BLKSIZE;
641 if (statfs(savedir, &fsbuf) < 0) {
642 syslog(LOG_ERR, "%s: %m", savedir);
645 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
646 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
648 snprintf(path, sizeof(path), "%s/minfree", savedir);
649 if ((fp = fopen(path, "r")) == NULL)
652 if (fgets(mybuf, sizeof(mybuf), fp) == NULL)
655 minfree = atoi(mybuf);
659 needed = (dumpsize + kernelsize) / 1024;
660 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
662 "no dump, not enough free space on device (%lld available, need %lld)",
663 (long long)(minfree > 0 ? spacefree : totfree),
667 if (spacefree - needed < 0)
669 "dump performed, but free space threshold crossed");
674 Open(const char *name, int rw)
678 if ((fd = open(name, rw, 0)) < 0) {
679 syslog(LOG_ERR, "%s: %m", name);
686 Read(int fd, void *bp, int size)
690 nr = read(fd, bp, size);
692 syslog(LOG_ERR, "read: %m");
699 Lseek(int fd, off_t off, int flag)
703 ret = lseek(fd, off, flag);
705 syslog(LOG_ERR, "lseek: %m");
711 * DumpWrite and DumpRead block io requests to the * dump device.
713 #define DUMPBUFSIZE 8192
715 DumpWrite(int fd, void *bp, int size, off_t off, int flag)
717 unsigned char mybuf[DUMPBUFSIZE], *p, *q;
722 syslog(LOG_ERR, "lseek: not LSET");
727 pos = off & ~(DUMPBUFSIZE - 1);
728 Lseek(fd, pos, flag);
729 Read(fd, mybuf, sizeof(mybuf));
730 j = off & (DUMPBUFSIZE - 1);
733 if (i > DUMPBUFSIZE - j)
736 Lseek(fd, pos, flag);
737 Write(fd, mybuf, sizeof(mybuf));
745 DumpRead(int fd, void *bp, int size, off_t off, int flag)
747 unsigned char mybuf[DUMPBUFSIZE], *p, *q;
752 syslog(LOG_ERR, "lseek: not LSET");
757 pos = off & ~(DUMPBUFSIZE - 1);
758 Lseek(fd, pos, flag);
759 Read(fd, mybuf, sizeof(mybuf));
760 j = off & (DUMPBUFSIZE - 1);
763 if (i > DUMPBUFSIZE - j)
773 Write(int fd, void *bp, int size)
777 if ((n = write(fd, bp, size)) < size) {
778 syslog(LOG_ERR, "write: %m");
784 kdumplo_adjust(char *cp, int kmem, u_int64_t *kdumplop)
786 uint64_t AllowMem, sanity, Maxmem, CurrMaxmem;
789 /* based on getmemsize() in i386/i386/machdep.c */
790 sanity = AllowMem = strtouq(cp, &ep, 0);
791 if ((ep != cp) && (*ep != 0)) {
806 if (AllowMem < sanity)
810 errx(1, "invalid memory size: '%s'\n", cp);
812 Maxmem = atop(AllowMem);
814 Lseek(kmem, (off_t)current_nl[X_MAXMEM].n_value, L_SET);
815 Read(kmem, &CurrMaxmem, sizeof(CurrMaxmem));
817 /* based on setdumpdev() in kern_shutdown.c */
818 *kdumplop += (u_int64_t)CurrMaxmem * PAGE_SIZE / DEV_BSIZE;
819 *kdumplop -= (u_int64_t)Maxmem * PAGE_SIZE / DEV_BSIZE;
826 "usage: savecore [-cfkvz] [-N system] [-P physmem|-B blkno] directory");