Bug 128842

Summary: ps(1) command output includes thread name only when it is the last field
Product: Base System Reporter: Ed Maste <emaste>
Component: binAssignee: attilio
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 8.0-CURRENT   
Hardware: Any   
OS: Any   

Description Ed Maste freebsd_committer freebsd_triage 2008-11-13 15:30:01 UTC
If the command (comm field) is included in ps output and is the last field,
both the process and thread names are displayed separated by a slash.  If
however it is not the last field, only the process name is shown.

[emaste@ref8-amd64 ~]$ ps -axH -o pid,lwp,comm | head
  PID    LWP COMMAND
    0 100000 kernel/swapper
    0 100025 kernel/thread taskq
    0 100028 kernel/acpi_task_0
[...]

[emaste@ref8-amd64 ~]$ ps -axH -o comm,pid,lwp | head
COMMAND               PID    LWP
kernel                  0 100000
kernel                  0 100025
kernel                  0 100028
[...]

How-To-Repeat: Run ps with comm as a -o flag and not the last column.
Comment 1 Ed Maste freebsd_committer freebsd_triage 2008-11-13 15:31:55 UTC
Responsible Changed
From-To: freebsd-bugs->emaste

Hang on to my PR
Comment 2 dfilter service freebsd_committer freebsd_triage 2009-02-26 18:01:22 UTC
Author: attilio
Date: Thu Feb 26 18:01:07 2009
New Revision: 189078
URL: http://svn.freebsd.org/changeset/base/189078

Log:
  [1]	When showing threads, the thread name just appears if the comm
  	label is choosen as last printout (ucomm suffers of this such bug
  	too).  That bug is caused by the fact that the fixed size of
  	printout doesn't leave enough space for them to be printed out.
  	Implement ucomm and comm commands with a dynamic size lenght for
  	buffers.
  
  [2]	On AMD64 architecture pointers don't have enough chars space to
  	be shown (8 chars while they need 16).  Fix them by providing
  	a variadic space so that it fits well on both 64 and 32 bits
  	architectures.
  
  [3]	Check a return value of malloc() that wasn't checked before.
  
  PR:		bin/128841, bin/128842
  Reviewed by:	jhb, emaste
  Sponsored by:	Sandvine Incorporated

Modified:
  head/bin/ps/extern.h
  head/bin/ps/keyword.c
  head/bin/ps/print.c

Modified: head/bin/ps/extern.h
==============================================================================
--- head/bin/ps/extern.h	Thu Feb 26 17:46:54 2009	(r189077)
+++ head/bin/ps/extern.h	Thu Feb 26 18:01:07 2009	(r189078)
@@ -71,6 +71,7 @@ void	 priorityr(KINFO *, VARENT *);
 void	 rgroupname(KINFO *, VARENT *);
 void	 runame(KINFO *, VARENT *);
 void	 rvar(KINFO *, VARENT *);
+int	 s_comm(KINFO *);
 int	 s_label(KINFO *);
 int	 s_rgroupname(KINFO *);
 int	 s_runame(KINFO *);

Modified: head/bin/ps/keyword.c
==============================================================================
--- head/bin/ps/keyword.c	Thu Feb 26 17:46:54 2009	(r189077)
+++ head/bin/ps/keyword.c	Thu Feb 26 18:01:07 2009	(r189078)
@@ -79,8 +79,8 @@ static VAR var[] = {
 		CHAR, NULL, 0},
 	{"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
 	{"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
-	{"comm", "COMMAND", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR,
-		NULL, 0},
+	{"comm", "COMMAND", NULL, LJUST|DSIZ, ucomm, s_comm,
+		COMMLEN + OCOMMLEN + 1, 0, CHAR, NULL, 0},
 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0,
 		CHAR, NULL, 0},
 	{"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d",
@@ -135,12 +135,13 @@ static VAR var[] = {
 		LONG, "ld", 0},
 	{"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw),
 		LONG, "ld", 0},
-	{"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, 8, 0, CHAR, NULL, 0},
+	{"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, sizeof(void *) * 2, 0,
+		CHAR, NULL, 0},
 	{"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock),
 		LONG, "ld", 0},
 	{"oublock", "", "oublk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
-	{"paddr", "PADDR", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR,
-		"lx", 0},
+	{"paddr", "PADDR", NULL, 0, kvar, NULL, sizeof(void *) * 2,
+		KOFF(ki_paddr), KPTR, "lx", 0},
 	{"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, CHAR, NULL, 0},
 	{"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
 	{"pending", "", "sig", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
@@ -195,13 +196,13 @@ static VAR var[] = {
 	{"tsiz", "TSIZ", NULL, 0, kvar, NULL, 4, KOFF(ki_tsize), PGTOK, "ld", 0},
 	{"tt", "TT ", NULL, 0, tname, NULL, 4, 0, CHAR, NULL, 0},
 	{"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, CHAR, NULL, 0},
-	{"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, NULL,
-		0},
+	{"ucomm", "UCOMM", NULL, LJUST|DSIZ, ucomm, s_comm,
+		COMMLEN + OCOMMLEN + 1, 0, CHAR, NULL, 0},
 	{"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), UINT,
 		UIDFMT, 0},
 	{"upr", "UPR", NULL, 0, upr, NULL, 3, 0, CHAR, NULL, 0},
-	{"uprocp", "UPROCP", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR,
-		"lx", 0},
+	{"uprocp", "UPROCP", NULL, 0, kvar, NULL, sizeof(void *) * 2,
+		KOFF(ki_paddr), KPTR, "lx", 0},
 	{"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, CHAR,
 		NULL, 0},
 	{"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
@@ -325,6 +326,8 @@ findvar(char *p, int user, char **header
 			 */
 			rflen = strlen(v->alias) + strlen(hp) + 2;
 			realfmt = malloc(rflen);
+			if (realfmt == NULL)
+				errx(1, "malloc failed");
 			snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
 			parsefmt(realfmt, user);
 		}

Modified: head/bin/ps/print.c
==============================================================================
--- head/bin/ps/print.c	Thu Feb 26 17:46:54 2009	(r189077)
+++ head/bin/ps/print.c	Thu Feb 26 18:01:07 2009	(r189078)
@@ -177,6 +177,7 @@ command(KINFO *k, VARENT *ve)
 void
 ucomm(KINFO *k, VARENT *ve)
 {
+	char tmpbuff[COMMLEN + OCOMMLEN + 2];
 	VAR *v;
 
 	v = ve->var;
@@ -184,8 +185,15 @@ ucomm(KINFO *k, VARENT *ve)
 		(void)printf("%s", k->ki_p->ki_comm);
 		if (showthreads && k->ki_p->ki_numthreads > 1)
 			printf("/%s", k->ki_p->ki_ocomm);
-	} else
-		(void)printf("%-*s", v->width, k->ki_p->ki_comm);
+	} else {
+		bzero(tmpbuff, sizeof(tmpbuff));
+		if (showthreads && k->ki_p->ki_numthreads > 1)
+			sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
+			    k->ki_p->ki_ocomm);
+		else
+			sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
+		(void)printf("%-*s", v->width, tmpbuff);
+	}
 }
 
 void
@@ -821,6 +829,20 @@ out:
 }
 
 int
+s_comm(KINFO *k)
+{
+	char tmpbuff[COMMLEN + OCOMMLEN + 2];
+
+	bzero(tmpbuff, sizeof(tmpbuff));
+	if (showthreads && k->ki_p->ki_numthreads > 1)
+		sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
+		    k->ki_p->ki_ocomm);
+	else
+		sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
+	return (strlen(tmpbuff));
+}
+
+int
 s_label(KINFO *k)
 {
 	char *string = NULL;
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 3 Ed Maste freebsd_committer freebsd_triage 2009-03-09 17:16:00 UTC
Responsible Changed
From-To: emaste->attilio

Attilio is working on these issues for me.
Comment 4 Gavin Atkinson freebsd_committer freebsd_triage 2009-03-26 12:32:37 UTC
State Changed
From-To: open->patched

THis is fixed in HEAD but not MFC'd
Comment 5 Sergey Kandaurov freebsd_committer freebsd_triage 2011-07-15 14:22:17 UTC
State Changed
From-To: patched->closed

Merged to stable/7 and stable/6.