KernelLog.Mod 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE KernelLog; (** AUTHOR "pjm"; PURPOSE "Trace output for booting and debugging"; *)
  3. (* AFI 12.03.2003 - procedure Init modified to obtain trace port info from Aos.Par i.o. being hardcoded. *)
  4. IMPORT SYSTEM, Trace, Machine, Objects;
  5. CONST
  6. BufSize = 8000; (* default trace buffer size (usually overriden by System.StartLog or LogWindow.Open *)
  7. VAR
  8. traceBufDef: ARRAY BufSize OF CHAR; (* default trace buffer *)
  9. traceBufAdr: ADDRESS; traceBufSize: SIZE; (* current trace buffer virtual addresses *)
  10. traceHead, traceTail: ADDRESS;
  11. (** Send the specified characters to the trace output (cf. Streams.Sender). *)
  12. PROCEDURE Send*(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD);
  13. VAR next: ADDRESS; c: CHAR;
  14. BEGIN
  15. INC(len, ofs); (* len is now end position *)
  16. Machine.Acquire(Machine.TraceOutput);
  17. LOOP
  18. IF ofs >= len THEN EXIT END;
  19. c := buf[ofs];
  20. IF c = 0X THEN EXIT END;
  21. next := (traceTail+1) MOD traceBufSize;
  22. IF next # traceHead THEN
  23. SYSTEM.PUT8(traceBufAdr+traceTail, c);
  24. traceTail := next
  25. ELSE (* overwrite previous character with overflow signal *)
  26. SYSTEM.PUT8(traceBufAdr + (traceTail-1) MOD traceBufSize, 3X)
  27. END;
  28. Trace.Char (c);
  29. INC(ofs)
  30. END;
  31. Machine.Release(Machine.TraceOutput)
  32. END Send;
  33. (** Write a string to the trace output. *)
  34. PROCEDURE String*(CONST s: ARRAY OF CHAR);
  35. VAR len, n: LONGINT;
  36. BEGIN
  37. len := 0; n := LEN(s);
  38. WHILE (len # n) & (s[len] # 0X) DO INC(len) END;
  39. Send(s, 0, len, FALSE, n) (* ignore res *)
  40. END String;
  41. (** Skip to the next line on trace output. *)
  42. PROCEDURE Ln*;
  43. BEGIN Char (0DX); Char (0AX);
  44. END Ln;
  45. (** Write a character. *)
  46. PROCEDURE Char*(c: CHAR);
  47. TYPE Str = ARRAY 1 OF CHAR;
  48. BEGIN
  49. String(SYSTEM.VAL(Str, c))
  50. END Char;
  51. (** Write "x" as a decimal number. "w" is the field width. *)
  52. PROCEDURE Int*(x: HUGEINT; w: LONGINT);
  53. VAR i: SIZE; x0: HUGEINT; a: ARRAY 21 OF CHAR;
  54. BEGIN
  55. IF x < 0 THEN
  56. IF x = MIN(HUGEINT) THEN
  57. DEC(w, 20);
  58. WHILE w > 0 DO Char(" "); DEC(w) END;
  59. String ("-9223372036854775808");
  60. RETURN
  61. ELSE
  62. DEC(w); x0 := -x
  63. END
  64. ELSE
  65. x0 := x
  66. END;
  67. i := 0;
  68. REPEAT
  69. a[i] := CHR(x0 MOD 10 + 30H); x0 := x0 DIV 10; INC(i)
  70. UNTIL x0 = 0;
  71. WHILE w > i DO Char(" "); DEC(w) END;
  72. IF x < 0 THEN Char("-") END;
  73. REPEAT DEC(i); Char(a[i]) UNTIL i = 0
  74. END Int;
  75. PROCEDURE Boolean*(x : BOOLEAN);
  76. BEGIN
  77. IF x THEN String("TRUE") ELSE String("FALSE") END
  78. END Boolean;
  79. (** Write "x" as a decimal number with a power-of-two multiplier (K, M or G), followed by "suffix". "w" is the field width, excluding "suffix". *)
  80. PROCEDURE IntSuffix*(x, w: LONGINT; CONST suffix: ARRAY OF CHAR);
  81. CONST K = 1024; M = K*K; G = K*M;
  82. VAR mult: CHAR;
  83. BEGIN
  84. IF x MOD K # 0 THEN
  85. Int(x, w)
  86. ELSE
  87. IF x MOD M # 0 THEN mult := "K"; x := x DIV K
  88. ELSIF x MOD G # 0 THEN mult := "M"; x := x DIV M
  89. ELSE mult := "G"; x := x DIV G
  90. END;
  91. Int(x, w-1); Char(mult)
  92. END;
  93. String(suffix)
  94. END IntSuffix;
  95. (*
  96. (** Write "x" as a hexadecimal number. The absolute value of "w" is the field width. If "w" is negative, two hex digits are printed (x MOD 100H), otherwise 8 digits are printed. *)
  97. PROCEDURE Hex*(x: HUGEINT; w: LONGINT);
  98. VAR i, j: LONGINT; buf: ARRAY 10 OF CHAR;
  99. BEGIN
  100. IF w >= 0 THEN j := 8 ELSE j := 2; w := -w END;
  101. FOR i := j+1 TO w DO Char(" ") END;
  102. FOR i := j-1 TO 0 BY -1 DO
  103. buf[i] := CHR(x MOD 10H + 48);
  104. IF buf[i] > "9" THEN
  105. buf[i] := CHR(ORD(buf[i]) - 48 + 65 - 10)
  106. END;
  107. x := x DIV 10H
  108. END;
  109. buf[j] := 0X;
  110. String(buf)
  111. END Hex;
  112. *)
  113. (** Write an integer in hexadecimal right-justified in a field of at least ABS(w) characters.
  114. If w < 0, the w least significant hex digits of x are written (potentially including leading zeros)
  115. *)
  116. PROCEDURE Hex*( x: HUGEINT; w: LONGINT );
  117. VAR filler: CHAR; i, maxi, y: LONGINT; buf: ARRAY 20 OF CHAR;
  118. BEGIN
  119. IF w < 0 THEN filler := '0'; w := -w; maxi := w ELSE filler := ' '; maxi := 16 END;
  120. i := 0;
  121. REPEAT
  122. y := SHORT( x MOD 10H );
  123. IF y < 10 THEN buf[i] := CHR( y + ORD('0') ) ELSE buf[i] := CHR( y - 10 + ORD('A') ) END;
  124. x := x DIV 10H;
  125. INC( i );
  126. UNTIL (x = 0) OR (i = maxi);
  127. WHILE w > i DO Char( filler ); DEC( w ) END;
  128. REPEAT DEC( i ); Char( buf[i] ) UNTIL i = 0
  129. END Hex;
  130. (** Write "x" as a hexadecimal address. *)
  131. PROCEDURE Address* (x: ADDRESS);
  132. BEGIN
  133. Hex(x, -SIZEOF(ADDRESS)*2)
  134. END Address;
  135. (** Write "x" as a hexadecimal number. "w" is the field width. Always prints 16 digits. *)
  136. PROCEDURE HIntHex*(x: HUGEINT; w: LONGINT);
  137. BEGIN
  138. Hex( x, w )
  139. END HIntHex;
  140. (** Write a block of memory in hex. *)
  141. PROCEDURE Memory*(adr: ADDRESS; size: SIZE);
  142. VAR i, j: ADDRESS; ch: CHAR;
  143. BEGIN
  144. Char(0EX); (* "fixed font" *)
  145. size := adr+size-1;
  146. FOR i := adr TO size BY 16 DO
  147. Address (i); Char (' ');
  148. FOR j := i TO i+15 DO
  149. IF j <= size THEN
  150. SYSTEM.GET(j, ch);
  151. Hex(ORD(ch), -2)
  152. ELSE
  153. String(" ")
  154. END
  155. END;
  156. String(" ");
  157. FOR j := i TO i+15 DO
  158. IF j <= size THEN
  159. SYSTEM.GET(j, ch);
  160. IF (ch < " ") OR (ch >= CHR(127)) THEN ch := "." END;
  161. Char(ch)
  162. END
  163. END;
  164. Ln
  165. END;
  166. Char(0FX) (* "proportional font" *)
  167. END Memory;
  168. (** Write a buffer in hex. *)
  169. PROCEDURE Buffer*(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT);
  170. BEGIN
  171. Memory(ADDRESSOF(buf[ofs]), len)
  172. END Buffer;
  173. (** Write bits (ofs..ofs+n-1) of x in binary. *)
  174. PROCEDURE Bits*(x: SET; ofs, n: LONGINT);
  175. BEGIN
  176. REPEAT
  177. DEC(n);
  178. IF (ofs+n) IN x THEN Char("1") ELSE Char("0") END
  179. UNTIL n = 0
  180. END Bits;
  181. (** write a set as set *)
  182. PROCEDURE Set*(x: SET);
  183. VAR first: BOOLEAN; i: LONGINT;
  184. BEGIN
  185. first := TRUE;
  186. Char("{");
  187. FOR i := 0 TO MAX(SET) DO
  188. IF i IN x THEN
  189. IF ~first THEN Char(",") ELSE first := FALSE END;
  190. Int(i,1);
  191. END;
  192. END;
  193. Char("}");
  194. END Set;
  195. (** Enter mutually exclusive region for writing, using a fine-grained lock. This region should be kept as short as possible, and only procedures from KernelLog should be called inside it. *)
  196. PROCEDURE Enter*;
  197. BEGIN
  198. Machine.Acquire(Machine.KernelLog);
  199. String("{P cpuid= "); Int(Machine.ID(), 0); String (", pid= "); Int (Objects.GetProcessID (), 0); Char (' ');
  200. END Enter;
  201. (** Exit mutually exclusive region for writing. *)
  202. PROCEDURE Exit*;
  203. BEGIN
  204. Char("}"); Ln;
  205. Machine.Release(Machine.KernelLog)
  206. END Exit;
  207. (* Switch to a new tracing buffer, copying the existing data. *)
  208. PROCEDURE SwitchToBuffer(adr: ADDRESS; size: SIZE);
  209. VAR tail: ADDRESS; c: CHAR;
  210. BEGIN
  211. tail := 0; ASSERT(size > 0);
  212. WHILE (traceHead # traceTail) & (tail+1 # size) DO (* source not empty, destination not full *)
  213. SYSTEM.GET (traceBufAdr + traceHead, c);
  214. SYSTEM.PUT (adr + tail, c);
  215. traceHead := (traceHead+1) MOD traceBufSize;
  216. INC(tail)
  217. END;
  218. traceBufAdr := adr; traceBufSize := size;
  219. traceHead := 0; traceTail := tail
  220. END SwitchToBuffer;
  221. (** Assign a new trace buffer. Used by a display process. *)
  222. PROCEDURE OpenBuffer*(adr: ADDRESS; size: SIZE): BOOLEAN;
  223. VAR ok: BOOLEAN;
  224. BEGIN
  225. Machine.Acquire(Machine.TraceOutput);
  226. IF traceBufAdr = ADDRESSOF(traceBufDef[0]) THEN
  227. SwitchToBuffer(adr, size); ok := TRUE
  228. ELSE
  229. ok := FALSE
  230. END;
  231. Machine.Release(Machine.TraceOutput);
  232. RETURN ok
  233. END OpenBuffer;
  234. (** Return output buffer contents. Used by a display process. *)
  235. PROCEDURE GetBuffer*(VAR val: ARRAY OF CHAR);
  236. VAR i, m: LONGINT;
  237. BEGIN
  238. i := 0; m := LEN(val)-1;
  239. Machine.Acquire(Machine.TraceOutput);
  240. WHILE (i < m) & (traceHead # traceTail) DO
  241. val[i] := CHR(SYSTEM.GET8(traceBufAdr + traceHead));
  242. traceHead := (traceHead+1) MOD traceBufSize;
  243. INC(i)
  244. END;
  245. Machine.Release(Machine.TraceOutput);
  246. val[i] := 0X
  247. END GetBuffer;
  248. (** Close the trace buffer and revert to the default. Used by a display process. *)
  249. PROCEDURE CloseBuffer*;
  250. BEGIN
  251. Machine.Acquire(Machine.TraceOutput);
  252. IF traceBufAdr # ADDRESSOF(traceBufDef[0]) THEN
  253. SwitchToBuffer(ADDRESSOF(traceBufDef[0]), LEN(traceBufDef))
  254. END;
  255. Machine.Release(Machine.TraceOutput)
  256. END CloseBuffer;
  257. BEGIN
  258. traceBufAdr := ADDRESSOF(traceBufDef[0]);
  259. traceBufSize := LEN(traceBufDef);
  260. traceHead := 0; traceTail := 0;
  261. END KernelLog.
  262. (**
  263. Notes
  264. This module provides low-level output facilities for Aos. It is similar to the Out module of Oberon, but it can be called from anywhere, even from active object bodies and interrupt handlers. It can write to the text display (when not using a graphics mode), a serial port, a memory buffer, or all of the above. This is controlled by the TraceMode and related config strings (see Aos.Par).
  265. Typically, a memory buffer is used. The buffer is installed by the LogWindow.Open, or with the System.StartLog command when using Oberon. The latter is recommended, as it also interprets traps specially and opens a new viewer for them. The displaying of the buffer is done off-line by the LogWindow or Oberon threads, thereby allowing the procedures here to be called from anywhere.
  266. Control characters:
  267. 0X end of string (can not be printed)
  268. 1X start of trap (if System.StartLog active then trap viewer will be opened and output redirected)
  269. 2X end of trap (if System.StartLog active then it will revert output to the kernel log text)
  270. 3X signal log overflow
  271. 9X TAB (or single space)
  272. 0DX CR (or NL and LF ignored)
  273. 0AX LF (ignored if CR is NL)
  274. 0EX set fixed-width font
  275. 0FX set proportial font (default)
  276. *)
  277. (*
  278. TraceMode:
  279. 0 1 Screen
  280. 2 4 V24
  281. *)
  282. (*
  283. 03.03.1998 pjm First version
  284. 16.06.2000 pjm Cleaned up
  285. 29.11.2000 pjm buffering
  286. 12.06.2001 pjm moved Flags to Traps, moved SegDesc and TSS to AosFragments
  287. *)