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;
204 print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack);
207 void factor_vm::print_retainstack()
209 std::cout << "==== RETAIN STACK:" << std::endl;
210 print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack);
213 struct stack_frame_printer {
216 explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
217 void operator()(stack_frame *frame)
219 std::cout << "frame: " << std::hex << (cell)frame << std::dec << std::endl;
220 std::cout << "executing: ";
221 parent->print_obj(parent->frame_executing(frame));
222 std::cout << std::endl;
223 std::cout << "scan: ";
224 parent->print_obj(parent->frame_scan(frame));
225 std::cout << std::endl;
226 std::cout << "word/quot addr: ";
227 std::cout << std::hex << (cell)parent->frame_executing(frame) << std::dec;
228 std::cout << std::endl;
229 std::cout << "word/quot xt: ";
230 std::cout << std::hex << (cell)frame->entry_point << std::dec;
231 std::cout << std::endl;
232 std::cout << "return address: ";
233 std::cout << std::hex << (cell)FRAME_RETURN_ADDRESS(frame,parent) << std::dec;
234 std::cout << std::endl;
238 void factor_vm::print_callstack()
240 std::cout << "==== CALL STACK:" << std::endl;
241 stack_frame_printer printer(this);
242 iterate_callstack(ctx,printer);
245 struct padded_address {
248 explicit padded_address(cell value_) : value(value_) {}
251 std::ostream &operator<<(std::ostream &out, const padded_address &value)
253 char prev = out.fill('0');
254 out.width(sizeof(cell) * 2);
255 out << std::hex << value.value << std::dec;
260 void factor_vm::dump_cell(cell x)
262 std::cout << padded_address(x) << ": ";
264 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
267 void factor_vm::dump_memory(cell from, cell to)
271 for(; from <= to; from += sizeof(cell))
275 template<typename Generation>
276 void factor_vm::dump_generation(const char *name, Generation *gen)
278 std::cout << name << ": ";
279 std::cout << "Start=" << gen->start;
280 std::cout << ", size=" << gen->size;
281 std::cout << ", end=" << gen->end;
282 std::cout << std::endl;
285 void factor_vm::dump_generations()
287 std::cout << std::hex;
289 dump_generation("Nursery",&nursery);
290 dump_generation("Aging",data->aging);
291 dump_generation("Tenured",data->tenured);
293 std::cout << "Cards:";
294 std::cout << "base=" << (cell)data->cards << ", ";
295 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
297 std::cout << std::dec;
300 struct object_dumper {
304 explicit object_dumper(factor_vm *parent_, cell type_) :
305 parent(parent_), type(type_) {}
307 void operator()(object *obj)
309 if(type == TYPE_COUNT || obj->type() == type)
311 std::cout << padded_address((cell)obj) << " ";
312 parent->print_nested_obj(tag_dynamic(obj),2);
313 std::cout << std::endl;
318 void factor_vm::dump_objects(cell type)
321 object_dumper dumper(this,type);
325 struct find_data_reference_slot_visitor {
330 explicit find_data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
331 look_for(look_for_), obj(obj_), parent(parent_) { }
333 void operator()(cell *scan)
335 if(look_for == *scan)
337 std::cout << padded_address((cell)obj) << " ";
338 parent->print_nested_obj(tag_dynamic(obj),2);
339 std::cout << std::endl;
344 struct dump_edges_slot_visitor {
348 explicit dump_edges_slot_visitor(cell, object *obj_, factor_vm *parent_) :
349 obj(obj_), parent(parent_) { }
351 void operator()(cell *scan)
353 if (TAG(*scan) > F_TYPE)
354 std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan << std::endl;
358 template <typename SlotVisitor>
359 struct data_reference_object_visitor {
363 explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
364 look_for(look_for_), parent(parent_) {}
366 void operator()(object *obj)
368 SlotVisitor visitor(look_for,obj,parent);
369 obj->each_slot(visitor);
373 void factor_vm::find_data_references(cell look_for)
375 data_reference_object_visitor<find_data_reference_slot_visitor> visitor(look_for,this);
376 each_object(visitor);
379 void factor_vm::dump_edges()
381 data_reference_object_visitor<dump_edges_slot_visitor> visitor(0,this);
382 each_object(visitor);
385 struct code_block_printer {
387 cell reloc_size, parameter_size;
389 explicit code_block_printer(factor_vm *parent_) :
390 parent(parent_), reloc_size(0), parameter_size(0) {}
392 void operator()(code_block *scan, cell size)
399 reloc_size += parent->object_size(scan->relocation);
400 parameter_size += parent->object_size(scan->parameters);
402 if(parent->code->marked_p(scan))
405 status = "allocated";
407 std::cout << std::hex << (cell)scan << std::dec << " ";
408 std::cout << std::hex << size << std::dec << " ";
409 std::cout << status << std::endl;
414 /* Dump all code blocks for debugging */
415 void factor_vm::dump_code_heap()
417 code_block_printer printer(this);
418 code->allocator->iterate(printer);
419 std::cout << printer.reloc_size << " bytes used by relocation tables" << std::endl;
420 std::cout << printer.parameter_size << " bytes used by parameter tables" << std::endl;
423 void factor_vm::factorbug_usage(bool advanced_p)
425 std::cout << "Basic commands:" << std::endl;
427 std::cout << " q ^Z -- quit Factor" << std::endl;
429 std::cout << " q ^D -- quit Factor" << std::endl;
431 std::cout << " c -- continue executing Factor - NOT SAFE" << std::endl;
432 std::cout << " t -- throw exception in Factor - NOT SAFE" << std::endl;
433 std::cout << " .s .r .c -- print data, retain, call stacks" << std::endl;
436 std::cout << " help -- reprint this message" << std::endl;
437 std::cout << "Advanced commands:" << std::endl;
438 std::cout << " e -- dump environment" << std::endl;
439 std::cout << " d <addr> <count> -- dump memory" << std::endl;
440 std::cout << " u <addr> -- dump object at tagged <addr>" << std::endl;
441 std::cout << " . <addr> -- print object at tagged <addr>" << std::endl;
442 std::cout << " g -- dump generations" << std::endl;
443 std::cout << " ds dr -- dump data, retain stacks" << std::endl;
444 std::cout << " trim -- toggle output trimming" << std::endl;
445 std::cout << " data -- data heap dump" << std::endl;
446 std::cout << " words -- words dump" << std::endl;
447 std::cout << " tuples -- tuples dump" << std::endl;
448 std::cout << " edges -- print all object-to-object references" << std::endl;
449 std::cout << " refs <addr> -- find data heap references to object" << std::endl;
450 std::cout << " push <addr> -- push object on data stack - NOT SAFE" << std::endl;
451 std::cout << " gc -- trigger full GC - NOT SAFE" << std::endl;
452 std::cout << " code -- code heap dump" << std::endl;
456 std::cout << " help -- full help, including advanced commands" << std::endl;
459 std::cout << std::endl;
463 static void exit_fep(factor_vm *vm)
465 vm->unlock_console();
469 void factor_vm::factorbug()
473 std::cout << "Low level debugger disabled" << std::endl;
477 if (sampling_profiler_p)
478 end_sampling_profiler();
482 std::cout << "Starting low level debugger..." << std::endl;
484 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
485 // that pumps the console is still running concurrently. We lock a mutex so
486 // the thread will take a break and give us exclusive access to stdin.
489 if (!fep_help_was_shown) {
490 factorbug_usage(false);
491 fep_help_was_shown = true;
493 bool seen_command = false;
499 std::cout << "> " << std::flush;
501 std::cin >> std::setw(1024) >> cmd >> std::setw(0);
506 /* If we exit with an EOF immediately, then
507 dump stacks. This is useful for builder and
508 other cases where Factor is run with stdin
509 redirected to /dev/null */
522 if(strcmp(cmd,"q") == 0)
524 if(strcmp(cmd,"d") == 0)
526 cell addr = read_cell_hex();
527 if (std::cin.peek() == ' ')
530 if(!std::cin.good()) break;
531 cell count = read_cell_hex();
532 dump_memory(addr,addr+count);
534 else if(strcmp(cmd,"u") == 0)
536 cell addr = read_cell_hex();
537 cell count = object_size(addr);
538 dump_memory(addr,addr+count);
540 else if(strcmp(cmd,".") == 0)
542 cell addr = read_cell_hex();
544 std::cout << std::endl;
546 else if(strcmp(cmd,"trim") == 0)
547 full_output = !full_output;
548 else if(strcmp(cmd,"ds") == 0)
549 dump_memory(ctx->datastack_seg->start,ctx->datastack);
550 else if(strcmp(cmd,"dr") == 0)
551 dump_memory(ctx->retainstack_seg->start,ctx->retainstack);
552 else if(strcmp(cmd,".s") == 0)
554 else if(strcmp(cmd,".r") == 0)
556 else if(strcmp(cmd,".c") == 0)
558 else if(strcmp(cmd,"e") == 0)
560 for(cell i = 0; i < special_object_count; i++)
561 dump_cell((cell)&special_objects[i]);
563 else if(strcmp(cmd,"g") == 0)
565 else if(strcmp(cmd,"c") == 0)
570 else if(strcmp(cmd,"t") == 0)
573 general_error(ERROR_INTERRUPT,false_object,false_object);
576 else if(strcmp(cmd,"data") == 0)
577 dump_objects(TYPE_COUNT);
578 else if(strcmp(cmd,"edges") == 0)
580 else if(strcmp(cmd,"refs") == 0)
582 cell addr = read_cell_hex();
583 std::cout << "Data heap references:" << std::endl;
584 find_data_references(addr);
585 std::cout << std::endl;
587 else if(strcmp(cmd,"words") == 0)
588 dump_objects(WORD_TYPE);
589 else if(strcmp(cmd,"tuples") == 0)
590 dump_objects(TUPLE_TYPE);
591 else if(strcmp(cmd,"push") == 0)
593 cell addr = read_cell_hex();
596 else if(strcmp(cmd,"code") == 0)
598 else if(strcmp(cmd,"gc") == 0)
600 else if(strcmp(cmd,"help") == 0)
601 factorbug_usage(true);
603 std::cout << "unknown command" << std::endl;
607 void factor_vm::primitive_die()
609 std::cout << "The die word was called by the library. Unless you called it yourself," << std::endl;
610 std::cout << "you have triggered a bug in Factor. Please report." << std::endl;