aboutsummaryrefslogtreecommitdiff
path: root/toys/other/pmap.c
blob: 1641c95c1d086310d3a2a0f2797687f524926cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* pmap.c - Reports the memory map of a process or processes.
 *
 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com>
 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
 *
 * No Standard.

USE_PMAP(NEWTOY(pmap, "<1xq", TOYFLAG_USR|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)
{
  char **optargs;

  for (optargs = toys.optargs; *optargs; optargs++) {
    pid_t pid = atolx(*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 xx = 0;

    snprintf(toybuf, sizeof(toybuf), "/proc/%u/cmdline", pid);
    line = readfile(toybuf, 0, 0);
    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",
        (int)(sizeof(long)*2)-4, ' ');

    // Loop through mappings
    for (;;) {
      int off, count;

      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<sscanf(line, "Pss: %lld", &pss)
            || 0<sscanf(line, "Private_Dirty: %lld", &dirty)
            || 0<sscanf(line, "Swap: %lld", &swap)) xx++;
        free(line);
        if (xx<3) continue;
        line = oldline;
        name = basename(name);
        xx = 0;
        printf("% 7lld %7lld %7lld ", pss, dirty, swap);
        tpss += pss;
        tdirty += dirty;
        tswap += swap;
      }

      xprintf("%s-  %s%s", toybuf, line[off]=='[' ? "  " : "", name);

      free(line);
      line = 0;
    }

    // Trailer
    if (!(toys.optflags & FLAG_q)) {
      int x = !!(toys.optflags & FLAG_x);
      if (x) {
        memset(toybuf, '-', 16);
        xprintf("%.*s  ------  ------  ------  ------\n", (int)(sizeof(long)*2),
          toybuf);
      }
      printf("total% *lld%s", 2*(int)(sizeof(long)+1)+x, total, k);
      if (x) printf("% 8lld% 8lld% 8lld", tpss, tdirty, tswap);
      xputc('\n');
    }
 
    fclose(fp);
  }
}