⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.c

📁 一个MIPS虚拟机的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	*index = 0;
	for (i=3;i<len;i++)
		if ((input[i]!=' ') && (input[i]!=10 ))
			break;
	//printf
	if (i==len)
		{
			*index = -1 ;   //means all registers
			return true;
		}
		
	j=i;
	for (;j<len;j++)
		{
			if ((input[j]==' ')||(input[j]=='\0')||(input[j]==10) )
				break;
			
		}
	j=j-1;

	for(k=0;k<=j-i;k++)
		a[k]=input[i+k];
	a[k]='\0';
	//printf("a = %s\n",a);
	
	if (!strncasecmp(a,"ZERO",4))
		{
			*index = 0;
			printf("a = zero \n");
		}
	else
		{
			if (strlen(a)>2)
			{
				printf("Wrong CPU Register index \n");
				return false;
				
			}
				 if (!strncasecmp(a,"AT",2))
					*index = 1;
				 else if (!strncasecmp(a,"V0",2))
				 	*index = 2;
				 else if (!strncasecmp(a,"V1",2))
				 	*index = 3;
				 else if (!strncasecmp(a,"A0",2))
				 	*index = 4;
				 else if (!strncasecmp(a,"A1",2))
				 	*index = 5;
				 else if (!strncasecmp(a,"A2",2))
				 	*index = 6;
				 else if (!strncasecmp(a,"A3",2))
				 	*index = 7;
				 else if (!strncasecmp(a,"T0",2))
				 	*index = 8;
				 else if (!strncasecmp(a,"T1",2))
				 	*index = 9;
				 else if (!strncasecmp(a,"T2",2))
				 	*index = 10;
				 else if (!strncasecmp(a,"T3",2))
				 	*index = 11;
				 else if (!strncasecmp(a,"T4",2))
				 	*index = 12;
				 else if (!strncasecmp(a,"T5",2))
				 	*index = 13;
				 else if (!strncasecmp(a,"T6",2))
				 	*index = 14;
				 else if (!strncasecmp(a,"T7",2))
				 	*index = 15;
				 else if (!strncasecmp(a,"S0",2))
				 	*index = 16;
				 else if (!strncasecmp(a,"S1",2))
				 	*index = 17;
				 else if (!strncasecmp(a,"S2",2))
				 	*index = 18;
				 else if (!strncasecmp(a,"S3",2))
				 	*index = 19;
				 else if (!strncasecmp(a,"S4",2))
				 	*index = 20;
				 else if (!strncasecmp(a,"S5",2))
				 	*index = 21;
				 else if (!strncasecmp(a,"S6",2))
				 	*index = 22;
				 else if (!strncasecmp(a,"S7",2))
				 	*index = 23;
				 else if (!strncasecmp(a,"T8",2))
				 	*index = 24;
				 else if (!strncasecmp(a,"T9",2))
				 	*index = 25;	
				 else if (!strncasecmp(a,"K0",2))
				 	*index = 26;
				 else if (!strncasecmp(a,"K1",2))
				 	*index = 27;
				 else if (!strncasecmp(a,"GP",2))
				 	*index = 28;
				 else if (!strncasecmp(a,"SP",2))
				 	*index = 29;
				 else if (!strncasecmp(a,"FP",2))
				 	*index = 30;
				 else if (!strncasecmp(a,"S8",2))
				 	*index = 30;
				 else if (!strncasecmp(a,"RA",2))
				 	*index = 31;
				else
					{
						if (((strlen(a)==1) && !isdigit(a[0])) ||((strlen(a)==2) && (!isdigit(a[1])  || !isdigit(a[0]))  ) )
							{
								printf("Wrong CPU Register index \n");
								return false;
							}
						else
							{
								*index = atoi(a);
							}
					}
		}
	return true;
	
}


boolean  get_cp0r_index(char input[],int len,int *index)
{
	int i,j,k;
	char a[20];
	
	*index = 0;
	for (i=4;i<len;i++)
		if ((input[i]!=' ') && (input[i]!=10 ))
			break;
	if (i==len)
		{
			//printf("Index can not be null.\n");
			*index = -1 ;   //means all registers
			return true;
		}
		
	j=i;
	for (;j<len;j++)
		{
			if ((input[j]==' ')||(input[j]=='\0')||(input[j]==10) )
				break;
				
		}
	j=j-1;

	for(k=0;k<=j-i;k++)
		a[k]=input[i+k];
	a[k]='\0';

	
	if (!strncasecmp(a,"INDEX",5))
		{
			*index = INDEX;
		}
	else if (!strncasecmp(a,"RANDOM",6))
		{
			*index = RANDOM;
		}
	else if (!strncasecmp(a,"ENTRYLO",7))
		{
			*index = ENTRYLO;
		}
	else if (!strncasecmp(a,"CONTEXT",7))
		{
			*index = CONTEXT;
		}
	else if (!strncasecmp(a,"BADVADDR",8))
		{
			*index = BADVADDR;
		}
	else if (!strncasecmp(a,"ENTRYHI",7))
		{
			*index = ENTRYHI;
		}
	else if (!strncasecmp(a,"SR",2))
		{
			*index = SR;
		}
	else if (!strncasecmp(a,"CAUSE",5))
		{
			*index = CAUSE;
		}
	else if (!strncasecmp(a,"EPC",3))
		{
			*index = EPC;
		}
	else if (!strncasecmp(a,"PRID",3))
		{
			*index = PRID;
		}
	else
		{
			
			if (((strlen(a)==1) && !isdigit(a[0])) ||((strlen(a)==2) && (!isdigit(a[1])  || !isdigit(a[0]))  ) )
				{
					printf("Wrong CP0 Register index \n");
					return false;
				}
			*index = atoi(a);
		}


	switch (*index)
		{
			case INDEX:
			case RANDOM:
			case ENTRYLO:
			case CONTEXT:
			case BADVADDR:
			case ENTRYHI:
			case SR:
			case CAUSE:
			case EPC:
			case PRID:
				break;
			default:
				printf("CP0 Register index must be [%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]\n",INDEX,\
							RANDOM,ENTRYLO,CONTEXT,BADVADDR,ENTRYHI,SR,CAUSE,EPC,PRID);
				return false;
				break;
		}
	return true;
	
}

boolean get_memory_address(char input[],int len,UINT32 *low,UINT32 *high)
{
	int i,j,k;
	int l;
	char a[20];
	char b[20];
	*low = 0;
	*high = 63;
	/* 0x12345678-0x12345678
            i		        l                 j

	*/
	for (i=2;i<len;i++)
		if ((input[i]!=' ') && (input[i]!=10 ))
			break;
	if (i==len)
		{
			printf("Please input memory addrss\n");
			return false;
		}
	
	j=i;
	for (;j<len;j++)
		{
			if ((input[j]==' ')||(input[j]=='\0')||(input[j]==10) )
				break;
			
		}
	j=j-1;

	for (l=i;l<=j;l++)
		{
			if (input[l]=='-')
				break;
		}
	if (l>j)
		{
			//no -
			for(k=0;k<=j-i;k++)
				a[k]=input[i+k];
			a[k]='\0';
			//printf("a = %s\n",a);
			if ((strlen(a)>10) || (strlen(a)==9))
			{
				printf("Wrong memory address \n");
				return false;
			}
			else if (strlen(a)==10)
			{
				if (a[0]!='0')
					{
					printf("Wrong memory address \n");
					return false;
					}
				else if (a[1]!='x' && a[1]!='X')
					{
						printf("Wrong memory address \n");
						return false;
					}
				else
					*low  = strtoul(a,NULL,16);
				
			}
			else
				{
				//printf("a = %s\n",a);
				*low  = strtoul(a,NULL,16);
				
				}
			*high=*low;
		}
	else if (l==j)
		{
			//wront input
			printf("Wrong memory address \n");
			return false;
		}
	else
		{
			for(k=0;k<l-i;k++)
				a[k]=input[i+k];
			a[k]='\0';
			//printf("a = %s\n",a);
			if ((strlen(a)>10) || (strlen(a)==9))
			{
				printf("Wrong memory address \n");
				return false;
			}
			else if (strlen(a)==10)
			{
				if (a[0]!='0')
					{
					printf("Wrong memory address \n");
					return false;
					}
				else if (a[1]!='x' && a[1]!='X')
					{
						printf("Wrong memory address \n");
						return false;
					}
				else
					*low  = strtoul(a,NULL,16);
				
			}
			else
				{
				//printf("a = %s\n",a);
				*low  = strtoul(a,NULL,16);
				
				}

			
				
			for(k=0;k<j-l;k++)
				b[k]=input[l+1+k];
			b[k]='\0';
			//printf("b = %s\n",b);
			if ((strlen(b)>10) || (strlen(b)==9))
			{
				printf("Wrong memory address \n");
				return false;
			}
			else if (strlen(b)==10)
			{
				if (b[0]!='0')
					{
					printf("Wrong memory address \n");
					return false;
					}
				else if (b[1]!='x' && b[1]!='X')
					{
						printf("Wrong memory address \n");
						return false;
					}
				else
					*high  = strtoul(b,NULL,16);
				
			}
			else
				{
				//printf("b = %s\n",b);
				*high  = strtoul(b,NULL,16);
				
				}
			//printf("*high %d \n",*high);
			//printf("*low %d \n",*low);
			if (*low>*high)
				{
					printf("Wrong memory address \n");
					return false;
				}
			return true;
		}

	return true;

	
	
}



static boolean check_memory_address(UINT32 addr_low,UINT32 addr_high,int *type)
{
	/*check the address. return which region it belongs to*/
	/*return value: 1 RAM 
				  2 ROM
				  3 CONSOLE
				  0 error
					*/
	
	if ((addr_low>=RAM_LOWER_BOUND) && (addr_low<=RAM_UPPER_BOUND)) 
		*type= RAM;
	else if ((addr_low>=ROM_LOWER_BOUND) && (addr_low<=ROM_UPPER_BOUND)) 
		*type= ROM ;
	else
		return false;
	return true;
	
}
static UINT8 get_memory_byte(UINT32 addr,int memory_type)
{
	UINT32 offset;
	
	if (memory_type==RAM)
		{	
			offset = addr-RAM_LOWER_BOUND;
			//printf("index 0x%x",offset);
			return ram[offset];
		}
	else if (memory_type==ROM)
		{
			offset = addr-ROM_LOWER_BOUND;
			//printf("index 0x%x",offset);
			return rom[offset];
		}
	
}
static void memory_info(UINT32	addr_low,UINT32 addr_high,int memory_type,int load_type)
{
	UINT32 addr;
	
	addr = addr_low;
	/*load_type=0 lb  
	   load_type=1  lh  
	   load_type=2 lw*/
	 //printf("load type %d",load_type);
	if (0==load_type)
		{
			while (addr<=addr_high)
				{
					printf("0x%x     ",addr);
					printf("0x%x",get_memory_byte(addr,memory_type));
					addr = addr + 1;
					printf("\n");
				}
		
		}
	else if(1==load_type)
		{	while (addr<=addr_high)
			{
			printf("0x%x     ",addr);
			if (target_end==LITTLEEND)
				{
					printf("0x%x",get_memory_byte(addr+1,memory_type));
					printf("%2x",get_memory_byte(addr,memory_type));
				}
			else
				{
					printf("0x%x",get_memory_byte(addr,memory_type));
					printf("%x",get_memory_byte(addr+1,memory_type));
				}
			addr = addr + 2;
			printf("\n");
			}
			
		}
	else if(2==load_type)
		{	while (addr<=addr_high)
			{
			printf("0x%x     ",addr);
			if (target_end==LITTLEEND)
				{
					printf("0x%x",get_memory_byte(addr+3,memory_type));
					printf("%x",get_memory_byte(addr+2,memory_type));
					printf("%x",get_memory_byte(addr+1,memory_type));
					printf("%x",get_memory_byte(addr+0,memory_type));
				}
			else
				{
					printf("0x%x",get_memory_byte(addr,memory_type));
					printf("%x",get_memory_byte(addr+2,memory_type));
					printf("%x",get_memory_byte(addr+3,memory_type));
					printf("%x",get_memory_byte(addr+4,memory_type));
				}
			addr = addr + 4;
			printf("\n");
			}
			
		}
	
}
boolean debug_input()
{
	char input[64];
	boolean run;
	UINT32 addr;
	int bp_index,cpr_index,cp0r_index;
	int tlb_low,tlb_high;
	UINT32 addr_low,addr_high;
	int memory_type;
	
	run=false;

	
	run=debug.current_mode==RUN?true:false;
	if (run==false)
		{
			printf(">");
			
			fgets(input,64,stdin);
			
			trim(input,strlen(input));
			//printf("%s",input);
			if (strncasecmp(input,"r",1)==0)
				{
					debug.current_mode=RUN;
				}
			else if (strncasecmp(input,"s",1)==0)
				{
					debug.current_mode=STEP;
				}
			else 
			{
			debug.current_mode=DEBUG;
			
			if (strncasecmp(input,"bpaddv",6)==0)
				{
					if (get_bpaddr(input,strlen(input),&addr)==false)
						{
							//printf("Please use [bpaddv virtual address] to add breakpoint!\n");
						}
					else
						{
							break_point_add(addr,VADDR);
							printf("Breakpoint 0x%x[virtual address] added!\n",addr);
						}
				}
			else if (strncasecmp(input,"bpaddp",6)==0)
				{
					if (get_bpaddr(input,strlen(input),&addr)==false)
						{
							//printf("Please use [bpaddp physical address] to add breakpoint!\n");
						}
					else
						{
							break_point_add(addr,PHYADDR);
							printf("Breakpoint 0x%x[physical address] added!\n",addr);
						}
				}
			else if (strncasecmp(input,"bpdel",5)==0)
				{
					if (!get_bp_del_index(input,strlen(input),&bp_index))
						{
						}
					else if (bp_index>debug.break_point_number)
						{
							printf("Breakpoint index(%d)> total breakpoint number(%d)\n",bp_index,debug.break_point_number);
						}
					else
						break_point_delete(bp_index);
				}
			else if (strncasecmp(input,"bp",2)==0)
				{
					break_point_info();
				}
			else if (strncasecmp(input,"cpr",3)==0)
				{
					if (!get_cpr_index(input,strlen(input),&cpr_index))
						{
						}
					else if (cpr_index>CPU_REGISTER_NUMBER)
						{
							printf("CPU register index should be between %d to %d\n",1,CPU_REGISTER_NUMBER);
						}
					else
						{
							
							cp_register_info(cpr_index);
							
						}
				}
			else if (strncasecmp(input,"cp0r",4)==0)
				{
					if (!get_cp0r_index(input,strlen(input),&cp0r_index))
						{
						}
					else if (cp0r_index>CP0_REGISTER_NUMBER)
						{
							printf("CPU register index should be between %d to %d\n",1,CP0_REGISTER_NUMBER);
						}
					else
						{
							cp0_register_info(cp0r_index);
						
						}
				}
			else if (strncasecmp(input,"tlb",3)==0)
				{
					get_tlb_index(input,strlen(input),&tlb_low,&tlb_high);
					tlb_info(tlb_low,tlb_high);
				}
			else if(strncasecmp(input,"lb",2)==0)
				{
					get_memory_address(input,strlen(input),&addr_low,&addr_high); 
					if (check_memory_address(addr_low,addr_high,&memory_type)==false)
						{
							printf("Wrong memory address!\n");
							return false;
						}
					memory_info(addr_low,addr_high,memory_type,0);
				}
			else if(strncasecmp(input,"lh",2)==0)
				{
					get_memory_address(input,strlen(input),&addr_low,&addr_high); 
					if (check_memory_address(addr_low,addr_high,&memory_type)==false)
						{
							printf("Wrong memory address!\n");
							return false;
						}
					if ((addr_low%2!=0)|| ((addr_high!=addr_low)&&( (addr_high-addr_low+1)%2!=0)))
						{
							printf("Address must be multiple of 2\n");
							return false;
						}
						
					memory_info(addr_low,addr_high,memory_type,1);
				}
			else if(strncasecmp(input,"lw",2)==0)
				{
					get_memory_address(input,strlen(input),&addr_low,&addr_high); 
					if (check_memory_address(addr_low,addr_high,&memory_type)==false)
						{
							printf("Wrong memory address!\n");
							return false;
						}
					if ((addr_low%4!=0)|| ((addr_high!=addr_low)&&( (addr_high-addr_low+1)%4!=0)))
						{
							printf("Address must be multiple of 4\n");
							return false;
						}
					memory_info(addr_low,addr_high,memory_type,2);
				}
			else
				{
					printf("Bad Input\n");
					return false;
				}
			//printf("adfadfs\n");
			
		}
	//printf("aaaa\n");
	}
	return true;
			
}

boolean is_breakpoint(UINT32 bp_addr,int *index)
{
	break_point_node * p;
	*index = 1;
	
	p = debug.break_point_header;
	if (debug.break_point_number==0)
		{
			return false;
		}
	while (p!=NULL)
		{
			if (p->addr==bp_addr)
				return true;
			else
				{
					p = p->next;
					*index  = *index  +1;
				}
				
		}
	return false;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -