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