From f3f1a6e826c552cd503dcbb7141c5c6a5a847d92 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Thu, 19 Aug 2010 22:49:20 +0300 Subject: [PATCH] Add support for parsing multiple pid numbers Pids are now stored in a linked list in the parse_opts structure. Signed-off-by: Timo Kokkonen --- main.c | 13 +++++++++++-- pagemap.h | 28 +++++++++++++++++++++++++++- parse.c | 19 ++++++++++++++----- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 092f224..219fca2 100644 --- 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); diff --git a/pagemap.h b/pagemap.h index 1d4780e..c2f712d 100644 --- 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 76b0eec..1170bab 100644 --- 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 " -- 2.45.0