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