diff --git a/usr.bin/gcore/aoutcore.c b/usr.bin/gcore/aoutcore.c index 46ef21eb766c..a943a304c1b5 100644 --- a/usr.bin/gcore/aoutcore.c +++ b/usr.bin/gcore/aoutcore.c @@ -42,7 +42,7 @@ static const char copyright[] = static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93"; #endif static const char rcsid[] = - "$Id: gcore.c,v 1.7 1997/11/18 03:50:25 jdp Exp $"; + "$Id: gcore.c,v 1.8 1998/08/24 16:25:30 wosch Exp $"; #endif /* not lint */ /* @@ -99,7 +99,8 @@ main(argc, argv) struct kinfo_proc *ki; struct exec exec; int ch, cnt, efd, fd, pid, sflag, uid; - char *corefile, errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; + char *binfile, *corefile; + char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; sflag = 0; corefile = NULL; @@ -119,15 +120,27 @@ main(argc, argv) argv += optind; argc -= optind; - if (argc != 2) + /* XXX we should check that the pid argument is really a number */ + switch (argc) { + case 1: + pid = atoi(argv[0]); + asprintf(&binfile, "/proc/%d/file", pid); + if (binfile == NULL) + errx(1, "allocation failure"); + break; + case 2: + pid = atoi(argv[1]); + binfile = argv[0]; + break; + default: usage(); + } kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); if (kd == NULL) errx(1, "%s", errbuf); uid = getuid(); - pid = atoi(argv[1]); ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt); if (ki == NULL || cnt != 1) @@ -153,21 +166,21 @@ main(argc, argv) if (fd < 0) err(1, "%s", corefile); - efd = open(argv[0], O_RDONLY, 0); + efd = open(binfile, O_RDONLY, 0); if (efd < 0) - err(1, "%s", argv[0]); + err(1, "%s", binfile); cnt = read(efd, &exec, sizeof(exec)); if (cnt != sizeof(exec)) errx(1, "%s exec header: %s", - argv[0], cnt > 0 ? strerror(EIO) : strerror(errno)); + binfile, cnt > 0 ? strerror(EIO) : strerror(errno)); /* check the text segment size of the executable and the process */ if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) errx(1, "The executable %s does not belong to process %d!\n" "Text segment size (in bytes): executable %d, process %d", - argv[0], pid, exec.a_text, + binfile, pid, exec.a_text, ptoa(ki->kp_eproc.e_vm.vm_tsize)); data_offset = N_DATOFF(exec); diff --git a/usr.bin/gcore/gcore.1 b/usr.bin/gcore/gcore.1 index 2e1d5f6e0bcb..8a66e88b063c 100644 --- a/usr.bin/gcore/gcore.1 +++ b/usr.bin/gcore/gcore.1 @@ -41,7 +41,8 @@ .Nm .Op Fl s .Op Fl c Ar core -.Ar exec pid +.Op Ar exec +.Ar pid .Sh DESCRIPTION .Nm Gcore creates a core image of the specified process, @@ -49,11 +50,13 @@ suitable for use with .Xr gdb 1 . By default, the core is written to the file .Dq Pa core. . -Both the executable image, -.Ar exec , -and the process identifier, +The process identifier, .Ar pid , -must be given on the command line. +must be given on the command line. If no executable image is +specified, +.Nm +will use +.Dq Pa /proc//file . .Pp The options are: .Bl -tag -width indent @@ -72,6 +75,8 @@ The same effect can be achieved manually with .Bl -tag -width /var/log/messages -compact .It Pa core. The core image. +.It Pa /proc//file +The executable image. .EL .Dp .Sh HISTORY @@ -90,8 +95,3 @@ to temporarily stop the target process. is not compatible with the original .Bx 4.2 version. -In particular, -.Bx 4.4 -requires the -.Ar exec -argument. diff --git a/usr.bin/gcore/gcore.c b/usr.bin/gcore/gcore.c index 46ef21eb766c..a943a304c1b5 100644 --- a/usr.bin/gcore/gcore.c +++ b/usr.bin/gcore/gcore.c @@ -42,7 +42,7 @@ static const char copyright[] = static char sccsid[] = "@(#)gcore.c 8.2 (Berkeley) 9/23/93"; #endif static const char rcsid[] = - "$Id: gcore.c,v 1.7 1997/11/18 03:50:25 jdp Exp $"; + "$Id: gcore.c,v 1.8 1998/08/24 16:25:30 wosch Exp $"; #endif /* not lint */ /* @@ -99,7 +99,8 @@ main(argc, argv) struct kinfo_proc *ki; struct exec exec; int ch, cnt, efd, fd, pid, sflag, uid; - char *corefile, errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; + char *binfile, *corefile; + char errbuf[_POSIX2_LINE_MAX], fname[MAXPATHLEN + 1]; sflag = 0; corefile = NULL; @@ -119,15 +120,27 @@ main(argc, argv) argv += optind; argc -= optind; - if (argc != 2) + /* XXX we should check that the pid argument is really a number */ + switch (argc) { + case 1: + pid = atoi(argv[0]); + asprintf(&binfile, "/proc/%d/file", pid); + if (binfile == NULL) + errx(1, "allocation failure"); + break; + case 2: + pid = atoi(argv[1]); + binfile = argv[0]; + break; + default: usage(); + } kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf); if (kd == NULL) errx(1, "%s", errbuf); uid = getuid(); - pid = atoi(argv[1]); ki = kvm_getprocs(kd, KERN_PROC_PID, pid, &cnt); if (ki == NULL || cnt != 1) @@ -153,21 +166,21 @@ main(argc, argv) if (fd < 0) err(1, "%s", corefile); - efd = open(argv[0], O_RDONLY, 0); + efd = open(binfile, O_RDONLY, 0); if (efd < 0) - err(1, "%s", argv[0]); + err(1, "%s", binfile); cnt = read(efd, &exec, sizeof(exec)); if (cnt != sizeof(exec)) errx(1, "%s exec header: %s", - argv[0], cnt > 0 ? strerror(EIO) : strerror(errno)); + binfile, cnt > 0 ? strerror(EIO) : strerror(errno)); /* check the text segment size of the executable and the process */ if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) errx(1, "The executable %s does not belong to process %d!\n" "Text segment size (in bytes): executable %d, process %d", - argv[0], pid, exec.a_text, + binfile, pid, exec.a_text, ptoa(ki->kp_eproc.e_vm.vm_tsize)); data_offset = N_DATOFF(exec);