]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
Add support for parsing multiple pid numbers
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 19 Aug 2010 19:49:20 +0000 (22:49 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 19 Aug 2010 19:49:20 +0000 (22:49 +0300)
Pids are now stored in a linked list in the parse_opts structure.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c
pagemap.h
parse.c

diff --git a/main.c b/main.c
index 092f2247ba80a93eb24b68016361e9f0ad71712d..219fca2eb44c153697782e240d5ded0b8fe3e216 100644 (file)
--- a/main.c
+++ b/main.c
@@ -48,9 +48,18 @@ void read_args(int argc, char *argv[], struct parse_opts *opts)
 
                switch (c) {
                case 'p':
-                       opts->pid = atoi(optarg);
+               {
+                       struct pidlist *pid = alloc_pidlist();
+
+                       if (pid == NULL) {
+                               perror("malloc");
+                               return;
+                       }
+                       pid->pid = atoi(optarg);
                        opts->parse_mask |= PARSE_PID;
+                       list_add_tail(&pid->list, &opts->pidlist);
                        break;
+               }
                case 'P':
                        opts->parse_mask |= PARSE_PROCESS_NAME;
                        opts->name = optarg;
@@ -83,7 +92,7 @@ int main(int argc, char *argv[])
                        "Results may be inaccurate\n");
        }
 
-       memset(&opts, 0, sizeof(opts));
+       init_parse_opts(&opts);
 
        read_args(argc, argv, &opts);
 
index 1d4780e5c9395fa1a1a063351c36a689b2aad651..c2f712d6bd5167ae4030a07d10d959b126b752a9 100644 (file)
--- a/pagemap.h
+++ b/pagemap.h
@@ -100,13 +100,39 @@ struct process {
 #define PARSE_DUMP             0x8
 #define PARSE_NOADD_TREE       0x10
 
+struct pidlist {
+       struct list_head list;
+
+       int pid;
+};
+
 struct parse_opts {
+       struct list_head pidlist;
+
        int parse_mask;
-       int pid;
        char *name;
        int with_threads;
 };
 
+static inline void init_parse_opts(struct parse_opts *p)
+{
+       memset(p, 0, sizeof(*p));
+       INIT_LIST_HEAD(&p->pidlist);
+}
+
+static inline struct pidlist *alloc_pidlist(void)
+{
+       struct pidlist *p = malloc(sizeof(*p));
+
+       if (p == NULL)
+               return p;
+
+       memset(p, 0, sizeof(*p));
+       INIT_LIST_HEAD(&p->list);
+
+       return p;
+}
+
 #define is_parse_option(parse_opts, flag) \
        (!!((parse_opts)->parse_mask & (flag)))
 
diff --git a/parse.c b/parse.c
index 76b0eec7c81ccf54f81542eab9ed78acf47afc23..1170bab12e0ad7d081e69146d6b473a4c38ba91b 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -151,6 +151,7 @@ static char *get_name_by_pid(int pid)
 
 static int should_scan_process(struct parse_opts *opts, struct process *process)
 {
+       struct pidlist *pid;
        int match = 0;
        char *name;
 
@@ -161,8 +162,12 @@ static int should_scan_process(struct parse_opts *opts, struct process *process)
        }
 
        if (is_parse_option(opts, PARSE_PID)) {
-               if (opts->pid == process->pid)
-                       match = 1;
+               list_for_each_entry(pid, &opts->pidlist, list) {
+                       if (pid->pid == process->pid) {
+                               match = 1;
+                               break;
+                       }
+               }
        }
 
        if (is_parse_option(opts, PARSE_MAP_NAME))
@@ -467,6 +472,7 @@ static int read_pageframe_with_threads(int pid,
 int scan_all_pids(struct pageframe *pf, struct process *process_list,
                struct parse_opts *opts)
 {
+       struct pidlist *pidlist;
        DIR *dir = NULL;
        int pid;
        int count = 0;
@@ -480,9 +486,12 @@ int scan_all_pids(struct pageframe *pf, struct process *process_list,
                dir = NULL;
        }
 
-       if (is_parse_option(opts, PARSE_PID))
-               count = read_pageframe_with_threads(opts->pid, pf, process_list,
-                                               opts);
+       if (is_parse_option(opts, PARSE_PID)) {
+               list_for_each_entry(pidlist, &opts->pidlist, list) {
+                       count += read_pageframe_with_threads(pidlist->pid, pf,
+                                                       process_list, opts);
+               }
+       }
 
        if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
                printf("Failed to find any matching processes "