]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
parser: Fix the order of argumets for list_add()
[scan-pagemap] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <getopt.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8
9 #include "parse.h"
10 #include "analyze.h"
11
12 void print_help_and_die(char *name)
13 {
14         printf("Usage: %s options\n"
15                 "-p, --pid=PID          scan maps belonging to a given pid\n"
16                 "-P, --process=PROCESS  scan maps belonging to processes with "
17                 "given name\n"
18                 "-m, --map=mapname      scan maps with given mapping name\n"
19                 "-d, --dump             dump process maps\n"
20                 "-h, --help             show this help\n",
21                 name);
22
23         exit(0);
24 }
25
26 #define OPT_WITH_THREADS        0x101
27
28 void read_args(int argc, char *argv[], struct parse_opts *opts)
29 {
30         int optind = 0, c;
31         static struct option long_options[] = {
32                 { .val = 'p', .name = "pid", .has_arg = 1, },
33                 { .val = 'P', .name = "process", .has_arg = 1, },
34                 { .val = 'm', .name = "map", .has_arg = 1, },
35                 { .val = OPT_WITH_THREADS, .name = "with-threads" },
36                 { .val = 'd', .name = "dump", },
37                 { .val = 'h', .name = "help", },
38         };
39         char short_options[] = "p:P:m:dh";
40         opts->parse_mask = 0;
41
42         while (1) {
43                 c = getopt_long(argc, argv, short_options, long_options,
44                                 &optind);
45
46                 if (c == -1)
47                         break;
48
49                 switch (c) {
50                 case 'p':
51                         opts->pid = atoi(optarg);
52                         opts->parse_mask |= PARSE_PID;
53                         break;
54                 case 'P':
55                         opts->parse_mask |= PARSE_PROCESS_NAME;
56                         opts->name = optarg;
57                         break;
58                 case 'm':
59                         opts->parse_mask |= PARSE_MAP_NAME;
60                         opts->name = optarg;
61                         break;
62                 case OPT_WITH_THREADS:
63                         opts->with_threads = 1;
64                         break;
65                 case 'd':
66                         opts->parse_mask |= PARSE_DUMP;
67                         break;
68                 case 'h':
69                         print_help_and_die(argv[0]);
70                         break;
71                 }
72         }
73 }
74
75 int main(int argc, char *argv[])
76 {
77         struct pageframe pf;
78         struct process *process_list = NULL;
79         struct parse_opts opts;
80
81         if (getuid()) {
82                 printf("WARNING: Running without root priviledges. "
83                         "Results may be inaccurate\n");
84         }
85
86         memset(&opts, 0, sizeof(opts));
87
88         read_args(argc, argv, &opts);
89
90         if (argc < 3) {
91                 printf("A pid needs to be given as an argument\n");
92                 print_help_and_die(argv[0]);
93         }
94
95         memset(&pf, 0, sizeof(pf));
96
97         if (scan_all_pids(&pf, &process_list, &opts))
98                 return 1;
99
100         if (opts.parse_mask & PARSE_DUMP)
101                 dump_process_maps(process_list);
102         else
103                 print_pid_stats(&pf, process_list, &opts);
104
105         print_page_stats(&pf);
106
107         return 0;
108 }