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