www.pudn.com > HEC-linux.zip > dbgdata.c
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ dbgdata.c reads executable, populates debug data structures +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ macros +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#define SIZE_HEADER 26 /* byte size of executable header */
#define SIZE_GLOBREC 37 /* byte size of global record */
#define SIZE_PROCREC 25 /* byte size of procedure record */
#define SIZE_RETREC 16 /* byte size of return value record */
#define SIZE_ARGREC 16 /* byte size of argument record */
#define SIZE_LOCREC 16 /* byte size of local variable record */
#define SIZE_LBLREC 20 /* byte size of label record */
#define MAX_RECSIZE SIZE_GLOBREC
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ declarations +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
struct HeaderRec
{
U2 magic; /*should be magic number 0xDEED */
U8 szSymTbl; /*size of symbol table*/
U8 szStrTbl; /*size of string table*/
U8 szByteCode; /*size of bytecode*/
};
struct Contents
{
U4 nGlobVarRec; /*number of global variable records in symbol table*/
U4 nProcRec; /*number of procedure records in symbol table*/
};
#define SZ_BYTE 1 /*used to indicate GlobVar type*/
#define SZ_WORD 2
#define SZ_DWORD 4
#define SZ_QWORD 8
char *globSz[]={"","SZ_BYTE","SZ_WORD","","SZ_DWORD",
"","","","SZ_QWORD"};
struct GlobVarRec
{
U8 text; /*index into StrTbl of where identifier begins*/
U1 dType; /* SZ_BYTE, SZ_WORD, SZ_DWORD, SZ_QWORD */
U8 len; /* # elements if array */
U8 size; /* total byte size */
S8 offset; /* offset below $TOP, address(g) = $TOP - offset*/
U4 line; /* source code line containing declaration */
};
struct StackFrameRec
{
U8 text; /* index into StrTbl of where identifier begins */
S4 fpOffset; /* +n or -n from $FP */
U4 line; /* source code line containing declaration */
};
struct LabelRec
{
U8 text; /* index into StrTbl of where identifier begins */
U8 address; /* address of label*/
U4 line; /* source code line containing declaration */
};
struct ProcRec
{
U8 text; /* index into StrTbl of where identifier begins */
U8 address; /* address of procedure */
U4 line; /* source code line containing declaration */
struct StackFrameRec ret;
U1 nRet; /* 0 = void return, 1 = returns a value*/
struct StackFrameRec *arg;
U1 nArg;
struct StackFrameRec *local;
U1 nLocal;
struct LabelRec *label;
U2 nLabel;
};
struct DebugData
{
struct Contents contents;
struct GlobVarRec *gvRec;
struct ProcRec *pRec;
U1 *strTbl;
};
U1 debug; /* TRUE if VM is in debug mode */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ private variables +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
struct DebugData debugData;
U1 debugbytes[MAX_RECSIZE]; /*holds data read in for single record*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ public prototypes +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void printDebugData(struct DebugData *ptr);
void populateDebugData(struct HeaderRec *hptr, struct DebugData *ptr, FILE *fptr);
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ private prototypes +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void printProcRec(struct ProcRec *pptr, struct DebugData *ptr);
void printStackFrameRec(struct StackFrameRec *sptr, struct DebugData *ptr);
void printLabelRec(struct LabelRec *lptr, struct DebugData *ptr);
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);
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ definitions +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void printDebugData(struct DebugData *ptr)
{
U8 i;
for(i=0;i<((*ptr).contents).nGlobVarRec;i++)
{
printf("GLOBVAR "); pU8(i); printf("\n");
printf("\tid=%s\n",&((*ptr).strTbl[(*ptr).gvRec[i].text]));
printf("\ttype=%s\n",globSz[(*ptr).gvRec[i].dType]);
printf("\tlen="); pU8((*ptr).gvRec[i].len); printf("\n");
printf("\tsize="); pU8((*ptr).gvRec[i].size); printf("\n");
printf("\toffset="); pS8((*ptr).gvRec[i].offset); printf("\n");
printf("\tline="); 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("\tid=%s\n",&((*ptr).strTbl[(*pptr).text]));
printf("\taddress="); pU8((*pptr).address); printf("\n");
printf("\tline="); pU4((*pptr).line); printf("\n");
if((*pptr).nRet)
{
printf("\tRET\n");
printStackFrameRec(&((*pptr).ret), ptr);
}
for(i=0;i<(*pptr).nArg;i++)
{
printf("\tARG->"); pU4(i); printf("\n");
printStackFrameRec(&((*pptr).arg[i]), ptr);
}
for(i=0;i<(*pptr).nLocal;i++)
{
printf("\tLOCAL->"); pU4(i); printf("\n");
printStackFrameRec(&((*pptr).local[i]), ptr);
}
for(i=0;i<(*pptr).nLabel;i++)
{
printf("\tLABEL->"); pU4(i); printf("\n");
printLabelRec(&((*pptr).label[i]), ptr);
}
return;
}/*end printProcRec*/
/*-----------------------------------------------------------------*/
void printStackFrameRec(struct StackFrameRec *sptr, struct DebugData *ptr)
{
printf("\t\tid=%s\n",&((*ptr).strTbl[(*sptr).text]));
printf("\t\tfpOffset="); pS4((*sptr).fpOffset); printf("\n");
printf("\t\tline="); pU4((*sptr).line); printf("\n");
return;
}/*end printStackFrameRec*/
/*-----------------------------------------------------------------*/
void printLabelRec(struct LabelRec *lptr, struct DebugData *ptr)
{
printf("\t\tid=%s\n",&((*ptr).strTbl[(*lptr).text]));
printf("\t\taddress="); pU8((*lptr).address); printf("\n");
printf("\t\tline="); pU4((*lptr).line); printf("\n");
return;
}/*end printLabelRec*/
/*-----------------------------------------------------------------*/
void populateDebugData(struct HeaderRec *hptr, struct DebugData *ptr, FILE *fptr)
{
U8 i;
U4 np;
U4 ng;
/*1) read header, skip population if zero sizes */
(*hptr) = readHeaderRec(fptr);
if(((*hptr).szSymTbl==0)&&((*hptr).szStrTbl==0))
{
(*ptr).contents.nGlobVarRec = 0;
(*ptr).contents.nProcRec = 0;
return;
}
/*2) read table of contents, info on number of globs and procs */
(*ptr).contents = readContents(fptr);
ng = ((*ptr).contents).nGlobVarRec;
np = ((*ptr).contents).nProcRec;
/*3) based on contents info, allocate globs and procs */
(*ptr).gvRec = (struct GlobVarRec*)malloc(sizeof(struct GlobVarRec)*ng);
if((*ptr).gvRec==NULL)
{
FCLOSE(fptr);
FATAL_ERROR1("could not allocate space for global variable records");
}
(*ptr).pRec = (struct ProcRec*)malloc(sizeof(struct ProcRec)*np);
if((*ptr).pRec==NULL)
{
FCLOSE(fptr);
FATAL_ERROR1("could not allocate space for procedure records");
}
/*4) read in globvar recs following contents */
for(i=0;i\n");
printf("\tmagic=%lX\n",hr.magic);
printf("\tbytes in symbol table %lu\n",hr.szSymTbl);
printf("\tbytes in string table %lu\n",hr.szStrTbl);
printf("\tbytes in bytecode %lu\n",hr.szByteCode);
printDebugData(&debugData);
return;
}*//*end main*/