os/kernelhwsrv/kerneltest/e32test/nkernsa/diag.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\nkernsa\diag.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <nktest/diag.h>
sl@0
    19
sl@0
    20
#ifdef __cplusplus
sl@0
    21
extern "C" {
sl@0
    22
#endif
sl@0
    23
sl@0
    24
const DiagIO* TheIoFunctions;
sl@0
    25
sl@0
    26
int InputAvailable(void)
sl@0
    27
	{
sl@0
    28
	return (*TheIoFunctions->iAvail)();
sl@0
    29
	}
sl@0
    30
sl@0
    31
char GetC(void)
sl@0
    32
	{
sl@0
    33
	int c;
sl@0
    34
	do	{
sl@0
    35
		c = (*TheIoFunctions->iPoll)();
sl@0
    36
		} while (c<0);
sl@0
    37
	return (char)c;
sl@0
    38
	}
sl@0
    39
sl@0
    40
void PutC(char c)
sl@0
    41
	{
sl@0
    42
	(*TheIoFunctions->iOut)(c);
sl@0
    43
	}
sl@0
    44
sl@0
    45
void PutS(const char* s)
sl@0
    46
	{
sl@0
    47
	while (*s)
sl@0
    48
		PutC(*s++);
sl@0
    49
	}
sl@0
    50
sl@0
    51
void PrtHex(unsigned long x, int n)
sl@0
    52
	{
sl@0
    53
	if (n>8)
sl@0
    54
		n=8;
sl@0
    55
	if (n<1)
sl@0
    56
		n=1;
sl@0
    57
	x <<= (32-4*n);
sl@0
    58
	while (n--)
sl@0
    59
		{
sl@0
    60
		char c = (char)(x>>28);
sl@0
    61
		x<<=4;
sl@0
    62
		c += '0';
sl@0
    63
		if (c > '9')
sl@0
    64
			c += ('a'-'9'-1);
sl@0
    65
		PutC(c);
sl@0
    66
		}
sl@0
    67
	}
sl@0
    68
sl@0
    69
void PrtHex2(unsigned long x)
sl@0
    70
	{
sl@0
    71
	PrtHex(x, 2);
sl@0
    72
	}
sl@0
    73
sl@0
    74
void PrtHex4(unsigned long x)
sl@0
    75
	{
sl@0
    76
	PrtHex(x, 4);
sl@0
    77
	}
sl@0
    78
sl@0
    79
void PrtHex8(unsigned long x)
sl@0
    80
	{
sl@0
    81
	PrtHex(x, 8);
sl@0
    82
	}
sl@0
    83
sl@0
    84
void NewLine(void)
sl@0
    85
	{
sl@0
    86
	PutC('\r');
sl@0
    87
	PutC('\n');
sl@0
    88
	}
sl@0
    89
sl@0
    90
void PutSpc(void)
sl@0
    91
	{
sl@0
    92
	PutC(' ');
sl@0
    93
	}
sl@0
    94
sl@0
    95
int GetAndEchoLine(char* buf, int max, const char* prompt)
sl@0
    96
	{
sl@0
    97
	int n = 0;
sl@0
    98
	PutS(prompt);
sl@0
    99
	for (;;)
sl@0
   100
		{
sl@0
   101
		int c;
sl@0
   102
		do	{
sl@0
   103
			c = GetC();
sl@0
   104
			} while (c==0x0a);	// ignore LF
sl@0
   105
		if (c==0x0d || c==0x18 || c==0x03)
sl@0
   106
			{
sl@0
   107
			if (c!=0x0d)
sl@0
   108
				n = 0;
sl@0
   109
			NewLine();
sl@0
   110
			break;
sl@0
   111
			}
sl@0
   112
		else if (c==0x08)
sl@0
   113
			{
sl@0
   114
			if (n>0)
sl@0
   115
				--n;
sl@0
   116
			}
sl@0
   117
		else if (n<max-1)
sl@0
   118
			{
sl@0
   119
			buf[n++] = (char)c;
sl@0
   120
			}
sl@0
   121
		PutC((char)c);
sl@0
   122
		}
sl@0
   123
	buf[n] = 0;
sl@0
   124
	return n;
sl@0
   125
	}
sl@0
   126
sl@0
   127
int SkipSpc(const char* s)
sl@0
   128
	{
sl@0
   129
	const char* s0 = s;
sl@0
   130
	while (*s==32 || *s==9)
sl@0
   131
		++s;
sl@0
   132
	return s - s0;
sl@0
   133
	}
sl@0
   134
sl@0
   135
int CharToHex(char c)
sl@0
   136
	{
sl@0
   137
	if (c>='0' && c<='9')
sl@0
   138
		return c - '0';
sl@0
   139
	else if (c>='A' && c<='F')
sl@0
   140
		return c - 'A' + 10;
sl@0
   141
	else if (c>='a' && c<='f')
sl@0
   142
		return c - 'a' + 10;
sl@0
   143
	return -1;
sl@0
   144
	}
sl@0
   145
sl@0
   146
int ReadHex(const char* s, unsigned long* d)
sl@0
   147
	{
sl@0
   148
	const char* s0 = s;
sl@0
   149
	unsigned long x = 0;
sl@0
   150
	for (;;)
sl@0
   151
		{
sl@0
   152
		int digit = CharToHex(*s);
sl@0
   153
		if (digit < 0)
sl@0
   154
			break;
sl@0
   155
		++s;
sl@0
   156
		x = (x<<4) | ((unsigned long)digit);
sl@0
   157
		}
sl@0
   158
	*d = x;
sl@0
   159
	return s - s0;
sl@0
   160
	}
sl@0
   161
sl@0
   162
int ReadRangeHex(const char* s, unsigned long* base, unsigned long* length)
sl@0
   163
	{
sl@0
   164
	unsigned long b = 0;
sl@0
   165
	unsigned long l = 0;
sl@0
   166
	const char* s0 = s;
sl@0
   167
	char c;
sl@0
   168
	int i;
sl@0
   169
	s += SkipSpc(s);
sl@0
   170
	i = ReadHex(s, &b);
sl@0
   171
	if (i == 0)
sl@0
   172
		return 0;
sl@0
   173
	s += i;
sl@0
   174
	s += SkipSpc(s);
sl@0
   175
	c = *s;
sl@0
   176
	if (c!='+' && CharToHex(c)<0)
sl@0
   177
		return 0;
sl@0
   178
	if (c=='+')
sl@0
   179
		{
sl@0
   180
		++s;
sl@0
   181
		s += SkipSpc(s);
sl@0
   182
		}
sl@0
   183
	i = ReadHex(s, &l);
sl@0
   184
	if (i == 0)
sl@0
   185
		return 0;
sl@0
   186
	s += i;
sl@0
   187
	if (c!='+')
sl@0
   188
		{
sl@0
   189
		if (l >= b)		// inclusive end address given
sl@0
   190
			l -= (b-1);	// calculate length
sl@0
   191
		else
sl@0
   192
			l = 1;		// end < base so assume length of 1
sl@0
   193
		}
sl@0
   194
	*base = b;
sl@0
   195
	*length = l;
sl@0
   196
	return s - s0;
sl@0
   197
	}
sl@0
   198
sl@0
   199
void DumpMemory1(const void* mem, const void* disp)
sl@0
   200
	{
sl@0
   201
	int i;
sl@0
   202
	const unsigned char* s = (const unsigned char*)mem;
sl@0
   203
	PrtHex8((unsigned long)disp);
sl@0
   204
	PutC(':');
sl@0
   205
	for (i=0; i<16; ++i)
sl@0
   206
		{
sl@0
   207
		PutSpc();
sl@0
   208
		PrtHex2(s[i]);
sl@0
   209
		}
sl@0
   210
	PutSpc();
sl@0
   211
	for (i=0; i<16; ++i)
sl@0
   212
		{
sl@0
   213
		char c = s[i];
sl@0
   214
		if (c<32 || c>=127)
sl@0
   215
			c = '.';
sl@0
   216
		PutC(c);
sl@0
   217
		}
sl@0
   218
	NewLine();
sl@0
   219
	}
sl@0
   220
sl@0
   221
void DumpMemoryRange1(const void* base, unsigned long length, const void* disp)
sl@0
   222
	{
sl@0
   223
	const char* s = (const char*)base;
sl@0
   224
	const char* sd = (const char*)disp;
sl@0
   225
	do	{
sl@0
   226
		DumpMemory1(s, sd);
sl@0
   227
		s += 16;
sl@0
   228
		sd += 16;
sl@0
   229
		if (length < 16)
sl@0
   230
			length = 0;
sl@0
   231
		else
sl@0
   232
			length -= 16;
sl@0
   233
		if ((*TheIoFunctions->iPoll)() == 3)
sl@0
   234
			break;
sl@0
   235
		} while (length > 0);
sl@0
   236
	}
sl@0
   237
sl@0
   238
unsigned long _readdata(const void** pp, unsigned long bytes, int align)
sl@0
   239
	{
sl@0
   240
	unsigned long x = 0;
sl@0
   241
	unsigned long addr = (unsigned long)(*pp);
sl@0
   242
	const unsigned char* s;
sl@0
   243
	if (align)
sl@0
   244
		{
sl@0
   245
		addr += (bytes - 1);
sl@0
   246
		addr &= ~(bytes - 1);
sl@0
   247
		}
sl@0
   248
	s = (const unsigned char*)(addr + bytes);
sl@0
   249
	*pp = s;
sl@0
   250
	while (bytes--)
sl@0
   251
		x = (x<<8) | (*--s);
sl@0
   252
	return x;
sl@0
   253
	}
sl@0
   254
sl@0
   255
void DumpStruct(const char* s, const void* p)
sl@0
   256
	{
sl@0
   257
	for (;;)
sl@0
   258
		{
sl@0
   259
		char c = *s++;
sl@0
   260
		if (c == 0)
sl@0
   261
			break;
sl@0
   262
		if (c == '\n')
sl@0
   263
			PutC('\r');
sl@0
   264
		if (c != '%')
sl@0
   265
			{
sl@0
   266
			PutC(c);
sl@0
   267
			continue;
sl@0
   268
			}
sl@0
   269
		c = *s++;
sl@0
   270
		switch (c)
sl@0
   271
			{
sl@0
   272
			case '%':	PutC(c); break;
sl@0
   273
			case 'b':	PrtHex2(_readdata(&p, 1, 1)); break;
sl@0
   274
			case 'h':	PrtHex4(_readdata(&p, 2, 1)); break;
sl@0
   275
			case 'w':	PrtHex8(_readdata(&p, 4, 1)); break;
sl@0
   276
			}
sl@0
   277
		}
sl@0
   278
	}
sl@0
   279
sl@0
   280
void RunCrashDebugger()
sl@0
   281
	{
sl@0
   282
	char buf[128];
sl@0
   283
	for (;;)
sl@0
   284
		{
sl@0
   285
		int len = GetAndEchoLine(buf, 128, ".");
sl@0
   286
		(void)len;
sl@0
   287
		int i = 0;
sl@0
   288
		int err = 0;
sl@0
   289
		const char* s = buf;
sl@0
   290
		char cmd = *s++;
sl@0
   291
		switch (cmd)
sl@0
   292
			{
sl@0
   293
			case 'x':
sl@0
   294
			case 'X':
sl@0
   295
				return;
sl@0
   296
			case '\0':
sl@0
   297
				break;
sl@0
   298
			case 'm':
sl@0
   299
				{
sl@0
   300
				unsigned long mbase = 0;
sl@0
   301
				unsigned long mlen = 0;
sl@0
   302
				i = ReadRangeHex(s, &mbase, &mlen);
sl@0
   303
				if (i)
sl@0
   304
					DumpMemoryRange1((void*)mbase, mlen, (void*)mbase);
sl@0
   305
				else
sl@0
   306
					err = 2;
sl@0
   307
				break;
sl@0
   308
				}
sl@0
   309
			default:
sl@0
   310
				err = 1;
sl@0
   311
				break;
sl@0
   312
			}
sl@0
   313
		switch (err)
sl@0
   314
			{
sl@0
   315
			case 1: PutS("\r\nUnknown command\r\n"); break;
sl@0
   316
			case 2: PutS("\r\nSyntax error\r\n"); break;
sl@0
   317
			default: break;
sl@0
   318
			}
sl@0
   319
		}
sl@0
   320
	}
sl@0
   321
sl@0
   322
sl@0
   323
sl@0
   324
#ifdef __cplusplus
sl@0
   325
}
sl@0
   326
#endif
sl@0
   327