7 void factorvm::print_chars(string* str)
10 for(i = 0; i < string_capacity(str); i++)
11 putchar(string_nth(str,i));
15 void factorvm::print_word(word* word, cell nesting)
17 if(tagged<object>(word->vocabulary).type_p(STRING_TYPE))
19 print_chars(untag<string>(word->vocabulary));
23 if(tagged<object>(word->name).type_p(STRING_TYPE))
24 print_chars(untag<string>(word->name));
27 print_string("#<not a string: ");
28 print_nested_obj(word->name,nesting);
34 void factorvm::print_factor_string(string* str)
42 void factorvm::print_array(array* array, cell nesting)
44 cell length = array_capacity(array);
48 if(length > 10 && !full_output)
56 for(i = 0; i < length; i++)
59 print_nested_obj(array_nth(array,i),nesting);
67 void factorvm::print_tuple(tuple *tuple, cell nesting)
69 tuple_layout *layout = untag<tuple_layout>(tuple->layout);
70 cell length = to_fixnum(layout->size);
73 print_nested_obj(layout->klass,nesting);
78 if(length > 10 && !full_output)
86 for(i = 0; i < length; i++)
89 print_nested_obj(tuple->data()[i],nesting);
97 void factorvm::print_nested_obj(cell obj, fixnum nesting)
99 if(nesting <= 0 && !full_output)
101 print_string(" ... ");
107 switch(tagged<object>(obj).type())
110 print_fixnum(untag_fixnum(obj));
113 print_word(untag<word>(obj),nesting - 1);
116 print_factor_string(untag<string>(obj));
123 print_tuple(untag<tuple>(obj),nesting - 1);
128 print_array(untag<array>(obj),nesting - 1);
133 quot = untag<quotation>(obj);
134 print_array(untag<array>(quot->array),nesting - 1);
138 print_string("#<type ");
139 print_cell(tagged<object>(obj).type());
148 void factorvm::print_obj(cell obj)
150 print_nested_obj(obj,10);
154 void factorvm::print_objects(cell *start, cell *end)
156 for(; start <= end; start++)
164 void factorvm::print_datastack()
166 print_string("==== DATA STACK:\n");
167 print_objects((cell *)ds_bot,(cell *)ds);
171 void factorvm::print_retainstack()
173 print_string("==== RETAIN STACK:\n");
174 print_objects((cell *)rs_bot,(cell *)rs);
178 void factorvm::print_stack_frame(stack_frame *frame)
180 print_obj(frame_executing(frame));
182 print_obj(frame_scan(frame));
184 print_string("word/quot addr: ");
185 print_cell_hex((cell)frame_executing(frame));
187 print_string("word/quot xt: ");
188 print_cell_hex((cell)frame->xt);
190 print_string("return address: ");
191 print_cell_hex((cell)FRAME_RETURN_ADDRESS(frame));
195 void print_stack_frame(stack_frame *frame, factorvm *myvm)
197 return myvm->print_stack_frame(frame);
200 void factorvm::print_callstack()
202 print_string("==== CALL STACK:\n");
203 cell bottom = (cell)stack_chain->callstack_bottom;
204 cell top = (cell)stack_chain->callstack_top;
205 iterate_callstack(top,bottom,factor::print_stack_frame);
209 void factorvm::dump_cell(cell x)
211 print_cell_hex_pad(x); print_string(": ");
213 print_cell_hex_pad(x); print_string(" tag "); print_cell(TAG(x));
218 void factorvm::dump_memory(cell from, cell to)
222 for(; from <= to; from += sizeof(cell))
227 void factorvm::dump_zone(zone *z)
229 print_string("Start="); print_cell(z->start);
230 print_string(", size="); print_cell(z->size);
231 print_string(", here="); print_cell(z->here - z->start); nl();
235 void factorvm::dump_generations()
239 print_string("Nursery: ");
242 for(i = 1; i < data->gen_count; i++)
244 print_string("Generation "); print_cell(i); print_string(": ");
245 dump_zone(&data->generations[i]);
248 for(i = 0; i < data->gen_count; i++)
250 print_string("Semispace "); print_cell(i); print_string(": ");
251 dump_zone(&data->semispaces[i]);
254 print_string("Cards: base=");
255 print_cell((cell)data->cards);
256 print_string(", size=");
257 print_cell((cell)(data->cards_end - data->cards));
262 void factorvm::dump_objects(cell type)
268 while((obj = next_object()) != F)
270 if(type == TYPE_COUNT || tagged<object>(obj).type_p(type))
272 print_cell_hex_pad(obj);
274 print_nested_obj(obj,2);
284 void factorvm::find_data_references_step(cell *scan)
286 if(look_for == *scan)
288 print_cell_hex_pad(obj);
290 print_nested_obj(obj,2);
295 void find_data_references_step(cell *scan,factorvm *myvm)
297 return myvm->find_data_references_step(scan);
300 void factorvm::find_data_references(cell look_for_)
302 look_for = look_for_;
306 while((obj = next_object()) != F)
307 do_slots(UNTAG(obj),factor::find_data_references_step);
313 /* Dump all code blocks for debugging */
314 void factorvm::dump_code_heap()
316 cell reloc_size = 0, literal_size = 0;
318 heap_block *scan = first_block(&code);
329 reloc_size += object_size(((code_block *)scan)->relocation);
330 literal_size += object_size(((code_block *)scan)->literals);
331 status = "allocated";
334 reloc_size += object_size(((code_block *)scan)->relocation);
335 literal_size += object_size(((code_block *)scan)->literals);
343 print_cell_hex((cell)scan); print_string(" ");
344 print_cell_hex(scan->size); print_string(" ");
345 print_string(status); print_string("\n");
347 scan = next_block(&code,scan);
350 print_cell(reloc_size); print_string(" bytes of relocation data\n");
351 print_cell(literal_size); print_string(" bytes of literal data\n");
355 void factorvm::factorbug()
359 print_string("Low level debugger disabled\n");
363 /* open_console(); */
365 print_string("Starting low level debugger...\n");
366 print_string(" Basic commands:\n");
367 print_string("q -- continue executing Factor - NOT SAFE\n");
368 print_string("im -- save image to fep.image\n");
369 print_string("x -- exit Factor\n");
370 print_string(" Advanced commands:\n");
371 print_string("d <addr> <count> -- dump memory\n");
372 print_string("u <addr> -- dump object at tagged <addr>\n");
373 print_string(". <addr> -- print object at tagged <addr>\n");
374 print_string("t -- toggle output trimming\n");
375 print_string("s r -- dump data, retain stacks\n");
376 print_string(".s .r .c -- print data, retain, call stacks\n");
377 print_string("e -- dump environment\n");
378 print_string("g -- dump generations\n");
379 print_string("card <addr> -- print card containing address\n");
380 print_string("addr <card> -- print address containing card\n");
381 print_string("data -- data heap dump\n");
382 print_string("words -- words dump\n");
383 print_string("tuples -- tuples dump\n");
384 print_string("refs <addr> -- find data heap references to object\n");
385 print_string("push <addr> -- push object on data stack - NOT SAFE\n");
386 print_string("code -- code heap dump\n");
388 bool seen_command = false;
394 print_string("READY\n");
397 if(scanf("%1000s",cmd) <= 0)
401 /* If we exit with an EOF immediately, then
402 dump stacks. This is useful for builder and
403 other cases where Factor is run with stdin
404 redirected to /dev/null */
417 if(strcmp(cmd,"d") == 0)
419 cell addr = read_cell_hex();
420 if(scanf(" ") < 0) break;
421 cell count = read_cell_hex();
422 dump_memory(addr,addr+count);
424 else if(strcmp(cmd,"u") == 0)
426 cell addr = read_cell_hex();
427 cell count = object_size(addr);
428 dump_memory(addr,addr+count);
430 else if(strcmp(cmd,".") == 0)
432 cell addr = read_cell_hex();
436 else if(strcmp(cmd,"t") == 0)
437 full_output = !full_output;
438 else if(strcmp(cmd,"s") == 0)
439 dump_memory(ds_bot,ds);
440 else if(strcmp(cmd,"r") == 0)
441 dump_memory(rs_bot,rs);
442 else if(strcmp(cmd,".s") == 0)
444 else if(strcmp(cmd,".r") == 0)
446 else if(strcmp(cmd,".c") == 0)
448 else if(strcmp(cmd,"e") == 0)
451 for(i = 0; i < USER_ENV; i++)
452 dump_cell((cell)&userenv[i]);
454 else if(strcmp(cmd,"g") == 0)
456 else if(strcmp(cmd,"card") == 0)
458 cell addr = read_cell_hex();
459 print_cell_hex((cell)addr_to_card(addr));
462 else if(strcmp(cmd,"addr") == 0)
464 card *ptr = (card *)read_cell_hex();
465 print_cell_hex(card_to_addr(ptr));
468 else if(strcmp(cmd,"q") == 0)
470 else if(strcmp(cmd,"x") == 0)
472 else if(strcmp(cmd,"im") == 0)
473 save_image(STRING_LITERAL("fep.image"));
474 else if(strcmp(cmd,"data") == 0)
475 dump_objects(TYPE_COUNT);
476 else if(strcmp(cmd,"refs") == 0)
478 cell addr = read_cell_hex();
479 print_string("Data heap references:\n");
480 find_data_references(addr);
483 else if(strcmp(cmd,"words") == 0)
484 dump_objects(WORD_TYPE);
485 else if(strcmp(cmd,"tuples") == 0)
486 dump_objects(TUPLE_TYPE);
487 else if(strcmp(cmd,"push") == 0)
489 cell addr = read_cell_hex();
492 else if(strcmp(cmd,"code") == 0)
495 print_string("unknown command\n");
500 inline void factorvm::primitive_die()
502 print_string("The die word was called by the library. Unless you called it yourself,\n");
503 print_string("you have triggered a bug in Factor. Please report.\n");
509 PRIMITIVE_GETVM()->primitive_die();