xref: /freebsd/usr.bin/gcore/gcore.c (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1992, 1993\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35 
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 /*
45  * Originally written by Eric Cooper in Fall 1981.
46  * Inspired by a version 6 program by Len Levin, 1978.
47  * Several pieces of code lifted from Bill Joy's 4BSD ps.
48  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
49  * Lawrence Berkeley Laboratory.
50  *
51  * Portions of this software were developed by the Computer Systems
52  * Engineering group at Lawrence Berkeley Laboratory under DARPA
53  * contract BG 91-66 and contributed to Berkeley.
54  */
55 
56 #include <sys/param.h>
57 #include <sys/time.h>
58 #include <sys/stat.h>
59 #include <sys/linker_set.h>
60 #include <sys/sysctl.h>
61 
62 #include <err.h>
63 #include <fcntl.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 
69 #include "extern.h"
70 int pflags;
71 
72 static void	killed(int);
73 static void	usage(void) __dead2;
74 
75 static pid_t pid;
76 
77 SET_DECLARE(dumpset, struct dumpers);
78 
79 int
80 main(int argc, char *argv[])
81 {
82 	int ch, efd, fd, name[4];
83 	char *binfile, *corefile;
84 	char passpath[MAXPATHLEN], fname[MAXPATHLEN];
85 	struct dumpers **d, *dumper;
86 	size_t len;
87 
88 	pflags = 0;
89 	corefile = NULL;
90         while ((ch = getopt(argc, argv, "c:f")) != -1) {
91                 switch (ch) {
92                 case 'c':
93 			corefile = optarg;
94                         break;
95 		case 'f':
96 			pflags |= PFLAGS_FULL;
97 			break;
98 		default:
99 			usage();
100 			break;
101 		}
102 	}
103 	argv += optind;
104 	argc -= optind;
105 	/* XXX we should check that the pid argument is really a number */
106 	switch (argc) {
107 	case 1:
108 		pid = atoi(argv[0]);
109 		name[0] = CTL_KERN;
110 		name[1] = KERN_PROC;
111 		name[2] = KERN_PROC_PATHNAME;
112 		name[3] = pid;
113 		len = sizeof(passpath);
114 		if (sysctl(name, 4, passpath, &len, NULL, 0) == -1)
115 			errx(1, "kern.proc.pathname failure");
116 		binfile = passpath;
117 		break;
118 	case 2:
119 		pid = atoi(argv[1]);
120 		binfile = argv[0];
121 		break;
122 	default:
123 		usage();
124 	}
125 	efd = open(binfile, O_RDONLY, 0);
126 	if (efd < 0)
127 		err(1, "%s", binfile);
128 	dumper = NULL;
129 	SET_FOREACH(d, dumpset) {
130 		lseek(efd, 0, SEEK_SET);
131 		if (((*d)->ident)(efd, pid, binfile)) {
132 			dumper = (*d);
133 			lseek(efd, 0, SEEK_SET);
134 			break;
135 		}
136 	}
137 	if (dumper == NULL)
138 		errx(1, "Invalid executable file");
139 	if (corefile == NULL) {
140 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
141 		corefile = fname;
142 	}
143 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
144 	if (fd < 0)
145 		err(1, "%s", corefile);
146 	/*
147 	 * The semantics of the 's' flag is to stop the target process.
148 	 * Previous versions of gcore would manage this by trapping SIGHUP,
149 	 * SIGINT and SIGTERM (to be passed to the target pid), and then
150 	 * signal the child to stop.
151 	 *
152 	 * However, this messes up if the selected dumper uses ptrace calls
153 	 * that leave the child already stopped. The waitpid call in elfcore
154 	 * never returns.
155 	 *
156 	 * The best thing to do here is to externalize the 's' flag and let
157 	 * each dumper dispose of what that means, if anything. For the elfcore
158 	 * dumper, the 's' flag is a no-op since the ptrace attach stops the
159 	 * process in question already.
160 	 */
161 
162 	dumper->dump(efd, fd, pid);
163 	(void)close(fd);
164 	(void)close(efd);
165 	exit(0);
166 }
167 
168 void
169 usage(void)
170 {
171 
172 	(void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");
173 	exit(1);
174 }
175