xref: /dragonfly/usr.bin/gcore/gcore.c (revision 37de577a)
1 /*-
2  * Copyright (c) 1992, 1993, 2019
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1992, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)gcore.c	8.2 (Berkeley) 9/23/93
31  * $FreeBSD: src/usr.bin/gcore/gcore.c,v 1.15.2.2 2001/08/17 20:56:22 mikeh Exp $
32  */
33 
34 /*
35  * Originally written by Eric Cooper in Fall 1981.
36  * Inspired by a version 6 program by Len Levin, 1978.
37  * Several pieces of code lifted from Bill Joy's 4BSD ps.
38  * Most recently, hacked beyond recognition for 4.4BSD by Steven McCanne,
39  * Lawrence Berkeley Laboratory.
40  *
41  * Portions of this software were developed by the Computer Systems
42  * Engineering group at Lawrence Berkeley Laboratory under DARPA
43  * contract BG 91-66 and contributed to Berkeley.
44  */
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <machine/elf.h>
50 
51 #include <machine/vmparam.h>
52 
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <kvm.h>
57 #include <limits.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "extern.h"
65 
66 static void	killed(int);
67 static void	restart_target(void);
68 static void	usage(void) __dead2;
69 
70 kvm_t *kd;
71 
72 static pid_t pid;
73 ssize_t gcore_seg_limit = -1;
74 int gcore_verbose;
75 
76 int
77 main(int argc, char **argv)
78 {
79 	void *exec;
80 	int ch, cnt, efd, fd, sflag;
81 	char *binfile, *corefile;
82 	char *eptr;
83 	char fname[MAXPATHLEN + 1];
84 
85 	sflag = 0;
86 	corefile = NULL;
87         while ((ch = getopt(argc, argv, "c:l:sv")) != -1) {
88                 switch (ch) {
89                 case 'c':
90 			corefile = optarg;
91                         break;
92 		case 'l':
93 			gcore_seg_limit = strtol(optarg, &eptr, 0);
94 			switch(*eptr) {
95 			case 0:
96 				break;
97 			case 't':
98 			case 'T':
99 				gcore_seg_limit *= 1024;
100 				/* fall through */
101 			case 'g':
102 			case 'G':
103 				gcore_seg_limit *= 1024;
104 				/* fall through */
105 			case 'm':
106 			case 'M':
107 				gcore_seg_limit *= 1024;
108 				/* fall through */
109 			case 'k':
110 			case 'K':
111 				gcore_seg_limit *= 1024;
112 				break;
113 			default:
114 				usage();
115 				break;
116 			}
117 			break;
118 		case 's':
119 			sflag = 1;
120 			break;
121 		case 'v':
122 			gcore_verbose = 1;
123 			break;
124 		default:
125 			usage();
126 			break;
127 		}
128 	}
129 	argv += optind;
130 	argc -= optind;
131 
132 	/* XXX we should check that the pid argument is really a number */
133 	switch (argc) {
134 	case 1:
135 		pid = atoi(argv[0]);
136 		asprintf(&binfile, "/proc/%d/file", pid);
137 		if (binfile == NULL)
138 			errx(1, "allocation failure");
139 		break;
140 	case 2:
141 		pid = atoi(argv[1]);
142 		binfile = argv[0];
143 		break;
144 	default:
145 		usage();
146 	}
147 
148 	efd = open(binfile, O_RDONLY, 0);
149 	if (efd < 0)
150 		err(1, "%s", binfile);
151 
152 	cnt = read(efd, &exec, sizeof(Elf_Ehdr));
153 	if (cnt != sizeof(Elf_Ehdr))
154 		errx(1, "%s exec header: %s",
155 		    binfile, cnt > 0 ? strerror(EIO) : strerror(errno));
156 	if (IS_ELF(*(Elf_Ehdr *)&exec)) {
157 		close(efd);
158 	} else
159 		errx(1, "Invalid executable file");
160 
161 	if (corefile == NULL) {
162 		snprintf(fname, sizeof(fname), "core.%d", pid);
163 		corefile = fname;
164 	}
165 	fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
166 	if (fd < 0)
167 		err(1, "%s", corefile);
168 
169 	if (sflag) {
170 		signal(SIGHUP, killed);
171 		signal(SIGINT, killed);
172 		signal(SIGTERM, killed);
173 		if (kill(pid, SIGSTOP) == -1)
174 			err(1, "%d: stop signal", pid);
175 		atexit(restart_target);
176 	}
177 
178 	elf_coredump(fd, pid);
179 
180 	close(fd);
181 	exit(0);
182 }
183 
184 static void
185 killed(int sig)
186 {
187 	restart_target();
188 	signal(sig, SIG_DFL);
189 	kill(getpid(), sig);
190 }
191 
192 static void
193 restart_target(void)
194 {
195 	kill(pid, SIGCONT);
196 }
197 
198 void
199 usage(void)
200 {
201 	fprintf(stderr,
202 	    "usage: gcore [-sv] [-c core] [-l seglimit] [executable] pid\n");
203 	exit(1);
204 }
205