Mercurial > hg > toybox
comparison toys/other/pmap.c @ 1030:9686469a857a draft
Rewrite pmap to be simpler and match other implementation's output more closely.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 27 Aug 2013 23:48:54 -0500 |
parents | toys/pending/pmap.c@b59f4b87b9ec |
children | acf7bb2b99e2 |
comparison
equal
deleted
inserted
replaced
1029:b59f4b87b9ec | 1030:9686469a857a |
---|---|
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. | |
7 | |
8 USE_PMAP(NEWTOY(pmap, "<1xq", TOYFLAG_BIN)) | |
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 */ | |
21 | |
22 #define FOR_pmap | |
23 #include "toys.h" | |
24 | |
25 void pmap_main(void) | |
26 { | |
27 while (*toys.optargs) { | |
28 pid_t pid = atolx(*toys.optargs++); | |
29 FILE *fp; | |
30 char *line, *oldline = 0, *name = 0, | |
31 *k = (toys.optflags & FLAG_x) ? "" : "K"; | |
32 size_t len; | |
33 long long start, end, pss, tpss = 0, dirty, tdirty = 0, swap, tswap = 0, | |
34 total = 0; | |
35 int count, xx = 0; | |
36 | |
37 snprintf(toybuf, sizeof(toybuf), "/proc/%u/cmdline", pid); | |
38 line = readfile(toybuf); | |
39 if (!line) error_msg("No %lu", (long)pid); | |
40 xprintf("%u: %s\n", (int)pid, line); | |
41 free(line); | |
42 | |
43 // Header | |
44 // Only use the more verbose file in -x mode | |
45 sprintf(toybuf, "/proc/%u/%smaps", pid, | |
46 (toys.optflags & FLAG_x) ? "s" : ""); | |
47 if (!(fp = fopen(toybuf, "r"))) { | |
48 error_msg("No %ld\n", (long)pid); | |
49 return; | |
50 } | |
51 | |
52 if ((toys.optflags & (FLAG_q|FLAG_x)) == FLAG_x) | |
53 xprintf("Address%*cKbytes PSS Dirty Swap Mode Mapping\n", | |
54 (sizeof(long)*2)-4, ' '); | |
55 | |
56 // Loop through mappings | |
57 for (;;) { | |
58 int off; | |
59 | |
60 line = 0; | |
61 if (0 >= getline(&line, &len, fp)) break; | |
62 count = sscanf(line, "%llx-%llx %s %*s %*s %*s %n", | |
63 &start, &end, toybuf, &off); | |
64 | |
65 if (count == 3) { | |
66 name = line[off] ? line+off : " [anon]\n"; | |
67 if (toybuf[3] == 'p') toybuf[3] = '-'; | |
68 total += end = (end-start)/1024; | |
69 printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start, | |
70 6+!!(toys.optflags & FLAG_x), end, k); | |
71 if (toys.optflags & FLAG_x) { | |
72 oldline = line; | |
73 continue; | |
74 } | |
75 } else { | |
76 if (0<sscanf(line, "Pss: %lld", &pss) | |
77 || 0<sscanf(line, "Private_Dirty: %lld", &dirty) | |
78 || 0<sscanf(line, "Swap: %lld", &swap)) xx++; | |
79 free(line); | |
80 if (xx<3) continue; | |
81 line = oldline; | |
82 name = basename(name); | |
83 xx = 0; | |
84 printf("% 7lld %7lld %7lld ", pss, dirty, swap); | |
85 tpss += pss; | |
86 tdirty += dirty; | |
87 tswap += swap; | |
88 } | |
89 | |
90 xprintf("%s- %s%s", toybuf, line[off]=='[' ? " " : "", name); | |
91 | |
92 free(line); | |
93 line = 0; | |
94 } | |
95 | |
96 // Trailer | |
97 if (!(toys.optflags & FLAG_q)) { | |
98 int x = !!(toys.optflags & FLAG_x); | |
99 if (x) { | |
100 memset(toybuf, '-', 16); | |
101 xprintf("%.*s ------ ------ ------ ------\n", (int)(sizeof(long)*2), | |
102 toybuf); | |
103 } | |
104 printf("total% *lld%s", 2*(int)(sizeof(long)+1)+x, total, k); | |
105 if (x) printf("% 8lld% 8lld% 8lld", tpss, tdirty, tswap); | |
106 xputc('\n'); | |
107 } | |
108 | |
109 fclose(fp); | |
110 } | |
111 } |