]> gitweb.factorcode.org Git - factor.git/blob - vm/debug.cpp
cleanups from code review
[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_tuple(tuple *tuple, cell nesting)
58 {
59         tuple_layout *layout = untag<tuple_layout>(tuple->layout);
60         cell length = to_fixnum(layout->size);
61
62         std::cout << " ";
63         print_nested_obj(layout->klass,nesting);
64
65         bool trimmed;
66         if(length > 10 && !full_output)
67         {
68                 trimmed = true;
69                 length = 10;
70         }
71         else
72                 trimmed = false;
73
74         for(cell i = 0; i < length; i++)
75         {
76                 std::cout << " ";
77                 print_nested_obj(tuple->data()[i],nesting);
78         }
79
80         if(trimmed)
81                 std::cout << "...";
82 }
83
84 void factor_vm::print_nested_obj(cell obj, fixnum nesting)
85 {
86         if(nesting <= 0 && !full_output)
87         {
88                 std::cout << " ... ";
89                 return;
90         }
91
92         quotation *quot;
93
94         switch(tagged<object>(obj).type())
95         {
96         case FIXNUM_TYPE:
97                 std::cout << untag_fixnum(obj);
98                 break;
99         case WORD_TYPE:
100                 print_word(untag<word>(obj),nesting - 1);
101                 break;
102         case STRING_TYPE:
103                 print_factor_string(untag<string>(obj));
104                 break;
105         case F_TYPE:
106                 std::cout << "f";
107                 break;
108         case TUPLE_TYPE:
109                 std::cout << "T{";
110                 print_tuple(untag<tuple>(obj),nesting - 1);
111                 std::cout << " }";
112                 break;
113         case ARRAY_TYPE:
114                 std::cout << "{";
115                 print_array(untag<array>(obj),nesting - 1);
116                 std::cout << " }";
117                 break;
118         case QUOTATION_TYPE:
119                 std::cout << "[";
120                 quot = untag<quotation>(obj);
121                 print_array(untag<array>(quot->array),nesting - 1);
122                 std::cout << " ]";
123                 break;
124         default:
125                 std::cout << "#<type " << tagged<object>(obj).type() << " @ ";
126                 std::cout << std::hex << obj << std::dec << ">";
127                 break;
128         }
129 }
130
131 void factor_vm::print_obj(cell obj)
132 {
133         print_nested_obj(obj,10);
134 }
135
136 void factor_vm::print_objects(cell *start, cell *end)
137 {
138         for(; start <= end; start++)
139         {
140                 print_obj(*start);
141                 std::cout << std::endl;
142         }
143 }
144
145 void factor_vm::print_datastack()
146 {
147         std::cout << "==== DATA STACK:" << std::endl;
148         print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack);
149 }
150
151 void factor_vm::print_retainstack()
152 {
153         std::cout << "==== RETAIN STACK:" << std::endl;
154         print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack);
155 }
156
157 struct stack_frame_printer {
158         factor_vm *parent;
159
160         explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {}
161         void operator()(stack_frame *frame)
162         {
163                 std::cout << "frame: " << std::hex << (cell)frame << std::dec << std::endl;
164                 std::cout << "executing: ";
165                 parent->print_obj(parent->frame_executing(frame));
166                 std::cout << std::endl;
167                 std::cout << "scan: ";
168                 parent->print_obj(parent->frame_scan(frame));
169                 std::cout << std::endl;
170                 std::cout << "word/quot addr: ";
171                 std::cout << std::hex << (cell)parent->frame_executing(frame) << std::dec;
172                 std::cout << std::endl;
173                 std::cout << "word/quot xt: ";
174                 std::cout << std::hex << (cell)frame->entry_point << std::dec;
175                 std::cout << std::endl;
176                 std::cout << "return address: ";
177                 std::cout << std::hex << (cell)FRAME_RETURN_ADDRESS(frame,parent) << std::dec;
178                 std::cout << std::endl;
179         }
180 };
181
182 void factor_vm::print_callstack()
183 {
184         std::cout << "==== CALL STACK:" << std::endl;
185         stack_frame_printer printer(this);
186         iterate_callstack(ctx,printer);
187 }
188
189 struct padded_address {
190         cell value;
191
192         explicit padded_address(cell value_) : value(value_) {}
193 };
194
195 std::ostream &operator<<(std::ostream &out, const padded_address &value)
196 {
197         char prev = out.fill('0');
198         out.width(sizeof(cell) * 2);
199         out << std::hex << value.value << std::dec;
200         out.fill(prev);
201         return out;
202 }
203
204 void factor_vm::dump_cell(cell x)
205 {
206         std::cout << padded_address(x) << ": ";
207         x = *(cell *)x;
208         std::cout << padded_address(x) << " tag " << TAG(x) << std::endl;
209 }
210
211 void factor_vm::dump_memory(cell from, cell to)
212 {
213         from = UNTAG(from);
214
215         for(; from <= to; from += sizeof(cell))
216                 dump_cell(from);
217 }
218
219 template<typename Generation>
220 void factor_vm::dump_generation(const char *name, Generation *gen)
221 {
222         std::cout << name << ": ";
223         std::cout << "Start=" << gen->start;
224         std::cout << ", size=" << gen->size;
225         std::cout << ", end=" << gen->end;
226         std::cout << std::endl;
227 }
228
229 void factor_vm::dump_generations()
230 {
231         std::cout << std::hex;
232
233         dump_generation("Nursery",&nursery);
234         dump_generation("Aging",data->aging);
235         dump_generation("Tenured",data->tenured);
236
237         std::cout << "Cards:";
238         std::cout << "base=" << (cell)data->cards << ", ";
239         std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl;
240
241         std::cout << std::dec;
242 }
243
244 struct object_dumper {
245         factor_vm *parent;
246         cell type;
247
248         explicit object_dumper(factor_vm *parent_, cell type_) :
249                 parent(parent_), type(type_) {}
250
251         void operator()(object *obj)
252         {
253                 if(type == TYPE_COUNT || obj->type() == type)
254                 {
255                         std::cout << padded_address((cell)obj) << " ";
256                         parent->print_nested_obj(tag_dynamic(obj),2);
257                         std::cout << std::endl;
258                 }
259         }
260 };
261
262 void factor_vm::dump_objects(cell type)
263 {
264         primitive_full_gc();
265         object_dumper dumper(this,type);
266         each_object(dumper);
267 }
268
269 struct data_reference_slot_visitor {
270         cell look_for;
271         object *obj;
272         factor_vm *parent;
273
274         explicit data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) :
275                 look_for(look_for_), obj(obj_), parent(parent_) { }
276
277         void operator()(cell *scan)
278         {
279                 if(look_for == *scan)
280                 {
281                         std::cout << padded_address((cell)obj) << " ";
282                         parent->print_nested_obj(tag_dynamic(obj),2);
283                         std::cout << std::endl;
284                 }
285         }
286 };
287
288 struct data_reference_object_visitor {
289         cell look_for;
290         factor_vm *parent;
291
292         explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) :
293                 look_for(look_for_), parent(parent_) {}
294
295         void operator()(object *obj)
296         {
297                 data_reference_slot_visitor visitor(look_for,obj,parent);
298                 obj->each_slot(visitor);
299         }
300 };
301
302 void factor_vm::find_data_references(cell look_for)
303 {
304         data_reference_object_visitor visitor(look_for,this);
305         each_object(visitor);
306 }
307
308 struct code_block_printer {
309         factor_vm *parent;
310         cell reloc_size, parameter_size;
311
312         explicit code_block_printer(factor_vm *parent_) :
313                 parent(parent_), reloc_size(0), parameter_size(0) {}
314
315         void operator()(code_block *scan, cell size)
316         {
317                 const char *status;
318                 if(scan->free_p())
319                         status = "free";
320                 else
321                 {
322                         reloc_size += parent->object_size(scan->relocation);
323                         parameter_size += parent->object_size(scan->parameters);
324
325                         if(parent->code->marked_p(scan))
326                                 status = "marked";
327                         else
328                                 status = "allocated";
329
330                         std::cout << std::hex << (cell)scan << std::dec << " ";
331                         std::cout << std::hex << size << std::dec << " ";
332                         std::cout << status << std::endl;
333                 }
334         }
335 };
336
337 /* Dump all code blocks for debugging */
338 void factor_vm::dump_code_heap()
339 {
340         code_block_printer printer(this);
341         code->allocator->iterate(printer);
342         std::cout << printer.reloc_size << " bytes used by relocation tables" << std::endl;
343         std::cout << printer.parameter_size << " bytes used by parameter tables" << std::endl;
344 }
345
346 void factor_vm::factorbug()
347 {
348         if(fep_disabled)
349         {
350                 std::cout << "Low level debugger disabled" << std::endl;
351                 exit(1);
352         }
353
354         fep_p = true;
355
356         std::cout << "Starting low level debugger..." << std::endl;
357         std::cout << "Basic commands:" << std::endl;
358         std::cout << "  q ^D             -- quit Factor" << std::endl;
359         std::cout << "  c                -- continue executing Factor - NOT SAFE" << std::endl;
360         std::cout << "  t                -- throw exception in Factor - NOT SAFE" << std::endl;
361         std::cout << "  .s .r .c         -- print data, retain, call stacks" << std::endl;
362         std::cout << "Advanced commands:" << std::endl;
363         std::cout << "  e                -- dump environment" << std::endl;
364         std::cout << "  d <addr> <count> -- dump memory" << std::endl;
365         std::cout << "  u <addr>         -- dump object at tagged <addr>" << std::endl;
366         std::cout << "  . <addr>         -- print object at tagged <addr>" << std::endl;
367         std::cout << "  g                -- dump generations" << std::endl;
368         std::cout << "  ds dr            -- dump data, retain stacks" << std::endl;
369         std::cout << "  trim             -- toggle output trimming" << std::endl;
370         std::cout << "  data             -- data heap dump" << std::endl;
371         std::cout << "  words            -- words dump" << std::endl;
372         std::cout << "  tuples           -- tuples dump" << std::endl;
373         std::cout << "  refs <addr>      -- find data heap references to object" << std::endl;
374         std::cout << "  push <addr>      -- push object on data stack - NOT SAFE" << std::endl;
375         std::cout << "  gc               -- trigger full GC - NOT SAFE" << std::endl;
376         std::cout << "  code             -- code heap dump" << std::endl;
377
378         bool seen_command = false;
379
380         for(;;)
381         {
382                 char cmd[1024];
383
384                 std::cout << "READY" << std::endl;
385                 std::cout.flush();
386
387                 std::cin >> std::setw(1024) >> cmd >> std::setw(0); 
388                 if(!std::cin.good())
389                 {
390                         if(!seen_command)
391                         {
392                                 /* If we exit with an EOF immediately, then
393                                 dump stacks. This is useful for builder and
394                                 other cases where Factor is run with stdin
395                                 redirected to /dev/null */
396                                 fep_disabled = true;
397
398                                 print_datastack();
399                                 print_retainstack();
400                                 print_callstack();
401                         }
402
403                         exit(1);
404                 }
405
406                 seen_command = true;
407
408                 if(strcmp(cmd,"q") == 0)
409                         exit(1);
410                 if(strcmp(cmd,"d") == 0)
411                 {
412                         cell addr = read_cell_hex();
413                         if (std::cin.peek() == ' ')
414                                 std::cin.ignore();
415
416                         if(!std::cin.good()) break;
417                         cell count = read_cell_hex();
418                         dump_memory(addr,addr+count);
419                 }
420                 else if(strcmp(cmd,"u") == 0)
421                 {
422                         cell addr = read_cell_hex();
423                         cell count = object_size(addr);
424                         dump_memory(addr,addr+count);
425                 }
426                 else if(strcmp(cmd,".") == 0)
427                 {
428                         cell addr = read_cell_hex();
429                         print_obj(addr);
430                         std::cout << std::endl;
431                 }
432                 else if(strcmp(cmd,"trim") == 0)
433                         full_output = !full_output;
434                 else if(strcmp(cmd,"ds") == 0)
435                         dump_memory(ctx->datastack_seg->start,ctx->datastack);
436                 else if(strcmp(cmd,"dr") == 0)
437                         dump_memory(ctx->retainstack_seg->start,ctx->retainstack);
438                 else if(strcmp(cmd,".s") == 0)
439                         print_datastack();
440                 else if(strcmp(cmd,".r") == 0)
441                         print_retainstack();
442                 else if(strcmp(cmd,".c") == 0)
443                         print_callstack();
444                 else if(strcmp(cmd,"e") == 0)
445                 {
446                         for(cell i = 0; i < special_object_count; i++)
447                                 dump_cell((cell)&special_objects[i]);
448                 }
449                 else if(strcmp(cmd,"g") == 0)
450                         dump_generations();
451                 else if(strcmp(cmd,"c") == 0)
452                 {
453                         fep_p = false;
454                         return;
455                 }
456                 else if(strcmp(cmd,"t") == 0)
457                 {
458                         fep_p = false;
459                         general_error(ERROR_INTERRUPT,false_object,false_object);
460                         assert(false);
461                 }
462                 else if(strcmp(cmd,"data") == 0)
463                         dump_objects(TYPE_COUNT);
464                 else if(strcmp(cmd,"refs") == 0)
465                 {
466                         cell addr = read_cell_hex();
467                         std::cout << "Data heap references:" << std::endl;
468                         find_data_references(addr);
469                         std::cout << std::endl;
470                 }
471                 else if(strcmp(cmd,"words") == 0)
472                         dump_objects(WORD_TYPE);
473                 else if(strcmp(cmd,"tuples") == 0)
474                         dump_objects(TUPLE_TYPE);
475                 else if(strcmp(cmd,"push") == 0)
476                 {
477                         cell addr = read_cell_hex();
478                         ctx->push(addr);
479                 }
480                 else if(strcmp(cmd,"code") == 0)
481                         dump_code_heap();
482                 else if(strcmp(cmd,"gc") == 0)
483                         primitive_full_gc();
484                 else
485                         std::cout << "unknown command" << std::endl;
486         }
487 }
488
489 void factor_vm::primitive_die()
490 {
491         std::cout << "The die word was called by the library. Unless you called it yourself," << std::endl;
492         std::cout << "you have triggered a bug in Factor. Please report." << std::endl;
493         factorbug();
494 }
495
496 }