19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1992, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35a5bf6586SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 40a5bf6586SPhilippe Charnier #if 0 4164567a41SDavid E. O'Brien #ifndef lint 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93"; 4364567a41SDavid E. O'Brien #endif /* not lint */ 44a5bf6586SPhilippe Charnier #endif 455bf3fd2bSPhilippe Charnier #include <sys/cdefs.h> 465bf3fd2bSPhilippe Charnier __FBSDID("$FreeBSD$"); 475bf3fd2bSPhilippe Charnier 489b50d902SRodney W. Grimes /* 499b50d902SRodney W. Grimes * Originally written by Eric Cooper in Fall 1981. 509b50d902SRodney W. Grimes * Inspired by a version 6 program by Len Levin, 1978. 519b50d902SRodney W. Grimes * Several pieces of code lifted from Bill Joy's 4BSD ps. 529b50d902SRodney W. Grimes * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne, 539b50d902SRodney W. Grimes * Lawrence Berkeley Laboratory. 549b50d902SRodney W. Grimes * 559b50d902SRodney W. Grimes * Portions of this software were developed by the Computer Systems 569b50d902SRodney W. Grimes * Engineering group at Lawrence Berkeley Laboratory under DARPA 579b50d902SRodney W. Grimes * contract BG 91-66 and contributed to Berkeley. 589b50d902SRodney W. Grimes */ 595bf3fd2bSPhilippe Charnier 609b50d902SRodney W. Grimes #include <sys/param.h> 619b50d902SRodney W. Grimes #include <sys/time.h> 629b50d902SRodney W. Grimes #include <sys/stat.h> 63e0491636SPeter Wemm #include <sys/linker_set.h> 642e7ecbfbSAttilio Rao #include <sys/sysctl.h> 659b50d902SRodney W. Grimes 66a5bf6586SPhilippe Charnier #include <err.h> 679b50d902SRodney W. Grimes #include <fcntl.h> 689b50d902SRodney W. Grimes #include <signal.h> 699b50d902SRodney W. Grimes #include <stdio.h> 709b50d902SRodney W. Grimes #include <stdlib.h> 719b50d902SRodney W. Grimes #include <string.h> 729b50d902SRodney W. Grimes #include <unistd.h> 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes #include "extern.h" 759b50d902SRodney W. Grimes 76f1bb2cd2SWarner Losh static void killed(int); 77f1bb2cd2SWarner Losh static void restart_target(void); 78f1bb2cd2SWarner Losh static void usage(void) __dead2; 799b50d902SRodney W. Grimes 80283c2d5aSJohn Polstra static pid_t pid; 819b50d902SRodney W. Grimes 82e0491636SPeter Wemm SET_DECLARE(dumpset, struct dumpers); 83e0491636SPeter Wemm 849b50d902SRodney W. Grimes int 85e0491636SPeter Wemm main(int argc, char *argv[]) 869b50d902SRodney W. Grimes { 872e7ecbfbSAttilio Rao int ch, efd, fd, name[4], sflag; 8849ee7af6SDag-Erling Smørgrav char *binfile, *corefile; 892e7ecbfbSAttilio Rao char passpath[MAXPATHLEN], fname[MAXPATHLEN]; 90e0491636SPeter Wemm struct dumpers **d, *dumper; 912e7ecbfbSAttilio Rao size_t len; 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes sflag = 0; 949b50d902SRodney W. Grimes corefile = NULL; 951c8af878SWarner Losh while ((ch = getopt(argc, argv, "c:s")) != -1) { 969b50d902SRodney W. Grimes switch (ch) { 979b50d902SRodney W. Grimes case 'c': 989b50d902SRodney W. Grimes corefile = optarg; 999b50d902SRodney W. Grimes break; 1009b50d902SRodney W. Grimes case 's': 1019b50d902SRodney W. Grimes sflag = 1; 1029b50d902SRodney W. Grimes break; 1039b50d902SRodney W. Grimes default: 1049b50d902SRodney W. Grimes usage(); 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes } 1079b50d902SRodney W. Grimes } 1089b50d902SRodney W. Grimes argv += optind; 1099b50d902SRodney W. Grimes argc -= optind; 11049ee7af6SDag-Erling Smørgrav /* XXX we should check that the pid argument is really a number */ 11149ee7af6SDag-Erling Smørgrav switch (argc) { 11249ee7af6SDag-Erling Smørgrav case 1: 11349ee7af6SDag-Erling Smørgrav pid = atoi(argv[0]); 1142e7ecbfbSAttilio Rao name[0] = CTL_KERN; 1152e7ecbfbSAttilio Rao name[1] = KERN_PROC; 1162e7ecbfbSAttilio Rao name[2] = KERN_PROC_PATHNAME; 1172e7ecbfbSAttilio Rao name[3] = pid; 1182e7ecbfbSAttilio Rao len = sizeof(passpath); 1192e7ecbfbSAttilio Rao if (sysctl(name, 4, passpath, &len, NULL, 0) == -1) 1202e7ecbfbSAttilio Rao errx(1, "kern.proc.pathname failure"); 1212e7ecbfbSAttilio Rao binfile = passpath; 12249ee7af6SDag-Erling Smørgrav break; 12349ee7af6SDag-Erling Smørgrav case 2: 12449ee7af6SDag-Erling Smørgrav pid = atoi(argv[1]); 12549ee7af6SDag-Erling Smørgrav binfile = argv[0]; 12649ee7af6SDag-Erling Smørgrav break; 12749ee7af6SDag-Erling Smørgrav default: 1289b50d902SRodney W. Grimes usage(); 12949ee7af6SDag-Erling Smørgrav } 13052e7cc0aSJohn Polstra efd = open(binfile, O_RDONLY, 0); 13152e7cc0aSJohn Polstra if (efd < 0) 13252e7cc0aSJohn Polstra err(1, "%s", binfile); 133e0491636SPeter Wemm dumper = NULL; 134e0491636SPeter Wemm SET_FOREACH(d, dumpset) { 135e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 136e0491636SPeter Wemm if (((*d)->ident)(efd, pid, binfile)) { 137e0491636SPeter Wemm dumper = (*d); 138e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 139e0491636SPeter Wemm break; 140e0491636SPeter Wemm } 141e0491636SPeter Wemm } 142e0491636SPeter Wemm if (dumper == NULL) 14352e7cc0aSJohn Polstra errx(1, "Invalid executable file"); 1449b50d902SRodney W. Grimes if (corefile == NULL) { 1459b50d902SRodney W. Grimes (void)snprintf(fname, sizeof(fname), "core.%d", pid); 1469b50d902SRodney W. Grimes corefile = fname; 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 1499b50d902SRodney W. Grimes if (fd < 0) 150a5bf6586SPhilippe Charnier err(1, "%s", corefile); 151283c2d5aSJohn Polstra if (sflag) { 152283c2d5aSJohn Polstra signal(SIGHUP, killed); 153283c2d5aSJohn Polstra signal(SIGINT, killed); 154283c2d5aSJohn Polstra signal(SIGTERM, killed); 155283c2d5aSJohn Polstra if (kill(pid, SIGSTOP) == -1) 156a5bf6586SPhilippe Charnier err(1, "%d: stop signal", pid); 157283c2d5aSJohn Polstra atexit(restart_target); 158283c2d5aSJohn Polstra } 159e0491636SPeter Wemm dumper->dump(efd, fd, pid); 1609b50d902SRodney W. Grimes (void)close(fd); 161e0491636SPeter Wemm (void)close(efd); 1629b50d902SRodney W. Grimes exit(0); 1639b50d902SRodney W. Grimes } 1649b50d902SRodney W. Grimes 165283c2d5aSJohn Polstra static void 166e0491636SPeter Wemm killed(int sig) 167283c2d5aSJohn Polstra { 168e0491636SPeter Wemm 169283c2d5aSJohn Polstra restart_target(); 170283c2d5aSJohn Polstra signal(sig, SIG_DFL); 171283c2d5aSJohn Polstra kill(getpid(), sig); 172283c2d5aSJohn Polstra } 173283c2d5aSJohn Polstra 174283c2d5aSJohn Polstra static void 175e0491636SPeter Wemm restart_target(void) 176283c2d5aSJohn Polstra { 177e0491636SPeter Wemm 178283c2d5aSJohn Polstra kill(pid, SIGCONT); 179283c2d5aSJohn Polstra } 180283c2d5aSJohn Polstra 1819b50d902SRodney W. Grimes void 182e0491636SPeter Wemm usage(void) 1839b50d902SRodney W. Grimes { 1849b50d902SRodney W. Grimes 185327a1d9dSMike Heffner (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n"); 1869b50d902SRodney W. Grimes exit(1); 1879b50d902SRodney W. Grimes } 188