#include #include #include #include #include #include "parse.h" #include "analyze.h" void print_help_and_die(char *name) { printf("Usage: %s options \n" "-p, --pid=PID scan maps belonging to a given pid\n" "-P, --process=PROCESS scan maps belonging to processes with " "given name\n" "-m, --map=mapname scan maps with given mapping name\n" "-h, --help show this help\n", name); exit(0); } #define OPT_WITH_THREADS 0x101 void read_args(int argc, char *argv[], struct parse_opts *opts) { int optind = 0, c; static struct option long_options[] = { { .val = 'p', .name = "pid", .has_arg = 1, }, { .val = 'P', .name = "process", .has_arg = 1, }, { .val = 'm', .name = "map", .has_arg = 1, }, { .val = OPT_WITH_THREADS, .name = "with-threads" }, { .val = 'h', .name = "help", }, }; char short_options[] = "p:P:m:h"; opts->parse_mask = 0; while (1) { c = getopt_long(argc, argv, short_options, long_options, &optind); if (c == -1) break; switch (c) { case 'p': opts->pid = atoi(optarg); opts->parse_mask |= PARSE_PID; break; case 'P': opts->parse_mask |= PARSE_PROCESS_NAME; opts->name = optarg; break; case 'm': opts->parse_mask |= PARSE_MAP_NAME; opts->name = optarg; break; case OPT_WITH_THREADS: opts->with_threads = 1; break; case 'h': print_help_and_die(argv[0]); } } } int main(int argc, char *argv[]) { struct pageframe pf; struct process *process_list = NULL; struct parse_opts opts; memset(&opts, 0, sizeof(opts)); read_args(argc, argv, &opts); if (argc < 3) { printf("A pid needs to be given as an argument\n"); print_help_and_die(argv[0]); } memset(&pf, 0, sizeof(pf)); if (scan_all_pids(&pf, &process_list, &opts)) return 1; print_pid_stats(&pf, process_list, &opts); print_page_stats(&pf); return 0; }