]> git.itanic.dy.fi Git - scan-pagemap/blobdiff - main.c
Show full process argument list instead only executable name
[scan-pagemap] / main.c
diff --git a/main.c b/main.c
index bca7436681721f2d1219a9e3d91eb8340a3dbba5..e092874b10e082e9502e92eabd23bef590653026 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,17 +1,41 @@
+/*
+ * Copyright (C) 2010 Timo Kokkonen <timo.t.kokkonen@iki.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <getopt.h>
+#include <unistd.h>
+#include <sys/types.h>
 
 #include "parse.h"
 #include "analyze.h"
+#include "pidlib.h"
 
 void print_help_and_die(char *name)
 {
-       printf("Usage: %s options \n"
-               "-p, --pid=PID          scan maps belingin to a given pid\n"
+       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"
+               "-d, --dump             dump process maps\n"
+               "-s, --shared-mappings  dump only shared mappings\n"
                "-h, --help             show this help\n",
                name);
 
@@ -20,62 +44,148 @@ void print_help_and_die(char *name)
 
 #define OPT_WITH_THREADS       0x101
 
+static void get_all_pids_by_name(struct parse_opts *opts, char *name)
+{
+       struct pidlist *pidlist;
+       DIR *dir = NULL;
+       int pid;
+
+       while ((pid = get_next_pid_by_name(&dir, name))) {
+               pidlist = alloc_pidlist();
+               if (pidlist == NULL)
+                       return;
+               pidlist->pid = pid;
+               list_add_tail(&pidlist->list, &opts->pidlist);
+       }
+}
+
+static int add_pid_to_pidlist(int pidi, struct list_head *pidlist)
+{
+       struct pidlist *pid = alloc_pidlist();
+
+       if (pid == NULL) {
+               perror("malloc");
+               return -1;
+       }
+       pid->pid = pidi;
+
+       list_add_tail(&pid->list, pidlist);
+
+       return 0;
+}
+
 void read_args(int argc, char *argv[], struct parse_opts *opts)
 {
-       int optind = 0, c;
+       int option_index = 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 = 'd', .name = "dump", },
+               { .val = 's', .name = "shared-mappings", },
                { .val = 'h', .name = "help", },
        };
-       char short_options[] = "p:m:h";
+       char short_options[] = "p:P:m:sdh";
        opts->parse_mask = 0;
 
        while (1) {
                c = getopt_long(argc, argv, short_options, long_options,
-                               &optind);
+                               &option_index);
 
                if (c == -1)
                        break;
 
-               printf("%c: %s\n", c, optarg);
                switch (c) {
                case 'p':
-                       opts->pid = atoi(optarg);
+               {
+                       int pid = pidstr_is_ok(optarg);
+                       if (!pid) {
+                               fprintf(stderr, "Invalid pid number %s\n",
+                                       optarg);
+                               break;
+                       }
+
+                       opts->parse_mask |= PARSE_PID;
+                       add_pid_to_pidlist(pid, &opts->pidlist);
+                       break;
+               }
+               case 'P':
+                       get_all_pids_by_name(opts, optarg);
                        opts->parse_mask |= PARSE_PID;
                        break;
                case 'm':
                        opts->parse_mask |= PARSE_MAP_NAME;
-                       opts->map_name = optarg;
+                       opts->name = optarg;
                        break;
                case OPT_WITH_THREADS:
                        opts->with_threads = 1;
                        break;
+               case 's':
+                       opts->parse_mask |= PARSE_SHARED_MAPPING;
+                       /* implies --dump */
+               case 'd':
+                       opts->parse_mask |= PARSE_DUMP;
+                       break;
                case 'h':
                        print_help_and_die(argv[0]);
+                       break;
+               }
+       }
+
+       while (optind < argc) {
+               int pid = pidstr_is_ok(argv[optind]);
+
+               if (pid) {
+                       opts->parse_mask |= PARSE_PID;
+                       add_pid_to_pidlist(pid, &opts->pidlist);
+               } else {
+                       get_all_pids_by_name(opts, argv[optind]);
+                       opts->parse_mask |= PARSE_PID;
                }
+               optind++;
        }
 }
 
 int main(int argc, char *argv[])
 {
-       struct pageframe pf;
-       struct process *process_list = NULL;
+       struct rb_root root;
+       struct process process_list;
        struct parse_opts opts;
 
+       if (geteuid()) {
+               printf("WARNING: Running without root priviledges. "
+                       "Results may be inaccurate\n");
+       }
+
+       init_parse_opts(&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]);
+       if (argc < 2) {
+               opts.parse_mask = PARSE_MAP_NAME;
+               opts.name = "";
        }
 
-       memset(&pf, 0, sizeof(pf));
+       memset(&root, 0, sizeof(root));
+       memset(&process_list, 0, sizeof(process_list));
+       INIT_LIST_HEAD(&process_list.list);
+
+       printf("Scanning all process IDs\n");
+
+       if (scan_all_pids(&root, &process_list, &opts))
+               return 1;
+
+       printf("Updating kpageflags\n");
+       update_kpageflags(&root);
+
+       printf("Preparing to print out results\n");
+       if (opts.parse_mask & PARSE_DUMP)
+               dump_process_maps(&root, &process_list, &opts);
+       else
+               print_pid_stats(&root, &process_list, &opts);
 
-       scan_all_pids(&pf, &process_list, &opts);
-       print_pid_stats(&pf, process_list, &opts);
-       print_page_stats(&pf);
+       print_page_stats(&root);
 
        return 0;
 }