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