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()(void* frame_top, cell frame_size, code_block* owner,
195 std::cout << std::endl;
196 std::cout << "frame: " << frame_top << " size " << frame_size << std::endl;
197 std::cout << "executing: ";
198 parent->print_obj(owner->owner);
199 std::cout << std::endl;
200 std::cout << "scan: ";
201 parent->print_obj(owner->scan(parent, addr));
202 std::cout << std::endl;
203 std::cout << "word/quot addr: ";
204 std::cout << std::hex << (cell)owner->owner << std::dec;
205 std::cout << std::endl;
206 std::cout << "word/quot xt: ";
207 std::cout << std::hex << (cell)owner->entry_point() << std::dec;
208 std::cout << std::endl;
209 std::cout << "return address: ";
210 std::cout << std::hex << (cell)addr << std::dec;
211 std::cout << std::endl;
215 void factor_vm::print_callstack() {
216 std::cout << "==== CALL STACK:" << std::endl;
218 stack_frame_printer printer(this);
219 iterate_callstack(ctx, printer);
221 std::cout << "*** Context not initialized" << std::endl;
224 void factor_vm::print_callstack_object(callstack* obj) {
225 stack_frame_printer printer(this);
226 iterate_callstack_object(obj, printer);
229 struct padded_address {
232 explicit padded_address(cell value) : value(value) {}
235 std::ostream& operator<<(std::ostream& out, const padded_address& value) {
236 char prev = out.fill('0');
237 out.width(sizeof(cell) * 2);
238 out << std::hex << value.value << std::dec;
243 void factor_vm::dump_cell(cell x) {
244 std::cout << padded_address(x) << ": ";
246 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
249 void factor_vm::dump_memory(cell from, cell to) {
252 for (; from <= to; from += sizeof(cell))
256 template <typename Generation>
257 void factor_vm::dump_generation(const char* name, Generation* gen) {
258 std::cout << name << ": ";
259 std::cout << "Start=" << gen->start;
260 std::cout << ", size=" << gen->size;
261 std::cout << ", end=" << gen->end;
262 std::cout << std::endl;
265 void factor_vm::dump_generations() {
266 std::cout << std::hex;
268 dump_generation("Nursery", &nursery);
269 dump_generation("Aging", data->aging);
270 dump_generation("Tenured", data->tenured);
272 std::cout << "Cards:";
273 std::cout << "base=" << (cell)data->cards << ", ";
274 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
276 std::cout << std::dec;
279 struct object_dumper {
283 object_dumper(factor_vm* parent, cell type)
284 : parent(parent), type(type) {}
286 void operator()(object* obj) {
287 if (type == TYPE_COUNT || obj->type() == type) {
288 std::cout << padded_address((cell)obj) << " ";
289 parent->print_nested_obj(tag_dynamic(obj), 2);
290 std::cout << std::endl;
295 void factor_vm::dump_objects(cell type) {
297 object_dumper dumper(this, type);
301 struct find_data_reference_slot_visitor {
306 find_data_reference_slot_visitor(cell look_for, object* obj,
308 : look_for(look_for), obj(obj), parent(parent) {}
310 void operator()(cell* scan) {
311 if (look_for == *scan) {
312 std::cout << padded_address((cell)obj) << " ";
313 parent->print_nested_obj(tag_dynamic(obj), 2);
314 std::cout << std::endl;
319 struct dump_edges_slot_visitor {
323 dump_edges_slot_visitor(cell, object* obj, factor_vm* parent)
324 : obj(obj), parent(parent) {}
326 void operator()(cell* scan) {
327 if (TAG(*scan) > F_TYPE)
328 std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan
333 template <typename SlotVisitor> struct data_reference_object_visitor {
337 data_reference_object_visitor(cell look_for, factor_vm* parent)
338 : look_for(look_for), parent(parent) {}
340 void operator()(object* obj) {
341 SlotVisitor visitor(look_for, obj, parent);
342 obj->each_slot(visitor);
346 void factor_vm::find_data_references(cell look_for) {
347 data_reference_object_visitor<find_data_reference_slot_visitor> visitor(
349 each_object(visitor);
352 void factor_vm::dump_edges() {
353 data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this);
354 each_object(visitor);
357 struct code_block_printer {
359 cell reloc_size, parameter_size;
361 explicit code_block_printer(factor_vm* parent)
362 : parent(parent), reloc_size(0), parameter_size(0) {}
364 void operator()(code_block* scan, cell size) {
369 reloc_size += parent->object_size(scan->relocation);
370 parameter_size += parent->object_size(scan->parameters);
372 if (parent->code->marked_p(scan))
375 status = "allocated";
377 std::cout << std::hex << (cell)scan << std::dec << " ";
378 std::cout << std::hex << size << std::dec << " ";
379 std::cout << status << " ";
380 std::cout << "stack frame " << scan->stack_frame_size();
381 std::cout << std::endl;
386 /* Dump all code blocks for debugging */
387 void factor_vm::dump_code_heap() {
388 code_block_printer printer(this);
389 code->allocator->iterate(printer);
390 std::cout << printer.reloc_size << " bytes used by relocation tables"
392 std::cout << printer.parameter_size << " bytes used by parameter tables"
396 void factor_vm::factorbug_usage(bool advanced_p) {
397 std::cout << "Basic commands:" << std::endl;
399 std::cout << " q ^Z -- quit Factor" << std::endl;
401 std::cout << " q ^D -- quit Factor" << std::endl;
403 std::cout << " c -- continue executing Factor - NOT SAFE"
405 std::cout << " t -- throw exception in Factor - NOT SAFE"
407 std::cout << " .s .r .c -- print data, retain, call stacks"
410 std::cout << " help -- reprint this message" << std::endl;
411 std::cout << "Advanced commands:" << std::endl;
412 std::cout << " e -- dump environment" << std::endl;
413 std::cout << " d <addr> <count> -- dump memory" << std::endl;
414 std::cout << " u <addr> -- dump object at tagged <addr>"
416 std::cout << " . <addr> -- print object at tagged <addr>"
418 std::cout << " g -- dump generations" << std::endl;
419 std::cout << " ds dr -- dump data, retain stacks" << std::endl;
420 std::cout << " trim -- toggle output trimming" << std::endl;
421 std::cout << " data -- data heap dump" << std::endl;
422 std::cout << " words -- words dump" << std::endl;
423 std::cout << " tuples -- tuples dump" << std::endl;
424 std::cout << " edges -- print all object-to-object references"
426 std::cout << " refs <addr> -- find data heap references to object"
428 std::cout << " push <addr> -- push object on data stack - NOT SAFE"
430 std::cout << " gc -- trigger full GC - NOT SAFE"
432 std::cout << " compact-gc -- trigger compacting GC - NOT SAFE"
434 std::cout << " code -- code heap dump" << std::endl;
435 std::cout << " abort -- call abort()" << std::endl;
436 std::cout << " breakpoint -- trigger system breakpoint" << std::endl;
438 std::cout << " help -- full help, including advanced commands"
442 std::cout << std::endl;
446 static void exit_fep(factor_vm* vm) {
447 vm->unlock_console();
452 void factor_vm::factorbug() {
454 std::cout << "Low level debugger disabled" << std::endl;
458 if (sampling_profiler_p)
459 end_sampling_profiler();
463 std::cout << "Starting low level debugger..." << std::endl;
465 // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
466 // that pumps the console is still running concurrently. We lock a mutex so
467 // the thread will take a break and give us exclusive access to stdin.
471 if (!fep_help_was_shown) {
472 factorbug_usage(false);
473 fep_help_was_shown = true;
475 bool seen_command = false;
480 std::cout << "> " << std::flush;
482 std::cin >> std::setw(1024) >> cmd >> std::setw(0);
483 if (!std::cin.good()) {
485 /* If we exit with an EOF immediately, then
486 dump stacks. This is useful for builder and
487 other cases where Factor is run with stdin
488 redirected to /dev/null */
504 cell addr = read_cell_hex();
505 if (std::cin.peek() == ' ')
508 if (!std::cin.good())
510 cell count = read_cell_hex();
511 dump_memory(addr, addr + count);
512 } else if (cmd == "u") {
513 cell addr = read_cell_hex();
514 cell count = object_size(addr);
515 dump_memory(addr, addr + count);
516 } else if (cmd == ".") {
517 cell addr = read_cell_hex();
519 std::cout << std::endl;
520 } else if (cmd == "trim")
521 full_output = !full_output;
522 else if (cmd == "ds")
523 dump_memory(ctx->datastack_seg->start, ctx->datastack);
524 else if (cmd == "dr")
525 dump_memory(ctx->retainstack_seg->start, ctx->retainstack);
526 else if (cmd == ".s")
528 else if (cmd == ".r")
530 else if (cmd == ".c")
532 else if (cmd == "e") {
533 for (cell i = 0; i < special_object_count; i++)
534 dump_cell((cell)&special_objects[i]);
535 } else if (cmd == "g")
537 else if (cmd == "c") {
540 } else if (cmd == "t") {
542 general_error(ERROR_INTERRUPT, false_object, false_object);
543 FACTOR_ASSERT(false);
544 } else if (cmd == "data")
545 dump_objects(TYPE_COUNT);
546 else if (cmd == "edges")
548 else if (cmd == "refs") {
549 cell addr = read_cell_hex();
550 std::cout << "Data heap references:" << std::endl;
551 find_data_references(addr);
552 std::cout << std::endl;
553 } else if (cmd == "words")
554 dump_objects(WORD_TYPE);
555 else if (cmd == "tuples")
556 dump_objects(TUPLE_TYPE);
557 else if (cmd == "push") {
558 cell addr = read_cell_hex();
560 } else if (cmd == "code")
562 else if (cmd == "compact-gc")
563 primitive_compact_gc();
564 else if (cmd == "gc")
566 else if (cmd == "compact-gc")
567 primitive_compact_gc();
568 else if (cmd == "help")
569 factorbug_usage(true);
570 else if (cmd == "abort")
572 else if (cmd == "breakpoint")
575 std::cout << "unknown command" << std::endl;
579 void factor_vm::primitive_die() {
580 std::cout << "The die word was called by the library. Unless you called it "
581 "yourself," << std::endl;
582 std::cout << "you have triggered a bug in Factor. Please report."