xref: /freebsd/usr.bin/gcore/gcore.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1992, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31a5bf6586SPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
36a5bf6586SPhilippe Charnier #if 0
3764567a41SDavid E. O'Brien #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
3964567a41SDavid E. O'Brien #endif /* not lint */
40a5bf6586SPhilippe Charnier #endif
415bf3fd2bSPhilippe Charnier #include <sys/cdefs.h>
425bf3fd2bSPhilippe Charnier __FBSDID("$FreeBSD$");
435bf3fd2bSPhilippe Charnier 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * Originally written by Eric Cooper in Fall 1981.
469b50d902SRodney W. Grimes  * Inspired by a version 6 program by Len Levin, 1978.
479b50d902SRodney W. Grimes  * Several pieces of code lifted from Bill Joy's 4BSD ps.
489b50d902SRodney W. Grimes  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
499b50d902SRodney W. Grimes  * Lawrence Berkeley Laboratory.
509b50d902SRodney W. Grimes  *
519b50d902SRodney W. Grimes  * Portions of this software were developed by the Computer Systems
529b50d902SRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA
539b50d902SRodney W. Grimes  * contract BG 91-66 and contributed to Berkeley.
549b50d902SRodney W. Grimes  */
555bf3fd2bSPhilippe Charnier 
569b50d902SRodney W. Grimes #include <sys/param.h>
579b50d902SRodney W. Grimes #include <sys/time.h>
589b50d902SRodney W. Grimes #include <sys/stat.h>
59e0491636SPeter Wemm #include <sys/linker_set.h>
602e7ecbfbSAttilio Rao #include <sys/sysctl.h>
619b50d902SRodney W. Grimes 
62a5bf6586SPhilippe Charnier #include <err.h>
639b50d902SRodney W. Grimes #include <fcntl.h>
649b50d902SRodney W. Grimes #include <stdio.h>
659b50d902SRodney W. Grimes #include <stdlib.h>
66821df508SXin LI #include <string.h>
679b50d902SRodney W. Grimes #include <unistd.h>
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes #include "extern.h"
701d73ef97SAttilio Rao int pflags;
719b50d902SRodney W. Grimes 
72f1bb2cd2SWarner Losh static void	killed(int);
73f1bb2cd2SWarner Losh static void	usage(void) __dead2;
749b50d902SRodney W. Grimes 
75283c2d5aSJohn Polstra static pid_t pid;
769b50d902SRodney W. Grimes 
77e0491636SPeter Wemm SET_DECLARE(dumpset, struct dumpers);
78e0491636SPeter Wemm 
799b50d902SRodney W. Grimes int
80e0491636SPeter Wemm main(int argc, char *argv[])
819b50d902SRodney W. Grimes {
82adaa8f14SMatt Jacob 	int ch, efd, fd, name[4];
8349ee7af6SDag-Erling Smørgrav 	char *binfile, *corefile;
842e7ecbfbSAttilio Rao 	char passpath[MAXPATHLEN], fname[MAXPATHLEN];
85e0491636SPeter Wemm 	struct dumpers **d, *dumper;
862e7ecbfbSAttilio Rao 	size_t len;
879b50d902SRodney W. Grimes 
881d73ef97SAttilio Rao 	pflags = 0;
899b50d902SRodney W. Grimes 	corefile = NULL;
900691d32bSMark Johnston         while ((ch = getopt(argc, argv, "c:f")) != -1) {
919b50d902SRodney W. Grimes                 switch (ch) {
929b50d902SRodney W. Grimes                 case 'c':
939b50d902SRodney W. Grimes 			corefile = optarg;
949b50d902SRodney W. Grimes                         break;
951d73ef97SAttilio Rao 		case 'f':
961d73ef97SAttilio Rao 			pflags |= PFLAGS_FULL;
971d73ef97SAttilio Rao 			break;
989b50d902SRodney W. Grimes 		default:
999b50d902SRodney W. Grimes 			usage();
1009b50d902SRodney W. Grimes 			break;
1019b50d902SRodney W. Grimes 		}
1029b50d902SRodney W. Grimes 	}
1039b50d902SRodney W. Grimes 	argv += optind;
1049b50d902SRodney W. Grimes 	argc -= optind;
10549ee7af6SDag-Erling Smørgrav 	/* XXX we should check that the pid argument is really a number */
10649ee7af6SDag-Erling Smørgrav 	switch (argc) {
10749ee7af6SDag-Erling Smørgrav 	case 1:
10849ee7af6SDag-Erling Smørgrav 		pid = atoi(argv[0]);
1092e7ecbfbSAttilio Rao 		name[0] = CTL_KERN;
1102e7ecbfbSAttilio Rao 		name[1] = KERN_PROC;
1112e7ecbfbSAttilio Rao 		name[2] = KERN_PROC_PATHNAME;
1122e7ecbfbSAttilio Rao 		name[3] = pid;
1132e7ecbfbSAttilio Rao 		len = sizeof(passpath);
1142e7ecbfbSAttilio Rao 		if (sysctl(name, 4, passpath, &len, NULL, 0) == -1)
1152e7ecbfbSAttilio Rao 			errx(1, "kern.proc.pathname failure");
1162e7ecbfbSAttilio Rao 		binfile = passpath;
11749ee7af6SDag-Erling Smørgrav 		break;
11849ee7af6SDag-Erling Smørgrav 	case 2:
11949ee7af6SDag-Erling Smørgrav 		pid = atoi(argv[1]);
12049ee7af6SDag-Erling Smørgrav 		binfile = argv[0];
12149ee7af6SDag-Erling Smørgrav 		break;
12249ee7af6SDag-Erling Smørgrav 	default:
1239b50d902SRodney W. Grimes 		usage();
12449ee7af6SDag-Erling Smørgrav 	}
12552e7cc0aSJohn Polstra 	efd = open(binfile, O_RDONLY, 0);
12652e7cc0aSJohn Polstra 	if (efd < 0)
12752e7cc0aSJohn Polstra 		err(1, "%s", binfile);
128e0491636SPeter Wemm 	dumper = NULL;
129e0491636SPeter Wemm 	SET_FOREACH(d, dumpset) {
130e0491636SPeter Wemm 		lseek(efd, 0, SEEK_SET);
131e0491636SPeter Wemm 		if (((*d)->ident)(efd, pid, binfile)) {
132e0491636SPeter Wemm 			dumper = (*d);
133e0491636SPeter Wemm 			lseek(efd, 0, SEEK_SET);
134e0491636SPeter Wemm 			break;
135e0491636SPeter Wemm 		}
136e0491636SPeter Wemm 	}
137e0491636SPeter Wemm 	if (dumper == NULL)
13852e7cc0aSJohn Polstra 		errx(1, "Invalid executable file");
1399b50d902SRodney W. Grimes 	if (corefile == NULL) {
1409b50d902SRodney W. Grimes 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
1419b50d902SRodney W. Grimes 		corefile = fname;
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
1449b50d902SRodney W. Grimes 	if (fd < 0)
145a5bf6586SPhilippe Charnier 		err(1, "%s", corefile);
146adaa8f14SMatt Jacob 
147e0491636SPeter Wemm 	dumper->dump(efd, fd, pid);
1489b50d902SRodney W. Grimes 	(void)close(fd);
149e0491636SPeter Wemm 	(void)close(efd);
1509b50d902SRodney W. Grimes 	exit(0);
1519b50d902SRodney W. Grimes }
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes void
154e0491636SPeter Wemm usage(void)
1559b50d902SRodney W. Grimes {
1569b50d902SRodney W. Grimes 
1579952941dSBryan Drewery 	(void)fprintf(stderr, "usage: gcore [-c core] [executable] pid\n");
1589b50d902SRodney W. Grimes 	exit(1);
1599b50d902SRodney W. Grimes }
160