xref: /freebsd/usr.bin/gcore/gcore.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
43 #endif
44 static const char rcsid[] =
45 	"$Id: gcore.c,v 1.8 1998/08/24 16:25:30 wosch Exp $";
46 #endif /* not lint */
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 #include <sys/param.h>
60 #include <sys/time.h>
61 #include <sys/stat.h>
62 #include <sys/proc.h>
63 #include <sys/user.h>
64 #include <sys/sysctl.h>
65 
66 #include <machine/vmparam.h>
67 
68 #include <a.out.h>
69 #include <err.h>
70 #include <fcntl.h>
71 #include <kvm.h>
72 #include <limits.h>
73 #include <signal.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78 
79 #include "extern.h"
80 
81 void	core __P((int, int, struct kinfo_proc *));
82 void	datadump __P((int, int, struct proc *, u_long, int));
83 void	usage __P((void));
84 void	userdump __P((int, struct proc *, u_long, int));
85 
86 kvm_t *kd;
87 /* XXX undocumented routine, should be in kvm.h? */
88 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t));
89 
90 
91 static int data_offset;
92 
93 int
94 main(argc, argv)
95 	int argc;
96 	char *argv[];
97 {
98 	register struct proc *p;
99 	struct kinfo_proc *ki;
100 	struct exec exec;
101 	int ch, cnt, efd, fd, pid, sflag, uid;
102 	char *binfile, *corefile;
103 	char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
104 
105 	sflag = 0;
106 	corefile = NULL;
107         while ((ch = getopt(argc, argv, "c:s")) != -1) {
108                 switch (ch) {
109                 case 'c':
110 			corefile = optarg;
111                         break;
112 		case 's':
113 			sflag = 1;
114 			break;
115 		default:
116 			usage();
117 			break;
118 		}
119 	}
120 	argv += optind;
121 	argc -= optind;
122 
123 	/* XXX we should check that the pid argument is really a number */
124 	switch (argc) {
125 	case 1:
126 		pid = atoi(argv[0]);
127 		asprintf(&binfile, "/proc/%d/file", pid);
128 		if (binfile == NULL)
129 			errx(1, "allocation failure");
130 		break;
131 	case 2:
132 		pid = atoi(argv[1]);
133 		binfile = argv[0];
134 		break;
135 	default:
136 		usage();
137 	}
138 
139 	kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
140 	if (kd == NULL)
141 		errx(1, "%s", errbuf);
142 
143 	uid = getuid();
144 
145 	ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
146 	if (ki == NULL || cnt != 1)
147 		errx(1, "%d: not found", pid);
148 
149 	p = &ki->kp_proc;
150 	if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0)
151 		errx(1, "%d: not owner", pid);
152 
153 	if (p->p_stat == SZOMB)
154 		errx(1, "%d: zombie", pid);
155 
156 	if (p->p_flag & P_WEXIT)
157 		errx(1, "%d: process exiting", pid);
158 	if (p->p_flag & P_SYSTEM)	/* Swapper or pagedaemon. */
159 		errx(1, "%d: system process", pid);
160 
161 	if (corefile == NULL) {
162 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
163 		corefile = fname;
164 	}
165 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
166 	if (fd < 0)
167 		err(1, "%s", corefile);
168 
169 	efd = open(binfile, O_RDONLY, 0);
170 	if (efd < 0)
171 		err(1, "%s", binfile);
172 
173 	cnt = read(efd, &exec, sizeof(exec));
174 	if (cnt != sizeof(exec))
175 		errx(1, "%s exec header: %s",
176 		    binfile, cnt > 0 ? strerror(EIO) : strerror(errno));
177 
178 	/* check the text segment size of the executable and the process */
179 	if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize))
180 		errx(1,
181 		     "The executable %s does not belong to process %d!\n"
182 		     "Text segment size (in bytes): executable %d, process %d",
183 		     binfile, pid, exec.a_text,
184 		     ptoa(ki->kp_eproc.e_vm.vm_tsize));
185 
186 	data_offset = N_DATOFF(exec);
187 
188 	if (sflag && kill(pid, SIGSTOP) < 0)
189 		err(1, "%d: stop signal", pid);
190 
191 	core(efd, fd, ki);
192 
193 	if (sflag && kill(pid, SIGCONT) < 0)
194 		err(1, "%d: continue signal", pid);
195 	(void)close(fd);
196 
197 	exit(0);
198 }
199 
200 /*
201  * core --
202  *	Build the core file.
203  */
204 void
205 core(efd, fd, ki)
206 	int efd;
207 	int fd;
208 	struct kinfo_proc *ki;
209 {
210 	union {
211 		struct user user;
212 		char ubytes[ctob(UPAGES)];
213 	} uarea;
214 	struct proc *p = &ki->kp_proc;
215 	int tsize = ki->kp_eproc.e_vm.vm_tsize;
216 	int dsize = ki->kp_eproc.e_vm.vm_dsize;
217 	int ssize = ki->kp_eproc.e_vm.vm_ssize;
218 	int cnt;
219 
220 	/* Read in user struct */
221 	cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
222 	if (cnt != sizeof(uarea))
223 		errx(1, "read user structure: %s",
224 		    cnt > 0 ? strerror(EIO) : strerror(errno));
225 
226 	/*
227 	 * Fill in the eproc vm parameters, since these are garbage unless
228 	 * the kernel is dumping core or something.
229 	 */
230 	uarea.user.u_kproc = *ki;
231 
232 	/* Dump user area */
233 	cnt = write(fd, &uarea, sizeof(uarea));
234 	if (cnt != sizeof(uarea))
235 		errx(1, "write user structure: %s",
236 		    cnt > 0 ? strerror(EIO) : strerror(errno));
237 
238 	/* Dump data segment */
239 	datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
240 
241 	/* Dump stack segment */
242 	userdump(fd, p, USRSTACK - ctob(ssize), ssize);
243 
244 	/* Dump machine dependent portions of the core. */
245 	md_core(kd, fd, ki);
246 }
247 
248 void
249 datadump(efd, fd, p, addr, npage)
250 	register int efd;
251 	register int fd;
252 	struct proc *p;
253 	register u_long addr;
254 	register int npage;
255 {
256 	register int cc, delta;
257 	char buffer[PAGE_SIZE];
258 
259 	delta = data_offset - addr;
260 	while (--npage >= 0) {
261 		cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
262 		if (cc != PAGE_SIZE) {
263 			/* Try to read the page from the executable. */
264 			if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
265 				err(1, "seek executable: %s", strerror(errno));
266 			cc = read(efd, buffer, sizeof(buffer));
267 			if (cc != sizeof(buffer))
268 				if (cc < 0)
269 					err(1, "read executable");
270 				else	/* Assume untouched bss page. */
271 					bzero(buffer, sizeof(buffer));
272 		}
273 		cc = write(fd, buffer, PAGE_SIZE);
274 		if (cc != PAGE_SIZE)
275 			errx(1, "write data segment: %s",
276 			    cc > 0 ? strerror(EIO) : strerror(errno));
277 		addr += PAGE_SIZE;
278 	}
279 }
280 
281 void
282 userdump(fd, p, addr, npage)
283 	register int fd;
284 	struct proc *p;
285 	register u_long addr;
286 	register int npage;
287 {
288 	register int cc;
289 	char buffer[PAGE_SIZE];
290 
291 	while (--npage >= 0) {
292 		cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
293 		if (cc != PAGE_SIZE)
294 			/* Could be an untouched fill-with-zero page. */
295 			bzero(buffer, PAGE_SIZE);
296 		cc = write(fd, buffer, PAGE_SIZE);
297 		if (cc != PAGE_SIZE)
298 			errx(1, "write stack segment: %s",
299 			    cc > 0 ? strerror(EIO) : strerror(errno));
300 		addr += PAGE_SIZE;
301 	}
302 }
303 
304 void
305 usage()
306 {
307 	(void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");
308 	exit(1);
309 }
310