idbg.c 3.01 KB

#include "romloader.h"
#include "arcs.h"
#include <idbg.h>

#define MAXIDBGCMDS 30
#define DO_ENV 0x45454545      /* first record */

extern int      _ArcsLoad(char *, unsigned long, int (**)(), unsigned long *);
extern int sprintf(char *, const char *, ...);
static void init_str(struct string_list *);
static new_str1(char *, register struct string_list *);
void _GetSymTable(int *,int *);
void _GetSymSize(int *);

int _MaxSyms = SYMMAX;
int _NameTabSize = NAMESIZ;
struct dbstbl _DbgTab[SYMMAX] = {0};
char _NameTab[NAMESIZ] = {0};
static dbgoff_t _CommondTab[MAXIDBGCMDS];

static struct string_list _DebuggerArgv;


void
_LoadDebugger(int argc, char **argv, void *start_pc)
{
	char *colon, *symName = "symmon.IP22";
	int (*init_pc)(int, char **, char **, void *);
	char pcstring[24];
	char bootPath[64];
	long error;
	unsigned long lowaddr;
	char **strptrs;

	strcpy(bootPath, argv[0]);
	if ((colon = strchr(bootPath, ':')) != NULL)
		strcpy(colon+1, symName);
	else if ((colon = strchr(bootPath, ')')) != NULL)
		strcpy(colon+1, symName);
	else
		strcpy(bootPath, symName);

	/* put the addr for the debugger to return to into the environ */

	sprintf (pcstring, "startpc=0x%x", start_pc);
	init_str(&_DebuggerArgv);
	new_str1(pcstring, &_DebuggerArgv);

	/* load the symmon */

	if ((error = _ArcsLoad(bootPath, 0, &init_pc, &lowaddr)) != 0) {
		_ArcsPerror(error, bootPath);
		return;
	}
	
	(*init_pc)(_DebuggerArgv.strcnt, _DebuggerArgv.strptrs, 0, 0);
}

void
_SetupDebugger(char *romPath)
{
	unsigned long romFD, count;
	long error;
	dbgoff_t *dp;

	/* download symbol tables */

	strcat(romPath, ".sym");
	
	if (((error = _ArcsOpen(romPath, OpenReadOnly, &romFD)) != 0) ||
	    ((error = _ArcsRead(romFD,
		     (void *)&_DbgTab, _NameTabSize+_MaxSyms, &count)) != 0)){
		return;
	}
	
	/* 
	 * initialize the debug block which contains a pointer 
	 * to _CommondTab[MAXIDBGCMDS]. _CommondTab is the table of
	 * commands that symmon calls
	 */

	dp = _CommondTab;

	if (SPB->DebugBlock) {
		db_t *d = (db_t *) SPB->DebugBlock;
		d->db_idbgbase = (int)dp;
	}

	dp->s_type = DO_ENV;
	dp++;

	/* Give dbgmon address of _DbgTab & _NameTab */
	dp->s_func = _GetSymTable;
	strcpy(dp->s_name, "symtab");
	dp++;

	/* Give dbgmon the size of _DbgTab */
	dp->s_func = _GetSymSize;
	strcpy(dp->s_name, "symsize");
	dp++;
	
}


/*
 * init_str -- initialize string_list
 */
static void
init_str(register struct string_list *slp)
{
	slp->strp = slp->strbuf;
	slp->strcnt = 0;
	slp->strptrs[0] = 0;
}

/*
 * new_str1 -- add new string to string list
 */
static
new_str1(char *strp, register struct string_list *slp)
{
	register int len;

	if (slp->strcnt >= MAXSTRINGS - 1) {
		return(-1);
	}

	len = strlen(strp) + 1;
	if (slp->strp + len >= &slp->strbuf[STRINGBYTES]) {
		return(-1);
	}

	slp->strptrs[slp->strcnt++] = slp->strp;
	slp->strptrs[slp->strcnt] = 0;
	strcpy(slp->strp, strp);
	slp->strp += len;
	return(0);
}

void
_GetSymTable(int *db,int *name )
{
	*db = (int)&_DbgTab[0];
	*name = (int)&_NameTab[0];
}

void
_GetSymSize( int *size )
{
	*size = (int)_MaxSyms;
}