objects-debug.cc 29.5 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

#include "disassembler.h"
#include "disasm.h"
#include "macro-assembler.h"
33
#include "jsregexp.h"
34

35 36
namespace v8 {
namespace internal {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

#ifdef DEBUG

static const char* TypeToString(InstanceType type);


void Object::Print() {
  if (IsSmi()) {
    Smi::cast(this)->SmiPrint();
  } else if (IsFailure()) {
    Failure::cast(this)->FailurePrint();
  } else {
    HeapObject::cast(this)->HeapObjectPrint();
  }
  Flush();
}


void Object::PrintLn() {
  Print();
  PrintF("\n");
}


void Object::Verify() {
  if (IsSmi()) {
    Smi::cast(this)->SmiVerify();
  } else if (IsFailure()) {
    Failure::cast(this)->FailureVerify();
  } else {
    HeapObject::cast(this)->HeapObjectVerify();
  }
}


void Object::VerifyPointer(Object* p) {
  if (p->IsHeapObject()) {
    HeapObject::VerifyHeapPointer(p);
  } else {
    ASSERT(p->IsSmi());
  }
}


void Smi::SmiVerify() {
  ASSERT(IsSmi());
}


void Failure::FailureVerify() {
  ASSERT(IsFailure());
}


void HeapObject::PrintHeader(const char* id) {
  PrintF("%p: [%s]\n", this, id);
}


void HeapObject::HeapObjectPrint() {
  InstanceType instance_type = map()->instance_type();

  HandleScope scope;
  if (instance_type < FIRST_NONSTRING_TYPE) {
    String::cast(this)->StringPrint();
    return;
  }

  switch (instance_type) {
    case MAP_TYPE:
      Map::cast(this)->MapPrint();
      break;
    case HEAP_NUMBER_TYPE:
      HeapNumber::cast(this)->HeapNumberPrint();
      break;
    case FIXED_ARRAY_TYPE:
      FixedArray::cast(this)->FixedArrayPrint();
      break;
    case BYTE_ARRAY_TYPE:
      ByteArray::cast(this)->ByteArrayPrint();
      break;
    case FILLER_TYPE:
      PrintF("filler");
      break;
    case JS_OBJECT_TYPE:  // fall through
ager@chromium.org's avatar
ager@chromium.org committed
122
    case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
123
    case JS_ARRAY_TYPE:
124
    case JS_REGEXP_TYPE:
125 126 127 128 129 130 131 132
      JSObject::cast(this)->JSObjectPrint();
      break;
    case ODDBALL_TYPE:
      Oddball::cast(this)->to_string()->Print();
      break;
    case JS_FUNCTION_TYPE:
      JSFunction::cast(this)->JSFunctionPrint();
      break;
133 134 135
    case JS_GLOBAL_PROXY_TYPE:
      JSGlobalProxy::cast(this)->JSGlobalProxyPrint();
      break;
136 137 138 139 140 141 142
    case JS_GLOBAL_OBJECT_TYPE:
      JSGlobalObject::cast(this)->JSGlobalObjectPrint();
      break;
    case JS_BUILTINS_OBJECT_TYPE:
      JSBuiltinsObject::cast(this)->JSBuiltinsObjectPrint();
      break;
    case JS_VALUE_TYPE:
143
      PrintF("Value wrapper around:");
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
      JSValue::cast(this)->value()->Print();
      break;
    case CODE_TYPE:
      Code::cast(this)->CodePrint();
      break;
    case PROXY_TYPE:
      Proxy::cast(this)->ProxyPrint();
      break;
    case SHARED_FUNCTION_INFO_TYPE:
      SharedFunctionInfo::cast(this)->SharedFunctionInfoPrint();
      break;

#define MAKE_STRUCT_CASE(NAME, Name, name) \
  case NAME##_TYPE:                        \
    Name::cast(this)->Name##Print();       \
    break;
  STRUCT_LIST(MAKE_STRUCT_CASE)
#undef MAKE_STRUCT_CASE

    default:
      PrintF("UNKNOWN TYPE %d", map()->instance_type());
      UNREACHABLE();
      break;
  }
}


void HeapObject::HeapObjectVerify() {
  InstanceType instance_type = map()->instance_type();

  if (instance_type < FIRST_NONSTRING_TYPE) {
    String::cast(this)->StringVerify();
    return;
  }

  switch (instance_type) {
    case MAP_TYPE:
      Map::cast(this)->MapVerify();
      break;
    case HEAP_NUMBER_TYPE:
      HeapNumber::cast(this)->HeapNumberVerify();
      break;
    case FIXED_ARRAY_TYPE:
      FixedArray::cast(this)->FixedArrayVerify();
      break;
    case BYTE_ARRAY_TYPE:
      ByteArray::cast(this)->ByteArrayVerify();
      break;
    case CODE_TYPE:
      Code::cast(this)->CodeVerify();
      break;
    case ODDBALL_TYPE:
      Oddball::cast(this)->OddballVerify();
      break;
    case JS_OBJECT_TYPE:
ager@chromium.org's avatar
ager@chromium.org committed
199
    case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
200 201 202 203 204 205 206 207
      JSObject::cast(this)->JSObjectVerify();
      break;
    case JS_VALUE_TYPE:
      JSValue::cast(this)->JSValueVerify();
      break;
    case JS_FUNCTION_TYPE:
      JSFunction::cast(this)->JSFunctionVerify();
      break;
208 209 210
    case JS_GLOBAL_PROXY_TYPE:
      JSGlobalProxy::cast(this)->JSGlobalProxyVerify();
      break;
211 212 213 214 215 216 217 218 219
    case JS_GLOBAL_OBJECT_TYPE:
      JSGlobalObject::cast(this)->JSGlobalObjectVerify();
      break;
    case JS_BUILTINS_OBJECT_TYPE:
      JSBuiltinsObject::cast(this)->JSBuiltinsObjectVerify();
      break;
    case JS_ARRAY_TYPE:
      JSArray::cast(this)->JSArrayVerify();
      break;
220 221 222
    case JS_REGEXP_TYPE:
      JSRegExp::cast(this)->JSRegExpVerify();
      break;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    case FILLER_TYPE:
      break;
    case PROXY_TYPE:
      Proxy::cast(this)->ProxyVerify();
      break;
    case SHARED_FUNCTION_INFO_TYPE:
      SharedFunctionInfo::cast(this)->SharedFunctionInfoVerify();
      break;

#define MAKE_STRUCT_CASE(NAME, Name, name) \
  case NAME##_TYPE:                        \
    Name::cast(this)->Name##Verify();      \
    break;
    STRUCT_LIST(MAKE_STRUCT_CASE)
#undef MAKE_STRUCT_CASE

    default:
      UNREACHABLE();
      break;
  }
}


void HeapObject::VerifyHeapPointer(Object* p) {
  ASSERT(p->IsHeapObject());
  ASSERT(Heap::Contains(HeapObject::cast(p)));
}


void HeapNumber::HeapNumberVerify() {
  ASSERT(IsHeapNumber());
}


void ByteArray::ByteArrayPrint() {
  PrintF("byte array, data starts at %p", GetDataStartAddress());
}


void ByteArray::ByteArrayVerify() {
  ASSERT(IsByteArray());
}


void JSObject::PrintProperties() {
  if (HasFastProperties()) {
    for (DescriptorReader r(map()->instance_descriptors());
         !r.eos();
         r.advance()) {
      PrintF("   ");
      r.GetKey()->StringPrint();
      PrintF(": ");
      if (r.type() == FIELD) {
276
        FastPropertyAt(r.GetFieldIndex())->ShortPrint();
277 278 279 280 281 282 283 284 285
        PrintF(" (field at offset %d)\n", r.GetFieldIndex());
      } else if (r.type() ==  CONSTANT_FUNCTION) {
        r.GetConstantFunction()->ShortPrint();
        PrintF(" (constant function)\n");
      } else if (r.type() == CALLBACKS) {
        r.GetCallbacksObject()->ShortPrint();
        PrintF(" (callback)\n");
      } else if (r.type() == MAP_TRANSITION) {
        PrintF(" (map transition)\n");
286 287 288 289
      } else if (r.type() == CONSTANT_TRANSITION) {
        PrintF(" (constant transition)\n");
      } else if (r.type() == NULL_DESCRIPTOR) {
        PrintF(" (null descriptor)\n");
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
      } else {
        UNREACHABLE();
      }
    }
  } else {
    property_dictionary()->Print();
  }
}


void JSObject::PrintElements() {
  if (HasFastElements()) {
    FixedArray* p = FixedArray::cast(elements());
    for (int i = 0; i < p->length(); i++) {
      PrintF("   %d: ", i);
      p->get(i)->ShortPrint();
      PrintF("\n");
    }
  } else {
    elements()->Print();
  }
}


void JSObject::JSObjectPrint() {
  PrintF("%p: [JSObject]\n", this);
  PrintF(" - map = %p\n", map());
  PrintF(" - prototype = %p\n", GetPrototype());
  PrintF(" {\n");
  PrintProperties();
  PrintElements();
  PrintF(" }\n");
}


void JSObject::JSObjectVerify() {
  VerifyHeapPointer(properties());
  VerifyHeapPointer(elements());
  if (HasFastProperties()) {
    CHECK(map()->unused_property_fields() ==
330 331
          (map()->inobject_properties() + properties()->length() -
           map()->NextFreePropertyIndex()));
332 333 334 335 336 337
  }
}


static const char* TypeToString(InstanceType type) {
  switch (type) {
sgjesse@chromium.org's avatar
sgjesse@chromium.org committed
338
    case INVALID_TYPE: return "INVALID";
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    case MAP_TYPE: return "MAP";
    case HEAP_NUMBER_TYPE: return "HEAP_NUMBER";
    case SHORT_SYMBOL_TYPE:
    case MEDIUM_SYMBOL_TYPE:
    case LONG_SYMBOL_TYPE: return "SYMBOL";
    case SHORT_ASCII_SYMBOL_TYPE:
    case MEDIUM_ASCII_SYMBOL_TYPE:
    case LONG_ASCII_SYMBOL_TYPE: return "ASCII_SYMBOL";
    case SHORT_SLICED_SYMBOL_TYPE:
    case MEDIUM_SLICED_SYMBOL_TYPE:
    case LONG_SLICED_SYMBOL_TYPE: return "SLICED_SYMBOL";
    case SHORT_SLICED_ASCII_SYMBOL_TYPE:
    case MEDIUM_SLICED_ASCII_SYMBOL_TYPE:
    case LONG_SLICED_ASCII_SYMBOL_TYPE: return "SLICED_ASCII_SYMBOL";
    case SHORT_CONS_SYMBOL_TYPE:
    case MEDIUM_CONS_SYMBOL_TYPE:
    case LONG_CONS_SYMBOL_TYPE: return "CONS_SYMBOL";
    case SHORT_CONS_ASCII_SYMBOL_TYPE:
    case MEDIUM_CONS_ASCII_SYMBOL_TYPE:
    case LONG_CONS_ASCII_SYMBOL_TYPE: return "CONS_ASCII_SYMBOL";
    case SHORT_EXTERNAL_ASCII_SYMBOL_TYPE:
    case MEDIUM_EXTERNAL_ASCII_SYMBOL_TYPE:
    case LONG_EXTERNAL_ASCII_SYMBOL_TYPE:
    case SHORT_EXTERNAL_SYMBOL_TYPE:
    case MEDIUM_EXTERNAL_SYMBOL_TYPE:
    case LONG_EXTERNAL_SYMBOL_TYPE: return "EXTERNAL_SYMBOL";
    case SHORT_ASCII_STRING_TYPE:
    case MEDIUM_ASCII_STRING_TYPE:
    case LONG_ASCII_STRING_TYPE: return "ASCII_STRING";
    case SHORT_STRING_TYPE:
    case MEDIUM_STRING_TYPE:
    case LONG_STRING_TYPE: return "TWO_BYTE_STRING";
    case SHORT_CONS_STRING_TYPE:
    case MEDIUM_CONS_STRING_TYPE:
    case LONG_CONS_STRING_TYPE:
    case SHORT_CONS_ASCII_STRING_TYPE:
    case MEDIUM_CONS_ASCII_STRING_TYPE:
    case LONG_CONS_ASCII_STRING_TYPE: return "CONS_STRING";
    case SHORT_SLICED_STRING_TYPE:
    case MEDIUM_SLICED_STRING_TYPE:
    case LONG_SLICED_STRING_TYPE:
    case SHORT_SLICED_ASCII_STRING_TYPE:
    case MEDIUM_SLICED_ASCII_STRING_TYPE:
    case LONG_SLICED_ASCII_STRING_TYPE: return "SLICED_STRING";
    case SHORT_EXTERNAL_ASCII_STRING_TYPE:
    case MEDIUM_EXTERNAL_ASCII_STRING_TYPE:
    case LONG_EXTERNAL_ASCII_STRING_TYPE:
    case SHORT_EXTERNAL_STRING_TYPE:
    case MEDIUM_EXTERNAL_STRING_TYPE:
    case LONG_EXTERNAL_STRING_TYPE: return "EXTERNAL_STRING";
    case FIXED_ARRAY_TYPE: return "FIXED_ARRAY";
    case BYTE_ARRAY_TYPE: return "BYTE_ARRAY";
    case FILLER_TYPE: return "FILLER";
    case JS_OBJECT_TYPE: return "JS_OBJECT";
ager@chromium.org's avatar
ager@chromium.org committed
393
    case JS_CONTEXT_EXTENSION_OBJECT_TYPE: return "JS_CONTEXT_EXTENSION_OBJECT";
394 395 396 397 398
    case ODDBALL_TYPE: return "ODDBALL";
    case SHARED_FUNCTION_INFO_TYPE: return "SHARED_FUNCTION_INFO";
    case JS_FUNCTION_TYPE: return "JS_FUNCTION";
    case CODE_TYPE: return "CODE";
    case JS_ARRAY_TYPE: return "JS_ARRAY";
399
    case JS_REGEXP_TYPE: return "JS_REGEXP";
400 401 402
    case JS_VALUE_TYPE: return "JS_VALUE";
    case JS_GLOBAL_OBJECT_TYPE: return "JS_GLOBAL_OBJECT";
    case JS_BUILTINS_OBJECT_TYPE: return "JS_BUILTINS_OBJECT";
403
    case JS_GLOBAL_PROXY_TYPE: return "JS_GLOBAL_PROXY";
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    case PROXY_TYPE: return "PROXY";
    case SMI_TYPE: return "SMI";
#define MAKE_STRUCT_CASE(NAME, Name, name) case NAME##_TYPE: return #NAME;
  STRUCT_LIST(MAKE_STRUCT_CASE)
#undef MAKE_STRUCT_CASE
  }
  return "UNKNOWN";
}


void Map::MapPrint() {
  HeapObject::PrintHeader("Map");
  PrintF(" - type: %s\n", TypeToString(instance_type()));
  PrintF(" - instance size: %d\n", instance_size());
  PrintF(" - unused property fields: %d\n", unused_property_fields());
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  if (is_hidden_prototype()) {
    PrintF(" - hidden_prototype\n");
  }
  if (has_named_interceptor()) {
    PrintF(" - named_interceptor\n");
  }
  if (has_indexed_interceptor()) {
    PrintF(" - indexed_interceptor\n");
  }
  if (is_undetectable()) {
    PrintF(" - undetectable\n");
  }
  if (has_instance_call_handler()) {
    PrintF(" - instance_call_handler\n");
  }
  if (is_access_check_needed()) {
    PrintF(" - access_check_needed\n");
  }
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  PrintF(" - instance descriptors: ");
  instance_descriptors()->ShortPrint();
  PrintF("\n - prototype: ");
  prototype()->ShortPrint();
  PrintF("\n - constructor: ");
  constructor()->ShortPrint();
  PrintF("\n");
}


void Map::MapVerify() {
  ASSERT(!Heap::InNewSpace(this));
  ASSERT(FIRST_TYPE <= instance_type() && instance_type() <= LAST_TYPE);
  ASSERT(kPointerSize <= instance_size()
         && instance_size() < Heap::Capacity());
  VerifyHeapPointer(prototype());
  VerifyHeapPointer(instance_descriptors());
}


void FixedArray::FixedArrayPrint() {
  HeapObject::PrintHeader("FixedArray");
  PrintF(" - length: %d", length());
  for (int i = 0; i < length(); i++) {
    PrintF("\n  [%d]: ", i);
    get(i)->ShortPrint();
  }
  PrintF("\n");
}


void FixedArray::FixedArrayVerify() {
  for (int i = 0; i < length(); i++) {
    Object* e = get(i);
    if (e->IsHeapObject()) {
      VerifyHeapPointer(e);
    } else {
      e->Verify();
    }
  }
}


void JSValue::JSValuePrint() {
  HeapObject::PrintHeader("ValueObject");
  value()->Print();
}


void JSValue::JSValueVerify() {
  Object* v = value();
  if (v->IsHeapObject()) {
    VerifyHeapPointer(v);
  }
}


void String::StringPrint() {
495
  if (StringShape(this).IsSymbol()) {
496
    PrintF("#");
497
  } else if (StringShape(this).IsCons()) {
498 499 500 501 502 503
    PrintF("c\"");
  } else {
    PrintF("\"");
  }

  for (int i = 0; i < length(); i++) {
504
    PrintF("%c", Get(i));
505 506
  }

507
  if (!StringShape(this).IsSymbol()) PrintF("\"");
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
}


void String::StringVerify() {
  CHECK(IsString());
  CHECK(length() >= 0 && length() <= Smi::kMaxValue);
  if (IsSymbol()) {
    CHECK(!Heap::InNewSpace(this));
  }
}


void JSFunction::JSFunctionPrint() {
  HeapObject::PrintHeader("Function");
  PrintF(" - map = 0x%p\n", map());
  PrintF(" - is boilerplate: %s\n", IsBoilerplate() ? "yes" : "no");
  PrintF(" - initial_map = ");
  if (has_initial_map()) {
    initial_map()->ShortPrint();
  }
  PrintF("\n - shared_info = ");
  shared()->ShortPrint();
  PrintF("\n   - name = ");
  shared()->name()->Print();
  PrintF("\n - context = ");
  unchecked_context()->ShortPrint();
  PrintF("\n - code = ");
  code()->ShortPrint();
  PrintF("\n");

  PrintProperties();
  PrintElements();

  PrintF("\n");
}


void JSFunction::JSFunctionVerify() {
  CHECK(IsJSFunction());
  VerifyObjectField(kPrototypeOrInitialMapOffset);
}


void SharedFunctionInfo::SharedFunctionInfoPrint() {
  HeapObject::PrintHeader("SharedFunctionInfo");
  PrintF(" - name: ");
  name()->ShortPrint();
  PrintF("\n - expected_nof_properties: %d", expected_nof_properties());
556
  PrintF("\n - instance class name = ");
557
  instance_class_name()->Print();
558
  PrintF("\n - code = ");
559
  code()->ShortPrint();
560
  PrintF("\n - source code = ");
561 562 563 564 565 566 567 568 569
  GetSourceCode()->ShortPrint();
  // Script files are often large, hard to read.
  // PrintF("\n - script =");
  // script()->Print();
  PrintF("\n - function token position = %d", function_token_position());
  PrintF("\n - start position = %d", start_position());
  PrintF("\n - end position = %d", end_position());
  PrintF("\n - is expression = %d", is_expression());
  PrintF("\n - debug info = ");
570
  debug_info()->ShortPrint();
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  PrintF("\n - length = %d", length());
  PrintF("\n");
}

void SharedFunctionInfo::SharedFunctionInfoVerify() {
  CHECK(IsSharedFunctionInfo());
  VerifyObjectField(kNameOffset);
  VerifyObjectField(kCodeOffset);
  VerifyObjectField(kInstanceClassNameOffset);
  VerifyObjectField(kExternalReferenceDataOffset);
  VerifyObjectField(kScriptOffset);
  VerifyObjectField(kDebugInfoOffset);
}


586 587 588 589 590 591 592 593 594 595 596 597 598 599
void JSGlobalProxy::JSGlobalProxyPrint() {
  PrintF("global_proxy");
  JSObjectPrint();
  PrintF("context : ");
  context()->ShortPrint();
  PrintF("\n");
}


void JSGlobalProxy::JSGlobalProxyVerify() {
  CHECK(IsJSGlobalProxy());
  JSObjectVerify();
  VerifyObjectField(JSGlobalProxy::kContextOffset);
  // Make sure that this object has no properties, elements.
feng@chromium.org's avatar
feng@chromium.org committed
600 601
  CHECK_EQ(0, properties()->length());
  CHECK_EQ(0, elements()->length());
602 603 604
}


605 606 607
void JSGlobalObject::JSGlobalObjectPrint() {
  PrintF("global ");
  JSObjectPrint();
608 609 610
  PrintF("global context : ");
  global_context()->ShortPrint();
  PrintF("\n");
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
}


void JSGlobalObject::JSGlobalObjectVerify() {
  CHECK(IsJSGlobalObject());
  JSObjectVerify();
  for (int i = GlobalObject::kBuiltinsOffset;
       i < JSGlobalObject::kSize;
       i += kPointerSize) {
    VerifyObjectField(i);
  }
}


void JSBuiltinsObject::JSBuiltinsObjectPrint() {
  PrintF("builtins ");
  JSObjectPrint();
}


void JSBuiltinsObject::JSBuiltinsObjectVerify() {
  CHECK(IsJSBuiltinsObject());
  JSObjectVerify();
  for (int i = GlobalObject::kBuiltinsOffset;
       i < JSBuiltinsObject::kSize;
       i += kPointerSize) {
    VerifyObjectField(i);
  }
}


void Oddball::OddballVerify() {
  CHECK(IsOddball());
  VerifyHeapPointer(to_string());
  Object* number = to_number();
  if (number->IsHeapObject()) {
    ASSERT(number == Heap::nan_value());
  } else {
    ASSERT(number->IsSmi());
    int value = Smi::cast(number)->value();
    ASSERT(value == 0 || value == 1 || value == -1);
  }
}


void Code::CodePrint() {
  HeapObject::PrintHeader("Code");
658
#ifdef ENABLE_DISASSEMBLER
659
  Disassemble(NULL);
660
#endif
661 662 663 664
}


void Code::CodeVerify() {
665
  CHECK(ic_flag() == IC_TARGET_IS_ADDRESS);
666 667
  CHECK(IsAligned(reinterpret_cast<intptr_t>(instruction_start()),
                  static_cast<intptr_t>(kCodeAlignment)));
668
  Address last_gc_pc = NULL;
669 670
  for (RelocIterator it(this); !it.done(); it.next()) {
    it.rinfo()->Verify();
671
    // Ensure that GC will not iterate twice over the same pointer.
672
    if (RelocInfo::IsGCRelocMode(it.rinfo()->rmode())) {
673 674 675
      CHECK(it.rinfo()->pc() != last_gc_pc);
      last_gc_pc = it.rinfo()->pc();
    }
676 677 678 679 680 681 682 683 684 685 686
  }
}


void JSArray::JSArrayVerify() {
  JSObjectVerify();
  ASSERT(length()->IsNumber() || length()->IsUndefined());
  ASSERT(elements()->IsUndefined() || elements()->IsFixedArray());
}


687 688
void JSRegExp::JSRegExpVerify() {
  JSObjectVerify();
689 690 691 692 693 694
  ASSERT(data()->IsUndefined() || data()->IsFixedArray());
  switch (TypeTag()) {
    case JSRegExp::ATOM: {
      FixedArray* arr = FixedArray::cast(data());
      ASSERT(arr->get(JSRegExp::kAtomPatternIndex)->IsString());
      break;
695
    }
696
    case JSRegExp::IRREGEXP: {
697 698
      bool is_native = RegExpImpl::UseNativeRegexp();

699
      FixedArray* arr = FixedArray::cast(data());
700 701 702 703 704 705 706 707
      Object* ascii_data = arr->get(JSRegExp::kIrregexpASCIICodeIndex);
      ASSERT(ascii_data->IsTheHole()
          || (is_native ? ascii_data->IsCode() : ascii_data->IsByteArray()));
      Object* uc16_data = arr->get(JSRegExp::kIrregexpUC16CodeIndex);
      ASSERT(uc16_data->IsTheHole()
          || (is_native ? uc16_data->IsCode() : uc16_data->IsByteArray()));
      ASSERT(arr->get(JSRegExp::kIrregexpCaptureCountIndex)->IsSmi());
      ASSERT(arr->get(JSRegExp::kIrregexpMaxRegisterCountIndex)->IsSmi());
708 709 710 711 712 713
      break;
    }
    default:
      ASSERT_EQ(JSRegExp::NOT_COMPILED, TypeTag());
      ASSERT(data()->IsUndefined());
      break;
714
  }
715 716 717
}


718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
void Proxy::ProxyPrint() {
  PrintF("proxy to %p", proxy());
}


void Proxy::ProxyVerify() {
  ASSERT(IsProxy());
}


void Dictionary::Print() {
  int capacity = Capacity();
  for (int i = 0; i < capacity; i++) {
    Object* k = KeyAt(i);
    if (IsKey(k)) {
      PrintF(" ");
      if (k->IsString()) {
        String::cast(k)->StringPrint();
      } else {
        k->ShortPrint();
      }
      PrintF(": ");
      ValueAt(i)->ShortPrint();
      PrintF("\n");
    }
  }
}


void AccessorInfo::AccessorInfoVerify() {
  CHECK(IsAccessorInfo());
  VerifyPointer(getter());
  VerifyPointer(setter());
  VerifyPointer(name());
  VerifyPointer(data());
  VerifyPointer(flag());
}

void AccessorInfo::AccessorInfoPrint() {
757
  HeapObject::PrintHeader("AccessorInfo");
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  PrintF("\n - getter: ");
  getter()->ShortPrint();
  PrintF("\n - setter: ");
  setter()->ShortPrint();
  PrintF("\n - name: ");
  name()->ShortPrint();
  PrintF("\n - data: ");
  data()->ShortPrint();
  PrintF("\n - flag: ");
  flag()->ShortPrint();
}

void AccessCheckInfo::AccessCheckInfoVerify() {
  CHECK(IsAccessCheckInfo());
  VerifyPointer(named_callback());
  VerifyPointer(indexed_callback());
  VerifyPointer(data());
}

void AccessCheckInfo::AccessCheckInfoPrint() {
778
  HeapObject::PrintHeader("AccessCheckInfo");
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
  PrintF("\n - named_callback: ");
  named_callback()->ShortPrint();
  PrintF("\n - indexed_callback: ");
  indexed_callback()->ShortPrint();
  PrintF("\n - data: ");
  data()->ShortPrint();
}

void InterceptorInfo::InterceptorInfoVerify() {
  CHECK(IsInterceptorInfo());
  VerifyPointer(getter());
  VerifyPointer(setter());
  VerifyPointer(query());
  VerifyPointer(deleter());
  VerifyPointer(enumerator());
  VerifyPointer(data());
}

void InterceptorInfo::InterceptorInfoPrint() {
798
  HeapObject::PrintHeader("InterceptorInfo");
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
  PrintF("\n - getter: ");
  getter()->ShortPrint();
  PrintF("\n - setter: ");
  setter()->ShortPrint();
  PrintF("\n - query: ");
  query()->ShortPrint();
  PrintF("\n - deleter: ");
  deleter()->ShortPrint();
  PrintF("\n - enumerator: ");
  enumerator()->ShortPrint();
  PrintF("\n - data: ");
  data()->ShortPrint();
}

void CallHandlerInfo::CallHandlerInfoVerify() {
  CHECK(IsCallHandlerInfo());
  VerifyPointer(callback());
  VerifyPointer(data());
}

void CallHandlerInfo::CallHandlerInfoPrint() {
820
  HeapObject::PrintHeader("CallHandlerInfo");
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
  PrintF("\n - callback: ");
  callback()->ShortPrint();
  PrintF("\n - data: ");
  data()->ShortPrint();
}

void TemplateInfo::TemplateInfoVerify() {
  VerifyPointer(tag());
  VerifyPointer(property_list());
}

void FunctionTemplateInfo::FunctionTemplateInfoVerify() {
  CHECK(IsFunctionTemplateInfo());
  TemplateInfoVerify();
  VerifyPointer(serial_number());
  VerifyPointer(call_code());
  VerifyPointer(property_accessors());
  VerifyPointer(prototype_template());
  VerifyPointer(parent_template());
  VerifyPointer(named_property_handler());
  VerifyPointer(indexed_property_handler());
  VerifyPointer(instance_template());
  VerifyPointer(signature());
  VerifyPointer(access_check_info());
}

void FunctionTemplateInfo::FunctionTemplateInfoPrint() {
848
  HeapObject::PrintHeader("FunctionTemplateInfo");
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
  PrintF("\n - tag: ");
  tag()->ShortPrint();
  PrintF("\n - property_list: ");
  property_list()->ShortPrint();
  PrintF("\n - serial_number: ");
  serial_number()->ShortPrint();
  PrintF("\n - call_code: ");
  call_code()->ShortPrint();
  PrintF("\n - property_accessors: ");
  property_accessors()->ShortPrint();
  PrintF("\n - prototype_template: ");
  prototype_template()->ShortPrint();
  PrintF("\n - parent_template: ");
  parent_template()->ShortPrint();
  PrintF("\n - named_property_handler: ");
  named_property_handler()->ShortPrint();
  PrintF("\n - indexed_property_handler: ");
  indexed_property_handler()->ShortPrint();
  PrintF("\n - instance_template: ");
  instance_template()->ShortPrint();
  PrintF("\n - signature: ");
  signature()->ShortPrint();
  PrintF("\n - access_check_info: ");
  access_check_info()->ShortPrint();
  PrintF("\n - hidden_prototype: %s", hidden_prototype() ? "true" : "false");
  PrintF("\n - undetectable: %s", undetectable() ? "true" : "false");
  PrintF("\n - need_access_check: %s", needs_access_check() ? "true" : "false");
}

void ObjectTemplateInfo::ObjectTemplateInfoVerify() {
  CHECK(IsObjectTemplateInfo());
  TemplateInfoVerify();
  VerifyPointer(constructor());
882
  VerifyPointer(internal_field_count());
883 884 885
}

void ObjectTemplateInfo::ObjectTemplateInfoPrint() {
886
  HeapObject::PrintHeader("ObjectTemplateInfo");
887
  PrintF("\n - constructor: ");
888
  constructor()->ShortPrint();
889 890
  PrintF("\n - internal_field_count: ");
  internal_field_count()->ShortPrint();
891 892 893 894 895 896 897 898 899
}

void SignatureInfo::SignatureInfoVerify() {
  CHECK(IsSignatureInfo());
  VerifyPointer(receiver());
  VerifyPointer(args());
}

void SignatureInfo::SignatureInfoPrint() {
900
  HeapObject::PrintHeader("SignatureInfo");
901
  PrintF("\n - receiver: ");
902
  receiver()->ShortPrint();
903
  PrintF("\n - args: ");
904 905 906 907 908 909 910 911 912
  args()->ShortPrint();
}

void TypeSwitchInfo::TypeSwitchInfoVerify() {
  CHECK(IsTypeSwitchInfo());
  VerifyPointer(types());
}

void TypeSwitchInfo::TypeSwitchInfoPrint() {
913
  HeapObject::PrintHeader("TypeSwitchInfo");
914
  PrintF("\n - types: ");
915 916 917 918 919 920 921 922 923 924
  types()->ShortPrint();
}


void Script::ScriptVerify() {
  CHECK(IsScript());
  VerifyPointer(source());
  VerifyPointer(name());
  line_offset()->SmiVerify();
  column_offset()->SmiVerify();
925 926
  VerifyPointer(data());
  VerifyPointer(wrapper());
927
  type()->SmiVerify();
928 929
  VerifyPointer(line_ends());
  VerifyPointer(id());
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
}


void Script::ScriptPrint() {
  HeapObject::PrintHeader("Script");
  PrintF("\n - source: ");
  source()->ShortPrint();
  PrintF("\n - name: ");
  name()->ShortPrint();
  PrintF("\n - line_offset: ");
  line_offset()->ShortPrint();
  PrintF("\n - column_offset: ");
  column_offset()->ShortPrint();
  PrintF("\n - type: ");
  type()->ShortPrint();
945 946
  PrintF("\n - id: ");
  id()->ShortPrint();
947 948 949 950 951 952 953 954 955 956 957 958 959 960
  PrintF("\n");
}


void DebugInfo::DebugInfoVerify() {
  CHECK(IsDebugInfo());
  VerifyPointer(shared());
  VerifyPointer(original_code());
  VerifyPointer(code());
  VerifyPointer(break_points());
}


void DebugInfo::DebugInfoPrint() {
961 962
  HeapObject::PrintHeader("DebugInfo");
  PrintF("\n - shared: ");
963
  shared()->ShortPrint();
964
  PrintF("\n - original_code: ");
965
  original_code()->ShortPrint();
966
  PrintF("\n - code: ");
967
  code()->ShortPrint();
968 969
  PrintF("\n - break_points: ");
  break_points()->Print();
970 971 972 973 974 975 976 977 978 979 980 981 982
}


void BreakPointInfo::BreakPointInfoVerify() {
  CHECK(IsBreakPointInfo());
  code_position()->SmiVerify();
  source_position()->SmiVerify();
  statement_position()->SmiVerify();
  VerifyPointer(break_point_objects());
}


void BreakPointInfo::BreakPointInfoPrint() {
983 984 985 986 987
  HeapObject::PrintHeader("BreakPointInfo");
  PrintF("\n - code_position: %d", code_position());
  PrintF("\n - source_position: %d", source_position());
  PrintF("\n - statement_position: %d", statement_position());
  PrintF("\n - break_point_objects: ");
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  break_point_objects()->ShortPrint();
}


void JSObject::IncrementSpillStatistics(SpillInformation* info) {
  info->number_of_objects_++;
  // Named properties
  if (HasFastProperties()) {
    info->number_of_objects_with_fast_properties_++;
    info->number_of_fast_used_fields_   += map()->NextFreePropertyIndex();
    info->number_of_fast_unused_fields_ += map()->unused_property_fields();
  } else {
    Dictionary* dict = property_dictionary();
    info->number_of_slow_used_properties_ += dict->NumberOfElements();
    info->number_of_slow_unused_properties_ +=
        dict->Capacity() - dict->NumberOfElements();
  }
  // Indexed properties
  if (HasFastElements()) {
    info->number_of_objects_with_fast_elements_++;
    int holes = 0;
    FixedArray* e = FixedArray::cast(elements());
    int len = e->length();
    for (int i = 0; i < len; i++) {
      if (e->get(i) == Heap::the_hole_value()) holes++;
    }
    info->number_of_fast_used_elements_   += len - holes;
    info->number_of_fast_unused_elements_ += holes;
  } else {
    Dictionary* dict = element_dictionary();
    info->number_of_slow_used_elements_ += dict->NumberOfElements();
    info->number_of_slow_unused_elements_ +=
        dict->Capacity() - dict->NumberOfElements();
  }
}


void JSObject::SpillInformation::Clear() {
  number_of_objects_ = 0;
  number_of_objects_with_fast_properties_ = 0;
  number_of_objects_with_fast_elements_ = 0;
  number_of_fast_used_fields_ = 0;
  number_of_fast_unused_fields_ = 0;
  number_of_slow_used_properties_ = 0;
  number_of_slow_unused_properties_ = 0;
  number_of_fast_used_elements_ = 0;
  number_of_fast_unused_elements_ = 0;
  number_of_slow_used_elements_ = 0;
  number_of_slow_unused_elements_ = 0;
}

void JSObject::SpillInformation::Print() {
  PrintF("\n  JSObject Spill Statistics (#%d):\n", number_of_objects_);

  PrintF("    - fast properties (#%d): %d (used) %d (unused)\n",
         number_of_objects_with_fast_properties_,
         number_of_fast_used_fields_, number_of_fast_unused_fields_);

  PrintF("    - slow properties (#%d): %d (used) %d (unused)\n",
         number_of_objects_ - number_of_objects_with_fast_properties_,
         number_of_slow_used_properties_, number_of_slow_unused_properties_);

  PrintF("    - fast elements (#%d): %d (used) %d (unused)\n",
         number_of_objects_with_fast_elements_,
         number_of_fast_used_elements_, number_of_fast_unused_elements_);

  PrintF("    - slow elements (#%d): %d (used) %d (unused)\n",
         number_of_objects_ - number_of_objects_with_fast_elements_,
         number_of_slow_used_elements_, number_of_slow_unused_elements_);

  PrintF("\n");
}


void DescriptorArray::PrintDescriptors() {
  PrintF("Descriptor array  %d\n", number_of_descriptors());
  int number = 0;
  for (DescriptorReader r(this); !r.eos(); r.advance()) {
    Descriptor desc;
    r.Get(&desc);
    PrintF(" %d: ", number++);
    desc.Print();
  }
  PrintF("\n");
}


1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
bool DescriptorArray::IsSortedNoDuplicates() {
  String* current_key = NULL;
  uint32_t current = 0;
  for (DescriptorReader r(this); !r.eos(); r.advance()) {
    String* key = r.GetKey();
    if (key == current_key) {
      PrintDescriptors();
      return false;
    }
    current_key = key;
    uint32_t hash = r.GetKey()->Hash();
    if (hash < current) {
      PrintDescriptors();
      return false;
    }
    current = hash;
  }
  return true;
}


1096 1097 1098
#endif  // DEBUG

} }  // namespace v8::internal