gcore.c revision 102955
135124Sjb/*-
235124Sjb * Copyright (c) 1992, 1993
335124Sjb *	The Regents of the University of California.  All rights reserved.
435124Sjb *
535124Sjb * Redistribution and use in source and binary forms, with or without
635124Sjb * modification, are permitted provided that the following conditions
735124Sjb * are met:
835124Sjb * 1. Redistributions of source code must retain the above copyright
935124Sjb *    notice, this list of conditions and the following disclaimer.
1035124Sjb * 2. Redistributions in binary form must reproduce the above copyright
1135124Sjb *    notice, this list of conditions and the following disclaimer in the
1235124Sjb *    documentation and/or other materials provided with the distribution.
13165968Simp * 3. All advertising materials mentioning features or use of this software
1435124Sjb *    must display the following acknowledgement:
1535124Sjb *	This product includes software developed by the University of
1635124Sjb *	California, Berkeley and its contributors.
1735124Sjb * 4. Neither the name of the University nor the names of its contributors
1835124Sjb *    may be used to endorse or promote products derived from this software
1935124Sjb *    without specific prior written permission.
2035124Sjb *
2135124Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2235124Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2335124Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2435124Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2535124Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2635124Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2735124Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2835124Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2950476Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3035124Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3135124Sjb * SUCH DAMAGE.
3235124Sjb */
3335124Sjb
3435124Sjb#ifndef lint
3535124Sjbstatic const char copyright[] =
3635124Sjb"@(#) Copyright (c) 1992, 1993\n\
37199606Sjhb	The Regents of the University of California.  All rights reserved.\n";
3835124Sjb#endif /* not lint */
3935124Sjb
4035124Sjb#if 0
4135124Sjb#ifndef lint
4235124Sjbstatic char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
4335124Sjb#endif /* not lint */
4435124Sjb#endif
4535124Sjb#include <sys/cdefs.h>
4635124Sjb__FBSDID("$FreeBSD: head/usr.bin/gcore/gcore.c 102944 2002-09-04 23:29:10Z dwmalone $");
4735124Sjb
4835124Sjb/*
4935124Sjb * Originally written by Eric Cooper in Fall 1981.
5035124Sjb * Inspired by a version 6 program by Len Levin, 1978.
5135124Sjb * Several pieces of code lifted from Bill Joy's 4BSD ps.
5235124Sjb * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
5335124Sjb * Lawrence Berkeley Laboratory.
5471579Sdeischen *
5535124Sjb * Portions of this software were developed by the Computer Systems
5635124Sjb * Engineering group at Lawrence Berkeley Laboratory under DARPA
5735124Sjb * contract BG 91-66 and contributed to Berkeley.
5835124Sjb */
5935124Sjb
6035124Sjb#include <sys/param.h>
6135124Sjb#include <sys/time.h>
6271579Sdeischen#include <sys/stat.h>
6335124Sjb#include <sys/proc.h>
6493399Smarkm#include <sys/user.h>
65106866Sdeischen#include <sys/sysctl.h>
66106866Sdeischen
67106866Sdeischen#include <arpa/inet.h>
68106866Sdeischen#include <machine/elf.h>
69106866Sdeischen#include <machine/vmparam.h>
70106866Sdeischen
71156319Sdeischen#include <a.out.h>
72156319Sdeischen#include <err.h>
73156319Sdeischen#include <fcntl.h>
74156319Sdeischen#include <kvm.h>
75156319Sdeischen#include <limits.h>
76156319Sdeischen#include <signal.h>
77156319Sdeischen#include <stdio.h>
78156319Sdeischen#include <stdlib.h>
79156319Sdeischen#include <string.h>
80156319Sdeischen#include <unistd.h>
81156319Sdeischen
82156319Sdeischen#include "extern.h"
83156319Sdeischen
84156319Sdeischenstatic void	core(int, int, struct kinfo_proc *);
85156319Sdeischenstatic void	datadump(int, int, struct kinfo_proc *, u_long, int);
86156319Sdeischenstatic void	killed(int);
87156319Sdeischenstatic void	restart_target(void);
88156319Sdeischenstatic void	usage(void) __dead2;
89156319Sdeischenstatic void	userdump(int, struct kinfo_proc *, u_long, int);
90156319Sdeischen
91156319Sdeischenkvm_t *kd;
92156319Sdeischen
93106866Sdeischenstatic int data_offset;
94106866Sdeischenstatic pid_t pid;
95106866Sdeischen
96106866Sdeischenint
97156319Sdeischenmain(argc, argv)
98106866Sdeischen	int argc;
99156319Sdeischen	char *argv[];
100156319Sdeischen{
101156319Sdeischen	struct kinfo_proc *ki = NULL;
102106866Sdeischen	struct exec exec;
103156319Sdeischen	int ch, cnt, efd, fd, sflag;
104106866Sdeischen	uid_t uid;
105106866Sdeischen	char *binfile, *corefile;
106156319Sdeischen	char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN];
107106866Sdeischen	int is_aout;
108156319Sdeischen
109156319Sdeischen	sflag = 0;
110156319Sdeischen	corefile = NULL;
111106866Sdeischen        while ((ch = getopt(argc, argv, "c:s")) != -1) {
112106866Sdeischen                switch (ch) {
113106866Sdeischen                case 'c':
114106866Sdeischen			corefile = optarg;
115106866Sdeischen                        break;
116106866Sdeischen		case 's':
117106866Sdeischen			sflag = 1;
118106866Sdeischen			break;
119106866Sdeischen		default:
120106866Sdeischen			usage();
121106866Sdeischen			break;
122106866Sdeischen		}
123106866Sdeischen	}
124106866Sdeischen	argv += optind;
125156319Sdeischen	argc -= optind;
126156319Sdeischen
127106866Sdeischen	/* XXX we should check that the pid argument is really a number */
128106866Sdeischen	switch (argc) {
129156319Sdeischen	case 1:
130201546Sdavidxu		pid = atoi(argv[0]);
131201546Sdavidxu		asprintf(&binfile, "/proc/%d/file", pid);
132213153Sdavidxu		if (binfile == NULL)
133213153Sdavidxu			errx(1, "allocation failure");
134106866Sdeischen		break;
135106866Sdeischen	case 2:
136106866Sdeischen		pid = atoi(argv[1]);
137106870Sdeischen		binfile = argv[0];
138106880Sdeischen		break;
139106866Sdeischen	default:
140106880Sdeischen		usage();
141106866Sdeischen	}
142106866Sdeischen
143111618Snectar	efd = open(binfile, O_RDONLY, 0);
144111618Snectar	if (efd < 0)
145111618Snectar		err(1, "%s", binfile);
146111618Snectar
147111618Snectar	cnt = read(efd, &exec, sizeof(exec));
148111618Snectar	if (cnt != sizeof(exec))
149133754Sdfr		errx(1, "%s exec header: %s",
150133754Sdfr		    binfile, cnt > 0 ? strerror(EIO) : strerror(errno));
151133754Sdfr	if (!N_BADMAG(exec)) {
152133754Sdfr		is_aout = 1;
153111618Snectar		/*
154111618Snectar		 * This legacy a.out support uses the kvm interface instead
155199606Sjhb		 * of procfs.
156199606Sjhb		 */
157199606Sjhb		kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
158199606Sjhb		if (kd == NULL)
159199606Sjhb			errx(1, "%s", errbuf);
160199606Sjhb
161133754Sdfr		uid = getuid();
162133754Sdfr
163133754Sdfr		ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
164133754Sdfr		if (ki == NULL || cnt != 1)
165133754Sdfr			errx(1, "%d: not found", pid);
16693399Smarkm
16793399Smarkm		if (ki->ki_ruid != uid && uid != 0)
16893399Smarkm			errx(1, "%d: not owner", pid);
16993399Smarkm
17093399Smarkm		if (ki->ki_stat == SZOMB)
171122069Sdeischen			errx(1, "%d: zombie", pid);
172182225Sjasone
173182225Sjasone		if (ki->ki_flag & P_WEXIT)
174182225Sjasone			errx(1, "%d: process exiting", pid);
175182225Sjasone		if (ki->ki_flag & P_SYSTEM)	/* Swapper or pagedaemon. */
176182225Sjasone			errx(1, "%d: system process", pid);
177182225Sjasone		if (exec.a_text != ptoa(ki->ki_tsize))
178154248Sjasone			errx(1, "The executable %s does not belong to"
179154248Sjasone			    " process %d!\n"
180122069Sdeischen			    "Text segment size (in bytes): executable %ld,"
181154248Sjasone			    " process %d", binfile, pid, exec.a_text,
182154248Sjasone			     ptoa(ki->ki_tsize));
183122069Sdeischen		data_offset = N_DATOFF(exec);
184150040Sstefanf	} else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
185150040Sstefanf		is_aout = 0;
186150040Sstefanf		close(efd);
187150040Sstefanf	} else
188150040Sstefanf		errx(1, "Invalid executable file");
189171219Speter
190171219Speter	if (corefile == NULL) {
191171219Speter		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
192171219Speter		corefile = fname;
193171219Speter	}
194171219Speter	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
195171219Speter	if (fd < 0)
196171219Speter		err(1, "%s", corefile);
197171219Speter
198171219Speter	if (sflag) {
199171219Speter		signal(SIGHUP, killed);
200171219Speter		signal(SIGINT, killed);
201171219Speter		signal(SIGTERM, killed);
202171219Speter		if (kill(pid, SIGSTOP) == -1)
203171219Speter			err(1, "%d: stop signal", pid);
204171219Speter		atexit(restart_target);
205171219Speter	}
206171219Speter
207171219Speter	if (is_aout)
208171219Speter		core(efd, fd, ki);
209171219Speter	else
210171219Speter		elf_coredump(fd, pid);
211171219Speter
212177911Sdfr	(void)close(fd);
213177911Sdfr	exit(0);
214177911Sdfr}
215179947Sed
216179947Sed/*
217179947Sed * core --
218211416Skib *	Build the core file.
219211706Skib */
220211706Skibvoid
221211416Skibcore(efd, fd, ki)
222213153Sdavidxu	int efd;
223213153Sdavidxu	int fd;
224213153Sdavidxu	struct kinfo_proc *ki;
22535124Sjb{
226	union {
227		struct user user;
228		struct {
229			char uabytes[ctob(UAREA_PAGES)];
230			char ksbytes[ctob(KSTACK_PAGES)];
231		} bytes;
232	} uarea;
233	int tsize = ki->ki_tsize;
234	int dsize = ki->ki_dsize;
235	int ssize = ki->ki_ssize;
236	int cnt;
237
238	/* Read in user struct */
239	cnt = kvm_read(kd, (u_long)ki->ki_addr, uarea.bytes.uabytes,
240	    ctob(UAREA_PAGES));
241	if (cnt != ctob(UAREA_PAGES))
242		errx(1, "read upages structure: %s",
243		    cnt > 0 ? strerror(EIO) : strerror(errno));
244
245	cnt = kvm_read(kd, (u_long)ki->ki_kstack, uarea.bytes.ksbytes,
246	    ctob(KSTACK_PAGES));
247	if (cnt != ctob(KSTACK_PAGES))
248		errx(1, "read kstack structure: %s",
249		    cnt > 0 ? strerror(EIO) : strerror(errno));
250
251	/*
252	 * Fill in the eproc vm parameters, since these are garbage unless
253	 * the kernel is dumping core or something.
254	 */
255	uarea.user.u_kproc = *ki;
256
257	/* Dump user area */
258	cnt = write(fd, &uarea, sizeof(uarea));
259	if (cnt != sizeof(uarea))
260		errx(1, "write user structure: %s",
261		    cnt > 0 ? strerror(EIO) : strerror(errno));
262
263	/* Dump data segment */
264	datadump(efd, fd, ki, USRTEXT + ctob(tsize), dsize);
265
266	/* Dump stack segment */
267	userdump(fd, ki, USRSTACK - ctob(ssize), ssize);
268
269	/* Dump machine dependent portions of the core. */
270	md_core(kd, fd, ki);
271}
272
273void
274datadump(efd, fd, kp, addr, npage)
275	register int efd;
276	register int fd;
277	struct kinfo_proc *kp;
278	register u_long addr;
279	register int npage;
280{
281	register int cc, delta;
282	char buffer[PAGE_SIZE];
283
284	delta = data_offset - addr;
285	while (--npage >= 0) {
286		cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
287		if (cc != PAGE_SIZE) {
288			/* Try to read the page from the executable. */
289			if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
290				err(1, "seek executable");
291			cc = read(efd, buffer, sizeof(buffer));
292			if (cc != sizeof(buffer)) {
293				if (cc < 0)
294					err(1, "read executable");
295				else	/* Assume untouched bss page. */
296					bzero(buffer, sizeof(buffer));
297			}
298		}
299		cc = write(fd, buffer, PAGE_SIZE);
300		if (cc != PAGE_SIZE)
301			errx(1, "write data segment: %s",
302			    cc > 0 ? strerror(EIO) : strerror(errno));
303		addr += PAGE_SIZE;
304	}
305}
306
307static void
308killed(sig)
309	int sig;
310{
311	restart_target();
312	signal(sig, SIG_DFL);
313	kill(getpid(), sig);
314}
315
316static void
317restart_target()
318{
319	kill(pid, SIGCONT);
320}
321
322void
323userdump(fd, kp, addr, npage)
324	register int fd;
325	struct kinfo_proc *kp;
326	register u_long addr;
327	register int npage;
328{
329	register int cc;
330	char buffer[PAGE_SIZE];
331
332	while (--npage >= 0) {
333		cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
334		if (cc != PAGE_SIZE)
335			/* Could be an untouched fill-with-zero page. */
336			bzero(buffer, PAGE_SIZE);
337		cc = write(fd, buffer, PAGE_SIZE);
338		if (cc != PAGE_SIZE)
339			errx(1, "write stack segment: %s",
340			    cc > 0 ? strerror(EIO) : strerror(errno));
341		addr += PAGE_SIZE;
342	}
343}
344
345void
346usage()
347{
348	(void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");
349	exit(1);
350}
351