]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
main.c: Add help text
[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
7 #include "parse.h"
8 #include "analyze.h"
9
10 void print_help_and_die(char *name)
11 {
12         printf("Usage: %s options \n"
13                 "-p, --pid=PID          scan maps belingin to a given pid\n"
14                 "-m, --map=mapname      scan maps with given mapping name\n"
15                 "-h, --help             show this help\n",
16                 name);
17
18         exit(0);
19 }
20
21 void read_args(int argc, char *argv[], struct parse_opts *opts)
22 {
23         int optind = 0, c;
24         static struct option long_options[] = {
25                 { .val = 'p', .name = "pid", .has_arg = 1, },
26                 { .val = 'm', .name = "map", .has_arg = 1, },
27                 { .val = 'h', .name = "help", },
28         };
29         char short_options[] = "p:m:h";
30         opts->parse_mask = 0;
31
32         while (1) {
33                 c = getopt_long(argc, argv, short_options, long_options,
34                                 &optind);
35
36                 if (c == -1)
37                         break;
38
39                 switch (c) {
40                 case 'p':
41                         opts->pid = atoi(optarg);
42                         opts->parse_mask |= PARSE_PID;
43                         break;
44                 case 'm':
45                         opts->parse_mask |= PARSE_MAP_NAME;
46                         opts->map_name = optarg;
47                         break;
48                 case 'h':
49                         print_help_and_die(argv[0]);
50                 }
51         }
52 }
53
54 int main(int argc, char *argv[])
55 {
56         struct pageframe pf;
57         struct process *process_list = NULL;
58         struct parse_opts opts;
59
60         read_args(argc, argv, &opts);
61
62         if (argc < 3) {
63                 printf("A pid needs to be given as an argument\n");
64                 print_help_and_die(argv[0]);
65         }
66
67         memset(&pf, 0, sizeof(pf));
68
69         scan_all_pids(&pf, &process_list, &opts);
70         print_pid_stats(&pf, process_list);
71         print_page_stats(&pf);
72
73         return 0;
74 }