Replace rand() implementation - fixes threading lockups
[tpg/acess2.git] / Kernel / debug.c
1 /*
2  * AcessOS Microkernel Version
3  * debug.c
4  * 
5  * TODO: Move the Debug_putchar methods out to the arch/ tree
6  */
7 #include <acess.h>
8 #include <stdarg.h>
9
10 #define DEBUG_TO_E9     1
11 #define DEBUG_TO_SERIAL 1
12 #define SERIAL_PORT     0x3F8
13 #define GDB_SERIAL_PORT 0x2F8
14 #define DEBUG_MAX_LINE_LEN      256
15
16 #define LOCK_DEBUG_OUTPUT       1
17
18 // === IMPORTS ===
19 extern void Threads_Dump(void);
20 extern void     KernelPanic_SetMode(void);
21 extern void     KernelPanic_PutChar(char Ch);
22
23 // === PROTOTYPES ===
24  int    putDebugChar(char ch);
25  int    getDebugChar(void);
26 static void     Debug_Putchar(char ch);
27 static void     Debug_Puts(int DbgOnly, char *Str);
28 void    Debug_Fmt(const char *format, va_list args);
29
30 // === GLOBALS ===
31  int    gDebug_Level = 0;
32  int    giDebug_KTerm = -1;
33  int    gbDebug_SerialSetup = 0;
34  int    gbGDB_SerialSetup = 0;
35  int    gbDebug_IsKPanic = 0;
36 volatile int    gbInPutChar = 0;
37 #if LOCK_DEBUG_OUTPUT
38 tShortSpinlock  glDebug_Lock;
39 #endif
40
41 // === CODE ===
42 int putDebugChar(char ch)
43 {
44         if(!gbGDB_SerialSetup) {
45                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
46                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
47                 outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
48                 outb(GDB_SERIAL_PORT + 1, 0x00);    //  (base is         (hi byte)
49                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit (8N1)
50                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
51                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
52                 gbDebug_SerialSetup = 1;
53         }
54         while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
55         outb(GDB_SERIAL_PORT, ch);
56         return 0;
57 }
58 int getDebugChar(void)
59 {
60         if(!gbGDB_SerialSetup) {
61                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
62                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
63                 outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
64                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                   (hi byte)
65                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
66                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
67                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
68                 gbDebug_SerialSetup = 1;
69         }
70         while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
71         return inb(GDB_SERIAL_PORT);
72 }
73
74 static void Debug_PutCharDebug(char ch)
75 {
76         #if DEBUG_TO_E9
77         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
78         #endif
79         
80         #if DEBUG_TO_SERIAL
81         if(!gbDebug_SerialSetup) {
82                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
83                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
84                 outb(SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
85                 outb(SERIAL_PORT + 1, 0x00);    //                   (hi byte)
86                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
87                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
88                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
89                 gbDebug_SerialSetup = 1;
90         }
91         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
92         outb(SERIAL_PORT, ch);
93         #endif
94 }
95
96 static void Debug_Putchar(char ch)
97 {       
98         Debug_PutCharDebug(ch);
99         if( !gbDebug_IsKPanic )
100         {
101                 if(gbInPutChar) return ;
102                 gbInPutChar = 1;
103                 if(giDebug_KTerm != -1)
104                         VFS_Write(giDebug_KTerm, 1, &ch);
105                 gbInPutChar = 0;
106         }
107         else
108                 KernelPanic_PutChar(ch);
109 }
110
111 static void Debug_Puts(int UseKTerm, char *Str)
112 {
113          int    len = 0;
114         while( *Str )
115         {
116                 Debug_PutCharDebug( *Str );
117                 
118                 if( gbDebug_IsKPanic )
119                         KernelPanic_PutChar(*Str);
120                 len ++;
121                 Str ++;
122         }
123         
124         Str -= len;
125         
126         if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
127         {
128                 if(gbInPutChar) return ;
129                 gbInPutChar = 1;
130                 VFS_Write(giDebug_KTerm, len, Str);
131                 gbInPutChar = 0;
132         }
133 }
134
135 void Debug_DbgOnlyFmt(const char *format, va_list args)
136 {
137         char    buf[DEBUG_MAX_LINE_LEN];
138          int    len;
139         buf[DEBUG_MAX_LINE_LEN-1] = 0;
140         len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
141         //if( len < DEBUG_MAX_LINE )
142                 // do something
143         Debug_Puts(0, buf);
144 }
145
146 void Debug_Fmt(const char *format, va_list args)
147 {
148         char    buf[DEBUG_MAX_LINE_LEN];
149          int    len;
150         buf[DEBUG_MAX_LINE_LEN-1] = 0;
151         len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
152         //if( len < DEBUG_MAX_LINE )
153                 // do something
154         Debug_Puts(1, buf);
155         return ;
156 }
157
158 void Debug_FmtS(const char *format, ...)
159 {
160         va_list args;   
161         va_start(args, format);
162         Debug_Fmt(format, args);
163         va_end(args);
164 }
165
166 void Debug_KernelPanic()
167 {
168         gbDebug_IsKPanic = 1;
169         KernelPanic_SetMode();
170 }
171
172 /**
173  * \fn void LogF(char *Msg, ...)
174  */
175 void LogF(char *Fmt, ...)
176 {
177         va_list args;
178
179         #if LOCK_DEBUG_OUTPUT
180         SHORTLOCK(&glDebug_Lock);
181         #endif
182         
183         va_start(args, Fmt);
184
185         Debug_Fmt(Fmt, args);
186
187         va_end(args);
188         
189         #if LOCK_DEBUG_OUTPUT
190         SHORTREL(&glDebug_Lock);
191         #endif
192 }
193 /**
194  * \fn void Debug(char *Msg, ...)
195  * \brief Print only to the debug channel
196  */
197 void Debug(char *Fmt, ...)
198 {
199         va_list args;
200         
201         #if LOCK_DEBUG_OUTPUT
202         SHORTLOCK(&glDebug_Lock);
203         #endif
204
205         Debug_Puts(0, "Debug: ");
206         va_start(args, Fmt);
207         Debug_DbgOnlyFmt(Fmt, args);
208         va_end(args);
209         Debug_PutCharDebug('\r');
210         Debug_PutCharDebug('\n');
211         #if LOCK_DEBUG_OUTPUT
212         SHORTREL(&glDebug_Lock);
213         #endif
214 }
215 /**
216  * \fn void Log(char *Msg, ...)
217  */
218 void Log(char *Fmt, ...)
219 {
220         va_list args;
221         
222         #if LOCK_DEBUG_OUTPUT
223         SHORTLOCK(&glDebug_Lock);
224         #endif
225
226         Debug_Puts(1, "Log: ");
227         va_start(args, Fmt);
228         Debug_Fmt(Fmt, args);
229         va_end(args);
230         Debug_Putchar('\r');
231         Debug_Putchar('\n');
232         
233         #if LOCK_DEBUG_OUTPUT
234         SHORTREL(&glDebug_Lock);
235         #endif
236 }
237 void Warning(char *Fmt, ...)
238 {
239         va_list args;
240         
241         #if LOCK_DEBUG_OUTPUT
242         SHORTLOCK(&glDebug_Lock);
243         #endif
244         
245         Debug_Puts(1, "Warning: ");
246         va_start(args, Fmt);
247         Debug_Fmt(Fmt, args);
248         va_end(args);
249         Debug_Putchar('\r');
250         Debug_Putchar('\n');
251         
252         #if LOCK_DEBUG_OUTPUT
253         SHORTREL(&glDebug_Lock);
254         #endif
255 }
256 void Panic(char *Fmt, ...)
257 {
258         va_list args;
259         
260         #if LOCK_DEBUG_OUTPUT
261         SHORTLOCK(&glDebug_Lock);
262         #endif
263         // And never SHORTREL
264         
265         Debug_KernelPanic();
266         
267         Debug_Puts(1, "Panic: ");
268         va_start(args, Fmt);
269         Debug_Fmt(Fmt, args);
270         va_end(args);
271         Debug_Putchar('\r');
272         Debug_Putchar('\n');
273
274         Threads_Dump();
275
276         __asm__ __volatile__ ("xchg %bx, %bx");
277         __asm__ __volatile__ ("cli;\n\thlt");
278         for(;;) __asm__ __volatile__ ("hlt");
279 }
280
281 void Debug_SetKTerminal(char *File)
282 {
283          int    tmp;
284         if(giDebug_KTerm != -1) {
285                 tmp = giDebug_KTerm;
286                 giDebug_KTerm = -1;
287                 VFS_Close(tmp);
288         }
289         tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
290         Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
291         giDebug_KTerm = tmp;
292         Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
293 }
294
295 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
296 {
297         va_list args;
298          int    i;
299          int    pos;
300         tTID    tid = Threads_GetTID();
301          
302         #if LOCK_DEBUG_OUTPUT
303         SHORTLOCK(&glDebug_Lock);
304         #endif
305
306         i = gDebug_Level ++;
307
308         va_start(args, ArgTypes);
309
310         while(i--)      Debug_Putchar(' ');
311
312         Debug_Puts(1, FuncName);
313         Debug_FmtS("[%i]", tid);
314         Debug_Puts(1, ": (");
315
316         while(*ArgTypes)
317         {
318                 pos = strpos(ArgTypes, ' ');
319                 if(pos != -1)   ArgTypes[pos] = '\0';
320                 if(pos == -1 || pos > 1) {
321                         Debug_Puts(1, ArgTypes+1);
322                         Debug_Putchar('=');
323                 }
324                 if(pos != -1)   ArgTypes[pos] = ' ';
325                 switch(*ArgTypes)
326                 {
327                 case 'p':       LogF("%p", va_arg(args, void*));        break;
328                 case 's':       LogF("'%s'", va_arg(args, char*));      break;
329                 case 'i':       LogF("%i", va_arg(args, int));  break;
330                 case 'u':       LogF("%u", va_arg(args, Uint)); break;
331                 case 'x':       LogF("0x%x", va_arg(args, Uint));       break;
332                 case 'b':       LogF("0b%b", va_arg(args, Uint));       break;
333                 case 'X':       LogF("0x%llx", va_arg(args, Uint64));   break;  // Extended (64-Bit)
334                 case 'B':       LogF("0b%llb", va_arg(args, Uint64));   break;  // Extended (64-Bit)
335                 }
336                 if(pos != -1) {
337                         Debug_Putchar(',');     Debug_Putchar(' ');
338                 }
339
340                 if(pos == -1)   break;
341                 ArgTypes = &ArgTypes[pos+1];
342         }
343
344         va_end(args);
345         Debug_Putchar(')');     Debug_Putchar('\r');    Debug_Putchar('\n');
346         
347         #if LOCK_DEBUG_OUTPUT
348         SHORTREL(&glDebug_Lock);
349         #endif
350 }
351
352 void Debug_Log(char *FuncName, char *Fmt, ...)
353 {
354         va_list args;
355          int    i = gDebug_Level;
356         tTID    tid = Threads_GetTID();
357
358         #if LOCK_DEBUG_OUTPUT
359         SHORTLOCK(&glDebug_Lock);
360         #endif
361
362         va_start(args, Fmt);
363
364         while(i--)      Debug_Putchar(' ');
365
366         Debug_Puts(1, FuncName);
367         Debug_FmtS("[%i]", tid);
368         Debug_Puts(1, ": ");
369         Debug_Fmt(Fmt, args);
370
371         va_end(args);
372         Debug_Putchar('\r');
373         Debug_Putchar('\n');
374         
375         #if LOCK_DEBUG_OUTPUT
376         SHORTREL(&glDebug_Lock);
377         #endif
378 }
379
380 void Debug_Leave(char *FuncName, char RetType, ...)
381 {
382         va_list args;
383          int    i;
384         tTID    tid = Threads_GetTID();
385
386         #if LOCK_DEBUG_OUTPUT
387         SHORTLOCK(&glDebug_Lock);
388         #endif
389         
390         i = --gDebug_Level;
391
392         va_start(args, RetType);
393
394         if( i == -1 ) {
395                 gDebug_Level = 0;
396                 i = 0;
397         }
398         // Indenting
399         while(i--)      Debug_Putchar(' ');
400
401         Debug_Puts(1, FuncName);
402         Debug_FmtS("(%i)", tid);
403         Debug_Puts(1, ": RETURN");
404
405         // No Return
406         if(RetType == '-') {
407                 Debug_Putchar('\r');
408                 Debug_Putchar('\n');
409                 #if LOCK_DEBUG_OUTPUT
410                 SHORTREL(&glDebug_Lock);
411                 #endif
412                 return;
413         }
414
415         Debug_Putchar(' ');
416         switch(RetType)
417         {
418         case 'n':       Debug_Puts(1, "NULL");  break;
419         case 'p':       Debug_Fmt("%p", args);  break;
420         case 's':       Debug_Fmt("'%s'", args);        break;
421         case 'i':       Debug_Fmt("%i", args);  break;
422         case 'u':       Debug_Fmt("%u", args);  break;
423         case 'x':       Debug_Fmt("0x%x", args);        break;
424         // Extended (64-Bit)
425         case 'X':       Debug_Fmt("0x%llx", args);      break;
426         }
427         Debug_Putchar('\r');
428         Debug_Putchar('\n');
429
430         va_end(args);
431         
432         #if LOCK_DEBUG_OUTPUT
433         SHORTREL(&glDebug_Lock);
434         #endif
435 }
436
437 void Debug_HexDump(char *Header, void *Data, Uint Length)
438 {
439         Uint8   *cdat = Data;
440         Uint    pos = 0;
441         Debug_Puts(1, Header);
442         LogF(" (Hexdump of %p)\r\n", Data);
443
444         #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
445
446         while(Length >= 16)
447         {
448                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
449                         " %02x %02x %02x %02x %02x %02x %02x %02x"
450                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
451                         pos,
452                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
453                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
454                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
455                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
456                         );
457                 Length -= 16;
458                 cdat += 16;
459                 pos += 16;
460         }
461
462         {
463                  int    i ;
464                 LogF("Log: %04x: ", pos);
465                 for(i = 0; i < Length; i ++)
466                 {
467                         LogF("%02x ", cdat[i]);
468                 }
469                 for( ; i < 16; i ++)    LogF("   ");
470                 LogF(" ");
471                 for(i = 0; i < Length; i ++)
472                 {
473                         if( i == 8 )    LogF(" ");
474                         LogF("%c", CH(i));
475                 }
476         }
477         
478         Debug_Putchar('\r');
479         Debug_Putchar('\n');
480 }
481
482 // --- EXPORTS ---
483 EXPORT(Debug);
484 EXPORT(Log);
485 EXPORT(Warning);
486 EXPORT(Debug_Enter);
487 EXPORT(Debug_Log);
488 EXPORT(Debug_Leave);

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