3 static bool full_output
;
5 void print_chars(F_STRING
* str
)
8 for(i
= 0; i
< string_capacity(str
); i
++)
9 putchar(string_nth(str
,i
));
12 void print_word(F_WORD
* word
, CELL nesting
)
15 if(type_of(word
->vocabulary
) == STRING_TYPE
)
17 print_chars(untag_string(word
->vocabulary
));
21 if(type_of(word
->name
) == STRING_TYPE
)
22 print_chars(untag_string(word
->name
));
25 print_string("#<not a string: ");
26 print_nested_obj(word
->name
,nesting
);
31 void print_factor_string(F_STRING
* str
)
38 void print_array(F_ARRAY
* array
, CELL nesting
)
40 CELL length
= array_capacity(array
);
44 if(length
> 10 && !full_output
)
52 for(i
= 0; i
< length
; i
++)
55 print_nested_obj(array_nth(array
,i
),nesting
);
62 void print_tuple(F_TUPLE
* tuple
, CELL nesting
)
64 F_TUPLE_LAYOUT
*layout
= untag_object(tuple
->layout
);
65 CELL length
= to_fixnum(layout
->size
);
68 print_nested_obj(layout
->class,nesting
);
73 if(length
> 10 && !full_output
)
81 for(i
= 0; i
< length
; i
++)
84 print_nested_obj(tuple_nth(tuple
,i
),nesting
);
91 void print_nested_obj(CELL obj
, F_FIXNUM nesting
)
93 if(nesting
<= 0 && !full_output
)
95 print_string(" ... ");
104 print_fixnum(untag_fixnum_fast(obj
));
107 print_word(untag_word(obj
),nesting
- 1);
110 print_factor_string(untag_string(obj
));
117 print_tuple(untag_object(obj
),nesting
- 1);
122 print_array(untag_object(obj
),nesting
- 1);
127 quot
= untag_object(obj
);
128 print_array(untag_object(quot
->array
),nesting
- 1);
132 print_string("#<type "); print_cell(type_of(obj
)); print_string(" @ "); print_cell_hex(obj
); print_string(">");
137 void print_obj(CELL obj
)
139 print_nested_obj(obj
,10);
142 void print_objects(CELL start
, CELL end
)
144 for(; start
<= end
; start
+= CELLS
)
146 print_obj(get(start
));
151 void print_datastack(void)
153 print_string("==== DATA STACK:\n");
154 print_objects(ds_bot
,ds
);
157 void print_retainstack(void)
159 print_string("==== RETAIN STACK:\n");
160 print_objects(rs_bot
,rs
);
163 void print_stack_frame(F_STACK_FRAME
*frame
)
165 print_obj(frame_executing(frame
));
167 print_obj(frame_scan(frame
));
169 print_cell_hex((CELL
)frame_executing(frame
));
171 print_cell_hex((CELL
)frame
->xt
);
175 void print_callstack(void)
177 print_string("==== CALL STACK:\n");
178 CELL bottom
= (CELL
)stack_chain
->callstack_bottom
;
179 CELL top
= (CELL
)stack_chain
->callstack_top
;
180 iterate_callstack(top
,bottom
,print_stack_frame
);
183 void dump_cell(CELL cell
)
185 print_cell_hex_pad(cell
); print_string(": ");
189 print_cell_hex_pad(cell
); print_string(" tag "); print_cell(TAG(cell
));
197 print_string(" -- F");
198 else if(cell
< TYPE_COUNT
<<TAG_BITS
)
200 print_string(" -- possible header: ");
201 print_cell(cell
>>TAG_BITS
);
203 else if(cell
>= data_heap
->segment
->start
204 && cell
< data_heap
->segment
->end
)
206 CELL header
= get(UNTAG(cell
));
207 CELL type
= header
>>TAG_BITS
;
208 print_string(" -- object; ");
209 if(TAG(header
) == 0 && type
< TYPE_COUNT
)
211 print_string(" type "); print_cell(type
);
214 print_string(" header corrupt");
222 void dump_memory(CELL from
, CELL to
)
226 for(; from
<= to
; from
+= CELLS
)
230 void dump_zone(F_ZONE
*z
)
232 print_string("Start="); print_cell(z
->start
);
233 print_string(", size="); print_cell(z
->size
);
234 print_string(", here="); print_cell(z
->here
- z
->start
); nl();
237 void dump_generations(void)
241 print_string("Nursery: ");
244 for(i
= 1; i
< data_heap
->gen_count
; i
++)
246 print_string("Generation "); print_cell(i
); print_string(": ");
247 dump_zone(&data_heap
->generations
[i
]);
250 for(i
= 0; i
< data_heap
->gen_count
; i
++)
252 print_string("Semispace "); print_cell(i
); print_string(": ");
253 dump_zone(&data_heap
->semispaces
[i
]);
256 print_string("Cards: base=");
257 print_cell((CELL
)data_heap
->cards
);
258 print_string(", size=");
259 print_cell((CELL
)(data_heap
->cards_end
- data_heap
->cards
));
263 void dump_objects(F_FIXNUM type
)
269 while((obj
= next_object()) != F
)
271 if(type
== -1 || type_of(obj
) == type
)
273 print_cell_hex_pad(obj
);
275 print_nested_obj(obj
,2);
287 void find_data_references_step(CELL
*scan
)
289 if(look_for
== *scan
)
291 print_cell_hex_pad(obj
);
293 print_nested_obj(obj
,2);
298 void find_data_references(CELL look_for_
)
300 look_for
= look_for_
;
304 while((obj
= next_object()) != F
)
305 do_slots(UNTAG(obj
),find_data_references_step
);
313 void find_code_references_step(F_COMPILED
*compiled
, CELL code_start
, CELL literals_start
)
316 CELL literal_end
= literals_start
+ compiled
->literals_length
;
318 for(scan
= literals_start
; scan
< literal_end
; scan
+= CELLS
)
320 CELL code_start
= (CELL
)(compiled
+ 1);
321 CELL literal_start
= code_start
+ compiled
->code_length
;
323 CELL obj
= get(literal_start
);
325 if(look_for
== get(scan
))
327 print_cell_hex_pad(obj
);
329 print_nested_obj(obj
,2);
335 void find_code_references(CELL look_for_
)
337 look_for
= look_for_
;
338 iterate_code_heap(find_code_references_step
);
345 print_string("Low level debugger disabled\n");
349 /* open_console(); */
351 print_string("Starting low level debugger...\n");
352 print_string(" Basic commands:\n");
353 print_string("q -- continue executing Factor - NOT SAFE\n");
354 print_string("im -- save image to fep.image\n");
355 print_string("x -- exit Factor\n");
356 print_string(" Advanced commands:\n");
357 print_string("d <addr> <count> -- dump memory\n");
358 print_string("u <addr> -- dump object at tagged <addr>\n");
359 print_string(". <addr> -- print object at tagged <addr>\n");
360 print_string("t -- toggle output trimming\n");
361 print_string("s r -- dump data, retain stacks\n");
362 print_string(".s .r .c -- print data, retain, call stacks\n");
363 print_string("e -- dump environment\n");
364 print_string("g -- dump generations\n");
365 print_string("card <addr> -- print card containing address\n");
366 print_string("addr <card> -- print address containing card\n");
367 print_string("data -- data heap dump\n");
368 print_string("words -- words dump\n");
369 print_string("tuples -- tuples dump\n");
370 print_string("refs <addr> -- find data heap references to object\n");
371 print_string("push <addr> -- push object on data stack - NOT SAFE\n");
372 print_string("code -- code heap dump\n");
374 bool seen_command
= false;
380 print_string("READY\n");
383 if(scanf("%1000s",cmd
) <= 0)
387 /* If we exit with an EOF immediately, then
388 dump stacks. This is useful for builder and
389 other cases where Factor is run with stdin
390 redirected to /dev/null */
403 if(strcmp(cmd
,"d") == 0)
405 CELL addr
= read_cell_hex();
407 CELL count
= read_cell_hex();
408 dump_memory(addr
,addr
+count
);
410 else if(strcmp(cmd
,"u") == 0)
412 CELL addr
= read_cell_hex();
413 CELL count
= object_size(addr
);
414 dump_memory(addr
,addr
+count
);
416 else if(strcmp(cmd
,".") == 0)
418 CELL addr
= read_cell_hex();
422 else if(strcmp(cmd
,"t") == 0)
423 full_output
= !full_output
;
424 else if(strcmp(cmd
,"s") == 0)
425 dump_memory(ds_bot
,ds
);
426 else if(strcmp(cmd
,"r") == 0)
427 dump_memory(rs_bot
,rs
);
428 else if(strcmp(cmd
,".s") == 0)
430 else if(strcmp(cmd
,".r") == 0)
432 else if(strcmp(cmd
,".c") == 0)
434 else if(strcmp(cmd
,"e") == 0)
437 for(i
= 0; i
< USER_ENV
; i
++)
438 dump_cell((CELL
)&userenv
[i
]);
440 else if(strcmp(cmd
,"g") == 0)
442 else if(strcmp(cmd
,"card") == 0)
444 CELL addr
= read_cell_hex();
445 print_cell_hex((CELL
)ADDR_TO_CARD(addr
));
448 else if(strcmp(cmd
,"addr") == 0)
450 CELL card
= read_cell_hex();
451 print_cell_hex((CELL
)CARD_TO_ADDR(card
));
454 else if(strcmp(cmd
,"q") == 0)
456 else if(strcmp(cmd
,"x") == 0)
458 else if(strcmp(cmd
,"im") == 0)
459 save_image(STRING_LITERAL("fep.image"));
460 else if(strcmp(cmd
,"data") == 0)
462 else if(strcmp(cmd
,"refs") == 0)
464 CELL addr
= read_cell_hex();
465 print_string("Data heap references:\n");
466 find_data_references(addr
);
467 print_string("Code heap references:\n");
468 find_code_references(addr
);
471 else if(strcmp(cmd
,"words") == 0)
472 dump_objects(WORD_TYPE
);
473 else if(strcmp(cmd
,"tuples") == 0)
474 dump_objects(TUPLE_TYPE
);
475 else if(strcmp(cmd
,"push") == 0)
477 CELL addr
= read_cell_hex();
480 else if(strcmp(cmd
,"code") == 0)
481 dump_heap(&code_heap
);
483 print_string("unknown command\n");
487 void primitive_die(void)
489 print_string("The die word was called by the library. Unless you called it yourself,\n");
490 print_string("you have triggered a bug in Factor. Please report.\n");