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