Mercurial > hg > toybox
annotate toys/other/pmap.c @ 1037:af1780148f7c draft
Implement ls --color=auto, suggested by Rich Felker.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 01 Sep 2013 08:00:41 -0500 |
parents | 9686469a857a |
children | acf7bb2b99e2 |
rev | line source |
---|---|
1029 | 1 /* pmap.c - Reports the memory map of a process or processes. |
2 * | |
3 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com> | |
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> | |
5 * | |
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 | 9 |
10 config PMAP | |
11 bool "pmap" | |
12 default y | |
13 help | |
14 usage: pmap [-xq] [pids...] | |
15 | |
16 Reports the memory map of a process or processes. | |
17 | |
18 -x Show the extended format. | |
19 -q Do not display some header/footer lines. | |
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 | 22 #define FOR_pmap |
23 #include "toys.h" | |
24 | |
25 void pmap_main(void) | |
26 { | |
27 while (*toys.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
|
28 pid_t pid = atolx(*toys.optargs++); |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
29 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
|
30 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
|
31 *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
|
32 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
|
33 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
|
34 total = 0; |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
35 int count, 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
|
36 |
1029 | 37 snprintf(toybuf, sizeof(toybuf), "/proc/%u/cmdline", pid); |
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 line = readfile(toybuf); |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
43 // Header |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
44 // 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
|
45 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
|
46 (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
|
47 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
|
48 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
|
49 return; |
1029 | 50 } |
1030
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
51 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
52 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
|
53 xprintf("Address%*cKbytes PSS Dirty Swap Mode Mapping\n", |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
54 (sizeof(long)*2)-4, ' '); |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
55 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
56 // 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
|
57 for (;;) { |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
58 int off; |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
59 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 &start, &end, toybuf, &off); |
1029 | 64 |
1030
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 continue; |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
74 } |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
75 } else { |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
76 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
|
77 || 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
|
78 || 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 } |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
89 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
90 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
|
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 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
|
93 line = 0; |
1029 | 94 } |
1030
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
95 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
96 // Trailer |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 toybuf); |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
103 } |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 } |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
108 |
9686469a857a
Rewrite pmap to be simpler and match other implementation's output more closely.
Rob Landley <rob@landley.net>
parents:
1029
diff
changeset
|
109 fclose(fp); |
1029 | 110 } |
111 } |