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

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