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 static int add_pid_to_pidlist(int pidi, struct list_head *pidlist)
47 struct pidlist *pid = alloc_pidlist();
55 list_add_tail(&pid->list, pidlist);
60 void read_args(int argc, char *argv[], struct parse_opts *opts)
62 int option_index = 0, c;
63 static struct option long_options[] = {
64 { .val = 'p', .name = "pid", .has_arg = 1, },
65 { .val = 'P', .name = "process", .has_arg = 1, },
66 { .val = 'm', .name = "map", .has_arg = 1, },
67 { .val = OPT_WITH_THREADS, .name = "with-threads" },
68 { .val = 'd', .name = "dump", },
69 { .val = 's', .name = "shared-mappings", },
70 { .val = 'h', .name = "help", },
72 char short_options[] = "p:P:m:sdh";
76 c = getopt_long(argc, argv, short_options, long_options,
85 int pid = pidstr_is_ok(optarg);
87 fprintf(stderr, "Invalid pid number %s\n",
92 opts->parse_mask |= PARSE_PID;
93 add_pid_to_pidlist(pid, &opts->pidlist);
97 get_all_pids_by_name(opts, optarg);
98 opts->parse_mask |= PARSE_PID;
101 opts->parse_mask |= PARSE_MAP_NAME;
104 case OPT_WITH_THREADS:
105 opts->with_threads = 1;
108 opts->parse_mask |= PARSE_SHARED_MAPPING;
111 opts->parse_mask |= PARSE_DUMP;
114 print_help_and_die(argv[0]);
119 while (optind < argc) {
120 int pid = pidstr_is_ok(argv[optind]);
123 opts->parse_mask |= PARSE_PID;
124 add_pid_to_pidlist(pid, &opts->pidlist);
126 get_all_pids_by_name(opts, argv[optind]);
127 opts->parse_mask |= PARSE_PID;
133 int main(int argc, char *argv[])
136 struct process process_list;
137 struct parse_opts opts;
140 printf("WARNING: Running without root priviledges. "
141 "Results may be inaccurate\n");
144 init_parse_opts(&opts);
146 read_args(argc, argv, &opts);
149 printf("A pid or process name "
150 "needs to be given as an argument\n");
151 print_help_and_die(argv[0]);
154 clear_pageframe(&pf);
156 memset(&process_list, 0, sizeof(process_list));
157 INIT_LIST_HEAD(&process_list.list);
159 if (scan_all_pids(&pf, &process_list, &opts))
162 update_kpageflags(&pf);
164 if (opts.parse_mask & PARSE_DUMP)
165 dump_process_maps(&pf, &process_list, &opts);
167 print_pid_stats(&pf, &process_list, &opts);
169 print_page_stats(&pf);