os/kernelhwsrv/kerneltest/e32test/nkernsa/diag.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/nkernsa/diag.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,327 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\nkernsa\diag.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <nktest/diag.h>
    1.22 +
    1.23 +#ifdef __cplusplus
    1.24 +extern "C" {
    1.25 +#endif
    1.26 +
    1.27 +const DiagIO* TheIoFunctions;
    1.28 +
    1.29 +int InputAvailable(void)
    1.30 +	{
    1.31 +	return (*TheIoFunctions->iAvail)();
    1.32 +	}
    1.33 +
    1.34 +char GetC(void)
    1.35 +	{
    1.36 +	int c;
    1.37 +	do	{
    1.38 +		c = (*TheIoFunctions->iPoll)();
    1.39 +		} while (c<0);
    1.40 +	return (char)c;
    1.41 +	}
    1.42 +
    1.43 +void PutC(char c)
    1.44 +	{
    1.45 +	(*TheIoFunctions->iOut)(c);
    1.46 +	}
    1.47 +
    1.48 +void PutS(const char* s)
    1.49 +	{
    1.50 +	while (*s)
    1.51 +		PutC(*s++);
    1.52 +	}
    1.53 +
    1.54 +void PrtHex(unsigned long x, int n)
    1.55 +	{
    1.56 +	if (n>8)
    1.57 +		n=8;
    1.58 +	if (n<1)
    1.59 +		n=1;
    1.60 +	x <<= (32-4*n);
    1.61 +	while (n--)
    1.62 +		{
    1.63 +		char c = (char)(x>>28);
    1.64 +		x<<=4;
    1.65 +		c += '0';
    1.66 +		if (c > '9')
    1.67 +			c += ('a'-'9'-1);
    1.68 +		PutC(c);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +void PrtHex2(unsigned long x)
    1.73 +	{
    1.74 +	PrtHex(x, 2);
    1.75 +	}
    1.76 +
    1.77 +void PrtHex4(unsigned long x)
    1.78 +	{
    1.79 +	PrtHex(x, 4);
    1.80 +	}
    1.81 +
    1.82 +void PrtHex8(unsigned long x)
    1.83 +	{
    1.84 +	PrtHex(x, 8);
    1.85 +	}
    1.86 +
    1.87 +void NewLine(void)
    1.88 +	{
    1.89 +	PutC('\r');
    1.90 +	PutC('\n');
    1.91 +	}
    1.92 +
    1.93 +void PutSpc(void)
    1.94 +	{
    1.95 +	PutC(' ');
    1.96 +	}
    1.97 +
    1.98 +int GetAndEchoLine(char* buf, int max, const char* prompt)
    1.99 +	{
   1.100 +	int n = 0;
   1.101 +	PutS(prompt);
   1.102 +	for (;;)
   1.103 +		{
   1.104 +		int c;
   1.105 +		do	{
   1.106 +			c = GetC();
   1.107 +			} while (c==0x0a);	// ignore LF
   1.108 +		if (c==0x0d || c==0x18 || c==0x03)
   1.109 +			{
   1.110 +			if (c!=0x0d)
   1.111 +				n = 0;
   1.112 +			NewLine();
   1.113 +			break;
   1.114 +			}
   1.115 +		else if (c==0x08)
   1.116 +			{
   1.117 +			if (n>0)
   1.118 +				--n;
   1.119 +			}
   1.120 +		else if (n<max-1)
   1.121 +			{
   1.122 +			buf[n++] = (char)c;
   1.123 +			}
   1.124 +		PutC((char)c);
   1.125 +		}
   1.126 +	buf[n] = 0;
   1.127 +	return n;
   1.128 +	}
   1.129 +
   1.130 +int SkipSpc(const char* s)
   1.131 +	{
   1.132 +	const char* s0 = s;
   1.133 +	while (*s==32 || *s==9)
   1.134 +		++s;
   1.135 +	return s - s0;
   1.136 +	}
   1.137 +
   1.138 +int CharToHex(char c)
   1.139 +	{
   1.140 +	if (c>='0' && c<='9')
   1.141 +		return c - '0';
   1.142 +	else if (c>='A' && c<='F')
   1.143 +		return c - 'A' + 10;
   1.144 +	else if (c>='a' && c<='f')
   1.145 +		return c - 'a' + 10;
   1.146 +	return -1;
   1.147 +	}
   1.148 +
   1.149 +int ReadHex(const char* s, unsigned long* d)
   1.150 +	{
   1.151 +	const char* s0 = s;
   1.152 +	unsigned long x = 0;
   1.153 +	for (;;)
   1.154 +		{
   1.155 +		int digit = CharToHex(*s);
   1.156 +		if (digit < 0)
   1.157 +			break;
   1.158 +		++s;
   1.159 +		x = (x<<4) | ((unsigned long)digit);
   1.160 +		}
   1.161 +	*d = x;
   1.162 +	return s - s0;
   1.163 +	}
   1.164 +
   1.165 +int ReadRangeHex(const char* s, unsigned long* base, unsigned long* length)
   1.166 +	{
   1.167 +	unsigned long b = 0;
   1.168 +	unsigned long l = 0;
   1.169 +	const char* s0 = s;
   1.170 +	char c;
   1.171 +	int i;
   1.172 +	s += SkipSpc(s);
   1.173 +	i = ReadHex(s, &b);
   1.174 +	if (i == 0)
   1.175 +		return 0;
   1.176 +	s += i;
   1.177 +	s += SkipSpc(s);
   1.178 +	c = *s;
   1.179 +	if (c!='+' && CharToHex(c)<0)
   1.180 +		return 0;
   1.181 +	if (c=='+')
   1.182 +		{
   1.183 +		++s;
   1.184 +		s += SkipSpc(s);
   1.185 +		}
   1.186 +	i = ReadHex(s, &l);
   1.187 +	if (i == 0)
   1.188 +		return 0;
   1.189 +	s += i;
   1.190 +	if (c!='+')
   1.191 +		{
   1.192 +		if (l >= b)		// inclusive end address given
   1.193 +			l -= (b-1);	// calculate length
   1.194 +		else
   1.195 +			l = 1;		// end < base so assume length of 1
   1.196 +		}
   1.197 +	*base = b;
   1.198 +	*length = l;
   1.199 +	return s - s0;
   1.200 +	}
   1.201 +
   1.202 +void DumpMemory1(const void* mem, const void* disp)
   1.203 +	{
   1.204 +	int i;
   1.205 +	const unsigned char* s = (const unsigned char*)mem;
   1.206 +	PrtHex8((unsigned long)disp);
   1.207 +	PutC(':');
   1.208 +	for (i=0; i<16; ++i)
   1.209 +		{
   1.210 +		PutSpc();
   1.211 +		PrtHex2(s[i]);
   1.212 +		}
   1.213 +	PutSpc();
   1.214 +	for (i=0; i<16; ++i)
   1.215 +		{
   1.216 +		char c = s[i];
   1.217 +		if (c<32 || c>=127)
   1.218 +			c = '.';
   1.219 +		PutC(c);
   1.220 +		}
   1.221 +	NewLine();
   1.222 +	}
   1.223 +
   1.224 +void DumpMemoryRange1(const void* base, unsigned long length, const void* disp)
   1.225 +	{
   1.226 +	const char* s = (const char*)base;
   1.227 +	const char* sd = (const char*)disp;
   1.228 +	do	{
   1.229 +		DumpMemory1(s, sd);
   1.230 +		s += 16;
   1.231 +		sd += 16;
   1.232 +		if (length < 16)
   1.233 +			length = 0;
   1.234 +		else
   1.235 +			length -= 16;
   1.236 +		if ((*TheIoFunctions->iPoll)() == 3)
   1.237 +			break;
   1.238 +		} while (length > 0);
   1.239 +	}
   1.240 +
   1.241 +unsigned long _readdata(const void** pp, unsigned long bytes, int align)
   1.242 +	{
   1.243 +	unsigned long x = 0;
   1.244 +	unsigned long addr = (unsigned long)(*pp);
   1.245 +	const unsigned char* s;
   1.246 +	if (align)
   1.247 +		{
   1.248 +		addr += (bytes - 1);
   1.249 +		addr &= ~(bytes - 1);
   1.250 +		}
   1.251 +	s = (const unsigned char*)(addr + bytes);
   1.252 +	*pp = s;
   1.253 +	while (bytes--)
   1.254 +		x = (x<<8) | (*--s);
   1.255 +	return x;
   1.256 +	}
   1.257 +
   1.258 +void DumpStruct(const char* s, const void* p)
   1.259 +	{
   1.260 +	for (;;)
   1.261 +		{
   1.262 +		char c = *s++;
   1.263 +		if (c == 0)
   1.264 +			break;
   1.265 +		if (c == '\n')
   1.266 +			PutC('\r');
   1.267 +		if (c != '%')
   1.268 +			{
   1.269 +			PutC(c);
   1.270 +			continue;
   1.271 +			}
   1.272 +		c = *s++;
   1.273 +		switch (c)
   1.274 +			{
   1.275 +			case '%':	PutC(c); break;
   1.276 +			case 'b':	PrtHex2(_readdata(&p, 1, 1)); break;
   1.277 +			case 'h':	PrtHex4(_readdata(&p, 2, 1)); break;
   1.278 +			case 'w':	PrtHex8(_readdata(&p, 4, 1)); break;
   1.279 +			}
   1.280 +		}
   1.281 +	}
   1.282 +
   1.283 +void RunCrashDebugger()
   1.284 +	{
   1.285 +	char buf[128];
   1.286 +	for (;;)
   1.287 +		{
   1.288 +		int len = GetAndEchoLine(buf, 128, ".");
   1.289 +		(void)len;
   1.290 +		int i = 0;
   1.291 +		int err = 0;
   1.292 +		const char* s = buf;
   1.293 +		char cmd = *s++;
   1.294 +		switch (cmd)
   1.295 +			{
   1.296 +			case 'x':
   1.297 +			case 'X':
   1.298 +				return;
   1.299 +			case '\0':
   1.300 +				break;
   1.301 +			case 'm':
   1.302 +				{
   1.303 +				unsigned long mbase = 0;
   1.304 +				unsigned long mlen = 0;
   1.305 +				i = ReadRangeHex(s, &mbase, &mlen);
   1.306 +				if (i)
   1.307 +					DumpMemoryRange1((void*)mbase, mlen, (void*)mbase);
   1.308 +				else
   1.309 +					err = 2;
   1.310 +				break;
   1.311 +				}
   1.312 +			default:
   1.313 +				err = 1;
   1.314 +				break;
   1.315 +			}
   1.316 +		switch (err)
   1.317 +			{
   1.318 +			case 1: PutS("\r\nUnknown command\r\n"); break;
   1.319 +			case 2: PutS("\r\nSyntax error\r\n"); break;
   1.320 +			default: break;
   1.321 +			}
   1.322 +		}
   1.323 +	}
   1.324 +
   1.325 +
   1.326 +
   1.327 +#ifdef __cplusplus
   1.328 +}
   1.329 +#endif
   1.330 +