6 std::ostream &operator<<(std::ostream &out, const string *str)
8 for(cell i = 0; i < string_capacity(str); i++)
9 out << (char)str->nth(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:\n";
148 print_objects((cell *)ds_bot,(cell *)ds);
151 void factor_vm::print_retainstack()
153 std::cout << "==== RETAIN STACK:\n";
154 print_objects((cell *)rs_bot,(cell *)rs);
157 struct stack_frame_printer {
160 explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
161 void operator()(stack_frame *frame)
163 parent->print_obj(parent->frame_executing(frame));
164 std::cout << std::endl;
165 parent->print_obj(parent->frame_scan(frame));
166 std::cout << std::endl;
167 std::cout << "word/quot addr: ";
168 std::cout << std::hex << (cell)parent->frame_executing(frame) << std::dec;
169 std::cout << std::endl;
170 std::cout << "word/quot xt: ";
171 std::cout << std::hex << (cell)frame->xt << std::dec;
172 std::cout << std::endl;
173 std::cout << "return address: ";
174 std::cout << std::hex << (cell)FRAME_RETURN_ADDRESS(frame,parent) << std::dec;
175 std::cout << std::endl;
179 void factor_vm::print_callstack()
181 std::cout << "==== CALL STACK:\n";
182 stack_frame_printer printer(this);
183 iterate_callstack(ctx,printer);
186 struct padded_address {
189 explicit padded_address(cell value_) : value(value_) {}
192 std::ostream &operator<<(std::ostream &out, const padded_address &value)
194 char prev = out.fill('0');
195 out.width(sizeof(cell) * 2);
196 out << std::hex << value.value << std::dec;
201 void factor_vm::dump_cell(cell x)
203 std::cout << padded_address(x) << ": ";
205 std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
208 void factor_vm::dump_memory(cell from, cell to)
212 for(; from <= to; from += sizeof(cell))
216 template<typename Generation>
217 void factor_vm::dump_generation(const char *name, Generation *gen)
219 std::cout << name << ": ";
220 std::cout << "Start=" << gen->start;
221 std::cout << ", size=" << gen->size;
222 std::cout << ", end=" << gen->end;
223 std::cout << std::endl;
226 void factor_vm::dump_generations()
228 dump_generation("Nursery",&nursery);
229 dump_generation("Aging",data->aging);
230 dump_generation("Tenured",data->tenured);
232 std::cout << "Cards:";
233 std::cout << "base=" << (cell)data->cards << ", ";
234 std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
237 struct object_dumper {
241 explicit object_dumper(factor_vm *parent_, cell type_) :
242 parent(parent_), type(type_) {}
244 void operator()(object *obj)
246 if(type == TYPE_COUNT || obj->type() == type)
248 std::cout << padded_address((cell)obj) << " ";
249 parent->print_nested_obj(tag_dynamic(obj),2);
250 std::cout << std::endl;
255 void factor_vm::dump_objects(cell type)
258 object_dumper dumper(this,type);
262 struct data_reference_slot_visitor {
267 explicit data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
268 look_for(look_for_), obj(obj_), parent(parent_) { }
270 void operator()(cell *scan)
272 if(look_for == *scan)
274 std::cout << padded_address((cell)obj) << " ";
275 parent->print_nested_obj(tag_dynamic(obj),2);
276 std::cout << std::endl;
281 struct data_reference_object_visitor {
285 explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
286 look_for(look_for_), parent(parent_) {}
288 void operator()(object *obj)
290 data_reference_slot_visitor visitor(look_for,obj,parent);
291 parent->do_slots(obj,visitor);
295 void factor_vm::find_data_references(cell look_for)
297 data_reference_object_visitor visitor(look_for,this);
298 each_object(visitor);
301 struct code_block_printer {
303 cell reloc_size, literal_size;
305 explicit code_block_printer(factor_vm *parent_) :
306 parent(parent_), reloc_size(0), literal_size(0) {}
308 void operator()(code_block *scan, cell size)
313 else if(parent->code->marked_p(scan))
315 reloc_size += parent->object_size(scan->relocation);
316 literal_size += parent->object_size(scan->literals);
321 reloc_size += parent->object_size(scan->relocation);
322 literal_size += parent->object_size(scan->literals);
323 status = "allocated";
326 std::cout << std::hex << (cell)scan << std::dec << " ";
327 std::cout << std::hex << size << std::dec << " ";
328 std::cout << status << std::endl;
332 /* Dump all code blocks for debugging */
333 void factor_vm::dump_code_heap()
335 code_block_printer printer(this);
336 code->allocator->iterate(printer);
337 std::cout << printer.reloc_size << " bytes of relocation data\n";
338 std::cout << printer.literal_size << " bytes of literal data\n";
341 void factor_vm::factorbug()
345 std::cout << "Low level debugger disabled\n";
349 /* open_console(); */
351 std::cout << "Starting low level debugger...\n";
352 std::cout << " Basic commands:\n";
353 std::cout << "q -- continue executing Factor - NOT SAFE\n";
354 std::cout << "im -- save image to fep.image\n";
355 std::cout << "x -- exit Factor\n";
356 std::cout << " Advanced commands:\n";
357 std::cout << "d <addr> <count> -- dump memory\n";
358 std::cout << "u <addr> -- dump object at tagged <addr>\n";
359 std::cout << ". <addr> -- print object at tagged <addr>\n";
360 std::cout << "t -- toggle output trimming\n";
361 std::cout << "s r -- dump data, retain stacks\n";
362 std::cout << ".s .r .c -- print data, retain, call stacks\n";
363 std::cout << "e -- dump environment\n";
364 std::cout << "g -- dump generations\n";
365 std::cout << "data -- data heap dump\n";
366 std::cout << "words -- words dump\n";
367 std::cout << "tuples -- tuples dump\n";
368 std::cout << "refs <addr> -- find data heap references to object\n";
369 std::cout << "push <addr> -- push object on data stack - NOT SAFE\n";
370 std::cout << "code -- code heap dump\n";
372 bool seen_command = false;
378 std::cout << "READY\n";
381 if(scanf("%1000s",cmd) <= 0)
385 /* If we exit with an EOF immediately, then
386 dump stacks. This is useful for builder and
387 other cases where Factor is run with stdin
388 redirected to /dev/null */
401 if(strcmp(cmd,"d") == 0)
403 cell addr = read_cell_hex();
404 if(scanf(" ") < 0) break;
405 cell count = read_cell_hex();
406 dump_memory(addr,addr+count);
408 else if(strcmp(cmd,"u") == 0)
410 cell addr = read_cell_hex();
411 cell count = object_size(addr);
412 dump_memory(addr,addr+count);
414 else if(strcmp(cmd,".") == 0)
416 cell addr = read_cell_hex();
418 std::cout << std::endl;
420 else if(strcmp(cmd,"t") == 0)
421 full_output = !full_output;
422 else if(strcmp(cmd,"s") == 0)
423 dump_memory(ds_bot,ds);
424 else if(strcmp(cmd,"r") == 0)
425 dump_memory(rs_bot,rs);
426 else if(strcmp(cmd,".s") == 0)
428 else if(strcmp(cmd,".r") == 0)
430 else if(strcmp(cmd,".c") == 0)
432 else if(strcmp(cmd,"e") == 0)
434 for(cell i = 0; i < special_object_count; i++)
435 dump_cell((cell)&special_objects[i]);
437 else if(strcmp(cmd,"g") == 0)
439 else if(strcmp(cmd,"q") == 0)
441 else if(strcmp(cmd,"x") == 0)
443 else if(strcmp(cmd,"im") == 0)
444 save_image(STRING_LITERAL("fep.image"));
445 else if(strcmp(cmd,"data") == 0)
446 dump_objects(TYPE_COUNT);
447 else if(strcmp(cmd,"refs") == 0)
449 cell addr = read_cell_hex();
450 std::cout << "Data heap references:\n";
451 find_data_references(addr);
452 std::cout << std::endl;
454 else if(strcmp(cmd,"words") == 0)
455 dump_objects(WORD_TYPE);
456 else if(strcmp(cmd,"tuples") == 0)
457 dump_objects(TUPLE_TYPE);
458 else if(strcmp(cmd,"push") == 0)
460 cell addr = read_cell_hex();
463 else if(strcmp(cmd,"code") == 0)
466 std::cout << "unknown command\n";
470 void factor_vm::primitive_die()
472 std::cout << "The die word was called by the library. Unless you called it yourself,\n";
473 std::cout << "you have triggered a bug in Factor. Please report.\n";