| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | std::ostream &operator<<(std::ostream &out, const string *str) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	for(cell i = 0; i < string_capacity(str); i++) | 
					
						
							| 
									
										
										
										
											2010-04-25 20:19:50 -04:00
										 |  |  | 		out << (char)str->data()[i]; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	return out; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 07:20:09 -05:00
										 |  |  | void factor_vm::print_word(word *word, cell nesting) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	if(tagged<object>(word->vocabulary).type_p(STRING_TYPE)) | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << untag<string>(word->vocabulary) << ":"; | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	if(tagged<object>(word->name).type_p(STRING_TYPE)) | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << untag<string>(word->name); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "#<not a string: "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		print_nested_obj(word->name,nesting); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << ">"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | void factor_vm::print_factor_string(string *str) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << '"' << str << '"'; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 07:20:09 -05:00
										 |  |  | void factor_vm::print_array(array *array, cell nesting) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	cell length = array_capacity(array); | 
					
						
							|  |  |  | 	cell i; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	bool trimmed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(length > 10 && !full_output) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		trimmed = true; | 
					
						
							|  |  |  | 		length = 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		trimmed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(i = 0; i < length; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		print_nested_obj(array_nth(array,i),nesting); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(trimmed) | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "..."; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 23:26:10 -05:00
										 |  |  | void factor_vm::print_alien(alien *alien, cell nesting) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (to_boolean(alien->expired)) | 
					
						
							|  |  |  | 		std::cout << "#<expired alien>"; | 
					
						
							|  |  |  | 	else if (to_boolean(alien->base)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << "#<displaced alien " << alien->displacement << "+"; | 
					
						
							|  |  |  | 		print_nested_obj(alien->base, nesting); | 
					
						
							|  |  |  | 		std::cout << ">"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << "#<alien " << (void*)alien->address << ">"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::print_byte_array(byte_array *array, cell nesting) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	cell length = array->capacity; | 
					
						
							|  |  |  | 	cell i; | 
					
						
							|  |  |  | 	bool trimmed; | 
					
						
							|  |  |  | 	unsigned char *data = array->data<unsigned char>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(length > 16 && !full_output) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		trimmed = true; | 
					
						
							|  |  |  | 		length = 16; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		trimmed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(i = 0; i < length; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << " " << (unsigned)data[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(trimmed) | 
					
						
							|  |  |  | 		std::cout << "..."; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_tuple(tuple *tuple, cell nesting) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	tuple_layout *layout = untag<tuple_layout>(tuple->layout); | 
					
						
							|  |  |  | 	cell length = to_fixnum(layout->size); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << " "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	print_nested_obj(layout->klass,nesting); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool trimmed; | 
					
						
							|  |  |  | 	if(length > 10 && !full_output) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		trimmed = true; | 
					
						
							|  |  |  | 		length = 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		trimmed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	for(cell i = 0; i < length; i++) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " "; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		print_nested_obj(tuple->data()[i],nesting); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(trimmed) | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "..."; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_nested_obj(cell obj, fixnum nesting) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	if(nesting <= 0 && !full_output) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " ... "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	quotation *quot; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	switch(tagged<object>(obj).type()) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	case FIXNUM_TYPE: | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << untag_fixnum(obj); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2011-11-06 23:26:10 -05:00
										 |  |  | 	case FLOAT_TYPE: | 
					
						
							|  |  |  | 		std::cout << untag_float(obj); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	case WORD_TYPE: | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		print_word(untag<word>(obj),nesting - 1); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case STRING_TYPE: | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		print_factor_string(untag<string>(obj)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case F_TYPE: | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "f"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case TUPLE_TYPE: | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "T{"; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		print_tuple(untag<tuple>(obj),nesting - 1); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " }"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2011-11-06 23:26:10 -05:00
										 |  |  | 	case WRAPPER_TYPE: | 
					
						
							|  |  |  | 		std::cout << "W{ "; | 
					
						
							|  |  |  | 		print_nested_obj(untag<wrapper>(obj)->object,nesting - 1); | 
					
						
							|  |  |  | 		std::cout << " }"; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case BYTE_ARRAY_TYPE: | 
					
						
							|  |  |  | 		std::cout << "B{"; | 
					
						
							|  |  |  | 		print_byte_array(untag<byte_array>(obj),nesting - 1); | 
					
						
							|  |  |  | 		std::cout << " }"; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	case ARRAY_TYPE: | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "{"; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		print_array(untag<array>(obj),nesting - 1); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " }"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case QUOTATION_TYPE: | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "["; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 		quot = untag<quotation>(obj); | 
					
						
							|  |  |  | 		print_array(untag<array>(quot->array),nesting - 1); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << " ]"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2011-11-06 23:26:10 -05:00
										 |  |  | 	case ALIEN_TYPE: | 
					
						
							|  |  |  | 		print_alien(untag<alien>(obj), nesting - 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2011-11-06 23:26:10 -05:00
										 |  |  | 		std::cout << "#<" << type_name(tagged<object>(obj).type()) << " @ "; | 
					
						
							| 
									
										
										
										
											2011-11-06 22:56:47 -05:00
										 |  |  | 		std::cout << (void*)obj << ">"; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-21 16:25:28 -05:00
										 |  |  | 	std::cout << std::flush; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_obj(cell obj) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	print_nested_obj(obj,10); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_objects(cell *start, cell *end) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 	for(; start <= end; start++) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 		print_obj(*start); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_datastack() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "==== DATA STACK:" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (ctx) | 
					
						
							|  |  |  | 		print_objects((cell *)ctx->datastack_seg->start,(cell *)ctx->datastack); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		std::cout << "*** Context not initialized" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_retainstack() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "==== RETAIN STACK:" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (ctx) | 
					
						
							|  |  |  | 		print_objects((cell *)ctx->retainstack_seg->start,(cell *)ctx->retainstack); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		std::cout << "*** Context not initialized" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | struct stack_frame_printer { | 
					
						
							| 
									
										
										
										
											2009-10-18 21:31:59 -04:00
										 |  |  | 	factor_vm *parent; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-18 21:31:59 -04:00
										 |  |  | 	explicit stack_frame_printer(factor_vm *parent_) : parent(parent_) {} | 
					
						
							| 
									
										
										
										
											2011-12-06 12:14:56 -05:00
										 |  |  | 	void operator()(void *frame_top, cell frame_size, code_block *owner, void *addr) | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2011-12-06 12:14:56 -05:00
										 |  |  | 		std::cout << "frame: " << frame_top << " size " << frame_size << std::endl; | 
					
						
							| 
									
										
										
										
											2009-12-15 07:20:09 -05:00
										 |  |  | 		std::cout << "executing: "; | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		parent->print_obj(owner->owner); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-12-15 07:20:09 -05:00
										 |  |  | 		std::cout << "scan: "; | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		parent->print_obj(owner->scan(parent, addr)); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							|  |  |  | 		std::cout << "word/quot addr: "; | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		std::cout << std::hex << (cell)owner->owner << std::dec; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							|  |  |  | 		std::cout << "word/quot xt: "; | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		std::cout << std::hex << (cell)owner->entry_point() << std::dec; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							|  |  |  | 		std::cout << "return address: "; | 
					
						
							| 
									
										
										
										
											2011-12-02 12:11:34 -05:00
										 |  |  | 		std::cout << std::hex << (cell)addr << std::dec; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::print_callstack() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "==== CALL STACK:" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (ctx) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		stack_frame_printer printer(this); | 
					
						
							| 
									
										
										
										
											2011-12-06 18:00:02 -05:00
										 |  |  | 		iterate_callstack(ctx,printer); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		std::cout << "*** Context not initialized" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 17:38:01 -05:00
										 |  |  | void factor_vm::print_callstack_object(callstack *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	stack_frame_printer printer(this); | 
					
						
							|  |  |  | 	iterate_callstack_object(obj,printer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | struct padded_address { | 
					
						
							|  |  |  | 	cell value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	explicit padded_address(cell value_) : value(value_) {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::ostream &operator<<(std::ostream &out, const padded_address &value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char prev = out.fill('0'); | 
					
						
							|  |  |  | 	out.width(sizeof(cell) * 2); | 
					
						
							|  |  |  | 	out << std::hex << value.value << std::dec; | 
					
						
							|  |  |  | 	out.fill(prev); | 
					
						
							|  |  |  | 	return out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::dump_cell(cell x) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << padded_address(x) << ": "; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	x = *(cell *)x; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << padded_address(x) << " tag " << TAG(x) << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::dump_memory(cell from, cell to) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	from = UNTAG(from); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 	for(; from <= to; from += sizeof(cell)) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		dump_cell(from); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 23:20:49 -04:00
										 |  |  | template<typename Generation> | 
					
						
							|  |  |  | void factor_vm::dump_generation(const char *name, Generation *gen) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << name << ": "; | 
					
						
							|  |  |  | 	std::cout << "Start=" << gen->start; | 
					
						
							|  |  |  | 	std::cout << ", size=" << gen->size; | 
					
						
							|  |  |  | 	std::cout << ", end=" << gen->end; | 
					
						
							|  |  |  | 	std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::dump_generations() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-09 23:36:50 -04:00
										 |  |  | 	std::cout << std::hex; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 23:20:49 -04:00
										 |  |  | 	dump_generation("Nursery",&nursery); | 
					
						
							|  |  |  | 	dump_generation("Aging",data->aging); | 
					
						
							|  |  |  | 	dump_generation("Tenured",data->tenured); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "Cards:"; | 
					
						
							|  |  |  | 	std::cout << "base=" << (cell)data->cards << ", "; | 
					
						
							|  |  |  | 	std::cout << "size=" << (cell)(data->cards_end - data->cards) << std::endl; | 
					
						
							| 
									
										
										
										
											2010-09-09 23:36:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	std::cout << std::dec; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | struct object_dumper { | 
					
						
							|  |  |  | 	factor_vm *parent; | 
					
						
							|  |  |  | 	cell type; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | 	explicit object_dumper(factor_vm *parent_, cell type_) : | 
					
						
							|  |  |  | 		parent(parent_), type(type_) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 22:49:03 -05:00
										 |  |  | 	void operator()(object *obj) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-11-10 22:06:36 -05:00
										 |  |  | 		if(type == TYPE_COUNT || obj->type() == type) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-11-05 22:49:03 -05:00
										 |  |  | 			std::cout << padded_address((cell)obj) << " "; | 
					
						
							|  |  |  | 			parent->print_nested_obj(tag_dynamic(obj),2); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 			std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | void factor_vm::dump_objects(cell type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	primitive_full_gc(); | 
					
						
							|  |  |  | 	object_dumper dumper(this,type); | 
					
						
							|  |  |  | 	each_object(dumper); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | struct find_data_reference_slot_visitor { | 
					
						
							| 
									
										
										
										
											2009-11-05 22:49:03 -05:00
										 |  |  | 	cell look_for; | 
					
						
							|  |  |  | 	object *obj; | 
					
						
							| 
									
										
										
										
											2009-10-18 21:31:59 -04:00
										 |  |  | 	factor_vm *parent; | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | 	explicit find_data_reference_slot_visitor(cell look_for_, object *obj_, factor_vm *parent_) : | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | 		look_for(look_for_), obj(obj_), parent(parent_) { } | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void operator()(cell *scan) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 		if(look_for == *scan) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-11-05 22:49:03 -05:00
										 |  |  | 			std::cout << padded_address((cell)obj) << " "; | 
					
						
							|  |  |  | 			parent->print_nested_obj(tag_dynamic(obj),2); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 			std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | struct dump_edges_slot_visitor { | 
					
						
							|  |  |  | 	object *obj; | 
					
						
							|  |  |  | 	factor_vm *parent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	explicit dump_edges_slot_visitor(cell, object *obj_, factor_vm *parent_) : | 
					
						
							|  |  |  | 		obj(obj_), parent(parent_) { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void operator()(cell *scan) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (TAG(*scan) > F_TYPE) | 
					
						
							|  |  |  | 			std::cout << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan << std::endl; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename SlotVisitor> | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | struct data_reference_object_visitor { | 
					
						
							|  |  |  | 	cell look_for; | 
					
						
							|  |  |  | 	factor_vm *parent; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | 	explicit data_reference_object_visitor(cell look_for_, factor_vm *parent_) : | 
					
						
							|  |  |  | 		look_for(look_for_), parent(parent_) {} | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 22:49:03 -05:00
										 |  |  | 	void operator()(object *obj) | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | 		SlotVisitor visitor(look_for,obj,parent); | 
					
						
							| 
									
										
										
										
											2009-11-11 20:31:18 -05:00
										 |  |  | 		obj->each_slot(visitor); | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | void factor_vm::find_data_references(cell look_for) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | 	data_reference_object_visitor<find_data_reference_slot_visitor> visitor(look_for,this); | 
					
						
							|  |  |  | 	each_object(visitor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::dump_edges() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	data_reference_object_visitor<dump_edges_slot_visitor> visitor(0,this); | 
					
						
							| 
									
										
										
										
											2009-10-31 22:06:34 -04:00
										 |  |  | 	each_object(visitor); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 11:22:06 -04:00
										 |  |  | struct code_block_printer { | 
					
						
							|  |  |  | 	factor_vm *parent; | 
					
						
							| 
									
										
										
										
											2009-12-02 05:28:15 -05:00
										 |  |  | 	cell reloc_size, parameter_size; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 00:02:58 -04:00
										 |  |  | 	explicit code_block_printer(factor_vm *parent_) : | 
					
						
							| 
									
										
										
										
											2009-12-02 05:28:15 -05:00
										 |  |  | 		parent(parent_), reloc_size(0), parameter_size(0) {} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-24 22:24:06 -04:00
										 |  |  | 	void operator()(code_block *scan, cell size) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-05-04 08:00:06 -04:00
										 |  |  | 		const char *status; | 
					
						
							| 
									
										
										
										
											2009-10-20 10:37:24 -04:00
										 |  |  | 		if(scan->free_p()) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			status = "free"; | 
					
						
							| 
									
										
										
										
											2009-10-06 06:52:45 -04:00
										 |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-10-24 22:24:06 -04:00
										 |  |  | 			reloc_size += parent->object_size(scan->relocation); | 
					
						
							| 
									
										
										
										
											2009-12-02 05:28:15 -05:00
										 |  |  | 			parameter_size += parent->object_size(scan->parameters); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-02 02:09:08 -05:00
										 |  |  | 			if(parent->code->marked_p(scan)) | 
					
						
							|  |  |  | 				status = "marked"; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				status = "allocated"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			std::cout << std::hex << (cell)scan << std::dec << " "; | 
					
						
							|  |  |  | 			std::cout << std::hex << size << std::dec << " "; | 
					
						
							| 
									
										
										
										
											2011-11-25 21:58:21 -05:00
										 |  |  | 			std::cout << status << " "; | 
					
						
							| 
									
										
										
										
											2011-11-25 22:23:07 -05:00
										 |  |  | 			std::cout << "stack frame " << scan->stack_frame_size(); | 
					
						
							| 
									
										
										
										
											2011-11-25 21:58:21 -05:00
										 |  |  | 			std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-12-02 02:09:08 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-20 11:22:06 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Dump all code blocks for debugging */ | 
					
						
							|  |  |  | void factor_vm::dump_code_heap() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	code_block_printer printer(this); | 
					
						
							| 
									
										
										
										
											2009-10-20 16:15:05 -04:00
										 |  |  | 	code->allocator->iterate(printer); | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << printer.reloc_size << " bytes used by relocation tables" << std::endl; | 
					
						
							|  |  |  | 	std::cout << printer.parameter_size << " bytes used by parameter tables" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | void factor_vm::factorbug_usage(bool advanced_p) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "Basic commands:" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | #ifdef WINDOWS
 | 
					
						
							|  |  |  | 	std::cout << "  q ^Z             -- quit Factor" << std::endl; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "  q ^D             -- quit Factor" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "  c                -- continue executing Factor - NOT SAFE" << std::endl; | 
					
						
							|  |  |  | 	std::cout << "  t                -- throw exception in Factor - NOT SAFE" << std::endl; | 
					
						
							|  |  |  | 	std::cout << "  .s .r .c         -- print data, retain, call stacks" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | 	if (advanced_p) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << "  help             -- reprint this message" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "Advanced commands:" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  e                -- dump environment" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  d <addr> <count> -- dump memory" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  u <addr>         -- dump object at tagged <addr>" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  . <addr>         -- print object at tagged <addr>" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  g                -- dump generations" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  ds dr            -- dump data, retain stacks" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  trim             -- toggle output trimming" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  data             -- data heap dump" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  words            -- words dump" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  tuples           -- tuples dump" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | 		std::cout << "  edges            -- print all object-to-object references" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | 		std::cout << "  refs <addr>      -- find data heap references to object" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  push <addr>      -- push object on data stack - NOT SAFE" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  gc               -- trigger full GC - NOT SAFE" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-12-12 22:03:42 -05:00
										 |  |  | 		std::cout << "  compact-gc       -- trigger compacting GC - NOT SAFE" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | 		std::cout << "  code             -- code heap dump" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 		std::cout << "  abort            -- call abort()" << std::endl; | 
					
						
							|  |  |  | 		std::cout << "  breakpoint       -- trigger system breakpoint" << std::endl; | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << "  help             -- full help, including advanced commands" << std::endl; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | 	std::cout << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | static void exit_fep(factor_vm *vm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	vm->unlock_console(); | 
					
						
							| 
									
										
										
										
											2011-11-15 21:50:42 -05:00
										 |  |  | 	vm->handle_ctrl_c(); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	vm->fep_p = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | void factor_vm::factorbug() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(fep_disabled) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::cout << "Low level debugger disabled" << std::endl; | 
					
						
							|  |  |  | 		exit(1); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-31 19:50:21 -04:00
										 |  |  | 	if (sampling_profiler_p) | 
					
						
							|  |  |  | 		end_sampling_profiler(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | 	fep_p = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::cout << "Starting low level debugger..." << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-08 11:52:52 -05:00
										 |  |  | 	// Even though we've stopped the VM, the stdin_loop thread (see os-*.cpp)
 | 
					
						
							|  |  |  | 	// that pumps the console is still running concurrently. We lock a mutex so
 | 
					
						
							|  |  |  | 	// the thread will take a break and give us exclusive access to stdin.
 | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	lock_console(); | 
					
						
							| 
									
										
										
										
											2011-11-15 21:50:42 -05:00
										 |  |  | 	ignore_ctrl_c(); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-31 20:38:42 -04:00
										 |  |  | 	if (!fep_help_was_shown) { | 
					
						
							|  |  |  | 		factorbug_usage(false); | 
					
						
							|  |  |  | 		fep_help_was_shown = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	bool seen_command = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(;;) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		char cmd[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | 		std::cout << "> " << std::flush; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-01 19:14:20 -04:00
										 |  |  | 		std::cin >> std::setw(1024) >> cmd >> std::setw(0);  | 
					
						
							| 
									
										
										
										
											2011-10-26 23:47:04 -04:00
										 |  |  | 		if(!std::cin.good()) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			if(!seen_command) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				/* If we exit with an EOF immediately, then
 | 
					
						
							|  |  |  | 				dump stacks. This is useful for builder and | 
					
						
							|  |  |  | 				other cases where Factor is run with stdin | 
					
						
							|  |  |  | 				redirected to /dev/null */ | 
					
						
							|  |  |  | 				fep_disabled = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				print_datastack(); | 
					
						
							|  |  |  | 				print_retainstack(); | 
					
						
							|  |  |  | 				print_callstack(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			exit(1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		seen_command = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-26 20:04:11 -04:00
										 |  |  | 		if(strcmp(cmd,"q") == 0) | 
					
						
							|  |  |  | 			exit(1); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		if(strcmp(cmd,"d") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell addr = read_cell_hex(); | 
					
						
							| 
									
										
										
										
											2010-08-01 19:14:20 -04:00
										 |  |  | 			if (std::cin.peek() == ' ') | 
					
						
							|  |  |  | 				std::cin.ignore(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(!std::cin.good()) break; | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell count = read_cell_hex(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			dump_memory(addr,addr+count); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"u") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell addr = read_cell_hex(); | 
					
						
							|  |  |  | 			cell count = object_size(addr); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			dump_memory(addr,addr+count); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if(strcmp(cmd,".") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell addr = read_cell_hex(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			print_obj(addr); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 			std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-10-21 02:19:32 -04:00
										 |  |  | 		else if(strcmp(cmd,"trim") == 0) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			full_output = !full_output; | 
					
						
							| 
									
										
										
										
											2011-10-26 20:04:11 -04:00
										 |  |  | 		else if(strcmp(cmd,"ds") == 0) | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 			dump_memory(ctx->datastack_seg->start,ctx->datastack); | 
					
						
							| 
									
										
										
										
											2011-10-26 20:04:11 -04:00
										 |  |  | 		else if(strcmp(cmd,"dr") == 0) | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 			dump_memory(ctx->retainstack_seg->start,ctx->retainstack); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		else if(strcmp(cmd,".s") == 0) | 
					
						
							|  |  |  | 			print_datastack(); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,".r") == 0) | 
					
						
							|  |  |  | 			print_retainstack(); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,".c") == 0) | 
					
						
							|  |  |  | 			print_callstack(); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"e") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 			for(cell i = 0; i < special_object_count; i++) | 
					
						
							|  |  |  | 				dump_cell((cell)&special_objects[i]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"g") == 0) | 
					
						
							|  |  |  | 			dump_generations(); | 
					
						
							| 
									
										
										
										
											2011-10-26 20:04:11 -04:00
										 |  |  | 		else if(strcmp(cmd,"c") == 0) | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 			exit_fep(this); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-10-21 02:19:32 -04:00
										 |  |  | 		else if(strcmp(cmd,"t") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 			exit_fep(this); | 
					
						
							| 
									
										
										
										
											2011-10-21 02:19:32 -04:00
										 |  |  | 			general_error(ERROR_INTERRUPT,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 			FACTOR_ASSERT(false); | 
					
						
							| 
									
										
										
										
											2011-10-21 02:19:32 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		else if(strcmp(cmd,"data") == 0) | 
					
						
							|  |  |  | 			dump_objects(TYPE_COUNT); | 
					
						
							| 
									
										
										
										
											2011-11-11 13:12:36 -05:00
										 |  |  | 		else if(strcmp(cmd,"edges") == 0) | 
					
						
							|  |  |  | 			dump_edges(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		else if(strcmp(cmd,"refs") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell addr = read_cell_hex(); | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 			std::cout << "Data heap references:" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			find_data_references(addr); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 			std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"words") == 0) | 
					
						
							|  |  |  | 			dump_objects(WORD_TYPE); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"tuples") == 0) | 
					
						
							|  |  |  | 			dump_objects(TUPLE_TYPE); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"push") == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | 			cell addr = read_cell_hex(); | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | 			ctx->push(addr); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"code") == 0) | 
					
						
							|  |  |  | 			dump_code_heap(); | 
					
						
							| 
									
										
										
										
											2011-12-05 17:18:42 -05:00
										 |  |  | 		else if(strcmp(cmd,"compact-gc") == 0) | 
					
						
							|  |  |  | 			primitive_compact_gc(); | 
					
						
							| 
									
										
										
										
											2011-10-27 00:55:51 -04:00
										 |  |  | 		else if(strcmp(cmd,"gc") == 0) | 
					
						
							|  |  |  | 			primitive_full_gc(); | 
					
						
							| 
									
										
										
										
											2011-12-12 22:03:42 -05:00
										 |  |  | 		else if(strcmp(cmd,"compact-gc") == 0) | 
					
						
							|  |  |  | 			primitive_compact_gc(); | 
					
						
							| 
									
										
										
										
											2011-10-27 23:55:12 -04:00
										 |  |  | 		else if(strcmp(cmd,"help") == 0) | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:59 -04:00
										 |  |  | 			factorbug_usage(true); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 		else if(strcmp(cmd,"abort") == 0) | 
					
						
							|  |  |  | 			abort(); | 
					
						
							|  |  |  | 		else if(strcmp(cmd,"breakpoint") == 0) | 
					
						
							|  |  |  | 			breakpoint(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 			std::cout << "unknown command" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-27 14:42:18 -04:00
										 |  |  | void factor_vm::primitive_die() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 	std::cout << "The die word was called by the library. Unless you called it yourself," << std::endl; | 
					
						
							|  |  |  | 	std::cout << "you have triggered a bug in Factor. Please report." << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	factorbug(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |