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 (TAG(word->vocabulary) == STRING_TYPE)
17 out << untag<string>(word->vocabulary) << ":";
19 if (TAG(word->name) == 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) {
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(TAG(obj)) << " @ ";
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 void dump_memory_range(ostream& out, const char* name, cell name_w,
268 cell start, cell end) {
269 out << setw(name_w) << left << name << ": ";
271 out << "[" << (void*)start << " -> " << (void*)end << "] ";
272 out << setw(10) << right << (end - start) << " bytes" << endl;
275 template <typename Generation>
276 void dump_generation(ostream& out, const char* name, Generation* gen) {
277 dump_memory_range(out, name, 10, gen->start, gen->end);
280 void factor_vm::dump_memory_layout(ostream& out) {
281 dump_generation(out, "Nursery", data->nursery);
282 dump_generation(out, "Aging", data->aging);
283 dump_generation(out, "Tenured", data->tenured);
284 dump_memory_range(out, "Cards", 10, (cell)data->cards, (cell)data->cards_end);
286 out << endl << "Contexts:" << endl << endl;
287 FACTOR_FOR_EACH(active_contexts) {
288 context* the_ctx = *iter;
289 segment* ds = the_ctx->datastack_seg;
290 segment* rs = the_ctx->retainstack_seg;
291 segment* cs = the_ctx->callstack_seg;
292 if (the_ctx == ctx) {
293 out << " Active:" << endl;
295 dump_memory_range(out, " Datastack", 14, ds->start, ds->end);
296 dump_memory_range(out, " Retainstack", 14, rs->start, rs->end);
297 dump_memory_range(out, " Callstack", 14, cs->start, cs->end);
302 void factor_vm::dump_objects(ostream& out, cell type) {
304 auto object_dumper = [&](object* obj) {
305 if (type == TYPE_COUNT || obj->type() == type) {
306 out << padded_address((cell)obj) << " ";
307 print_nested_obj(out, tag_dynamic(obj), 2);
311 each_object(object_dumper);
314 void factor_vm::find_data_references(ostream& out, cell look_for) {
316 auto find_data_ref_func = [&](object* obj, cell* slot) {
317 if (look_for == *slot) {
318 out << padded_address((cell)obj) << " ";
319 print_nested_obj(out, tag_dynamic(obj), 2);
323 each_object_each_slot(find_data_ref_func);
326 void factor_vm::dump_edges(ostream& out) {
328 auto dump_edges_func = [&](object* obj, cell* scan) {
329 if (TAG(*scan) > F_TYPE) {
330 out << (void*)tag_dynamic(obj);
332 out << (void*)*scan << endl;
335 each_object_each_slot(dump_edges_func);
338 struct code_block_printer {
341 cell reloc_size, parameter_size;
343 explicit code_block_printer(factor_vm* parent, ostream& out)
344 : parent(parent), out(out), reloc_size(0), parameter_size(0) {}
346 void operator()(code_block* scan, cell size) {
351 reloc_size += object_size(scan->relocation);
352 parameter_size += object_size(scan->parameters);
354 if (parent->code->allocator->state.marked_p((cell)scan))
357 status = "allocated";
359 out << hex << (cell)scan << dec << " ";
360 out << hex << size << dec << " ";
361 out << status << " ";
362 out << "stack frame " << scan->stack_frame_size();
368 // Dump all code blocks for debugging
369 void factor_vm::dump_code_heap(ostream& out) {
370 code_block_printer printer(this, out);
371 code->allocator->iterate(printer, no_fixup());
372 out << printer.reloc_size << " bytes used by relocation tables" << endl;
373 out << printer.parameter_size << " bytes used by parameter tables" << endl;
376 void factor_vm::factorbug_usage(bool advanced_p) {
377 cout << "Basic commands:" << endl;
379 cout << " q ^Z -- quit Factor" << endl;
381 cout << " q ^D -- quit Factor" << endl;
383 cout << " c -- continue executing Factor - NOT SAFE"
385 cout << " t -- throw exception in Factor - NOT SAFE"
387 cout << " .s .r .c -- print data, retain, call stacks"
390 cout << " help -- reprint this message" << endl;
391 cout << "Advanced commands:" << endl;
392 cout << " e -- dump environment" << endl;
393 cout << " d <addr> <count> -- dump memory" << endl;
394 cout << " u <addr> -- dump object at tagged <addr>"
396 cout << " . <addr> -- print object at tagged <addr>"
398 cout << " g -- dump memory layout" << endl;
399 cout << " ds dr -- dump data, retain stacks" << endl;
400 cout << " trim -- toggle output trimming" << endl;
401 cout << " data -- data heap dump" << endl;
402 cout << " words -- words dump" << endl;
403 cout << " tuples -- tuples dump" << endl;
404 cout << " edges -- print all object-to-object references"
406 cout << " refs <addr> -- find data heap references to object"
408 cout << " push <addr> -- push object on data stack - NOT SAFE"
410 cout << " gc -- trigger full GC - NOT SAFE"
412 cout << " compact-gc -- trigger compacting GC - NOT SAFE"
414 cout << " code -- code heap dump" << endl;
415 cout << " abort -- call abort()" << endl;
416 cout << " breakpoint -- trigger system breakpoint" << endl;
418 cout << " help -- full help, including advanced commands"
424 static void exit_fep(factor_vm* vm) {
430 void factor_vm::factorbug() {
432 cout << "Low level debugger disabled" << endl;
436 if (sampling_profiler_p)
437 end_sampling_profiler();
441 cout << "Starting low level debugger..." << endl;
443 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
444 // that pumps the console is still running concurrently. We lock a mutex so
445 // the thread will take a break and give us exclusive access to stdin.
449 if (!fep_help_was_shown) {
450 factorbug_usage(false);
451 fep_help_was_shown = true;
453 bool seen_command = false;
458 cout << "> " << flush;
460 cin >> setw(1024) >> cmd >> setw(0);
463 // If we exit with an EOF immediately, then
464 // dump stacks. This is useful for builder and
465 // other cases where Factor is run with stdin
466 // redirected to /dev/null
469 print_datastack(cout);
470 print_retainstack(cout);
471 print_callstack(cout);
482 cell addr = read_cell_hex();
483 if (cin.peek() == ' ')
488 cell count = read_cell_hex();
489 dump_memory(cout, addr, addr + count);
490 } else if (cmd == "u") {
491 cell addr = read_cell_hex();
492 cell count = object_size(addr);
493 dump_memory(cout, addr, addr + count);
494 } else if (cmd == ".") {
495 cell addr = read_cell_hex();
496 print_obj(cout, addr);
498 } else if (cmd == "trim")
499 full_output = !full_output;
500 else if (cmd == "ds")
501 dump_memory(cout, ctx->datastack_seg->start, ctx->datastack);
502 else if (cmd == "dr")
503 dump_memory(cout, ctx->retainstack_seg->start, ctx->retainstack);
504 else if (cmd == ".s")
505 print_datastack(cout);
506 else if (cmd == ".r")
507 print_retainstack(cout);
508 else if (cmd == ".c")
509 print_callstack(cout);
510 else if (cmd == "e") {
511 for (cell i = 0; i < special_object_count; i++)
512 dump_cell(cout, (cell)&special_objects[i]);
513 } else if (cmd == "g")
514 dump_memory_layout(cout);
515 else if (cmd == "c") {
518 } else if (cmd == "t") {
520 general_error(ERROR_INTERRUPT, false_object, false_object);
521 FACTOR_ASSERT(false);
522 } else if (cmd == "data")
523 dump_objects(cout, TYPE_COUNT);
524 else if (cmd == "edges")
526 else if (cmd == "refs") {
527 cell addr = read_cell_hex();
528 cout << "Data heap references:" << endl;
529 find_data_references(cout, addr);
531 } else if (cmd == "words")
532 dump_objects(cout, WORD_TYPE);
533 else if (cmd == "tuples")
534 dump_objects(cout, TUPLE_TYPE);
535 else if (cmd == "push") {
536 cell addr = read_cell_hex();
538 } else if (cmd == "code")
539 dump_code_heap(cout);
540 else if (cmd == "gc")
542 else if (cmd == "compact-gc")
543 primitive_compact_gc();
544 else if (cmd == "help")
545 factorbug_usage(true);
546 else if (cmd == "abort")
548 else if (cmd == "breakpoint")
551 cout << "unknown command" << endl;
555 void factor_vm::primitive_die() {
556 critical_error("The die word was called by the library.", 0);