#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <getopt.h>
#include "parse.h"
#include "analyze.h"
+void print_help_and_die(char *name)
+{
+ printf("Usage: %s options \n"
+ "-p, --pid=PID scan maps belingin to a given pid\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;
+
+ printf("%c: %s\n", c, optarg);
+ 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;
+ 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;
- int pid;
+ struct parse_opts opts;
- if (argc < 2) {
+ read_args(argc, argv, &opts);
+
+ if (argc < 3) {
printf("A pid needs to be given as an argument\n");
- return 1;
+ print_help_and_die(argv[0]);
}
- pid = atoi(argv[1]);
-
memset(&pf, 0, sizeof(pf));
- scan_all_pids(&pf, &process_list, pid);
+
+ scan_all_pids(&pf, &process_list, &opts);
+ print_pid_stats(&pf, process_list, &opts);
print_page_stats(&pf);
return 0;