ATA Fixes, Module compilation fixups
[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('\n');
185 }
186 /**
187  * \fn void Log(char *Msg, ...)
188  */
189 void Log(char *Fmt, ...)
190 {
191         va_list args;
192
193         Debug_Puts(1, "Log: ");
194         va_start(args, Fmt);
195         Debug_Fmt(Fmt, args);
196         va_end(args);
197         Debug_Putchar('\n');
198 }
199 void Warning(char *Fmt, ...)
200 {
201         va_list args;
202         Debug_Puts(1, "Warning: ");
203         va_start(args, Fmt);
204         Debug_Fmt(Fmt, args);
205         va_end(args);
206         Debug_Putchar('\n');
207 }
208 void Panic(char *Fmt, ...)
209 {
210         va_list args;
211         
212         Debug_KernelPanic();
213         
214         Debug_Puts(1, "Panic: ");
215         va_start(args, Fmt);
216         Debug_Fmt(Fmt, args);
217         va_end(args);
218         Debug_Putchar('\n');
219
220         Threads_Dump();
221
222         __asm__ __volatile__ ("xchg %bx, %bx");
223         __asm__ __volatile__ ("cli;\n\thlt");
224         for(;;) __asm__ __volatile__ ("hlt");
225 }
226
227 void Debug_SetKTerminal(char *File)
228 {
229          int    tmp;
230         if(giDebug_KTerm != -1) {
231                 tmp = giDebug_KTerm;
232                 giDebug_KTerm = -1;
233                 VFS_Close(tmp);
234         }
235         tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
236         Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
237         giDebug_KTerm = tmp;
238         Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
239 }
240
241 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
242 {
243         va_list args;
244          int    i = gDebug_Level ++;
245          int    pos;
246
247         va_start(args, ArgTypes);
248
249         while(i--)      Debug_Putchar(' ');
250
251         Debug_Puts(1, FuncName);        Debug_Puts(1, ": (");
252
253         while(*ArgTypes)
254         {
255                 pos = strpos(ArgTypes, ' ');
256                 if(pos != -1)   ArgTypes[pos] = '\0';
257                 if(pos == -1 || pos > 1) {
258                         Debug_Puts(1, ArgTypes+1);
259                         Debug_Putchar('=');
260                 }
261                 if(pos != -1)   ArgTypes[pos] = ' ';
262                 switch(*ArgTypes)
263                 {
264                 case 'p':       LogF("%p", va_arg(args, void*));        break;
265                 case 's':       LogF("'%s'", va_arg(args, char*));      break;
266                 case 'i':       LogF("%i", va_arg(args, int));  break;
267                 case 'u':       LogF("%u", va_arg(args, Uint)); break;
268                 case 'x':       LogF("0x%x", va_arg(args, Uint));       break;
269                 case 'b':       LogF("0b%b", va_arg(args, Uint));       break;
270                 case 'X':       LogF("0x%llx", va_arg(args, Uint64));   break;  // Extended (64-Bit)
271                 case 'B':       LogF("0b%llb", va_arg(args, Uint64));   break;  // Extended (64-Bit)
272                 }
273                 if(pos != -1) {
274                         Debug_Putchar(',');     Debug_Putchar(' ');
275                 }
276
277                 if(pos == -1)   break;
278                 ArgTypes = &ArgTypes[pos+1];
279         }
280
281         va_end(args);
282         Debug_Putchar(')');     Debug_Putchar('\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--)      Debug_Putchar(' ');
293
294         Debug_Puts(1, FuncName);        Debug_Puts(1, ": ");
295         Debug_Fmt(Fmt, args);
296
297         va_end(args);
298         Debug_Putchar('\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--)      Debug_Putchar(' ');
314
315         Debug_Puts(1, FuncName);        Debug_Puts(1, ": RETURN");
316
317         // No Return
318         if(RetType == '-') {
319                 Debug_Putchar('\n');
320                 return;
321         }
322
323         Debug_Putchar(' ');
324         switch(RetType)
325         {
326         case 'n':       Debug_Puts(1, "NULL");  break;
327         case 'p':       Debug_Fmt("%p", args);  break;
328         case 's':       Debug_Fmt("'%s'", args);        break;
329         case 'i':       Debug_Fmt("%i", args);  break;
330         case 'u':       Debug_Fmt("%u", args);  break;
331         case 'x':       Debug_Fmt("0x%x", args);        break;
332         // Extended (64-Bit)
333         case 'X':       Debug_Fmt("0x%llx", args);      break;
334         }
335         Debug_Putchar('\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         Debug_Puts(1, Header);
345         LogF(" (Hexdump of %p)\n", Data);
346
347         while(Length >= 16)
348         {
349                 #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
350                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
351                         " %02x %02x %02x %02x %02x %02x %02x %02x"
352                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
353                         pos,
354                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
355                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
356                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
357                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
358                         );
359                 Length -= 16;
360                 cdat += 16;
361                 pos += 16;
362         }
363
364         LogF("Log: %04x: ", pos);
365         while(Length)
366         {
367                 Uint    byte = *cdat;
368                 LogF("%02x ", byte);
369                 Length--;
370                 cdat ++;
371         }
372         Debug_Putchar('\n');
373 }
374
375 // --- EXPORTS ---
376 EXPORT(Debug);
377 EXPORT(Log);
378 EXPORT(Warning);
379 EXPORT(Debug_Enter);
380 EXPORT(Debug_Log);
381 EXPORT(Debug_Leave);

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