[Refactor] move memory map printing to helper

An early return out of this function makes it harder to add new prints
after the memory maps.

Test: m, flash, look at tombstone
Change-Id: Id06e432918d69ac3307761b244473b6b7ab769e8
diff --git a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
index 3ff82bc..6b5fba8 100644
--- a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
+++ b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
@@ -303,9 +303,84 @@
   }
 }
 
+static void print_memory_maps(CallbackType callback, const Tombstone& tombstone) {
+  int word_size = pointer_width(tombstone);
+  const auto format_pointer = [word_size](uint64_t ptr) -> std::string {
+    if (word_size == 8) {
+      uint64_t top = ptr >> 32;
+      uint64_t bottom = ptr & 0xFFFFFFFF;
+      return StringPrintf("%08" PRIx64 "'%08" PRIx64, top, bottom);
+    }
+
+    return StringPrintf("%0*" PRIx64, word_size * 2, ptr);
+  };
+
+  std::string memory_map_header =
+      StringPrintf("memory map (%d %s):", tombstone.memory_mappings().size(),
+                   tombstone.memory_mappings().size() == 1 ? "entry" : "entries");
+
+  const Signal& signal_info = tombstone.signal_info();
+  bool has_fault_address = signal_info.has_fault_address();
+  uint64_t fault_address = untag_address(signal_info.fault_address());
+  bool preamble_printed = false;
+  bool printed_fault_address_marker = false;
+  for (const auto& map : tombstone.memory_mappings()) {
+    if (!preamble_printed) {
+      preamble_printed = true;
+      if (has_fault_address) {
+        if (fault_address < map.begin_address()) {
+          memory_map_header +=
+              StringPrintf("\n--->Fault address falls at %s before any mapped regions",
+                           format_pointer(fault_address).c_str());
+          printed_fault_address_marker = true;
+        } else {
+          memory_map_header += " (fault address prefixed with --->)";
+        }
+      }
+      CBS("%s", memory_map_header.c_str());
+    }
+
+    std::string line = "    ";
+    if (has_fault_address && !printed_fault_address_marker) {
+      if (fault_address < map.begin_address()) {
+        printed_fault_address_marker = true;
+        CBS("--->Fault address falls at %s between mapped regions",
+            format_pointer(fault_address).c_str());
+      } else if (fault_address >= map.begin_address() && fault_address < map.end_address()) {
+        printed_fault_address_marker = true;
+        line = "--->";
+      }
+    }
+    StringAppendF(&line, "%s-%s", format_pointer(map.begin_address()).c_str(),
+                  format_pointer(map.end_address() - 1).c_str());
+    StringAppendF(&line, " %s%s%s", map.read() ? "r" : "-", map.write() ? "w" : "-",
+                  map.execute() ? "x" : "-");
+    StringAppendF(&line, "  %8" PRIx64 "  %8" PRIx64, map.offset(),
+                  map.end_address() - map.begin_address());
+
+    if (!map.mapping_name().empty()) {
+      StringAppendF(&line, "  %s", map.mapping_name().c_str());
+
+      if (!map.build_id().empty()) {
+        StringAppendF(&line, " (BuildId: %s)", map.build_id().c_str());
+      }
+
+      if (map.load_bias() != 0) {
+        StringAppendF(&line, " (load bias 0x%" PRIx64 ")", map.load_bias());
+      }
+    }
+
+    CBS("%s", line.c_str());
+  }
+
+  if (has_fault_address && !printed_fault_address_marker) {
+    CBS("--->Fault address falls at %s after any mapped regions",
+        format_pointer(fault_address).c_str());
+  }
+}
+
 static void print_main_thread(CallbackType callback, const Tombstone& tombstone,
                               const Thread& thread) {
-  int word_size = pointer_width(tombstone);
   print_thread_header(callback, tombstone, thread, true);
 
   const Signal& signal_info = tombstone.signal_info();
@@ -322,7 +397,8 @@
   } else {
     std::string fault_addr_desc;
     if (signal_info.has_fault_address()) {
-      fault_addr_desc = StringPrintf("0x%0*" PRIx64, 2 * word_size, signal_info.fault_address());
+      fault_addr_desc =
+          StringPrintf("0x%0*" PRIx64, 2 * pointer_width(tombstone), signal_info.fault_address());
     } else {
       fault_addr_desc = "--------";
     }
@@ -387,81 +463,10 @@
   CBS("");
 
   // No memory maps to print.
-  if (tombstone.memory_mappings().empty()) {
+  if (!tombstone.memory_mappings().empty()) {
+    print_memory_maps(callback, tombstone);
+  } else {
     CBS("No memory maps found");
-    return;
-  }
-
-  const auto format_pointer = [word_size](uint64_t ptr) -> std::string {
-    if (word_size == 8) {
-      uint64_t top = ptr >> 32;
-      uint64_t bottom = ptr & 0xFFFFFFFF;
-      return StringPrintf("%08" PRIx64 "'%08" PRIx64, top, bottom);
-    }
-
-    return StringPrintf("%0*" PRIx64, word_size * 2, ptr);
-  };
-
-  std::string memory_map_header =
-      StringPrintf("memory map (%d %s):", tombstone.memory_mappings().size(),
-                   tombstone.memory_mappings().size() == 1 ? "entry" : "entries");
-
-  bool has_fault_address = signal_info.has_fault_address();
-  uint64_t fault_address = untag_address(signal_info.fault_address());
-  bool preamble_printed = false;
-  bool printed_fault_address_marker = false;
-  for (const auto& map : tombstone.memory_mappings()) {
-    if (!preamble_printed) {
-      preamble_printed = true;
-      if (has_fault_address) {
-        if (fault_address < map.begin_address()) {
-          memory_map_header +=
-              StringPrintf("\n--->Fault address falls at %s before any mapped regions",
-                           format_pointer(fault_address).c_str());
-          printed_fault_address_marker = true;
-        } else {
-          memory_map_header += " (fault address prefixed with --->)";
-        }
-      }
-      CBS("%s", memory_map_header.c_str());
-    }
-
-    std::string line = "    ";
-    if (has_fault_address && !printed_fault_address_marker) {
-      if (fault_address < map.begin_address()) {
-        printed_fault_address_marker = true;
-        CBS("--->Fault address falls at %s between mapped regions",
-            format_pointer(fault_address).c_str());
-      } else if (fault_address >= map.begin_address() && fault_address < map.end_address()) {
-        printed_fault_address_marker = true;
-        line = "--->";
-      }
-    }
-    StringAppendF(&line, "%s-%s", format_pointer(map.begin_address()).c_str(),
-                  format_pointer(map.end_address() - 1).c_str());
-    StringAppendF(&line, " %s%s%s", map.read() ? "r" : "-", map.write() ? "w" : "-",
-                  map.execute() ? "x" : "-");
-    StringAppendF(&line, "  %8" PRIx64 "  %8" PRIx64, map.offset(),
-                  map.end_address() - map.begin_address());
-
-    if (!map.mapping_name().empty()) {
-      StringAppendF(&line, "  %s", map.mapping_name().c_str());
-
-      if (!map.build_id().empty()) {
-        StringAppendF(&line, " (BuildId: %s)", map.build_id().c_str());
-      }
-
-      if (map.load_bias() != 0) {
-        StringAppendF(&line, " (load bias 0x%" PRIx64 ")", map.load_bias());
-      }
-    }
-
-    CBS("%s", line.c_str());
-  }
-
-  if (has_fault_address && !printed_fault_address_marker) {
-    CBS("--->Fault address falls at %s after any mapped regions",
-        format_pointer(fault_address).c_str());
   }
 }