OSDN Git Service

Added -q flag to ignore errors.
authorFelipe Leme <felipeal@google.com>
Thu, 17 Mar 2016 21:56:34 +0000 (14:56 -0700)
committerFelipe Leme <felipeal@google.com>
Thu, 17 Mar 2016 21:56:34 +0000 (14:56 -0700)
dumpstate calls showmap for each pid, and since most of them are empty,
it ends up polluting logcat with entries like:

03-17 14:49:05.974 12160 12160 E dumpstate: command '/system/xbin/su
root showmap -q 9867' failed: No such file or directory

BUG: 26906985
Change-Id: I18d86adefe3f4b248f672732460d1145103e5828

showmap/showmap.cpp

index d7d41b6..6efe260 100644 (file)
@@ -25,6 +25,11 @@ struct mapinfo {
     char name[1];
 };
 
+static bool verbose = false;
+static bool terse = false;
+static bool addresses = false;
+static bool quiet = false;
+
 static int is_library(const char *name) {
     int len = strlen(name);
     return len >= 4 && name[0] == '/'
@@ -173,7 +178,7 @@ static mapinfo *load_maps(int pid, int sort_by_address, int coalesce_by_name)
     snprintf(fn, sizeof(fn), "/proc/%d/smaps", pid);
     fp = fopen(fn, "r");
     if (fp == 0) {
-        fprintf(stderr, "cannot open /proc/%d/smaps: %s\n", pid, strerror(errno));
+        if (!quiet) fprintf(stderr, "cannot open /proc/%d/smaps: %s\n", pid, strerror(errno));
         return NULL;
     }
 
@@ -202,17 +207,13 @@ static mapinfo *load_maps(int pid, int sort_by_address, int coalesce_by_name)
     fclose(fp);
 
     if (!head) {
-        fprintf(stderr, "could not read /proc/%d/smaps\n", pid);
+        if (!quiet) fprintf(stderr, "could not read /proc/%d/smaps\n", pid);
         return NULL;
     }
 
     return head;
 }
 
-static bool verbose = false;
-static bool terse = false;
-static bool addresses = false;
-
 static void print_header()
 {
     const char *addr1 = addresses ? "   start      end " : "";
@@ -264,7 +265,7 @@ static int show_map(int pid)
 
     mapinfo *milist = load_maps(pid, addresses, !verbose && !addresses);
     if (milist == NULL) {
-        return 1;
+        return quiet ? 0 : 1;
     }
 
     print_header();
@@ -282,7 +283,7 @@ static int show_map(int pid)
         total.pss += mi->pss;
         total.size += mi->size;
         total.count += mi->count;
-        
+
         if (terse && !mi->private_dirty) {
             goto out;
         }
@@ -328,6 +329,10 @@ int main(int argc, char *argv[])
             addresses = true;
             continue;
         }
+        if (!strcmp(arg,"-q")) {
+            quiet = true;
+            continue;
+        }
         if (argc != 1) {
             fprintf(stderr, "too many arguments\n");
             break;
@@ -346,10 +351,11 @@ int main(int argc, char *argv[])
 
     if (usage) {
         fprintf(stderr,
-                "showmap [-t] [-v] [-c] <pid>\n"
+                "showmap [-t] [-v] [-c] [-q] <pid>\n"
                 "        -t = terse (show only items with private pages)\n"
                 "        -v = verbose (don't coalesce maps with the same name)\n"
                 "        -a = addresses (show virtual memory map)\n"
+                "        -q = quiet (don't show error if map could not be read)\n"
                 );
         result = 1;
     }