Improve VerifyNoFromSpaceRefsObjectVisitor logging

Remove read barriers in PrettyTypeOf to prevent recursive failures.

Pass down holder and offset information to
VerifyNoFromSpaceRefsFieldVisitor.

Test: test-art-host
Bug: 37531237
Change-Id: I704ec18ebecfc1ca2982b38f67a2f0788e59dfe9
diff --git a/runtime/gc/collector/concurrent_copying.cc b/runtime/gc/collector/concurrent_copying.cc
index bcf5008..792c191 100644
--- a/runtime/gc/collector/concurrent_copying.cc
+++ b/runtime/gc/collector/concurrent_copying.cc
@@ -1031,13 +1031,15 @@
   explicit VerifyNoFromSpaceRefsVisitor(ConcurrentCopying* collector)
       : collector_(collector) {}
 
-  void operator()(mirror::Object* ref) const
+  void operator()(mirror::Object* ref,
+                  MemberOffset offset = MemberOffset(0),
+                  mirror::Object* holder = nullptr) const
       REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
     if (ref == nullptr) {
       // OK.
       return;
     }
-    collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
+    collector_->AssertToSpaceInvariant(holder, offset, ref);
     if (kUseBakerReadBarrier) {
       CHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState())
           << "Ref " << ref << " " << ref->PrettyTypeOf()
@@ -1067,7 +1069,7 @@
     mirror::Object* ref =
         obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset);
     VerifyNoFromSpaceRefsVisitor visitor(collector_);
-    visitor(ref);
+    visitor(ref, offset, obj.Ptr());
   }
   void operator()(ObjPtr<mirror::Class> klass,
                   ObjPtr<mirror::Reference> ref) const
diff --git a/runtime/mirror/class-inl.h b/runtime/mirror/class-inl.h
index be3b937..9124a3a 100644
--- a/runtime/mirror/class-inl.h
+++ b/runtime/mirror/class-inl.h
@@ -823,7 +823,10 @@
 }
 
 inline const DexFile& Class::GetDexFile() {
-  return *GetDexCache()->GetDexFile();
+  // From-space version is the same as the to-space version since the dex file never changes.
+  // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant issues
+  // from PrettyTypeOf.
+  return *GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetDexFile();
 }
 
 inline bool Class::DescriptorEquals(const char* match) {
diff --git a/runtime/mirror/object.cc b/runtime/mirror/object.cc
index f5b9ab3..eabc29a 100644
--- a/runtime/mirror/object.cc
+++ b/runtime/mirror/object.cc
@@ -281,12 +281,16 @@
 }
 
 std::string Object::PrettyTypeOf() {
-  if (GetClass() == nullptr) {
+  // From-space version is the same as the to-space version since the dex file never changes.
+  // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
+  // issues.
+  ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
+  if (klass == nullptr) {
     return "(raw)";
   }
   std::string temp;
-  std::string result(PrettyDescriptor(GetClass()->GetDescriptor(&temp)));
-  if (IsClass()) {
+  std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
+  if (klass->IsClassClass()) {
     result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
   }
   return result;