2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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.
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.
60 #include <sys/param.h>
63 #include <sys/linker_set.h>
75 static void killed(int);
76 static void restart_target(void);
77 static void usage(void) __dead2;
81 SET_DECLARE(dumpset, struct dumpers);
84 main(int argc, char *argv[])
86 int ch, efd, fd, sflag;
87 char *binfile, *corefile;
88 char fname[MAXPATHLEN];
89 struct dumpers **d, *dumper;
93 while ((ch = getopt(argc, argv, "c:s")) != -1) {
108 /* XXX we should check that the pid argument is really a number */
112 asprintf(&binfile, "/proc/%d/file", pid);
114 errx(1, "allocation failure");
123 efd = open(binfile, O_RDONLY, 0);
125 err(1, "%s", binfile);
127 SET_FOREACH(d, dumpset) {
128 lseek(efd, 0, SEEK_SET);
129 if (((*d)->ident)(efd, pid, binfile)) {
131 lseek(efd, 0, SEEK_SET);
136 errx(1, "Invalid executable file");
137 if (corefile == NULL) {
138 (void)snprintf(fname, sizeof(fname), "core.%d", pid);
141 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
143 err(1, "%s", corefile);
145 signal(SIGHUP, killed);
146 signal(SIGINT, killed);
147 signal(SIGTERM, killed);
148 if (kill(pid, SIGSTOP) == -1)
149 err(1, "%d: stop signal", pid);
150 atexit(restart_target);
152 dumper->dump(efd, fd, pid);
163 signal(sig, SIG_DFL);
178 (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");