sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\nkernsa\diag.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" { sl@0: #endif sl@0: sl@0: const DiagIO* TheIoFunctions; sl@0: sl@0: int InputAvailable(void) sl@0: { sl@0: return (*TheIoFunctions->iAvail)(); sl@0: } sl@0: sl@0: char GetC(void) sl@0: { sl@0: int c; sl@0: do { sl@0: c = (*TheIoFunctions->iPoll)(); sl@0: } while (c<0); sl@0: return (char)c; sl@0: } sl@0: sl@0: void PutC(char c) sl@0: { sl@0: (*TheIoFunctions->iOut)(c); sl@0: } sl@0: sl@0: void PutS(const char* s) sl@0: { sl@0: while (*s) sl@0: PutC(*s++); sl@0: } sl@0: sl@0: void PrtHex(unsigned long x, int n) sl@0: { sl@0: if (n>8) sl@0: n=8; sl@0: if (n<1) sl@0: n=1; sl@0: x <<= (32-4*n); sl@0: while (n--) sl@0: { sl@0: char c = (char)(x>>28); sl@0: x<<=4; sl@0: c += '0'; sl@0: if (c > '9') sl@0: c += ('a'-'9'-1); sl@0: PutC(c); sl@0: } sl@0: } sl@0: sl@0: void PrtHex2(unsigned long x) sl@0: { sl@0: PrtHex(x, 2); sl@0: } sl@0: sl@0: void PrtHex4(unsigned long x) sl@0: { sl@0: PrtHex(x, 4); sl@0: } sl@0: sl@0: void PrtHex8(unsigned long x) sl@0: { sl@0: PrtHex(x, 8); sl@0: } sl@0: sl@0: void NewLine(void) sl@0: { sl@0: PutC('\r'); sl@0: PutC('\n'); sl@0: } sl@0: sl@0: void PutSpc(void) sl@0: { sl@0: PutC(' '); sl@0: } sl@0: sl@0: int GetAndEchoLine(char* buf, int max, const char* prompt) sl@0: { sl@0: int n = 0; sl@0: PutS(prompt); sl@0: for (;;) sl@0: { sl@0: int c; sl@0: do { sl@0: c = GetC(); sl@0: } while (c==0x0a); // ignore LF sl@0: if (c==0x0d || c==0x18 || c==0x03) sl@0: { sl@0: if (c!=0x0d) sl@0: n = 0; sl@0: NewLine(); sl@0: break; sl@0: } sl@0: else if (c==0x08) sl@0: { sl@0: if (n>0) sl@0: --n; sl@0: } sl@0: else if (n='0' && c<='9') sl@0: return c - '0'; sl@0: else if (c>='A' && c<='F') sl@0: return c - 'A' + 10; sl@0: else if (c>='a' && c<='f') sl@0: return c - 'a' + 10; sl@0: return -1; sl@0: } sl@0: sl@0: int ReadHex(const char* s, unsigned long* d) sl@0: { sl@0: const char* s0 = s; sl@0: unsigned long x = 0; sl@0: for (;;) sl@0: { sl@0: int digit = CharToHex(*s); sl@0: if (digit < 0) sl@0: break; sl@0: ++s; sl@0: x = (x<<4) | ((unsigned long)digit); sl@0: } sl@0: *d = x; sl@0: return s - s0; sl@0: } sl@0: sl@0: int ReadRangeHex(const char* s, unsigned long* base, unsigned long* length) sl@0: { sl@0: unsigned long b = 0; sl@0: unsigned long l = 0; sl@0: const char* s0 = s; sl@0: char c; sl@0: int i; sl@0: s += SkipSpc(s); sl@0: i = ReadHex(s, &b); sl@0: if (i == 0) sl@0: return 0; sl@0: s += i; sl@0: s += SkipSpc(s); sl@0: c = *s; sl@0: if (c!='+' && CharToHex(c)<0) sl@0: return 0; sl@0: if (c=='+') sl@0: { sl@0: ++s; sl@0: s += SkipSpc(s); sl@0: } sl@0: i = ReadHex(s, &l); sl@0: if (i == 0) sl@0: return 0; sl@0: s += i; sl@0: if (c!='+') sl@0: { sl@0: if (l >= b) // inclusive end address given sl@0: l -= (b-1); // calculate length sl@0: else sl@0: l = 1; // end < base so assume length of 1 sl@0: } sl@0: *base = b; sl@0: *length = l; sl@0: return s - s0; sl@0: } sl@0: sl@0: void DumpMemory1(const void* mem, const void* disp) sl@0: { sl@0: int i; sl@0: const unsigned char* s = (const unsigned char*)mem; sl@0: PrtHex8((unsigned long)disp); sl@0: PutC(':'); sl@0: for (i=0; i<16; ++i) sl@0: { sl@0: PutSpc(); sl@0: PrtHex2(s[i]); sl@0: } sl@0: PutSpc(); sl@0: for (i=0; i<16; ++i) sl@0: { sl@0: char c = s[i]; sl@0: if (c<32 || c>=127) sl@0: c = '.'; sl@0: PutC(c); sl@0: } sl@0: NewLine(); sl@0: } sl@0: sl@0: void DumpMemoryRange1(const void* base, unsigned long length, const void* disp) sl@0: { sl@0: const char* s = (const char*)base; sl@0: const char* sd = (const char*)disp; sl@0: do { sl@0: DumpMemory1(s, sd); sl@0: s += 16; sl@0: sd += 16; sl@0: if (length < 16) sl@0: length = 0; sl@0: else sl@0: length -= 16; sl@0: if ((*TheIoFunctions->iPoll)() == 3) sl@0: break; sl@0: } while (length > 0); sl@0: } sl@0: sl@0: unsigned long _readdata(const void** pp, unsigned long bytes, int align) sl@0: { sl@0: unsigned long x = 0; sl@0: unsigned long addr = (unsigned long)(*pp); sl@0: const unsigned char* s; sl@0: if (align) sl@0: { sl@0: addr += (bytes - 1); sl@0: addr &= ~(bytes - 1); sl@0: } sl@0: s = (const unsigned char*)(addr + bytes); sl@0: *pp = s; sl@0: while (bytes--) sl@0: x = (x<<8) | (*--s); sl@0: return x; sl@0: } sl@0: sl@0: void DumpStruct(const char* s, const void* p) sl@0: { sl@0: for (;;) sl@0: { sl@0: char c = *s++; sl@0: if (c == 0) sl@0: break; sl@0: if (c == '\n') sl@0: PutC('\r'); sl@0: if (c != '%') sl@0: { sl@0: PutC(c); sl@0: continue; sl@0: } sl@0: c = *s++; sl@0: switch (c) sl@0: { sl@0: case '%': PutC(c); break; sl@0: case 'b': PrtHex2(_readdata(&p, 1, 1)); break; sl@0: case 'h': PrtHex4(_readdata(&p, 2, 1)); break; sl@0: case 'w': PrtHex8(_readdata(&p, 4, 1)); break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: void RunCrashDebugger() sl@0: { sl@0: char buf[128]; sl@0: for (;;) sl@0: { sl@0: int len = GetAndEchoLine(buf, 128, "."); sl@0: (void)len; sl@0: int i = 0; sl@0: int err = 0; sl@0: const char* s = buf; sl@0: char cmd = *s++; sl@0: switch (cmd) sl@0: { sl@0: case 'x': sl@0: case 'X': sl@0: return; sl@0: case '\0': sl@0: break; sl@0: case 'm': sl@0: { sl@0: unsigned long mbase = 0; sl@0: unsigned long mlen = 0; sl@0: i = ReadRangeHex(s, &mbase, &mlen); sl@0: if (i) sl@0: DumpMemoryRange1((void*)mbase, mlen, (void*)mbase); sl@0: else sl@0: err = 2; sl@0: break; sl@0: } sl@0: default: sl@0: err = 1; sl@0: break; sl@0: } sl@0: switch (err) sl@0: { sl@0: case 1: PutS("\r\nUnknown command\r\n"); break; sl@0: case 2: PutS("\r\nSyntax error\r\n"); break; sl@0: default: break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif sl@0: