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