6 static bool fep_disabled;
7 static bool full_output;
9 void print_chars(string* str)
12 for(i = 0; i < string_capacity(str); i++)
13 putchar(string_nth(str,i));
16 void print_word(word* word, cell nesting)
18 if(tagged<object>(word->vocabulary).type_p(STRING_TYPE))
20 print_chars(untag<string>(word->vocabulary));
24 if(tagged<object>(word->name).type_p(STRING_TYPE))
25 print_chars(untag<string>(word->name));
28 print_string("#<not a string: ");
29 print_nested_obj(word->name,nesting);
34 void print_factor_string(string* str)
41 void print_array(array* array, cell nesting)
43 cell length = array_capacity(array);
47 if(length > 10 && !full_output)
55 for(i = 0; i < length; i++)
58 print_nested_obj(array_nth(array,i),nesting);
65 void print_tuple(tuple *tuple, cell nesting)
67 tuple_layout *layout = untag<tuple_layout>(tuple->layout);
68 cell length = to_fixnum(layout->size);
71 print_nested_obj(layout->klass,nesting);
76 if(length > 10 && !full_output)
84 for(i = 0; i < length; i++)
87 print_nested_obj(tuple->data()[i],nesting);
94 void print_nested_obj(cell obj, fixnum nesting)
96 if(nesting <= 0 && !full_output)
98 print_string(" ... ");
104 switch(tagged<object>(obj).type())
107 print_fixnum(untag_fixnum(obj));
110 print_word(untag<word>(obj),nesting - 1);
113 print_factor_string(untag<string>(obj));
120 print_tuple(untag<tuple>(obj),nesting - 1);
125 print_array(untag<array>(obj),nesting - 1);
130 quot = untag<quotation>(obj);
131 print_array(untag<array>(quot->array),nesting - 1);
135 print_string("#<type ");
136 print_cell(tagged<object>(obj).type());
144 void print_obj(cell obj)
146 print_nested_obj(obj,10);
149 void print_objects(cell *start, cell *end)
151 for(; start <= end; start++)
158 void print_datastack()
160 print_string("==== DATA STACK:\n");
161 print_objects((cell *)ds_bot,(cell *)ds);
164 void print_retainstack()
166 print_string("==== RETAIN STACK:\n");
167 print_objects((cell *)rs_bot,(cell *)rs);
170 void print_stack_frame(stack_frame *frame)
172 print_obj(frame_executing(frame));
174 print_obj(frame_scan(frame));
176 print_string("word/quot addr: ");
177 print_cell_hex((cell)frame_executing(frame));
179 print_string("word/quot xt: ");
180 print_cell_hex((cell)frame->xt);
182 print_string("return address: ");
183 print_cell_hex((cell)FRAME_RETURN_ADDRESS(frame));
187 void print_callstack()
189 print_string("==== CALL STACK:\n");
190 cell bottom = (cell)stack_chain->callstack_bottom;
191 cell top = (cell)stack_chain->callstack_top;
192 iterate_callstack(top,bottom,print_stack_frame);
195 void dump_cell(cell x)
197 print_cell_hex_pad(x); print_string(": ");
199 print_cell_hex_pad(x); print_string(" tag "); print_cell(TAG(x));
203 void dump_memory(cell from, cell to)
207 for(; from <= to; from += sizeof(cell))
211 void dump_zone(zone *z)
213 print_string("Start="); print_cell(z->start);
214 print_string(", size="); print_cell(z->size);
215 print_string(", here="); print_cell(z->here - z->start); nl();
218 void dump_generations()
222 print_string("Nursery: ");
225 for(i = 1; i < data->gen_count; i++)
227 print_string("Generation "); print_cell(i); print_string(": ");
228 dump_zone(&data->generations[i]);
231 for(i = 0; i < data->gen_count; i++)
233 print_string("Semispace "); print_cell(i); print_string(": ");
234 dump_zone(&data->semispaces[i]);
237 print_string("Cards: base=");
238 print_cell((cell)data->cards);
239 print_string(", size=");
240 print_cell((cell)(data->cards_end - data->cards));
244 void dump_objects(cell type)
250 while((obj = next_object()) != F)
252 if(type == TYPE_COUNT || tagged<object>(obj).type_p(type))
254 print_cell_hex_pad(obj);
256 print_nested_obj(obj,2);
267 void find_data_references_step(cell *scan)
269 if(look_for == *scan)
271 print_cell_hex_pad(obj);
273 print_nested_obj(obj,2);
278 void find_data_references(cell look_for_)
280 look_for = look_for_;
284 while((obj = next_object()) != F)
285 do_slots(UNTAG(obj),find_data_references_step);
290 /* Dump all code blocks for debugging */
291 void dump_code_heap()
293 cell reloc_size = 0, literal_size = 0;
295 heap_block *scan = first_block(&code);
306 reloc_size += object_size(((code_block *)scan)->relocation);
307 literal_size += object_size(((code_block *)scan)->literals);
308 status = "allocated";
311 reloc_size += object_size(((code_block *)scan)->relocation);
312 literal_size += object_size(((code_block *)scan)->literals);
320 print_cell_hex((cell)scan); print_string(" ");
321 print_cell_hex(scan->size); print_string(" ");
322 print_string(status); print_string("\n");
324 scan = next_block(&code,scan);
327 print_cell(reloc_size); print_string(" bytes of relocation data\n");
328 print_cell(literal_size); print_string(" bytes of literal data\n");
335 print_string("Low level debugger disabled\n");
339 /* open_console(); */
341 print_string("Starting low level debugger...\n");
342 print_string(" Basic commands:\n");
343 print_string("q -- continue executing Factor - NOT SAFE\n");
344 print_string("im -- save image to fep.image\n");
345 print_string("x -- exit Factor\n");
346 print_string(" Advanced commands:\n");
347 print_string("d <addr> <count> -- dump memory\n");
348 print_string("u <addr> -- dump object at tagged <addr>\n");
349 print_string(". <addr> -- print object at tagged <addr>\n");
350 print_string("t -- toggle output trimming\n");
351 print_string("s r -- dump data, retain stacks\n");
352 print_string(".s .r .c -- print data, retain, call stacks\n");
353 print_string("e -- dump environment\n");
354 print_string("g -- dump generations\n");
355 print_string("card <addr> -- print card containing address\n");
356 print_string("addr <card> -- print address containing card\n");
357 print_string("data -- data heap dump\n");
358 print_string("words -- words dump\n");
359 print_string("tuples -- tuples dump\n");
360 print_string("refs <addr> -- find data heap references to object\n");
361 print_string("push <addr> -- push object on data stack - NOT SAFE\n");
362 print_string("code -- code heap dump\n");
364 bool seen_command = false;
370 print_string("READY\n");
373 if(scanf("%1000s",cmd) <= 0)
377 /* If we exit with an EOF immediately, then
378 dump stacks. This is useful for builder and
379 other cases where Factor is run with stdin
380 redirected to /dev/null */
393 if(strcmp(cmd,"d") == 0)
395 cell addr = read_cell_hex();
396 if(scanf(" ") < 0) break;
397 cell count = read_cell_hex();
398 dump_memory(addr,addr+count);
400 else if(strcmp(cmd,"u") == 0)
402 cell addr = read_cell_hex();
403 cell count = object_size(addr);
404 dump_memory(addr,addr+count);
406 else if(strcmp(cmd,".") == 0)
408 cell addr = read_cell_hex();
412 else if(strcmp(cmd,"t") == 0)
413 full_output = !full_output;
414 else if(strcmp(cmd,"s") == 0)
415 dump_memory(ds_bot,ds);
416 else if(strcmp(cmd,"r") == 0)
417 dump_memory(rs_bot,rs);
418 else if(strcmp(cmd,".s") == 0)
420 else if(strcmp(cmd,".r") == 0)
422 else if(strcmp(cmd,".c") == 0)
424 else if(strcmp(cmd,"e") == 0)
427 for(i = 0; i < USER_ENV; i++)
428 dump_cell((cell)&userenv[i]);
430 else if(strcmp(cmd,"g") == 0)
432 else if(strcmp(cmd,"card") == 0)
434 cell addr = read_cell_hex();
435 print_cell_hex((cell)addr_to_card(addr));
438 else if(strcmp(cmd,"addr") == 0)
440 card *ptr = (card *)read_cell_hex();
441 print_cell_hex(card_to_addr(ptr));
444 else if(strcmp(cmd,"q") == 0)
446 else if(strcmp(cmd,"x") == 0)
448 else if(strcmp(cmd,"im") == 0)
449 save_image(STRING_LITERAL("fep.image"));
450 else if(strcmp(cmd,"data") == 0)
451 dump_objects(TYPE_COUNT);
452 else if(strcmp(cmd,"refs") == 0)
454 cell addr = read_cell_hex();
455 print_string("Data heap references:\n");
456 find_data_references(addr);
459 else if(strcmp(cmd,"words") == 0)
460 dump_objects(WORD_TYPE);
461 else if(strcmp(cmd,"tuples") == 0)
462 dump_objects(TUPLE_TYPE);
463 else if(strcmp(cmd,"push") == 0)
465 cell addr = read_cell_hex();
468 else if(strcmp(cmd,"code") == 0)
471 print_string("unknown command\n");
477 print_string("The die word was called by the library. Unless you called it yourself,\n");
478 print_string("you have triggered a bug in Factor. Please report.\n");