Generic.Unix.Traps.Mod 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. (* ETH Oberon, Copyright 2000 ETH Zuerich Institut fuer Computersysteme, ETH Zentrum, CH-8092 Zuerich.
  2. Refer to the "General ETH Oberon System Source License" contract available at: http://www.oberon.ethz.ch/ *)
  3. MODULE Traps; (** AUTHOR "G.F."; PURPOSE "Exception Trap and symbolic debugging"; *)
  4. (* 2000.02.06 g.f. UnixOberon release 2.3.6d *)
  5. (* 2006.07.09 g.f. UnixAos version *)
  6. IMPORT S := SYSTEM, Trace, Glue, Unix, Objects, Machine, Heaps, Streams, Modules, Reflection,
  7. TrapWriters, Commands, StdIO;
  8. CONST
  9. AddrSize = SIZEOF( ADDRESS );
  10. MaxRecursion = 2;
  11. TYPE
  12. ExceptionHandler = RECORD pc, fp, sp: ADDRESS END;
  13. VAR
  14. trapHandlingLevel: LONGINT;
  15. trace: BOOLEAN;
  16. unix: Commands.Context;
  17. trapMutex: Unix.Mutex_t;
  18. PROCEDURE LockTrap;
  19. BEGIN
  20. Unix.MtxLock( trapMutex );
  21. END LockTrap;
  22. PROCEDURE UnlockTrap;
  23. BEGIN
  24. trapHandlingLevel := 0;
  25. Unix.MtxUnlock( trapMutex )
  26. END UnlockTrap;
  27. PROCEDURE Append( VAR ar: ARRAY OF CHAR; CONST this: ARRAY OF CHAR );
  28. VAR i, j: LONGINT;
  29. BEGIN
  30. i := 0; j := 0;
  31. WHILE ar[i] # 0X DO INC( i ) END;
  32. WHILE (i < LEN( ar ) - 1) & (this[j] # 0X) DO ar[i] := this[j]; INC( i ); INC( j ) END;
  33. ar[i] := 0X
  34. END Append;
  35. PROCEDURE TimeTag( w: Streams.Writer );
  36. VAR
  37. tv: Unix.Timeval; tz: Unix.Timezone; t: Unix.TmPtr; ret: LONGINT;
  38. PROCEDURE Int( i: LONGINT );
  39. BEGIN
  40. IF i < 10 THEN w.Char( '0' ) END;
  41. w.Int( i, 0 )
  42. END Int;
  43. BEGIN
  44. ret := Unix.gettimeofday( tv, tz );
  45. t := Unix.localtime( tv );
  46. w.Int( 1900 + t.year, 4 ); w.Char( '/' ); Int( t.mon + 1 ); w.Char( '/' ); Int( t.mday );
  47. w.String( " " ); Int( t.hour ); w.Char( ':' ); Int( t.min ); w.Ln;
  48. END TimeTag;
  49. PROCEDURE FinishTrap( w: Streams.Writer; p: Objects.Process );
  50. VAR tag: ADDRESS; td: Modules.TypeDesc; name: ARRAY 72 OF CHAR;
  51. BEGIN
  52. w.Char( 2X ); (* end of trap text *)
  53. w.Update;
  54. TrapWriters.Trapped;
  55. IF p.obj = NIL THEN (* the main thread is not associated with any object *)
  56. Unix.exit( -1 )
  57. ELSE
  58. S.GET( S.VAL( ADDRESS, p.obj ) - AddrSize, tag );
  59. S.GET( tag - AddrSize, td );
  60. IF td.mod # NIL THEN
  61. COPY( td.mod.name, name ); Append( name, "." ); Append( name, td.name );
  62. IF name = "Oberon.System.OberonRunner" THEN UnlockOberon END
  63. END
  64. END;
  65. END FinishTrap;
  66. PROCEDURE Registers(CONST c:Unix.McontextDesc; w: Streams.Writer);
  67. BEGIN
  68. w.String("EAX= "); w.Address(c.r_ax);
  69. w.String(" EBX= "); w.Address(c.r_bx);
  70. w.String(" ECX= "); w.Address(c.r_cx);
  71. w.String(" EDX= "); w.Address(c.r_dx);
  72. w.String(" ESI= "); w.Address(c.r_si);
  73. w.String(" EDI= "); w.Address(c.r_di);
  74. w.Ln;
  75. END Registers;
  76. PROCEDURE Trap( sig: LONGINT; ucp: Unix.Ucontext);
  77. VAR
  78. pc, sp, bp: ADDRESS;
  79. trapno: LONGINT;
  80. process: Objects.Process;
  81. handler: ExceptionHandler;
  82. w: Streams.Writer;
  83. BEGIN
  84. LockTrap;
  85. IF trace THEN
  86. Machine.Acquire( Machine.TraceOutput );
  87. Trace.Ln;
  88. Trace.String( "Aos Trap: signal = " ); Trace.Int( sig, 0 );
  89. Trace.String( ", ucp = " ); Trace.Address( S.VAL( ADDRESS, ucp ) );
  90. Trace.String( ", traphandling level = " ); Trace.Int( trapHandlingLevel, 1 );
  91. Trace.Ln;
  92. Machine.Acquire( Machine.TraceOutput );
  93. END;
  94. INC( trapHandlingLevel );
  95. IF trapHandlingLevel > MaxRecursion THEN
  96. UnlockTrap;
  97. Objects.Terminate
  98. END;
  99. w := TrapWriters.GetWriter();
  100. w.Char( 1X ); (* begin of trap text *)
  101. w.Ln;
  102. w.String( Machine.version ); w.String( " " ); TimeTag( w ); w.Ln;
  103. IF trapHandlingLevel = 1 THEN
  104. w.String( "Trap " );
  105. ELSE
  106. w.String( "[recursive Trap] " );
  107. END;
  108. sp := ucp.mc.r_sp;
  109. CASE sig OF
  110. | 1: w.String( "1 (Hangup signal)" );
  111. | 2: w.String( "2 (User interrupt)" );
  112. | 3: w.String( "3 (Quit signal)" );
  113. | 4: w.String( "4 (Illegal instruction)" );
  114. | 5: w.String( "5." );
  115. S.GET( sp, trapno ); w.Int( trapno, 0 );
  116. CASE trapno OF
  117. | 1: w.String( " (WITH guard failed)" )
  118. | 2: w.String( " (CASE invalid)" )
  119. | 3: w.String( " (RETURN missing)" )
  120. | 5: w.String( " (implicit type guard failed)" )
  121. | 6: w.String( " (type guard failed)" )
  122. | 7: w.String( " (index out of range)" )
  123. | 8: w.String( " (ASSERT failed)" )
  124. | 9: w.String( " (array dimension error)" )
  125. |12: w.String( " (division error)" )
  126. ELSE
  127. IF trapno >= 30 THEN w.String( " (programmed HALT)" )
  128. ELSE w.String( " (unknown exception)" )
  129. END
  130. END;
  131. | 8: w.String( "8 (Arithmetic exception)" );
  132. |10: w.String( "10 (Bus Error)" )
  133. |11: w.String( "11 (Segmentation violation)" )
  134. |13: w.String( "13 (Broken pipe)" )
  135. |14: w.String( "14 (Alarm signal)" )
  136. ELSE
  137. w.String( "(Signal " ); w.Int( sig, 0 ); w.Char( ')' );
  138. END;
  139. w.Ln;
  140. process := Objects.CurrentProcess( );
  141. pc := ucp.mc.r_pc; bp := ucp.mc.r_bp;
  142. IF pc = 0 THEN
  143. (* assume call of procedure variable with value NIL *)
  144. S.GET( sp, pc ); (* get return address on top of stack *)
  145. END;
  146. w.Ln;
  147. w.String( " sp = " ); w.Address( sp ); w.String( ", fp = " ); w.Address( bp );
  148. w.String( ", pc = " ); w.Address( pc ); w.Ln;
  149. w.Ln;
  150. Registers( ucp.mc, w );
  151. IF process # NIL THEN
  152. Reflection.StackTraceBack( w, pc, bp, sp, Objects.GetStackBottom( process ), TRUE, FALSE );
  153. SearchExceptionHandler( process, ucp, handler );
  154. ELSE
  155. (* avoid recusive trap in case of faulty module Objects *)
  156. Reflection.StackTraceBack( w, pc, bp, sp, sp+512, TRUE, FALSE );
  157. END;
  158. w.Ln; w.Ln;
  159. w.String("----------------------------------------------------"); w.Ln;
  160. FinishTrap( w, process);
  161. UnlockTrap;
  162. IF handler.pc # 0 THEN
  163. IF Unix.Version # "Darwin" THEN
  164. (* in the Darwin port Unix.ModifyContext fails with bus error. Stack alignment problem? *)
  165. w.Ln;
  166. w.String( "### program continues with exception handler ###" ); w.Ln;
  167. Unix.ModifyContext( ucp, handler.pc, handler.fp, handler.sp );
  168. RETURN (*! to exception handler !! *)
  169. END
  170. END;
  171. IF Machine.standaloneAppl THEN
  172. unix.error.Ln; unix.error.Ln;
  173. unix.error.String( "### Program aborted. Stack traceback in logfile" ); unix.error.Ln;
  174. unix.error.Update;
  175. Machine.Shutdown( FALSE )
  176. ELSE
  177. Objects.ExitTrap()
  178. END
  179. END Trap;
  180. PROCEDURE UnlockOberon;
  181. CONST OberonKernel = "Oberon.Kernel";
  182. VAR c: PROCEDURE;
  183. BEGIN
  184. IF Modules.ModuleByName( OberonKernel ) # NIL THEN
  185. GETPROCEDURE( OberonKernel, "UnlockOberon", c );
  186. IF c # NIL THEN c END
  187. END;
  188. END UnlockOberon;
  189. PROCEDURE CheckBP(fp: ADDRESS): ADDRESS;
  190. VAR n: ADDRESS;
  191. BEGIN
  192. IF (fp # NIL) THEN
  193. S.GET(fp, n);
  194. IF ODD(n) THEN RETURN fp + SIZEOF(ADDRESS) END;
  195. END;
  196. RETURN fp;
  197. END CheckBP;
  198. PROCEDURE SearchExceptionHandler( process: Objects.Process; cont: Unix.Ucontext; VAR handler: ExceptionHandler );
  199. VAR entry, fp, sp, pc: ADDRESS;
  200. BEGIN
  201. handler.pc := 0; (* no handler *)
  202. pc := cont.mc.r_pc; fp := cont.mc.r_bp; sp := cont.mc.r_sp;
  203. IF pc = 0 THEN
  204. (* assume call of procedure variable with value NIL *)
  205. S.GET( sp, pc ); (* get return address on top of stack *)
  206. END;
  207. entry := Modules.GetExceptionHandler( pc );
  208. WHILE (entry = -1) & (fp <= process.stackBottom) & (fp#0) & (fp MOD SIZEOF(ADDRESS)=0) DO
  209. fp := CheckBP(fp);
  210. S.GET( fp + AddrSize, pc );
  211. pc := pc - 1; (* CALL instruction, machine dependent!!! *)
  212. entry := Modules.GetExceptionHandler( pc );
  213. sp := fp; (* Save the old framepointer into the stack pointer *)
  214. S.GET( fp, fp ) (* Unwind PAF *)
  215. END;
  216. IF entry # -1 THEN
  217. handler.pc := entry; handler.fp := fp; handler.sp := sp
  218. END
  219. END SearchExceptionHandler;
  220. PROCEDURE SignalHandler( signal: LONGINT; scp, ucp: ADDRESS );
  221. (* 'dummy' for 16 byte stack alignment, MacOS! *)
  222. BEGIN
  223. IF ~(signal IN {1, 2, 14, 15}) (* SIGHUP, SIGINT, SIGALRM, SIGTERM *) THEN
  224. Trap( signal, S.VAL( Unix.Ucontext, ucp ))
  225. END
  226. END SignalHandler;
  227. BEGIN
  228. trapMutex := Unix.NewRecursiveMtx( );
  229. trace := FALSE;
  230. Unix.InstallSignalHandler( SignalHandler );
  231. unix := StdIO.env
  232. END Traps.