gcore.c revision 83366
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  "$FreeBSD: head/usr.bin/gcore/gcore.c 83366 2001-09-12 08:38:13Z julian $";
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/lock.h>
61#include <sys/time.h>
62#include <sys/stat.h>
63#include <sys/proc.h>
64#include <sys/user.h>
65#include <sys/sysctl.h>
66
67#include <machine/elf.h>
68#include <machine/vmparam.h>
69
70#include <a.out.h>
71#include <err.h>
72#include <fcntl.h>
73#include <kvm.h>
74#include <limits.h>
75#include <signal.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79#include <unistd.h>
80
81#include "extern.h"
82
83static void	core __P((int, int, struct kinfo_proc *));
84static void	datadump __P((int, int, struct kinfo_proc *, u_long, int));
85static void	killed __P((int));
86static void	restart_target __P((void));
87static void	usage __P((void)) __dead2;
88static void	userdump __P((int, struct kinfo_proc *, u_long, int));
89
90kvm_t *kd;
91
92static int data_offset;
93static pid_t pid;
94
95int
96main(argc, argv)
97	int argc;
98	char *argv[];
99{
100	struct kinfo_proc *ki = NULL;
101	struct exec exec;
102	int ch, cnt, efd, fd, sflag, uid;
103	char *binfile, *corefile;
104	char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN];
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		if (ki->ki_ruid != uid && uid != 0)
166			errx(1, "%d: not owner", pid);
167
168		if (ki->ki_stat == SZOMB)
169			errx(1, "%d: zombie", pid);
170
171		if (ki->ki_flag & P_WEXIT)
172			errx(1, "%d: process exiting", pid);
173		if (ki->ki_flag & P_SYSTEM)	/* Swapper or pagedaemon. */
174			errx(1, "%d: system process", pid);
175		if (exec.a_text != ptoa(ki->ki_tsize))
176			errx(1, "The executable %s does not belong to"
177			    " process %d!\n"
178			    "Text segment size (in bytes): executable %ld,"
179			    " process %d", binfile, pid, exec.a_text,
180			     ptoa(ki->ki_tsize));
181		data_offset = N_DATOFF(exec);
182	} else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
183		is_aout = 0;
184		close(efd);
185	} else
186		errx(1, "Invalid executable file");
187
188	if (corefile == NULL) {
189		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
190		corefile = fname;
191	}
192	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
193	if (fd < 0)
194		err(1, "%s", corefile);
195
196	if (sflag) {
197		signal(SIGHUP, killed);
198		signal(SIGINT, killed);
199		signal(SIGTERM, killed);
200		if (kill(pid, SIGSTOP) == -1)
201			err(1, "%d: stop signal", pid);
202		atexit(restart_target);
203	}
204
205	if (is_aout)
206		core(efd, fd, ki);
207	else
208		elf_coredump(fd, pid);
209
210	(void)close(fd);
211	exit(0);
212}
213
214/*
215 * core --
216 *	Build the core file.
217 */
218void
219core(efd, fd, ki)
220	int efd;
221	int fd;
222	struct kinfo_proc *ki;
223{
224	union {
225		struct user user;
226		struct {
227			char uabytes[ctob(UAREA_PAGES)];
228			char ksbytes[ctob(KSTACK_PAGES)];
229		} bytes;
230	} uarea;
231	int tsize = ki->ki_tsize;
232	int dsize = ki->ki_dsize;
233	int ssize = ki->ki_ssize;
234	int cnt;
235
236	/* Read in user struct */
237	cnt = kvm_read(kd, (u_long)ki->ki_addr, uarea.bytes.uabytes,
238	    ctob(UAREA_PAGES));
239	if (cnt != ctob(UAREA_PAGES))
240		errx(1, "read upages structure: %s",
241		    cnt > 0 ? strerror(EIO) : strerror(errno));
242
243	cnt = kvm_read(kd, (u_long)ki->ki_kstack, uarea.bytes.ksbytes,
244	    ctob(KSTACK_PAGES));
245	if (cnt != ctob(KSTACK_PAGES))
246		errx(1, "read kstack structure: %s",
247		    cnt > 0 ? strerror(EIO) : strerror(errno));
248
249	/*
250	 * Fill in the eproc vm parameters, since these are garbage unless
251	 * the kernel is dumping core or something.
252	 */
253	uarea.user.u_kproc = *ki;
254
255	/* Dump user area */
256	cnt = write(fd, &uarea, sizeof(uarea));
257	if (cnt != sizeof(uarea))
258		errx(1, "write user structure: %s",
259		    cnt > 0 ? strerror(EIO) : strerror(errno));
260
261	/* Dump data segment */
262	datadump(efd, fd, ki, USRTEXT + ctob(tsize), dsize);
263
264	/* Dump stack segment */
265	userdump(fd, ki, USRSTACK - ctob(ssize), ssize);
266
267	/* Dump machine dependent portions of the core. */
268	md_core(kd, fd, ki);
269}
270
271void
272datadump(efd, fd, kp, addr, npage)
273	register int efd;
274	register int fd;
275	struct kinfo_proc *kp;
276	register u_long addr;
277	register int npage;
278{
279	register int cc, delta;
280	char buffer[PAGE_SIZE];
281
282	delta = data_offset - addr;
283	while (--npage >= 0) {
284		cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
285		if (cc != PAGE_SIZE) {
286			/* Try to read the page from the executable. */
287			if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
288				err(1, "seek executable: %s", strerror(errno));
289			cc = read(efd, buffer, sizeof(buffer));
290			if (cc != sizeof(buffer)) {
291				if (cc < 0)
292					err(1, "read executable");
293				else	/* Assume untouched bss page. */
294					bzero(buffer, sizeof(buffer));
295			}
296		}
297		cc = write(fd, buffer, PAGE_SIZE);
298		if (cc != PAGE_SIZE)
299			errx(1, "write data segment: %s",
300			    cc > 0 ? strerror(EIO) : strerror(errno));
301		addr += PAGE_SIZE;
302	}
303}
304
305static void
306killed(sig)
307	int sig;
308{
309	restart_target();
310	signal(sig, SIG_DFL);
311	kill(getpid(), sig);
312}
313
314static void
315restart_target()
316{
317	kill(pid, SIGCONT);
318}
319
320void
321userdump(fd, kp, addr, npage)
322	register int fd;
323	struct kinfo_proc *kp;
324	register u_long addr;
325	register int npage;
326{
327	register int cc;
328	char buffer[PAGE_SIZE];
329
330	while (--npage >= 0) {
331		cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
332		if (cc != PAGE_SIZE)
333			/* Could be an untouched fill-with-zero page. */
334			bzero(buffer, PAGE_SIZE);
335		cc = write(fd, buffer, PAGE_SIZE);
336		if (cc != PAGE_SIZE)
337			errx(1, "write stack segment: %s",
338			    cc > 0 ? strerror(EIO) : strerror(errno));
339		addr += PAGE_SIZE;
340	}
341}
342
343void
344usage()
345{
346	(void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");
347	exit(1);
348}
349