From 455865a837f2a6c44f91e5a5a2cd40c3a64d4b68 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 27 Aug 2013 23:48:54 -0500 Subject: Rewrite pmap to be simpler and match other implementation's output more closely. --- toys/other/pmap.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 toys/other/pmap.c (limited to 'toys/other') diff --git a/toys/other/pmap.c b/toys/other/pmap.c new file mode 100644 index 00000000..012280dd --- /dev/null +++ b/toys/other/pmap.c @@ -0,0 +1,111 @@ +/* pmap.c - Reports the memory map of a process or processes. + * + * Copyright 2013 Ranjan Kumar + * Copyright 2013 Kyungwan Han + * + * No Standard. + +USE_PMAP(NEWTOY(pmap, "<1xq", TOYFLAG_BIN)) + +config PMAP + bool "pmap" + default y + help + usage: pmap [-xq] [pids...] + + Reports the memory map of a process or processes. + + -x Show the extended format. + -q Do not display some header/footer lines. +*/ + +#define FOR_pmap +#include "toys.h" + +void pmap_main(void) +{ + while (*toys.optargs) { + pid_t pid = atolx(*toys.optargs++); + FILE *fp; + char *line, *oldline = 0, *name = 0, + *k = (toys.optflags & FLAG_x) ? "" : "K"; + size_t len; + long long start, end, pss, tpss = 0, dirty, tdirty = 0, swap, tswap = 0, + total = 0; + int count, xx = 0; + + snprintf(toybuf, sizeof(toybuf), "/proc/%u/cmdline", pid); + line = readfile(toybuf); + if (!line) error_msg("No %lu", (long)pid); + xprintf("%u: %s\n", (int)pid, line); + free(line); + + // Header + // Only use the more verbose file in -x mode + sprintf(toybuf, "/proc/%u/%smaps", pid, + (toys.optflags & FLAG_x) ? "s" : ""); + if (!(fp = fopen(toybuf, "r"))) { + error_msg("No %ld\n", (long)pid); + return; + } + + if ((toys.optflags & (FLAG_q|FLAG_x)) == FLAG_x) + xprintf("Address%*cKbytes PSS Dirty Swap Mode Mapping\n", + (sizeof(long)*2)-4, ' '); + + // Loop through mappings + for (;;) { + int off; + + line = 0; + if (0 >= getline(&line, &len, fp)) break; + count = sscanf(line, "%llx-%llx %s %*s %*s %*s %n", + &start, &end, toybuf, &off); + + if (count == 3) { + name = line[off] ? line+off : " [anon]\n"; + if (toybuf[3] == 'p') toybuf[3] = '-'; + total += end = (end-start)/1024; + printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start, + 6+!!(toys.optflags & FLAG_x), end, k); + if (toys.optflags & FLAG_x) { + oldline = line; + continue; + } + } else { + if (0