blob: 408cbfce7570d165b26d62948c4230b396cefeb5 [file] [log] [blame]
Alexander Shishkin0834a6d2010-08-28 23:20:34 +02001/*
2 * pmap implementation for busybox
3 *
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Written by Alexander Shishkin <virtuoso@slind.org>
6 *
7 * Licensed under GPLv2 or later, see the LICENSE file in this source tree
8 * for details.
9 */
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020010//config:config PMAP
Denys Vlasenko72089cf2017-07-21 09:50:55 +020011//config: bool "pmap (6 kb)"
12//config: default y
13//config: help
14//config: Display processes' memory mappings.
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020015
Denys Vlasenko66426762011-06-05 03:58:28 +020016//applet:IF_PMAP(APPLET(pmap, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020017
Denys Vlasenko66426762011-06-05 03:58:28 +020018//kbuild:lib-$(CONFIG_PMAP) += pmap.o
19
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020020//usage:#define pmap_trivial_usage
Denys Vlasenko947b2392017-08-04 18:36:55 +020021//usage: "[-xq] PID..."
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020022//usage:#define pmap_full_usage "\n\n"
Denys Vlasenko049b0072015-10-24 03:45:57 +020023//usage: "Display process memory usage"
Denys Vlasenko66426762011-06-05 03:58:28 +020024//usage: "\n"
25//usage: "\n -x Show details"
26//usage: "\n -q Quiet"
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020027
28#include "libbb.h"
29
Denys Vlasenko2266c122021-01-05 19:39:34 +010030#if ULLONG_MAX == 0xffffffff
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020031# define TABS "\t"
Denys Vlasenko2266c122021-01-05 19:39:34 +010032# define AFMTLL "8"
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020033# define DASHES ""
34#else
35# define TABS "\t\t"
Denys Vlasenkoaad76962018-12-30 20:24:59 +010036# define AFMTLL "16"
Denys Vlasenko2266c122021-01-05 19:39:34 +010037# define DASHES "--------"
Denys Vlasenkoaad76962018-12-30 20:24:59 +010038#endif
39
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020040enum {
41 OPT_x = 1 << 0,
42 OPT_q = 1 << 1,
43};
44
45static void print_smaprec(struct smaprec *currec, void *data)
46{
Denys Vlasenkod3036ef2010-10-22 13:15:15 +020047 unsigned opt = (uintptr_t)data;
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020048
Denys Vlasenkoaad76962018-12-30 20:24:59 +010049 printf("%0" AFMTLL "llx ", currec->smap_start);
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020050
51 if (opt & OPT_x)
52 printf("%7lu %7lu %7lu %7lu ",
53 currec->smap_size,
54 currec->smap_pss,
55 currec->private_dirty,
56 currec->smap_swap);
57 else
58 printf("%7luK", currec->smap_size);
59
60 printf(" %.4s %s\n", currec->smap_mode, currec->smap_name);
61}
62
63static int procps_get_maps(pid_t pid, unsigned opt)
64{
65 struct smaprec total;
66 int ret;
67 char buf[256];
68
Denys Vlasenko049b0072015-10-24 03:45:57 +020069 read_cmdline(buf, sizeof(buf), pid, NULL);
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020070 printf("%u: %s\n", (int)pid, buf);
71
72 if (!(opt & OPT_q) && (opt & OPT_x))
73 puts("Address" TABS " Kbytes PSS Dirty Swap Mode Mapping");
74
75 memset(&total, 0, sizeof(total));
76
Denys Vlasenkod3036ef2010-10-22 13:15:15 +020077 ret = procps_read_smaps(pid, &total, print_smaprec, (void*)(uintptr_t)opt);
Alexander Shishkin0834a6d2010-08-28 23:20:34 +020078 if (ret)
79 return ret;
80
81 if (!(opt & OPT_q)) {
82 if (opt & OPT_x)
83 printf("--------" DASHES " ------ ------ ------ ------\n"
84 "total" TABS " %7lu %7lu %7lu %7lu\n",
85 total.smap_size, total.smap_pss, total.private_dirty, total.smap_swap);
86 else
87 printf("mapped: %luK\n", total.smap_size);
88 }
89
90 return 0;
91}
92
93int pmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
94int pmap_main(int argc UNUSED_PARAM, char **argv)
95{
96 unsigned opts;
97 int ret;
98
Denys Vlasenko22542ec2017-08-08 21:55:02 +020099 opts = getopt32(argv, "^" "xq" "\0" "-1"); /* min one arg */
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200100 argv += optind;
101
102 ret = 0;
103 while (*argv) {
104 pid_t pid = xatoi_positive(*argv++);
105 /* GNU pmap returns 42 if any of the pids failed */
106 if (procps_get_maps(pid, opts) != 0)
107 ret = 42;
108 }
109
110 return ret;
111}