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 <stdio.h> 699b50d902SRodney W. Grimes #include <stdlib.h> 70821df508SXin LI #include <string.h> 719b50d902SRodney W. Grimes #include <unistd.h> 729b50d902SRodney W. Grimes 739b50d902SRodney W. Grimes #include "extern.h" 74adaa8f14SMatt Jacob int sflag; 759b50d902SRodney W. Grimes 76f1bb2cd2SWarner Losh static void killed(int); 77f1bb2cd2SWarner Losh static void usage(void) __dead2; 789b50d902SRodney W. Grimes 79283c2d5aSJohn Polstra static pid_t pid; 809b50d902SRodney W. Grimes 81e0491636SPeter Wemm SET_DECLARE(dumpset, struct dumpers); 82e0491636SPeter Wemm 839b50d902SRodney W. Grimes int 84e0491636SPeter Wemm main(int argc, char *argv[]) 859b50d902SRodney W. Grimes { 86adaa8f14SMatt Jacob int ch, efd, fd, name[4]; 8749ee7af6SDag-Erling Smørgrav char *binfile, *corefile; 882e7ecbfbSAttilio Rao char passpath[MAXPATHLEN], fname[MAXPATHLEN]; 89e0491636SPeter Wemm struct dumpers **d, *dumper; 902e7ecbfbSAttilio Rao size_t len; 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes sflag = 0; 939b50d902SRodney W. Grimes corefile = NULL; 941c8af878SWarner Losh while ((ch = getopt(argc, argv, "c:s")) != -1) { 959b50d902SRodney W. Grimes switch (ch) { 969b50d902SRodney W. Grimes case 'c': 979b50d902SRodney W. Grimes corefile = optarg; 989b50d902SRodney W. Grimes break; 999b50d902SRodney W. Grimes case 's': 1009b50d902SRodney W. Grimes sflag = 1; 1019b50d902SRodney W. Grimes break; 1029b50d902SRodney W. Grimes default: 1039b50d902SRodney W. Grimes usage(); 1049b50d902SRodney W. Grimes break; 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes } 1079b50d902SRodney W. Grimes argv += optind; 1089b50d902SRodney W. Grimes argc -= optind; 10949ee7af6SDag-Erling Smørgrav /* XXX we should check that the pid argument is really a number */ 11049ee7af6SDag-Erling Smørgrav switch (argc) { 11149ee7af6SDag-Erling Smørgrav case 1: 11249ee7af6SDag-Erling Smørgrav pid = atoi(argv[0]); 1132e7ecbfbSAttilio Rao name[0] = CTL_KERN; 1142e7ecbfbSAttilio Rao name[1] = KERN_PROC; 1152e7ecbfbSAttilio Rao name[2] = KERN_PROC_PATHNAME; 1162e7ecbfbSAttilio Rao name[3] = pid; 1172e7ecbfbSAttilio Rao len = sizeof(passpath); 1182e7ecbfbSAttilio Rao if (sysctl(name, 4, passpath, &len, NULL, 0) == -1) 1192e7ecbfbSAttilio Rao errx(1, "kern.proc.pathname failure"); 1202e7ecbfbSAttilio Rao binfile = passpath; 12149ee7af6SDag-Erling Smørgrav break; 12249ee7af6SDag-Erling Smørgrav case 2: 12349ee7af6SDag-Erling Smørgrav pid = atoi(argv[1]); 12449ee7af6SDag-Erling Smørgrav binfile = argv[0]; 12549ee7af6SDag-Erling Smørgrav break; 12649ee7af6SDag-Erling Smørgrav default: 1279b50d902SRodney W. Grimes usage(); 12849ee7af6SDag-Erling Smørgrav } 12952e7cc0aSJohn Polstra efd = open(binfile, O_RDONLY, 0); 13052e7cc0aSJohn Polstra if (efd < 0) 13152e7cc0aSJohn Polstra err(1, "%s", binfile); 132e0491636SPeter Wemm dumper = NULL; 133e0491636SPeter Wemm SET_FOREACH(d, dumpset) { 134e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 135e0491636SPeter Wemm if (((*d)->ident)(efd, pid, binfile)) { 136e0491636SPeter Wemm dumper = (*d); 137e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 138e0491636SPeter Wemm break; 139e0491636SPeter Wemm } 140e0491636SPeter Wemm } 141e0491636SPeter Wemm if (dumper == NULL) 14252e7cc0aSJohn Polstra errx(1, "Invalid executable file"); 1439b50d902SRodney W. Grimes if (corefile == NULL) { 1449b50d902SRodney W. Grimes (void)snprintf(fname, sizeof(fname), "core.%d", pid); 1459b50d902SRodney W. Grimes corefile = fname; 1469b50d902SRodney W. Grimes } 1479b50d902SRodney W. Grimes fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 1489b50d902SRodney W. Grimes if (fd < 0) 149a5bf6586SPhilippe Charnier err(1, "%s", corefile); 150adaa8f14SMatt Jacob /* 151adaa8f14SMatt Jacob * The semantics of the 's' flag is to stop the target process. 152adaa8f14SMatt Jacob * Previous versions of gcore would manage this by trapping SIGHUP, 153adaa8f14SMatt Jacob * SIGINT and SIGTERM (to be passed to the target pid), and then 154adaa8f14SMatt Jacob * signal the child to stop. 155adaa8f14SMatt Jacob * 156adaa8f14SMatt Jacob * However, this messes up if the selected dumper uses ptrace calls 157adaa8f14SMatt Jacob * that leave the child already stopped. The waitpid call in elfcore 158adaa8f14SMatt Jacob * never returns. 159adaa8f14SMatt Jacob * 160adaa8f14SMatt Jacob * The best thing to do here is to externalize the 's' flag and let 161adaa8f14SMatt Jacob * each dumper dispose of what that means, if anything. For the elfcore 162adaa8f14SMatt Jacob * dumper, the 's' flag is a no-op since the ptrace attach stops the 163adaa8f14SMatt Jacob * process in question already. 164adaa8f14SMatt Jacob */ 165adaa8f14SMatt Jacob 166e0491636SPeter Wemm dumper->dump(efd, fd, pid); 1679b50d902SRodney W. Grimes (void)close(fd); 168e0491636SPeter Wemm (void)close(efd); 1699b50d902SRodney W. Grimes exit(0); 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes void 173e0491636SPeter Wemm usage(void) 1749b50d902SRodney W. Grimes { 1759b50d902SRodney W. Grimes 176327a1d9dSMike Heffner (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n"); 1779b50d902SRodney W. Grimes exit(1); 1789b50d902SRodney W. Grimes } 179