7 bool factor_print_p = true;
9 ostream& operator<<(ostream& out, const string* str) {
10 for (cell i = 0; i < string_capacity(str); i++)
11 out << (char)str->data()[i];
15 void factor_vm::print_word(ostream& out, word* word, cell nesting) {
16 if (tagged<object>(word->vocabulary).type_p(STRING_TYPE))
17 out << untag<string>(word->vocabulary) << ":";
19 if (tagged<object>(word->name).type_p(STRING_TYPE))
20 out << untag<string>(word->name);
22 out << "#<not a string: ";
23 print_nested_obj(out, word->name, nesting);
28 void factor_vm::print_factor_string(ostream& out, string* str) {
29 out << '"' << str << '"';
32 void factor_vm::print_array(ostream& out, array* array, cell nesting) {
33 cell length = array_capacity(array);
37 if (length > 10 && !full_output) {
43 for (i = 0; i < length; i++) {
45 print_nested_obj(out, array_nth(array, i), nesting);
52 void factor_vm::print_alien(ostream& out, alien* alien, cell nesting) {
53 if (to_boolean(alien->expired))
54 out << "#<expired alien>";
55 else if (to_boolean(alien->base)) {
56 out << "#<displaced alien " << alien->displacement << "+";
57 print_nested_obj(out, alien->base, nesting);
60 out << "#<alien " << (void*)alien->address << ">";
64 void factor_vm::print_byte_array(ostream& out, byte_array* array, cell nesting) {
65 cell length = array->capacity;
68 unsigned char* data = array->data<unsigned char>();
70 if (length > 16 && !full_output) {
76 for (i = 0; i < length; i++) {
77 out << " " << (unsigned) data[i];
84 void factor_vm::print_tuple(ostream& out, tuple* tuple, cell nesting) {
85 tuple_layout* layout = untag<tuple_layout>(tuple->layout);
86 cell length = to_cell(layout->size);
89 print_nested_obj(out, layout->klass, nesting);
92 if (length > 10 && !full_output) {
98 for (cell i = 0; i < length; i++) {
100 print_nested_obj(out, tuple->data()[i], nesting);
107 void factor_vm::print_nested_obj(ostream& out, cell obj, fixnum nesting) {
108 if (nesting <= 0 && !full_output) {
115 switch (tagged<object>(obj).type()) {
117 out << untag_fixnum(obj);
120 out << untag_float(obj);
123 print_word(out, untag<word>(obj), nesting - 1);
126 print_factor_string(out, untag<string>(obj));
133 print_tuple(out, untag<tuple>(obj), nesting - 1);
138 print_nested_obj(out, untag<wrapper>(obj)->object, nesting - 1);
141 case BYTE_ARRAY_TYPE:
143 print_byte_array(out, untag<byte_array>(obj), nesting - 1);
148 print_array(out, untag<array>(obj), nesting - 1);
153 quot = untag<quotation>(obj);
154 print_array(out, untag<array>(quot->array), nesting - 1);
158 print_alien(out, untag<alien>(obj), nesting - 1);
161 out << "#<" << type_name(tagged<object>(obj).type()) << " @ ";
162 out << (void*)obj << ">";
168 void factor_vm::print_obj(ostream& out, cell obj) {
169 print_nested_obj(out, obj, 10);
172 void factor_vm::print_objects(ostream& out, cell* start, cell* end) {
173 for (; start <= end; start++) {
174 print_obj(out, *start);
179 void factor_vm::print_datastack(ostream& out) {
180 out << "==== DATA STACK:" << endl;
183 (cell*)ctx->datastack_seg->start,
184 (cell*)ctx->datastack);
186 out << "*** Context not initialized" << endl;
189 void factor_vm::print_retainstack(ostream& out) {
190 out << "==== RETAIN STACK:" << endl;
193 (cell*)ctx->retainstack_seg->start,
194 (cell*)ctx->retainstack);
196 out << "*** Context not initialized" << endl;
199 struct stack_frame_printer {
203 explicit stack_frame_printer(factor_vm* parent, ostream& out)
204 : parent(parent), out(out) {}
205 void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
207 out << "frame: " << (void*)frame_top << " size " << size << endl;
208 out << "executing: ";
209 parent->print_obj(out, owner->owner);
212 parent->print_obj(out, owner->scan(parent, addr));
214 out << "word/quot addr: ";
215 out << hex << owner->owner << dec;
217 out << "word/quot xt: ";
218 out << hex << owner->entry_point() << dec;
220 out << "return address: ";
221 out << hex << addr << dec;
226 void factor_vm::print_callstack(ostream& out) {
227 out << "==== CALL STACK:" << endl;
229 stack_frame_printer printer(this, out);
230 iterate_callstack(ctx, printer);
232 out << "*** Context not initialized" << endl;
235 void factor_vm::print_callstack_object(ostream& out, callstack* obj) {
236 stack_frame_printer printer(this, out);
237 iterate_callstack_object(obj, printer);
240 struct padded_address {
243 explicit padded_address(cell value) : value(value) {}
246 ostream& operator<<(ostream& out, const padded_address& value) {
247 char prev = out.fill('0');
248 out.width(sizeof(cell) * 2);
249 out << hex << value.value << dec;
254 void factor_vm::dump_cell(ostream& out, cell x) {
255 out << padded_address(x) << ": ";
257 out << padded_address(x) << " tag " << TAG(x) << endl;
260 void factor_vm::dump_memory(ostream& out, cell from, cell to) {
263 for (; from <= to; from += sizeof(cell))
264 dump_cell(out, from);
267 template <typename Generation>
268 void factor_vm::dump_generation(ostream& out, const char* name, Generation* gen) {
270 out << "Start=" << gen->start;
271 out << ", size=" << gen->size;
272 out << ", end=" << gen->end;
276 void factor_vm::dump_generations(ostream& out) {
279 dump_generation(out, "Nursery", &nursery);
280 dump_generation(out, "Aging", data->aging);
281 dump_generation(out, "Tenured", data->tenured);
284 out << "base=" << (cell)data->cards << ", ";
285 out << "size=" << (cell)(data->cards_end - data->cards) << endl;
289 void factor_vm::dump_objects(ostream& out, cell type) {
291 auto object_dumper = [&](object* obj) {
292 if (type == TYPE_COUNT || obj->type() == type) {
293 out << padded_address((cell)obj) << " ";
294 print_nested_obj(out, tag_dynamic(obj), 2);
298 each_object(object_dumper);
301 struct find_data_reference_slot_visitor {
307 find_data_reference_slot_visitor(cell look_for,
311 : look_for(look_for), obj(obj), parent(parent), out(out) {}
313 void operator()(cell* scan) {
314 if (look_for == *scan) {
315 out << padded_address((cell)obj) << " ";
316 parent->print_nested_obj(out, tag_dynamic(obj), 2);
322 struct dump_edges_slot_visitor {
327 dump_edges_slot_visitor(cell, object* obj, factor_vm* parent, ostream& out)
328 : obj(obj), parent(parent), out(out) {}
330 void operator()(cell* scan) {
331 if (TAG(*scan) > F_TYPE)
332 out << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan << endl;
336 template <typename SlotVisitor> struct data_reference_object_visitor {
341 data_reference_object_visitor(cell look_for, factor_vm* parent, ostream& out)
342 : look_for(look_for), parent(parent), out(out) {}
344 void operator()(object* obj) {
345 SlotVisitor visitor(look_for, obj, parent, out);
346 obj->each_slot(visitor);
350 void factor_vm::find_data_references(ostream& out, cell look_for) {
351 data_reference_object_visitor<find_data_reference_slot_visitor>
352 visitor(look_for, this, out);
353 each_object(visitor);
356 void factor_vm::dump_edges(ostream& out) {
357 data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this, out);
358 each_object(visitor);
361 struct code_block_printer {
364 cell reloc_size, parameter_size;
366 explicit code_block_printer(factor_vm* parent, ostream& out)
367 : parent(parent), out(out), reloc_size(0), parameter_size(0) {}
369 void operator()(code_block* scan, cell size) {
374 reloc_size += parent->object_size(scan->relocation);
375 parameter_size += parent->object_size(scan->parameters);
377 if (parent->code->allocator->state.marked_p((cell)scan))
380 status = "allocated";
382 out << hex << (cell)scan << dec << " ";
383 out << hex << size << dec << " ";
384 out << status << " ";
385 out << "stack frame " << scan->stack_frame_size();
391 /* Dump all code blocks for debugging */
392 void factor_vm::dump_code_heap(ostream& out) {
393 code_block_printer printer(this, out);
394 code->allocator->iterate(printer);
395 out << printer.reloc_size << " bytes used by relocation tables" << endl;
396 out << printer.parameter_size << " bytes used by parameter tables" << endl;
399 void factor_vm::factorbug_usage(bool advanced_p) {
400 cout << "Basic commands:" << endl;
402 cout << " q ^Z -- quit Factor" << endl;
404 cout << " q ^D -- quit Factor" << endl;
406 cout << " c -- continue executing Factor - NOT SAFE"
408 cout << " t -- throw exception in Factor - NOT SAFE"
410 cout << " .s .r .c -- print data, retain, call stacks"
413 cout << " help -- reprint this message" << endl;
414 cout << "Advanced commands:" << endl;
415 cout << " e -- dump environment" << endl;
416 cout << " d <addr> <count> -- dump memory" << endl;
417 cout << " u <addr> -- dump object at tagged <addr>"
419 cout << " . <addr> -- print object at tagged <addr>"
421 cout << " g -- dump generations" << endl;
422 cout << " ds dr -- dump data, retain stacks" << endl;
423 cout << " trim -- toggle output trimming" << endl;
424 cout << " data -- data heap dump" << endl;
425 cout << " words -- words dump" << endl;
426 cout << " tuples -- tuples dump" << endl;
427 cout << " edges -- print all object-to-object references"
429 cout << " refs <addr> -- find data heap references to object"
431 cout << " push <addr> -- push object on data stack - NOT SAFE"
433 cout << " gc -- trigger full GC - NOT SAFE"
435 cout << " compact-gc -- trigger compacting GC - NOT SAFE"
437 cout << " code -- code heap dump" << endl;
438 cout << " abort -- call abort()" << endl;
439 cout << " breakpoint -- trigger system breakpoint" << endl;
441 cout << " help -- full help, including advanced commands"
447 static void exit_fep(factor_vm* vm) {
453 void factor_vm::factorbug() {
455 cout << "Low level debugger disabled" << endl;
459 if (sampling_profiler_p)
460 end_sampling_profiler();
464 cout << "Starting low level debugger..." << endl;
466 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
467 // that pumps the console is still running concurrently. We lock a mutex so
468 // the thread will take a break and give us exclusive access to stdin.
472 if (!fep_help_was_shown) {
473 factorbug_usage(false);
474 fep_help_was_shown = true;
476 bool seen_command = false;
481 cout << "> " << flush;
483 cin >> setw(1024) >> cmd >> setw(0);
486 /* If we exit with an EOF immediately, then
487 dump stacks. This is useful for builder and
488 other cases where Factor is run with stdin
489 redirected to /dev/null */
492 print_datastack(cout);
493 print_retainstack(cout);
494 print_callstack(cout);
505 cell addr = read_cell_hex();
506 if (cin.peek() == ' ')
511 cell count = read_cell_hex();
512 dump_memory(cout, addr, addr + count);
513 } else if (cmd == "u") {
514 cell addr = read_cell_hex();
515 cell count = object_size(addr);
516 dump_memory(cout, addr, addr + count);
517 } else if (cmd == ".") {
518 cell addr = read_cell_hex();
519 print_obj(cout, addr);
521 } else if (cmd == "trim")
522 full_output = !full_output;
523 else if (cmd == "ds")
524 dump_memory(cout, ctx->datastack_seg->start, ctx->datastack);
525 else if (cmd == "dr")
526 dump_memory(cout, ctx->retainstack_seg->start, ctx->retainstack);
527 else if (cmd == ".s")
528 print_datastack(cout);
529 else if (cmd == ".r")
530 print_retainstack(cout);
531 else if (cmd == ".c")
532 print_callstack(cout);
533 else if (cmd == "e") {
534 for (cell i = 0; i < special_object_count; i++)
535 dump_cell(cout, (cell)&special_objects[i]);
536 } else if (cmd == "g")
537 dump_generations(cout);
538 else if (cmd == "c") {
541 } else if (cmd == "t") {
543 general_error(ERROR_INTERRUPT, false_object, false_object);
544 FACTOR_ASSERT(false);
545 } else if (cmd == "data")
546 dump_objects(cout, TYPE_COUNT);
547 else if (cmd == "edges")
549 else if (cmd == "refs") {
550 cell addr = read_cell_hex();
551 cout << "Data heap references:" << endl;
552 find_data_references(cout, addr);
554 } else if (cmd == "words")
555 dump_objects(cout, WORD_TYPE);
556 else if (cmd == "tuples")
557 dump_objects(cout, TUPLE_TYPE);
558 else if (cmd == "push") {
559 cell addr = read_cell_hex();
561 } else if (cmd == "code")
562 dump_code_heap(cout);
563 else if (cmd == "compact-gc")
564 primitive_compact_gc();
565 else if (cmd == "gc")
567 else if (cmd == "compact-gc")
568 primitive_compact_gc();
569 else if (cmd == "help")
570 factorbug_usage(true);
571 else if (cmd == "abort")
573 else if (cmd == "breakpoint")
576 cout << "unknown command" << endl;
580 void factor_vm::primitive_die() {
581 cout << "The die word was called by the library. Unless you called it "
583 cout << "you have triggered a bug in Factor. Please report."