ARM.Traps.Mod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. MODULE Traps; (** AUTHOR "pjm"; PURPOSE "Trap handling and symbolic debugging"; *)
  2. IMPORT SYSTEM, Machine, KernelLog, Streams, Modules, Objects, Kernel, Reflection, TrapWriters;
  3. CONST
  4. RecursiveLimit = 2; (* normally 1 or 2 - how many recursive traps to display before stopping *)
  5. TraceVerbose = FALSE;
  6. TestTrap = TRUE;
  7. (* Process termination halt codes *)
  8. halt* = Objects.halt;
  9. haltUnbreakable* = Objects.haltUnbreakable;
  10. (** Trap Numbers -- Do not modify: these are related to the compiler code generation. *)
  11. (*
  12. WithTrap* = 1; (* generated when a WITH statement fails *)
  13. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  14. ReturnTrap* = 3;
  15. TypeEqualTrap* = 5;
  16. TypeCheckTrap* = 6;
  17. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  18. AssertTrap* = 8; (* generated when an assert fails *)
  19. ArraySizeTrap* = 9;
  20. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  21. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  22. NegativeDivisorTrap*=12;
  23. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  24. ELSIF code = 13 THEN StrAppend( desc, "Keyboard interrupt" )
  25. ELSIF code = 14 THEN StrAppend( desc, "Out of memory" )
  26. ELSIF code = 15 THEN StrAppend( desc, "Deadlock (active objects)" );
  27. ELSIF code = 16 THEN StrAppend( desc, "Procedure returned" );
  28. ELSIF code = 23 THEN StrAppend( desc, "Exceptions.Raise" )
  29. *)
  30. DivisionError = 0;
  31. WithError = 1; (* Compiler generated *)
  32. CaseError = 2; (* Compiler generated *)
  33. ReturnError = 3; (* Compiler generated *)
  34. IntOverflow = 4;
  35. ImplicitTypeGuardError = 5; (* Compiler generated *)
  36. TypeGuardError = 6; (* Compiler generated *)
  37. IndexOutOfRange = 7; (* Compiler generated *)
  38. AssertError = 8; (* Compiler generated *)
  39. ArraySize = 9; (* Compiler generated *)
  40. ArrayForm = 10; (* Compiler generated *)
  41. SetElement = 11; (* Compiler generated *)
  42. NegativeDivisor = 12; (* Compiler generated *)
  43. KeyboardInt = 13;
  44. OutOfMemory = 14;
  45. Deadlock = 15;
  46. ProcedureReturned = 16; (* Compiler generated *)
  47. UndefinedInstn = 17; (* ARM specific *)
  48. NilPointer = 18; (* ARM specific *)
  49. MemoryError = 19; (* ARM specific *)
  50. ExceptionRaised = 23;
  51. ProcessResurrected = 2201;
  52. RecursiveExclusive = 2203;
  53. AwaitOutsideExclusive = 2204;
  54. (** Trap descriptions, human-readable *)
  55. (*
  56. |0: w.String("division error")
  57. |1: w.String("WITH guard failed")
  58. |2: w.String("CASE invalid")
  59. |3: w.String("RETURN missing")
  60. |4: w.String("integer overflow")
  61. |5: w.String("implicit type guard failed")
  62. |6: w.String("type guard failed")
  63. |7: w.String("index out of range")
  64. |8: w.String("ASSERT failed")
  65. |9: w.String("array dimension error")
  66. |14: w.String("out of memory")
  67. |16: w.String("procedure returned")
  68. *)
  69. DivisionErrorDesc = "division error";
  70. WithErrorDesc = "WITH guard failed";
  71. CaseErrorDesc = "CASE invalid";
  72. ReturnErrorDesc = "RETURN missing";
  73. IntOverflowDesc = "integer overflow";
  74. ImplicitTypeGuardErrorDesc = "implicit type guard failed";
  75. TypeGuardErrorDesc = "type guard failed";
  76. IndexOutOfRangeDesc = "index out of range";
  77. AssertErrorDesc = "ASSERT failed";
  78. ArraySizeDesc = "array dimension error";
  79. ArrayFormDesc = "invalid array shape";
  80. SetElementDesc = "invalid SET element";
  81. NegativeDivisorDesc = "negative divisor";
  82. KeyboardIntDesc = "keyboard interrupt";
  83. OutOfMemoryDesc = "out of memory";
  84. DeadlockDesc = "deadlock";
  85. ProcedureReturnedDesc = "procedure returned";
  86. UndefinedInstnDesc = "undefined instruction";
  87. NilPointerDesc = "NIL pointer";
  88. MemoryErrorDesc = "invalid memory location";
  89. ExceptionRaisedDesc = "exception";
  90. ProcessResurrectedDesc = "process resurrected";
  91. RecursiveExclusiveDesc = "recursive entrance in EXCLUSIVE section";
  92. AwaitOutsideExclusiveDesc = "AWAIT statement outside EXCLUSIVE section";
  93. TYPE
  94. Variable* = RECORD (** variable descriptor *)
  95. adr-: ADDRESS;
  96. type-, size-, n-, tdadr-: LONGINT
  97. END;
  98. VAR
  99. trapState: ARRAY Machine.MaxCPU OF LONGINT; (* indexed by Machine.ID() *)
  100. modes: ARRAY 25 OF CHAR;
  101. flags: ARRAY 13 OF CHAR;
  102. (* Write flag values. *)
  103. PROCEDURE Flags(w: Streams.Writer; s: SET);
  104. VAR i: SHORTINT; ch: CHAR;
  105. BEGIN
  106. FOR i := 0 TO 11 DO
  107. ch := flags[i];
  108. IF ch # "!" THEN
  109. IF i IN s THEN ch := CAP(ch) END;
  110. w.Char(ch)
  111. END
  112. END;
  113. w.String(" iopl"); w.Int(ASH(SYSTEM.VAL(LONGINT, s * {12,13}), -12), 1)
  114. END Flags;
  115. (** Display trap state. *)
  116. PROCEDURE Show*(p: Objects.Process; VAR int: Machine.State; VAR exc: Machine.ExceptionState; long: BOOLEAN);
  117. VAR id: LONGINT; overflow: BOOLEAN; w: Streams.Writer;
  118. PROCEDURE Val(CONST s: ARRAY OF CHAR; val: HUGEINT);
  119. BEGIN
  120. w.Char(" "); w.String(s); w.Char("="); w.Hex(val, -8)
  121. END Val;
  122. BEGIN
  123. overflow := FALSE;
  124. w := TrapWriters.GetWriter();
  125. w.Update; (* flush previous output stuck in global writer w *)
  126. w.Char(1X); (* "start of trap" *)
  127. id := Machine.ID();
  128. INC(trapState[id]);
  129. IF trapState[id] > RecursiveLimit THEN
  130. w.String(" [Recursive TRAP]")
  131. ELSE
  132. (* output first line *)
  133. w.String("["); w.Int(trapState[id], 1); w.String("] ");
  134. w.String("TRAP "); w.Int(SHORT(exc.halt), 1); w.String(" ");
  135. CASE exc.halt OF
  136. DivisionError: w.String(DivisionErrorDesc)
  137. |WithError: w.String(WithErrorDesc)
  138. |CaseError: w.String(CaseErrorDesc)
  139. |ReturnError: w.String(ReturnErrorDesc)
  140. |IntOverflow: w.String(IntOverflowDesc)
  141. |ImplicitTypeGuardError: w.String(ImplicitTypeGuardErrorDesc)
  142. |TypeGuardError: w.String(TypeGuardErrorDesc)
  143. |IndexOutOfRange: w.String(IndexOutOfRangeDesc)
  144. |AssertError: w.String(AssertErrorDesc)
  145. |ArraySize: w.String(ArraySizeDesc)
  146. |ArrayForm: w.String(ArrayFormDesc)
  147. |SetElement: w.String(SetElementDesc)
  148. |NegativeDivisor: w.String(NegativeDivisorDesc)
  149. |KeyboardInt: w.String(KeyboardIntDesc)
  150. |OutOfMemory: w.String(OutOfMemoryDesc)
  151. |Deadlock: w.String(DeadlockDesc)
  152. |ProcedureReturned: w.String(ProcedureReturnedDesc)
  153. |UndefinedInstn: w.String(UndefinedInstnDesc); w.String(": "); w.Hex(exc.instn,-8)
  154. |NilPointer: w.String(NilPointerDesc)
  155. |MemoryError: w.String(MemoryErrorDesc); w.String(" at "); w.Address(exc.pf)
  156. |ExceptionRaised: w.String(ExceptionRaisedDesc)
  157. |ProcessResurrected: w.String(ProcessResurrectedDesc)
  158. |RecursiveExclusive: w.String(RecursiveExclusiveDesc)
  159. |AwaitOutsideExclusive: w.String(AwaitOutsideExclusiveDesc)
  160. ELSE
  161. w.String("HALT statement: ");
  162. w.Int(exc.halt, 0)
  163. END;
  164. IF exc.locks # {} THEN
  165. w.String(", Locks: "); w.Set(exc.locks)
  166. END;
  167. w.Char(" "); w.String(Machine.version);
  168. IF long THEN
  169. w.Char(0EX); (* "fixed font" *)
  170. w.Ln;
  171. (* output values *)
  172. Val("R0", int.R[0]); Val("R1", int.R[1]); Val("R2", int.R[2]); Val("R3", int.R[3]);
  173. Val("R4", int.R[4]); Val("R5", int.R[5]); Val("R6", int.R[6]); Val("R7", int.R[7]);
  174. Val("R8", int.R[8]); Val("R9", int.R[9]); Val("R10", int.R[10]); Val("R11", int.R[11]);
  175. Val("FP", int.BP); Val("SP", int.SP); Val("LR", int.LR); Val("PC", int.PC);
  176. Val("PSR", int.PSR);
  177. Val("TMR", Kernel.GetTicks()); w.Ln
  178. ELSE
  179. w.Ln
  180. END;
  181. IF exc.halt = UndefinedInstn THEN
  182. Val("Instruction", exc.instn)
  183. ELSIF exc.halt = MemoryError THEN
  184. Val("Location", exc.pf);
  185. IF exc.status # - 1 THEN
  186. Val("Status", exc.status)
  187. END
  188. END;
  189. w.String("Process:"); Reflection.WriteProcess(w, p); w.Ln;
  190. Reflection.StackTraceBack(w, int.PC, int.BP, Objects.GetStackBottom(p), long, overflow);
  191. END;
  192. w.String("---------------------------------"); w.Ln;
  193. w.Char(02X); (* "end of trap" *)
  194. w.Update;
  195. TrapWriters.Trapped();
  196. trapState[id] := 0
  197. END Show;
  198. PROCEDURE SetLastExceptionState(ex: Machine.ExceptionState);
  199. VAR id: LONGINT;
  200. BEGIN
  201. id := Machine.AcquirePreemption();
  202. Objects.running[id].exp := ex;
  203. Machine.ReleasePreemption;
  204. END SetLastExceptionState;
  205. PROCEDURE GetLastExceptionState*(): Machine.ExceptionState;
  206. VAR
  207. id: LONGINT;
  208. ex: Machine.ExceptionState;
  209. BEGIN
  210. id := Machine.AcquirePreemption();
  211. ex := Objects.running[id].exp;
  212. Machine.ReleasePreemption;
  213. RETURN ex;
  214. END GetLastExceptionState;
  215. (** Handles an exception. Interrupts are on during this procedure. *)
  216. PROCEDURE HandleException(VAR int: Machine.State; VAR exc: Machine.ExceptionState; VAR handled: BOOLEAN);
  217. VAR
  218. bp, sp, pc, handler: ADDRESS;
  219. BEGIN
  220. bp := int.BP; sp := int.SP; pc := int.PC;
  221. handler := Modules.GetExceptionHandler(pc);
  222. IF handler # -1 THEN (* Handler in the current PAF *)
  223. int.PC := handler; handled := TRUE;
  224. SetTrapVariable(pc, bp); SetLastExceptionState(exc)
  225. ELSE
  226. WHILE (bp # 0) & (handler = -1) DO
  227. SYSTEM.GET(bp + 4, pc);
  228. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  229. handler := Modules.GetExceptionHandler(pc);
  230. sp := bp; (* Save the old basepointer into the stack pointer *)
  231. SYSTEM.GET(bp, bp) (* Unwind PAF *)
  232. END;
  233. IF handler = -1 THEN
  234. handled := FALSE;
  235. ELSE
  236. int.PC := handler; int.BP := bp; int.SP := sp;
  237. SetTrapVariable(pc, bp); SetLastExceptionState(exc);
  238. handled := TRUE
  239. END
  240. END
  241. END HandleException;
  242. PROCEDURE SetTrapVariable(pc, fp: ADDRESS);
  243. VAR
  244. varadr: ADDRESS;
  245. BEGIN
  246. varadr := Reflection.GetVariableAdr(pc, fp, "trap");
  247. IF varadr # -1 THEN
  248. SYSTEM.PUT8(varadr, 1)
  249. END
  250. END SetTrapVariable;
  251. (* Unbreakable stack trace back with regard to every FINALLY on the way *)
  252. PROCEDURE Unbreakable(p: Objects.Process; VAR int: Machine.State; VAR exc: Machine.ExceptionState; VAR handled: BOOLEAN);
  253. VAR
  254. bp, bpSave, pc, handler, bpBottom:ADDRESS;
  255. hasFinally : BOOLEAN;
  256. BEGIN
  257. bp := int.BP;
  258. pc := int.PC;
  259. hasFinally := FALSE;
  260. handler := Modules.GetExceptionHandler(pc);
  261. (* Handler in the current PAF *)
  262. IF handler # -1 THEN
  263. int.PC := handler;
  264. hasFinally := TRUE;
  265. SetTrapVariable(pc, bp);
  266. END;
  267. (* The first waypoint is the bp of the top PAF *)
  268. bpSave := bp;
  269. WHILE (bp # 0) DO
  270. (* Did we reach the last PAF? *)
  271. SYSTEM.GET(bp, pc);
  272. IF (pc = 0) THEN
  273. bpBottom := bp; (* Save the FP of the last PAF *)
  274. END;
  275. (* Get the return pc *)
  276. SYSTEM.GET(bp + SIZEOF(ADDRESS), pc);
  277. handler := Modules.GetExceptionHandler(pc);
  278. (* Save the last framepointer as stackpointer *)
  279. IF ~hasFinally THEN
  280. int.SP := bp;
  281. END;
  282. SYSTEM.GET(bp, bp);
  283. (* Here bp may be 0. *)
  284. IF (handler # -1) & (bp # 0) THEN (* If Objects.Terminate has a FINALLY this doesn't work !!! *)
  285. IF hasFinally THEN
  286. (* Connect Finally to Finally *)
  287. SYSTEM.PUT(bpSave + SIZEOF(ADDRESS), handler); (* Adapt the return pc *)
  288. SYSTEM.PUT(bpSave, bp); (* Adapt the dynamic link *)
  289. bpSave := bp;
  290. ELSE
  291. int.PC := handler;
  292. int.BP := bp;
  293. bpSave := bp;
  294. hasFinally := TRUE;
  295. END;
  296. SetTrapVariable(pc, bp)
  297. END
  298. END;
  299. (* Now bp = 0, bottom of the stack, so link the last known return PC to the Termination *)
  300. IF ~hasFinally THEN
  301. SYSTEM.GET(bpBottom + SIZEOF(ADDRESS), pc); (* PC of the Terminate *)
  302. int.PC := pc;
  303. int.BP := bpBottom;
  304. ELSIF bpSave # bpBottom THEN
  305. SYSTEM.GET(bpBottom + SIZEOF(ADDRESS), pc); (* PC of the Terminate *)
  306. SYSTEM.PUT(bpSave + SIZEOF(ADDRESS), pc);
  307. SetLastExceptionState(exc)
  308. END;
  309. handled := TRUE; (* If FALSE the process could be restarted, may be this is the meaning? *)
  310. END Unbreakable;
  311. (* General exception handler. *)
  312. PROCEDURE Exception(VAR int: Machine.State);
  313. VAR t: Objects.Process; exc: Machine.ExceptionState; user, traceTrap, handled: BOOLEAN;
  314. BEGIN (* interrupts off *)
  315. t := Objects.running[Machine.ID()]; (* t is running process *)
  316. handled := FALSE;
  317. Machine.GetExceptionState(int, exc);
  318. user := TRUE;
  319. traceTrap := (exc.locks = {}) & (exc.halt >= MAX(INTEGER)) & (exc.halt <= MAX(INTEGER)+1);
  320. Show(t, int, exc, exc.halt # MAX(INTEGER)+1); (* Always show the trap info!*)
  321. IF exc.halt = haltUnbreakable THEN
  322. Unbreakable(t, int, exc, handled)
  323. ELSIF ~ traceTrap THEN
  324. HandleException( int, exc, handled)
  325. END;
  326. IF ~handled THEN
  327. (* Taken from Machine to allow the FINALLY in the kernel *)
  328. exc.locks := Machine.BreakAll();
  329. Machine.EnableInterrupts();
  330. IF ~traceTrap THEN (* trap *)
  331. IF user THEN (* return to outer level *)
  332. IF TraceVerbose THEN
  333. KernelLog.Enter;
  334. KernelLog.String("Jump"); KernelLog.Hex(t.restartPC, 9);
  335. KernelLog.Hex(t.restartSP, 9); KernelLog.Hex(t.stack.high, 9);
  336. KernelLog.Exit
  337. END;
  338. (*INCL(int.FLAGS, Machine.IFBit); (* enable interrupts *)*)
  339. int.BP := t.restartSP; int.SP := t.restartSP; (* reset stack *)
  340. int.PC := t.restartPC; (* restart object body or terminate *)
  341. ELSE (* trap was in kernel (interrupt handler) *) (* fixme: recover from trap in stack traceback *)
  342. KernelLog.Enter; KernelLog.String("Kernel halt"); KernelLog.Exit;
  343. Machine.Shutdown(FALSE)
  344. END
  345. END
  346. END;
  347. IF Objects.PleaseHalt IN t.flags THEN
  348. EXCL(t.flags, Objects.PleaseHalt);
  349. IF Objects.Unbreakable IN t.flags THEN EXCL(t.flags, Objects.Unbreakable) END;
  350. IF Objects.SelfTermination IN t.flags THEN EXCL(t.flags, Objects.SelfTermination) END
  351. END
  352. END Exception;
  353. (* Page fault handler. *)
  354. PROCEDURE PageFault(VAR state: Machine.State);
  355. VAR
  356. t: Objects.Process;
  357. adr: ADDRESS;
  358. ignored: LONGINT;
  359. BEGIN
  360. t := Objects.running[Machine.ID()];
  361. Machine.GetPageFault(adr, ignored);
  362. (*IF Machine.IFBit IN state.FLAGS THEN (* enable interrupts again if they were enabled *)
  363. Machine.Sti() (* avoid Processors.StopAll deadlock when waiting for locks below (fixme: remove) *)
  364. END;*)
  365. IF adr > 4096 THEN
  366. (* Not a NIL pointer, maybe stack overflow? *)
  367. IF (t = NIL) OR ~Machine.ExtendStack(t.stack, adr) THEN
  368. IF TraceVerbose THEN
  369. IF t = NIL THEN
  370. KernelLog.Enter; KernelLog.String("GrowStack running=NIL");
  371. KernelLog.Hex(state.PC, 9); KernelLog.Exit
  372. ELSE
  373. KernelLog.Enter;
  374. KernelLog.String("GrowStack failed, pf="); KernelLog.Hex(adr, 8);
  375. KernelLog.String(" adr="); KernelLog.Hex(t.stack.adr, 8);
  376. KernelLog.String(" high="); KernelLog.Hex(t.stack.high, 8);
  377. KernelLog.Exit
  378. END
  379. END;
  380. Exception(state)
  381. ELSE
  382. IF TraceVerbose THEN
  383. KernelLog.Enter; KernelLog.String("GrowStack");
  384. KernelLog.Hex(t.stack.adr, 9); KernelLog.Hex(t.stack.high, 9); KernelLog.Exit
  385. END
  386. END;
  387. ELSE
  388. Exception(state)
  389. END
  390. END PageFault;
  391. PROCEDURE Init;
  392. VAR i: LONGINT; s: ARRAY 8 OF CHAR;
  393. BEGIN
  394. IF TestTrap THEN
  395. Machine.GetConfig("TestTrap", s);
  396. IF s[0] = "1" THEN HALT(98) END
  397. END;
  398. FOR i := 0 TO Machine.MaxCPU-1 DO trapState[i] := 0 END;
  399. Machine.InstallExceptionHandler(PageFault, Machine.Data);
  400. Machine.InstallExceptionHandler(PageFault, Machine.Prefetch);
  401. Machine.InstallExceptionHandler(Exception, Machine.Undef);
  402. Machine.InstallExceptionHandler(Exception, Machine.Swi);
  403. Machine.InstallExceptionHandler(Exception, Machine.Fiq);
  404. IF TestTrap & (s[0] = "2") THEN HALT(99) END
  405. END Init;
  406. BEGIN
  407. modes := " rdy run awl awc awe rip"; (* 4 characters per mode from Objects.Ready to Objects.Terminated *)
  408. flags := "c!p!a!zstido"; (* bottom flags, !=reserved *)
  409. Init
  410. END Traps.
  411. (*
  412. 12.03.1998 pjm Started
  413. 06.08.1998 pjm Exported Show and removed AosException upcall installation & Modules lock
  414. 10.12.1998 pjm New refblk
  415. 23.06.1999 pjm State added
  416. *)
  417. (*
  418. to do:
  419. o stack overflow message is not correctly displayed in case of dynamic arrays (EDI = CR2, ESP # CR2)
  420. o fix KernelLog.Memory calls removed when switching to Streams
  421. o fix use of KernelLog lock in Show
  422. o if allowing modification of variables using their descriptors, it should also have reference to module to avoid gc after free.
  423. *)