Fixes to Libc, Doxygen Comments and VTerm layout
[tpg/acess2.git] / Kernel / debug.c
1 /*
2  * AcessOS Microkernel Version
3  * debug.c
4  */
5 #include <common.h>
6 #include <stdarg.h>
7
8 // === IMPORTS ===
9 extern void Threads_Dump();
10
11 // === GLOBALS ===
12  int    gDebug_Level = 0;
13  int    giDebug_KTerm = -1;
14
15 // === CODE ===
16 static void E9(char ch)
17 {
18         if(giDebug_KTerm != -1)
19                 VFS_Write(giDebug_KTerm, 1, &ch);
20         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
21 }
22
23 static void E9_Str(char *Str)
24 {
25         while(*Str)     E9(*Str++);
26 }
27
28 void E9_Fmt(const char *format, va_list *args)
29 {
30         char    c, pad = ' ';
31          int    minSize = 0;
32         char    tmpBuf[34];     // For Integers
33         char    *p = NULL;
34          int    isLongLong = 0;
35         Uint64  arg;
36   
37         while((c = *format++) != 0)
38         {
39                 // Non control character
40                 if( c != '%' ) {
41                         E9(c);
42                         continue;
43                 }
44                 
45                 c = *format++;
46                 
47                 // Literal %
48                 if(c == '%') {
49                         E9('%');
50                         continue;
51                 }
52                 
53                 // Pointer
54                 if(c == 'p') {
55                         Uint    ptr = va_arg(*args, Uint);
56                         E9('*');        E9('0');        E9('x');
57                         p = tmpBuf;
58                         itoa(p, ptr, 16, BITS/4, '0');
59                         goto printString;
60                 }
61                 
62                 // Get Argument
63                 arg = va_arg(*args, Uint);
64                 
65                 // Padding
66                 if(c == '0') {
67                         pad = '0';
68                         c = *format++;
69                 } else
70                         pad = ' ';
71                 
72                 // Minimum length
73                 minSize = 1;
74                 if('1' <= c && c <= '9')
75                 {
76                         minSize = 0;
77                         while('0' <= c && c <= '9')
78                         {
79                                 minSize *= 10;
80                                 minSize += c - '0';
81                                 c = *format++;
82                         }
83                 }
84                 
85                 // Long (default)
86                 isLongLong = 0;
87                 if(c == 'l') {
88                         c = *format++;
89                         if(c == 'l') {
90                                 #if BITS == 32
91                                 arg |= va_arg(*args, Uint);
92                                 #endif
93                                 c = *format++;
94                                 isLongLong = 1;
95                         }
96                 }
97                 
98                 p = tmpBuf;
99                 switch (c) {
100                 case 'd':
101                 case 'i':
102                         if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
103                                 E9('-');
104                                 arg = -arg;
105                         }
106                         itoa(p, arg, 10, minSize, pad);
107                         goto printString;
108                 case 'u':
109                         itoa(p, arg, 10, minSize, pad);
110                         goto printString;
111                 case 'x':
112                         itoa(p, arg, 16, minSize, pad);
113                         goto printString;
114                 case 'o':
115                         itoa(p, arg, 8, minSize, pad);
116                         goto printString;
117                 case 'b':
118                         itoa(p, arg, 2, minSize, pad);
119                         goto printString;
120
121                 case 'B':       //Boolean
122                         if(arg) E9_Str("True");
123                         else    E9_Str("False");
124                         break;
125                 
126                 case 's':
127                         p = (char*)(Uint)arg;
128                 printString:
129                         if(!p)          p = "(null)";
130                         while(*p)       E9(*p++);
131                         break;
132                         
133                 default:        E9(arg);        break;
134                 }
135     }
136 }
137
138 /**
139  * \fn void LogV(char *Fmt, va_list Args)
140  */
141 void LogV(char *Fmt, va_list Args)
142 {
143         E9_Str("Log: ");
144         E9_Fmt(Fmt, &Args);
145         E9('\n');
146 }
147 /**
148  * \fn void LogF(char *Msg, ...)
149  */
150 void LogF(char *Fmt, ...)
151 {
152         va_list args;
153         
154         va_start(args, Fmt);
155         
156         E9_Fmt(Fmt, &args);
157         
158         va_end(args);
159 }
160 /**
161  * \fn void Log(char *Msg, ...)
162  */
163 void Log(char *Fmt, ...)
164 {
165         va_list args;
166         
167         E9_Str("Log: ");
168         va_start(args, Fmt);
169         E9_Fmt(Fmt, &args);
170         va_end(args);
171         E9('\n');
172 }
173 void Warning(char *Fmt, ...)
174 {
175         va_list args;
176         E9_Str("Warning: ");
177         va_start(args, Fmt);
178         E9_Fmt(Fmt, &args);
179         va_end(args);
180         E9('\n');
181 }
182 void Panic(char *Fmt, ...)
183 {
184         va_list args;
185         E9_Str("Panic: ");
186         va_start(args, Fmt);
187         E9_Fmt(Fmt, &args);
188         va_end(args);
189         E9('\n');
190         
191         Threads_Dump();
192         
193         __asm__ __volatile__ ("xchg %bx, %bx");
194         __asm__ __volatile__ ("cli;\n\thlt");
195         for(;;) __asm__ __volatile__ ("hlt");
196 }
197
198 void Debug_SetKTerminal(char *File)
199 {
200         if(giDebug_KTerm != -1)
201                 VFS_Close(giDebug_KTerm);
202         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
203 }
204
205 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
206 {
207         va_list args;
208          int    i = gDebug_Level ++;
209          int    pos;
210         
211         va_start(args, ArgTypes);
212         
213         while(i--)      E9(' ');
214         
215         E9_Str(FuncName);       E9_Str(": (");
216         
217         while(*ArgTypes)
218         {
219                 pos = strpos(ArgTypes, ' ');
220                 if(pos != -1)   ArgTypes[pos] = '\0';
221                 if(pos == -1 || pos > 1) {
222                         E9_Str(ArgTypes+1);
223                         E9('=');
224                 }
225                 if(pos != -1)   ArgTypes[pos] = ' ';
226                 switch(*ArgTypes)
227                 {
228                 case 'p':       E9_Fmt("%p", &args);    break;
229                 case 's':       E9_Fmt("'%s'", &args);  break;
230                 case 'i':       E9_Fmt("%i", &args);    break;
231                 case 'u':       E9_Fmt("%u", &args);    break;
232                 case 'x':       E9_Fmt("0x%x", &args);  break;
233                 case 'b':       E9_Fmt("0b%b", &args);  break;
234                 // Extended (64-Bit)
235                 case 'X':       E9_Fmt("0x%llx", &args);        break;
236                 case 'B':       E9_Fmt("0b%llb", &args);        break;
237                 }
238                 if(pos != -1) {
239                         E9(',');        E9(' ');
240                 }
241                 
242                 if(pos == -1)   break;
243                 ArgTypes = &ArgTypes[pos+1];
244         }
245         
246         va_end(args);
247         E9(')');        E9('\n');
248 }
249
250 void Debug_Log(char *FuncName, char *Fmt, ...)
251 {
252         va_list args;
253          int    i = gDebug_Level;
254         
255         va_start(args, Fmt);
256         
257         while(i--)      E9(' ');
258         
259         E9_Str(FuncName);       E9_Str(": ");
260         E9_Fmt(Fmt, &args);
261         
262         va_end(args);
263         E9('\n');
264 }
265
266 void Debug_Leave(char *FuncName, char RetType, ...)
267 {
268         va_list args;
269          int    i = --gDebug_Level;
270         
271         va_start(args, RetType);
272         
273         // Indenting
274         while(i--)      E9(' ');
275         
276         E9_Str(FuncName);       E9_Str(": RETURN");
277         
278         // No Return
279         if(RetType == '-') {
280                 E9('\n');
281                 return;
282         }
283         
284         E9(' ');
285         switch(RetType)
286         {
287         case 'n':       E9_Str("NULL"); break;
288         case 'p':       E9_Fmt("%p", &args);    break;
289         case 's':       E9_Fmt("'%s'", &args);  break;
290         case 'i':       E9_Fmt("%i", &args);    break;
291         case 'u':       E9_Fmt("%u", &args);    break;
292         case 'x':       E9_Fmt("0x%x", &args);  break;
293         // Extended (64-Bit)
294         case 'X':       E9_Fmt("0x%llx", &args);        break;
295         }
296         E9('\n');
297         
298         va_end(args);
299 }
300
301 void Debug_HexDump(char *Header, void *Data, Uint Length)
302 {
303         Uint8   *cdat = Data;
304         Uint    pos = 0;
305         E9_Str(Header);
306         LogF(" (Hexdump of %p)\n", Data);
307         
308         while(Length >= 16)
309         {
310                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
311                         pos,
312                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
313                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
314                         );
315                 Length -= 16;
316                 cdat += 16;
317                 pos += 16;
318         }
319         
320         LogF("Log: %04x: ", pos);
321         while(Length)
322         {
323                 Uint    byte = *cdat;
324                 LogF("%02x ", byte);
325                 Length--;
326                 cdat ++;
327         }
328         E9('\n');
329 }
330
331 // --- EXPORTS ---
332 EXPORT(Warning);
333 EXPORT(Debug_Enter);
334 EXPORT(Debug_Log);
335 EXPORT(Debug_Leave);

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