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

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