]> gitweb.factorcode.org Git - factor.git/blob - vm/debug.cpp
VM: simplified fixup_gc_workhorse::fixup_data by mering lots of functions only used...
[factor.git] / vm / debug.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 std::ostream& operator<<(std::ostream& out, const string* str) {
6   for (cell i = 0; i < string_capacity(str); i++)
7     out << (char)str->data()[i];
8   return out;
9 }
10
11 void factor_vm::print_word(word* word, cell nesting) {
12   if (tagged<object>(word->vocabulary).type_p(STRING_TYPE))
13     std::cout << untag<string>(word->vocabulary) << ":";
14
15   if (tagged<object>(word->name).type_p(STRING_TYPE))
16     std::cout << untag<string>(word->name);
17   else {
18     std::cout << "#<not a string: ";
19     print_nested_obj(word->name, nesting);
20     std::cout << ">";
21   }
22 }
23
24 void factor_vm::print_factor_string(string* str) {
25   std::cout << '"' << str << '"';
26 }
27
28 void factor_vm::print_array(array* array, cell nesting) {
29   cell length = array_capacity(array);
30   cell i;
31   bool trimmed;
32
33   if (length > 10 && !full_output) {
34     trimmed = true;
35     length = 10;
36   } else
37     trimmed = false;
38
39   for (i = 0; i < length; i++) {
40     std::cout << " ";
41     print_nested_obj(array_nth(array, i), nesting);
42   }
43
44   if (trimmed)
45     std::cout << "...";
46 }
47
48 void factor_vm::print_alien(alien* alien, cell nesting) {
49   if (to_boolean(alien->expired))
50     std::cout << "#<expired alien>";
51   else if (to_boolean(alien->base)) {
52     std::cout << "#<displaced alien " << alien->displacement << "+";
53     print_nested_obj(alien->base, nesting);
54     std::cout << ">";
55   } else {
56     std::cout << "#<alien " << (void*)alien->address << ">";
57   }
58 }
59
60 void factor_vm::print_byte_array(byte_array* array, cell nesting) {
61   cell length = array->capacity;
62   cell i;
63   bool trimmed;
64   unsigned char* data = array->data<unsigned char>();
65
66   if (length > 16 && !full_output) {
67     trimmed = true;
68     length = 16;
69   } else
70     trimmed = false;
71
72   for (i = 0; i < length; i++) {
73     std::cout << " " << (unsigned) data[i];
74   }
75
76   if (trimmed)
77     std::cout << "...";
78 }
79
80 void factor_vm::print_tuple(tuple* tuple, cell nesting) {
81   tuple_layout* layout = untag<tuple_layout>(tuple->layout);
82   cell length = to_cell(layout->size);
83
84   std::cout << " ";
85   print_nested_obj(layout->klass, nesting);
86
87   bool trimmed;
88   if (length > 10 && !full_output) {
89     trimmed = true;
90     length = 10;
91   } else
92     trimmed = false;
93
94   for (cell i = 0; i < length; i++) {
95     std::cout << " ";
96     print_nested_obj(tuple->data()[i], nesting);
97   }
98
99   if (trimmed)
100     std::cout << "...";
101 }
102
103 void factor_vm::print_nested_obj(cell obj, fixnum nesting) {
104   if (nesting <= 0 && !full_output) {
105     std::cout << " ... ";
106     return;
107   }
108
109   quotation* quot;
110
111   switch (tagged<object>(obj).type()) {
112     case FIXNUM_TYPE:
113       std::cout << untag_fixnum(obj);
114       break;
115     case FLOAT_TYPE:
116       std::cout << untag_float(obj);
117       break;
118     case WORD_TYPE:
119       print_word(untag<word>(obj), nesting - 1);
120       break;
121     case STRING_TYPE:
122       print_factor_string(untag<string>(obj));
123       break;
124     case F_TYPE:
125       std::cout << "f";
126       break;
127     case TUPLE_TYPE:
128       std::cout << "T{";
129       print_tuple(untag<tuple>(obj), nesting - 1);
130       std::cout << " }";
131       break;
132     case WRAPPER_TYPE:
133       std::cout << "W{ ";
134       print_nested_obj(untag<wrapper>(obj)->object, nesting - 1);
135       std::cout << " }";
136       break;
137     case BYTE_ARRAY_TYPE:
138       std::cout << "B{";
139       print_byte_array(untag<byte_array>(obj), nesting - 1);
140       std::cout << " }";
141       break;
142     case ARRAY_TYPE:
143       std::cout << "{";
144       print_array(untag<array>(obj), nesting - 1);
145       std::cout << " }";
146       break;
147     case QUOTATION_TYPE:
148       std::cout << "[";
149       quot = untag<quotation>(obj);
150       print_array(untag<array>(quot->array), nesting - 1);
151       std::cout << " ]";
152       break;
153     case ALIEN_TYPE:
154       print_alien(untag<alien>(obj), nesting - 1);
155       break;
156     default:
157       std::cout << "#<" << type_name(tagged<object>(obj).type()) << " @ ";
158       std::cout << (void*)obj << ">";
159       break;
160   }
161   std::cout << std::flush;
162 }
163
164 void factor_vm::print_obj(cell obj) { print_nested_obj(obj, 10); }
165
166 void factor_vm::print_objects(cell* start, cell* end) {
167   for (; start <= end; start++) {
168     print_obj(*start);
169     std::cout << std::endl;
170   }
171 }
172
173 void factor_vm::print_datastack() {
174   std::cout << "==== DATA STACK:" << std::endl;
175   if (ctx)
176     print_objects((cell*)ctx->datastack_seg->start, (cell*)ctx->datastack);
177   else
178     std::cout << "*** Context not initialized" << std::endl;
179 }
180
181 void factor_vm::print_retainstack() {
182   std::cout << "==== RETAIN STACK:" << std::endl;
183   if (ctx)
184     print_objects((cell*)ctx->retainstack_seg->start, (cell*)ctx->retainstack);
185   else
186     std::cout << "*** Context not initialized" << std::endl;
187 }
188
189 struct stack_frame_printer {
190   factor_vm* parent;
191
192   explicit stack_frame_printer(factor_vm* parent) : parent(parent) {}
193   void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
194     std::cout << std::endl;
195     std::cout << "frame: " << (void*)frame_top << " size " << size << std::endl;
196     std::cout << "executing: ";
197     parent->print_obj(owner->owner);
198     std::cout << std::endl;
199     std::cout << "scan: ";
200     parent->print_obj(owner->scan(parent, addr));
201     std::cout << std::endl;
202     std::cout << "word/quot addr: ";
203     std::cout << std::hex << owner->owner << std::dec;
204     std::cout << std::endl;
205     std::cout << "word/quot xt: ";
206     std::cout << std::hex << owner->entry_point() << std::dec;
207     std::cout << std::endl;
208     std::cout << "return address: ";
209     std::cout << std::hex << addr << std::dec;
210     std::cout << std::endl;
211   }
212 };
213
214 void factor_vm::print_callstack() {
215   std::cout << "==== CALL STACK:" << std::endl;
216   if (ctx) {
217     stack_frame_printer printer(this);
218     iterate_callstack(ctx, printer);
219   } else
220     std::cout << "*** Context not initialized" << std::endl;
221 }
222
223 void factor_vm::print_callstack_object(callstack* obj) {
224   stack_frame_printer printer(this);
225   iterate_callstack_object(obj, printer);
226 }
227
228 struct padded_address {
229   cell value;
230
231   explicit padded_address(cell value) : value(value) {}
232 };
233
234 std::ostream& operator<<(std::ostream& out, const padded_address& value) {
235   char prev = out.fill('0');
236   out.width(sizeof(cell) * 2);
237   out << std::hex << value.value << std::dec;
238   out.fill(prev);
239   return out;
240 }
241
242 void factor_vm::dump_cell(cell x) {
243   std::cout << padded_address(x) << ": ";
244   x = *(cell*)x;
245   std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
246 }
247
248 void factor_vm::dump_memory(cell from, cell to) {
249   from = UNTAG(from);
250
251   for (; from <= to; from += sizeof(cell))
252     dump_cell(from);
253 }
254
255 template <typename Generation>
256 void factor_vm::dump_generation(const char* name, Generation* gen) {
257   std::cout << name << ": ";
258   std::cout << "Start=" << gen->start;
259   std::cout << ", size=" << gen->size;
260   std::cout << ", end=" << gen->end;
261   std::cout << std::endl;
262 }
263
264 void factor_vm::dump_generations() {
265   std::cout << std::hex;
266
267   dump_generation("Nursery", &nursery);
268   dump_generation("Aging", data->aging);
269   dump_generation("Tenured", data->tenured);
270
271   std::cout << "Cards:";
272   std::cout << "base=" << (cell)data->cards << ", ";
273   std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
274
275   std::cout << std::dec;
276 }
277
278 struct object_dumper {
279   factor_vm* parent;
280   cell type;
281
282   object_dumper(factor_vm* parent, cell type)
283       : parent(parent), type(type) {}
284
285   void operator()(object* obj) {
286     if (type == TYPE_COUNT || obj->type() == type) {
287       std::cout << padded_address((cell)obj) << " ";
288       parent->print_nested_obj(tag_dynamic(obj), 2);
289       std::cout << std::endl;
290     }
291   }
292 };
293
294 void factor_vm::dump_objects(cell type) {
295   primitive_full_gc();
296   object_dumper dumper(this, type);
297   each_object(dumper);
298 }
299
300 struct find_data_reference_slot_visitor {
301   cell look_for;
302   object* obj;
303   factor_vm* parent;
304
305   find_data_reference_slot_visitor(cell look_for, object* obj,
306                                    factor_vm* parent)
307       : look_for(look_for), obj(obj), parent(parent) {}
308
309   void operator()(cell* scan) {
310     if (look_for == *scan) {
311       std::cout << padded_address((cell)obj) << " ";
312       parent->print_nested_obj(tag_dynamic(obj), 2);
313       std::cout << std::endl;
314     }
315   }
316 };
317
318 struct dump_edges_slot_visitor {
319   object* obj;
320   factor_vm* parent;
321
322   dump_edges_slot_visitor(cell, object* obj, factor_vm* parent)
323       : obj(obj), parent(parent) {}
324
325   void operator()(cell* scan) {
326     if (TAG(*scan) > F_TYPE)
327       std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan
328                 << std::endl;
329   }
330 };
331
332 template <typename SlotVisitor> struct data_reference_object_visitor {
333   cell look_for;
334   factor_vm* parent;
335
336   data_reference_object_visitor(cell look_for, factor_vm* parent)
337       : look_for(look_for), parent(parent) {}
338
339   void operator()(object* obj) {
340     SlotVisitor visitor(look_for, obj, parent);
341     obj->each_slot(visitor);
342   }
343 };
344
345 void factor_vm::find_data_references(cell look_for) {
346   data_reference_object_visitor<find_data_reference_slot_visitor> visitor(
347       look_for, this);
348   each_object(visitor);
349 }
350
351 void factor_vm::dump_edges() {
352   data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this);
353   each_object(visitor);
354 }
355
356 struct code_block_printer {
357   factor_vm* parent;
358   cell reloc_size, parameter_size;
359
360   explicit code_block_printer(factor_vm* parent)
361       : parent(parent), reloc_size(0), parameter_size(0) {}
362
363   void operator()(code_block* scan, cell size) {
364     const char* status;
365     if (scan->free_p())
366       status = "free";
367     else {
368       reloc_size += parent->object_size(scan->relocation);
369       parameter_size += parent->object_size(scan->parameters);
370
371       if (parent->code->allocator->state.marked_p((cell)scan))
372         status = "marked";
373       else
374         status = "allocated";
375
376       std::cout << std::hex << (cell)scan << std::dec << " ";
377       std::cout << std::hex << size << std::dec << " ";
378       std::cout << status << " ";
379       std::cout << "stack frame " << scan->stack_frame_size();
380       std::cout << std::endl;
381     }
382   }
383 };
384
385 /* Dump all code blocks for debugging */
386 void factor_vm::dump_code_heap() {
387   code_block_printer printer(this);
388   code->allocator->iterate(printer);
389   std::cout << printer.reloc_size << " bytes used by relocation tables"
390             << std::endl;
391   std::cout << printer.parameter_size << " bytes used by parameter tables"
392             << std::endl;
393 }
394
395 void factor_vm::factorbug_usage(bool advanced_p) {
396   std::cout << "Basic commands:" << std::endl;
397 #ifdef WINDOWS
398   std::cout << "  q ^Z             -- quit Factor" << std::endl;
399 #else
400   std::cout << "  q ^D             -- quit Factor" << std::endl;
401 #endif
402   std::cout << "  c                -- continue executing Factor - NOT SAFE"
403             << std::endl;
404   std::cout << "  t                -- throw exception in Factor - NOT SAFE"
405             << std::endl;
406   std::cout << "  .s .r .c         -- print data, retain, call stacks"
407             << std::endl;
408   if (advanced_p) {
409     std::cout << "  help             -- reprint this message" << std::endl;
410     std::cout << "Advanced commands:" << std::endl;
411     std::cout << "  e                -- dump environment" << std::endl;
412     std::cout << "  d <addr> <count> -- dump memory" << std::endl;
413     std::cout << "  u <addr>         -- dump object at tagged <addr>"
414               << std::endl;
415     std::cout << "  . <addr>         -- print object at tagged <addr>"
416               << std::endl;
417     std::cout << "  g                -- dump generations" << std::endl;
418     std::cout << "  ds dr            -- dump data, retain stacks" << std::endl;
419     std::cout << "  trim             -- toggle output trimming" << std::endl;
420     std::cout << "  data             -- data heap dump" << std::endl;
421     std::cout << "  words            -- words dump" << std::endl;
422     std::cout << "  tuples           -- tuples dump" << std::endl;
423     std::cout << "  edges            -- print all object-to-object references"
424               << std::endl;
425     std::cout << "  refs <addr>      -- find data heap references to object"
426               << std::endl;
427     std::cout << "  push <addr>      -- push object on data stack - NOT SAFE"
428               << std::endl;
429     std::cout << "  gc               -- trigger full GC - NOT SAFE"
430               << std::endl;
431     std::cout << "  compact-gc       -- trigger compacting GC - NOT SAFE"
432               << std::endl;
433     std::cout << "  code             -- code heap dump" << std::endl;
434     std::cout << "  abort            -- call abort()" << std::endl;
435     std::cout << "  breakpoint       -- trigger system breakpoint" << std::endl;
436   } else {
437     std::cout << "  help             -- full help, including advanced commands"
438               << std::endl;
439   }
440
441   std::cout << std::endl;
442
443 }
444
445 static void exit_fep(factor_vm* vm) {
446   vm->unlock_console();
447   vm->handle_ctrl_c();
448   vm->fep_p = false;
449 }
450
451 void factor_vm::factorbug() {
452   if (fep_disabled) {
453     std::cout << "Low level debugger disabled" << std::endl;
454     exit(1);
455   }
456
457   if (sampling_profiler_p)
458     end_sampling_profiler();
459
460   fep_p = true;
461
462   std::cout << "Starting low level debugger..." << std::endl;
463
464   // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
465   // that pumps the console is still running concurrently. We lock a mutex so
466   // the thread will take a break and give us exclusive access to stdin.
467   lock_console();
468   ignore_ctrl_c();
469
470   if (!fep_help_was_shown) {
471     factorbug_usage(false);
472     fep_help_was_shown = true;
473   }
474   bool seen_command = false;
475
476   for (;;) {
477     std::string cmd;
478
479     std::cout << "> " << std::flush;
480
481     std::cin >> std::setw(1024) >> cmd >> std::setw(0);
482     if (!std::cin.good()) {
483       if (!seen_command) {
484         /* If we exit with an EOF immediately, then
485            dump stacks. This is useful for builder and
486            other cases where Factor is run with stdin
487            redirected to /dev/null */
488         fep_disabled = true;
489
490         print_datastack();
491         print_retainstack();
492         print_callstack();
493       }
494
495       exit(1);
496     }
497
498     seen_command = true;
499
500     if (cmd == "q")
501       exit(1);
502     if (cmd == "d") {
503       cell addr = read_cell_hex();
504       if (std::cin.peek() == ' ')
505         std::cin.ignore();
506
507       if (!std::cin.good())
508         break;
509       cell count = read_cell_hex();
510       dump_memory(addr, addr + count);
511     } else if (cmd == "u") {
512       cell addr = read_cell_hex();
513       cell count = object_size(addr);
514       dump_memory(addr, addr + count);
515     } else if (cmd == ".") {
516       cell addr = read_cell_hex();
517       print_obj(addr);
518       std::cout << std::endl;
519     } else if (cmd == "trim")
520       full_output = !full_output;
521     else if (cmd == "ds")
522       dump_memory(ctx->datastack_seg->start, ctx->datastack);
523     else if (cmd == "dr")
524       dump_memory(ctx->retainstack_seg->start, ctx->retainstack);
525     else if (cmd == ".s")
526       print_datastack();
527     else if (cmd == ".r")
528       print_retainstack();
529     else if (cmd == ".c")
530       print_callstack();
531     else if (cmd == "e") {
532       for (cell i = 0; i < special_object_count; i++)
533         dump_cell((cell)&special_objects[i]);
534     } else if (cmd == "g")
535       dump_generations();
536     else if (cmd == "c") {
537       exit_fep(this);
538       return;
539     } else if (cmd == "t") {
540       exit_fep(this);
541       general_error(ERROR_INTERRUPT, false_object, false_object);
542       FACTOR_ASSERT(false);
543     } else if (cmd == "data")
544       dump_objects(TYPE_COUNT);
545     else if (cmd == "edges")
546       dump_edges();
547     else if (cmd == "refs") {
548       cell addr = read_cell_hex();
549       std::cout << "Data heap references:" << std::endl;
550       find_data_references(addr);
551       std::cout << std::endl;
552     } else if (cmd == "words")
553       dump_objects(WORD_TYPE);
554     else if (cmd == "tuples")
555       dump_objects(TUPLE_TYPE);
556     else if (cmd == "push") {
557       cell addr = read_cell_hex();
558       ctx->push(addr);
559     } else if (cmd == "code")
560       dump_code_heap();
561     else if (cmd == "compact-gc")
562       primitive_compact_gc();
563     else if (cmd == "gc")
564       primitive_full_gc();
565     else if (cmd == "compact-gc")
566       primitive_compact_gc();
567     else if (cmd == "help")
568       factorbug_usage(true);
569     else if (cmd == "abort")
570       abort();
571     else if (cmd == "breakpoint")
572       breakpoint();
573     else
574       std::cout << "unknown command" << std::endl;
575   }
576 }
577
578 void factor_vm::primitive_die() {
579   std::cout << "The die word was called by the library. Unless you called it "
580                "yourself," << std::endl;
581   std::cout << "you have triggered a bug in Factor. Please report."
582             << std::endl;
583   factorbug();
584 }
585
586 }