]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
parser: opendir_check: Open the correct directory
[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 #define OPT_WITH_THREADS        0x101
22
23 void read_args(int argc, char *argv[], struct parse_opts *opts)
24 {
25         int optind = 0, c;
26         static struct option long_options[] = {
27                 { .val = 'p', .name = "pid", .has_arg = 1, },
28                 { .val = 'P', .name = "process", .has_arg = 1, },
29                 { .val = 'm', .name = "map", .has_arg = 1, },
30                 { .val = OPT_WITH_THREADS, .name = "with-threads" },
31                 { .val = 'h', .name = "help", },
32         };
33         char short_options[] = "p:P:m:h";
34         opts->parse_mask = 0;
35
36         while (1) {
37                 c = getopt_long(argc, argv, short_options, long_options,
38                                 &optind);
39
40                 if (c == -1)
41                         break;
42
43                 printf("%c: %s\n", c, optarg);
44                 switch (c) {
45                 case 'p':
46                         opts->pid = atoi(optarg);
47                         opts->parse_mask |= PARSE_PID;
48                         break;
49                 case 'P':
50                         opts->parse_mask |= PARSE_PROCESS_NAME;
51                         opts->name = optarg;
52                 case 'm':
53                         opts->parse_mask |= PARSE_MAP_NAME;
54                         opts->name = optarg;
55                         break;
56                 case OPT_WITH_THREADS:
57                         opts->with_threads = 1;
58                         break;
59                 case 'h':
60                         print_help_and_die(argv[0]);
61                 }
62         }
63 }
64
65 int main(int argc, char *argv[])
66 {
67         struct pageframe pf;
68         struct process *process_list = NULL;
69         struct parse_opts opts;
70
71         read_args(argc, argv, &opts);
72
73         if (argc < 3) {
74                 printf("A pid needs to be given as an argument\n");
75                 print_help_and_die(argv[0]);
76         }
77
78         memset(&pf, 0, sizeof(pf));
79
80         scan_all_pids(&pf, &process_list, &opts);
81         print_pid_stats(&pf, process_list, &opts);
82         print_page_stats(&pf);
83
84         return 0;
85 }