b42459de3c20f84c3cdfcf9cbb20349e053e190c
[tpg/acess2.git] / Kernel / debug.c
1 /*
2  * AcessOS Microkernel Version
3  * debug.c
4  */
5 #include <acess.h>
6 #include <stdarg.h>
7
8 #define DEBUG_TO_E9     1
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT     0x3F8
11
12 // === IMPORTS ===
13 extern void Threads_Dump();
14
15 // === GLOBALS ===
16  int    gDebug_Level = 0;
17  int    giDebug_KTerm = -1;
18  int    gbDebug_SerialSetup = 0;
19
20 // === CODE ===
21 static void E9(char ch)
22 {
23         if(giDebug_KTerm != -1)
24                 VFS_Write(giDebug_KTerm, 1, &ch);
25         
26         #if DEBUG_TO_SERIAL
27         if(!gbDebug_SerialSetup) {
28                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
29                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
30                 outb(SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
31                 outb(SERIAL_PORT + 1, 0x00);    //                  (hi byte)
32                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
33                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
34                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
35                 gbDebug_SerialSetup = 1;
36         }
37         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
38         outb(SERIAL_PORT, ch);
39         #endif
40         
41         #if DEBUG_TO_E9
42         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
43         #endif
44 }
45
46 static void E9_Str(char *Str)
47 {
48         while(*Str)     E9(*Str++);
49 }
50
51 void E9_Fmt(const char *format, va_list *args)
52 {
53         char    c, pad = ' ';
54          int    minSize = 0;
55         char    tmpBuf[34];     // For Integers
56         char    *p = NULL;
57          int    isLongLong = 0;
58         Uint64  arg;
59   
60         while((c = *format++) != 0)
61         {
62                 // Non control character
63                 if( c != '%' ) {
64                         E9(c);
65                         continue;
66                 }
67                 
68                 c = *format++;
69                 
70                 // Literal %
71                 if(c == '%') {
72                         E9('%');
73                         continue;
74                 }
75                 
76                 // Pointer
77                 if(c == 'p') {
78                         Uint    ptr = va_arg(*args, Uint);
79                         E9('*');        E9('0');        E9('x');
80                         p = tmpBuf;
81                         itoa(p, ptr, 16, BITS/4, '0');
82                         goto printString;
83                 }
84                 
85                 // Get Argument
86                 arg = va_arg(*args, Uint);
87                 
88                 // Padding
89                 if(c == '0') {
90                         pad = '0';
91                         c = *format++;
92                 } else
93                         pad = ' ';
94                 
95                 // Minimum length
96                 minSize = 1;
97                 if('1' <= c && c <= '9')
98                 {
99                         minSize = 0;
100                         while('0' <= c && c <= '9')
101                         {
102                                 minSize *= 10;
103                                 minSize += c - '0';
104                                 c = *format++;
105                         }
106                 }
107                 
108                 // Long (default)
109                 isLongLong = 0;
110                 if(c == 'l') {
111                         c = *format++;
112                         if(c == 'l') {
113                                 #if BITS == 32
114                                 arg |= va_arg(*args, Uint);
115                                 #endif
116                                 c = *format++;
117                                 isLongLong = 1;
118                         }
119                 }
120                 
121                 p = tmpBuf;
122                 switch (c) {
123                 case 'd':
124                 case 'i':
125                         if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
126                                 E9('-');
127                                 arg = -arg;
128                         }
129                         itoa(p, arg, 10, minSize, pad);
130                         goto printString;
131                 case 'u':
132                         itoa(p, arg, 10, minSize, pad);
133                         goto printString;
134                 case 'x':
135                         itoa(p, arg, 16, minSize, pad);
136                         goto printString;
137                 case 'o':
138                         itoa(p, arg, 8, minSize, pad);
139                         goto printString;
140                 case 'b':
141                         itoa(p, arg, 2, minSize, pad);
142                         goto printString;
143
144                 case 'B':       //Boolean
145                         if(arg) E9_Str("True");
146                         else    E9_Str("False");
147                         break;
148                 
149                 case 's':
150                         p = (char*)(Uint)arg;
151                 printString:
152                         if(!p)          p = "(null)";
153                         while(*p)       E9(*p++);
154                         break;
155                         
156                 default:        E9(arg);        break;
157                 }
158     }
159 }
160
161 /**
162  * \fn void LogV(char *Fmt, va_list Args)
163  */
164 void LogV(char *Fmt, va_list Args)
165 {
166         E9_Str("Log: ");
167         E9_Fmt(Fmt, &Args);
168         E9('\n');
169 }
170 /**
171  * \fn void LogF(char *Msg, ...)
172  */
173 void LogF(char *Fmt, ...)
174 {
175         va_list args;
176         
177         va_start(args, Fmt);
178         
179         E9_Fmt(Fmt, &args);
180         
181         va_end(args);
182 }
183 /**
184  * \fn void Log(char *Msg, ...)
185  */
186 void Log(char *Fmt, ...)
187 {
188         va_list args;
189         
190         E9_Str("Log: ");
191         va_start(args, Fmt);
192         E9_Fmt(Fmt, &args);
193         va_end(args);
194         E9('\n');
195 }
196 void Warning(char *Fmt, ...)
197 {
198         va_list args;
199         E9_Str("Warning: ");
200         va_start(args, Fmt);
201         E9_Fmt(Fmt, &args);
202         va_end(args);
203         E9('\n');
204 }
205 void Panic(char *Fmt, ...)
206 {
207         va_list args;
208         E9_Str("Panic: ");
209         va_start(args, Fmt);
210         E9_Fmt(Fmt, &args);
211         va_end(args);
212         E9('\n');
213         
214         Threads_Dump();
215         
216         __asm__ __volatile__ ("xchg %bx, %bx");
217         __asm__ __volatile__ ("cli;\n\thlt");
218         for(;;) __asm__ __volatile__ ("hlt");
219 }
220
221 void Debug_SetKTerminal(char *File)
222 {
223         if(giDebug_KTerm != -1)
224                 VFS_Close(giDebug_KTerm);
225         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
226         Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
227 }
228
229 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
230 {
231         va_list args;
232          int    i = gDebug_Level ++;
233          int    pos;
234         
235         va_start(args, ArgTypes);
236         
237         while(i--)      E9(' ');
238         
239         E9_Str(FuncName);       E9_Str(": (");
240         
241         while(*ArgTypes)
242         {
243                 pos = strpos(ArgTypes, ' ');
244                 if(pos != -1)   ArgTypes[pos] = '\0';
245                 if(pos == -1 || pos > 1) {
246                         E9_Str(ArgTypes+1);
247                         E9('=');
248                 }
249                 if(pos != -1)   ArgTypes[pos] = ' ';
250                 switch(*ArgTypes)
251                 {
252                 case 'p':       E9_Fmt("%p", &args);    break;
253                 case 's':       E9_Fmt("'%s'", &args);  break;
254                 case 'i':       E9_Fmt("%i", &args);    break;
255                 case 'u':       E9_Fmt("%u", &args);    break;
256                 case 'x':       E9_Fmt("0x%x", &args);  break;
257                 case 'b':       E9_Fmt("0b%b", &args);  break;
258                 // Extended (64-Bit)
259                 case 'X':       E9_Fmt("0x%llx", &args);        break;
260                 case 'B':       E9_Fmt("0b%llb", &args);        break;
261                 }
262                 if(pos != -1) {
263                         E9(',');        E9(' ');
264                 }
265                 
266                 if(pos == -1)   break;
267                 ArgTypes = &ArgTypes[pos+1];
268         }
269         
270         va_end(args);
271         E9(')');        E9('\n');
272 }
273
274 void Debug_Log(char *FuncName, char *Fmt, ...)
275 {
276         va_list args;
277          int    i = gDebug_Level;
278         
279         va_start(args, Fmt);
280         
281         while(i--)      E9(' ');
282         
283         E9_Str(FuncName);       E9_Str(": ");
284         E9_Fmt(Fmt, &args);
285         
286         va_end(args);
287         E9('\n');
288 }
289
290 void Debug_Leave(char *FuncName, char RetType, ...)
291 {
292         va_list args;
293          int    i = --gDebug_Level;
294         
295         va_start(args, RetType);
296         
297         // Indenting
298         while(i--)      E9(' ');
299         
300         E9_Str(FuncName);       E9_Str(": RETURN");
301         
302         // No Return
303         if(RetType == '-') {
304                 E9('\n');
305                 return;
306         }
307         
308         E9(' ');
309         switch(RetType)
310         {
311         case 'n':       E9_Str("NULL"); break;
312         case 'p':       E9_Fmt("%p", &args);    break;
313         case 's':       E9_Fmt("'%s'", &args);  break;
314         case 'i':       E9_Fmt("%i", &args);    break;
315         case 'u':       E9_Fmt("%u", &args);    break;
316         case 'x':       E9_Fmt("0x%x", &args);  break;
317         // Extended (64-Bit)
318         case 'X':       E9_Fmt("0x%llx", &args);        break;
319         }
320         E9('\n');
321         
322         va_end(args);
323 }
324
325 void Debug_HexDump(char *Header, void *Data, Uint Length)
326 {
327         Uint8   *cdat = Data;
328         Uint    pos = 0;
329         E9_Str(Header);
330         LogF(" (Hexdump of %p)\n", Data);
331         
332         while(Length >= 16)
333         {
334                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
335                         pos,
336                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
337                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
338                         );
339                 Length -= 16;
340                 cdat += 16;
341                 pos += 16;
342         }
343         
344         LogF("Log: %04x: ", pos);
345         while(Length)
346         {
347                 Uint    byte = *cdat;
348                 LogF("%02x ", byte);
349                 Length--;
350                 cdat ++;
351         }
352         E9('\n');
353 }
354
355 // --- EXPORTS ---
356 EXPORT(Log);
357 EXPORT(Warning);
358 EXPORT(Debug_Enter);
359 EXPORT(Debug_Log);
360 EXPORT(Debug_Leave);

UCC git Repository :: git.ucc.asn.au