Home | History | Annotate | Line # | Download | only in gcore
gcore.c revision 1.1
      1  1.1  tls /*-
      2  1.1  tls  * Copyright (c) 1992, 1993
      3  1.1  tls  *	The Regents of the University of California.  All rights reserved.
      4  1.1  tls  *
      5  1.1  tls  * Redistribution and use in source and binary forms, with or without
      6  1.1  tls  * modification, are permitted provided that the following conditions
      7  1.1  tls  * are met:
      8  1.1  tls  * 1. Redistributions of source code must retain the above copyright
      9  1.1  tls  *    notice, this list of conditions and the following disclaimer.
     10  1.1  tls  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  tls  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  tls  *    documentation and/or other materials provided with the distribution.
     13  1.1  tls  * 3. All advertising materials mentioning features or use of this software
     14  1.1  tls  *    must display the following acknowledgement:
     15  1.1  tls  *	This product includes software developed by the University of
     16  1.1  tls  *	California, Berkeley and its contributors.
     17  1.1  tls  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  tls  *    may be used to endorse or promote products derived from this software
     19  1.1  tls  *    without specific prior written permission.
     20  1.1  tls  *
     21  1.1  tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  tls  * SUCH DAMAGE.
     32  1.1  tls  */
     33  1.1  tls 
     34  1.1  tls #ifndef lint
     35  1.1  tls static char copyright[] =
     36  1.1  tls "@(#) Copyright (c) 1992, 1993\n\
     37  1.1  tls 	The Regents of the University of California.  All rights reserved.\n";
     38  1.1  tls #endif /* not lint */
     39  1.1  tls 
     40  1.1  tls #ifndef lint
     41  1.1  tls static char sccsid[] = "@(#)gcore.c	8.2 (Berkeley) 9/23/93";
     42  1.1  tls #endif /* not lint */
     43  1.1  tls 
     44  1.1  tls /*
     45  1.1  tls  * Originally written by Eric Cooper in Fall 1981.
     46  1.1  tls  * Inspired by a version 6 program by Len Levin, 1978.
     47  1.1  tls  * Several pieces of code lifted from Bill Joy's 4BSD ps.
     48  1.1  tls  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
     49  1.1  tls  * Lawrence Berkeley Laboratory.
     50  1.1  tls  *
     51  1.1  tls  * Portions of this software were developed by the Computer Systems
     52  1.1  tls  * Engineering group at Lawrence Berkeley Laboratory under DARPA
     53  1.1  tls  * contract BG 91-66 and contributed to Berkeley.
     54  1.1  tls  */
     55  1.1  tls #include <sys/param.h>
     56  1.1  tls #include <sys/time.h>
     57  1.1  tls #include <sys/stat.h>
     58  1.1  tls #include <sys/proc.h>
     59  1.1  tls #include <sys/user.h>
     60  1.1  tls #include <sys/sysctl.h>
     61  1.1  tls 
     62  1.1  tls #include <machine/vmparam.h>
     63  1.1  tls 
     64  1.1  tls #include <a.out.h>
     65  1.1  tls #include <fcntl.h>
     66  1.1  tls #include <kvm.h>
     67  1.1  tls #include <limits.h>
     68  1.1  tls #include <signal.h>
     69  1.1  tls #include <stdio.h>
     70  1.1  tls #include <stdlib.h>
     71  1.1  tls #include <string.h>
     72  1.1  tls #include <unistd.h>
     73  1.1  tls 
     74  1.1  tls #include "extern.h"
     75  1.1  tls 
     76  1.1  tls void	core __P((int, int, struct kinfo_proc *));
     77  1.1  tls void	datadump __P((int, int, struct proc *, u_long, int));
     78  1.1  tls void	usage __P((void));
     79  1.1  tls void	userdump __P((int, struct proc *, u_long, int));
     80  1.1  tls 
     81  1.1  tls kvm_t *kd;
     82  1.1  tls /* XXX undocumented routine, should be in kvm.h? */
     83  1.1  tls ssize_t kvm_uread __P((kvm_t *, struct proc *, u_long, char *, size_t));
     84  1.1  tls 
     85  1.1  tls static int data_offset;
     86  1.1  tls 
     87  1.1  tls int
     88  1.1  tls main(argc, argv)
     89  1.1  tls 	int argc;
     90  1.1  tls 	char *argv[];
     91  1.1  tls {
     92  1.1  tls 	register struct proc *p;
     93  1.1  tls 	struct kinfo_proc *ki;
     94  1.1  tls 	struct exec exec;
     95  1.1  tls 	int ch, cnt, efd, fd, pid, sflag, uid;
     96  1.1  tls 	char *corefile, errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
     97  1.1  tls 
     98  1.1  tls 	sflag = 0;
     99  1.1  tls 	corefile = NULL;
    100  1.1  tls         while ((ch = getopt(argc, argv, "c:s")) != EOF) {
    101  1.1  tls                 switch (ch) {
    102  1.1  tls                 case 'c':
    103  1.1  tls 			corefile = optarg;
    104  1.1  tls                         break;
    105  1.1  tls 		case 's':
    106  1.1  tls 			sflag = 1;
    107  1.1  tls 			break;
    108  1.1  tls 		default:
    109  1.1  tls 			usage();
    110  1.1  tls 			break;
    111  1.1  tls 		}
    112  1.1  tls 	}
    113  1.1  tls 	argv += optind;
    114  1.1  tls 	argc -= optind;
    115  1.1  tls 
    116  1.1  tls 	if (argc != 2)
    117  1.1  tls 		usage();
    118  1.1  tls 
    119  1.1  tls 	kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
    120  1.1  tls 	if (kd == NULL)
    121  1.1  tls 		err(1, "%s", errbuf);
    122  1.1  tls 
    123  1.1  tls 	uid = getuid();
    124  1.1  tls 	pid = atoi(argv[1]);
    125  1.1  tls 
    126  1.1  tls 	ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
    127  1.1  tls 	if (ki == NULL || cnt != 1)
    128  1.1  tls 		err(1, "%d: not found", pid);
    129  1.1  tls 
    130  1.1  tls 	p = &ki->kp_proc;
    131  1.1  tls 	if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0)
    132  1.1  tls 		err(1, "%d: not owner", pid);
    133  1.1  tls 
    134  1.1  tls 	if (p->p_stat == SZOMB)
    135  1.1  tls 		err(1, "%d: zombie", pid);
    136  1.1  tls 
    137  1.1  tls 	if (p->p_flag & P_WEXIT)
    138  1.1  tls 		err(0, "process exiting");
    139  1.1  tls 	if (p->p_flag & P_SYSTEM)	/* Swapper or pagedaemon. */
    140  1.1  tls 		err(1, "%d: system process");
    141  1.1  tls 
    142  1.1  tls 	if (corefile == NULL) {
    143  1.1  tls 		(void)snprintf(fname, sizeof(fname), "core.%d", pid);
    144  1.1  tls 		corefile = fname;
    145  1.1  tls 	}
    146  1.1  tls 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
    147  1.1  tls 	if (fd < 0)
    148  1.1  tls 		err(1, "%s: %s\n", corefile, strerror(errno));
    149  1.1  tls 
    150  1.1  tls 	efd = open(argv[0], O_RDONLY, 0);
    151  1.1  tls 	if (efd < 0)
    152  1.1  tls 		err(1, "%s: %s\n", argv[0], strerror(errno));
    153  1.1  tls 
    154  1.1  tls 	cnt = read(efd, &exec, sizeof(exec));
    155  1.1  tls 	if (cnt != sizeof(exec))
    156  1.1  tls 		err(1, "%s exec header: %s",
    157  1.1  tls 		    argv[0], cnt > 0 ? strerror(EIO) : strerror(errno));
    158  1.1  tls 
    159  1.1  tls 	data_offset = N_DATOFF(exec);
    160  1.1  tls 
    161  1.1  tls 	if (sflag && kill(pid, SIGSTOP) < 0)
    162  1.1  tls 		err(0, "%d: stop signal: %s", pid, strerror(errno));
    163  1.1  tls 
    164  1.1  tls 	core(efd, fd, ki);
    165  1.1  tls 
    166  1.1  tls 	if (sflag && kill(pid, SIGCONT) < 0)
    167  1.1  tls 		err(0, "%d: continue signal: %s", pid, strerror(errno));
    168  1.1  tls 	(void)close(fd);
    169  1.1  tls 
    170  1.1  tls 	exit(0);
    171  1.1  tls }
    172  1.1  tls 
    173  1.1  tls /*
    174  1.1  tls  * core --
    175  1.1  tls  *	Build the core file.
    176  1.1  tls  */
    177  1.1  tls void
    178  1.1  tls core(efd, fd, ki)
    179  1.1  tls 	int efd;
    180  1.1  tls 	int fd;
    181  1.1  tls 	struct kinfo_proc *ki;
    182  1.1  tls {
    183  1.1  tls 	union {
    184  1.1  tls 		struct user user;
    185  1.1  tls 		char ubytes[ctob(UPAGES)];
    186  1.1  tls 	} uarea;
    187  1.1  tls 	struct proc *p = &ki->kp_proc;
    188  1.1  tls 	int tsize = ki->kp_eproc.e_vm.vm_tsize;
    189  1.1  tls 	int dsize = ki->kp_eproc.e_vm.vm_dsize;
    190  1.1  tls 	int ssize = ki->kp_eproc.e_vm.vm_ssize;
    191  1.1  tls 	int cnt;
    192  1.1  tls 
    193  1.1  tls 	/* Read in user struct */
    194  1.1  tls 	cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
    195  1.1  tls 	if (cnt != sizeof(uarea))
    196  1.1  tls 		err(1, "read user structure: %s",
    197  1.1  tls 		    cnt > 0 ? strerror(EIO) : strerror(errno));
    198  1.1  tls 
    199  1.1  tls 	/*
    200  1.1  tls 	 * Fill in the eproc vm parameters, since these are garbage unless
    201  1.1  tls 	 * the kernel is dumping core or something.
    202  1.1  tls 	 */
    203  1.1  tls 	uarea.user.u_kproc = *ki;
    204  1.1  tls 
    205  1.1  tls 	/* Dump user area */
    206  1.1  tls 	cnt = write(fd, &uarea, sizeof(uarea));
    207  1.1  tls 	if (cnt != sizeof(uarea))
    208  1.1  tls 		err(1, "write user structure: %s",
    209  1.1  tls 		    cnt > 0 ? strerror(EIO) : strerror(errno));
    210  1.1  tls 
    211  1.1  tls 	/* Dump data segment */
    212  1.1  tls 	datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
    213  1.1  tls 
    214  1.1  tls 	/* Dump stack segment */
    215  1.1  tls 	userdump(fd, p, USRSTACK - ctob(ssize), ssize);
    216  1.1  tls 
    217  1.1  tls 	/* Dump machine dependent portions of the core. */
    218  1.1  tls 	md_core(kd, fd, ki);
    219  1.1  tls }
    220  1.1  tls 
    221  1.1  tls void
    222  1.1  tls datadump(efd, fd, p, addr, npage)
    223  1.1  tls 	register int efd;
    224  1.1  tls 	register int fd;
    225  1.1  tls 	struct proc *p;
    226  1.1  tls 	register u_long addr;
    227  1.1  tls 	register int npage;
    228  1.1  tls {
    229  1.1  tls 	register int cc, delta;
    230  1.1  tls 	char buffer[NBPG];
    231  1.1  tls 
    232  1.1  tls 	delta = data_offset - addr;
    233  1.1  tls 	while (--npage >= 0) {
    234  1.1  tls 		cc = kvm_uread(kd, p, addr, buffer, NBPG);
    235  1.1  tls 		if (cc != NBPG) {
    236  1.1  tls 			/* Try to read the page from the executable. */
    237  1.1  tls 			if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
    238  1.1  tls 				err(1, "seek executable: %s", strerror(errno));
    239  1.1  tls 			cc = read(efd, buffer, sizeof(buffer));
    240  1.1  tls 			if (cc != sizeof(buffer))
    241  1.1  tls 				if (cc < 0)
    242  1.1  tls 					err(1, "read executable: %s",
    243  1.1  tls 					    strerror(errno));
    244  1.1  tls 				else	/* Assume untouched bss page. */
    245  1.1  tls 					bzero(buffer, sizeof(buffer));
    246  1.1  tls 		}
    247  1.1  tls 		cc = write(fd, buffer, NBPG);
    248  1.1  tls 		if (cc != NBPG)
    249  1.1  tls 			err(1, "write data segment: %s",
    250  1.1  tls 			    cc > 0 ? strerror(EIO) : strerror(errno));
    251  1.1  tls 		addr += NBPG;
    252  1.1  tls 	}
    253  1.1  tls }
    254  1.1  tls 
    255  1.1  tls void
    256  1.1  tls userdump(fd, p, addr, npage)
    257  1.1  tls 	register int fd;
    258  1.1  tls 	struct proc *p;
    259  1.1  tls 	register u_long addr;
    260  1.1  tls 	register int npage;
    261  1.1  tls {
    262  1.1  tls 	register int cc;
    263  1.1  tls 	char buffer[NBPG];
    264  1.1  tls 
    265  1.1  tls 	while (--npage >= 0) {
    266  1.1  tls 		cc = kvm_uread(kd, p, addr, buffer, NBPG);
    267  1.1  tls 		if (cc != NBPG)
    268  1.1  tls 			/* Could be an untouched fill-with-zero page. */
    269  1.1  tls 			bzero(buffer, NBPG);
    270  1.1  tls 		cc = write(fd, buffer, NBPG);
    271  1.1  tls 		if (cc != NBPG)
    272  1.1  tls 			err(1, "write stack segment: %s",
    273  1.1  tls 			    cc > 0 ? strerror(EIO) : strerror(errno));
    274  1.1  tls 		addr += NBPG;
    275  1.1  tls 	}
    276  1.1  tls }
    277  1.1  tls 
    278  1.1  tls void
    279  1.1  tls usage()
    280  1.1  tls {
    281  1.1  tls 	(void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");
    282  1.1  tls 	exit(1);
    283  1.1  tls }
    284  1.1  tls 
    285  1.1  tls #if __STDC__
    286  1.1  tls #include <stdarg.h>
    287  1.1  tls #else
    288  1.1  tls #include <varargs.h>
    289  1.1  tls #endif
    290  1.1  tls 
    291  1.1  tls void
    292  1.1  tls #if __STDC__
    293  1.1  tls err(int fatal, const char *fmt, ...)
    294  1.1  tls #else
    295  1.1  tls err(fatal, fmt, va_alist)
    296  1.1  tls 	int fatal;
    297  1.1  tls 	char *fmt;
    298  1.1  tls         va_dcl
    299  1.1  tls #endif
    300  1.1  tls {
    301  1.1  tls 	va_list ap;
    302  1.1  tls #if __STDC__
    303  1.1  tls 	va_start(ap, fmt);
    304  1.1  tls #else
    305  1.1  tls 	va_start(ap);
    306  1.1  tls #endif
    307  1.1  tls 	(void)fprintf(stderr, "gcore: ");
    308  1.1  tls 	(void)vfprintf(stderr, fmt, ap);
    309  1.1  tls 	va_end(ap);
    310  1.1  tls 	(void)fprintf(stderr, "\n");
    311  1.1  tls 	exit(1);
    312  1.1  tls 	/* NOTREACHED */
    313  1.1  tls }
    314