xref: /freebsd/usr.bin/gcore/gcore.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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
35 static 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 static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
42 #endif /* not lint */
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 #include <sys/param.h>
56 #include <sys/time.h>
57 #include <sys/stat.h>
58 #include <sys/proc.h>
59 #include <sys/user.h>
60 #include <sys/sysctl.h>
61 
62 #include <machine/vmparam.h>
63 
64 #include <a.out.h>
65 #include <fcntl.h>
66 #include <kvm.h>
67 #include <limits.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 
74 #include "extern.h"
75 
76 void	core __P((int, int, struct kinfo_proc *));
77 void	datadump __P((int, int, struct proc *, u_long, int));
78 void	usage __P((void));
79 void	userdump __P((int, struct proc *, u_long, int));
80 
81 kvm_t *kd;
82 /* XXX undocumented routine, should be in kvm.h? */
83 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t));
84 
85 
86 static int data_offset;
87 
88 int
89 main(argc, argv)
90 	int argc;
91 	char *argv[];
92 {
93 	register struct proc *p;
94 	struct kinfo_proc *ki;
95 	struct exec exec;
96 	int ch, cnt, efd, fd, pid, sflag, uid;
97 	char *corefile, errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
98 
99 	sflag = 0;
100 	corefile = NULL;
101         while ((ch = getopt(argc, argv, "c:s")) != EOF) {
102                 switch (ch) {
103                 case 'c':
104 			corefile = optarg;
105                         break;
106 		case 's':
107 			sflag = 1;
108 			break;
109 		default:
110 			usage();
111 			break;
112 		}
113 	}
114 	argv += optind;
115 	argc -= optind;
116 
117 	if (argc != 2)
118 		usage();
119 
120 	kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
121 	if (kd == NULL)
122 		err(1, "%s", errbuf);
123 
124 	uid = getuid();
125 	pid = atoi(argv[1]);
126 
127 	ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
128 	if (ki == NULL || cnt != 1)
129 		err(1, "%d: not found", pid);
130 
131 	p = &ki->kp_proc;
132 	if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0)
133 		err(1, "%d: not owner", pid);
134 
135 	if (p->p_stat == SZOMB)
136 		err(1, "%d: zombie", pid);
137 
138 	if (p->p_flag & P_WEXIT)
139 		err(0, "process exiting");
140 	if (p->p_flag & P_SYSTEM)	/* Swapper or pagedaemon. */
141 		err(1, "%d: system process");
142 
143 	if (corefile == NULL) {
144 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
145 		corefile = fname;
146 	}
147 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
148 	if (fd < 0)
149 		err(1, "%s: %s\n", corefile, strerror(errno));
150 
151 	efd = open(argv[0], O_RDONLY, 0);
152 	if (efd < 0)
153 		err(1, "%s: %s\n", argv[0], strerror(errno));
154 
155 	cnt = read(efd, &exec, sizeof(exec));
156 	if (cnt != sizeof(exec))
157 		err(1, "%s exec header: %s",
158 		    argv[0], cnt > 0 ? strerror(EIO) : strerror(errno));
159 
160 	data_offset = N_DATOFF(exec);
161 
162 	if (sflag && kill(pid, SIGSTOP) < 0)
163 		err(0, "%d: stop signal: %s", pid, strerror(errno));
164 
165 	core(efd, fd, ki);
166 
167 	if (sflag && kill(pid, SIGCONT) < 0)
168 		err(0, "%d: continue signal: %s", pid, strerror(errno));
169 	(void)close(fd);
170 
171 	exit(0);
172 }
173 
174 /*
175  * core --
176  *	Build the core file.
177  */
178 void
179 core(efd, fd, ki)
180 	int efd;
181 	int fd;
182 	struct kinfo_proc *ki;
183 {
184 	union {
185 		struct user user;
186 		char ubytes[ctob(UPAGES)];
187 	} uarea;
188 	struct proc *p = &ki->kp_proc;
189 	int tsize = ki->kp_eproc.e_vm.vm_tsize;
190 	int dsize = ki->kp_eproc.e_vm.vm_dsize;
191 	int ssize = ki->kp_eproc.e_vm.vm_ssize;
192 	int cnt;
193 
194 	/* Read in user struct */
195 	cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
196 	if (cnt != sizeof(uarea))
197 		err(1, "read user structure: %s",
198 		    cnt > 0 ? strerror(EIO) : strerror(errno));
199 
200 	/*
201 	 * Fill in the eproc vm parameters, since these are garbage unless
202 	 * the kernel is dumping core or something.
203 	 */
204 	uarea.user.u_kproc = *ki;
205 
206 	/* Dump user area */
207 	cnt = write(fd, &uarea, sizeof(uarea));
208 	if (cnt != sizeof(uarea))
209 		err(1, "write user structure: %s",
210 		    cnt > 0 ? strerror(EIO) : strerror(errno));
211 
212 	/* Dump data segment */
213 	datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
214 
215 	/* Dump stack segment */
216 	userdump(fd, p, USRSTACK - ctob(ssize), ssize);
217 
218 	/* Dump machine dependent portions of the core. */
219 	md_core(kd, fd, ki);
220 }
221 
222 void
223 datadump(efd, fd, p, addr, npage)
224 	register int efd;
225 	register int fd;
226 	struct proc *p;
227 	register u_long addr;
228 	register int npage;
229 {
230 	register int cc, delta;
231 	char buffer[PAGE_SIZE];
232 
233 	delta = data_offset - addr;
234 	while (--npage >= 0) {
235 		cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
236 		if (cc != PAGE_SIZE) {
237 			/* Try to read the page from the executable. */
238 			if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
239 				err(1, "seek executable: %s", strerror(errno));
240 			cc = read(efd, buffer, sizeof(buffer));
241 			if (cc != sizeof(buffer))
242 				if (cc < 0)
243 					err(1, "read executable: %s",
244 					    strerror(errno));
245 				else	/* Assume untouched bss page. */
246 					bzero(buffer, sizeof(buffer));
247 		}
248 		cc = write(fd, buffer, PAGE_SIZE);
249 		if (cc != PAGE_SIZE)
250 			err(1, "write data segment: %s",
251 			    cc > 0 ? strerror(EIO) : strerror(errno));
252 		addr += PAGE_SIZE;
253 	}
254 }
255 
256 void
257 userdump(fd, p, addr, npage)
258 	register int fd;
259 	struct proc *p;
260 	register u_long addr;
261 	register int npage;
262 {
263 	register int cc;
264 	char buffer[PAGE_SIZE];
265 
266 	while (--npage >= 0) {
267 		cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
268 		if (cc != PAGE_SIZE)
269 			/* Could be an untouched fill-with-zero page. */
270 			bzero(buffer, PAGE_SIZE);
271 		cc = write(fd, buffer, PAGE_SIZE);
272 		if (cc != PAGE_SIZE)
273 			err(1, "write stack segment: %s",
274 			    cc > 0 ? strerror(EIO) : strerror(errno));
275 		addr += PAGE_SIZE;
276 	}
277 }
278 
279 void
280 usage()
281 {
282 	(void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");
283 	exit(1);
284 }
285 
286 #if __STDC__
287 #include <stdarg.h>
288 #else
289 #include <varargs.h>
290 #endif
291 
292 void
293 #if __STDC__
294 err(int fatal, const char *fmt, ...)
295 #else
296 err(fatal, fmt, va_alist)
297 	int fatal;
298 	char *fmt;
299         va_dcl
300 #endif
301 {
302 	va_list ap;
303 #if __STDC__
304 	va_start(ap, fmt);
305 #else
306 	va_start(ap);
307 #endif
308 	(void)fprintf(stderr, "gcore: ");
309 	(void)vfprintf(stderr, fmt, ap);
310 	va_end(ap);
311 	(void)fprintf(stderr, "\n");
312 	exit(1);
313 	/* NOTREACHED */
314 }
315