2 #include <e32svr.h> // RDebug
\r
7 void ExceptionHandler(TExcType exc) {}
\r
11 static const wchar_t * const exception_names[] = {
\r
13 L"IntegerDivideByZero",
\r
26 L"FloatDivideByZero",
\r
27 L"FloatInexactResult",
\r
28 L"FloatInvalidOperation",
\r
43 static void getASpace(TUint *code_start, TUint *code_end, TUint *stack_start, TUint *stack_end)
\r
47 TFullName chunkname;
\r
48 TFindChunk findChunk(_L("*"));
\r
50 asm volatile ("str pc, %0" : "=m" (pc) );
\r
51 asm volatile ("str sp, %0" : "=m" (sp) );
\r
53 while( findChunk.Next(chunkname) != KErrNotFound ) {
\r
54 chunk.Open(findChunk);
\r
55 if((TUint)chunk.Base()+chunk.Bottom() < pc && pc < (TUint)chunk.Base()+chunk.Top()) {
\r
56 if(code_start) *code_start = (TUint)chunk.Base()+chunk.Bottom();
\r
57 if(code_end) *code_end = (TUint)chunk.Base()+chunk.Top();
\r
59 if((TUint)chunk.Base()+chunk.Bottom() < sp && sp < (TUint)chunk.Base()+chunk.Top()) {
\r
60 if(stack_start) *stack_start = (TUint)chunk.Base()+chunk.Bottom();
\r
61 if(stack_end) *stack_end = (TUint)chunk.Base()+chunk.Top();
\r
68 #if defined(__DEBUG_PRINT)
\r
69 extern "C" char *debugString();
\r
72 // our very own exception handler
\r
73 void ExceptionHandler(TExcType exc)
\r
76 TUint stack_end = 0; // ending address of our stack chunk
\r
77 TUint code_start = 0, code_end = 0; // starting and ending addresses of our code chunk
\r
78 TUint guessed_address = 0;
\r
80 DEBUGPRINT(_L("ExceptionHandler()")); // this seems to never be called
\r
82 asm volatile ("str lr, %0" : "=m" (lr) );
\r
83 asm volatile ("str sp, %0" : "=m" (sp) );
\r
85 // first get some info about the chunks we live in
\r
86 getASpace(&code_start, &code_end, 0, &stack_end);
\r
88 // now we begin some black magic tricks
\r
89 // we go up our stack until we pass our caller address
\r
90 for(; sp < stack_end; sp += 4)
\r
91 if(*(TUint *)sp == lr) break;
\r
93 // there might be mirored caller address
\r
94 for(i = sp + 4; i < sp + 0x300 && i < stack_end; i += 4)
\r
95 if(*(TUint *)i == lr) { sp = i; break; }
\r
97 // aah, it is always 0x9c bytes away from the caller address in my firmware,
\r
98 // don't know how to detect it in any other way
\r
100 guessed_address = *(TUint *)sp;
\r
103 TUint exec_show = exc;
\r
104 if(exec_show > 27) exec_show = 27;
\r
105 TPtrC ptrExc((TUint16 *) exception_names[exec_show]);
\r
107 RDebug::Print(_L("!!!Exception %i (%S) @ 0x%08x (guessed; relative=0x%08x)"), exc, &ptrExc, guessed_address, guessed_address - code_start);
\r
108 #ifdef __DEBUG_PRINT_FILE
\r
109 DEBUGPRINT( _L("!!!Exception %i (%S) @ 0x%08x (guessed; relative=0x%08x)"), exc, &ptrExc, guessed_address, guessed_address - code_start);
\r
114 buff1.Copy(_L(" guessed stack: "));
\r
116 for(sp += 4, i = 0; i < 5 && sp < stack_end; sp += 4) {
\r
117 if((*(TUint *)sp >> 28) == 5) {
\r
118 if(i++) buff1.Append(_L(", "));
\r
119 buff2.Format(_L("0x%08x"), *(TUint *)sp);
\r
120 buff1.Append(buff2);
\r
122 else if(code_start < *(TUint *)sp && *(TUint *)sp < code_end) {
\r
123 if(i++) buff1.Append(_L(", "));
\r
124 buff2.Format(_L("0x%08x"), *(TUint *)sp);
\r
125 buff1.Append(buff2);
\r
126 buff1.Append(_L(" ("));
\r
127 buff2.Format(_L("0x%08x"), *(TUint *)sp - code_start);
\r
128 buff1.Append(buff2);
\r
129 buff1.Append(_L(")"));
\r
132 RDebug::Print(_L("%S"), &buff1);
\r
133 #ifdef __DEBUG_PRINT_FILE
\r
134 DEBUGPRINT(_L("%S"), &buff1);
\r
138 #if defined(__DEBUG_PRINT)
\r
139 char *ps, *cstr = debugString();
\r
140 for(ps = cstr; *ps; ps++) {
\r
149 // RDebug::Print(_L("Stack dump:"));
\r
150 // asm volatile ("str sp, %0" : "=m" (sp) );
\r
151 // for(TUint i = sp+0x400; i >= sp-16; i-=4)
\r
152 // RDebug::Print(_L("%08x: %08x"), i, *(int *)i);
\r
154 // more descriptive replacement of "KERN-EXEC 3" panic
\r
155 buff1.Format(_L("K-EX3: %S"), &ptrExc);
\r
156 User::Panic(buff1, exc);
\r
159 #endif // ifdef __WINS__
\r
162 #if defined(__DEBUG_PRINT) || defined(__WINS__)
\r
165 // c string dumper for RDebug::Print()
\r
166 static TBuf<1024> sTextBuffer;
\r
167 TDesC* DO_CONV(const char* s)
\r
169 TPtrC8 text8((TUint8*) (s));
\r
170 sTextBuffer.Copy(text8);
\r
171 return &sTextBuffer;
\r
175 #ifdef __DEBUG_PRINT_C
\r
176 #include <stdarg.h> // va_*
\r
177 #include <stdio.h> // vsprintf
\r
179 // debug print from c code
\r
180 extern "C" void dprintf(char *format, ...)
\r
185 va_start(args,format);
\r
186 vsprintf(buffer,format,args);
\r
189 DEBUGPRINT(_L("%S"), DO_CONV(buffer));
\r
193 #ifdef __DEBUG_PRINT_FILE
\r
194 #include <f32file.h>
\r
196 //static RFile logFile;
\r
197 // static TBool logInited = 0;
\r
200 static void debugPrintFileInit()
\r
203 logMutex.CreateLocal();
\r
207 logFile.Replace(fserv, _L("C:\\logs\\pico.log"), EFileWrite|EFileShareAny);
\r
212 // debug print to file
\r
213 void debugPrintFile(TRefByValue<const TDesC> aFmt, ...)
\r
215 if (logMutex.Handle() <= 0) debugPrintFileInit();
\r
221 TTime now; now.UniversalTime();
\r
223 TBuf8<512> tmpBuff8;
\r
226 RThread thisThread;
\r
228 res = logFile.Open(fserv, _L("C:\\logs\\pico.log"), EFileWrite|EFileShareAny);
\r
229 if(res) goto fail1;
\r
231 logFile.Size(size); logFile.Seek(ESeekStart, size);
\r
233 now.FormatL(tmpBuff, _L("%H:%T:%S.%C: "));
\r
234 tmpBuff8.Copy(tmpBuff);
\r
235 logFile.Write(tmpBuff8);
\r
237 tmpBuff8.Format(TPtr8((TUint8 *)"%03i: ", 6, 6), (TInt32) thisThread.Id());
\r
238 logFile.Write(tmpBuff8);
\r
241 VA_START(args, aFmt);
\r
242 tmpBuff.FormatList(aFmt, args);
\r
244 tmpBuff8.Copy(tmpBuff);
\r
245 logFile.Write(tmpBuff8);
\r
247 logFile.Write(TPtrC8((TUint8 const *) "\n"));
\r
251 thisThread.Close();
\r