xref: /freebsd/usr.bin/gcore/gcore.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
43 #endif /* not lint */
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 /*
49  * Originally written by Eric Cooper in Fall 1981.
50  * Inspired by a version 6 program by Len Levin, 1978.
51  * Several pieces of code lifted from Bill Joy's 4BSD ps.
52  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
53  * Lawrence Berkeley Laboratory.
54  *
55  * Portions of this software were developed by the Computer Systems
56  * Engineering group at Lawrence Berkeley Laboratory under DARPA
57  * contract BG 91-66 and contributed to Berkeley.
58  */
59 
60 #include <sys/param.h>
61 #include <sys/time.h>
62 #include <sys/stat.h>
63 #include <sys/linker_set.h>
64 #include <sys/sysctl.h>
65 
66 #include <err.h>
67 #include <fcntl.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 
73 #include "extern.h"
74 int pflags;
75 
76 static void	killed(int);
77 static void	usage(void) __dead2;
78 
79 static pid_t pid;
80 
81 SET_DECLARE(dumpset, struct dumpers);
82 
83 int
84 main(int argc, char *argv[])
85 {
86 	int ch, efd, fd, name[4];
87 	char *binfile, *corefile;
88 	char passpath[MAXPATHLEN], fname[MAXPATHLEN];
89 	struct dumpers **d, *dumper;
90 	size_t len;
91 
92 	pflags = 0;
93 	corefile = NULL;
94         while ((ch = getopt(argc, argv, "c:fs")) != -1) {
95                 switch (ch) {
96                 case 'c':
97 			corefile = optarg;
98                         break;
99 		case 'f':
100 			pflags |= PFLAGS_FULL;
101 			break;
102 		case 's':
103 			pflags |= PFLAGS_RESUME;
104 			break;
105 		default:
106 			usage();
107 			break;
108 		}
109 	}
110 	argv += optind;
111 	argc -= optind;
112 	/* XXX we should check that the pid argument is really a number */
113 	switch (argc) {
114 	case 1:
115 		pid = atoi(argv[0]);
116 		name[0] = CTL_KERN;
117 		name[1] = KERN_PROC;
118 		name[2] = KERN_PROC_PATHNAME;
119 		name[3] = pid;
120 		len = sizeof(passpath);
121 		if (sysctl(name, 4, passpath, &len, NULL, 0) == -1)
122 			errx(1, "kern.proc.pathname failure");
123 		binfile = passpath;
124 		break;
125 	case 2:
126 		pid = atoi(argv[1]);
127 		binfile = argv[0];
128 		break;
129 	default:
130 		usage();
131 	}
132 	efd = open(binfile, O_RDONLY, 0);
133 	if (efd < 0)
134 		err(1, "%s", binfile);
135 	dumper = NULL;
136 	SET_FOREACH(d, dumpset) {
137 		lseek(efd, 0, SEEK_SET);
138 		if (((*d)->ident)(efd, pid, binfile)) {
139 			dumper = (*d);
140 			lseek(efd, 0, SEEK_SET);
141 			break;
142 		}
143 	}
144 	if (dumper == NULL)
145 		errx(1, "Invalid executable file");
146 	if (corefile == NULL) {
147 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
148 		corefile = fname;
149 	}
150 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
151 	if (fd < 0)
152 		err(1, "%s", corefile);
153 	/*
154 	 * The semantics of the 's' flag is to stop the target process.
155 	 * Previous versions of gcore would manage this by trapping SIGHUP,
156 	 * SIGINT and SIGTERM (to be passed to the target pid), and then
157 	 * signal the child to stop.
158 	 *
159 	 * However, this messes up if the selected dumper uses ptrace calls
160 	 * that leave the child already stopped. The waitpid call in elfcore
161 	 * never returns.
162 	 *
163 	 * The best thing to do here is to externalize the 's' flag and let
164 	 * each dumper dispose of what that means, if anything. For the elfcore
165 	 * dumper, the 's' flag is a no-op since the ptrace attach stops the
166 	 * process in question already.
167 	 */
168 
169 	dumper->dump(efd, fd, pid);
170 	(void)close(fd);
171 	(void)close(efd);
172 	exit(0);
173 }
174 
175 void
176 usage(void)
177 {
178 
179 	(void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");
180 	exit(1);
181 }
182