[roheap] Switch to ReadOnlyHeapIterator in mkgrokdump

HeapObjectIterator now has a DCHECK preventing iterating over read-only
space. This removes DumpMaps and switches to ReadOnlyHeapIterator
instead.

Bug: v8:7464
Change-Id: Ifd186d0e26d94c315308aa3a6ecb9da02a270fc1
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1622120
Commit-Queue: Maciej Goszczycki <goszczycki@google.com>
Reviewed-by: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61708}
diff --git a/test/mkgrokdump/mkgrokdump.cc b/test/mkgrokdump/mkgrokdump.cc
index 0ab7142..54bf517 100644
--- a/test/mkgrokdump/mkgrokdump.cc
+++ b/test/mkgrokdump/mkgrokdump.cc
@@ -42,41 +42,41 @@
   void Free(void* p, size_t) override {}
 };
 
+static void DumpKnownMap(i::Heap* heap, const char* space_name,
+                         i::HeapObject object) {
 #define RO_ROOT_LIST_CASE(type, name, CamelName) \
-  if (n == NULL && o == roots.name()) n = #CamelName;
+  if (root_name == nullptr && object == roots.name()) root_name = #CamelName;
 #define MUTABLE_ROOT_LIST_CASE(type, name, CamelName) \
-  if (n == NULL && o == space->heap()->name()) n = #CamelName;
-static void DumpMaps(i::PagedSpace* space) {
-  i::HeapObjectIterator it(space);
-  i::ReadOnlyRoots roots(space->heap());
-  for (i::HeapObject o = it.Next(); !o.is_null(); o = it.Next()) {
-    if (!o->IsMap()) continue;
-    i::Map m = i::Map::cast(o);
-    const char* n = nullptr;
-    intptr_t p = static_cast<intptr_t>(m.ptr()) & (i::Page::kPageSize - 1);
-    int t = m->instance_type();
-    READ_ONLY_ROOT_LIST(RO_ROOT_LIST_CASE)
-    MUTABLE_ROOT_LIST(MUTABLE_ROOT_LIST_CASE)
-    if (n == nullptr) continue;
-    const char* sname = space->name();
-    i::PrintF("  (\"%s\", 0x%05" V8PRIxPTR "): (%d, \"%s\"),\n", sname, p, t,
-              n);
-  }
-}
+  if (root_name == nullptr && object == heap->name()) root_name = #CamelName;
+
+  i::ReadOnlyRoots roots(heap);
+  const char* root_name = nullptr;
+  i::Map map = i::Map::cast(object);
+  intptr_t root_ptr =
+      static_cast<intptr_t>(map.ptr()) & (i::Page::kPageSize - 1);
+
+  READ_ONLY_ROOT_LIST(RO_ROOT_LIST_CASE)
+  MUTABLE_ROOT_LIST(MUTABLE_ROOT_LIST_CASE)
+
+  if (root_name == nullptr) return;
+  i::PrintF("  (\"%s\", 0x%05" V8PRIxPTR "): (%d, \"%s\"),\n", space_name,
+            root_ptr, map->instance_type(), root_name);
+
 #undef MUTABLE_ROOT_LIST_CASE
 #undef RO_ROOT_LIST_CASE
+}
 
 static void DumpKnownObject(i::Heap* heap, const char* space_name,
                             i::HeapObject object) {
-#define RO_ROOT_LIST_CASE(type, name, CamelName)     \
-  if (root_name == NULL && object == roots.name()) { \
-    root_name = #CamelName;                          \
-    root_index = i::RootIndex::k##CamelName;         \
+#define RO_ROOT_LIST_CASE(type, name, CamelName)        \
+  if (root_name == nullptr && object == roots.name()) { \
+    root_name = #CamelName;                             \
+    root_index = i::RootIndex::k##CamelName;            \
   }
-#define ROOT_LIST_CASE(type, name, CamelName)        \
-  if (root_name == NULL && object == heap->name()) { \
-    root_name = #CamelName;                          \
-    root_index = i::RootIndex::k##CamelName;         \
+#define ROOT_LIST_CASE(type, name, CamelName)           \
+  if (root_name == nullptr && object == heap->name()) { \
+    root_name = #CamelName;                             \
+    root_index = i::RootIndex::k##CamelName;            \
   }
 
   i::ReadOnlyRoots roots(heap);
@@ -117,36 +117,50 @@
     i::PrintF("}\n");
 #undef DUMP_TYPE
 
-    // Dump the KNOWN_MAP table to the console.
-    i::PrintF("\n# List of known V8 maps.\n");
-    i::PrintF("KNOWN_MAPS = {\n");
-    DumpMaps(heap->read_only_space());
-    DumpMaps(heap->map_space());
-    i::PrintF("}\n");
-
-    // Dump the KNOWN_OBJECTS table to the console.
-    i::PrintF("\n# List of known V8 objects.\n");
-    i::PrintF("KNOWN_OBJECTS = {\n");
-    i::ReadOnlyHeapIterator ro_iterator(heap->read_only_heap());
-    for (i::HeapObject object = ro_iterator.Next(); !object.is_null();
-         object = ro_iterator.Next()) {
-      // Skip read-only heap maps, they will be reported elsewhere.
-      if (object->IsMap()) continue;
-      DumpKnownObject(heap, i::Heap::GetSpaceName(i::RO_SPACE), object);
-    }
-
-    i::PagedSpaces spit(heap);
-    for (i::PagedSpace* s = spit.next(); s != nullptr; s = spit.next()) {
-      i::HeapObjectIterator it(s);
-      // Code objects are generally platform-dependent.
-      if (s->identity() == i::CODE_SPACE || s->identity() == i::MAP_SPACE)
-        continue;
-      const char* sname = s->name();
-      for (i::HeapObject o = it.Next(); !o.is_null(); o = it.Next()) {
-        DumpKnownObject(heap, sname, o);
+    {
+      // Dump the KNOWN_MAP table to the console.
+      i::PrintF("\n# List of known V8 maps.\n");
+      i::PrintF("KNOWN_MAPS = {\n");
+      i::ReadOnlyHeapIterator ro_iterator(heap->read_only_heap());
+      for (i::HeapObject object = ro_iterator.Next(); !object.is_null();
+           object = ro_iterator.Next()) {
+        if (!object->IsMap()) continue;
+        DumpKnownMap(heap, i::Heap::GetSpaceName(i::RO_SPACE), object);
       }
+      i::HeapObjectIterator iterator(heap->map_space());
+      for (i::HeapObject object = iterator.Next(); !object.is_null();
+           object = iterator.Next()) {
+        if (!object->IsMap()) continue;
+        DumpKnownMap(heap, i::Heap::GetSpaceName(i::MAP_SPACE), object);
+      }
+      i::PrintF("}\n");
     }
-    i::PrintF("}\n");
+
+    {
+      // Dump the KNOWN_OBJECTS table to the console.
+      i::PrintF("\n# List of known V8 objects.\n");
+      i::PrintF("KNOWN_OBJECTS = {\n");
+      i::ReadOnlyHeapIterator ro_iterator(heap->read_only_heap());
+      for (i::HeapObject object = ro_iterator.Next(); !object.is_null();
+           object = ro_iterator.Next()) {
+        // Skip read-only heap maps, they will be reported elsewhere.
+        if (object->IsMap()) continue;
+        DumpKnownObject(heap, i::Heap::GetSpaceName(i::RO_SPACE), object);
+      }
+
+      i::PagedSpaces spit(heap);
+      for (i::PagedSpace* s = spit.next(); s != nullptr; s = spit.next()) {
+        i::HeapObjectIterator it(s);
+        // Code objects are generally platform-dependent.
+        if (s->identity() == i::CODE_SPACE || s->identity() == i::MAP_SPACE)
+          continue;
+        const char* sname = s->name();
+        for (i::HeapObject o = it.Next(); !o.is_null(); o = it.Next()) {
+          DumpKnownObject(heap, sname, o);
+        }
+      }
+      i::PrintF("}\n");
+    }
 
     // Dump frame markers
     i::PrintF("\n# List of known V8 Frame Markers.\n");