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