]> gitweb.factorcode.org Git - factor.git/blob - vm/debug.cpp
Lower the stack usage of a few functions
[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_fixnum(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()(void* frame_top, cell frame_size, code_block* owner,
194                   void* addr) {
195     std::cout << std::endl;
196     std::cout << "frame: " << frame_top << " size " << frame_size << std::endl;
197     std::cout << "executing: ";
198     parent->print_obj(owner->owner);
199     std::cout << std::endl;
200     std::cout << "scan: ";
201     parent->print_obj(owner->scan(parent, addr));
202     std::cout << std::endl;
203     std::cout << "word/quot addr: ";
204     std::cout << std::hex << (cell)owner->owner << std::dec;
205     std::cout << std::endl;
206     std::cout << "word/quot xt: ";
207     std::cout << std::hex << (cell)owner->entry_point() << std::dec;
208     std::cout << std::endl;
209     std::cout << "return address: ";
210     std::cout << std::hex << (cell)addr << std::dec;
211     std::cout << std::endl;
212   }
213 };
214
215 void factor_vm::print_callstack() {
216   std::cout << "==== CALL STACK:" << std::endl;
217   if (ctx) {
218     stack_frame_printer printer(this);
219     iterate_callstack(ctx, printer);
220   } else
221     std::cout << "*** Context not initialized" << std::endl;
222 }
223
224 void factor_vm::print_callstack_object(callstack* obj) {
225   stack_frame_printer printer(this);
226   iterate_callstack_object(obj, printer);
227 }
228
229 struct padded_address {
230   cell value;
231
232   explicit padded_address(cell value) : value(value) {}
233 };
234
235 std::ostream& operator<<(std::ostream& out, const padded_address& value) {
236   char prev = out.fill('0');
237   out.width(sizeof(cell) * 2);
238   out << std::hex << value.value << std::dec;
239   out.fill(prev);
240   return out;
241 }
242
243 void factor_vm::dump_cell(cell x) {
244   std::cout << padded_address(x) << ": ";
245   x = *(cell*)x;
246   std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
247 }
248
249 void factor_vm::dump_memory(cell from, cell to) {
250   from = UNTAG(from);
251
252   for (; from <= to; from += sizeof(cell))
253     dump_cell(from);
254 }
255
256 template <typename Generation>
257 void factor_vm::dump_generation(const char* name, Generation* gen) {
258   std::cout << name << ": ";
259   std::cout << "Start=" << gen->start;
260   std::cout << ", size=" << gen->size;
261   std::cout << ", end=" << gen->end;
262   std::cout << std::endl;
263 }
264
265 void factor_vm::dump_generations() {
266   std::cout << std::hex;
267
268   dump_generation("Nursery", &nursery);
269   dump_generation("Aging", data->aging);
270   dump_generation("Tenured", data->tenured);
271
272   std::cout << "Cards:";
273   std::cout << "base=" << (cell)data->cards << ", ";
274   std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
275
276   std::cout << std::dec;
277 }
278
279 struct object_dumper {
280   factor_vm* parent;
281   cell type;
282
283   object_dumper(factor_vm* parent, cell type)
284       : parent(parent), type(type) {}
285
286   void operator()(object* obj) {
287     if (type == TYPE_COUNT || obj->type() == type) {
288       std::cout << padded_address((cell)obj) << " ";
289       parent->print_nested_obj(tag_dynamic(obj), 2);
290       std::cout << std::endl;
291     }
292   }
293 };
294
295 void factor_vm::dump_objects(cell type) {
296   primitive_full_gc();
297   object_dumper dumper(this, type);
298   each_object(dumper);
299 }
300
301 struct find_data_reference_slot_visitor {
302   cell look_for;
303   object* obj;
304   factor_vm* parent;
305
306   find_data_reference_slot_visitor(cell look_for, object* obj,
307                                    factor_vm* parent)
308       : look_for(look_for), obj(obj), parent(parent) {}
309
310   void operator()(cell* scan) {
311     if (look_for == *scan) {
312       std::cout << padded_address((cell)obj) << " ";
313       parent->print_nested_obj(tag_dynamic(obj), 2);
314       std::cout << std::endl;
315     }
316   }
317 };
318
319 struct dump_edges_slot_visitor {
320   object* obj;
321   factor_vm* parent;
322
323   dump_edges_slot_visitor(cell, object* obj, factor_vm* parent)
324       : obj(obj), parent(parent) {}
325
326   void operator()(cell* scan) {
327     if (TAG(*scan) > F_TYPE)
328       std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan
329                 << std::endl;
330   }
331 };
332
333 template <typename SlotVisitor> struct data_reference_object_visitor {
334   cell look_for;
335   factor_vm* parent;
336
337   data_reference_object_visitor(cell look_for, factor_vm* parent)
338       : look_for(look_for), parent(parent) {}
339
340   void operator()(object* obj) {
341     SlotVisitor visitor(look_for, obj, parent);
342     obj->each_slot(visitor);
343   }
344 };
345
346 void factor_vm::find_data_references(cell look_for) {
347   data_reference_object_visitor<find_data_reference_slot_visitor> visitor(
348       look_for, this);
349   each_object(visitor);
350 }
351
352 void factor_vm::dump_edges() {
353   data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this);
354   each_object(visitor);
355 }
356
357 struct code_block_printer {
358   factor_vm* parent;
359   cell reloc_size, parameter_size;
360
361   explicit code_block_printer(factor_vm* parent)
362       : parent(parent), reloc_size(0), parameter_size(0) {}
363
364   void operator()(code_block* scan, cell size) {
365     const char* status;
366     if (scan->free_p())
367       status = "free";
368     else {
369       reloc_size += parent->object_size(scan->relocation);
370       parameter_size += parent->object_size(scan->parameters);
371
372       if (parent->code->marked_p(scan))
373         status = "marked";
374       else
375         status = "allocated";
376
377       std::cout << std::hex << (cell)scan << std::dec << " ";
378       std::cout << std::hex << size << std::dec << " ";
379       std::cout << status << " ";
380       std::cout << "stack frame " << scan->stack_frame_size();
381       std::cout << std::endl;
382     }
383   }
384 };
385
386 /* Dump all code blocks for debugging */
387 void factor_vm::dump_code_heap() {
388   code_block_printer printer(this);
389   code->allocator->iterate(printer);
390   std::cout << printer.reloc_size << " bytes used by relocation tables"
391             << std::endl;
392   std::cout << printer.parameter_size << " bytes used by parameter tables"
393             << std::endl;
394 }
395
396 void factor_vm::factorbug_usage(bool advanced_p) {
397   std::cout << "Basic commands:" << std::endl;
398 #ifdef WINDOWS
399   std::cout << "  q ^Z             -- quit Factor" << std::endl;
400 #else
401   std::cout << "  q ^D             -- quit Factor" << std::endl;
402 #endif
403   std::cout << "  c                -- continue executing Factor - NOT SAFE"
404             << std::endl;
405   std::cout << "  t                -- throw exception in Factor - NOT SAFE"
406             << std::endl;
407   std::cout << "  .s .r .c         -- print data, retain, call stacks"
408             << std::endl;
409   if (advanced_p) {
410     std::cout << "  help             -- reprint this message" << std::endl;
411     std::cout << "Advanced commands:" << std::endl;
412     std::cout << "  e                -- dump environment" << std::endl;
413     std::cout << "  d <addr> <count> -- dump memory" << std::endl;
414     std::cout << "  u <addr>         -- dump object at tagged <addr>"
415               << std::endl;
416     std::cout << "  . <addr>         -- print object at tagged <addr>"
417               << std::endl;
418     std::cout << "  g                -- dump generations" << std::endl;
419     std::cout << "  ds dr            -- dump data, retain stacks" << std::endl;
420     std::cout << "  trim             -- toggle output trimming" << std::endl;
421     std::cout << "  data             -- data heap dump" << std::endl;
422     std::cout << "  words            -- words dump" << std::endl;
423     std::cout << "  tuples           -- tuples dump" << std::endl;
424     std::cout << "  edges            -- print all object-to-object references"
425               << std::endl;
426     std::cout << "  refs <addr>      -- find data heap references to object"
427               << std::endl;
428     std::cout << "  push <addr>      -- push object on data stack - NOT SAFE"
429               << std::endl;
430     std::cout << "  gc               -- trigger full GC - NOT SAFE"
431               << std::endl;
432     std::cout << "  compact-gc       -- trigger compacting GC - NOT SAFE"
433               << std::endl;
434     std::cout << "  code             -- code heap dump" << std::endl;
435     std::cout << "  abort            -- call abort()" << std::endl;
436     std::cout << "  breakpoint       -- trigger system breakpoint" << std::endl;
437   } else {
438     std::cout << "  help             -- full help, including advanced commands"
439               << std::endl;
440   }
441
442   std::cout << std::endl;
443
444 }
445
446 static void exit_fep(factor_vm* vm) {
447   vm->unlock_console();
448   vm->handle_ctrl_c();
449   vm->fep_p = false;
450 }
451
452 void factor_vm::factorbug() {
453   if (fep_disabled) {
454     std::cout << "Low level debugger disabled" << std::endl;
455     exit(1);
456   }
457
458   if (sampling_profiler_p)
459     end_sampling_profiler();
460
461   fep_p = true;
462
463   std::cout << "Starting low level debugger..." << std::endl;
464
465   // Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
466   // that pumps the console is still running concurrently. We lock a mutex so
467   // the thread will take a break and give us exclusive access to stdin.
468   lock_console();
469   ignore_ctrl_c();
470
471   if (!fep_help_was_shown) {
472     factorbug_usage(false);
473     fep_help_was_shown = true;
474   }
475   bool seen_command = false;
476
477   for (;;) {
478     std::string cmd;
479
480     std::cout << "> " << std::flush;
481
482     std::cin >> std::setw(1024) >> cmd >> std::setw(0);
483     if (!std::cin.good()) {
484       if (!seen_command) {
485         /* If we exit with an EOF immediately, then
486            dump stacks. This is useful for builder and
487            other cases where Factor is run with stdin
488            redirected to /dev/null */
489         fep_disabled = true;
490
491         print_datastack();
492         print_retainstack();
493         print_callstack();
494       }
495
496       exit(1);
497     }
498
499     seen_command = true;
500
501     if (cmd == "q")
502       exit(1);
503     if (cmd == "d") {
504       cell addr = read_cell_hex();
505       if (std::cin.peek() == ' ')
506         std::cin.ignore();
507
508       if (!std::cin.good())
509         break;
510       cell count = read_cell_hex();
511       dump_memory(addr, addr + count);
512     } else if (cmd == "u") {
513       cell addr = read_cell_hex();
514       cell count = object_size(addr);
515       dump_memory(addr, addr + count);
516     } else if (cmd == ".") {
517       cell addr = read_cell_hex();
518       print_obj(addr);
519       std::cout << std::endl;
520     } else if (cmd == "trim")
521       full_output = !full_output;
522     else if (cmd == "ds")
523       dump_memory(ctx->datastack_seg->start, ctx->datastack);
524     else if (cmd == "dr")
525       dump_memory(ctx->retainstack_seg->start, ctx->retainstack);
526     else if (cmd == ".s")
527       print_datastack();
528     else if (cmd == ".r")
529       print_retainstack();
530     else if (cmd == ".c")
531       print_callstack();
532     else if (cmd == "e") {
533       for (cell i = 0; i < special_object_count; i++)
534         dump_cell((cell)&special_objects[i]);
535     } else if (cmd == "g")
536       dump_generations();
537     else if (cmd == "c") {
538       exit_fep(this);
539       return;
540     } else if (cmd == "t") {
541       exit_fep(this);
542       general_error(ERROR_INTERRUPT, false_object, false_object);
543       FACTOR_ASSERT(false);
544     } else if (cmd == "data")
545       dump_objects(TYPE_COUNT);
546     else if (cmd == "edges")
547       dump_edges();
548     else if (cmd == "refs") {
549       cell addr = read_cell_hex();
550       std::cout << "Data heap references:" << std::endl;
551       find_data_references(addr);
552       std::cout << std::endl;
553     } else if (cmd == "words")
554       dump_objects(WORD_TYPE);
555     else if (cmd == "tuples")
556       dump_objects(TUPLE_TYPE);
557     else if (cmd == "push") {
558       cell addr = read_cell_hex();
559       ctx->push(addr);
560     } else if (cmd == "code")
561       dump_code_heap();
562     else if (cmd == "compact-gc")
563       primitive_compact_gc();
564     else if (cmd == "gc")
565       primitive_full_gc();
566     else if (cmd == "compact-gc")
567       primitive_compact_gc();
568     else if (cmd == "help")
569       factorbug_usage(true);
570     else if (cmd == "abort")
571       abort();
572     else if (cmd == "breakpoint")
573       breakpoint();
574     else
575       std::cout << "unknown command" << std::endl;
576   }
577 }
578
579 void factor_vm::primitive_die() {
580   std::cout << "The die word was called by the library. Unless you called it "
581                "yourself," << std::endl;
582   std::cout << "you have triggered a bug in Factor. Please report."
583             << std::endl;
584   factorbug();
585 }
586
587 }