runtime.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /* Copyright (c) 1982 Regents of the University of California */
  2. static char sccsid[] = "@(#)runtime.c 1.9 8/14/83";
  3. /*
  4. * Runtime organization dependent routines, mostly dealing with
  5. * activation records.
  6. */
  7. #include "defs.h"
  8. #include "runtime.h"
  9. #include "process.h"
  10. #include "machine.h"
  11. #include "events.h"
  12. #include "mappings.h"
  13. #include "symbols.h"
  14. #include "tree.h"
  15. #include "eval.h"
  16. #include "operators.h"
  17. #include "object.h"
  18. #include <sys/param.h>
  19. #ifndef public
  20. typedef struct Frame *Frame;
  21. #include "machine.h"
  22. #endif
  23. #define NSAVEREG 12
  24. struct Frame {
  25. Integer condition_handler;
  26. Integer mask;
  27. Address save_ap; /* argument pointer */
  28. Address save_fp; /* frame pointer */
  29. Address save_pc; /* program counter */
  30. Word save_reg[NSAVEREG]; /* not necessarily there */
  31. };
  32. private Boolean walkingstack = false;
  33. /*
  34. * Set a frame to the current activation record.
  35. */
  36. private getcurframe(frp)
  37. register Frame frp;
  38. {
  39. register int i;
  40. checkref(frp);
  41. frp->mask = reg(NREG);
  42. frp->save_ap = reg(ARGP);
  43. frp->save_fp = reg(FRP);
  44. frp->save_pc = reg(PROGCTR) + 1;
  45. for (i = 0; i < NSAVEREG; i++) {
  46. frp->save_reg[i] = reg(i);
  47. }
  48. }
  49. /*
  50. * Return a pointer to the next activation record up the stack.
  51. * Return nil if there is none.
  52. * Writes over space pointed to by given argument.
  53. */
  54. #define bis(b, n) ((b & (1 << (n))) != 0)
  55. private Frame nextframe(frp)
  56. Frame frp;
  57. {
  58. register Frame newfrp;
  59. struct Frame frame;
  60. register Integer i, j, mask;
  61. Address prev_frame, callpc;
  62. static Integer ntramp = 0;
  63. newfrp = frp;
  64. prev_frame = frp->save_fp;
  65. /*
  66. * The check for interrupt generated frames is taken from adb with only
  67. * partial understanding. If you're in "sub" and on a sigxxx "sigsub"
  68. * gets control, then the stack does NOT look like <main, sub, sigsub>.
  69. *
  70. * As best I can make out it looks like:
  71. *
  72. * <main, (machine check exception block + sub), sysframe, sigsub>.
  73. *
  74. * When the signal occurs an exception block and a frame for the routine
  75. * in which it occured are pushed on the user stack. Then another frame
  76. * is pushed corresponding to a call from the kernel to sigsub.
  77. *
  78. * The addr in sub at which the exception occured is not in sub.save_pc
  79. * but in the machine check exception block. It is at the magic address
  80. * fp + 84.
  81. *
  82. * The current approach ignores the sys_frame (what adb reports as sigtramp)
  83. * and takes the pc for sub from the exception block. This allows the
  84. * "where" command to report <main, sub, sigsub>, which seems reasonable.
  85. */
  86. nextf:
  87. dread(&frame, prev_frame, sizeof(struct Frame));
  88. if (ntramp == 1) {
  89. dread(&callpc, prev_frame + 84, sizeof(callpc));
  90. } else {
  91. callpc = frame.save_pc;
  92. }
  93. if (frame.save_fp == nil) {
  94. newfrp = nil;
  95. } else if (callpc > 0x80000000 - 0x200 * UPAGES ) {
  96. ntramp++;
  97. prev_frame = frame.save_fp;
  98. goto nextf;
  99. } else {
  100. frame.save_pc = callpc;
  101. ntramp = 0;
  102. mask = ((frame.mask >> 16) & 0x0fff);
  103. j = 0;
  104. for (i = 0; i < NSAVEREG; i++) {
  105. if (bis(mask, i)) {
  106. newfrp->save_reg[i] = frame.save_reg[j];
  107. ++j;
  108. }
  109. }
  110. newfrp->condition_handler = frame.condition_handler;
  111. newfrp->mask = mask;
  112. newfrp->save_ap = frame.save_ap;
  113. newfrp->save_fp = frame.save_fp;
  114. newfrp->save_pc = frame.save_pc;
  115. }
  116. return newfrp;
  117. }
  118. /*
  119. * Return the frame associated with the given function.
  120. * If the function is nil, return the most recently activated frame.
  121. *
  122. * Static allocation for the frame.
  123. */
  124. public Frame findframe(f)
  125. Symbol f;
  126. {
  127. register Frame frp;
  128. static struct Frame frame;
  129. Symbol p;
  130. Boolean done;
  131. frp = &frame;
  132. getcurframe(frp);
  133. if (f != nil) {
  134. done = false;
  135. do {
  136. p = whatblock(frp->save_pc);
  137. if (p == f) {
  138. done = true;
  139. } else if (p == program) {
  140. done = true;
  141. frp = nil;
  142. } else {
  143. frp = nextframe(frp);
  144. if (frp == nil) {
  145. done = true;
  146. }
  147. }
  148. } while (not done);
  149. }
  150. return frp;
  151. }
  152. /*
  153. * Find the return address of the current procedure/function.
  154. */
  155. public Address return_addr()
  156. {
  157. Frame frp;
  158. Address addr;
  159. struct Frame frame;
  160. frp = &frame;
  161. getcurframe(frp);
  162. frp = nextframe(frp);
  163. if (frp == nil) {
  164. addr = 0;
  165. } else {
  166. addr = frp->save_pc;
  167. }
  168. return addr;
  169. }
  170. /*
  171. * Push the value associated with the current function.
  172. */
  173. public pushretval(len, isindirect)
  174. Integer len;
  175. Boolean isindirect;
  176. {
  177. Word r0;
  178. r0 = reg(0);
  179. if (isindirect) {
  180. rpush((Address) r0, len);
  181. } else {
  182. switch (len) {
  183. case sizeof(char):
  184. push(char, r0);
  185. break;
  186. case sizeof(short):
  187. push(short, r0);
  188. break;
  189. default:
  190. if (len == sizeof(Word)) {
  191. push(Word, r0);
  192. } else if (len == 2*sizeof(Word)) {
  193. push(Word, r0);
  194. push(Word, reg(1));
  195. } else {
  196. panic("not indirect in pushretval?");
  197. }
  198. break;
  199. }
  200. }
  201. }
  202. /*
  203. * Return the base address for locals in the given frame.
  204. */
  205. public Address locals_base(frp)
  206. register Frame frp;
  207. {
  208. return (frp == nil) ? reg(FRP) : frp->save_fp;
  209. }
  210. /*
  211. * Return the base address for arguments in the given frame.
  212. */
  213. public Address args_base(frp)
  214. register Frame frp;
  215. {
  216. return (frp == nil) ? reg(ARGP) : frp->save_ap;
  217. }
  218. /*
  219. * Return saved register n from the given frame.
  220. */
  221. public Word savereg(n, frp)
  222. register Integer n;
  223. register Frame frp;
  224. {
  225. register Word w;
  226. if (frp == nil) {
  227. w = reg(n);
  228. } else {
  229. switch (n) {
  230. case ARGP:
  231. w = frp->save_ap;
  232. break;
  233. case FRP:
  234. w = frp->save_fp;
  235. break;
  236. case STKP:
  237. w = reg(STKP);
  238. break;
  239. case PROGCTR:
  240. w = frp->save_pc;
  241. break;
  242. default:
  243. assert(n >= 0 and n < NSAVEREG);
  244. w = frp->save_reg[n];
  245. break;
  246. }
  247. }
  248. return w;
  249. }
  250. /*
  251. * Return the nth argument to the current procedure.
  252. */
  253. public Word argn(n, frp)
  254. Integer n;
  255. Frame frp;
  256. {
  257. Word w;
  258. dread(&w, args_base(frp) + (n * sizeof(Word)), sizeof(w));
  259. return w;
  260. }
  261. /*
  262. * Calculate the entry address for a procedure or function parameter,
  263. * given the address of the descriptor.
  264. */
  265. public Address fparamaddr(a)
  266. Address a;
  267. {
  268. Address r;
  269. dread(&r, a, sizeof(r));
  270. return r;
  271. }
  272. /*
  273. * Print a list of currently active blocks starting with most recent.
  274. */
  275. public wherecmd()
  276. {
  277. walkstack(false);
  278. }
  279. /*
  280. * Dump the world to the given file.
  281. * Like "where", but variables are dumped also.
  282. */
  283. public dump()
  284. {
  285. walkstack(true);
  286. }
  287. /*
  288. * Walk the stack of active procedures printing information
  289. * about each active procedure.
  290. */
  291. private walkstack(dumpvariables)
  292. Boolean dumpvariables;
  293. {
  294. register Frame frp;
  295. register Symbol f;
  296. register Boolean save;
  297. register Lineno line;
  298. struct Frame frame;
  299. if (notstarted(process)) {
  300. error("program is not active");
  301. } else {
  302. save = walkingstack;
  303. walkingstack = true;
  304. frp = &frame;
  305. getcurframe(frp);
  306. f = whatblock(frp->save_pc);
  307. do {
  308. printf("%s", symname(f));
  309. if (not isinline(f)) {
  310. printparams(f, frp);
  311. }
  312. line = srcline(frp->save_pc - 1);
  313. if (line != 0) {
  314. printf(", line %d", line);
  315. printf(" in \"%s\"\n", srcfilename(frp->save_pc - 1));
  316. } else {
  317. printf(" at 0x%x\n", frp->save_pc);
  318. }
  319. if (dumpvariables) {
  320. dumpvars(f, frp);
  321. putchar('\n');
  322. }
  323. if (isinline(f)) {
  324. f = container(f);
  325. } else {
  326. frp = nextframe(frp);
  327. if (frp != nil) {
  328. f = whatblock(frp->save_pc);
  329. }
  330. }
  331. } while (frp != nil and f != program);
  332. if (dumpvariables) {
  333. printf("in \"%s\":\n", symname(program));
  334. dumpvars(program, nil);
  335. putchar('\n');
  336. }
  337. walkingstack = save;
  338. }
  339. }
  340. /*
  341. * Find the entry point of a procedure or function.
  342. */
  343. public findbeginning(f)
  344. Symbol f;
  345. {
  346. f->symvalue.funcv.beginaddr += 2;
  347. }
  348. /*
  349. * Return the address corresponding to the first line in a function.
  350. */
  351. public Address firstline(f)
  352. Symbol f;
  353. {
  354. Address addr;
  355. addr = codeloc(f);
  356. while (linelookup(addr) == 0 and addr < objsize) {
  357. ++addr;
  358. }
  359. if (addr == objsize) {
  360. addr = -1;
  361. }
  362. return addr;
  363. }
  364. /*
  365. * Catcher drops strike three ...
  366. */
  367. public runtofirst()
  368. {
  369. Address addr;
  370. addr = pc;
  371. while (linelookup(addr) == 0 and addr < objsize) {
  372. ++addr;
  373. }
  374. if (addr < objsize) {
  375. stepto(addr);
  376. }
  377. }
  378. /*
  379. * Return the address corresponding to the end of the program.
  380. *
  381. * We look for the entry to "exit".
  382. */
  383. public Address lastaddr()
  384. {
  385. register Symbol s;
  386. s = lookup(identname("exit", true));
  387. if (s == nil) {
  388. panic("can't find exit");
  389. }
  390. return codeloc(s);
  391. }
  392. /*
  393. * Decide if the given function is currently active.
  394. *
  395. * We avoid calls to "findframe" during a stack trace for efficiency.
  396. * Presumably information evaluated while walking the stack is active.
  397. */
  398. public Boolean isactive(f)
  399. Symbol f;
  400. {
  401. register Boolean b;
  402. if (isfinished(process)) {
  403. b = false;
  404. } else {
  405. if (walkingstack or f == program or
  406. (ismodule(f) and isactive(container(f)))) {
  407. b = true;
  408. } else {
  409. b = (Boolean) (findframe(f) != nil);
  410. }
  411. }
  412. return b;
  413. }
  414. /*
  415. * Evaluate a call to a procedure.
  416. */
  417. public callproc(procnode, arglist)
  418. Node procnode;
  419. Node arglist;
  420. {
  421. Symbol proc;
  422. Integer argc;
  423. if (procnode->op != O_SYM) {
  424. beginerrmsg();
  425. fprintf(stderr, "can't call \"");
  426. prtree(stderr, procnode);
  427. fprintf(stderr, "\"");
  428. enderrmsg();
  429. }
  430. assert(procnode->op == O_SYM);
  431. proc = procnode->value.sym;
  432. if (not isblock(proc)) {
  433. error("\"%s\" is not a procedure or function", symname(proc));
  434. }
  435. pushenv();
  436. pc = codeloc(proc);
  437. argc = pushargs(proc, arglist);
  438. beginproc(proc, argc);
  439. isstopped = true;
  440. event_once(build(O_EQ, build(O_SYM, pcsym), build(O_SYM, retaddrsym)),
  441. buildcmdlist(build(O_PROCRTN, proc)));
  442. cont();
  443. /* NOTREACHED */
  444. }
  445. /*
  446. * Push the arguments on the process' stack. We do this by first
  447. * evaluating them on the "eval" stack, then copying into the process'
  448. * space.
  449. */
  450. private Integer pushargs(proc, arglist)
  451. Symbol proc;
  452. Node arglist;
  453. {
  454. Stack *savesp;
  455. int argc, args_size;
  456. savesp = sp;
  457. argc = evalargs(proc, arglist);
  458. args_size = sp - savesp;
  459. setreg(STKP, reg(STKP) - args_size);
  460. dwrite(savesp, reg(STKP), args_size);
  461. sp = savesp;
  462. return argc;
  463. }
  464. /*
  465. * Evaluate arguments left-to-right.
  466. */
  467. private Integer evalargs(proc, arglist)
  468. Symbol proc;
  469. Node arglist;
  470. {
  471. Node p, exp;
  472. Symbol arg;
  473. Stack *savesp;
  474. Address addr;
  475. Integer count;
  476. savesp = sp;
  477. count = 0;
  478. arg = proc->chain;
  479. for (p = arglist; p != nil; p = p->value.arg[1]) {
  480. if (p->op != O_COMMA) {
  481. panic("evalargs: arglist missing comma");
  482. }
  483. if (arg == nil) {
  484. sp = savesp;
  485. error("too many parameters to %s", symname(proc));
  486. }
  487. exp = p->value.arg[0];
  488. if (not compatible(arg->type, exp->nodetype)) {
  489. sp = savesp;
  490. error("expression for parameter %s is of wrong type", symname(arg));
  491. }
  492. if (arg->class == REF) {
  493. if (exp->op != O_RVAL) {
  494. sp = savesp;
  495. error("variable expected for parameter \"%s\"", symname(arg));
  496. }
  497. addr = lval(exp->value.arg[0]);
  498. push(Address, addr);
  499. } else {
  500. eval(exp);
  501. }
  502. arg = arg->chain;
  503. ++count;
  504. }
  505. if (arg != nil) {
  506. sp = savesp;
  507. error("not enough parameters to %s", symname(proc));
  508. }
  509. return count;
  510. }
  511. public procreturn(f)
  512. Symbol f;
  513. {
  514. flushoutput();
  515. putchar('\n');
  516. printname(stdout, f);
  517. printf(" returns successfully\n", symname(f));
  518. popenv();
  519. erecover();
  520. }
  521. /*
  522. * Push the current environment.
  523. */
  524. private pushenv()
  525. {
  526. push(Address, pc);
  527. push(Lineno, curline);
  528. push(String, cursource);
  529. push(Boolean, isstopped);
  530. push(Symbol, curfunc);
  531. push(Word, reg(PROGCTR));
  532. push(Word, reg(STKP));
  533. }
  534. /*
  535. * Pop back to the real world.
  536. */
  537. public popenv()
  538. {
  539. register String filename;
  540. setreg(STKP, pop(Word));
  541. setreg(PROGCTR, pop(Word));
  542. curfunc = pop(Symbol);
  543. isstopped = pop(Boolean);
  544. filename = pop(String);
  545. curline = pop(Lineno);
  546. pc = pop(Address);
  547. setsource(filename);
  548. }
  549. /*
  550. * Flush the debuggee's standard output.
  551. *
  552. * This is VERY dependent on the use of stdio.
  553. */
  554. public flushoutput()
  555. {
  556. register Symbol p, iob;
  557. register Stack *savesp;
  558. p = lookup(identname("fflush", true));
  559. while (p != nil and not isblock(p)) {
  560. p = p->next_sym;
  561. }
  562. if (p != nil) {
  563. iob = lookup(identname("_iob", true));
  564. if (iob != nil) {
  565. pushenv();
  566. pc = codeloc(p);
  567. savesp = sp;
  568. push(long, address(iob, nil) + sizeof(struct _iobuf));
  569. setreg(STKP, reg(STKP) - sizeof(long));
  570. dwrite(savesp, reg(STKP), sizeof(long));
  571. sp = savesp;
  572. beginproc(p, 1);
  573. stepto(return_addr());
  574. popenv();
  575. }
  576. }
  577. }