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