2 * Copyright (C) 2010 Timo Kokkonen <kaapeli@itanic.dy.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
30 void print_help_and_die(char *name)
32 printf("Usage: %s options\n"
33 "-p, --pid=PID scan maps belonging to a given pid\n"
34 "-P, --process=PROCESS scan maps belonging to processes with "
36 "-m, --map=mapname scan maps with given mapping name\n"
37 "-d, --dump dump process maps\n"
38 "-s, --shared-mappings dump only shared mappings\n"
39 "-h, --help show this help\n",
45 #define OPT_WITH_THREADS 0x101
47 static void get_all_pids_by_name(struct parse_opts *opts, char *name)
49 struct pidlist *pidlist;
53 while ((pid = get_next_pid_by_name(&dir, name))) {
54 pidlist = alloc_pidlist();
58 list_add_tail(&pidlist->list, &opts->pidlist);
62 static int add_pid_to_pidlist(int pidi, struct list_head *pidlist)
64 struct pidlist *pid = alloc_pidlist();
72 list_add_tail(&pid->list, pidlist);
77 void read_args(int argc, char *argv[], struct parse_opts *opts)
79 int option_index = 0, c;
80 static struct option long_options[] = {
81 { .val = 'p', .name = "pid", .has_arg = 1, },
82 { .val = 'P', .name = "process", .has_arg = 1, },
83 { .val = 'm', .name = "map", .has_arg = 1, },
84 { .val = OPT_WITH_THREADS, .name = "with-threads" },
85 { .val = 'd', .name = "dump", },
86 { .val = 's', .name = "shared-mappings", },
87 { .val = 'h', .name = "help", },
89 char short_options[] = "p:P:m:sdh";
93 c = getopt_long(argc, argv, short_options, long_options,
102 int pid = pidstr_is_ok(optarg);
104 fprintf(stderr, "Invalid pid number %s\n",
109 opts->parse_mask |= PARSE_PID;
110 add_pid_to_pidlist(pid, &opts->pidlist);
114 get_all_pids_by_name(opts, optarg);
115 opts->parse_mask |= PARSE_PID;
118 opts->parse_mask |= PARSE_MAP_NAME;
121 case OPT_WITH_THREADS:
122 opts->with_threads = 1;
125 opts->parse_mask |= PARSE_SHARED_MAPPING;
128 opts->parse_mask |= PARSE_DUMP;
131 print_help_and_die(argv[0]);
136 while (optind < argc) {
137 int pid = pidstr_is_ok(argv[optind]);
140 opts->parse_mask |= PARSE_PID;
141 add_pid_to_pidlist(pid, &opts->pidlist);
143 get_all_pids_by_name(opts, argv[optind]);
144 opts->parse_mask |= PARSE_PID;
150 int main(int argc, char *argv[])
153 struct process process_list;
154 struct parse_opts opts;
157 printf("WARNING: Running without root priviledges. "
158 "Results may be inaccurate\n");
161 init_parse_opts(&opts);
163 read_args(argc, argv, &opts);
166 opts.parse_mask = PARSE_MAP_NAME;
170 memset(&root, 0, sizeof(root));
171 memset(&process_list, 0, sizeof(process_list));
172 INIT_LIST_HEAD(&process_list.list);
174 printf("Scanning all process IDs\n");
176 if (scan_all_pids(&root, &process_list, &opts))
179 printf("Updating kpageflags\n");
180 update_kpageflags(&root);
182 printf("Preparing to print out results\n");
183 if (opts.parse_mask & PARSE_DUMP)
184 dump_process_maps(&root, &process_list, &opts);
186 print_pid_stats(&root, &process_list, &opts);
188 print_page_stats(&root);