Merge from vendor branch OPENSSH:
[games.git] / usr.bin / gcore / gcore.c
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  * @(#) Copyright (c) 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)gcore.c  8.2 (Berkeley) 9/23/93
35  * $FreeBSD: src/usr.bin/gcore/gcore.c,v 1.15.2.2 2001/08/17 20:56:22 mikeh Exp $
36  * $DragonFly: src/usr.bin/gcore/gcore.c,v 1.7 2005/04/10 20:55:38 drhodus Exp $
37  */
38
39 /*
40  * Originally written by Eric Cooper in Fall 1981.
41  * Inspired by a version 6 program by Len Levin, 1978.
42  * Several pieces of code lifted from Bill Joy's 4BSD ps.
43  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
44  * Lawrence Berkeley Laboratory.
45  *
46  * Portions of this software were developed by the Computer Systems
47  * Engineering group at Lawrence Berkeley Laboratory under DARPA
48  * contract BG 91-66 and contributed to Berkeley.
49  */
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/stat.h>
53 #include <sys/user.h>
54 #include <sys/sysctl.h>
55 #include <machine/elf.h>
56
57 #include <machine/vmparam.h>
58
59 #include <a.out.h>
60 #include <err.h>
61 #include <fcntl.h>
62 #include <kvm.h>
63 #include <limits.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 #include "extern.h"
71
72 static void     core(int, int, struct kinfo_proc *);
73 static void     datadump(int, int, struct proc *, u_long, int);
74 static void     killed(int);
75 static void     restart_target(void);
76 static void     usage(void) __dead2;
77 static void     userdump(int, struct proc *, u_long, int);
78
79 kvm_t *kd;
80 /* XXX undocumented routine, should be in kvm.h? */
81 ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t);
82
83 static int data_offset;
84 static pid_t pid;
85
86 int
87 main(int argc, char **argv)
88 {
89         struct proc *p;
90         struct kinfo_proc *ki = NULL;
91         struct exec exec;
92         int ch, cnt, efd, fd, sflag, uid;
93         char *binfile, *corefile;
94         char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
95         int is_aout;
96
97         sflag = 0;
98         corefile = NULL;
99         while ((ch = getopt(argc, argv, "c:s")) != -1) {
100                 switch (ch) {
101                 case 'c':
102                         corefile = optarg;
103                         break;
104                 case 's':
105                         sflag = 1;
106                         break;
107                 default:
108                         usage();
109                         break;
110                 }
111         }
112         argv += optind;
113         argc -= optind;
114
115         /* XXX we should check that the pid argument is really a number */
116         switch (argc) {
117         case 1:
118                 pid = atoi(argv[0]);
119                 asprintf(&binfile, "/proc/%d/file", pid);
120                 if (binfile == NULL)
121                         errx(1, "allocation failure");
122                 break;
123         case 2:
124                 pid = atoi(argv[1]);
125                 binfile = argv[0];
126                 break;
127         default:
128                 usage();
129         }
130
131         efd = open(binfile, O_RDONLY, 0);
132         if (efd < 0)
133                 err(1, "%s", binfile);
134
135         cnt = read(efd, &exec, sizeof(exec));
136         if (cnt != sizeof(exec))
137                 errx(1, "%s exec header: %s",
138                     binfile, cnt > 0 ? strerror(EIO) : strerror(errno));
139         if (!N_BADMAG(exec)) {
140                 is_aout = 1;
141                 /*
142                  * This legacy a.out support uses the kvm interface instead
143                  * of procfs.
144                  */
145                 kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
146                 if (kd == NULL)
147                         errx(1, "%s", errbuf);
148
149                 uid = getuid();
150
151                 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
152                 if (ki == NULL || cnt != 1)
153                         errx(1, "%d: not found", pid);
154
155                 p = &ki->kp_proc;
156                 if (ki->kp_eproc.e_ucred.cr_ruid != uid && uid != 0)
157                         errx(1, "%d: not owner", pid);
158
159                 if (p->p_stat == SZOMB)
160                         errx(1, "%d: zombie", pid);
161
162                 if (p->p_flag & P_WEXIT)
163                         errx(1, "%d: process exiting", pid);
164                 if (p->p_flag & P_SYSTEM)       /* Swapper or pagedaemon. */
165                         errx(1, "%d: system process", pid);
166                 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize))
167                         errx(1, "The executable %s does not belong to"
168                             " process %d!\n"
169                             "Text segment size (in bytes): executable %ld,"
170                             " process %d", binfile, pid, exec.a_text, 
171                              ptoa(ki->kp_eproc.e_vm.vm_tsize));
172                 data_offset = N_DATOFF(exec);
173         } else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
174                 is_aout = 0;
175                 close(efd);
176         } else
177                 errx(1, "Invalid executable file");
178
179         if (corefile == NULL) {
180                 (void)snprintf(fname, sizeof(fname), "core.%d", pid);
181                 corefile = fname;
182         }
183         fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
184         if (fd < 0)
185                 err(1, "%s", corefile);
186
187         if (sflag) {
188                 signal(SIGHUP, killed);
189                 signal(SIGINT, killed);
190                 signal(SIGTERM, killed);
191                 if (kill(pid, SIGSTOP) == -1)
192                         err(1, "%d: stop signal", pid);
193                 atexit(restart_target);
194         }
195
196         if (is_aout)
197                 core(efd, fd, ki);
198         else
199                 elf_coredump(fd, pid);
200
201         (void)close(fd);
202         exit(0);
203 }
204
205 /*
206  * core --
207  *      Build the core file.
208  */
209 void
210 core(int efd, int fd, struct kinfo_proc *ki)
211 {
212         union {
213                 struct user user;
214                 char ubytes[ctob(UPAGES)];
215         } uarea;
216         struct proc *p = &ki->kp_proc;
217         int tsize = ki->kp_eproc.e_vm.vm_tsize;
218         int dsize = ki->kp_eproc.e_vm.vm_dsize;
219         int ssize = ki->kp_eproc.e_vm.vm_ssize;
220         int cnt;
221
222         /* Read in user struct */
223         cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
224         if (cnt != sizeof(uarea))
225                 errx(1, "read user structure: %s",
226                     cnt > 0 ? strerror(EIO) : strerror(errno));
227
228         /*
229          * Fill in the eproc vm parameters, since these are garbage unless
230          * the kernel is dumping core or something.
231          */
232         uarea.user.u_kproc = *ki;
233
234         /* Dump user area */
235         cnt = write(fd, &uarea, sizeof(uarea));
236         if (cnt != sizeof(uarea))
237                 errx(1, "write user structure: %s",
238                     cnt > 0 ? strerror(EIO) : strerror(errno));
239
240         /* Dump data segment */
241         datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
242
243         /* Dump stack segment */
244         userdump(fd, p, USRSTACK - ctob(ssize), ssize);
245
246         /* Dump machine dependent portions of the core. */
247         md_core(kd, fd, ki);
248 }
249
250 void
251 datadump(int efd, int fd, struct proc *p,
252          u_long addr, int npage)
253 {
254         int cc, delta;
255         char buffer[PAGE_SIZE];
256
257         delta = data_offset - addr;
258         while (--npage >= 0) {
259                 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
260                 if (cc != PAGE_SIZE) {
261                         /* Try to read the page from the executable. */
262                         if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
263                                 err(1, "seek executable: %s", strerror(errno));
264                         cc = read(efd, buffer, sizeof(buffer));
265                         if (cc != sizeof(buffer)) {
266                                 if (cc < 0)
267                                         err(1, "read executable");
268                                 else    /* Assume untouched bss page. */
269                                         bzero(buffer, sizeof(buffer));
270                         }
271                 }
272                 cc = write(fd, buffer, PAGE_SIZE);
273                 if (cc != PAGE_SIZE)
274                         errx(1, "write data segment: %s",
275                             cc > 0 ? strerror(EIO) : strerror(errno));
276                 addr += PAGE_SIZE;
277         }
278 }
279
280 static void
281 killed(int sig)
282 {
283         restart_target();
284         signal(sig, SIG_DFL);
285         kill(getpid(), sig);
286 }
287
288 static void
289 restart_target(void)
290 {
291         kill(pid, SIGCONT);
292 }
293
294 void
295 userdump(int fd, struct proc *p, u_long addr,
296          int npage)
297 {
298         int cc;
299         char buffer[PAGE_SIZE];
300
301         while (--npage >= 0) {
302                 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
303                 if (cc != PAGE_SIZE)
304                         /* Could be an untouched fill-with-zero page. */
305                         bzero(buffer, PAGE_SIZE);
306                 cc = write(fd, buffer, PAGE_SIZE);
307                 if (cc != PAGE_SIZE)
308                         errx(1, "write stack segment: %s",
309                             cc > 0 ? strerror(EIO) : strerror(errno));
310                 addr += PAGE_SIZE;
311         }
312 }
313
314 void
315 usage(void)
316 {
317         (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n");
318         exit(1);
319 }