#include #include #include #include #include #define Extern extern #include "mips.h" char buf[128], lastcmd[128]; char fmt = 'X'; int width = 60; int inc; ulong expr(char*); ulong expr1(char*); char* term(char*, ulong*); char * nextc(char *p) { while(*p && (*p == ' ' || *p == '\t') && *p != '\n') p++; if(*p == '\n') *p = '\0'; return p; } char * numsym(char *addr, ulong *val) { char tsym[128], *t; static char *delim = "`'<>/\\@*|-~+-/=?\n"; Symbol s; char c; t = tsym; while(c = *addr) { if(strchr(delim, c)) break; *t++ = c; addr++; } t[0] = '\0'; if(strcmp(tsym, ".") == 0) { *val = dot; return addr; } if(lookup(0, tsym, &s)) *val = s.value; else { if(tsym[0] == '#') *val = strtoul(tsym+1, 0, 16); else *val = strtoul(tsym, 0, 0); } return addr; } ulong expr(char *addr) { ulong t, t2; char op; if(*addr == '\0') return dot; addr = numsym(addr, &t); if(*addr == '\0') return t; addr = nextc(addr); op = *addr++; numsym(addr, &t2); switch(op) { default: Bprint(bioout, "expr syntax\n"); return 0; case '+': t += t2; break; case '-': t -= t2; break; case '%': t /= t2; break; case '&': t &= t2; break; case '|': t |= t2; break; } return t; } int buildargv(char *str, char **args, int max) { int na = 0; while (na < max) { while((*str == ' ' || *str == '\t' || *str == '\n') && *str != '\0') str++; if(*str == '\0') return na; args[na++] = str; while(!(*str == ' ' || *str == '\t'|| *str == '\n') && *str != '\0') str++; if(*str == '\n') *str = '\0'; if(*str == '\0') break; *str++ = '\0'; } return na; } void colon(char *addr, char *cp) { int argc; char *argv[100]; char tbuf[512]; cp = nextc(cp); switch(*cp) { default: Bprint(bioout, "?\n"); return; case 'b': breakpoint(addr, cp+1); return; case 'd': delbpt(addr); return; /* These fall through to print the stopped address */ case 'r': reset(); argc = buildargv(cp+1, argv, 100); initstk(argc, argv); count = 0; atbpt = 0; run(); break; case 'c': count = 0; atbpt = 0; run(); break; case 's': cp = nextc(cp+1); count = 0; if(*cp) count = strtoul(cp, 0, 0); if(count == 0) count = 1; atbpt = 0; run(); break; } dot = reg.pc; Bprint(bioout, "%s at #%lux ", atbpt ? "breakpoint" : "stopped", dot); symoff(tbuf, sizeof(tbuf), dot, CTEXT); Bprint(bioout, tbuf); if(fmt == 'z') printsource(dot); Bprint(bioout, "\n"); } void dollar(char *cp) { int nr; cp = nextc(cp); switch(*cp) { default: Bprint(bioout, "?\n"); break; case 'c': stktrace(*cp); break; case 'C': stktrace(*cp); break; case 'b': dobplist(); break; case 'r': dumpreg(); break; case 'R': dumpreg(); case 'f': dumpfreg(); break; case 'F': dumpdreg(); break; case 'q': exits(0); break; case 'Q': isum(); tlbsum(); segsum(); break; case 't': cp++; switch(*cp) { default: Bprint(bioout, "$t[0sicr#]\n"); break; case '\0': trace = 1; break; case '0': trace = 0; sysdbg = 0; calltree = 0; break; case 's': sysdbg = 1; break; case 'i': trace = 1; break; case 'c': calltree = 1; break; case 'r': nr = atoi(cp+1); if(nr < 0 || nr > 31) { print("bad register\n"); break; } rtrace ^= (1< 0) inc = -inc; break; case 'z': if (findsym(dot, CTEXT, &s)) Bprint(bioout, " %s() ", s.name); printsource(dot); inc = 0; break; } return c; } void eval(char *addr, char *p) { ulong val; val = expr(addr); p = nextc(p); if(*p == '\0') { p[0] = fmt; p[1] = '\0'; } pfmt(*p, 0, val); Bprint(bioout, "\n"); } void quesie(char *p) { int c, count, i; char tbuf[512]; c = 0; symoff(tbuf, sizeof(tbuf), dot, CTEXT); Bprint(bioout, "%s?\t", tbuf); while(*p) { p = nextc(p); if(*p == '"') { for(p++; *p && *p != '"'; p++) { Bputc(bioout, *p); c++; } if(*p) p++; continue; } count = 0; while(*p >= '0' && *p <= '9') count = count*10 + (*p++ - '0'); if(count == 0) count = 1; p = nextc(p); if(*p == '\0') { p[0] = fmt; p[1] = '\0'; } for(i = 0; i < count; i++) { c += pfmt(*p, 1, 0); dot += inc; if(c > width) { Bprint(bioout, "\n"); symoff(tbuf, sizeof(tbuf), dot, CTEXT); Bprint(bioout, "%s?\t", tbuf); c = 0; } } fmt = *p++; p = nextc(p); } Bprint(bioout, "\n"); } void catcher(void *a, char *msg) { USED(a); if(strcmp(msg, "interrupt") != 0) noted(NDFLT); count = 1; print("vi\n"); noted(NCONT); } void setreg(char *addr, char *cp) { int rn; dot = expr(addr); cp = nextc(cp); if(strcmp(cp, "pc") == 0) { reg.pc = dot; return; } if(strcmp(cp, "sp") == 0) { reg.r[29] = dot; return; } if(strcmp(cp, "mh") == 0) { reg.mhi = dot; return; } if(strcmp(cp, "ml") == 0) { reg.mlo = dot; return; } if(*cp++ == 'r') { rn = strtoul(cp, 0, 10); if(rn > 0 && rn < 32) { reg.r[rn] = dot; return; } } Bprint(bioout, "bad register\n"); } void cmd(void) { char *p, *a, *cp, *gotint; char addr[128]; static char *cmdlet = ":$?/=>"; int n, i; notify(catcher); dot = reg.pc; setjmp(errjmp); for(;;) { Bflush(bioout); p = buf; n = 0; while(n < sizeof(buf)) { i = Bgetc(bin); if(i < 0) exits(0); *p++ = i; n++; if(i == '\n') break; } if(buf[0] == '\n') strcpy(buf, lastcmd); else { buf[n-1] = '\0'; strcpy(lastcmd, buf); } p = buf; a = addr; for(;;) { p = nextc(p); if(*p == 0 || strchr(cmdlet, *p)) break; *a++ = *p++; } *a = '\0'; cmdcount = 1; cp = strchr(addr, ','); if(cp != 0) { if(cp[1] == '#') cmdcount = strtoul(cp+2, &gotint, 16); else cmdcount = strtoul(cp+1, &gotint, 0); *cp = '\0'; } switch(*p) { case '$': dollar(p+1); break; case ':': colon(addr, p+1); break; case '/': case '?': dot = expr(addr); for(i = 0; i < cmdcount; i++) quesie(p+1); break; case '=': eval(addr, p+1); break; case '>': setreg(addr, p+1); break; default: Bprint(bioout, "?\n"); break; } } }