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