]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/debug.cpp
io.streams.256color: faster by caching styles
[factor.git] / vm / debug.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 4cee4c7..4ffa5ac
 #include "master.hpp"
 
-namespace factor
-{
-
-std::ostream &operator<<(std::ostream &out, const string *str)
-{
-       for(cell i = 0; i < string_capacity(str); i++)
-               out << (char)str->data()[i];
-       return out;
-}
+using namespace std;
+
+namespace factor {
 
-void factor_vm::print_word(word *word, cell nesting)
-{
-       if(tagged<object>(word->vocabulary).type_p(STRING_TYPE))
-               std::cout << untag<string>(word->vocabulary) << ":";
-
-       if(tagged<object>(word->name).type_p(STRING_TYPE))
-               std::cout << untag<string>(word->name);
-       else
-       {
-               std::cout << "#<not a string: ";
-               print_nested_obj(word->name,nesting);
-               std::cout << ">";
-       }
+bool factor_print_p = true;
+
+ostream& operator<<(ostream& out, const string* str) {
+  for (cell i = 0; i < string_capacity(str); i++)
+    out << (char)str->data()[i];
+  return out;
 }
 
-void factor_vm::print_factor_string(string *str)
-{
-       std::cout << '"' << str << '"';
+void factor_vm::print_word(ostream& out, word* word, cell nesting) {
+  if (TAG(word->vocabulary) == STRING_TYPE)
+    out << untag<string>(word->vocabulary) << ":";
+
+  if (TAG(word->name) == STRING_TYPE)
+    out << untag<string>(word->name);
+  else {
+    out << "#<not a string: ";
+    print_nested_obj(out, word->name, nesting);
+    out << ">";
+  }
 }
 
-void factor_vm::print_array(array *array, cell nesting)
-{
-       cell length = array_capacity(array);
-       cell i;
-       bool trimmed;
-
-       if(length > 10 && !full_output)
-       {
-               trimmed = true;
-               length = 10;
-       }
-       else
-               trimmed = false;
-
-       for(i = 0; i < length; i++)
-       {
-               std::cout << " ";
-               print_nested_obj(array_nth(array,i),nesting);
-       }
-
-       if(trimmed)
-               std::cout << "...";
+void factor_vm::print_factor_string(ostream& out, string* str) {
+  out << '"' << str << '"';
 }
 
-void factor_vm::print_tuple(tuple *tuple, cell nesting)
-{
-       tuple_layout *layout = untag<tuple_layout>(tuple->layout);
-       cell length = to_fixnum(layout->size);
-
-       std::cout << " ";
-       print_nested_obj(layout->klass,nesting);
-
-       bool trimmed;
-       if(length > 10 && !full_output)
-       {
-               trimmed = true;
-               length = 10;
-       }
-       else
-               trimmed = false;
-
-       for(cell i = 0; i < length; i++)
-       {
-               std::cout << " ";
-               print_nested_obj(tuple->data()[i],nesting);
-       }
-
-       if(trimmed)
-               std::cout << "...";
+void factor_vm::print_array(ostream& out, array* array, cell nesting) {
+  cell length = array_capacity(array);
+  cell i;
+  bool trimmed;
+
+  if (length > 10 && !full_output) {
+    trimmed = true;
+    length = 10;
+  } else
+    trimmed = false;
+
+  for (i = 0; i < length; i++) {
+    out << " ";
+    print_nested_obj(out, array_nth(array, i), nesting);
+  }
+
+  if (trimmed)
+    out << "...";
 }
 
-void factor_vm::print_nested_obj(cell obj, fixnum nesting)
-{
-       if(nesting <= 0 && !full_output)
-       {
-               std::cout << " ... ";
-               return;
-       }
-
-       quotation *quot;
-
-       switch(tagged<object>(obj).type())
-       {
-       case FIXNUM_TYPE:
-               std::cout << untag_fixnum(obj);
-               break;
-       case WORD_TYPE:
-               print_word(untag<word>(obj),nesting - 1);
-               break;
-       case STRING_TYPE:
-               print_factor_string(untag<string>(obj));
-               break;
-       case F_TYPE:
-               std::cout << "f";
-               break;
-       case TUPLE_TYPE:
-               std::cout << "T{";
-               print_tuple(untag<tuple>(obj),nesting - 1);
-               std::cout << " }";
-               break;
-       case ARRAY_TYPE:
-               std::cout << "{";
-               print_array(untag<array>(obj),nesting - 1);
-               std::cout << " }";
-               break;
-       case QUOTATION_TYPE:
-               std::cout << "[";
-               quot = untag<quotation>(obj);
-               print_array(untag<array>(quot->array),nesting - 1);
-               std::cout << " ]";
-               break;
-       default:
-               std::cout << "#<type " << tagged<object>(obj).type() << " @ ";
-               std::cout << std::hex << obj << std::dec << ">";
-               break;
-       }
+void factor_vm::print_alien(ostream& out, alien* alien, cell nesting) {
+  if (to_boolean(alien->expired))
+    out << "#<expired alien>";
+  else if (to_boolean(alien->base)) {
+    out << "#<displaced alien " << alien->displacement << "+";
+    print_nested_obj(out, alien->base, nesting);
+    out << ">";
+  } else {
+    out << "#<alien " << (void*)alien->address << ">";
+  }
 }
 
-void factor_vm::print_obj(cell obj)
-{
-       print_nested_obj(obj,10);
+void factor_vm::print_byte_array(ostream& out, byte_array* array, cell nesting) {
+  (void)nesting;
+  cell length = array->capacity;
+  cell i;
+  bool trimmed;
+  unsigned char* data = array->data<unsigned char>();
+
+  if (length > 16 && !full_output) {
+    trimmed = true;
+    length = 16;
+  } else
+    trimmed = false;
+
+  for (i = 0; i < length; i++) {
+    out << " " << (unsigned) data[i];
+  }
+
+  if (trimmed)
+    out << "...";
 }
 
-void factor_vm::print_objects(cell *start, cell *end)
-{
-       for(; start <= end; start++)
-       {
-               print_obj(*start);
-               std::cout << std::endl;
-       }
+void factor_vm::print_tuple(ostream& out, tuple* tuple, cell nesting) {
+  tuple_layout* layout = untag<tuple_layout>(tuple->layout);
+  cell length = to_cell(layout->size);
+
+  out << " ";
+  print_nested_obj(out, layout->klass, nesting);
+
+  bool trimmed;
+  if (length > 10 && !full_output) {
+    trimmed = true;
+    length = 10;
+  } else
+    trimmed = false;
+
+  for (cell i = 0; i < length; i++) {
+    out << " ";
+    print_nested_obj(out, tuple->data()[i], nesting);
+  }
+
+  if (trimmed)
+    out << "...";
 }
 
-void factor_vm::print_datastack()
-{
-       std::cout << "==== DATA STACK:\n";
-       print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack);
+void factor_vm::print_nested_obj(ostream& out, cell obj, fixnum nesting) {
+  if (nesting <= 0 && !full_output) {
+    out << " ... ";
+    return;
+  }
+
+  quotation* quot;
+
+  switch (TAG(obj)) {
+    case FIXNUM_TYPE:
+      out << untag_fixnum(obj);
+      break;
+    case FLOAT_TYPE:
+      out << untag_float(obj);
+      break;
+    case WORD_TYPE:
+      print_word(out, untag<word>(obj), nesting - 1);
+      break;
+    case STRING_TYPE:
+      print_factor_string(out, untag<string>(obj));
+      break;
+    case F_TYPE:
+      out << "f";
+      break;
+    case TUPLE_TYPE:
+      out << "T{";
+      print_tuple(out, untag<tuple>(obj), nesting - 1);
+      out << " }";
+      break;
+    case WRAPPER_TYPE:
+      out << "W{ ";
+      print_nested_obj(out, untag<wrapper>(obj)->object, nesting - 1);
+      out << " }";
+      break;
+    case BYTE_ARRAY_TYPE:
+      out << "B{";
+      print_byte_array(out, untag<byte_array>(obj), nesting - 1);
+      out << " }";
+      break;
+    case ARRAY_TYPE:
+      out << "{";
+      print_array(out, untag<array>(obj), nesting - 1);
+      out << " }";
+      break;
+    case QUOTATION_TYPE:
+      out << "[";
+      quot = untag<quotation>(obj);
+      print_array(out, untag<array>(quot->array), nesting - 1);
+      out << " ]";
+      break;
+    case ALIEN_TYPE:
+      print_alien(out, untag<alien>(obj), nesting - 1);
+      break;
+    default:
+      out << "#<" << type_name(TAG(obj)) << " @ ";
+      out << (void*)obj << ">";
+      break;
+  }
+  out << flush;
 }
 
-void factor_vm::print_retainstack()
-{
-       std::cout << "==== RETAIN STACK:\n";
-       print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack);
+void factor_vm::print_obj(ostream& out, cell obj) {
+  print_nested_obj(out, obj, 10);
 }
 
-struct stack_frame_printer {
-       factor_vm *parent;
-
-       explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
-       void operator()(stack_frame *frame)
-       {
-               std::cout << "frame: " << std::hex << (cell)frame << std::dec << std::endl;
-               std::cout << "executing: ";
-               parent->print_obj(parent->frame_executing(frame));
-               std::cout << std::endl;
-               std::cout << "scan: ";
-               parent->print_obj(parent->frame_scan(frame));
-               std::cout << std::endl;
-               std::cout << "word/quot addr: ";
-               std::cout << std::hex << (cell)parent->frame_executing(frame) << std::dec;
-               std::cout << std::endl;
-               std::cout << "word/quot xt: ";
-               std::cout << std::hex << (cell)frame->entry_point << std::dec;
-               std::cout << std::endl;
-               std::cout << "return address: ";
-               std::cout << std::hex << (cell)FRAME_RETURN_ADDRESS(frame,parent) << std::dec;
-               std::cout << std::endl;
-       }
-};
+void factor_vm::print_objects(ostream& out, cell* start, cell* end) {
+  for (; start <= end; start++) {
+    print_obj(out, *start);
+    cout << endl;
+  }
+}
 
-void factor_vm::print_callstack()
-{
-       std::cout << "==== CALL STACK:\n";
-       stack_frame_printer printer(this);
-       iterate_callstack(ctx,printer);
+void factor_vm::print_datastack(ostream& out) {
+  out << "==== DATA STACK:" << endl;
+  if (ctx)
+    print_objects(out,
+                  (cell*)ctx->datastack_seg->start,
+                  (cell*)ctx->datastack);
+  else
+    out << "*** Context not initialized" << endl;
 }
 
-struct padded_address {
-       cell value;
+void factor_vm::print_retainstack(ostream& out) {
+  out << "==== RETAIN STACK:" << endl;
+  if (ctx)
+    print_objects(out,
+                  (cell*)ctx->retainstack_seg->start,
+                  (cell*)ctx->retainstack);
+  else
+    out << "*** Context not initialized" << endl;
+}
 
-       explicit padded_address(cell value_) : value(value_) {}
+struct stack_frame_printer {
+  factor_vm* parent;
+  ostream& out;
+
+  explicit stack_frame_printer(factor_vm* parent, ostream& out)
+      : parent(parent), out(out) {}
+  void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
+    out << endl;
+    out << "frame: " << (void*)frame_top << " size " << size << endl;
+    out << "executing: ";
+    parent->print_obj(out, owner->owner);
+    out << endl;
+    out << "scan: ";
+    parent->print_obj(out, owner->scan(parent, addr));
+    out << endl;
+    out << "word/quot addr: ";
+    out << hex << owner->owner << dec;
+    out << endl;
+    out << "word/quot xt: ";
+    out << hex << owner->entry_point() << dec;
+    out << endl;
+    out << "return address: ";
+    out << hex << addr << dec;
+    out << endl;
+  }
 };
 
-std::ostream &operator<<(std::ostream &out, const padded_address &value)
-{
-       char prev = out.fill('0');
-       out.width(sizeof(cell) * 2);
-       out << std::hex << value.value << std::dec;
-       out.fill(prev);
-       return out;
+void factor_vm::print_callstack(ostream& out) {
+  out << "==== CALL STACK:" << endl;
+  if (ctx) {
+    stack_frame_printer printer(this, out);
+    iterate_callstack(ctx, printer);
+  } else
+    out << "*** Context not initialized" << endl;
 }
 
-void factor_vm::dump_cell(cell x)
-{
-       std::cout << padded_address(x) << ": ";
-       x = *(cell *)x;
-       std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
+void factor_vm::print_callstack_object(ostream& out, callstack* obj) {
+  stack_frame_printer printer(this, out);
+  iterate_callstack_object(obj, printer);
 }
 
-void factor_vm::dump_memory(cell from, cell to)
-{
-       from = UNTAG(from);
+struct padded_address {
+  cell value;
+
+  explicit padded_address(cell value) : value(value) {}
+};
 
-       for(; from <= to; from += sizeof(cell))
-               dump_cell(from);
+ostream& operator<<(ostream& out, const padded_address& value) {
+  char prev = out.fill('0');
+  out.width(sizeof(cell) * 2);
+  out << hex << value.value << dec;
+  out.fill(prev);
+  return out;
 }
 
-template<typename Generation>
-void factor_vm::dump_generation(const char *name, Generation *gen)
-{
-       std::cout << name << ": ";
-       std::cout << "Start=" << gen->start;
-       std::cout << ", size=" << gen->size;
-       std::cout << ", end=" << gen->end;
-       std::cout << std::endl;
+void factor_vm::dump_cell(ostream& out, cell x) {
+  out << padded_address(x) << ": ";
+  x = *(cell*)x;
+  out << padded_address(x) << " tag " << TAG(x) << endl;
 }
 
-void factor_vm::dump_generations()
-{
-       std::cout << std::hex;
+void factor_vm::dump_memory(ostream& out, cell from, cell to) {
+  from = UNTAG(from);
 
-       dump_generation("Nursery",&nursery);
-       dump_generation("Aging",data->aging);
-       dump_generation("Tenured",data->tenured);
+  for (; from <= to; from += sizeof(cell))
+    dump_cell(out, from);
+}
 
-       std::cout << "Cards:";
-       std::cout << "base=" << (cell)data->cards << ", ";
-       std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
+void dump_memory_range(ostream& out, const char* name, cell name_w,
+                       cell start, cell end) {
+  out << setw(static_cast<int>(name_w)) << left << name << ": ";
 
-       std::cout << std::dec;
+  out << "[" << (void*)start << " -> " << (void*)end << "] ";
+  out << setw(10) << right << (end - start) << " bytes" << endl;
 }
 
-struct object_dumper {
-       factor_vm *parent;
-       cell type;
-
-       explicit object_dumper(factor_vm *parent_, cell type_) :
-               parent(parent_), type(type_) {}
-
-       void operator()(object *obj)
-       {
-               if(type == TYPE_COUNT || obj->type() == type)
-               {
-                       std::cout << padded_address((cell)obj) << " ";
-                       parent->print_nested_obj(tag_dynamic(obj),2);
-                       std::cout << std::endl;
-               }
-       }
-};
+template <typename Generation>
+void dump_generation(ostream& out, const char* name, Generation* gen) {
+  dump_memory_range(out, name, 10, gen->start, gen->end);
+}
 
-void factor_vm::dump_objects(cell type)
-{
-       primitive_full_gc();
-       object_dumper dumper(this,type);
-       each_object(dumper);
+void factor_vm::dump_memory_layout(ostream& out) {
+  dump_generation(out, "Nursery", data->nursery);
+  dump_generation(out, "Aging", data->aging);
+  dump_generation(out, "Tenured", data->tenured);
+  dump_memory_range(out, "Cards", 10, (cell)data->cards, (cell)data->cards_end);
+
+  out << endl << "Contexts:" << endl << endl;
+  FACTOR_FOR_EACH(active_contexts) {
+    context* the_ctx = *iter;
+    segment* ds = the_ctx->datastack_seg;
+    segment* rs = the_ctx->retainstack_seg;
+    segment* cs = the_ctx->callstack_seg;
+    if (the_ctx == ctx) {
+      out << "  Active:" << endl;
+    }
+    dump_memory_range(out, "  Datastack", 14, ds->start, ds->end);
+    dump_memory_range(out, "  Retainstack", 14, rs->start, rs->end);
+    dump_memory_range(out, "  Callstack", 14, cs->start, cs->end);
+    out << endl;
+  }
 }
 
-struct data_reference_slot_visitor {
-       cell look_for;
-       object *obj;
-       factor_vm *parent;
-
-       explicit data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
-               look_for(look_for_), obj(obj_), parent(parent_) { }
-
-       void operator()(cell *scan)
-       {
-               if(look_for == *scan)
-               {
-                       std::cout << padded_address((cell)obj) << " ";
-                       parent->print_nested_obj(tag_dynamic(obj),2);
-                       std::cout << std::endl;
-               }
-       }
-};
+void factor_vm::dump_objects(ostream& out, cell type) {
+  primitive_full_gc();
+  auto object_dumper = [&](object* obj) {
+     if (type == TYPE_COUNT || obj->type() == type) {
+      out << padded_address((cell)obj) << " ";
+      print_nested_obj(out, tag_dynamic(obj), 2);
+      out << endl;
+    }
+  };
+  each_object(object_dumper);
+}
 
-struct data_reference_object_visitor {
-       cell look_for;
-       factor_vm *parent;
+void factor_vm::find_data_references(ostream& out, cell look_for) {
+  primitive_full_gc();
+  auto find_data_ref_func = [&](object* obj, cell* slot) {
+    if (look_for == *slot) {
+      out << padded_address((cell)obj) << " ";
+      print_nested_obj(out, tag_dynamic(obj), 2);
+      out << endl;
+    }
+  };
+  each_object_each_slot(find_data_ref_func);
+}
 
-       explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
-               look_for(look_for_), parent(parent_) {}
+void factor_vm::dump_edges(ostream& out) {
+  primitive_full_gc();
+  auto dump_edges_func = [&](object* obj, cell* scan) {
+    if (TAG(*scan) > F_TYPE) {
+      out << (void*)tag_dynamic(obj);
+      out << " ==> ";
+      out << (void*)*scan << endl;
+    }
+  };
+  each_object_each_slot(dump_edges_func);
+}
 
-       void operator()(object *obj)
-       {
-               data_reference_slot_visitor visitor(look_for,obj,parent);
-               obj->each_slot(visitor);
-       }
+struct code_block_printer {
+  factor_vm* parent;
+  ostream& out;
+  cell reloc_size, parameter_size;
+
+  explicit code_block_printer(factor_vm* parent, ostream& out)
+      : parent(parent), out(out), reloc_size(0), parameter_size(0) {}
+
+  void operator()(code_block* scan, cell size) {
+    const char* status;
+    if (scan->free_p())
+      status = "free";
+    else {
+      reloc_size += object_size(scan->relocation);
+      parameter_size += object_size(scan->parameters);
+
+      if (parent->code->allocator->state.marked_p((cell)scan))
+        status = "marked";
+      else
+        status = "allocated";
+
+      out << hex << (cell)scan << dec << " ";
+      out << hex << size << dec << " ";
+      out << status << " ";
+      out << "stack frame " << scan->stack_frame_size();
+      out << endl;
+    }
+  }
 };
 
-void factor_vm::find_data_references(cell look_for)
-{
-       data_reference_object_visitor visitor(look_for,this);
-       each_object(visitor);
+// Dump all code blocks for debugging
+void factor_vm::dump_code_heap(ostream& out) {
+  code_block_printer printer(this, out);
+  code->allocator->iterate(printer, no_fixup());
+  out << printer.reloc_size << " bytes used by relocation tables" << endl;
+  out << printer.parameter_size << " bytes used by parameter tables" << endl;
 }
 
-struct code_block_printer {
-       factor_vm *parent;
-       cell reloc_size, parameter_size;
-
-       explicit code_block_printer(factor_vm *parent_) :
-               parent(parent_), reloc_size(0), parameter_size(0) {}
-
-       void operator()(code_block *scan, cell size)
-       {
-               const char *status;
-               if(scan->free_p())
-                       status = "free";
-               else
-               {
-                       reloc_size += parent->object_size(scan->relocation);
-                       parameter_size += parent->object_size(scan->parameters);
-
-                       if(parent->code->marked_p(scan))
-                               status = "marked";
-                       else
-                               status = "allocated";
-
-                       std::cout << std::hex << (cell)scan << std::dec << " ";
-                       std::cout << std::hex << size << std::dec << " ";
-                       std::cout << status << std::endl;
-               }
-       }
-};
+void factor_vm::factorbug_usage(bool advanced_p) {
+  cout << "Basic commands:" << endl;
+#ifdef WINDOWS
+  cout << "  q ^Z             -- quit Factor" << endl;
+#else
+  cout << "  q ^D             -- quit Factor" << endl;
+#endif
+  cout << "  c                -- continue executing Factor - NOT SAFE"
+       << endl;
+  cout << "  t                -- throw exception in Factor - NOT SAFE"
+       << endl;
+  cout << "  .s .r .c         -- print data, retain, call stacks"
+       << endl;
+  if (advanced_p) {
+    cout << "  help             -- reprint this message" << endl;
+    cout << "Advanced commands:" << endl;
+    cout << "  e                -- dump environment" << endl;
+    cout << "  d <addr> <count> -- dump memory" << endl;
+    cout << "  u <addr>         -- dump object at tagged <addr>"
+         << endl;
+    cout << "  . <addr>         -- print object at tagged <addr>"
+         << endl;
+    cout << "  g                -- dump memory layout" << endl;
+    cout << "  ds dr            -- dump data, retain stacks" << endl;
+    cout << "  trim             -- toggle output trimming" << endl;
+    cout << "  data             -- data heap dump" << endl;
+    cout << "  words            -- words dump" << endl;
+    cout << "  tuples           -- tuples dump" << endl;
+    cout << "  edges            -- print all object-to-object references"
+         << endl;
+    cout << "  refs <addr>      -- find data heap references to object"
+         << endl;
+    cout << "  push <addr>      -- push object on data stack - NOT SAFE"
+         << endl;
+    cout << "  gc               -- trigger full GC - NOT SAFE"
+         << endl;
+    cout << "  compact-gc       -- trigger compacting GC - NOT SAFE"
+         << endl;
+    cout << "  code             -- code heap dump" << endl;
+    cout << "  abort            -- call abort()" << endl;
+    cout << "  breakpoint       -- trigger system breakpoint" << endl;
+  } else {
+    cout << "  help             -- full help, including advanced commands"
+         << endl;
+  }
+  cout << endl;
+}
 
-/* Dump all code blocks for debugging */
-void factor_vm::dump_code_heap()
-{
-       code_block_printer printer(this);
-       code->allocator->iterate(printer);
-       std::cout << printer.reloc_size << " bytes used by relocation tables\n";
-       std::cout << printer.parameter_size << " bytes used by parameter tables\n";
+static void exit_fep(factor_vm* vm) {
+  unlock_console();
+  handle_ctrl_c();
+  vm->fep_p = false;
 }
 
-void factor_vm::factorbug()
-{
-       if(fep_disabled)
-       {
-               std::cout << "Low level debugger disabled\n";
-               exit(1);
-       }
-
-       fep_p = true;
-
-       std::cout << "Starting low level debugger...\n";
-       std::cout << "Basic commands:\n";
-       std::cout << "  q ^D             -- quit Factor\n";
-       std::cout << "  c                -- continue executing Factor - NOT SAFE\n";
-       std::cout << "  t                -- throw exception in Factor - NOT SAFE\n";
-       std::cout << "  .s .r .c         -- print data, retain, call stacks\n";
-       std::cout << "Advanced commands:\n";
-       std::cout << "  e                -- dump environment\n";
-       std::cout << "  d <addr> <count> -- dump memory\n";
-       std::cout << "  u <addr>         -- dump object at tagged <addr>\n";
-       std::cout << "  . <addr>         -- print object at tagged <addr>\n";
-       std::cout << "  g                -- dump generations\n";
-       std::cout << "  ds dr            -- dump data, retain stacks\n";
-       std::cout << "  trim             -- toggle output trimming\n";
-       std::cout << "  data             -- data heap dump\n";
-       std::cout << "  words            -- words dump\n";
-       std::cout << "  tuples           -- tuples dump\n";
-       std::cout << "  refs <addr>      -- find data heap references to object\n";
-       std::cout << "  push <addr>      -- push object on data stack - NOT SAFE\n";
-       std::cout << "  gc               -- trigger full GC - NOT SAFE\n";
-       std::cout << "  code             -- code heap dump\n";
-
-       bool seen_command = false;
-
-       for(;;)
-       {
-               char cmd[1024];
-
-               std::cout << "READY\n";
-               std::cout.flush();
-
-               std::cin >> std::setw(1024) >> cmd >> std::setw(0); 
-               if(!std::cin.good())
-               {
-                       if(!seen_command)
-                       {
-                               /* If we exit with an EOF immediately, then
-                               dump stacks. This is useful for builder and
-                               other cases where Factor is run with stdin
-                               redirected to /dev/null */
-                               fep_disabled = true;
-
-                               print_datastack();
-                               print_retainstack();
-                               print_callstack();
-                       }
-
-                       exit(1);
-               }
-
-               seen_command = true;
-
-               if(strcmp(cmd,"q") == 0)
-                       exit(1);
-               if(strcmp(cmd,"d") == 0)
-               {
-                       cell addr = read_cell_hex();
-                       if (std::cin.peek() == ' ')
-                               std::cin.ignore();
-
-                       if(!std::cin.good()) break;
-                       cell count = read_cell_hex();
-                       dump_memory(addr,addr+count);
-               }
-               else if(strcmp(cmd,"u") == 0)
-               {
-                       cell addr = read_cell_hex();
-                       cell count = object_size(addr);
-                       dump_memory(addr,addr+count);
-               }
-               else if(strcmp(cmd,".") == 0)
-               {
-                       cell addr = read_cell_hex();
-                       print_obj(addr);
-                       std::cout << std::endl;
-               }
-               else if(strcmp(cmd,"trim") == 0)
-                       full_output = !full_output;
-               else if(strcmp(cmd,"ds") == 0)
-                       dump_memory(ctx->datastack_seg->start,ctx->datastack);
-               else if(strcmp(cmd,"dr") == 0)
-                       dump_memory(ctx->retainstack_seg->start,ctx->retainstack);
-               else if(strcmp(cmd,".s") == 0)
-                       print_datastack();
-               else if(strcmp(cmd,".r") == 0)
-                       print_retainstack();
-               else if(strcmp(cmd,".c") == 0)
-                       print_callstack();
-               else if(strcmp(cmd,"e") == 0)
-               {
-                       for(cell i = 0; i < special_object_count; i++)
-                               dump_cell((cell)&special_objects[i]);
-               }
-               else if(strcmp(cmd,"g") == 0)
-                       dump_generations();
-               else if(strcmp(cmd,"c") == 0)
-               {
-                       fep_p = false;
-                       return;
-               }
-               else if(strcmp(cmd,"t") == 0)
-               {
-                       fep_p = false;
-                       general_error(ERROR_INTERRUPT,false_object,false_object);
-                       assert(false);
-               }
-               else if(strcmp(cmd,"data") == 0)
-                       dump_objects(TYPE_COUNT);
-               else if(strcmp(cmd,"refs") == 0)
-               {
-                       cell addr = read_cell_hex();
-                       std::cout << "Data heap references:\n";
-                       find_data_references(addr);
-                       std::cout << std::endl;
-               }
-               else if(strcmp(cmd,"words") == 0)
-                       dump_objects(WORD_TYPE);
-               else if(strcmp(cmd,"tuples") == 0)
-                       dump_objects(TUPLE_TYPE);
-               else if(strcmp(cmd,"push") == 0)
-               {
-                       cell addr = read_cell_hex();
-                       ctx->push(addr);
-               }
-               else if(strcmp(cmd,"code") == 0)
-                       dump_code_heap();
-               else if(strcmp(cmd,"gc") == 0)
-                       primitive_full_gc();
-               else
-                       std::cout << "unknown command\n";
-       }
+void factor_vm::factorbug() {
+  if (fep_disabled) {
+    cout << "Low level debugger disabled" << endl;
+    exit(1);
+  }
+
+  if (sampling_profiler_p)
+    end_sampling_profiler();
+
+  fep_p = true;
+
+  cout << "Starting low level debugger..." << endl;
+
+  // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
+  // that pumps the console is still running concurrently. We lock a mutex so
+  // the thread will take a break and give us exclusive access to stdin.
+  lock_console();
+  ignore_ctrl_c();
+
+  if (!fep_help_was_shown) {
+    factorbug_usage(false);
+    fep_help_was_shown = true;
+  }
+  bool seen_command = false;
+
+  for (;;) {
+    std::string cmd;
+
+    cout << "> " << flush;
+
+    cin >> setw(1024) >> cmd >> setw(0);
+    if (!cin.good()) {
+      if (!seen_command) {
+        // If we exit with an EOF immediately, then
+        // dump stacks. This is useful for builder and
+        // other cases where Factor is run with stdin
+        // redirected to /dev/null
+        fep_disabled = true;
+
+        print_datastack(cout);
+        print_retainstack(cout);
+        print_callstack(cout);
+      }
+
+      exit(1);
+    }
+
+    seen_command = true;
+
+    if (cmd == "q")
+      exit(1);
+    if (cmd == "d") {
+      cell addr = read_cell_hex();
+      if (cin.peek() == ' ')
+        cin.ignore();
+
+      if (!cin.good())
+        break;
+      cell count = read_cell_hex();
+      dump_memory(cout, addr, addr + count);
+    } else if (cmd == "u") {
+      cell addr = read_cell_hex();
+      cell count = object_size(addr);
+      dump_memory(cout, addr, addr + count);
+    } else if (cmd == ".") {
+      cell addr = read_cell_hex();
+      print_obj(cout, addr);
+      cout << endl;
+    } else if (cmd == "trim")
+      full_output = !full_output;
+    else if (cmd == "ds")
+      dump_memory(cout, ctx->datastack_seg->start, ctx->datastack);
+    else if (cmd == "dr")
+      dump_memory(cout, ctx->retainstack_seg->start, ctx->retainstack);
+    else if (cmd == ".s")
+      print_datastack(cout);
+    else if (cmd == ".r")
+      print_retainstack(cout);
+    else if (cmd == ".c")
+      print_callstack(cout);
+    else if (cmd == "e") {
+      for (cell i = 0; i < special_object_count; i++)
+        dump_cell(cout, (cell)&special_objects[i]);
+    } else if (cmd == "g")
+      dump_memory_layout(cout);
+    else if (cmd == "c") {
+      exit_fep(this);
+      return;
+    } else if (cmd == "t") {
+      exit_fep(this);
+      general_error(ERROR_INTERRUPT, false_object, false_object);
+      FACTOR_ASSERT(false);
+    } else if (cmd == "data")
+      dump_objects(cout, TYPE_COUNT);
+    else if (cmd == "edges")
+      dump_edges(cout);
+    else if (cmd == "refs") {
+      cell addr = read_cell_hex();
+      cout << "Data heap references:" << endl;
+      find_data_references(cout, addr);
+      cout << endl;
+    } else if (cmd == "words")
+      dump_objects(cout, WORD_TYPE);
+    else if (cmd == "tuples")
+      dump_objects(cout, TUPLE_TYPE);
+    else if (cmd == "push") {
+      cell addr = read_cell_hex();
+      ctx->push(addr);
+    } else if (cmd == "code")
+      dump_code_heap(cout);
+    else if (cmd == "gc")
+      primitive_full_gc();
+    else if (cmd == "compact-gc")
+      primitive_compact_gc();
+    else if (cmd == "help")
+      factorbug_usage(true);
+    else if (cmd == "abort")
+      abort();
+    else if (cmd == "breakpoint")
+      breakpoint();
+    else
+      cout << "unknown command" << endl;
+  }
 }
 
-void factor_vm::primitive_die()
-{
-       std::cout << "The die word was called by the library. Unless you called it yourself,\n";
-       std::cout << "you have triggered a bug in Factor. Please report.\n";
-       factorbug();
+void factor_vm::primitive_die() {
+  critical_error("The die word was called by the library.", 0);
 }
 
 }