]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
Makefile: Apply CFLAGS also when linking the final binary
[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 #include <unistd.h>
7 #include <sys/types.h>
8
9 #include "parse.h"
10 #include "analyze.h"
11
12 void print_help_and_die(char *name)
13 {
14         printf("Usage: %s options\n"
15                 "-p, --pid=PID          scan maps belonging to a given pid\n"
16                 "-P, --process=PROCESS  scan maps belonging to processes with "
17                 "given name\n"
18                 "-m, --map=mapname      scan maps with given mapping name\n"
19                 "-d, --dump             dump process maps\n"
20                 "-h, --help             show this help\n",
21                 name);
22
23         exit(0);
24 }
25
26 #define OPT_WITH_THREADS        0x101
27
28 void read_args(int argc, char *argv[], struct parse_opts *opts)
29 {
30         int optind = 0, c;
31         static struct option long_options[] = {
32                 { .val = 'p', .name = "pid", .has_arg = 1, },
33                 { .val = 'P', .name = "process", .has_arg = 1, },
34                 { .val = 'm', .name = "map", .has_arg = 1, },
35                 { .val = OPT_WITH_THREADS, .name = "with-threads" },
36                 { .val = 'd', .name = "dump", },
37                 { .val = 'h', .name = "help", },
38         };
39         char short_options[] = "p:P:m:dh";
40         opts->parse_mask = 0;
41
42         while (1) {
43                 c = getopt_long(argc, argv, short_options, long_options,
44                                 &optind);
45
46                 if (c == -1)
47                         break;
48
49                 switch (c) {
50                 case 'p':
51                 {
52                         struct pidlist *pid = alloc_pidlist();
53
54                         if (pid == NULL) {
55                                 perror("malloc");
56                                 return;
57                         }
58                         pid->pid = atoi(optarg);
59                         opts->parse_mask |= PARSE_PID;
60                         list_add_tail(&pid->list, &opts->pidlist);
61                         break;
62                 }
63                 case 'P':
64                         opts->parse_mask |= PARSE_PROCESS_NAME;
65                         opts->name = optarg;
66                         break;
67                 case 'm':
68                         opts->parse_mask |= PARSE_MAP_NAME;
69                         opts->name = optarg;
70                         break;
71                 case OPT_WITH_THREADS:
72                         opts->with_threads = 1;
73                         break;
74                 case 'd':
75                         opts->parse_mask |= PARSE_DUMP;
76                         break;
77                 case 'h':
78                         print_help_and_die(argv[0]);
79                         break;
80                 }
81         }
82 }
83
84 int main(int argc, char *argv[])
85 {
86         struct pageframe pf;
87         struct process process_list;
88         struct parse_opts opts;
89
90         if (geteuid()) {
91                 printf("WARNING: Running without root priviledges. "
92                         "Results may be inaccurate\n");
93         }
94
95         init_parse_opts(&opts);
96
97         read_args(argc, argv, &opts);
98
99         if (argc < 3) {
100                 printf("A pid needs to be given as an argument\n");
101                 print_help_and_die(argv[0]);
102         }
103
104         clear_pageframe(&pf);
105
106         memset(&process_list, 0, sizeof(process_list));
107         INIT_LIST_HEAD(&process_list.list);
108
109         if (scan_all_pids(&pf, &process_list, &opts))
110                 return 1;
111
112         if (opts.parse_mask & PARSE_DUMP)
113                 dump_process_maps(&process_list);
114         else
115                 print_pid_stats(&pf, &process_list, &opts);
116
117         print_page_stats(&pf);
118
119         return 0;
120 }