6 std::ostream &operator<<(std::ostream &out, const string *str)
8 for(cell i = 0; i < string_capacity(str); i++)
9 out << (char)str->data()[i];
13 void factor_vm::print_word(word *word, cell nesting)
15 if(tagged<object>(word->vocabulary).type_p(STRING_TYPE))
16 std::cout << untag<string>(word->vocabulary) << ":";
18 if(tagged<object>(word->name).type_p(STRING_TYPE))
19 std::cout << untag<string>(word->name);
22 std::cout << "#<not a string: ";
23 print_nested_obj(word->name,nesting);
28 void factor_vm::print_factor_string(string *str)
30 std::cout << '"' << str << '"';
33 void factor_vm::print_array(array *array, cell nesting)
35 cell length = array_capacity(array);
39 if(length > 10 && !full_output)
47 for(i = 0; i < length; i++)
50 print_nested_obj(array_nth(array,i),nesting);
57 void factor_vm::print_alien(alien *alien, cell nesting)
59 if (to_boolean(alien->expired))
60 std::cout << "#<expired alien>";
61 else if (to_boolean(alien->base))
63 std::cout << "#<displaced alien " << alien->displacement << "+";
64 print_nested_obj(alien->base, nesting);
69 std::cout << "#<alien " << (void*)alien->address << ">";
73 void factor_vm::print_byte_array(byte_array *array, cell nesting)
75 cell length = array->capacity;
78 unsigned char *data = array->data<unsigned char>();
80 if(length > 16 && !full_output)
88 for(i = 0; i < length; i++)
90 std::cout << " " << (unsigned)data[i];
97 void factor_vm::print_tuple(tuple *tuple, cell nesting)
99 tuple_layout *layout = untag<tuple_layout>(tuple->layout);
100 cell length = to_fixnum(layout->size);
103 print_nested_obj(layout->klass,nesting);
106 if(length > 10 && !full_output)
114 for(cell i = 0; i < length; i++)
117 print_nested_obj(tuple->data()[i],nesting);
124 void factor_vm::print_nested_obj(cell obj, fixnum nesting)
126 if(nesting <= 0 && !full_output)
128 std::cout << " ... ";
134 switch(tagged<object>(obj).type())
137 std::cout << untag_fixnum(obj);
140 std::cout << untag_float(obj);
143 print_word(untag<word>(obj),nesting - 1);
146 print_factor_string(untag<string>(obj));
153 print_tuple(untag<tuple>(obj),nesting - 1);
158 print_nested_obj(untag<wrapper>(obj)->object,nesting - 1);
161 case BYTE_ARRAY_TYPE:
163 print_byte_array(untag<byte_array>(obj),nesting - 1);
168 print_array(untag<array>(obj),nesting - 1);
173 quot = untag<quotation>(obj);
174 print_array(untag<array>(quot->array),nesting - 1);
178 print_alien(untag<alien>(obj), nesting - 1);
181 std::cout << "#<" << type_name(tagged<object>(obj).type()) << " @ ";
182 std::cout << (void*)obj << ">";
187 void factor_vm::print_obj(cell obj)
189 print_nested_obj(obj,10);
192 void factor_vm::print_objects(cell *start, cell *end)
194 for(; start <= end; start++)
197 std::cout << std::endl;
201 void factor_vm::print_datastack()
203 std::cout << "==== DATA STACK:" << std::endl;
205 print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack);
207 std::cout << "*** Context not initialized" << std::endl;
210 void factor_vm::print_retainstack()
212 std::cout << "==== RETAIN STACK:" << std::endl;
214 print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack);
216 std::cout << "*** Context not initialized" << std::endl;
219 struct stack_frame_printer {
222 explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
223 void operator()(void *frame_top, code_block *owner, void *addr)
225 std::cout << std::endl;
226 std::cout << "frame: " << frame_top << std::endl;
227 std::cout << "executing: ";
228 parent->print_obj(owner->owner);
229 std::cout << std::endl;
230 std::cout << "scan: ";
231 parent->print_obj(owner->scan(parent, addr));
232 std::cout << std::endl;
233 std::cout << "word/quot addr: ";
234 std::cout << std::hex << (cell)owner->owner << std::dec;
235 std::cout << std::endl;
236 std::cout << "word/quot xt: ";
237 std::cout << std::hex << (cell)owner->entry_point() << std::dec;
238 std::cout << std::endl;
239 std::cout << "return address: ";
240 std::cout << std::hex << (cell)addr << std::dec;
241 std::cout << std::endl;
245 void factor_vm::print_callstack()
247 std::cout << "==== CALL STACK:" << std::endl;
250 stack_frame_printer printer(this);
251 iterate_callstack_reversed(ctx,printer);
254 std::cout << "*** Context not initialized" << std::endl;
257 struct padded_address {
260 explicit padded_address(cell value_) : value(value_) {}
263 std::ostream &operator<<(std::ostream &out, const padded_address &value)
265 char prev = out.fill('0');
266 out.width(sizeof(cell) * 2);
267 out << std::hex << value.value << std::dec;
272 void factor_vm::dump_cell(cell x)
274 std::cout << padded_address(x) << ": ";
276 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
279 void factor_vm::dump_memory(cell from, cell to)
283 for(; from <= to; from += sizeof(cell))
287 template<typename Generation>
288 void factor_vm::dump_generation(const char *name, Generation *gen)
290 std::cout << name << ": ";
291 std::cout << "Start=" << gen->start;
292 std::cout << ", size=" << gen->size;
293 std::cout << ", end=" << gen->end;
294 std::cout << std::endl;
297 void factor_vm::dump_generations()
299 std::cout << std::hex;
301 dump_generation("Nursery",&nursery);
302 dump_generation("Aging",data->aging);
303 dump_generation("Tenured",data->tenured);
305 std::cout << "Cards:";
306 std::cout << "base=" << (cell)data->cards << ", ";
307 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
309 std::cout << std::dec;
312 struct object_dumper {
316 explicit object_dumper(factor_vm *parent_, cell type_) :
317 parent(parent_), type(type_) {}
319 void operator()(object *obj)
321 if(type == TYPE_COUNT || obj->type() == type)
323 std::cout << padded_address((cell)obj) << " ";
324 parent->print_nested_obj(tag_dynamic(obj),2);
325 std::cout << std::endl;
330 void factor_vm::dump_objects(cell type)
333 object_dumper dumper(this,type);
337 struct find_data_reference_slot_visitor {
342 explicit find_data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
343 look_for(look_for_), obj(obj_), parent(parent_) { }
345 void operator()(cell *scan)
347 if(look_for == *scan)
349 std::cout << padded_address((cell)obj) << " ";
350 parent->print_nested_obj(tag_dynamic(obj),2);
351 std::cout << std::endl;
356 struct dump_edges_slot_visitor {
360 explicit dump_edges_slot_visitor(cell, object *obj_, factor_vm *parent_) :
361 obj(obj_), parent(parent_) { }
363 void operator()(cell *scan)
365 if (TAG(*scan) > F_TYPE)
366 std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan << std::endl;
370 template <typename SlotVisitor>
371 struct data_reference_object_visitor {
375 explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
376 look_for(look_for_), parent(parent_) {}
378 void operator()(object *obj)
380 SlotVisitor visitor(look_for,obj,parent);
381 obj->each_slot(visitor);
385 void factor_vm::find_data_references(cell look_for)
387 data_reference_object_visitor<find_data_reference_slot_visitor> visitor(look_for,this);
388 each_object(visitor);
391 void factor_vm::dump_edges()
393 data_reference_object_visitor<dump_edges_slot_visitor> visitor(0,this);
394 each_object(visitor);
397 struct code_block_printer {
399 cell reloc_size, parameter_size;
401 explicit code_block_printer(factor_vm *parent_) :
402 parent(parent_), reloc_size(0), parameter_size(0) {}
404 void operator()(code_block *scan, cell size)
411 reloc_size += parent->object_size(scan->relocation);
412 parameter_size += parent->object_size(scan->parameters);
414 if(parent->code->marked_p(scan))
417 status = "allocated";
419 std::cout << std::hex << (cell)scan << std::dec << " ";
420 std::cout << std::hex << size << std::dec << " ";
421 std::cout << status << " ";
422 std::cout << "stack frame " << scan->stack_frame_size();
423 std::cout << std::endl;
428 /* Dump all code blocks for debugging */
429 void factor_vm::dump_code_heap()
431 code_block_printer printer(this);
432 code->allocator->iterate(printer);
433 std::cout << printer.reloc_size << " bytes used by relocation tables" << std::endl;
434 std::cout << printer.parameter_size << " bytes used by parameter tables" << std::endl;
437 void factor_vm::factorbug_usage(bool advanced_p)
439 std::cout << "Basic commands:" << std::endl;
441 std::cout << " q ^Z -- quit Factor" << std::endl;
443 std::cout << " q ^D -- quit Factor" << std::endl;
445 std::cout << " c -- continue executing Factor - NOT SAFE" << std::endl;
446 std::cout << " t -- throw exception in Factor - NOT SAFE" << std::endl;
447 std::cout << " .s .r .c -- print data, retain, call stacks" << std::endl;
450 std::cout << " help -- reprint this message" << std::endl;
451 std::cout << "Advanced commands:" << std::endl;
452 std::cout << " e -- dump environment" << std::endl;
453 std::cout << " d <addr> <count> -- dump memory" << std::endl;
454 std::cout << " u <addr> -- dump object at tagged <addr>" << std::endl;
455 std::cout << " . <addr> -- print object at tagged <addr>" << std::endl;
456 std::cout << " g -- dump generations" << std::endl;
457 std::cout << " ds dr -- dump data, retain stacks" << std::endl;
458 std::cout << " trim -- toggle output trimming" << std::endl;
459 std::cout << " data -- data heap dump" << std::endl;
460 std::cout << " words -- words dump" << std::endl;
461 std::cout << " tuples -- tuples dump" << std::endl;
462 std::cout << " edges -- print all object-to-object references" << std::endl;
463 std::cout << " refs <addr> -- find data heap references to object" << std::endl;
464 std::cout << " push <addr> -- push object on data stack - NOT SAFE" << std::endl;
465 std::cout << " gc -- trigger full GC - NOT SAFE" << std::endl;
466 std::cout << " compact-gc -- trigger compacting GC - NOT SAFE" << std::endl;
467 std::cout << " code -- code heap dump" << std::endl;
468 std::cout << " abort -- call abort()" << std::endl;
469 std::cout << " breakpoint -- trigger system breakpoint" << std::endl;
473 std::cout << " help -- full help, including advanced commands" << std::endl;
476 std::cout << std::endl;
480 static void exit_fep(factor_vm *vm)
482 vm->unlock_console();
487 void factor_vm::factorbug()
491 std::cout << "Low level debugger disabled" << std::endl;
495 if (sampling_profiler_p)
496 end_sampling_profiler();
500 std::cout << "Starting low level debugger..." << std::endl;
502 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
503 // that pumps the console is still running concurrently. We lock a mutex so
504 // the thread will take a break and give us exclusive access to stdin.
508 if (!fep_help_was_shown) {
509 factorbug_usage(false);
510 fep_help_was_shown = true;
512 bool seen_command = false;
518 std::cout << "> " << std::flush;
520 std::cin >> std::setw(1024) >> cmd >> std::setw(0);
525 /* If we exit with an EOF immediately, then
526 dump stacks. This is useful for builder and
527 other cases where Factor is run with stdin
528 redirected to /dev/null */
541 if(strcmp(cmd,"q") == 0)
543 if(strcmp(cmd,"d") == 0)
545 cell addr = read_cell_hex();
546 if (std::cin.peek() == ' ')
549 if(!std::cin.good()) break;
550 cell count = read_cell_hex();
551 dump_memory(addr,addr+count);
553 else if(strcmp(cmd,"u") == 0)
555 cell addr = read_cell_hex();
556 cell count = object_size(addr);
557 dump_memory(addr,addr+count);
559 else if(strcmp(cmd,".") == 0)
561 cell addr = read_cell_hex();
563 std::cout << std::endl;
565 else if(strcmp(cmd,"trim") == 0)
566 full_output = !full_output;
567 else if(strcmp(cmd,"ds") == 0)
568 dump_memory(ctx->datastack_seg->start,ctx->datastack);
569 else if(strcmp(cmd,"dr") == 0)
570 dump_memory(ctx->retainstack_seg->start,ctx->retainstack);
571 else if(strcmp(cmd,".s") == 0)
573 else if(strcmp(cmd,".r") == 0)
575 else if(strcmp(cmd,".c") == 0)
577 else if(strcmp(cmd,"e") == 0)
579 for(cell i = 0; i < special_object_count; i++)
580 dump_cell((cell)&special_objects[i]);
582 else if(strcmp(cmd,"g") == 0)
584 else if(strcmp(cmd,"c") == 0)
589 else if(strcmp(cmd,"t") == 0)
592 general_error(ERROR_INTERRUPT,false_object,false_object);
593 FACTOR_ASSERT(false);
595 else if(strcmp(cmd,"data") == 0)
596 dump_objects(TYPE_COUNT);
597 else if(strcmp(cmd,"edges") == 0)
599 else if(strcmp(cmd,"refs") == 0)
601 cell addr = read_cell_hex();
602 std::cout << "Data heap references:" << std::endl;
603 find_data_references(addr);
604 std::cout << std::endl;
606 else if(strcmp(cmd,"words") == 0)
607 dump_objects(WORD_TYPE);
608 else if(strcmp(cmd,"tuples") == 0)
609 dump_objects(TUPLE_TYPE);
610 else if(strcmp(cmd,"push") == 0)
612 cell addr = read_cell_hex();
615 else if(strcmp(cmd,"code") == 0)
617 else if(strcmp(cmd,"compact-gc") == 0)
618 primitive_compact_gc();
619 else if(strcmp(cmd,"gc") == 0)
621 else if(strcmp(cmd,"compact-gc") == 0)
622 primitive_compact_gc();
623 else if(strcmp(cmd,"help") == 0)
624 factorbug_usage(true);
625 else if(strcmp(cmd,"abort") == 0)
627 else if(strcmp(cmd,"breakpoint") == 0)
630 std::cout << "unknown command" << std::endl;
634 void factor_vm::primitive_die()
636 std::cout << "The die word was called by the library. Unless you called it yourself," << std::endl;
637 std::cout << "you have triggered a bug in Factor. Please report." << std::endl;