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 "-h, --help show this help\n",
27 #define OPT_WITH_THREADS 0x101
29 static void get_all_pids_by_name(struct parse_opts *opts, char *name)
31 struct pidlist *pidlist;
35 while ((pid = get_next_pid_by_name(&dir, name))) {
36 pidlist = alloc_pidlist();
40 list_add_tail(&pidlist->list, &opts->pidlist);
44 void read_args(int argc, char *argv[], struct parse_opts *opts)
47 static struct option long_options[] = {
48 { .val = 'p', .name = "pid", .has_arg = 1, },
49 { .val = 'P', .name = "process", .has_arg = 1, },
50 { .val = 'm', .name = "map", .has_arg = 1, },
51 { .val = OPT_WITH_THREADS, .name = "with-threads" },
52 { .val = 'd', .name = "dump", },
53 { .val = 'h', .name = "help", },
55 char short_options[] = "p:P:m:dh";
59 c = getopt_long(argc, argv, short_options, long_options,
68 struct pidlist *pid = alloc_pidlist();
74 pid->pid = atoi(optarg);
75 opts->parse_mask |= PARSE_PID;
76 list_add_tail(&pid->list, &opts->pidlist);
80 get_all_pids_by_name(opts, optarg);
81 opts->parse_mask |= PARSE_PID;
84 opts->parse_mask |= PARSE_MAP_NAME;
87 case OPT_WITH_THREADS:
88 opts->with_threads = 1;
91 opts->parse_mask |= PARSE_DUMP;
94 print_help_and_die(argv[0]);
100 int main(int argc, char *argv[])
103 struct process process_list;
104 struct parse_opts opts;
107 printf("WARNING: Running without root priviledges. "
108 "Results may be inaccurate\n");
111 init_parse_opts(&opts);
113 read_args(argc, argv, &opts);
116 printf("A pid needs to be given as an argument\n");
117 print_help_and_die(argv[0]);
120 clear_pageframe(&pf);
122 memset(&process_list, 0, sizeof(process_list));
123 INIT_LIST_HEAD(&process_list.list);
125 if (scan_all_pids(&pf, &process_list, &opts))
128 if (opts.parse_mask & PARSE_DUMP)
129 dump_process_maps(&process_list);
131 print_pid_stats(&pf, &process_list, &opts);
133 print_page_stats(&pf);