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

📄 dbgdata.c

📁 < 虚拟机设计与实现> 的source code, linux版本
💻 C
字号:
#include "dbgdata.h"

#include "error.h"

U1 debug;   /* TRUE if VM is in debug mode */
struct DebugData debugData;

U1 debugbytes[MAX_RECSIZE];

struct HeaderRec readHeaderRec(FILE *fptr);
struct Contents readContents(FILE* fptr);
struct GlobVarRec readGlobVarRec(FILE *fptr);
struct ProcRec readProcRec(FILE *fptr);
struct StackFrameRec readStackFrameRec(FILE *fptr);
struct LabelRec readLabelRec(FILE *fptr);

void printDebugData(struct DebugData * ptr);
void printProcRec(struct ProcRec * pptr, struct DebugData * ptr);
void printStackFrameRec(struct StackFrameRec * sptr, struct DebugData * ptr);
void printLabelRec(struct LabelRec * lptr, struct DebugData * ptr);

char * globSz[] = {"", "SZ_BYTE", "SZ_WORD", "", "SZ_DWORD",
                   "", "", "", "SZ_QWORD"};

void populateDebugData(struct HeaderRec hr, struct DebugData * ptr, FILE * fptr)
{
	U8 i;
	U4 np;
	U4 ng;

	/*1) read table of contents, info on number of globs and procs */

	(*ptr).contents = readContents(fptr);
	if (nErrors > 0)
		return;
	ng = ((*ptr).contents).nGlobVarRec;
	np = ((*ptr).contents).nProcRec;

	/*2) based on contents info, allocate globs and procs */

	if (ng > 0)
	{
	    (*ptr).gvRec = (struct GlobVarRec *)malloc(sizeof(struct GlobVarRec) * ng);
        if ((*ptr).gvRec == NULL)
		{
		    ERROR0("could not allocate space for global variable records");
			return;
		}
	}

	if (np > 0)
	{
	    (*ptr).pRec = (struct ProcRec *)malloc(sizeof(struct ProcRec) * np);
        if ((*ptr).pRec == NULL)
		{
		    ERROR0("could not allocate space for procedure records");
			return;
		}
	}

	/*3) read in globvar recs following contents */

	for (i = 0; i < ng; i++)
	{
		(*ptr).gvRec[i] = readGlobVarRec(fptr);
		if (nErrors > 0)
			return;
	}

	/*4) read in procs which follow globvars */

	for (i = 0; i < np; i++)
	{
		(*ptr).pRec[i] = readProcRec(fptr);
		if (nErrors > 0)
			return;
	}

	/*
	5) allocate and populate string table (follows symbol table)
	   note: native limit via malloc(size_t) 
	*/

	(*ptr).strTbl = (U1*)malloc((unsigned int)hr.szStrTbl);
	if ((*ptr).strTbl == NULL)
	{
		ERROR0("could not allocate space for string table");
		return;
	}

	for (i = 0; i < hr.szStrTbl; i++)
	{
		(*ptr).strTbl[i] = (U1)fgetc(fptr);
	}
    return;

}/*end populateDebugData*/

struct Contents readContents(FILE * fptr)
{
	struct Contents ct;
	int i;

	ct.nGlobVarRec = 0;
	ct.nProcRec = 0;

	i = fread(debugbytes, sizeof(U1), 8, fptr);
	if (i < 8)
	{
		ERROR0("error reading contents record");
		return ct;
	}

#ifdef ENDIANCHANGE_NEEDED 
	ct.nGlobVarRec = bytecodeToDWord(&(debugbytes[0]));
	ct.nProcRec = bytecodeToDWord(&(debugbytes[4]));
#else
	ct.nGlobVarRec = (U4)*((U4 *)&debugbytes[0]);
	ct.nProcRec = (U4)*((U4 *)&debugbytes[4]);
#endif

	return ct;

}/*end readContents*/

struct GlobVarRec readGlobVarRec(FILE * fptr)
{
	struct GlobVarRec gr;
	int i;

	gr.dType = 0;
	gr.len = 0;
	gr.line = 0;
	gr.offset = 0;
	gr.size = 0;
	gr.text = 0;

	i = fread(debugbytes, sizeof(U1), SIZE_GLOBREC, fptr);
	if (i < SIZE_GLOBREC)
	{
		ERROR0("error reading global record");
		return gr;
	}

#ifdef ENDIANCHANGE_NEEDED 
	gr.text = bytecodeToQWord(&(debugbytes[0]));
	gr.dType = debugbytes[8];
	gr.len = bytecodeToQWord(&(debugbytes[9]));
	gr.size = bytecodeToQWord(&(debugbytes[17]));
	gr.offset = bytecodeToQWord(&(debugbytes[25]));
	gr.line = bytecodeToDWord(&(debugbytes[33]));
#else
	gr.text = (U8)*((U8 *)&debugbytes[0]);
	gr.dType = debugbytes[8];
	gr.len = (U8)*((U8 *)&debugbytes[9]);
	gr.size = (U8)*((U8 *)&debugbytes[17]);
	gr.offset = (S8)*((S8 *)&debugbytes[25]);
	gr.line = (U4)*((U4 *)&debugbytes[33]);
#endif

	return gr;

}/*end readGlobVarRec*/

struct ProcRec readProcRec(FILE * fptr)
{
	struct ProcRec pr;
	int i;

	pr.text = 0;
	pr.address = 0;
    pr.line = 0;

	pr.ret.fpOffset = 0;
	pr.ret.line = 0;
	pr.ret.text = 0;
    pr.nRet = 0;

	pr.arg = NULL;
	pr.nArg = 0;
	pr.local = NULL;
	pr.nLocal = 0;
	pr.label = NULL;
	pr.nLabel = 0;

	i = fread(debugbytes, sizeof(U1), SIZE_PROCREC, fptr);
	if (i < SIZE_PROCREC)
	{
		ERROR0("error reading procedure record");
		return pr;
	}

#ifdef ENDIANCHANGE_NEEDED 
	pr.text = bytecodeToQWord(&(debugbytes[0]));
	pr.address = bytecodeToQWord(&(debugbytes[8]));
	pr.line = bytecodeToDWord(&(debugbytes[16]));
	pr.nRet = debugbytes[20];
	pr.nArg = debugbytes[21];
	pr.nLocal = debugbytes[22];
	pr.nLabel = bytecodeToWord(&(debugbytes[23]));
#else
	pr.text = (U8)*((U8 *)&debugbytes[0]);
	pr.address = (U8)*((U8 *)&debugbytes[8]);
	pr.line = (U4)*((U4 *)&debugbytes[16]);
	pr.nRet = debugbytes[20];
	pr.nArg = debugbytes[21];
	pr.nLocal = debugbytes[22];
	pr.nLabel = (U2)*((U2 *)&debugbytes[23]);
#endif

	if (pr.nArg > 0)
	{
	    pr.arg = (struct StackFrameRec *)malloc(pr.nArg * sizeof(struct StackFrameRec));
		if (pr.arg == NULL)
		{
		    ERROR0("could not allocate space for procedure-argument records");
			return pr;
		}
	}
	
	if (pr.nLocal > 0)
	{
	    pr.local = (struct StackFrameRec *)malloc(pr.nLocal * sizeof(struct StackFrameRec));
		if (pr.local == NULL)
		{
		    ERROR0("could not allocate space for procedure-local records");
			return pr;
		}
	}

	if (pr.nLabel > 0)
	{
	    pr.label = (struct LabelRec *)malloc(pr.nLabel * sizeof(struct LabelRec));
		if (pr.label == NULL)
		{
		    ERROR0("could not allocate space for procedure-label records");
			return pr;
		}
	}

	if (pr.nRet)
	{
		pr.ret = readStackFrameRec(fptr);
		if (nErrors > 0)
			return pr;
	}

	for (i = 0; i < pr.nArg; i++)
	{
		pr.arg[i] = readStackFrameRec(fptr);
		if (nErrors > 0)
			return pr;
	}

	for (i = 0; i < pr.nLocal; i++)
	{
		pr.local[i] = readStackFrameRec(fptr);
		if (nErrors > 0)
			return pr;
	}

	for ( i = 0; i < pr.nLabel; i++)
	{
		pr.label[i] = readLabelRec(fptr);
		if (nErrors > 0)
			return pr;
	}

	return pr;

}/*end readProcRec*/

struct StackFrameRec readStackFrameRec(FILE * fptr)
{
	struct StackFrameRec sfr;
	int i;

	sfr.fpOffset = 0;
	sfr.line = 0;
	sfr.text = 0;

	i = fread(debugbytes, sizeof(U1), SIZE_RETREC, fptr);
	if (i < SIZE_RETREC)
	{
		ERROR0("error reading stackframe record");
		return sfr;
	}

#ifdef ENDIANCHANGE_NEEDED 
	sfr.text = bytecodeToQWord(&(debugbytes[0]));
	sfr.fpOffset = bytecodeToDWord(&(debugbytes[8]));
	sfr.line = bytecodeToDWord(&(debugbytes[12]));
#else
	sfr.text = (U8)*((U8 *)&debugbytes[0]);
	sfr.fpOffset = (S4)*((S4 *)debugbytes[8]);
	sfr.line = (U4)*((U4 *)&debugbytes[12]);
#endif

	return sfr;

}/*end readStackFramerec*/

struct LabelRec readLabelRec(FILE * fptr)
{
	struct LabelRec lr;
	int i;

	lr.address = 0;
	lr.line = 0;
	lr.text = 0;

	i = fread(debugbytes, sizeof(U1), SIZE_LBLREC, fptr);
	if (i < SIZE_LBLREC)
	{
		ERROR0("error reading label record");
		return lr;
	}

#ifdef ENDIANCHANGE_NEEDED 
	lr.text = bytecodeToQWord(&(debugbytes[0]));
	lr.address = bytecodeToQWord(&(debugbytes[8]));
	lr.line = bytecodeToDWord(&(debugbytes[16]));
#else
	lr.text = (U8)*((U8 *)&debugbytes[0]);
	lr.address = (U8)*((U8 *)&debugbytes[8]);
	lr.line = (U4)*((U4 *)&debugbytes[16]);
#endif

	return lr;

}/*end readLabelRec*/

void DebugData_init()
{
	debugData.contents.nGlobVarRec = 0;
	debugData.contents.nProcRec = 0;
	debugData.gvRec = NULL;
	debugData.pRec = NULL;
	debugData.strTbl = NULL;
}

void DebugData_free()
{
	unsigned int i;

	if (debugData.gvRec != NULL)
	{
		free(debugData.gvRec);
		debugData.gvRec = NULL;
	}

    if (debugData.pRec != NULL)
	{
	    for (i = 0; i < debugData.contents.nProcRec; i++)
		{
		    if (debugData.pRec[i].arg != NULL)
			{
		        free(debugData.pRec[i].arg);
			    debugData.pRec[i].arg = NULL;
			}
		    if (debugData.pRec[i].local != NULL)
			{
		        free(debugData.pRec[i].local);
			    debugData.pRec[i].local = NULL;
			}
		    if (debugData.pRec[i].label != NULL)
			{
		        free(debugData.pRec[i].label);
			    debugData.pRec[i].label = NULL;
			}
		}
		free(debugData.pRec);
		debugData.pRec = NULL;
	}

	if (debugData.strTbl != NULL)
	{
		free(debugData.strTbl);
		debugData.strTbl = NULL;
	}
	debugData.contents.nGlobVarRec = 0;
	debugData.contents.nProcRec = 0;
	printf("debugData freed.\n");

	return;
}

void printDebugData(struct DebugData * ptr)
{
	U8 i;

	if (((*ptr).contents.nGlobVarRec == 0) && ((*ptr).contents.nProcRec == 0))
	    return;

	for (i = 0; i < (*ptr).contents.nGlobVarRec; i++)
	{
		printf("GLOBVAR "); 
		pU8(i); 
		printf("\n");
		
		printf("\t id = %s\n", &((*ptr).strTbl[(*ptr).gvRec[i].text]));
		
		printf("\t type = %s\n", globSz[(*ptr).gvRec[i].dType]);
		
		printf("\t len = "); 
		pU8((*ptr).gvRec[i].len); 
		printf("\n");
		
		printf("\t size = "); 
		pU8((*ptr).gvRec[i].size); 
		printf("\n");
		
		printf("\t offset = "); 
		pS8((*ptr).gvRec[i].offset); 
		printf("\n");
		
		printf("\t line = "); 
		pU4((*ptr).gvRec[i].line); 
		printf("\n\n");
	}

	for (i = 0; i < (*ptr).contents.nProcRec; i++)
	{
		printf("PROC "); 
		pU8(i); 
		printf("\n");
		
		printProcRec(&((*ptr).pRec[i]), ptr);
	}
	return;

}/*end printDebugData*/

void printProcRec(struct ProcRec * pptr, struct DebugData * ptr)
{
	U4 i;

	printf("\t id = %s\n", &((*ptr).strTbl[(*pptr).text]));
	
	printf("\t address = "); 
	pU8((*pptr).address); 
	printf("\n");
	
	printf("\t line = "); 
	pU4((*pptr).line); 
	printf("\n");
	
	if ((*pptr).nRet)
	{
		printf("\t RET \n");
		printStackFrameRec(&((*pptr).ret), ptr);
	}

	for (i = 0; i < (*pptr).nArg; i++)
	{
		printf("\t ARG-> "); 
		pU4(i); 
		printf("\n");
		
		printStackFrameRec(&((*pptr).arg[i]), ptr);
	}

	for (i = 0; i < (*pptr).nLocal; i++)
	{
		printf("\t LOCAL-> "); 
		pU4(i); 
		printf("\n");
		
		printStackFrameRec(&((*pptr).local[i]), ptr);
	}

	for(i = 0; i < (*pptr).nLabel; i++)
	{
		printf("\t LABEL-> "); 
		pU4(i); 
		printf("\n");
		
		printLabelRec(&((*pptr).label[i]), ptr);
	}
	return;

}/*end printProcRec*/

void printStackFrameRec(struct StackFrameRec * sptr, struct DebugData * ptr)
{
	printf("\t\t id = %s\n", &((*ptr).strTbl[(*sptr).text]));
	
	printf("\t\t fpOffset = "); 
	pS4((*sptr).fpOffset); 
	printf("\n");
	
	printf("\t\t line = "); 
	pU4((*sptr).line); 
	printf("\n");
	return;

}/*end printStackFrameRec*/

void printLabelRec(struct LabelRec * lptr, struct DebugData * ptr)
{
	printf("\t\t id = %s\n", &((*ptr).strTbl[(*lptr).text]));
	
	printf("\t\t address = "); 
	pU8((*lptr).address); 
	printf("\n");
	
	printf("\t\t line = "); 
	pU4((*lptr).line); 
	printf("\n");
	return;

}/*end printLabelRec*/

⌨️ 快捷键说明

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