5 std::ostream& operator<<(std::ostream& out, const string* str) {
6 for (cell i = 0; i < string_capacity(str); i++)
7 out << (char)str->data()[i];
11 void factor_vm::print_word(word* word, cell nesting) {
12 if (tagged<object>(word->vocabulary).type_p(STRING_TYPE))
13 std::cout << untag<string>(word->vocabulary) << ":";
15 if (tagged<object>(word->name).type_p(STRING_TYPE))
16 std::cout << untag<string>(word->name);
18 std::cout << "#<not a string: ";
19 print_nested_obj(word->name, nesting);
24 void factor_vm::print_factor_string(string* str) {
25 std::cout << '"' << str << '"';
28 void factor_vm::print_array(array* array, cell nesting) {
29 cell length = array_capacity(array);
33 if (length > 10 && !full_output) {
39 for (i = 0; i < length; i++) {
41 print_nested_obj(array_nth(array, i), nesting);
48 void factor_vm::print_alien(alien* alien, cell nesting) {
49 if (to_boolean(alien->expired))
50 std::cout << "#<expired alien>";
51 else if (to_boolean(alien->base)) {
52 std::cout << "#<displaced alien " << alien->displacement << "+";
53 print_nested_obj(alien->base, nesting);
56 std::cout << "#<alien " << (void*)alien->address << ">";
60 void factor_vm::print_byte_array(byte_array* array, cell nesting) {
61 cell length = array->capacity;
64 unsigned char* data = array->data<unsigned char>();
66 if (length > 16 && !full_output) {
72 for (i = 0; i < length; i++) {
73 std::cout << " " << (unsigned) data[i];
80 void factor_vm::print_tuple(tuple* tuple, cell nesting) {
81 tuple_layout* layout = untag<tuple_layout>(tuple->layout);
82 cell length = to_cell(layout->size);
85 print_nested_obj(layout->klass, nesting);
88 if (length > 10 && !full_output) {
94 for (cell i = 0; i < length; i++) {
96 print_nested_obj(tuple->data()[i], nesting);
103 void factor_vm::print_nested_obj(cell obj, fixnum nesting) {
104 if (nesting <= 0 && !full_output) {
105 std::cout << " ... ";
111 switch (tagged<object>(obj).type()) {
113 std::cout << untag_fixnum(obj);
116 std::cout << untag_float(obj);
119 print_word(untag<word>(obj), nesting - 1);
122 print_factor_string(untag<string>(obj));
129 print_tuple(untag<tuple>(obj), nesting - 1);
134 print_nested_obj(untag<wrapper>(obj)->object, nesting - 1);
137 case BYTE_ARRAY_TYPE:
139 print_byte_array(untag<byte_array>(obj), nesting - 1);
144 print_array(untag<array>(obj), nesting - 1);
149 quot = untag<quotation>(obj);
150 print_array(untag<array>(quot->array), nesting - 1);
154 print_alien(untag<alien>(obj), nesting - 1);
157 std::cout << "#<" << type_name(tagged<object>(obj).type()) << " @ ";
158 std::cout << (void*)obj << ">";
161 std::cout << std::flush;
164 void factor_vm::print_obj(cell obj) { print_nested_obj(obj, 10); }
166 void factor_vm::print_objects(cell* start, cell* end) {
167 for (; start <= end; start++) {
169 std::cout << std::endl;
173 void factor_vm::print_datastack() {
174 std::cout << "==== DATA STACK:" << std::endl;
176 print_objects((cell*)ctx->datastack_seg->start, (cell*)ctx->datastack);
178 std::cout << "*** Context not initialized" << std::endl;
181 void factor_vm::print_retainstack() {
182 std::cout << "==== RETAIN STACK:" << std::endl;
184 print_objects((cell*)ctx->retainstack_seg->start, (cell*)ctx->retainstack);
186 std::cout << "*** Context not initialized" << std::endl;
189 struct stack_frame_printer {
192 explicit stack_frame_printer(factor_vm* parent) : parent(parent) {}
193 void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
194 std::cout << std::endl;
195 std::cout << "frame: " << (void*)frame_top << " size " << size << std::endl;
196 std::cout << "executing: ";
197 parent->print_obj(owner->owner);
198 std::cout << std::endl;
199 std::cout << "scan: ";
200 parent->print_obj(owner->scan(parent, addr));
201 std::cout << std::endl;
202 std::cout << "word/quot addr: ";
203 std::cout << std::hex << owner->owner << std::dec;
204 std::cout << std::endl;
205 std::cout << "word/quot xt: ";
206 std::cout << std::hex << owner->entry_point() << std::dec;
207 std::cout << std::endl;
208 std::cout << "return address: ";
209 std::cout << std::hex << addr << std::dec;
210 std::cout << std::endl;
214 void factor_vm::print_callstack() {
215 std::cout << "==== CALL STACK:" << std::endl;
217 stack_frame_printer printer(this);
218 iterate_callstack(ctx, printer);
220 std::cout << "*** Context not initialized" << std::endl;
223 void factor_vm::print_callstack_object(callstack* obj) {
224 stack_frame_printer printer(this);
225 iterate_callstack_object(obj, printer);
228 struct padded_address {
231 explicit padded_address(cell value) : value(value) {}
234 std::ostream& operator<<(std::ostream& out, const padded_address& value) {
235 char prev = out.fill('0');
236 out.width(sizeof(cell) * 2);
237 out << std::hex << value.value << std::dec;
242 void factor_vm::dump_cell(cell x) {
243 std::cout << padded_address(x) << ": ";
245 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
248 void factor_vm::dump_memory(cell from, cell to) {
251 for (; from <= to; from += sizeof(cell))
255 template <typename Generation>
256 void factor_vm::dump_generation(const char* name, Generation* gen) {
257 std::cout << name << ": ";
258 std::cout << "Start=" << gen->start;
259 std::cout << ", size=" << gen->size;
260 std::cout << ", end=" << gen->end;
261 std::cout << std::endl;
264 void factor_vm::dump_generations() {
265 std::cout << std::hex;
267 dump_generation("Nursery", &nursery);
268 dump_generation("Aging", data->aging);
269 dump_generation("Tenured", data->tenured);
271 std::cout << "Cards:";
272 std::cout << "base=" << (cell)data->cards << ", ";
273 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
275 std::cout << std::dec;
278 struct object_dumper {
282 object_dumper(factor_vm* parent, cell type)
283 : parent(parent), type(type) {}
285 void operator()(object* obj) {
286 if (type == TYPE_COUNT || obj->type() == type) {
287 std::cout << padded_address((cell)obj) << " ";
288 parent->print_nested_obj(tag_dynamic(obj), 2);
289 std::cout << std::endl;
294 void factor_vm::dump_objects(cell type) {
296 object_dumper dumper(this, type);
300 struct find_data_reference_slot_visitor {
305 find_data_reference_slot_visitor(cell look_for, object* obj,
307 : look_for(look_for), obj(obj), parent(parent) {}
309 void operator()(cell* scan) {
310 if (look_for == *scan) {
311 std::cout << padded_address((cell)obj) << " ";
312 parent->print_nested_obj(tag_dynamic(obj), 2);
313 std::cout << std::endl;
318 struct dump_edges_slot_visitor {
322 dump_edges_slot_visitor(cell, object* obj, factor_vm* parent)
323 : obj(obj), parent(parent) {}
325 void operator()(cell* scan) {
326 if (TAG(*scan) > F_TYPE)
327 std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan
332 template <typename SlotVisitor> struct data_reference_object_visitor {
336 data_reference_object_visitor(cell look_for, factor_vm* parent)
337 : look_for(look_for), parent(parent) {}
339 void operator()(object* obj) {
340 SlotVisitor visitor(look_for, obj, parent);
341 obj->each_slot(visitor);
345 void factor_vm::find_data_references(cell look_for) {
346 data_reference_object_visitor<find_data_reference_slot_visitor> visitor(
348 each_object(visitor);
351 void factor_vm::dump_edges() {
352 data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this);
353 each_object(visitor);
356 struct code_block_printer {
358 cell reloc_size, parameter_size;
360 explicit code_block_printer(factor_vm* parent)
361 : parent(parent), reloc_size(0), parameter_size(0) {}
363 void operator()(code_block* scan, cell size) {
368 reloc_size += parent->object_size(scan->relocation);
369 parameter_size += parent->object_size(scan->parameters);
371 if (parent->code->allocator->state.marked_p((cell)scan))
374 status = "allocated";
376 std::cout << std::hex << (cell)scan << std::dec << " ";
377 std::cout << std::hex << size << std::dec << " ";
378 std::cout << status << " ";
379 std::cout << "stack frame " << scan->stack_frame_size();
380 std::cout << std::endl;
385 /* Dump all code blocks for debugging */
386 void factor_vm::dump_code_heap() {
387 code_block_printer printer(this);
388 code->allocator->iterate(printer);
389 std::cout << printer.reloc_size << " bytes used by relocation tables"
391 std::cout << printer.parameter_size << " bytes used by parameter tables"
395 void factor_vm::factorbug_usage(bool advanced_p) {
396 std::cout << "Basic commands:" << std::endl;
398 std::cout << " q ^Z -- quit Factor" << std::endl;
400 std::cout << " q ^D -- quit Factor" << std::endl;
402 std::cout << " c -- continue executing Factor - NOT SAFE"
404 std::cout << " t -- throw exception in Factor - NOT SAFE"
406 std::cout << " .s .r .c -- print data, retain, call stacks"
409 std::cout << " help -- reprint this message" << std::endl;
410 std::cout << "Advanced commands:" << std::endl;
411 std::cout << " e -- dump environment" << std::endl;
412 std::cout << " d <addr> <count> -- dump memory" << std::endl;
413 std::cout << " u <addr> -- dump object at tagged <addr>"
415 std::cout << " . <addr> -- print object at tagged <addr>"
417 std::cout << " g -- dump generations" << std::endl;
418 std::cout << " ds dr -- dump data, retain stacks" << std::endl;
419 std::cout << " trim -- toggle output trimming" << std::endl;
420 std::cout << " data -- data heap dump" << std::endl;
421 std::cout << " words -- words dump" << std::endl;
422 std::cout << " tuples -- tuples dump" << std::endl;
423 std::cout << " edges -- print all object-to-object references"
425 std::cout << " refs <addr> -- find data heap references to object"
427 std::cout << " push <addr> -- push object on data stack - NOT SAFE"
429 std::cout << " gc -- trigger full GC - NOT SAFE"
431 std::cout << " compact-gc -- trigger compacting GC - NOT SAFE"
433 std::cout << " code -- code heap dump" << std::endl;
434 std::cout << " abort -- call abort()" << std::endl;
435 std::cout << " breakpoint -- trigger system breakpoint" << std::endl;
437 std::cout << " help -- full help, including advanced commands"
441 std::cout << std::endl;
445 static void exit_fep(factor_vm* vm) {
446 vm->unlock_console();
451 void factor_vm::factorbug() {
453 std::cout << "Low level debugger disabled" << std::endl;
457 if (sampling_profiler_p)
458 end_sampling_profiler();
462 std::cout << "Starting low level debugger..." << std::endl;
464 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
465 // that pumps the console is still running concurrently. We lock a mutex so
466 // the thread will take a break and give us exclusive access to stdin.
470 if (!fep_help_was_shown) {
471 factorbug_usage(false);
472 fep_help_was_shown = true;
474 bool seen_command = false;
479 std::cout << "> " << std::flush;
481 std::cin >> std::setw(1024) >> cmd >> std::setw(0);
482 if (!std::cin.good()) {
484 /* If we exit with an EOF immediately, then
485 dump stacks. This is useful for builder and
486 other cases where Factor is run with stdin
487 redirected to /dev/null */
503 cell addr = read_cell_hex();
504 if (std::cin.peek() == ' ')
507 if (!std::cin.good())
509 cell count = read_cell_hex();
510 dump_memory(addr, addr + count);
511 } else if (cmd == "u") {
512 cell addr = read_cell_hex();
513 cell count = object_size(addr);
514 dump_memory(addr, addr + count);
515 } else if (cmd == ".") {
516 cell addr = read_cell_hex();
518 std::cout << std::endl;
519 } else if (cmd == "trim")
520 full_output = !full_output;
521 else if (cmd == "ds")
522 dump_memory(ctx->datastack_seg->start, ctx->datastack);
523 else if (cmd == "dr")
524 dump_memory(ctx->retainstack_seg->start, ctx->retainstack);
525 else if (cmd == ".s")
527 else if (cmd == ".r")
529 else if (cmd == ".c")
531 else if (cmd == "e") {
532 for (cell i = 0; i < special_object_count; i++)
533 dump_cell((cell)&special_objects[i]);
534 } else if (cmd == "g")
536 else if (cmd == "c") {
539 } else if (cmd == "t") {
541 general_error(ERROR_INTERRUPT, false_object, false_object);
542 FACTOR_ASSERT(false);
543 } else if (cmd == "data")
544 dump_objects(TYPE_COUNT);
545 else if (cmd == "edges")
547 else if (cmd == "refs") {
548 cell addr = read_cell_hex();
549 std::cout << "Data heap references:" << std::endl;
550 find_data_references(addr);
551 std::cout << std::endl;
552 } else if (cmd == "words")
553 dump_objects(WORD_TYPE);
554 else if (cmd == "tuples")
555 dump_objects(TUPLE_TYPE);
556 else if (cmd == "push") {
557 cell addr = read_cell_hex();
559 } else if (cmd == "code")
561 else if (cmd == "compact-gc")
562 primitive_compact_gc();
563 else if (cmd == "gc")
565 else if (cmd == "compact-gc")
566 primitive_compact_gc();
567 else if (cmd == "help")
568 factorbug_usage(true);
569 else if (cmd == "abort")
571 else if (cmd == "breakpoint")
574 std::cout << "unknown command" << std::endl;
578 void factor_vm::primitive_die() {
579 std::cout << "The die word was called by the library. Unless you called it "
580 "yourself," << std::endl;
581 std::cout << "you have triggered a bug in Factor. Please report."