aboutsummaryrefslogtreecommitdiff
path: root/toys/other/pmap.c
blob: d222f0108347826f5fdb803dfc25b5ae7e6a9290 (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
/* 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.
 *
 * TODO: two passes so we can auto-size the columns?

USE_PMAP(NEWTOY(pmap, "<1pqx", TOYFLAG_USR|TOYFLAG_BIN))

config PMAP
  bool "pmap"
  default y
  help
    usage: pmap [-pqx] PID...

    Report the memory map of a process or processes.

    -q	Show full paths
    -q	Do not show header or footer
    -x	Show the extended format
*/

#define FOR_pmap
#include "toys.h"

void pmap_main(void)
{
  char **optargs, *line = 0;
  size_t len = 0;

  for (optargs = toys.optargs; *optargs; optargs++) {
    long long start, end, pss, tpss=0, dirty, tdirty=0, swap, tswap=0, total=0;
    char *name = 0, *k = FLAG(x) ? "" : "K", mode[5];
    pid_t pid = atolx(*optargs);
    int extras = 0, off, count;
    FILE *fp;

    sprintf(toybuf, "/proc/%u/cmdline", pid);
    if (!(name = readfile(toybuf, 0, 0))) {
      error_msg("no %s", toybuf);
      continue;
    }
    xprintf("%d: %s\n", pid, name);
    free(name);

    // Only bother scanning the more verbose smaps file in -x mode.
    sprintf(toybuf, "/proc/%u/%smaps", pid, "s"+!FLAG(x));
    if (!(fp = fopen(toybuf, "r"))) {
      error_msg("no %s", toybuf);
      continue;
    }

    if (FLAG(x) && !FLAG(q))
      xprintf("Address%*cKbytes     PSS   Dirty    Swap Mode  Mapping\n",
          (int)(sizeof(long)*2)-5, ' ');

    while (getline(&line, &len, fp) > 0) {
      count = sscanf(line, "%llx-%llx %4s %*s %*s %*s %n", &start, &end, mode,
          &off);
      if (count == 3) {
        name = line[off] ? line+off : "  [anon]\n";
        if (mode[3] == 'p') mode[3] = '-';
        total += end = (end-start)/1024;
        printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start, 6+!!FLAG(x),
            end, k);
        if (FLAG(x)) {
          strcpy(toybuf, name);
          name = toybuf;
          continue;
        }
      } else {
        if (sscanf(line, "Pss: %lld", &pss) ||
            sscanf(line, "Private_Dirty: %lld", &dirty) ||
            sscanf(line, "Swap: %lld", &swap)) extras++;
        if (extras==3) {
          printf("% 7lld %7lld %7lld ", pss, dirty, swap);
          tpss += pss;
          tdirty += dirty;
          tswap += swap;
          extras = 0;
        } else continue;
      }

      xprintf("%s- %s%s", mode, *name == '[' ? "  " : "",
              FLAG(p) ? name : basename(name));
    }

    if (!FLAG(q)) {
      if (FLAG(x)) {
        xprintf("----------------  ------  ------  ------  ------\n" +
            ((sizeof(long)==4)?8:0));
      }
      printf("total% *lld%s", 2*(int)(sizeof(long)+1)+!!FLAG(x), total, k);
      if (FLAG(x)) printf("% 8lld% 8lld% 8lld", tpss, tdirty, tswap);
      xputc('\n');
    }

    fclose(fp);
  }
  free(line);
}