Improve printing for Symbols.
authormvstanton@chromium.org <mvstanton@chromium.org>
Fri, 24 Oct 2014 12:36:45 +0000 (12:36 +0000)
committermvstanton@chromium.org <mvstanton@chromium.org>
Fri, 24 Oct 2014 13:12:07 +0000 (13:12 +0000)
Private symbols we create in the heap don't have names, but we can
resolve them to a constant string.

This gives handy debugger output like:

(gdb) job 0x2020c67d
0x2020c67d: [Symbol]
 - hash: 547385396
 - name: 0x20208091 <undefined> (uninitialized_symbol)
 - private: 1
 - own: 1
$7 = void
(gdb)

or with ShortPrint() in an array:

...
  [5]: 0x2020c67d <Symbol: 547385396 (uninitialized_symbol)>
...

Printing help for internal symbols

R=yangguo@chromium.org

Review URL: https://codereview.chromium.org/677633003

Cr-Commit-Position: refs/heads/master@{#24869}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24869 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/objects-printer.cc
src/objects.cc
src/objects.h

index d9a8676..75e2ed1 100644 (file)
@@ -395,6 +395,9 @@ void Symbol::SymbolPrint(std::ostream& os) {  // NOLINT
   HeapObject::PrintHeader(os, "Symbol");
   os << " - hash: " << Hash();
   os << "\n - name: " << Brief(name());
+  if (name()->IsUndefined()) {
+    os << " (" << PrivateSymbolToName() << ")";
+  }
   os << "\n - private: " << is_private();
   os << "\n - own: " << is_own();
   os << "\n";
index c09f801..3d18e3d 100644 (file)
@@ -1532,15 +1532,7 @@ void HeapObject::HeapObjectShortPrint(std::ostream& os) {  // NOLINT
     }
     case SYMBOL_TYPE: {
       Symbol* symbol = Symbol::cast(this);
-      os << "<Symbol: " << symbol->Hash();
-      if (!symbol->name()->IsUndefined()) {
-        os << " ";
-        HeapStringAllocator allocator;
-        StringStream accumulator(&allocator);
-        String::cast(symbol->name())->StringShortPrint(&accumulator);
-        os << accumulator.ToCString().get();
-      }
-      os << ">";
+      symbol->SymbolShortPrint(os);
       break;
     }
     case HEAP_NUMBER_TYPE: {
@@ -13616,6 +13608,31 @@ int JSObject::GetEnumElementKeys(FixedArray* storage) {
 }
 
 
+const char* Symbol::PrivateSymbolToName() const {
+  Heap* heap = GetIsolate()->heap();
+#define SYMBOL_CHECK_AND_PRINT(name) \
+  if (this == heap->name()) return #name;
+  PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT)
+#undef SYMBOL_CHECK_AND_PRINT
+  return "UNKNOWN";
+}
+
+
+void Symbol::SymbolShortPrint(std::ostream& os) {
+  os << "<Symbol: " << Hash();
+  if (!name()->IsUndefined()) {
+    os << " ";
+    HeapStringAllocator allocator;
+    StringStream accumulator(&allocator);
+    String::cast(name())->StringShortPrint(&accumulator);
+    os << accumulator.ToCString().get();
+  } else {
+    os << " (" << PrivateSymbolToName() << ")";
+  }
+  os << ">";
+}
+
+
 // StringSharedKeys are used as keys in the eval cache.
 class StringSharedKey : public HashTableKey {
  public:
index 5969ae6..be73a64 100644 (file)
@@ -8654,10 +8654,14 @@ class Symbol: public Name {
 
   typedef FixedBodyDescriptor<kNameOffset, kFlagsOffset, kSize> BodyDescriptor;
 
+  void SymbolShortPrint(std::ostream& os);
+
  private:
   static const int kPrivateBit = 0;
   static const int kOwnBit = 1;
 
+  const char* PrivateSymbolToName() const;
+
   DISALLOW_IMPLICIT_CONSTRUCTORS(Symbol);
 };