112546Scsvaf 29678Slinton /* Copyright (c) 1982 Regents of the University of California */ 39678Slinton 4*14620Ssam static char sccsid[] = "@(#)runtime.vax.c 1.9 08/14/83"; 59678Slinton 69678Slinton /* 79678Slinton * Runtime organization dependent routines, mostly dealing with 89678Slinton * activation records. 99678Slinton */ 109678Slinton 119678Slinton #include "defs.h" 129678Slinton #include "runtime.h" 139678Slinton #include "process.h" 149678Slinton #include "machine.h" 159678Slinton #include "events.h" 169678Slinton #include "mappings.h" 179678Slinton #include "symbols.h" 189678Slinton #include "tree.h" 199678Slinton #include "eval.h" 209678Slinton #include "operators.h" 219678Slinton #include "object.h" 2212546Scsvaf #include <sys/param.h> 239678Slinton 249678Slinton #ifndef public 259678Slinton typedef struct Frame *Frame; 269678Slinton 279678Slinton #include "machine.h" 289678Slinton #endif 299678Slinton 309678Slinton #define NSAVEREG 12 319678Slinton 329678Slinton struct Frame { 339678Slinton Integer condition_handler; 349678Slinton Integer mask; 359678Slinton Address save_ap; /* argument pointer */ 369678Slinton Address save_fp; /* frame pointer */ 379678Slinton Address save_pc; /* program counter */ 389678Slinton Word save_reg[NSAVEREG]; /* not necessarily there */ 399678Slinton }; 409678Slinton 419678Slinton private Boolean walkingstack = false; 429678Slinton 439678Slinton /* 449678Slinton * Set a frame to the current activation record. 459678Slinton */ 469678Slinton 479678Slinton private getcurframe(frp) 489678Slinton register Frame frp; 499678Slinton { 509678Slinton register int i; 519678Slinton 529678Slinton checkref(frp); 539678Slinton frp->mask = reg(NREG); 549678Slinton frp->save_ap = reg(ARGP); 559678Slinton frp->save_fp = reg(FRP); 5612051Slinton frp->save_pc = reg(PROGCTR) + 1; 579678Slinton for (i = 0; i < NSAVEREG; i++) { 589678Slinton frp->save_reg[i] = reg(i); 599678Slinton } 609678Slinton } 619678Slinton 629678Slinton /* 639678Slinton * Return a pointer to the next activation record up the stack. 649678Slinton * Return nil if there is none. 659678Slinton * Writes over space pointed to by given argument. 669678Slinton */ 679678Slinton 689678Slinton #define bis(b, n) ((b & (1 << (n))) != 0) 699678Slinton 709678Slinton private Frame nextframe(frp) 719678Slinton Frame frp; 729678Slinton { 739678Slinton register Frame newfrp; 749678Slinton struct Frame frame; 759678Slinton register Integer i, j, mask; 7612546Scsvaf Address prev_frame, callpc; 7713937Slinton static Integer ntramp = 0; 789678Slinton 799678Slinton newfrp = frp; 8012546Scsvaf prev_frame = frp->save_fp; 8112546Scsvaf 8213937Slinton /* 8313937Slinton * The check for interrupt generated frames is taken from adb with only 8413937Slinton * partial understanding. If you're in "sub" and on a sigxxx "sigsub" 8513937Slinton * gets control, then the stack does NOT look like <main, sub, sigsub>. 8612546Scsvaf * 8712546Scsvaf * As best I can make out it looks like: 8812546Scsvaf * 8913937Slinton * <main, (machine check exception block + sub), sysframe, sigsub>. 9013937Slinton * 9113937Slinton * When the signal occurs an exception block and a frame for the routine 9213937Slinton * in which it occured are pushed on the user stack. Then another frame 9313937Slinton * is pushed corresponding to a call from the kernel to sigsub. 9413937Slinton * 9512546Scsvaf * The addr in sub at which the exception occured is not in sub.save_pc 9613937Slinton * but in the machine check exception block. It is at the magic address 97*14620Ssam * fp + 84. 9812546Scsvaf * 9912546Scsvaf * The current approach ignores the sys_frame (what adb reports as sigtramp) 10013937Slinton * and takes the pc for sub from the exception block. This allows the 10113937Slinton * "where" command to report <main, sub, sigsub>, which seems reasonable. 10212546Scsvaf */ 10312546Scsvaf 10413937Slinton nextf: 10513937Slinton dread(&frame, prev_frame, sizeof(struct Frame)); 10613937Slinton if (ntramp == 1) { 107*14620Ssam dread(&callpc, prev_frame + 84, sizeof(callpc)); 10813937Slinton } else { 10913937Slinton callpc = frame.save_pc; 11013937Slinton } 1119678Slinton if (frame.save_fp == nil) { 1129678Slinton newfrp = nil; 11313937Slinton } else if (callpc > 0x80000000 - 0x200 * UPAGES ) { 11412546Scsvaf ntramp++; 11512546Scsvaf prev_frame = frame.save_fp; 11612546Scsvaf goto nextf; 11713937Slinton } else { 11812546Scsvaf frame.save_pc = callpc; 11913937Slinton ntramp = 0; 1209678Slinton mask = ((frame.mask >> 16) & 0x0fff); 1219678Slinton j = 0; 1229678Slinton for (i = 0; i < NSAVEREG; i++) { 1239678Slinton if (bis(mask, i)) { 1249678Slinton newfrp->save_reg[i] = frame.save_reg[j]; 1259678Slinton ++j; 1269678Slinton } 1279678Slinton } 1289678Slinton newfrp->condition_handler = frame.condition_handler; 1299678Slinton newfrp->mask = mask; 1309678Slinton newfrp->save_ap = frame.save_ap; 1319678Slinton newfrp->save_fp = frame.save_fp; 1329678Slinton newfrp->save_pc = frame.save_pc; 1339678Slinton } 1349678Slinton return newfrp; 1359678Slinton } 1369678Slinton 1379678Slinton /* 1389678Slinton * Return the frame associated with the given function. 1399678Slinton * If the function is nil, return the most recently activated frame. 1409678Slinton * 1419678Slinton * Static allocation for the frame. 1429678Slinton */ 1439678Slinton 1449678Slinton public Frame findframe(f) 1459678Slinton Symbol f; 1469678Slinton { 1479678Slinton register Frame frp; 1489678Slinton static struct Frame frame; 14911866Slinton Symbol p; 15011866Slinton Boolean done; 1519678Slinton 1529678Slinton frp = &frame; 1539678Slinton getcurframe(frp); 1549678Slinton if (f != nil) { 15511866Slinton done = false; 15611866Slinton do { 15711866Slinton p = whatblock(frp->save_pc); 15811866Slinton if (p == f) { 15911866Slinton done = true; 16011866Slinton } else if (p == program) { 16111866Slinton done = true; 16211866Slinton frp = nil; 16311866Slinton } else { 16411866Slinton frp = nextframe(frp); 16511866Slinton if (frp == nil) { 16611866Slinton done = true; 16711866Slinton } 16811866Slinton } 16911866Slinton } while (not done); 1709678Slinton } 1719678Slinton return frp; 1729678Slinton } 1739678Slinton 1749678Slinton /* 1759678Slinton * Find the return address of the current procedure/function. 1769678Slinton */ 1779678Slinton 1789678Slinton public Address return_addr() 1799678Slinton { 1809678Slinton Frame frp; 1819678Slinton Address addr; 1829678Slinton struct Frame frame; 1839678Slinton 1849678Slinton frp = &frame; 1859678Slinton getcurframe(frp); 1869678Slinton frp = nextframe(frp); 1879678Slinton if (frp == nil) { 1889678Slinton addr = 0; 1899678Slinton } else { 1909678Slinton addr = frp->save_pc; 1919678Slinton } 1929678Slinton return addr; 1939678Slinton } 1949678Slinton 1959678Slinton /* 1969678Slinton * Push the value associated with the current function. 1979678Slinton */ 1989678Slinton 1999678Slinton public pushretval(len, isindirect) 2009678Slinton Integer len; 2019678Slinton Boolean isindirect; 2029678Slinton { 2039678Slinton Word r0; 2049678Slinton 2059678Slinton r0 = reg(0); 2069678Slinton if (isindirect) { 2079678Slinton rpush((Address) r0, len); 2089678Slinton } else { 2099678Slinton switch (len) { 2109678Slinton case sizeof(char): 2119678Slinton push(char, r0); 2129678Slinton break; 2139678Slinton 2149678Slinton case sizeof(short): 2159678Slinton push(short, r0); 2169678Slinton break; 2179678Slinton 2189678Slinton default: 2199678Slinton if (len == sizeof(Word)) { 2209678Slinton push(Word, r0); 2219678Slinton } else if (len == 2*sizeof(Word)) { 2229678Slinton push(Word, r0); 2239678Slinton push(Word, reg(1)); 2249678Slinton } else { 2259678Slinton panic("not indirect in pushretval?"); 2269678Slinton } 2279678Slinton break; 2289678Slinton } 2299678Slinton } 2309678Slinton } 2319678Slinton 2329678Slinton /* 2339678Slinton * Return the base address for locals in the given frame. 2349678Slinton */ 2359678Slinton 2369678Slinton public Address locals_base(frp) 2379678Slinton register Frame frp; 2389678Slinton { 2399678Slinton return (frp == nil) ? reg(FRP) : frp->save_fp; 2409678Slinton } 2419678Slinton 2429678Slinton /* 2439678Slinton * Return the base address for arguments in the given frame. 2449678Slinton */ 2459678Slinton 2469678Slinton public Address args_base(frp) 2479678Slinton register Frame frp; 2489678Slinton { 2499678Slinton return (frp == nil) ? reg(ARGP) : frp->save_ap; 2509678Slinton } 2519678Slinton 2529678Slinton /* 2539678Slinton * Return saved register n from the given frame. 2549678Slinton */ 2559678Slinton 2569678Slinton public Word savereg(n, frp) 2579678Slinton register Integer n; 2589678Slinton register Frame frp; 2599678Slinton { 2609678Slinton register Word w; 2619678Slinton 2629678Slinton if (frp == nil) { 2639678Slinton w = reg(n); 2649678Slinton } else { 2659678Slinton switch (n) { 2669678Slinton case ARGP: 2679678Slinton w = frp->save_ap; 2689678Slinton break; 2699678Slinton 2709678Slinton case FRP: 2719678Slinton w = frp->save_fp; 2729678Slinton break; 2739678Slinton 2749678Slinton case STKP: 2759678Slinton w = reg(STKP); 2769678Slinton break; 2779678Slinton 2789678Slinton case PROGCTR: 2799678Slinton w = frp->save_pc; 2809678Slinton break; 2819678Slinton 2829678Slinton default: 2839678Slinton assert(n >= 0 and n < NSAVEREG); 2849678Slinton w = frp->save_reg[n]; 2859678Slinton break; 2869678Slinton } 2879678Slinton } 2889678Slinton return w; 2899678Slinton } 2909678Slinton 2919678Slinton /* 2929678Slinton * Return the nth argument to the current procedure. 2939678Slinton */ 2949678Slinton 2959678Slinton public Word argn(n, frp) 2969678Slinton Integer n; 2979678Slinton Frame frp; 2989678Slinton { 2999678Slinton Word w; 3009678Slinton 3019678Slinton dread(&w, args_base(frp) + (n * sizeof(Word)), sizeof(w)); 3029678Slinton return w; 3039678Slinton } 3049678Slinton 3059678Slinton /* 3069678Slinton * Calculate the entry address for a procedure or function parameter, 3079678Slinton * given the address of the descriptor. 3089678Slinton */ 3099678Slinton 3109678Slinton public Address fparamaddr(a) 3119678Slinton Address a; 3129678Slinton { 3139678Slinton Address r; 3149678Slinton 3159678Slinton dread(&r, a, sizeof(r)); 3169678Slinton return r; 3179678Slinton } 3189678Slinton 3199678Slinton /* 3209678Slinton * Print a list of currently active blocks starting with most recent. 3219678Slinton */ 3229678Slinton 3239678Slinton public wherecmd() 3249678Slinton { 3259678Slinton walkstack(false); 3269678Slinton } 3279678Slinton 3289678Slinton /* 3299678Slinton * Dump the world to the given file. 3309678Slinton * Like "where", but variables are dumped also. 3319678Slinton */ 3329678Slinton 3339678Slinton public dump() 3349678Slinton { 3359678Slinton walkstack(true); 3369678Slinton } 3379678Slinton 3389678Slinton /* 3399678Slinton * Walk the stack of active procedures printing information 3409678Slinton * about each active procedure. 3419678Slinton */ 3429678Slinton 3439678Slinton private walkstack(dumpvariables) 3449678Slinton Boolean dumpvariables; 3459678Slinton { 3469678Slinton register Frame frp; 3479678Slinton register Symbol f; 3489678Slinton register Boolean save; 3499678Slinton register Lineno line; 3509678Slinton struct Frame frame; 3519678Slinton 3529678Slinton if (notstarted(process)) { 3539678Slinton error("program is not active"); 3549678Slinton } else { 3559678Slinton save = walkingstack; 3569678Slinton walkingstack = true; 3579678Slinton frp = &frame; 3589678Slinton getcurframe(frp); 3599678Slinton f = whatblock(frp->save_pc); 3609678Slinton do { 3619678Slinton printf("%s", symname(f)); 36214442Slinton if (not isinline(f)) { 36314442Slinton printparams(f, frp); 36414442Slinton } 3659841Slinton line = srcline(frp->save_pc - 1); 3669678Slinton if (line != 0) { 3679678Slinton printf(", line %d", line); 3689841Slinton printf(" in \"%s\"\n", srcfilename(frp->save_pc - 1)); 3699678Slinton } else { 3709678Slinton printf(" at 0x%x\n", frp->save_pc); 3719678Slinton } 3729678Slinton if (dumpvariables) { 3739678Slinton dumpvars(f, frp); 3749678Slinton putchar('\n'); 3759678Slinton } 37614442Slinton if (isinline(f)) { 37714442Slinton f = container(f); 37814442Slinton } else { 37914442Slinton frp = nextframe(frp); 38014442Slinton if (frp != nil) { 38114442Slinton f = whatblock(frp->save_pc); 38214442Slinton } 3839678Slinton } 38411866Slinton } while (frp != nil and f != program); 3859678Slinton if (dumpvariables) { 3869678Slinton printf("in \"%s\":\n", symname(program)); 3879678Slinton dumpvars(program, nil); 3889678Slinton putchar('\n'); 3899678Slinton } 3909678Slinton walkingstack = save; 3919678Slinton } 3929678Slinton } 3939678Slinton 3949678Slinton /* 3959678Slinton * Find the entry point of a procedure or function. 3969678Slinton */ 3979678Slinton 3989678Slinton public findbeginning(f) 3999678Slinton Symbol f; 4009678Slinton { 4019678Slinton f->symvalue.funcv.beginaddr += 2; 4029678Slinton } 4039678Slinton 4049678Slinton /* 4059678Slinton * Return the address corresponding to the first line in a function. 4069678Slinton */ 4079678Slinton 4089678Slinton public Address firstline(f) 4099678Slinton Symbol f; 4109678Slinton { 4119678Slinton Address addr; 4129678Slinton 4139678Slinton addr = codeloc(f); 4149678Slinton while (linelookup(addr) == 0 and addr < objsize) { 4159678Slinton ++addr; 4169678Slinton } 4179678Slinton if (addr == objsize) { 4189678Slinton addr = -1; 4199678Slinton } 4209678Slinton return addr; 4219678Slinton } 4229678Slinton 4239678Slinton /* 4249678Slinton * Catcher drops strike three ... 4259678Slinton */ 4269678Slinton 4279678Slinton public runtofirst() 4289678Slinton { 4299678Slinton Address addr; 4309678Slinton 4319678Slinton addr = pc; 4329678Slinton while (linelookup(addr) == 0 and addr < objsize) { 4339678Slinton ++addr; 4349678Slinton } 4359678Slinton if (addr < objsize) { 4369678Slinton stepto(addr); 4379678Slinton } 4389678Slinton } 4399678Slinton 4409678Slinton /* 4419678Slinton * Return the address corresponding to the end of the program. 4429678Slinton * 4439678Slinton * We look for the entry to "exit". 4449678Slinton */ 4459678Slinton 4469678Slinton public Address lastaddr() 4479678Slinton { 4489678Slinton register Symbol s; 4499678Slinton 4509678Slinton s = lookup(identname("exit", true)); 4519678Slinton if (s == nil) { 4529678Slinton panic("can't find exit"); 4539678Slinton } 4549678Slinton return codeloc(s); 4559678Slinton } 4569678Slinton 4579678Slinton /* 4589678Slinton * Decide if the given function is currently active. 4599678Slinton * 4609678Slinton * We avoid calls to "findframe" during a stack trace for efficiency. 4619678Slinton * Presumably information evaluated while walking the stack is active. 4629678Slinton */ 4639678Slinton 4649678Slinton public Boolean isactive(f) 4659678Slinton Symbol f; 4669678Slinton { 4679678Slinton register Boolean b; 4689678Slinton 4699678Slinton if (isfinished(process)) { 4709678Slinton b = false; 4719678Slinton } else { 4729678Slinton if (walkingstack or f == program or 4739678Slinton (ismodule(f) and isactive(container(f)))) { 4749678Slinton b = true; 4759678Slinton } else { 4769678Slinton b = (Boolean) (findframe(f) != nil); 4779678Slinton } 4789678Slinton } 4799678Slinton return b; 4809678Slinton } 4819678Slinton 4829678Slinton /* 4839678Slinton * Evaluate a call to a procedure. 4849678Slinton */ 4859678Slinton 4869678Slinton public callproc(procnode, arglist) 4879678Slinton Node procnode; 4889678Slinton Node arglist; 4899678Slinton { 4909678Slinton Symbol proc; 4919678Slinton Integer argc; 4929678Slinton 4939678Slinton if (procnode->op != O_SYM) { 4949678Slinton beginerrmsg(); 4959678Slinton fprintf(stderr, "can't call \""); 4969678Slinton prtree(stderr, procnode); 4979678Slinton fprintf(stderr, "\""); 4989678Slinton enderrmsg(); 4999678Slinton } 5009678Slinton assert(procnode->op == O_SYM); 5019678Slinton proc = procnode->value.sym; 5029678Slinton if (not isblock(proc)) { 5039678Slinton error("\"%s\" is not a procedure or function", symname(proc)); 5049678Slinton } 5059678Slinton pushenv(); 5069678Slinton pc = codeloc(proc); 5079678Slinton argc = pushargs(proc, arglist); 5089678Slinton beginproc(proc, argc); 5099678Slinton isstopped = true; 5109678Slinton event_once(build(O_EQ, build(O_SYM, pcsym), build(O_SYM, retaddrsym)), 5119678Slinton buildcmdlist(build(O_PROCRTN, proc))); 5129678Slinton cont(); 5139678Slinton /* NOTREACHED */ 5149678Slinton } 5159678Slinton 5169678Slinton /* 5179678Slinton * Push the arguments on the process' stack. We do this by first 5189678Slinton * evaluating them on the "eval" stack, then copying into the process' 5199678Slinton * space. 5209678Slinton */ 5219678Slinton 5229678Slinton private Integer pushargs(proc, arglist) 5239678Slinton Symbol proc; 5249678Slinton Node arglist; 5259678Slinton { 5269678Slinton Stack *savesp; 5279678Slinton int argc, args_size; 5289678Slinton 5299678Slinton savesp = sp; 5309678Slinton argc = evalargs(proc, arglist); 5319678Slinton args_size = sp - savesp; 5329678Slinton setreg(STKP, reg(STKP) - args_size); 5339678Slinton dwrite(savesp, reg(STKP), args_size); 5349678Slinton sp = savesp; 5359678Slinton return argc; 5369678Slinton } 5379678Slinton 5389678Slinton /* 5399678Slinton * Evaluate arguments left-to-right. 5409678Slinton */ 5419678Slinton 5429678Slinton private Integer evalargs(proc, arglist) 5439678Slinton Symbol proc; 5449678Slinton Node arglist; 5459678Slinton { 5469678Slinton Node p, exp; 5479678Slinton Symbol arg; 5489678Slinton Stack *savesp; 5499678Slinton Address addr; 5509678Slinton Integer count; 5519678Slinton 5529678Slinton savesp = sp; 5539678Slinton count = 0; 5549678Slinton arg = proc->chain; 5559678Slinton for (p = arglist; p != nil; p = p->value.arg[1]) { 5569678Slinton if (p->op != O_COMMA) { 5579678Slinton panic("evalargs: arglist missing comma"); 5589678Slinton } 5599678Slinton if (arg == nil) { 5609678Slinton sp = savesp; 5619678Slinton error("too many parameters to %s", symname(proc)); 5629678Slinton } 5639678Slinton exp = p->value.arg[0]; 5649678Slinton if (not compatible(arg->type, exp->nodetype)) { 5659678Slinton sp = savesp; 5669678Slinton error("expression for parameter %s is of wrong type", symname(arg)); 5679678Slinton } 5689678Slinton if (arg->class == REF) { 5699678Slinton if (exp->op != O_RVAL) { 5709678Slinton sp = savesp; 5719678Slinton error("variable expected for parameter \"%s\"", symname(arg)); 5729678Slinton } 5739678Slinton addr = lval(exp->value.arg[0]); 5749678Slinton push(Address, addr); 5759678Slinton } else { 5769678Slinton eval(exp); 5779678Slinton } 5789678Slinton arg = arg->chain; 5799678Slinton ++count; 5809678Slinton } 5819678Slinton if (arg != nil) { 5829678Slinton sp = savesp; 5839678Slinton error("not enough parameters to %s", symname(proc)); 5849678Slinton } 5859678Slinton return count; 5869678Slinton } 5879678Slinton 5889678Slinton public procreturn(f) 5899678Slinton Symbol f; 5909678Slinton { 5919678Slinton flushoutput(); 5929678Slinton putchar('\n'); 5939678Slinton printname(stdout, f); 5949678Slinton printf(" returns successfully\n", symname(f)); 5959678Slinton popenv(); 5969678Slinton erecover(); 5979678Slinton } 5989678Slinton 5999678Slinton /* 6009678Slinton * Push the current environment. 6019678Slinton */ 6029678Slinton 6039678Slinton private pushenv() 6049678Slinton { 6059678Slinton push(Address, pc); 6069678Slinton push(Lineno, curline); 6079678Slinton push(String, cursource); 6089678Slinton push(Boolean, isstopped); 6099678Slinton push(Symbol, curfunc); 6109678Slinton push(Word, reg(PROGCTR)); 6119678Slinton push(Word, reg(STKP)); 6129678Slinton } 6139678Slinton 6149678Slinton /* 6159678Slinton * Pop back to the real world. 6169678Slinton */ 6179678Slinton 6189678Slinton public popenv() 6199678Slinton { 6209678Slinton register String filename; 6219678Slinton 6229678Slinton setreg(STKP, pop(Word)); 6239678Slinton setreg(PROGCTR, pop(Word)); 6249678Slinton curfunc = pop(Symbol); 6259678Slinton isstopped = pop(Boolean); 6269678Slinton filename = pop(String); 6279678Slinton curline = pop(Lineno); 6289678Slinton pc = pop(Address); 6299678Slinton setsource(filename); 6309678Slinton } 6319678Slinton 6329678Slinton /* 6339678Slinton * Flush the debuggee's standard output. 6349678Slinton * 6359678Slinton * This is VERY dependent on the use of stdio. 6369678Slinton */ 6379678Slinton 6389678Slinton public flushoutput() 6399678Slinton { 6409678Slinton register Symbol p, iob; 6419678Slinton register Stack *savesp; 6429678Slinton 6439678Slinton p = lookup(identname("fflush", true)); 6449678Slinton while (p != nil and not isblock(p)) { 6459678Slinton p = p->next_sym; 6469678Slinton } 6479678Slinton if (p != nil) { 6489678Slinton iob = lookup(identname("_iob", true)); 6499678Slinton if (iob != nil) { 6509678Slinton pushenv(); 6519678Slinton pc = codeloc(p); 6529678Slinton savesp = sp; 6539678Slinton push(long, address(iob, nil) + sizeof(struct _iobuf)); 6549678Slinton setreg(STKP, reg(STKP) - sizeof(long)); 6559678Slinton dwrite(savesp, reg(STKP), sizeof(long)); 6569678Slinton sp = savesp; 6579678Slinton beginproc(p, 1); 6589678Slinton stepto(return_addr()); 6599678Slinton popenv(); 6609678Slinton } 6619678Slinton } 6629678Slinton } 663