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" 74*1d73ef97SAttilio Rao int pflags; 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 92*1d73ef97SAttilio Rao pflags = 0; 939b50d902SRodney W. Grimes corefile = NULL; 94*1d73ef97SAttilio Rao while ((ch = getopt(argc, argv, "c:fs")) != -1) { 959b50d902SRodney W. Grimes switch (ch) { 969b50d902SRodney W. Grimes case 'c': 979b50d902SRodney W. Grimes corefile = optarg; 989b50d902SRodney W. Grimes break; 99*1d73ef97SAttilio Rao case 'f': 100*1d73ef97SAttilio Rao pflags |= PFLAGS_FULL; 101*1d73ef97SAttilio Rao break; 1029b50d902SRodney W. Grimes case 's': 103*1d73ef97SAttilio Rao pflags |= PFLAGS_RESUME; 1049b50d902SRodney W. Grimes break; 1059b50d902SRodney W. Grimes default: 1069b50d902SRodney W. Grimes usage(); 1079b50d902SRodney W. Grimes break; 1089b50d902SRodney W. Grimes } 1099b50d902SRodney W. Grimes } 1109b50d902SRodney W. Grimes argv += optind; 1119b50d902SRodney W. Grimes argc -= optind; 11249ee7af6SDag-Erling Smørgrav /* XXX we should check that the pid argument is really a number */ 11349ee7af6SDag-Erling Smørgrav switch (argc) { 11449ee7af6SDag-Erling Smørgrav case 1: 11549ee7af6SDag-Erling Smørgrav pid = atoi(argv[0]); 1162e7ecbfbSAttilio Rao name[0] = CTL_KERN; 1172e7ecbfbSAttilio Rao name[1] = KERN_PROC; 1182e7ecbfbSAttilio Rao name[2] = KERN_PROC_PATHNAME; 1192e7ecbfbSAttilio Rao name[3] = pid; 1202e7ecbfbSAttilio Rao len = sizeof(passpath); 1212e7ecbfbSAttilio Rao if (sysctl(name, 4, passpath, &len, NULL, 0) == -1) 1222e7ecbfbSAttilio Rao errx(1, "kern.proc.pathname failure"); 1232e7ecbfbSAttilio Rao binfile = passpath; 12449ee7af6SDag-Erling Smørgrav break; 12549ee7af6SDag-Erling Smørgrav case 2: 12649ee7af6SDag-Erling Smørgrav pid = atoi(argv[1]); 12749ee7af6SDag-Erling Smørgrav binfile = argv[0]; 12849ee7af6SDag-Erling Smørgrav break; 12949ee7af6SDag-Erling Smørgrav default: 1309b50d902SRodney W. Grimes usage(); 13149ee7af6SDag-Erling Smørgrav } 13252e7cc0aSJohn Polstra efd = open(binfile, O_RDONLY, 0); 13352e7cc0aSJohn Polstra if (efd < 0) 13452e7cc0aSJohn Polstra err(1, "%s", binfile); 135e0491636SPeter Wemm dumper = NULL; 136e0491636SPeter Wemm SET_FOREACH(d, dumpset) { 137e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 138e0491636SPeter Wemm if (((*d)->ident)(efd, pid, binfile)) { 139e0491636SPeter Wemm dumper = (*d); 140e0491636SPeter Wemm lseek(efd, 0, SEEK_SET); 141e0491636SPeter Wemm break; 142e0491636SPeter Wemm } 143e0491636SPeter Wemm } 144e0491636SPeter Wemm if (dumper == NULL) 14552e7cc0aSJohn Polstra errx(1, "Invalid executable file"); 1469b50d902SRodney W. Grimes if (corefile == NULL) { 1479b50d902SRodney W. Grimes (void)snprintf(fname, sizeof(fname), "core.%d", pid); 1489b50d902SRodney W. Grimes corefile = fname; 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 1519b50d902SRodney W. Grimes if (fd < 0) 152a5bf6586SPhilippe Charnier err(1, "%s", corefile); 153adaa8f14SMatt Jacob /* 154adaa8f14SMatt Jacob * The semantics of the 's' flag is to stop the target process. 155adaa8f14SMatt Jacob * Previous versions of gcore would manage this by trapping SIGHUP, 156adaa8f14SMatt Jacob * SIGINT and SIGTERM (to be passed to the target pid), and then 157adaa8f14SMatt Jacob * signal the child to stop. 158adaa8f14SMatt Jacob * 159adaa8f14SMatt Jacob * However, this messes up if the selected dumper uses ptrace calls 160adaa8f14SMatt Jacob * that leave the child already stopped. The waitpid call in elfcore 161adaa8f14SMatt Jacob * never returns. 162adaa8f14SMatt Jacob * 163adaa8f14SMatt Jacob * The best thing to do here is to externalize the 's' flag and let 164adaa8f14SMatt Jacob * each dumper dispose of what that means, if anything. For the elfcore 165adaa8f14SMatt Jacob * dumper, the 's' flag is a no-op since the ptrace attach stops the 166adaa8f14SMatt Jacob * process in question already. 167adaa8f14SMatt Jacob */ 168adaa8f14SMatt Jacob 169e0491636SPeter Wemm dumper->dump(efd, fd, pid); 1709b50d902SRodney W. Grimes (void)close(fd); 171e0491636SPeter Wemm (void)close(efd); 1729b50d902SRodney W. Grimes exit(0); 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes 1759b50d902SRodney W. Grimes void 176e0491636SPeter Wemm usage(void) 1779b50d902SRodney W. Grimes { 1789b50d902SRodney W. Grimes 179327a1d9dSMike Heffner (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n"); 1809b50d902SRodney W. Grimes exit(1); 1819b50d902SRodney W. Grimes } 182