Deleted Added
full compact
gcore.c (50477) gcore.c (69896)
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 "$FreeBSD: head/usr.bin/gcore/gcore.c 50477 1999-08-28 01:08:13Z peter $";
45 "$FreeBSD: head/usr.bin/gcore/gcore.c 69896 2000-12-12 07:25:57Z mckusick $";
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.

--- 21 unchanged lines hidden (view full) ---

75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
78#include <unistd.h>
79
80#include "extern.h"
81
82static void core __P((int, int, struct kinfo_proc *));
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.

--- 21 unchanged lines hidden (view full) ---

75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
78#include <unistd.h>
79
80#include "extern.h"
81
82static void core __P((int, int, struct kinfo_proc *));
83static void datadump __P((int, int, struct proc *, u_long, int));
83static void datadump __P((int, int, struct kinfo_proc *, u_long, int));
84static void killed __P((int));
85static void restart_target __P((void));
86static void usage __P((void)) __dead2;
84static void killed __P((int));
85static void restart_target __P((void));
86static void usage __P((void)) __dead2;
87static void userdump __P((int, struct proc *, u_long, int));
87static void userdump __P((int, struct kinfo_proc *, u_long, int));
88
89kvm_t *kd;
88
89kvm_t *kd;
90/* XXX undocumented routine, should be in kvm.h? */
91ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, size_t));
92
93static int data_offset;
94static pid_t pid;
95
96int
97main(argc, argv)
98 int argc;
99 char *argv[];
100{
90
91static int data_offset;
92static pid_t pid;
93
94int
95main(argc, argv)
96 int argc;
97 char *argv[];
98{
101 register struct proc *p;
102 struct kinfo_proc *ki = NULL;
103 struct exec exec;
104 int ch, cnt, efd, fd, sflag, uid;
105 char *binfile, *corefile;
106 char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
107 int is_aout;
108
109 sflag = 0;

--- 49 unchanged lines hidden (view full) ---

159 errx(1, "%s", errbuf);
160
161 uid = getuid();
162
163 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
164 if (ki == NULL || cnt != 1)
165 errx(1, "%d: not found", pid);
166
99 struct kinfo_proc *ki = NULL;
100 struct exec exec;
101 int ch, cnt, efd, fd, sflag, uid;
102 char *binfile, *corefile;
103 char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1];
104 int is_aout;
105
106 sflag = 0;

--- 49 unchanged lines hidden (view full) ---

156 errx(1, "%s", errbuf);
157
158 uid = getuid();
159
160 ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt);
161 if (ki == NULL || cnt != 1)
162 errx(1, "%d: not found", pid);
163
167 p = &ki->kp_proc;
168 if (ki->kp_eproc.e_pcred.p_ruid != uid && uid != 0)
164 if (ki->ki_ruid != uid && uid != 0)
169 errx(1, "%d: not owner", pid);
170
165 errx(1, "%d: not owner", pid);
166
171 if (p->p_stat == SZOMB)
167 if (ki->ki_stat == SZOMB)
172 errx(1, "%d: zombie", pid);
173
168 errx(1, "%d: zombie", pid);
169
174 if (p->p_flag & P_WEXIT)
170 if (ki->ki_flag & P_WEXIT)
175 errx(1, "%d: process exiting", pid);
171 errx(1, "%d: process exiting", pid);
176 if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */
172 if (ki->ki_flag & P_SYSTEM) /* Swapper or pagedaemon. */
177 errx(1, "%d: system process", pid);
173 errx(1, "%d: system process", pid);
178 if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize))
174 if (exec.a_text != ptoa(ki->ki_tsize))
179 errx(1, "The executable %s does not belong to"
180 " process %d!\n"
181 "Text segment size (in bytes): executable %ld,"
182 " process %d", binfile, pid, exec.a_text,
175 errx(1, "The executable %s does not belong to"
176 " process %d!\n"
177 "Text segment size (in bytes): executable %ld,"
178 " process %d", binfile, pid, exec.a_text,
183 ptoa(ki->kp_eproc.e_vm.vm_tsize));
179 ptoa(ki->ki_tsize));
184 data_offset = N_DATOFF(exec);
185 } else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
186 is_aout = 0;
187 close(efd);
188 } else
189 errx(1, "Invalid executable file");
190
191 if (corefile == NULL) {

--- 31 unchanged lines hidden (view full) ---

223 int efd;
224 int fd;
225 struct kinfo_proc *ki;
226{
227 union {
228 struct user user;
229 char ubytes[ctob(UPAGES)];
230 } uarea;
180 data_offset = N_DATOFF(exec);
181 } else if (IS_ELF(*(Elf_Ehdr *)&exec)) {
182 is_aout = 0;
183 close(efd);
184 } else
185 errx(1, "Invalid executable file");
186
187 if (corefile == NULL) {

--- 31 unchanged lines hidden (view full) ---

219 int efd;
220 int fd;
221 struct kinfo_proc *ki;
222{
223 union {
224 struct user user;
225 char ubytes[ctob(UPAGES)];
226 } uarea;
231 struct proc *p = &ki->kp_proc;
232 int tsize = ki->kp_eproc.e_vm.vm_tsize;
233 int dsize = ki->kp_eproc.e_vm.vm_dsize;
234 int ssize = ki->kp_eproc.e_vm.vm_ssize;
227 int tsize = ki->ki_tsize;
228 int dsize = ki->ki_dsize;
229 int ssize = ki->ki_ssize;
235 int cnt;
236
237 /* Read in user struct */
230 int cnt;
231
232 /* Read in user struct */
238 cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea));
233 cnt = kvm_read(kd, (u_long)ki->ki_addr, &uarea, sizeof(uarea));
239 if (cnt != sizeof(uarea))
240 errx(1, "read user structure: %s",
241 cnt > 0 ? strerror(EIO) : strerror(errno));
242
243 /*
244 * Fill in the eproc vm parameters, since these are garbage unless
245 * the kernel is dumping core or something.
246 */
247 uarea.user.u_kproc = *ki;
248
249 /* Dump user area */
250 cnt = write(fd, &uarea, sizeof(uarea));
251 if (cnt != sizeof(uarea))
252 errx(1, "write user structure: %s",
253 cnt > 0 ? strerror(EIO) : strerror(errno));
254
255 /* Dump data segment */
234 if (cnt != sizeof(uarea))
235 errx(1, "read user structure: %s",
236 cnt > 0 ? strerror(EIO) : strerror(errno));
237
238 /*
239 * Fill in the eproc vm parameters, since these are garbage unless
240 * the kernel is dumping core or something.
241 */
242 uarea.user.u_kproc = *ki;
243
244 /* Dump user area */
245 cnt = write(fd, &uarea, sizeof(uarea));
246 if (cnt != sizeof(uarea))
247 errx(1, "write user structure: %s",
248 cnt > 0 ? strerror(EIO) : strerror(errno));
249
250 /* Dump data segment */
256 datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize);
251 datadump(efd, fd, ki, USRTEXT + ctob(tsize), dsize);
257
258 /* Dump stack segment */
252
253 /* Dump stack segment */
259 userdump(fd, p, USRSTACK - ctob(ssize), ssize);
254 userdump(fd, ki, USRSTACK - ctob(ssize), ssize);
260
261 /* Dump machine dependent portions of the core. */
262 md_core(kd, fd, ki);
263}
264
265void
255
256 /* Dump machine dependent portions of the core. */
257 md_core(kd, fd, ki);
258}
259
260void
266datadump(efd, fd, p, addr, npage)
261datadump(efd, fd, kp, addr, npage)
267 register int efd;
268 register int fd;
262 register int efd;
263 register int fd;
269 struct proc *p;
264 struct kinfo_proc *kp;
270 register u_long addr;
271 register int npage;
272{
273 register int cc, delta;
274 char buffer[PAGE_SIZE];
275
276 delta = data_offset - addr;
277 while (--npage >= 0) {
265 register u_long addr;
266 register int npage;
267{
268 register int cc, delta;
269 char buffer[PAGE_SIZE];
270
271 delta = data_offset - addr;
272 while (--npage >= 0) {
278 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
273 cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
279 if (cc != PAGE_SIZE) {
280 /* Try to read the page from the executable. */
281 if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
282 err(1, "seek executable: %s", strerror(errno));
283 cc = read(efd, buffer, sizeof(buffer));
284 if (cc != sizeof(buffer)) {
285 if (cc < 0)
286 err(1, "read executable");

--- 20 unchanged lines hidden (view full) ---

307
308static void
309restart_target()
310{
311 kill(pid, SIGCONT);
312}
313
314void
274 if (cc != PAGE_SIZE) {
275 /* Try to read the page from the executable. */
276 if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1)
277 err(1, "seek executable: %s", strerror(errno));
278 cc = read(efd, buffer, sizeof(buffer));
279 if (cc != sizeof(buffer)) {
280 if (cc < 0)
281 err(1, "read executable");

--- 20 unchanged lines hidden (view full) ---

302
303static void
304restart_target()
305{
306 kill(pid, SIGCONT);
307}
308
309void
315userdump(fd, p, addr, npage)
310userdump(fd, kp, addr, npage)
316 register int fd;
311 register int fd;
317 struct proc *p;
312 struct kinfo_proc *kp;
318 register u_long addr;
319 register int npage;
320{
321 register int cc;
322 char buffer[PAGE_SIZE];
323
324 while (--npage >= 0) {
313 register u_long addr;
314 register int npage;
315{
316 register int cc;
317 char buffer[PAGE_SIZE];
318
319 while (--npage >= 0) {
325 cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE);
320 cc = kvm_uread(kd, kp, addr, buffer, PAGE_SIZE);
326 if (cc != PAGE_SIZE)
327 /* Could be an untouched fill-with-zero page. */
328 bzero(buffer, PAGE_SIZE);
329 cc = write(fd, buffer, PAGE_SIZE);
330 if (cc != PAGE_SIZE)
331 errx(1, "write stack segment: %s",
332 cc > 0 ? strerror(EIO) : strerror(errno));
333 addr += PAGE_SIZE;
334 }
335}
336
337void
338usage()
339{
340 (void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");
341 exit(1);
342}
321 if (cc != PAGE_SIZE)
322 /* Could be an untouched fill-with-zero page. */
323 bzero(buffer, PAGE_SIZE);
324 cc = write(fd, buffer, PAGE_SIZE);
325 if (cc != PAGE_SIZE)
326 errx(1, "write stack segment: %s",
327 cc > 0 ? strerror(EIO) : strerror(errno));
328 addr += PAGE_SIZE;
329 }
330}
331
332void
333usage()
334{
335 (void)fprintf(stderr, "usage: gcore [-s] [-c core] executable pid\n");
336 exit(1);
337}