pmap: fix bogus {no such process} comm field text
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 24 Oct 2015 01:45:57 +0000 (03:45 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 24 Oct 2015 01:45:57 +0000 (03:45 +0200)
function                                             old     new   delta
read_cmdline                                         246     266     +20
procps_get_maps                                      196     193      -3
packed_usage                                       30413   30404      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 20/-12)              Total: 8 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/procps.c
procps/pmap.c

index 05eefe0da9d3118a46451e5ba8b6ede705c42798..4edc54d486202498f2759ea1ec5139febe5bd4f3 100644 (file)
@@ -588,12 +588,14 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
                                buf[sz] = ' ';
                        sz--;
                }
+               if (base[0] == '-') /* "-sh" (login shell)? */
+                       base++;
 
                /* If comm differs from argv0, prepend "{comm} ".
                 * It allows to see thread names set by prctl(PR_SET_NAME).
                 */
-               if (base[0] == '-') /* "-sh" (login shell)? */
-                       base++;
+               if (!comm)
+                       return;
                comm_len = strlen(comm);
                /* Why compare up to comm_len, not COMM_LEN-1?
                 * Well, some processes rewrite argv, and use _spaces_ there
@@ -612,7 +614,7 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
                        buf[col - 1] = '\0';
                }
        } else {
-               snprintf(buf, col, "[%s]", comm);
+               snprintf(buf, col, "[%s]", comm ? comm : "?");
        }
 }
 
index fd995a54d2e90183ddf1c63b012b94191d6e439e..aa221cfb89530971d740a9705daa55f8145ada3b 100644 (file)
@@ -20,7 +20,7 @@
 //usage:#define pmap_trivial_usage
 //usage:       "[-xq] PID"
 //usage:#define pmap_full_usage "\n\n"
-//usage:       "Display detailed process memory usage"
+//usage:       "Display process memory usage"
 //usage:     "\n"
 //usage:     "\n       -x      Show details"
 //usage:     "\n       -q      Quiet"
@@ -66,7 +66,7 @@ static int procps_get_maps(pid_t pid, unsigned opt)
        int ret;
        char buf[256];
 
-       read_cmdline(buf, sizeof(buf), pid, "no such process");
+       read_cmdline(buf, sizeof(buf), pid, NULL);
        printf("%u: %s\n", (int)pid, buf);
 
        if (!(opt & OPT_q) && (opt & OPT_x))