]> git.itanic.dy.fi Git - scan-pagemap/blobdiff - parse.c
Break parsing if no processes are found
[scan-pagemap] / parse.c
diff --git a/parse.c b/parse.c
index 78311d9844cd0faa7553c59588445912631c35af..c35fbe19bafb6f6c3548f31ba045f23a9182fb5d 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -279,8 +279,8 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
        return 0;
 }
 
-void read_pageframe(int pid, int tid, struct pageframe *pageframe,
-               struct process **process_list, struct parse_opts *opts)
+static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
+                       struct process **process_list, struct parse_opts *opts)
 {
        struct maps *maps;
        struct process *process;
@@ -303,7 +303,7 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        file = fopen(path, "rb");
 
        if (!file)
-               return;
+               return 0;
 
        maps = parse_maps(file, pid, tid);
        fclose(file);
@@ -313,23 +313,23 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        file = fopen(path, "rb");
 
        if (!file)
-               return;
+               return 0;
 
        parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
        if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
-               return;
+               return 1;
 
        if (maps == NULL)
-               return;
+               return 1;
 
        list_for_each_entry(maps, &process->maps->list, list) {
                process->pages_present += maps->pages_present;
                process->pages_swapped += maps->pages_swapped;
        }
 
-       return;
+       return 1;
 }
 
 static int parse_pid(DIR **dir)
@@ -419,13 +419,14 @@ static int get_next_pid_by_name(DIR **dir, char *name)
        return 0;
 }
 
-static void read_pageframe_with_threads(int pid,
-                                       struct pageframe *pageframe,
-                                       struct process **process_list,
-                                       struct parse_opts *opts)
+static int read_pageframe_with_threads(int pid,
+                               struct pageframe *pageframe,
+                               struct process **process_list,
+                               struct parse_opts *opts)
 {
        DIR *dir = NULL;
        int tid;
+       int count = 0;
 
        while (1) {
                if (opts->with_threads)
@@ -434,31 +435,43 @@ static void read_pageframe_with_threads(int pid,
                        tid = pid;
 
                if (tid <= 0)
-                       return;
+                       return count;
 
-               read_pageframe(pid, tid, pageframe, process_list, opts);
+               count += read_pageframe(pid, tid, pageframe, process_list,
+                                       opts);
 
                if (!opts->with_threads)
                        break;
        }
+
+       return count;
 }
 
-void scan_all_pids(struct pageframe *pf, struct process **process_list,
+int scan_all_pids(struct pageframe *pf, struct process **process_list,
                struct parse_opts *opts)
 {
        DIR *dir = NULL;
        int pid;
+       int count = 0;
 
        if (opts->parse_mask & PARSE_PROCESS_NAME) {
                while ((pid = get_next_pid_by_name(&dir, opts->name))) {
-                       read_pageframe_with_threads(pid, pf, process_list,
-                                               opts);
+                       count += read_pageframe_with_threads(pid, pf,
+                                                       process_list,
+                                                       opts);
                }
                dir = NULL;
        }
 
        if (opts->parse_mask & PARSE_PID)
-               read_pageframe_with_threads(opts->pid, pf, process_list, opts);
+               count = read_pageframe_with_threads(opts->pid, pf, process_list,
+                                               opts);
+
+       if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
+               printf("Failed to find any matching processes "
+                       "with given arguments\n");
+               return -1;
+       }
 
        while (1) {
                pid = get_next_pid(&dir);
@@ -466,4 +479,6 @@ void scan_all_pids(struct pageframe *pf, struct process **process_list,
                        break;
                read_pageframe_with_threads(pid, pf, process_list, opts);
        }
+
+       return 0;
 }