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