From wosch@panke.de.freebsd.org  Sun Sep 13 02:20:47 1998
Received: from baerenklau.de.freebsd.org (baerenklau.de.freebsd.org [195.185.195.14])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id CAA06382
          for <FreeBSD-gnats-submit@freebsd.org>; Sun, 13 Sep 1998 02:20:45 -0700 (PDT)
          (envelope-from wosch@panke.de.freebsd.org)
Received: (from uucp@localhost)
	by baerenklau.de.freebsd.org (8.8.8/8.8.8) with UUCP id LAA00597
	for FreeBSD-gnats-submit@freebsd.org; Sun, 13 Sep 1998 11:20:31 +0200 (CEST)
	(envelope-from wosch@panke.de.freebsd.org)
Received: (from wosch@localhost)
	by campa.panke.de (8.8.8/8.8.8) id LAA00970;
	Sun, 13 Sep 1998 11:19:21 +0200 (MET DST)
	(envelope-from wosch)
Message-Id: <199809130919.LAA00970@campa.panke.de>
Date: Sun, 13 Sep 1998 11:19:21 +0200 (MET DST)
From: Wolfram Schneider <wosch@panke.de.freebsd.org>
Reply-To: wosch@panke.de.freebsd.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: gcore(1) exec argument
X-Send-Pr-Version: 3.2

>Number:         7915
>Category:       bin
>Synopsis:       gcore(1) exec argument
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Sep 13 02:30:05 PDT 1998
>Closed-Date:    Mon Sep 14 03:09:58 PDT 1998
>Last-Modified:  Mon Sep 14 03:11:28 PDT 1998
>Originator:     Wolfram Schneider
>Release:        FreeBSD 2.2.6-STABLE i386
>Organization:
>Environment:

Gcore expect 2 arguments, the file name of running binary (exec)
and the process identifier (pid).
 
        gcore [-s] [-c core] exec pid


With a running process filesystem /proc the exec argument is 
not necessary anymore. gcore should just read the text 
segment from /proc/<pid>/file.

Wolfram
>Description:
>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: des 
State-Changed-When: Sun Sep 13 05:45:18 PDT 1998 
State-Changed-Why:  
Patch sent to originator. 


Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: des 
Responsible-Changed-When: Sun Sep 13 05:45:18 PDT 1998 
Responsible-Changed-Why:  
I'll handle it. 

From: dag-erli@ifi.uio.no (Dag-Erling =?iso-8859-1?Q?Co=EFdan?=  =?iso-8859-1?Q?Sm=F8rgrav?= )
To: wosch@panke.de.freebsd.org
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/7915: gcore(1) exec argument
Date: 13 Sep 1998 14:45:12 +0200

 Wolfram Schneider <wosch@panke.de.freebsd.org> writes:
 > With a running process filesystem /proc the exec argument is 
 > not necessary anymore. gcore should just read the text 
 > segment from /proc/<pid>/file.
 
 Would this patch be satisfactory?
 
 Index: gcore.1
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/gcore/gcore.1,v
 retrieving revision 1.4
 diff -u -r1.4 gcore.1
 --- gcore.1	1997/07/08 11:04:16	1.4
 +++ gcore.1	1998/09/13 12:44:09
 @@ -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 @@
  .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 @@
  .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 @@
  is not compatible with the original
  .Bx 4.2 
  version.
 -In particular,
 -.Bx 4.4
 -requires the 
 -.Ar exec
 -argument.
 Index: gcore.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/gcore/gcore.c,v
 retrieving revision 1.8
 diff -u -r1.8 gcore.c
 --- gcore.c	1998/08/24 16:25:30	1.8
 +++ gcore.c	1998/09/13 12:44:09
 @@ -99,7 +99,8 @@
  	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 @@
  	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 @@
  	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);
 
 DES
 -- 
 Dag-Erling Smrgrav - dag-erli@ifi.uio.no
State-Changed-From-To: feedback->closed 
State-Changed-By: des 
State-Changed-When: Mon Sep 14 03:09:58 PDT 1998 
State-Changed-Why:  
Patch committed to -current. 
>Unformatted:
