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

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