Fixed places where char* was used in place of const char*
[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, const 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, const 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(const char *Msg, ...)
174  * \brief Raw debug log (no new line, no prefix)
175  */
176 void LogF(const char *Fmt, ...)
177 {
178         va_list args;
179
180         #if LOCK_DEBUG_OUTPUT
181         SHORTLOCK(&glDebug_Lock);
182         #endif
183         
184         va_start(args, Fmt);
185
186         Debug_Fmt(Fmt, args);
187
188         va_end(args);
189         
190         #if LOCK_DEBUG_OUTPUT
191         SHORTREL(&glDebug_Lock);
192         #endif
193 }
194 /**
195  * \fn void Debug(const char *Msg, ...)
196  * \brief Print only to the debug channel
197  */
198 void Debug(const char *Fmt, ...)
199 {
200         va_list args;
201         
202         #if LOCK_DEBUG_OUTPUT
203         SHORTLOCK(&glDebug_Lock);
204         #endif
205
206         Debug_Puts(0, "Debug: ");
207         va_start(args, Fmt);
208         Debug_DbgOnlyFmt(Fmt, args);
209         va_end(args);
210         Debug_PutCharDebug('\r');
211         Debug_PutCharDebug('\n');
212         #if LOCK_DEBUG_OUTPUT
213         SHORTREL(&glDebug_Lock);
214         #endif
215 }
216 /**
217  * \fn void Log(const char *Msg, ...)
218  */
219 void Log(const char *Fmt, ...)
220 {
221         va_list args;
222         
223         #if LOCK_DEBUG_OUTPUT
224         SHORTLOCK(&glDebug_Lock);
225         #endif
226
227         Debug_Puts(1, "Log: ");
228         va_start(args, Fmt);
229         Debug_Fmt(Fmt, args);
230         va_end(args);
231         Debug_Putchar('\r');
232         Debug_Putchar('\n');
233         
234         #if LOCK_DEBUG_OUTPUT
235         SHORTREL(&glDebug_Lock);
236         #endif
237 }
238 void Warning(const char *Fmt, ...)
239 {
240         va_list args;
241         
242         #if LOCK_DEBUG_OUTPUT
243         SHORTLOCK(&glDebug_Lock);
244         #endif
245         
246         Debug_Puts(1, "Warning: ");
247         va_start(args, Fmt);
248         Debug_Fmt(Fmt, args);
249         va_end(args);
250         Debug_Putchar('\r');
251         Debug_Putchar('\n');
252         
253         #if LOCK_DEBUG_OUTPUT
254         SHORTREL(&glDebug_Lock);
255         #endif
256 }
257 void Panic(const char *Fmt, ...)
258 {
259         va_list args;
260         
261         #if LOCK_DEBUG_OUTPUT
262         SHORTLOCK(&glDebug_Lock);
263         #endif
264         // And never SHORTREL
265         
266         Debug_KernelPanic();
267         
268         Debug_Puts(1, "Panic: ");
269         va_start(args, Fmt);
270         Debug_Fmt(Fmt, args);
271         va_end(args);
272         Debug_Putchar('\r');
273         Debug_Putchar('\n');
274
275         Threads_Dump();
276
277         __asm__ __volatile__ ("xchg %bx, %bx");
278         __asm__ __volatile__ ("cli;\n\thlt");
279         for(;;) __asm__ __volatile__ ("hlt");
280 }
281
282 void Debug_SetKTerminal(const char *File)
283 {
284          int    tmp;
285         if(giDebug_KTerm != -1) {
286                 tmp = giDebug_KTerm;
287                 giDebug_KTerm = -1;
288                 VFS_Close(tmp);
289         }
290         tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
291         Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
292         giDebug_KTerm = tmp;
293         Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
294 }
295
296 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
297 {
298         va_list args;
299          int    i;
300          int    pos;
301         tTID    tid = Threads_GetTID();
302          
303         #if LOCK_DEBUG_OUTPUT
304         SHORTLOCK(&glDebug_Lock);
305         #endif
306
307         i = gDebug_Level ++;
308
309         va_start(args, ArgTypes);
310
311         while(i--)      Debug_Putchar(' ');
312
313         Debug_Puts(1, FuncName);
314         Debug_FmtS("[%i]", tid);
315         Debug_Puts(1, ": (");
316
317         while(*ArgTypes)
318         {
319                 pos = strpos(ArgTypes, ' ');
320                 if(pos == -1 || pos > 1) {
321                         if(pos == -1)
322                                 Debug_Puts(1, ArgTypes+1);
323                         else {
324                                 for( i = 1; i < pos; i ++ )
325                                         Debug_Putchar(ArgTypes[i]);
326                         }
327                         Debug_Putchar('=');
328                 }
329                 switch(*ArgTypes)
330                 {
331                 case 'p':       LogF("%p", va_arg(args, void*));        break;
332                 case 's':       LogF("'%s'", va_arg(args, char*));      break;
333                 case 'i':       LogF("%i", va_arg(args, int));  break;
334                 case 'u':       LogF("%u", va_arg(args, Uint)); break;
335                 case 'x':       LogF("0x%x", va_arg(args, Uint));       break;
336                 case 'b':       LogF("0b%b", va_arg(args, Uint));       break;
337                 case 'X':       LogF("0x%llx", va_arg(args, Uint64));   break;  // Extended (64-Bit)
338                 case 'B':       LogF("0b%llb", va_arg(args, Uint64));   break;  // Extended (64-Bit)
339                 }
340                 if(pos != -1) {
341                         Debug_Putchar(',');     Debug_Putchar(' ');
342                 }
343
344                 if(pos == -1)   break;
345                 ArgTypes = &ArgTypes[pos+1];
346         }
347
348         va_end(args);
349         Debug_Putchar(')');     Debug_Putchar('\r');    Debug_Putchar('\n');
350         
351         #if LOCK_DEBUG_OUTPUT
352         SHORTREL(&glDebug_Lock);
353         #endif
354 }
355
356 void Debug_Log(const char *FuncName, const char *Fmt, ...)
357 {
358         va_list args;
359          int    i = gDebug_Level;
360         tTID    tid = Threads_GetTID();
361
362         #if LOCK_DEBUG_OUTPUT
363         SHORTLOCK(&glDebug_Lock);
364         #endif
365
366         va_start(args, Fmt);
367
368         while(i--)      Debug_Putchar(' ');
369
370         Debug_Puts(1, FuncName);
371         Debug_FmtS("[%i]", tid);
372         Debug_Puts(1, ": ");
373         Debug_Fmt(Fmt, args);
374
375         va_end(args);
376         Debug_Putchar('\r');
377         Debug_Putchar('\n');
378         
379         #if LOCK_DEBUG_OUTPUT
380         SHORTREL(&glDebug_Lock);
381         #endif
382 }
383
384 void Debug_Leave(const char *FuncName, char RetType, ...)
385 {
386         va_list args;
387          int    i;
388         tTID    tid = Threads_GetTID();
389
390         #if LOCK_DEBUG_OUTPUT
391         SHORTLOCK(&glDebug_Lock);
392         #endif
393         
394         i = --gDebug_Level;
395
396         va_start(args, RetType);
397
398         if( i == -1 ) {
399                 gDebug_Level = 0;
400                 i = 0;
401         }
402         // Indenting
403         while(i--)      Debug_Putchar(' ');
404
405         Debug_Puts(1, FuncName);
406         Debug_FmtS("[%i]", tid);
407         Debug_Puts(1, ": RETURN");
408
409         // No Return
410         if(RetType == '-') {
411                 Debug_Putchar('\r');
412                 Debug_Putchar('\n');
413                 #if LOCK_DEBUG_OUTPUT
414                 SHORTREL(&glDebug_Lock);
415                 #endif
416                 return;
417         }
418
419         Debug_Putchar(' ');
420         switch(RetType)
421         {
422         case 'n':       Debug_Puts(1, "NULL");  break;
423         case 'p':       Debug_Fmt("%p", args);  break;
424         case 's':       Debug_Fmt("'%s'", args);        break;
425         case 'i':       Debug_Fmt("%i", args);  break;
426         case 'u':       Debug_Fmt("%u", args);  break;
427         case 'x':       Debug_Fmt("0x%x", args);        break;
428         // Extended (64-Bit)
429         case 'X':       Debug_Fmt("0x%llx", args);      break;
430         }
431         Debug_Putchar('\r');
432         Debug_Putchar('\n');
433
434         va_end(args);
435         
436         #if LOCK_DEBUG_OUTPUT
437         SHORTREL(&glDebug_Lock);
438         #endif
439 }
440
441 void Debug_HexDump(const char *Header, const void *Data, Uint Length)
442 {
443         const Uint8     *cdat = Data;
444         Uint    pos = 0;
445         Debug_Puts(1, Header);
446         LogF(" (Hexdump of %p)\r\n", Data);
447
448         #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
449
450         while(Length >= 16)
451         {
452                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
453                         " %02x %02x %02x %02x %02x %02x %02x %02x"
454                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
455                         pos,
456                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
457                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
458                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
459                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
460                         );
461                 Length -= 16;
462                 cdat += 16;
463                 pos += 16;
464         }
465
466         {
467                  int    i ;
468                 LogF("Log: %04x: ", pos);
469                 for(i = 0; i < Length; i ++)
470                 {
471                         LogF("%02x ", cdat[i]);
472                 }
473                 for( ; i < 16; i ++)    LogF("   ");
474                 LogF(" ");
475                 for(i = 0; i < Length; i ++)
476                 {
477                         if( i == 8 )    LogF(" ");
478                         LogF("%c", CH(i));
479                 }
480         }
481         
482         Debug_Putchar('\r');
483         Debug_Putchar('\n');
484 }
485
486 // --- EXPORTS ---
487 EXPORT(Debug);
488 EXPORT(Log);
489 EXPORT(Warning);
490 EXPORT(Debug_Enter);
491 EXPORT(Debug_Log);
492 EXPORT(Debug_Leave);

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