ustat: Replace \0 with spaces in cmdline
authorSasha Goldshtein <goldshtn@gmail.com>
Thu, 27 Oct 2016 22:17:58 +0000 (15:17 -0700)
committerSasha Goldshtein <goldshtn@gmail.com>
Mon, 19 Dec 2016 09:46:05 +0000 (09:46 +0000)
The /proc/PID/cmdline file has \0 for spaces in the command
line, and there may be trailing \0 characters as well.
Replace them all with spaces.

tools/ustat.py

index 75e33f8..55e8493 100755 (executable)
@@ -55,7 +55,7 @@ class Probe(object):
                 comm = open('/proc/%d/comm' % pid).read().strip()
                 if comm in self.procnames:
                     cmdline = open('/proc/%d/cmdline' % pid).read()
-                    self.targets[pid] = cmdline
+                    self.targets[pid] = cmdline.replace('\0', ' ')
             except IOError:
                 continue    # process may already have terminated
 
@@ -220,8 +220,9 @@ class Tool(object):
             print()
         with open("/proc/loadavg") as stats:
             print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read()))
-        print("%-6s %-16s %-10s %-6s %-10s %-8s %-8s %-10s" % ("PID", "CMDLINE",
-            "METHOD/s", "GC/s", "OBJNEW/s", "CLOAD/s", "EXCP/s", "THREAD/s"))
+        print("%-6s %-20s %-10s %-6s %-10s %-8s %-6s %-6s" % (
+            "PID", "CMDLINE", "METHOD/s", "GC/s", "OBJNEW/s",
+            "CLOAD/s", "EXC/s", "THR/s"))
 
         line = 0
         counts = {}
@@ -235,8 +236,8 @@ class Tool(object):
         else:
             counts = sorted(counts.items(), key=lambda (k, _): k)
         for pid, stats in counts:
-            print("%-6s %-16s %-10d %-6d %-10d %-8d %-8d %-10d" % (
-                pid, targets[pid][0:16],
+            print("%-6d %-20s %-10d %-6d %-10d %-8d %-6d %-6d" % (
+                  pid, targets[pid][:20],
                   stats.get(Category.METHOD, 0) / self.args.interval,
                   stats.get(Category.GC, 0) / self.args.interval,
                   stats.get(Category.OBJNEW, 0) / self.args.interval,