gcore.c revision 38522
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#ifndef lint 41#if 0 42static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93"; 43#endif 44static const char rcsid[] = 45 "$Id: gcore.c,v 1.7 1997/11/18 03:50:25 jdp Exp $"; 46#endif /* not lint */ 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#include <sys/param.h> 60#include <sys/time.h> 61#include <sys/stat.h> 62#include <sys/proc.h> 63#include <sys/user.h> 64#include <sys/sysctl.h> 65 66#include <machine/vmparam.h> 67 68#include <a.out.h> 69#include <err.h> 70#include <fcntl.h> 71#include <kvm.h> 72#include <limits.h> 73#include <signal.h> 74#include <stdio.h> 75#include <stdlib.h> 76#include <string.h> 77#include <unistd.h> 78 79#include "extern.h" 80 81void core __P((int, int, struct kinfo_proc *)); 82void datadump __P((int, int, struct proc *, u_long, int)); 83void usage __P((void)); 84void userdump __P((int, struct proc *, u_long, int)); 85 86kvm_t *kd; 87/* XXX undocumented routine, should be in kvm.h? */ 88ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t)); 89 90 91static int data_offset; 92 93int 94main(argc, argv) 95 int argc; 96 char *argv[]; 97{ 98 register struct proc *p; 99 struct kinfo_proc *ki; 100 struct exec exec; 101 int ch, cnt, efd, fd, pid, sflag, uid; 102 char *corefile, errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; 103 104 sflag = 0; 105 corefile = NULL; 106 while ((ch = getopt(argc, argv, "c:s")) != -1) { 107 switch (ch) { 108 case 'c': 109 corefile = optarg; 110 break; 111 case 's': 112 sflag = 1; 113 break; 114 default: 115 usage(); 116 break; 117 } 118 } 119 argv += optind; 120 argc -= optind; 121 122 if (argc != 2) 123 usage(); 124 125 kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); 126 if (kd == NULL) 127 errx(1, "%s", errbuf); 128 129 uid = getuid(); 130 pid = atoi(argv[1]); 131 132 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt); 133 if (ki == NULL || cnt != 1) 134 errx(1, "%d: not found", pid); 135 136 p = &ki->kp_proc; 137 if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0) 138 errx(1, "%d: not owner", pid); 139 140 if (p->p_stat == SZOMB) 141 errx(1, "%d: zombie", pid); 142 143 if (p->p_flag & P_WEXIT) 144 errx(1, "%d: process exiting", pid); 145 if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */ 146 errx(1, "%d: system process", pid); 147 148 if (corefile == NULL) { 149 (void)snprintf(fname, sizeof(fname), "core.%d", pid); 150 corefile = fname; 151 } 152 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 153 if (fd < 0) 154 err(1, "%s", corefile); 155 156 efd = open(argv[0], O_RDONLY, 0); 157 if (efd < 0) 158 err(1, "%s", argv[0]); 159 160 cnt = read(efd, &exec, sizeof(exec)); 161 if (cnt != sizeof(exec)) 162 errx(1, "%s exec header: %s", 163 argv[0], cnt > 0 ? strerror(EIO) : strerror(errno)); 164 165 /* check the text segment size of the executable and the process */ 166 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) 167 errx(1, 168 "The executable %s does not belong to process %d!\n" 169 "Text segment size (in bytes): executable %d, process %d", 170 argv[0], pid, exec.a_text, 171 ptoa(ki->kp_eproc.e_vm.vm_tsize)); 172 173 data_offset = N_DATOFF(exec); 174 175 if (sflag && kill(pid, SIGSTOP) < 0) 176 err(1, "%d: stop signal", pid); 177 178 core(efd, fd, ki); 179 180 if (sflag && kill(pid, SIGCONT) < 0) 181 err(1, "%d: continue signal", pid); 182 (void)close(fd); 183 184 exit(0); 185} 186 187/* 188 * core -- 189 * Build the core file. 190 */ 191void 192core(efd, fd, ki) 193 int efd; 194 int fd; 195 struct kinfo_proc *ki; 196{ 197 union { 198 struct user user; 199 char ubytes[ctob(UPAGES)]; 200 } uarea; 201 struct proc *p = &ki->kp_proc; 202 int tsize = ki->kp_eproc.e_vm.vm_tsize; 203 int dsize = ki->kp_eproc.e_vm.vm_dsize; 204 int ssize = ki->kp_eproc.e_vm.vm_ssize; 205 int cnt; 206 207 /* Read in user struct */ 208 cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea)); 209 if (cnt != sizeof(uarea)) 210 errx(1, "read user structure: %s", 211 cnt > 0 ? strerror(EIO) : strerror(errno)); 212 213 /* 214 * Fill in the eproc vm parameters, since these are garbage unless 215 * the kernel is dumping core or something. 216 */ 217 uarea.user.u_kproc = *ki; 218 219 /* Dump user area */ 220 cnt = write(fd, &uarea, sizeof(uarea)); 221 if (cnt != sizeof(uarea)) 222 errx(1, "write user structure: %s", 223 cnt > 0 ? strerror(EIO) : strerror(errno)); 224 225 /* Dump data segment */ 226 datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize); 227 228 /* Dump stack segment */ 229 userdump(fd, p, USRSTACK - ctob(ssize), ssize); 230 231 /* Dump machine dependent portions of the core. */ 232 md_core(kd, fd, ki); 233} 234 235void 236datadump(efd, fd, p, addr, npage) 237 register int efd; 238 register int fd; 239 struct proc *p; 240 register u_long addr; 241 register int npage; 242{ 243 register int cc, delta; 244 char buffer[PAGE_SIZE]; 245 246 delta = data_offset - addr; 247 while (--npage >= 0) { 248 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE); 249 if (cc != PAGE_SIZE) { 250 /* Try to read the page from the executable. */ 251 if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1) 252 err(1, "seek executable: %s", strerror(errno)); 253 cc = read(efd, buffer, sizeof(buffer)); 254 if (cc != sizeof(buffer)) 255 if (cc < 0) 256 err(1, "read executable"); 257 else /* Assume untouched bss page. */ 258 bzero(buffer, sizeof(buffer)); 259 } 260 cc = write(fd, buffer, PAGE_SIZE); 261 if (cc != PAGE_SIZE) 262 errx(1, "write data segment: %s", 263 cc > 0 ? strerror(EIO) : strerror(errno)); 264 addr += PAGE_SIZE; 265 } 266} 267 268void 269userdump(fd, p, addr, npage) 270 register int fd; 271 struct proc *p; 272 register u_long addr; 273 register int npage; 274{ 275 register int cc; 276 char buffer[PAGE_SIZE]; 277 278 while (--npage >= 0) { 279 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE); 280 if (cc != PAGE_SIZE) 281 /* Could be an untouched fill-with-zero page. */ 282 bzero(buffer, PAGE_SIZE); 283 cc = write(fd, buffer, PAGE_SIZE); 284 if (cc != PAGE_SIZE) 285 errx(1, "write stack segment: %s", 286 cc > 0 ? strerror(EIO) : strerror(errno)); 287 addr += PAGE_SIZE; 288 } 289} 290 291void 292usage() 293{ 294 (void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n"); 295 exit(1); 296} 297