13 void print_help_and_die(char *name)
15 printf("Usage: %s options\n"
16 "-p, --pid=PID scan maps belonging to a given pid\n"
17 "-P, --process=PROCESS scan maps belonging to processes with "
19 "-m, --map=mapname scan maps with given mapping name\n"
20 "-d, --dump dump process maps\n"
21 "-s, --shared-mappings dump only shared mappings\n"
22 "-h, --help show this help\n",
28 #define OPT_WITH_THREADS 0x101
30 static void get_all_pids_by_name(struct parse_opts *opts, char *name)
32 struct pidlist *pidlist;
36 while ((pid = get_next_pid_by_name(&dir, name))) {
37 pidlist = alloc_pidlist();
41 list_add_tail(&pidlist->list, &opts->pidlist);
45 void read_args(int argc, char *argv[], struct parse_opts *opts)
48 static struct option long_options[] = {
49 { .val = 'p', .name = "pid", .has_arg = 1, },
50 { .val = 'P', .name = "process", .has_arg = 1, },
51 { .val = 'm', .name = "map", .has_arg = 1, },
52 { .val = OPT_WITH_THREADS, .name = "with-threads" },
53 { .val = 'd', .name = "dump", },
54 { .val = 's', .name = "shared-mappings", },
55 { .val = 'h', .name = "help", },
57 char short_options[] = "p:P:m:sdh";
61 c = getopt_long(argc, argv, short_options, long_options,
70 struct pidlist *pid = alloc_pidlist();
76 pid->pid = atoi(optarg);
77 opts->parse_mask |= PARSE_PID;
78 list_add_tail(&pid->list, &opts->pidlist);
82 get_all_pids_by_name(opts, optarg);
83 opts->parse_mask |= PARSE_PID;
86 opts->parse_mask |= PARSE_MAP_NAME;
89 case OPT_WITH_THREADS:
90 opts->with_threads = 1;
93 opts->parse_mask |= PARSE_SHARED_MAPPING;
96 opts->parse_mask |= PARSE_DUMP;
99 print_help_and_die(argv[0]);
105 int main(int argc, char *argv[])
108 struct process process_list;
109 struct parse_opts opts;
112 printf("WARNING: Running without root priviledges. "
113 "Results may be inaccurate\n");
116 init_parse_opts(&opts);
118 read_args(argc, argv, &opts);
121 printf("A pid needs to be given as an argument\n");
122 print_help_and_die(argv[0]);
125 clear_pageframe(&pf);
127 memset(&process_list, 0, sizeof(process_list));
128 INIT_LIST_HEAD(&process_list.list);
130 if (scan_all_pids(&pf, &process_list, &opts))
133 if (opts.parse_mask & PARSE_DUMP)
134 dump_process_maps(&pf, &process_list, &opts);
136 print_pid_stats(&pf, &process_list, &opts);
138 print_page_stats(&pf);