annotate toys/other/pmap.c @ 1736:5892daac85ab draft

Switch nsenter to default y.
author Rob Landley <rob@landley.net>
date Thu, 12 Mar 2015 15:34:03 -0500
parents 3ac823675413
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
1 /* pmap.c - Reports the memory map of a process or processes.
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
2 *
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
3 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com>
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
5 *
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
6 * No Standard.
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
7
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
8 USE_PMAP(NEWTOY(pmap, "<1xq", TOYFLAG_BIN))
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
9
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
10 config PMAP
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
11 bool "pmap"
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
12 default y
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
13 help
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
14 usage: pmap [-xq] [pids...]
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
15
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
16 Reports the memory map of a process or processes.
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
17
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
18 -x Show the extended format.
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
19 -q Do not display some header/footer lines.
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
20 */
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
21
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
22 #define FOR_pmap
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
23 #include "toys.h"
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
24
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
25 void pmap_main(void)
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
26 {
1150
ac4a0cde89c2 Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents: 1043
diff changeset
27 char **optargs;
ac4a0cde89c2 Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents: 1043
diff changeset
28
ac4a0cde89c2 Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents: 1043
diff changeset
29 for (optargs = toys.optargs; *optargs; optargs++) {
ac4a0cde89c2 Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents: 1043
diff changeset
30 pid_t pid = atolx(*optargs);
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
31 FILE *fp;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
32 char *line, *oldline = 0, *name = 0,
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
33 *k = (toys.optflags & FLAG_x) ? "" : "K";
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
34 size_t len;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
35 long long start, end, pss, tpss = 0, dirty, tdirty = 0, swap, tswap = 0,
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
36 total = 0;
1202
4f080cdb2f6e Various cleanups found by Tom Sparrow's static analysis.
Rob Landley <rob@landley.net>
parents: 1150
diff changeset
37 int xx = 0;
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
38
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
39 snprintf(toybuf, sizeof(toybuf), "/proc/%u/cmdline", pid);
1043
acf7bb2b99e2 Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents: 1030
diff changeset
40 line = readfile(toybuf, 0, 0);
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
41 if (!line) error_msg("No %lu", (long)pid);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
42 xprintf("%u: %s\n", (int)pid, line);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
43 free(line);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
44
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
45 // Header
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
46 // Only use the more verbose file in -x mode
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
47 sprintf(toybuf, "/proc/%u/%smaps", pid,
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
48 (toys.optflags & FLAG_x) ? "s" : "");
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
49 if (!(fp = fopen(toybuf, "r"))) {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
50 error_msg("No %ld\n", (long)pid);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
51 return;
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
52 }
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
53
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
54 if ((toys.optflags & (FLAG_q|FLAG_x)) == FLAG_x)
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
55 xprintf("Address%*cKbytes PSS Dirty Swap Mode Mapping\n",
1718
3ac823675413 Fix several printf_format warnings.
Rob Landley <rob@landley.net>
parents: 1202
diff changeset
56 (int)(sizeof(long)*2)-4, ' ');
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
57
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
58 // Loop through mappings
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
59 for (;;) {
1202
4f080cdb2f6e Various cleanups found by Tom Sparrow's static analysis.
Rob Landley <rob@landley.net>
parents: 1150
diff changeset
60 int off, count;
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
61
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
62 line = 0;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
63 if (0 >= getline(&line, &len, fp)) break;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
64 count = sscanf(line, "%llx-%llx %s %*s %*s %*s %n",
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
65 &start, &end, toybuf, &off);
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
66
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
67 if (count == 3) {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
68 name = line[off] ? line+off : " [anon]\n";
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
69 if (toybuf[3] == 'p') toybuf[3] = '-';
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
70 total += end = (end-start)/1024;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
71 printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start,
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
72 6+!!(toys.optflags & FLAG_x), end, k);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
73 if (toys.optflags & FLAG_x) {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
74 oldline = line;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
75 continue;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
76 }
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
77 } else {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
78 if (0<sscanf(line, "Pss: %lld", &pss)
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
79 || 0<sscanf(line, "Private_Dirty: %lld", &dirty)
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
80 || 0<sscanf(line, "Swap: %lld", &swap)) xx++;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
81 free(line);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
82 if (xx<3) continue;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
83 line = oldline;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
84 name = basename(name);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
85 xx = 0;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
86 printf("% 7lld %7lld %7lld ", pss, dirty, swap);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
87 tpss += pss;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
88 tdirty += dirty;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
89 tswap += swap;
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
90 }
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
91
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
92 xprintf("%s- %s%s", toybuf, line[off]=='[' ? " " : "", name);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
93
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
94 free(line);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
95 line = 0;
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
96 }
1030
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
97
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
98 // Trailer
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
99 if (!(toys.optflags & FLAG_q)) {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
100 int x = !!(toys.optflags & FLAG_x);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
101 if (x) {
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
102 memset(toybuf, '-', 16);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
103 xprintf("%.*s ------ ------ ------ ------\n", (int)(sizeof(long)*2),
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
104 toybuf);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
105 }
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
106 printf("total% *lld%s", 2*(int)(sizeof(long)+1)+x, total, k);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
107 if (x) printf("% 8lld% 8lld% 8lld", tpss, tdirty, tswap);
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
108 xputc('\n');
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
109 }
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
110
9686469a857a Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents: 1029
diff changeset
111 fclose(fp);
1029
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
112 }
b59f4b87b9ec Add pmap.
Ashwini Sharma <ak.ashwini@gmail.com>
parents:
diff changeset
113 }