gcore.c (40350) | gcore.c (40525) |
---|---|
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 --- 28 unchanged lines hidden (view full) --- 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[] = | 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 --- 28 unchanged lines hidden (view full) --- 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.9 1998/09/14 10:09:30 des Exp $"; | 45 "$Id: gcore.c,v 1.10 1998/10/14 16:16:50 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. --- 22 unchanged lines hidden (view full) --- 76#include <stdlib.h> 77#include <string.h> 78#include <unistd.h> 79 80#include "extern.h" 81 82void core __P((int, int, struct kinfo_proc *)); 83void datadump __P((int, int, struct proc *, u_long, int)); | 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. --- 22 unchanged lines hidden (view full) --- 76#include <stdlib.h> 77#include <string.h> 78#include <unistd.h> 79 80#include "extern.h" 81 82void core __P((int, int, struct kinfo_proc *)); 83void datadump __P((int, int, struct proc *, u_long, int)); |
84void usage __P((void)); | 84void usage __P((void)) __dead2; |
85void userdump __P((int, struct proc *, u_long, int)); 86 87kvm_t *kd; 88/* XXX undocumented routine, should be in kvm.h? */ 89ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t)); 90 91 92static int data_offset; 93 94int 95main(argc, argv) 96 int argc; 97 char *argv[]; 98{ 99 register struct proc *p; | 85void userdump __P((int, struct proc *, u_long, int)); 86 87kvm_t *kd; 88/* XXX undocumented routine, should be in kvm.h? */ 89ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t)); 90 91 92static int data_offset; 93 94int 95main(argc, argv) 96 int argc; 97 char *argv[]; 98{ 99 register struct proc *p; |
100 struct kinfo_proc *ki; | 100 struct kinfo_proc *ki = NULL; |
101 struct exec exec; 102 int ch, cnt, efd, fd, pid, sflag, uid; 103 char *binfile, *corefile; 104 char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; | 101 struct exec exec; 102 int ch, cnt, efd, fd, pid, sflag, uid; 103 char *binfile, *corefile; 104 char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; |
105 int is_aout; |
|
105 106 sflag = 0; 107 corefile = NULL; 108 while ((ch = getopt(argc, argv, "c:s")) != -1) { 109 switch (ch) { 110 case 'c': 111 corefile = optarg; 112 break; --- 19 unchanged lines hidden (view full) --- 132 case 2: 133 pid = atoi(argv[1]); 134 binfile = argv[0]; 135 break; 136 default: 137 usage(); 138 } 139 | 106 107 sflag = 0; 108 corefile = NULL; 109 while ((ch = getopt(argc, argv, "c:s")) != -1) { 110 switch (ch) { 111 case 'c': 112 corefile = optarg; 113 break; --- 19 unchanged lines hidden (view full) --- 133 case 2: 134 pid = atoi(argv[1]); 135 binfile = argv[0]; 136 break; 137 default: 138 usage(); 139 } 140 |
140 kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); 141 if (kd == NULL) 142 errx(1, "%s", errbuf); | 141 efd = open(binfile, O_RDONLY, 0); 142 if (efd < 0) 143 err(1, "%s", binfile); |
143 | 144 |
144 uid = getuid(); | 145 cnt = read(efd, &exec, sizeof(exec)); 146 if (cnt != sizeof(exec)) 147 errx(1, "%s exec header: %s", 148 binfile, cnt > 0 ? strerror(EIO) : strerror(errno)); 149 if (!N_BADMAG(exec)) { 150 is_aout = 1; 151 /* 152 * This legacy a.out support uses the kvm interface instead 153 * of procfs. 154 */ 155 kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); 156 if (kd == NULL) 157 errx(1, "%s", errbuf); |
145 | 158 |
146 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt); 147 if (ki == NULL || cnt != 1) 148 errx(1, "%d: not found", pid); | 159 uid = getuid(); |
149 | 160 |
150 p = &ki->kp_proc; 151 if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0) 152 errx(1, "%d: not owner", pid); | 161 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt); 162 if (ki == NULL || cnt != 1) 163 errx(1, "%d: not found", pid); |
153 | 164 |
154 if (p->p_stat == SZOMB) 155 errx(1, "%d: zombie", pid); | 165 p = &ki->kp_proc; 166 if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0) 167 errx(1, "%d: not owner", pid); |
156 | 168 |
157 if (p->p_flag & P_WEXIT) 158 errx(1, "%d: process exiting", pid); 159 if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */ 160 errx(1, "%d: system process", pid); | 169 if (p->p_stat == SZOMB) 170 errx(1, "%d: zombie", pid); |
161 | 171 |
172 if (p->p_flag & P_WEXIT) 173 errx(1, "%d: process exiting", pid); 174 if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */ 175 errx(1, "%d: system process", pid); 176 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) 177 errx(1, "The executable %s does not belong to" 178 " process %d!\n" 179 "Text segment size (in bytes): executable %d," 180 " process %d", binfile, pid, exec.a_text, 181 ptoa(ki->kp_eproc.e_vm.vm_tsize)); 182 data_offset = N_DATOFF(exec); 183 } else if (IS_ELF(*(Elf_Ehdr *)&exec)) { 184 is_aout = 0; 185 close(efd); 186 } else 187 errx(1, "Invalid executable file"); 188 |
|
162 if (corefile == NULL) { 163 (void)snprintf(fname, sizeof(fname), "core.%d", pid); 164 corefile = fname; 165 } 166 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 167 if (fd < 0) 168 err(1, "%s", corefile); 169 | 189 if (corefile == NULL) { 190 (void)snprintf(fname, sizeof(fname), "core.%d", pid); 191 corefile = fname; 192 } 193 fd = open(corefile, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE); 194 if (fd < 0) 195 err(1, "%s", corefile); 196 |
170 efd = open(binfile, O_RDONLY, 0); 171 if (efd < 0) 172 err(1, "%s", binfile); 173 174 cnt = read(efd, &exec, sizeof(exec)); 175 if (cnt != sizeof(exec)) 176 errx(1, "%s exec header: %s", 177 binfile, cnt > 0 ? strerror(EIO) : strerror(errno)); 178 if (N_BADMAG(exec)) { 179 const Elf_Ehdr *ehdr = (const Elf_Ehdr *)&exec; 180 181 if (IS_ELF(*ehdr)) 182 errx(1, "ELF executables are not supported yet"); 183 errx(1, "Invalid executable file"); 184 } 185 186 /* check the text segment size of the executable and the process */ 187 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) 188 errx(1, 189 "The executable %s does not belong to process %d!\n" 190 "Text segment size (in bytes): executable %d, process %d", 191 binfile, pid, exec.a_text, 192 ptoa(ki->kp_eproc.e_vm.vm_tsize)); 193 194 data_offset = N_DATOFF(exec); 195 | |
196 if (sflag && kill(pid, SIGSTOP) < 0) 197 err(1, "%d: stop signal", pid); 198 | 197 if (sflag && kill(pid, SIGSTOP) < 0) 198 err(1, "%d: stop signal", pid); 199 |
199 core(efd, fd, ki); | 200 if (is_aout) 201 core(efd, fd, ki); 202 else 203 elf_coredump(fd, pid); |
200 201 if (sflag && kill(pid, SIGCONT) < 0) 202 err(1, "%d: continue signal", pid); 203 (void)close(fd); 204 205 exit(0); 206} 207 --- 110 unchanged lines hidden --- | 204 205 if (sflag && kill(pid, SIGCONT) < 0) 206 err(1, "%d: continue signal", pid); 207 (void)close(fd); 208 209 exit(0); 210} 211 --- 110 unchanged lines hidden --- |