summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-12-08 23:32:38 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-12-08 23:32:38 +0000
commit113206fb6548cf95bdb91821c7b4dd384c926756 (patch)
tree0ce6feb6d9522ac5233ae02af2b5c952843443be
parent8a72d54a4f6636734f1f6a8a9d76022829f2001a (diff)
parentddbe8c3fcb3d8272129fd17ee3b06611f7df8185 (diff)
downloadextras-113206fb6548cf95bdb91821c7b4dd384c926756.tar.gz
Merge "Prevent future instances of the last bug."
-rw-r--r--showmap/Android.mk3
-rw-r--r--showmap/showmap.cpp (renamed from showmap/showmap.c)118
2 files changed, 52 insertions, 69 deletions
diff --git a/showmap/Android.mk b/showmap/Android.mk
index 5c90f12f..44ca8183 100644
--- a/showmap/Android.mk
+++ b/showmap/Android.mk
@@ -3,8 +3,7 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= showmap.c
-LOCAL_SHARED_LIBRARIES := libcutils
+LOCAL_SRC_FILES:= showmap.cpp
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE:= showmap
diff --git a/showmap/showmap.c b/showmap/showmap.cpp
index 2d602161..d7d41b66 100644
--- a/showmap/showmap.c
+++ b/showmap/showmap.cpp
@@ -1,15 +1,12 @@
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <math.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <math.h>
#include <string.h>
-#include <errno.h>
#include <unistd.h>
-#include <fcntl.h>
-
-#include <ctype.h>
-#include <stddef.h>
-
-typedef struct mapinfo mapinfo;
struct mapinfo {
mapinfo *next;
@@ -69,7 +66,7 @@ static int parse_header(const char* line, const mapinfo* prev, mapinfo** mi) {
}
const int name_size = strlen(name) + 1;
- struct mapinfo* info = calloc(1, sizeof(mapinfo) + name_size);
+ struct mapinfo* info = reinterpret_cast<mapinfo*>(calloc(1, sizeof(mapinfo) + name_size));
if (info == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
@@ -212,21 +209,17 @@ static mapinfo *load_maps(int pid, int sort_by_address, int coalesce_by_name)
return head;
}
-static int verbose = 0;
-static int terse = 0;
-static int addresses = 0;
+static bool verbose = false;
+static bool terse = false;
+static bool addresses = false;
static void print_header()
{
- if (addresses) {
- printf(" start end ");
- }
- printf(" virtual shared shared private private\n");
+ const char *addr1 = addresses ? " start end " : "";
+ const char *addr2 = addresses ? " addr addr " : "";
- if (addresses) {
- printf(" addr addr ");
- }
- printf(" size RSS PSS clean dirty clean dirty swap ");
+ printf("%s virtual shared shared private private\n", addr1);
+ printf("%s size RSS PSS clean dirty clean dirty swap ", addr2);
if (!verbose && !addresses) {
printf(" # ");
}
@@ -245,21 +238,31 @@ static void print_divider()
printf("------------------------------\n");
}
+static void print_mi(mapinfo *mi, bool total)
+{
+ if (addresses) {
+ if (total) {
+ printf(" ");
+ } else {
+ printf("%08x %08x ", mi->start, mi->end);
+ }
+ }
+ printf("%8d %8d %8d %8d %8d %8d %8d %8d ", mi->size,
+ mi->rss,
+ mi->pss,
+ mi->shared_clean, mi->shared_dirty,
+ mi->private_clean, mi->private_dirty, mi->swap);
+ if (!verbose && !addresses) {
+ printf("%4d ", mi->count);
+ }
+}
+
static int show_map(int pid)
{
- mapinfo *milist;
- mapinfo *mi;
- unsigned shared_dirty = 0;
- unsigned shared_clean = 0;
- unsigned private_dirty = 0;
- unsigned private_clean = 0;
- unsigned swap = 0;
- unsigned rss = 0;
- unsigned pss = 0;
- unsigned size = 0;
- unsigned count = 0;
-
- milist = load_maps(pid, addresses, !verbose && !addresses);
+ mapinfo total;
+ memset(&total, 0, sizeof(total));
+
+ mapinfo *milist = load_maps(pid, addresses, !verbose && !addresses);
if (milist == NULL) {
return 1;
}
@@ -267,34 +270,24 @@ static int show_map(int pid)
print_header();
print_divider();
- for (mi = milist; mi;) {
+ for (mapinfo *mi = milist; mi;) {
mapinfo* last = mi;
- shared_clean += mi->shared_clean;
- shared_dirty += mi->shared_dirty;
- private_clean += mi->private_clean;
- private_dirty += mi->private_dirty;
- swap += mi->swap;
- rss += mi->rss;
- pss += mi->pss;
- size += mi->size;
- count += mi->count;
+ total.shared_clean += mi->shared_clean;
+ total.shared_dirty += mi->shared_dirty;
+ total.private_clean += mi->private_clean;
+ total.private_dirty += mi->private_dirty;
+ total.swap += mi->swap;
+ total.rss += mi->rss;
+ total.pss += mi->pss;
+ total.size += mi->size;
+ total.count += mi->count;
if (terse && !mi->private_dirty) {
goto out;
}
- if (addresses) {
- printf("%08x %08x ", mi->start, mi->end);
- }
- printf("%8d %8d %8d %8d %8d %8d %8d %8d", mi->size,
- mi->rss,
- mi->pss,
- mi->shared_clean, mi->shared_dirty,
- mi->private_clean, mi->private_dirty, mi->swap);
- if (!verbose && !addresses) {
- printf("%4d ", mi->count);
- }
+ print_mi(mi, false);
printf("%s%s\n", mi->name, mi->is_bss ? " [bss]" : "");
out:
@@ -306,16 +299,7 @@ out:
print_header();
print_divider();
- if (addresses) {
- printf(" ");
- }
- printf("%8d %8d %8d %8d %8d %8d %8d %8d", size,
- rss, pss,
- shared_clean, shared_dirty,
- private_clean, private_dirty, swap);
- if (!verbose && !addresses) {
- printf("%4d ", count);
- }
+ print_mi(&total, true);
printf("TOTAL\n");
return 0;
@@ -333,15 +317,15 @@ int main(int argc, char *argv[])
for (argc--, argv++; argc > 0; argc--, argv++) {
arg = argv[0];
if (!strcmp(arg,"-v")) {
- verbose = 1;
+ verbose = true;
continue;
}
if (!strcmp(arg,"-t")) {
- terse = 1;
+ terse = true;
continue;
}
if (!strcmp(arg,"-a")) {
- addresses = 1;
+ addresses = true;
continue;
}
if (argc != 1) {