]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
Dump process page maps 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 belonging to a given pid\n"
14                 "-P, --process=PROCESS  scan maps belonging to processes with "
15                 "given name\n"
16                 "-m, --map=mapname      scan maps with given mapping name\n"
17                 "-d, --dump             dump process maps\n"
18                 "-h, --help             show this help\n",
19                 name);
20
21         exit(0);
22 }
23
24 #define OPT_WITH_THREADS        0x101
25
26 void read_args(int argc, char *argv[], struct parse_opts *opts)
27 {
28         int optind = 0, c;
29         static struct option long_options[] = {
30                 { .val = 'p', .name = "pid", .has_arg = 1, },
31                 { .val = 'P', .name = "process", .has_arg = 1, },
32                 { .val = 'm', .name = "map", .has_arg = 1, },
33                 { .val = OPT_WITH_THREADS, .name = "with-threads" },
34                 { .val = 'd', .name = "dump", },
35                 { .val = 'h', .name = "help", },
36         };
37         char short_options[] = "p:P:m:dh";
38         opts->parse_mask = 0;
39
40         while (1) {
41                 c = getopt_long(argc, argv, short_options, long_options,
42                                 &optind);
43
44                 if (c == -1)
45                         break;
46
47                 switch (c) {
48                 case 'p':
49                         opts->pid = atoi(optarg);
50                         opts->parse_mask |= PARSE_PID;
51                         break;
52                 case 'P':
53                         opts->parse_mask |= PARSE_PROCESS_NAME;
54                         opts->name = optarg;
55                         break;
56                 case 'm':
57                         opts->parse_mask |= PARSE_MAP_NAME;
58                         opts->name = optarg;
59                         break;
60                 case OPT_WITH_THREADS:
61                         opts->with_threads = 1;
62                         break;
63                 case 'd':
64                         opts->parse_mask |= PARSE_DUMP;
65                         break;
66                 case 'h':
67                         print_help_and_die(argv[0]);
68                         break;
69                 }
70         }
71 }
72
73 int main(int argc, char *argv[])
74 {
75         struct pageframe pf;
76         struct process *process_list = NULL;
77         struct parse_opts opts;
78
79         memset(&opts, 0, sizeof(opts));
80
81         read_args(argc, argv, &opts);
82
83         if (argc < 3) {
84                 printf("A pid needs to be given as an argument\n");
85                 print_help_and_die(argv[0]);
86         }
87
88         memset(&pf, 0, sizeof(pf));
89
90         if (scan_all_pids(&pf, &process_list, &opts))
91                 return 1;
92
93         if (opts.parse_mask & PARSE_DUMP)
94                 dump_process_maps(process_list);
95         else {
96                 print_pid_stats(&pf, process_list, &opts);
97                 print_page_stats(&pf);
98         }
99
100         return 0;
101 }