diff options
author | des <des@FreeBSD.org> | 1998-09-14 10:09:30 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 1998-09-14 10:09:30 +0000 |
commit | efe6cafb5de5d87d7ed69ddd998cbc6d33de913b (patch) | |
tree | 682f176c63a8721af6a67452f90c3cf8f81c2db4 /usr.bin/gcore | |
parent | 3e5a3a2a28e4c102e4f8b124073a84c3f1bac8ce (diff) | |
download | FreeBSD-src-efe6cafb5de5d87d7ed69ddd998cbc6d33de913b.zip FreeBSD-src-efe6cafb5de5d87d7ed69ddd998cbc6d33de913b.tar.gz |
Don't require an executable file name. If no executable image is
specified, use /proc/<pid>/file. Document it.
PR: bin/7915
Suggested-By: Wolfram Schneider <wosch@panke.de.freebsd.org>
Diffstat (limited to 'usr.bin/gcore')
-rw-r--r-- | usr.bin/gcore/aoutcore.c | 29 | ||||
-rw-r--r-- | usr.bin/gcore/gcore.1 | 20 | ||||
-rw-r--r-- | usr.bin/gcore/gcore.c | 29 |
3 files changed, 52 insertions, 26 deletions
diff --git a/usr.bin/gcore/aoutcore.c b/usr.bin/gcore/aoutcore.c index 46ef21e..a943a30 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 2e1d5f6..8a66e88 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.<pid> . -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/<pid>/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.<pid> The core image. +.It Pa /proc/<pid>/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 46ef21e..a943a30 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); |