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