Skip to content

Commit

Permalink
ustat: Replace \0 with spaces in cmdline
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goldshtn committed Dec 19, 2016
1 parent e725b14 commit d8c7f47
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tools/ustat.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _find_targets(self):
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

Expand Down Expand Up @@ -220,8 +220,9 @@ def _loop_iter(self):
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 = {}
Expand All @@ -235,8 +236,8 @@ def _loop_iter(self):
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,
Expand Down

0 comments on commit d8c7f47

Please sign in to comment.