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_tuple(tuple *tuple, cell nesting)
59 tuple_layout *layout = untag<tuple_layout>(tuple->layout);
60 cell length = to_fixnum(layout->size);
63 print_nested_obj(layout->klass,nesting);
66 if(length > 10 && !full_output)
74 for(cell i = 0; i < length; i++)
77 print_nested_obj(tuple->data()[i],nesting);
84 void factor_vm::print_nested_obj(cell obj, fixnum nesting)
86 if(nesting <= 0 && !full_output)
94 switch(tagged<object>(obj).type())
97 std::cout << untag_fixnum(obj);
100 print_word(untag<word>(obj),nesting - 1);
103 print_factor_string(untag<string>(obj));
110 print_tuple(untag<tuple>(obj),nesting - 1);
115 print_array(untag<array>(obj),nesting - 1);
120 quot = untag<quotation>(obj);
121 print_array(untag<array>(quot->array),nesting - 1);
125 std::cout << "#<type " << tagged<object>(obj).type() << " @ ";
126 std::cout << std::hex << obj << std::dec << ">";
131 void factor_vm::print_obj(cell obj)
133 print_nested_obj(obj,10);
136 void factor_vm::print_objects(cell *start, cell *end)
138 for(; start <= end; start++)
141 std::cout << std::endl;
145 void factor_vm::print_datastack()
147 std::cout << "==== DATA STACK:" << std::endl;
148 print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack);
151 void factor_vm::print_retainstack()
153 std::cout << "==== RETAIN STACK:" << std::endl;
154 print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack);
157 struct stack_frame_printer {
160 explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
161 void operator()(stack_frame *frame)
163 std::cout << "frame: " << std::hex << (cell)frame << std::dec << std::endl;
164 std::cout << "executing: ";
165 parent->print_obj(parent->frame_executing(frame));
166 std::cout << std::endl;
167 std::cout << "scan: ";
168 parent->print_obj(parent->frame_scan(frame));
169 std::cout << std::endl;
170 std::cout << "word/quot addr: ";
171 std::cout << std::hex << (cell)parent->frame_executing(frame) << std::dec;
172 std::cout << std::endl;
173 std::cout << "word/quot xt: ";
174 std::cout << std::hex << (cell)frame->entry_point << std::dec;
175 std::cout << std::endl;
176 std::cout << "return address: ";
177 std::cout << std::hex << (cell)FRAME_RETURN_ADDRESS(frame,parent) << std::dec;
178 std::cout << std::endl;
182 void factor_vm::print_callstack()
184 std::cout << "==== CALL STACK:" << std::endl;
185 stack_frame_printer printer(this);
186 iterate_callstack(ctx,printer);
189 struct padded_address {
192 explicit padded_address(cell value_) : value(value_) {}
195 std::ostream &operator<<(std::ostream &out, const padded_address &value)
197 char prev = out.fill('0');
198 out.width(sizeof(cell) * 2);
199 out << std::hex << value.value << std::dec;
204 void factor_vm::dump_cell(cell x)
206 std::cout << padded_address(x) << ": ";
208 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
211 void factor_vm::dump_memory(cell from, cell to)
215 for(; from <= to; from += sizeof(cell))
219 template<typename Generation>
220 void factor_vm::dump_generation(const char *name, Generation *gen)
222 std::cout << name << ": ";
223 std::cout << "Start=" << gen->start;
224 std::cout << ", size=" << gen->size;
225 std::cout << ", end=" << gen->end;
226 std::cout << std::endl;
229 void factor_vm::dump_generations()
231 std::cout << std::hex;
233 dump_generation("Nursery",&nursery);
234 dump_generation("Aging",data->aging);
235 dump_generation("Tenured",data->tenured);
237 std::cout << "Cards:";
238 std::cout << "base=" << (cell)data->cards << ", ";
239 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
241 std::cout << std::dec;
244 struct object_dumper {
248 explicit object_dumper(factor_vm *parent_, cell type_) :
249 parent(parent_), type(type_) {}
251 void operator()(object *obj)
253 if(type == TYPE_COUNT || obj->type() == type)
255 std::cout << padded_address((cell)obj) << " ";
256 parent->print_nested_obj(tag_dynamic(obj),2);
257 std::cout << std::endl;
262 void factor_vm::dump_objects(cell type)
265 object_dumper dumper(this,type);
269 struct data_reference_slot_visitor {
274 explicit data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
275 look_for(look_for_), obj(obj_), parent(parent_) { }
277 void operator()(cell *scan)
279 if(look_for == *scan)
281 std::cout << padded_address((cell)obj) << " ";
282 parent->print_nested_obj(tag_dynamic(obj),2);
283 std::cout << std::endl;
288 struct data_reference_object_visitor {
292 explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
293 look_for(look_for_), parent(parent_) {}
295 void operator()(object *obj)
297 data_reference_slot_visitor visitor(look_for,obj,parent);
298 obj->each_slot(visitor);
302 void factor_vm::find_data_references(cell look_for)
304 data_reference_object_visitor visitor(look_for,this);
305 each_object(visitor);
308 struct code_block_printer {
310 cell reloc_size, parameter_size;
312 explicit code_block_printer(factor_vm *parent_) :
313 parent(parent_), reloc_size(0), parameter_size(0) {}
315 void operator()(code_block *scan, cell size)
322 reloc_size += parent->object_size(scan->relocation);
323 parameter_size += parent->object_size(scan->parameters);
325 if(parent->code->marked_p(scan))
328 status = "allocated";
330 std::cout << std::hex << (cell)scan << std::dec << " ";
331 std::cout << std::hex << size << std::dec << " ";
332 std::cout << status << std::endl;
337 /* Dump all code blocks for debugging */
338 void factor_vm::dump_code_heap()
340 code_block_printer printer(this);
341 code->allocator->iterate(printer);
342 std::cout << printer.reloc_size << " bytes used by relocation tables" << std::endl;
343 std::cout << printer.parameter_size << " bytes used by parameter tables" << std::endl;
346 void factor_vm::factorbug_usage(bool advanced_p)
348 std::cout << "Basic commands:" << std::endl;
350 std::cout << " q ^Z -- quit Factor" << std::endl;
352 std::cout << " q ^D -- quit Factor" << std::endl;
354 std::cout << " c -- continue executing Factor - NOT SAFE" << std::endl;
355 std::cout << " t -- throw exception in Factor - NOT SAFE" << std::endl;
356 std::cout << " .s .r .c -- print data, retain, call stacks" << std::endl;
359 std::cout << " help -- reprint this message" << std::endl;
360 std::cout << "Advanced commands:" << std::endl;
361 std::cout << " e -- dump environment" << std::endl;
362 std::cout << " d <addr> <count> -- dump memory" << std::endl;
363 std::cout << " u <addr> -- dump object at tagged <addr>" << std::endl;
364 std::cout << " . <addr> -- print object at tagged <addr>" << std::endl;
365 std::cout << " g -- dump generations" << std::endl;
366 std::cout << " ds dr -- dump data, retain stacks" << std::endl;
367 std::cout << " trim -- toggle output trimming" << std::endl;
368 std::cout << " data -- data heap dump" << std::endl;
369 std::cout << " words -- words dump" << std::endl;
370 std::cout << " tuples -- tuples dump" << std::endl;
371 std::cout << " refs <addr> -- find data heap references to object" << std::endl;
372 std::cout << " push <addr> -- push object on data stack - NOT SAFE" << std::endl;
373 std::cout << " gc -- trigger full GC - NOT SAFE" << std::endl;
374 std::cout << " code -- code heap dump" << std::endl;
378 std::cout << " help -- full help, including advanced commands" << std::endl;
381 std::cout << std::endl;
385 void factor_vm::factorbug()
389 std::cout << "Low level debugger disabled" << std::endl;
395 std::cout << "Starting low level debugger..." << std::endl;
396 factorbug_usage(false);
397 bool seen_command = false;
403 std::cout << "> " << std::flush;
405 std::cin >> std::setw(1024) >> cmd >> std::setw(0);
410 /* If we exit with an EOF immediately, then
411 dump stacks. This is useful for builder and
412 other cases where Factor is run with stdin
413 redirected to /dev/null */
426 if(strcmp(cmd,"q") == 0)
428 if(strcmp(cmd,"d") == 0)
430 cell addr = read_cell_hex();
431 if (std::cin.peek() == ' ')
434 if(!std::cin.good()) break;
435 cell count = read_cell_hex();
436 dump_memory(addr,addr+count);
438 else if(strcmp(cmd,"u") == 0)
440 cell addr = read_cell_hex();
441 cell count = object_size(addr);
442 dump_memory(addr,addr+count);
444 else if(strcmp(cmd,".") == 0)
446 cell addr = read_cell_hex();
448 std::cout << std::endl;
450 else if(strcmp(cmd,"trim") == 0)
451 full_output = !full_output;
452 else if(strcmp(cmd,"ds") == 0)
453 dump_memory(ctx->datastack_seg->start,ctx->datastack);
454 else if(strcmp(cmd,"dr") == 0)
455 dump_memory(ctx->retainstack_seg->start,ctx->retainstack);
456 else if(strcmp(cmd,".s") == 0)
458 else if(strcmp(cmd,".r") == 0)
460 else if(strcmp(cmd,".c") == 0)
462 else if(strcmp(cmd,"e") == 0)
464 for(cell i = 0; i < special_object_count; i++)
465 dump_cell((cell)&special_objects[i]);
467 else if(strcmp(cmd,"g") == 0)
469 else if(strcmp(cmd,"c") == 0)
474 else if(strcmp(cmd,"t") == 0)
477 general_error(ERROR_INTERRUPT,false_object,false_object);
480 else if(strcmp(cmd,"data") == 0)
481 dump_objects(TYPE_COUNT);
482 else if(strcmp(cmd,"refs") == 0)
484 cell addr = read_cell_hex();
485 std::cout << "Data heap references:" << std::endl;
486 find_data_references(addr);
487 std::cout << std::endl;
489 else if(strcmp(cmd,"words") == 0)
490 dump_objects(WORD_TYPE);
491 else if(strcmp(cmd,"tuples") == 0)
492 dump_objects(TUPLE_TYPE);
493 else if(strcmp(cmd,"push") == 0)
495 cell addr = read_cell_hex();
498 else if(strcmp(cmd,"code") == 0)
500 else if(strcmp(cmd,"gc") == 0)
502 else if(strcmp(cmd,"help") == 0)
503 factorbug_usage(true);
505 std::cout << "unknown command" << std::endl;
509 void factor_vm::primitive_die()
511 std::cout << "The die word was called by the library. Unless you called it yourself," << std::endl;
512 std::cout << "you have triggered a bug in Factor. Please report." << std::endl;