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

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