2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93";
44 static const char rcsid[] =
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.
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.
59 #include <sys/param.h>
64 #include <sys/sysctl.h>
66 #include <machine/vmparam.h>
82 static void core __P((int, int, struct kinfo_proc *));
83 static void datadump __P((int, int, struct proc *, u_long, int));
84 static void killed __P((int));
85 static void restart_target __P((void));
86 static void usage __P((void)) __dead2;
87 static void userdump __P((int, struct proc *, u_long, int));
90 /* XXX undocumented routine, should be in kvm.h? */
91 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t));
93 static int data_offset;
101 register struct proc *p;
102 struct kinfo_proc *ki = NULL;
104 int ch, cnt, efd, fd, sflag, uid;
105 char *binfile, *corefile;
106 char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
111 while ((ch = getopt(argc, argv, "c:s")) != -1) {
127 /* XXX we should check that the pid argument is really a number */
131 asprintf(&binfile, "/proc/%d/file", pid);
133 errx(1, "allocation failure");
143 efd = open(binfile, O_RDONLY, 0);
145 err(1, "%s", binfile);
147 cnt = read(efd, &exec, sizeof(exec));
148 if (cnt != sizeof(exec))
149 errx(1, "%s exec header: %s",
150 binfile, cnt > 0 ? strerror(EIO) : strerror(errno));
151 if (!N_BADMAG(exec)) {
154 * This legacy a.out support uses the kvm interface instead
157 kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
159 errx(1, "%s", errbuf);
163 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
164 if (ki == NULL || cnt != 1)
165 errx(1, "%d: not found", pid);
168 if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0)
169 errx(1, "%d: not owner", pid);
171 if (p->p_stat == SZOMB)
172 errx(1, "%d: zombie", pid);
174 if (p->p_flag & P_WEXIT)
175 errx(1, "%d: process exiting", pid);
176 if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */
177 errx(1, "%d: system process", pid);
178 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize))
179 errx(1, "The executable %s does not belong to"
181 "Text segment size (in bytes): executable %ld,"
182 " process %d", binfile, pid, exec.a_text,
183 ptoa(ki->kp_eproc.e_vm.vm_tsize));
184 data_offset = N_DATOFF(exec);
185 } else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
189 errx(1, "Invalid executable file");
191 if (corefile == NULL) {
192 (void)snprintf(fname, sizeof(fname), "core.%d", pid);
195 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
197 err(1, "%s", corefile);
200 signal(SIGHUP, killed);
201 signal(SIGINT, killed);
202 signal(SIGTERM, killed);
203 if (kill(pid, SIGSTOP) == -1)
204 err(1, "%d: stop signal", pid);
205 atexit(restart_target);
211 elf_coredump(fd, pid);
219 * Build the core file.
225 struct kinfo_proc *ki;
229 char ubytes[ctob(UPAGES)];
231 struct proc *p = &ki->kp_proc;
232 int tsize = ki->kp_eproc.e_vm.vm_tsize;
233 int dsize = ki->kp_eproc.e_vm.vm_dsize;
234 int ssize = ki->kp_eproc.e_vm.vm_ssize;
237 /* Read in user struct */
238 cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
239 if (cnt != sizeof(uarea))
240 errx(1, "read user structure: %s",
241 cnt > 0 ? strerror(EIO) : strerror(errno));
244 * Fill in the eproc vm parameters, since these are garbage unless
245 * the kernel is dumping core or something.
247 uarea.user.u_kproc = *ki;
250 cnt = write(fd, &uarea, sizeof(uarea));
251 if (cnt != sizeof(uarea))
252 errx(1, "write user structure: %s",
253 cnt > 0 ? strerror(EIO) : strerror(errno));
255 /* Dump data segment */
256 datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
258 /* Dump stack segment */
259 userdump(fd, p, USRSTACK - ctob(ssize), ssize);
261 /* Dump machine dependent portions of the core. */
266 datadump(efd, fd, p, addr, npage)
270 register u_long addr;
273 register int cc, delta;
274 char buffer[PAGE_SIZE];
276 delta = data_offset - addr;
277 while (--npage >= 0) {
278 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
279 if (cc != PAGE_SIZE) {
280 /* Try to read the page from the executable. */
281 if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
282 err(1, "seek executable: %s", strerror(errno));
283 cc = read(efd, buffer, sizeof(buffer));
284 if (cc != sizeof(buffer)) {
286 err(1, "read executable");
287 else /* Assume untouched bss page. */
288 bzero(buffer, sizeof(buffer));
291 cc = write(fd, buffer, PAGE_SIZE);
293 errx(1, "write data segment: %s",
294 cc > 0 ? strerror(EIO) : strerror(errno));
304 signal(sig, SIG_DFL);
315 userdump(fd, p, addr, npage)
318 register u_long addr;
322 char buffer[PAGE_SIZE];
324 while (--npage >= 0) {
325 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
327 /* Could be an untouched fill-with-zero page. */
328 bzero(buffer, PAGE_SIZE);
329 cc = write(fd, buffer, PAGE_SIZE);
331 errx(1, "write stack segment: %s",
332 cc > 0 ? strerror(EIO) : strerror(errno));
340 (void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");