Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Oct 2016 21:17:18 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r306786 - in stable: 10/sys/kern 10/usr.bin/gcore 11/sys/kern 11/usr.bin/gcore
Message-ID:  <201610062117.u96LHIen076994@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Thu Oct  6 21:17:18 2016
New Revision: 306786
URL: https://svnweb.freebsd.org/changeset/base/306786

Log:
  MFC 302859: Include command line arguments in core dump process info.
  
  Fill in pr_psargs in the NT_PRSINFO ELF core dump note with command
  line arguments.

Modified:
  stable/11/sys/kern/imgact_elf.c
  stable/11/usr.bin/gcore/elfcore.c
Directory Properties:
  stable/11/   (props changed)

Changes in other areas also in this revision:
Modified:
  stable/10/sys/kern/imgact_elf.c
  stable/10/usr.bin/gcore/elfcore.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/11/sys/kern/imgact_elf.c
==============================================================================
--- stable/11/sys/kern/imgact_elf.c	Thu Oct  6 20:37:23 2016	(r306785)
+++ stable/11/sys/kern/imgact_elf.c	Thu Oct  6 21:17:18 2016	(r306786)
@@ -1823,8 +1823,12 @@ typedef vm_offset_t elf_ps_strings_t;
 static void
 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
 {
+	struct sbuf sbarg;
+	size_t len;
+	char *cp, *end;
 	struct proc *p;
 	elf_prpsinfo_t *psinfo;
+	int error;
 
 	p = (struct proc *)arg;
 	if (sb != NULL) {
@@ -1833,13 +1837,43 @@ __elfN(note_prpsinfo)(void *arg, struct 
 		psinfo->pr_version = PRPSINFO_VERSION;
 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
-		/*
-		 * XXX - We don't fill in the command line arguments properly
-		 * yet.
-		 */
-		strlcpy(psinfo->pr_psargs, p->p_comm,
-		    sizeof(psinfo->pr_psargs));
-
+		PROC_LOCK(p);
+		if (p->p_args != NULL) {
+			len = sizeof(psinfo->pr_psargs) - 1;
+			if (len > p->p_args->ar_length)
+				len = p->p_args->ar_length;
+			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
+			PROC_UNLOCK(p);
+			error = 0;
+		} else {
+			_PHOLD(p);
+			PROC_UNLOCK(p);
+			sbuf_new(&sbarg, psinfo->pr_psargs,
+			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
+			error = proc_getargv(curthread, p, &sbarg);
+			PRELE(p);
+			if (sbuf_finish(&sbarg) == 0)
+				len = sbuf_len(&sbarg) - 1;
+			else
+				len = sizeof(psinfo->pr_psargs) - 1;
+			sbuf_delete(&sbarg);
+		}
+		if (error || len == 0)
+			strlcpy(psinfo->pr_psargs, p->p_comm,
+			    sizeof(psinfo->pr_psargs));
+		else {
+			KASSERT(len < sizeof(psinfo->pr_psargs),
+			    ("len is too long: %zu vs %zu", len,
+			    sizeof(psinfo->pr_psargs)));
+			cp = psinfo->pr_psargs;
+			end = cp + len - 1;
+			for (;;) {
+				cp = memchr(cp, '\0', end - cp);
+				if (cp == NULL)
+					break;
+				*cp = ' ';
+			}
+		}
 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
 		free(psinfo, M_TEMP);
 	}

Modified: stable/11/usr.bin/gcore/elfcore.c
==============================================================================
--- stable/11/usr.bin/gcore/elfcore.c	Thu Oct  6 20:37:23 2016	(r306785)
+++ stable/11/usr.bin/gcore/elfcore.c	Thu Oct  6 21:17:18 2016	(r306786)
@@ -548,6 +548,7 @@ readmap(pid_t pid)
 static void *
 elf_note_prpsinfo(void *arg, size_t *sizep)
 {
+	char *cp, *end;
 	pid_t pid;
 	elfcore_prpsinfo_t *psinfo;
 	struct kinfo_proc kip;
@@ -571,7 +572,20 @@ elf_note_prpsinfo(void *arg, size_t *siz
 	if (kip.ki_pid != pid)
 		err(1, "kern.proc.pid.%u", pid);
 	strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname));
-	strlcpy(psinfo->pr_psargs, psinfo->pr_fname, sizeof(psinfo->pr_psargs));
+	name[2] = KERN_PROC_ARGS;
+	len = sizeof(psinfo->pr_psargs) - 1;
+	if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) {
+		cp = psinfo->pr_psargs;
+		end = cp + len - 1;
+		for (;;) {
+			cp = memchr(cp, '\0', end - cp);
+			if (cp == NULL)
+				break;
+			*cp = ' ';
+		}
+	} else
+		strlcpy(psinfo->pr_psargs, kip.ki_comm,
+		    sizeof(psinfo->pr_psargs));
 
 	*sizep = sizeof(*psinfo);
 	return (psinfo);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201610062117.u96LHIen076994>