]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
main.c: read_args: Add missing break to switch clause
[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                         break;
53                 case 'm':
54                         opts->parse_mask |= PARSE_MAP_NAME;
55                         opts->name = optarg;
56                         break;
57                 case OPT_WITH_THREADS:
58                         opts->with_threads = 1;
59                         break;
60                 case 'h':
61                         print_help_and_die(argv[0]);
62                 }
63         }
64 }
65
66 int main(int argc, char *argv[])
67 {
68         struct pageframe pf;
69         struct process *process_list = NULL;
70         struct parse_opts opts;
71
72         memset(&opts, 0, sizeof(opts));
73
74         read_args(argc, argv, &opts);
75
76         if (argc < 3) {
77                 printf("A pid needs to be given as an argument\n");
78                 print_help_and_die(argv[0]);
79         }
80
81         memset(&pf, 0, sizeof(pf));
82
83         scan_all_pids(&pf, &process_list, &opts);
84         print_pid_stats(&pf, process_list, &opts);
85         print_page_stats(&pf);
86
87         return 0;
88 }