Bugfixes, Cleanup and Rewite started on UDI interface
[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                 // Single Character / Array
157                 case 'c':
158                         if(minSize == 1) {
159                                 E9(arg);
160                                 break;
161                         }
162                         p = (char*)(Uint)arg;
163                         if(!p)  goto printString;
164                         while(minSize--)        E9(*p++);
165                         break;
166                 
167                 default:        E9(arg);        break;
168                 }
169     }
170 }
171
172 /**
173  * \fn void LogV(char *Fmt, va_list Args)
174  */
175 void LogV(char *Fmt, va_list Args)
176 {
177         E9_Str("Log: ");
178         E9_Fmt(Fmt, &Args);
179         E9('\n');
180 }
181 /**
182  * \fn void LogF(char *Msg, ...)
183  */
184 void LogF(char *Fmt, ...)
185 {
186         va_list args;
187         
188         va_start(args, Fmt);
189         
190         E9_Fmt(Fmt, &args);
191         
192         va_end(args);
193 }
194 /**
195  * \fn void Log(char *Msg, ...)
196  */
197 void Log(char *Fmt, ...)
198 {
199         va_list args;
200         
201         E9_Str("Log: ");
202         va_start(args, Fmt);
203         E9_Fmt(Fmt, &args);
204         va_end(args);
205         E9('\n');
206 }
207 void Warning(char *Fmt, ...)
208 {
209         va_list args;
210         E9_Str("Warning: ");
211         va_start(args, Fmt);
212         E9_Fmt(Fmt, &args);
213         va_end(args);
214         E9('\n');
215 }
216 void Panic(char *Fmt, ...)
217 {
218         va_list args;
219         E9_Str("Panic: ");
220         va_start(args, Fmt);
221         E9_Fmt(Fmt, &args);
222         va_end(args);
223         E9('\n');
224         
225         Threads_Dump();
226         
227         __asm__ __volatile__ ("xchg %bx, %bx");
228         __asm__ __volatile__ ("cli;\n\thlt");
229         for(;;) __asm__ __volatile__ ("hlt");
230 }
231
232 void Debug_SetKTerminal(char *File)
233 {
234         if(giDebug_KTerm != -1)
235                 VFS_Close(giDebug_KTerm);
236         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
237         Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
238 }
239
240 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
241 {
242         va_list args;
243          int    i = gDebug_Level ++;
244          int    pos;
245         
246         va_start(args, ArgTypes);
247         
248         while(i--)      E9(' ');
249         
250         E9_Str(FuncName);       E9_Str(": (");
251         
252         while(*ArgTypes)
253         {
254                 pos = strpos(ArgTypes, ' ');
255                 if(pos != -1)   ArgTypes[pos] = '\0';
256                 if(pos == -1 || pos > 1) {
257                         E9_Str(ArgTypes+1);
258                         E9('=');
259                 }
260                 if(pos != -1)   ArgTypes[pos] = ' ';
261                 switch(*ArgTypes)
262                 {
263                 case 'p':       E9_Fmt("%p", &args);    break;
264                 case 's':       E9_Fmt("'%s'", &args);  break;
265                 case 'i':       E9_Fmt("%i", &args);    break;
266                 case 'u':       E9_Fmt("%u", &args);    break;
267                 case 'x':       E9_Fmt("0x%x", &args);  break;
268                 case 'b':       E9_Fmt("0b%b", &args);  break;
269                 // Extended (64-Bit)
270                 case 'X':       E9_Fmt("0x%llx", &args);        break;
271                 case 'B':       E9_Fmt("0b%llb", &args);        break;
272                 }
273                 if(pos != -1) {
274                         E9(',');        E9(' ');
275                 }
276                 
277                 if(pos == -1)   break;
278                 ArgTypes = &ArgTypes[pos+1];
279         }
280         
281         va_end(args);
282         E9(')');        E9('\n');
283 }
284
285 void Debug_Log(char *FuncName, char *Fmt, ...)
286 {
287         va_list args;
288          int    i = gDebug_Level;
289         
290         va_start(args, Fmt);
291         
292         while(i--)      E9(' ');
293         
294         E9_Str(FuncName);       E9_Str(": ");
295         E9_Fmt(Fmt, &args);
296         
297         va_end(args);
298         E9('\n');
299 }
300
301 void Debug_Leave(char *FuncName, char RetType, ...)
302 {
303         va_list args;
304          int    i = --gDebug_Level;
305         
306         va_start(args, RetType);
307         
308         if( i == -1 ) {
309                 gDebug_Level = 0;
310                 i = 0;
311         }
312         // Indenting
313         while(i--)      E9(' ');
314         
315         E9_Str(FuncName);       E9_Str(": RETURN");
316         
317         // No Return
318         if(RetType == '-') {
319                 E9('\n');
320                 return;
321         }
322         
323         E9(' ');
324         switch(RetType)
325         {
326         case 'n':       E9_Str("NULL"); break;
327         case 'p':       E9_Fmt("%p", &args);    break;
328         case 's':       E9_Fmt("'%s'", &args);  break;
329         case 'i':       E9_Fmt("%i", &args);    break;
330         case 'u':       E9_Fmt("%u", &args);    break;
331         case 'x':       E9_Fmt("0x%x", &args);  break;
332         // Extended (64-Bit)
333         case 'X':       E9_Fmt("0x%llx", &args);        break;
334         }
335         E9('\n');
336         
337         va_end(args);
338 }
339
340 void Debug_HexDump(char *Header, void *Data, Uint Length)
341 {
342         Uint8   *cdat = Data;
343         Uint    pos = 0;
344         E9_Str(Header);
345         LogF(" (Hexdump of %p)\n", Data);
346         
347         while(Length >= 16)
348         {
349                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
350                         pos,
351                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
352                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
353                         );
354                 Length -= 16;
355                 cdat += 16;
356                 pos += 16;
357         }
358         
359         LogF("Log: %04x: ", pos);
360         while(Length)
361         {
362                 Uint    byte = *cdat;
363                 LogF("%02x ", byte);
364                 Length--;
365                 cdat ++;
366         }
367         E9('\n');
368 }
369
370 // --- EXPORTS ---
371 EXPORT(Log);
372 EXPORT(Warning);
373 EXPORT(Debug_Enter);
374 EXPORT(Debug_Log);
375 EXPORT(Debug_Leave);

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