2 * pmap implementation for busybox
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Written by Alexander Shishkin <virtuoso@slind.org>
7 * Licensed under GPLv2 or later, see the LICENSE file in this source tree
11 //config: bool "pmap (6 kb)"
14 //config: Display processes' memory mappings.
16 //applet:IF_PMAP(APPLET(pmap, BB_DIR_USR_BIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_PMAP) += pmap.o
20 //usage:#define pmap_trivial_usage
21 //usage: "[-xq] PID..."
22 //usage:#define pmap_full_usage "\n\n"
23 //usage: "Display process memory usage"
25 //usage: "\n -x Show details"
26 //usage: "\n -q Quiet"
30 #if ULONG_MAX == 0xffffffff
37 # define DASHES "--------"
40 #if ULLONG_MAX == 0xffffffff
51 static void print_smaprec(struct smaprec *currec, void *data)
53 unsigned opt = (uintptr_t)data;
55 printf("%0" AFMTLL "llx ", currec->smap_start);
58 printf("%7lu %7lu %7lu %7lu ",
61 currec->private_dirty,
64 printf("%7luK", currec->smap_size);
66 printf(" %.4s %s\n", currec->smap_mode, currec->smap_name);
69 static int procps_get_maps(pid_t pid, unsigned opt)
75 read_cmdline(buf, sizeof(buf), pid, NULL);
76 printf("%u: %s\n", (int)pid, buf);
78 if (!(opt & OPT_q) && (opt & OPT_x))
79 puts("Address" TABS " Kbytes PSS Dirty Swap Mode Mapping");
81 memset(&total, 0, sizeof(total));
83 ret = procps_read_smaps(pid, &total, print_smaprec, (void*)(uintptr_t)opt);
89 printf("--------" DASHES " ------ ------ ------ ------\n"
90 "total" TABS " %7lu %7lu %7lu %7lu\n",
91 total.smap_size, total.smap_pss, total.private_dirty, total.smap_swap);
93 printf("mapped: %luK\n", total.smap_size);
99 int pmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
100 int pmap_main(int argc UNUSED_PARAM, char **argv)
105 opts = getopt32(argv, "^" "xq" "\0" "-1"); /* min one arg */
110 pid_t pid = xatoi_positive(*argv++);
111 /* GNU pmap returns 42 if any of the pids failed */
112 if (procps_get_maps(pid, opts) != 0)