12 typedef struct mapinfo mapinfo;
21 unsigned shared_clean;
22 unsigned shared_dirty;
23 unsigned private_clean;
24 unsigned private_dirty;
30 static int is_library(const char *name) {
31 int len = strlen(name);
32 return len >= 4 && name[0] == '/'
33 && name[len - 3] == '.' && name[len - 2] == 's' && name[len - 1] == 'o';
36 // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /android/lib/libcomposer.so
37 // 012345678901234567890123456789012345678901234567890123456789
40 static int parse_header(const char* line, const mapinfo* prev, mapinfo** mi) {
47 if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
52 while (isspace(line[name_pos])) {
57 strlcpy(name, line + name_pos, sizeof(name));
59 if (prev && start == prev->end && is_library(prev->name)) {
60 // anonymous mappings immediately adjacent to shared libraries
61 // usually correspond to the library BSS segment, so we use the
63 strlcpy(name, prev->name, sizeof(name));
66 strlcpy(name, "[anon]", sizeof(name));
70 const int name_size = strlen(name) + 1;
71 struct mapinfo* info = calloc(1, sizeof(mapinfo) + name_size);
73 fprintf(stderr, "out of memory\n");
79 info->is_bss = is_bss;
81 strlcpy(info->name, name, name_size);
87 static int parse_field(mapinfo* mi, const char* line) {
91 if (sscanf(line, "%63s %n", field, &len) == 1
92 && *field && field[strlen(field) - 1] == ':') {
94 if (sscanf(line + len, "%d kB", &size) == 1) {
95 if (!strcmp(field, "Size:")) {
97 } else if (!strcmp(field, "Rss:")) {
99 } else if (!strcmp(field, "Pss:")) {
101 } else if (!strcmp(field, "Shared_Clean:")) {
102 mi->shared_clean = size;
103 } else if (!strcmp(field, "Shared_Dirty:")) {
104 mi->shared_dirty = size;
105 } else if (!strcmp(field, "Private_Clean:")) {
106 mi->private_clean = size;
107 } else if (!strcmp(field, "Private_Dirty:")) {
108 mi->private_dirty = size;
116 static int order_before(const mapinfo *a, const mapinfo *b, int sort_by_address) {
117 if (sort_by_address) {
118 return a->start < b->start
119 || (a->start == b->start && a->end < b->end);
121 return strcmp(a->name, b->name) < 0;
125 static void enqueue_map(mapinfo **head, mapinfo *map, int sort_by_address, int coalesce_by_name) {
126 mapinfo *prev = NULL;
127 mapinfo *current = *head;
134 if (current && coalesce_by_name && !strcmp(map->name, current->name)) {
135 current->size += map->size;
136 current->rss += map->rss;
137 current->pss += map->pss;
138 current->shared_clean += map->shared_clean;
139 current->shared_dirty += map->shared_dirty;
140 current->private_clean += map->private_clean;
141 current->private_dirty += map->private_dirty;
142 current->is_bss &= map->is_bss;
148 if (!current || order_before(map, current, sort_by_address)) {
159 current = current->next;
163 static mapinfo *load_maps(int pid, int sort_by_address, int coalesce_by_name)
168 mapinfo *head = NULL;
169 mapinfo *current = NULL;
172 snprintf(fn, sizeof(fn), "/proc/%d/smaps", pid);
175 fprintf(stderr, "cannot open /proc/%d/smaps: %s\n", pid, strerror(errno));
179 while (fgets(line, sizeof(line), fp) != 0) {
181 if (line[len - 1] == '\n') {
185 if (current != NULL && !parse_field(current, line)) {
190 if (!parse_header(line, current, &next)) {
191 enqueue_map(&head, current, sort_by_address, coalesce_by_name);
196 fprintf(stderr, "warning: could not parse map info line: %s\n", line);
199 enqueue_map(&head, current, sort_by_address, coalesce_by_name);
204 fprintf(stderr, "could not read /proc/%d/smaps\n", pid);
211 static int verbose = 0;
212 static int terse = 0;
213 static int addresses = 0;
215 static void print_header()
218 printf(" start end ");
220 printf(" virtual shared shared private private\n");
223 printf(" addr addr ");
225 printf(" size RSS PSS clean dirty clean dirty ");
226 if (!verbose && !addresses) {
232 static void print_divider()
235 printf("-------- -------- ");
237 printf("-------- -------- -------- -------- -------- -------- -------- ");
238 if (!verbose && !addresses) {
241 printf("------------------------------\n");
244 static int show_map(int pid)
248 unsigned shared_dirty = 0;
249 unsigned shared_clean = 0;
250 unsigned private_dirty = 0;
251 unsigned private_clean = 0;
257 milist = load_maps(pid, addresses, !verbose && !addresses);
258 if (milist == NULL) {
265 for (mi = milist; mi;) {
268 shared_clean += mi->shared_clean;
269 shared_dirty += mi->shared_dirty;
270 private_clean += mi->private_clean;
271 private_dirty += mi->private_dirty;
277 if (terse && !mi->private_dirty) {
282 printf("%08x %08x ", mi->start, mi->end);
284 printf("%8d %8d %8d %8d %8d %8d %8d ", mi->size,
287 mi->shared_clean, mi->shared_dirty,
288 mi->private_clean, mi->private_dirty);
289 if (!verbose && !addresses) {
290 printf("%4d ", mi->count);
292 printf("%s%s\n", mi->name, mi->is_bss ? " [bss]" : "");
306 printf("%8d %8d %8d %8d %8d %8d %8d ", size,
308 shared_clean, shared_dirty,
309 private_clean, private_dirty);
310 if (!verbose && !addresses) {
311 printf("%4d ", count);
318 int main(int argc, char *argv[])
326 signal(SIGPIPE, SIG_IGN);
327 for (argc--, argv++; argc > 0; argc--, argv++) {
329 if (!strcmp(arg,"-v")) {
333 if (!strcmp(arg,"-t")) {
337 if (!strcmp(arg,"-a")) {
342 fprintf(stderr, "too many arguments\n");
345 pid = strtol(arg, &argend, 10);
346 if (*arg && !*argend) {
353 fprintf(stderr, "unrecognized argument: %s\n", arg);
359 "showmap [-t] [-v] [-c] <pid>\n"
360 " -t = terse (show only items with private pages)\n"
361 " -v = verbose (don't coalesce maps with the same name)\n"
362 " -a = addresses (show virtual memory map)\n"