xref: /freebsd/usr.bin/gcore/gcore.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1992, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  * Originally written by Eric Cooper in Fall 1981.
48  * Inspired by a version 6 program by Len Levin, 1978.
49  * Several pieces of code lifted from Bill Joy's 4BSD ps.
50  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
51  * Lawrence Berkeley Laboratory.
52  *
53  * Portions of this software were developed by the Computer Systems
54  * Engineering group at Lawrence Berkeley Laboratory under DARPA
55  * contract BG 91-66 and contributed to Berkeley.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/ptrace.h>
60 #include <sys/time.h>
61 #include <sys/stat.h>
62 #include <sys/linker_set.h>
63 #include <sys/sysctl.h>
64 #include <sys/wait.h>
65 
66 #include <err.h>
67 #include <fcntl.h>
68 #include <stdbool.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 
74 #include "extern.h"
75 int pflags;
76 
77 static void	killed(int);
78 static void	usage(void) __dead2;
79 
80 static pid_t pid;
81 static bool kflag = false;
82 
83 SET_DECLARE(dumpset, struct dumpers);
84 
85 static int
86 open_corefile(char *corefile)
87 {
88 	char fname[MAXPATHLEN];
89 	int fd;
90 
91 	if (corefile == NULL) {
92 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
93 		corefile = fname;
94 	}
95 	fd = open(corefile, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
96 	if (fd < 0)
97 		err(1, "%s", corefile);
98 	return (fd);
99 }
100 
101 static void
102 kcoredump(int fd, pid_t pid)
103 {
104 	struct ptrace_coredump pc;
105 	int error, res, ret, waited;
106 
107 	error = ptrace(PT_ATTACH, pid, NULL, 0);
108 	if (error != 0)
109 		err(1, "attach");
110 
111 	waited = waitpid(pid, &res, 0);
112 	if (waited == -1)
113 		err(1, "wait for STOP");
114 
115 	ret = 0;
116 	memset(&pc, 0, sizeof(pc));
117 	pc.pc_fd = fd;
118 	pc.pc_flags = (pflags & PFLAGS_FULL) != 0 ? PC_ALL : 0;
119 	error = ptrace(PT_COREDUMP, pid, (void *)&pc, sizeof(pc));
120 	if (error == -1) {
121 		warn("coredump");
122 		ret = 1;
123 	}
124 
125 	waited = waitpid(pid, &res, WNOHANG);
126 	if (waited == -1) {
127 		warn("wait after coredump");
128 		ret = 1;
129 	}
130 
131 	error = ptrace(PT_DETACH, pid, NULL, 0);
132 	if (error == -1) {
133 		warn("detach failed, check process status");
134 		ret = 1;
135 	}
136 
137 	exit(ret);
138 }
139 
140 int
141 main(int argc, char *argv[])
142 {
143 	int ch, efd, fd, name[4];
144 	char *binfile, *corefile;
145 	char passpath[MAXPATHLEN];
146 	struct dumpers **d, *dumper;
147 	size_t len;
148 
149 	pflags = 0;
150 	corefile = NULL;
151         while ((ch = getopt(argc, argv, "c:fk")) != -1) {
152                 switch (ch) {
153                 case 'c':
154 			corefile = optarg;
155                         break;
156 		case 'f':
157 			pflags |= PFLAGS_FULL;
158 			break;
159 		case 'k':
160 			kflag = true;
161 			break;
162 		default:
163 			usage();
164 			break;
165 		}
166 	}
167 	argv += optind;
168 	argc -= optind;
169 
170 	/* XXX we should check that the pid argument is really a number */
171 	switch (argc) {
172 	case 1:
173 		pid = atoi(argv[0]);
174 		break;
175 	case 2:
176 		binfile = argv[0];
177 		pid = atoi(argv[1]);
178 		break;
179 	default:
180 		usage();
181 	}
182 
183 	if (kflag) {
184 		fd = open_corefile(corefile);
185 		kcoredump(fd, pid);
186 	}
187 
188 	if (argc == 1) {
189 		name[0] = CTL_KERN;
190 		name[1] = KERN_PROC;
191 		name[2] = KERN_PROC_PATHNAME;
192 		name[3] = pid;
193 		len = sizeof(passpath);
194 		if (sysctl(name, 4, passpath, &len, NULL, 0) == -1)
195 			errx(1, "kern.proc.pathname failure");
196 		binfile = passpath;
197 	}
198 	efd = open(binfile, O_RDONLY, 0);
199 	if (efd < 0)
200 		err(1, "%s", binfile);
201 	dumper = NULL;
202 	SET_FOREACH(d, dumpset) {
203 		lseek(efd, 0, SEEK_SET);
204 		if (((*d)->ident)(efd, pid, binfile)) {
205 			dumper = (*d);
206 			lseek(efd, 0, SEEK_SET);
207 			break;
208 		}
209 	}
210 	if (dumper == NULL)
211 		errx(1, "Invalid executable file");
212 	fd = open_corefile(corefile);
213 
214 	dumper->dump(efd, fd, pid);
215 	(void)close(fd);
216 	(void)close(efd);
217 	exit(0);
218 }
219 
220 void
221 usage(void)
222 {
223 
224 	(void)fprintf(stderr,
225 	    "usage: gcore [-kf] [-c core] [executable] pid\n");
226 	exit(1);
227 }
228