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