gcore.c revision 199805
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93"; 43#endif /* not lint */ 44#endif 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/usr.bin/gcore/gcore.c 199805 2009-11-25 15:23:14Z attilio $"); 47 48/* 49 * Originally written by Eric Cooper in Fall 1981. 50 * Inspired by a version 6 program by Len Levin, 1978. 51 * Several pieces of code lifted from Bill Joy's 4BSD ps. 52 * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne, 53 * Lawrence Berkeley Laboratory. 54 * 55 * Portions of this software were developed by the Computer Systems 56 * Engineering group at Lawrence Berkeley Laboratory under DARPA 57 * contract BG 91-66 and contributed to Berkeley. 58 */ 59 60#include <sys/param.h> 61#include <sys/time.h> 62#include <sys/stat.h> 63#include <sys/linker_set.h> 64#include <sys/sysctl.h> 65 66#include <err.h> 67#include <fcntl.h> 68#include <signal.h> 69#include <stdio.h> 70#include <stdlib.h> 71#include <string.h> 72#include <unistd.h> 73 74#include "extern.h" 75 76static void killed(int); 77static void restart_target(void); 78static void usage(void) __dead2; 79 80static pid_t pid; 81 82SET_DECLARE(dumpset, struct dumpers); 83 84int 85main(int argc, char *argv[]) 86{ 87 int ch, efd, fd, name[4], sflag; 88 char *binfile, *corefile; 89 char passpath[MAXPATHLEN], fname[MAXPATHLEN]; 90 struct dumpers **d, *dumper; 91 size_t len; 92 93 sflag = 0; 94 corefile = NULL; 95 while ((ch = getopt(argc, argv, "c:s")) != -1) { 96 switch (ch) { 97 case 'c': 98 corefile = optarg; 99 break; 100 case 's': 101 sflag = 1; 102 break; 103 default: 104 usage(); 105 break; 106 } 107 } 108 argv += optind; 109 argc -= optind; 110 /* XXX we should check that the pid argument is really a number */ 111 switch (argc) { 112 case 1: 113 pid = atoi(argv[0]); 114 name[0] = CTL_KERN; 115 name[1] = KERN_PROC; 116 name[2] = KERN_PROC_PATHNAME; 117 name[3] = pid; 118 len = sizeof(passpath); 119 if (sysctl(name, 4, passpath, &len, NULL, 0) == -1) 120 errx(1, "kern.proc.pathname failure"); 121 binfile = passpath; 122 break; 123 case 2: 124 pid = atoi(argv[1]); 125 binfile = argv[0]; 126 break; 127 default: 128 usage(); 129 } 130 efd = open(binfile, O_RDONLY, 0); 131 if (efd < 0) 132 err(1, "%s", binfile); 133 dumper = NULL; 134 SET_FOREACH(d, dumpset) { 135 lseek(efd, 0, SEEK_SET); 136 if (((*d)->ident)(efd, pid, binfile)) { 137 dumper = (*d); 138 lseek(efd, 0, SEEK_SET); 139 break; 140 } 141 } 142 if (dumper == NULL) 143 errx(1, "Invalid executable file"); 144 if (corefile == NULL) { 145 (void)snprintf(fname, sizeof(fname), "core.%d", pid); 146 corefile = fname; 147 } 148 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 149 if (fd < 0) 150 err(1, "%s", corefile); 151 if (sflag) { 152 signal(SIGHUP, killed); 153 signal(SIGINT, killed); 154 signal(SIGTERM, killed); 155 if (kill(pid, SIGSTOP) == -1) 156 err(1, "%d: stop signal", pid); 157 atexit(restart_target); 158 } 159 dumper->dump(efd, fd, pid); 160 (void)close(fd); 161 (void)close(efd); 162 exit(0); 163} 164 165static void 166killed(int sig) 167{ 168 169 restart_target(); 170 signal(sig, SIG_DFL); 171 kill(getpid(), sig); 172} 173 174static void 175restart_target(void) 176{ 177 178 kill(pid, SIGCONT); 179} 180 181void 182usage(void) 183{ 184 185 (void)fprintf(stderr, "usage: gcore [-s] [-c core] [executable] pid\n"); 186 exit(1); 187} 188