os/kernelhwsrv/kerneltest/e32test/rm_debug/t_rmdebug_app.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/rm_debug/t_rmdebug_app.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,328 @@
     1.4 +// Copyright (c) 2007-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 +//
    1.18 +
    1.19 +#include <e32base.h>
    1.20 +#include <e32base_private.h>
    1.21 +#include <e32cmn.h>
    1.22 +#include <e32cmn_private.h>
    1.23 +#include <e32debug.h>
    1.24 +#include <e32property.h> 
    1.25 +#include "t_rmdebug_app.h"
    1.26 +
    1.27 +IMPORT_C extern void RMDebug_BranchTst2();
    1.28 +
    1.29 +LOCAL_C void ParseCommandLineL(TInt32& aFunctionType, TUint& aDelay, TUint& aExtraThreads)
    1.30 +	{
    1.31 +	// get the length of the command line arguments
    1.32 +	TInt argc = User::CommandLineLength();
    1.33 +
    1.34 +	// allocate a buffer for the command line arguments and extract the data to it
    1.35 +	HBufC* commandLine = HBufC::NewLC(argc);
    1.36 +	TPtr commandLineBuffer = commandLine->Des();
    1.37 +	User::CommandLine(commandLineBuffer);
    1.38 +
    1.39 +	// create a lexer and read through the command line
    1.40 +	TLex lex(*commandLine);
    1.41 +	while (!lex.Eos())
    1.42 +		{
    1.43 +		// expecting the first character to be a '-'
    1.44 +		if (lex.Get() == '-')
    1.45 +			{
    1.46 +			TChar arg = lex.Get();
    1.47 +			switch (arg)
    1.48 +				{
    1.49 +				case 'f':
    1.50 +					// the digits following '-f' give the function type
    1.51 +					User::LeaveIfError(lex.Val(aFunctionType));
    1.52 +					break;
    1.53 +				case 'd':
    1.54 +					// the digits following '-d' give the delay
    1.55 +					User::LeaveIfError(lex.Val(aDelay));
    1.56 +					break;
    1.57 +				case 'e':
    1.58 +					// the digits following '-e' give the number of extra threads to launch
    1.59 +					User::LeaveIfError(lex.Val(aExtraThreads));
    1.60 +					break;
    1.61 +				default:
    1.62 +					// unknown argument so leave
    1.63 +					User::Leave(KErrArgument);
    1.64 +				}
    1.65 +			lex.SkipSpace();
    1.66 +			}
    1.67 +		else
    1.68 +			{
    1.69 +			// unknown argument so leave
    1.70 +			User::Leave(KErrArgument);
    1.71 +			}
    1.72 +		}
    1.73 +
    1.74 +	// do clean up
    1.75 +	CleanupStack::PopAndDestroy(commandLine);
    1.76 +	}
    1.77 +
    1.78 +typedef void (*TPfun)();
    1.79 +
    1.80 +// test function to call corresponding to EPrefetchAbortFunction
    1.81 +void PrefetchAbort()
    1.82 +	{
    1.83 +	TPfun f = NULL;
    1.84 +	f();
    1.85 +	}
    1.86 +
    1.87 +// test function to call corresponding to EUserPanicFunction
    1.88 +void UserPanic()
    1.89 +	{
    1.90 +	User::Panic(KUserPanic, KUserPanicCode);
    1.91 +	}
    1.92 +
    1.93 +// calls self repeatedly until stack is used up. Slightly convoluted to prevent UREL optimising this out...
    1.94 +TUint32 StackOverFlowFunction(TUint32 aInt=0)
    1.95 +	{
    1.96 +	TUint32 unusedArray[150];
    1.97 +	for(TInt i=0; i<150; i++)
    1.98 +		{
    1.99 +		unusedArray[i] = StackOverFlowFunction(i);
   1.100 +		}
   1.101 +	return unusedArray[0];
   1.102 +	}
   1.103 +
   1.104 +void DataAbort()
   1.105 +	{
   1.106 +	TInt* r = (TInt*) 0x1000;
   1.107 +	*r = 0x42;              
   1.108 +	}
   1.109 +
   1.110 +void UndefInstruction()
   1.111 +	{
   1.112 +	TUint32 undef = 0xE6000010;
   1.113 +	TPfun f = (TPfun) &undef;
   1.114 +	f();
   1.115 +	}
   1.116 +
   1.117 +TInt DataRead()
   1.118 +	{
   1.119 +	TInt* r = (TInt*) 0x1000;
   1.120 +	TInt rr = (TInt)*r;
   1.121 +	//include the following line to ensure that rr doesn't get optimised out
   1.122 +	RDebug::Printf("Shouldn't see this being printed out: %d", rr);
   1.123 +
   1.124 +	// Stop compilation warning. Should not get here anyway.
   1.125 +	rr++;
   1.126 +	return rr;
   1.127 +	}
   1.128 +
   1.129 +void DataWrite()
   1.130 +	{
   1.131 +	TInt* r = (TInt*) 0x1000;
   1.132 +	*r = 0x42;                
   1.133 +	}
   1.134 +
   1.135 +void UserException()
   1.136 +	{
   1.137 +	User::RaiseException(EExcGeneral);
   1.138 +	}
   1.139 +
   1.140 +void SpinForeverWithBreakPoint()
   1.141 +	{
   1.142 +
   1.143 +    // finding the process t_rmdebug2/t_rmdebug2_oem/t_rmdebug2_oem2
   1.144 +    // we find the process.SID to attach to the property
   1.145 +	_LIT(KThreadWildCard, "t_rmdebug2*");
   1.146 +
   1.147 +	TInt err = KErrNone;
   1.148 +	TUid propertySid = KNullUid;
   1.149 +	TFindThread find(KThreadWildCard);
   1.150 +	TFullName name;
   1.151 +	TBool found = EFalse;
   1.152 +	while(find.Next(name)==KErrNone && !found)
   1.153 +		{
   1.154 +		RThread thread;
   1.155 +		err = thread.Open(find);
   1.156 +		if (err == KErrNone)
   1.157 +			{
   1.158 +			RProcess process;
   1.159 +			thread.Process(process);
   1.160 +			TFullName fullname = thread.FullName();
   1.161 +		    //RDebug::Printf("SID Search Match Found Name %lS Process ID%ld Thread Id %ld", &fullname, process.Id().Id(), thread.Id().Id());
   1.162 +			found = ETrue;
   1.163 +			//SID saved so that the property can be attached to
   1.164 +			propertySid = process.SecureId();
   1.165 +			process.Close();
   1.166 +			}
   1.167 +		thread.Close();
   1.168 +	}
   1.169 +
   1.170 +    //attach to the property to publish the address of the RMDebug_BranchTst2 with the correct SID value
   1.171 +	RProperty integerProperty;
   1.172 +	err = integerProperty.Attach(propertySid, EMyPropertyInteger, EOwnerThread);
   1.173 +	if(KErrNone != err)
   1.174 +		RDebug::Printf("Error Attach to the property %d", err);
   1.175 +
   1.176 +	TInt address = (TInt)&RMDebug_BranchTst2;
   1.177 +	
   1.178 +	// publish the address where the breakpoint would be set
   1.179 +	err = integerProperty.Set(address);
   1.180 +	if(KErrNone != err)
   1.181 +		RDebug::Printf("Error Set of the property %d", err);
   1.182 +	integerProperty.Close();
   1.183 +	
   1.184 +	//open semaphore to signal the fact we have reached the point where we have to set the property
   1.185 +	RSemaphore globsem;
   1.186 +	globsem.OpenGlobal(_L("RMDebugGlobSem"));
   1.187 +	globsem.Signal();
   1.188 +	globsem.Close();
   1.189 +
   1.190 +	RProcess thisProcess;
   1.191 +	TFileName thisProcessName = thisProcess.FileName();
   1.192 +	RDebug::Printf("App Process Name %lS process id %ld thread id %ld", &thisProcessName, thisProcess.Id().Id(), RThread().Id().Id());
   1.193 +
   1.194 +	TInt i=0;
   1.195 +	RThread::Rendezvous(KErrNone);
   1.196 +	while(i<0xffffffff)
   1.197 +		{
   1.198 +		RMDebug_BranchTst2();
   1.199 +		User::After(10000);
   1.200 +		}
   1.201 +	}
   1.202 +void SpinForever()
   1.203 +	{
   1.204 +	TInt i=0;
   1.205 +	RThread::Rendezvous(KErrNone);
   1.206 +	while(i<0xffffffff)
   1.207 +		{
   1.208 +		User::After(10000);
   1.209 +		}
   1.210 +	}
   1.211 +
   1.212 +void LaunchThreads(TUint aNumber)
   1.213 +	{
   1.214 +	_LIT(KDebugThreadName, "DebugThread");
   1.215 +	const TUint KDebugThreadDefaultHeapSize=0x10000;
   1.216 +	for(TInt i=0; i<aNumber; i++)
   1.217 +		{
   1.218 +		RThread thread;
   1.219 +		RBuf threadName;
   1.220 +		threadName.Create(KDebugThreadName().Length()+10); // the 10 is for appending i to the end of the name
   1.221 +		threadName.Append(KDebugThreadName());
   1.222 +		threadName.AppendNum(i);
   1.223 +		TInt err = thread.Create(threadName, (TThreadFunction)SpinForever, KDefaultStackSize, KDebugThreadDefaultHeapSize, KDebugThreadDefaultHeapSize, NULL);
   1.224 +		if(err != KErrNone)
   1.225 +			{
   1.226 +			RDebug::Printf("Couldn't create thread %d", err);
   1.227 +			threadName.Close();
   1.228 +			thread.Close();
   1.229 +			break;
   1.230 +			}
   1.231 +		thread.SetPriority(EPriorityNormal);
   1.232 +		TRequestStatus status;
   1.233 +		thread.Rendezvous(status);
   1.234 +		thread.Resume();
   1.235 +		User::WaitForRequest(status);
   1.236 +		thread.Close();
   1.237 +		threadName.Close();
   1.238 +		}
   1.239 +	}
   1.240 +
   1.241 +void WaitFiveSecondsThenExit(void)
   1.242 +	{
   1.243 +	// wait for 5 seconds
   1.244 +	User::After(5000000);
   1.245 +	}
   1.246 +
   1.247 +// call the function corresponding to aFunctionType
   1.248 +LOCAL_C void CallFunction(TDebugFunctionType aFunctionType, TUint aDelay, TUint aExtraThreads)
   1.249 +	{
   1.250 +	// pause for aDelay microseconds
   1.251 +	User::After(aDelay);
   1.252 +
   1.253 +	// launch the extra threads
   1.254 +	LaunchThreads(aExtraThreads);
   1.255 +
   1.256 +	// call appropriate function
   1.257 +	switch( aFunctionType )
   1.258 +		{
   1.259 +		case EPrefetchAbortFunction:
   1.260 +			PrefetchAbort();
   1.261 +			break;
   1.262 +		case EUserPanicFunction:
   1.263 +			UserPanic();
   1.264 +			break;
   1.265 +		case EStackOverflowFunction:
   1.266 +			StackOverFlowFunction();
   1.267 +			break;
   1.268 +		case EDataAbortFunction:
   1.269 +			DataAbort();
   1.270 +			break;
   1.271 +		case EUndefInstructionFunction:
   1.272 +			UndefInstruction();
   1.273 +			break;
   1.274 +		case EDataReadErrorFunction:
   1.275 +			DataRead();
   1.276 +			break;
   1.277 +		case EDataWriteErrorFunction:
   1.278 +			DataWrite();
   1.279 +			break;
   1.280 +		case EUserExceptionFunction:
   1.281 +			UserException();
   1.282 +			break;
   1.283 +		case EWaitFiveSecondsThenExit:
   1.284 +			WaitFiveSecondsThenExit();
   1.285 +			break;
   1.286 +		case ESpinForever:
   1.287 +			SpinForever();
   1.288 +			break;
   1.289 +		case ESpinForeverWithBreakPoint:
   1.290 +			SpinForeverWithBreakPoint();
   1.291 +			break;
   1.292 +		case EDefaultDebugFunction:
   1.293 +		default:
   1.294 +			break;
   1.295 +		}
   1.296 +	}
   1.297 +
   1.298 +void PrintHelp()
   1.299 +	{
   1.300 +	RDebug::Printf("Invoke with arguments:\n");
   1.301 +	RDebug::Printf("\t-d<delay>\n\t: delay in microseconds before calling target function\n");
   1.302 +	RDebug::Printf("\t-f<function-number>\n\t: enumerator from TDebugFunctionType representing function to call\n");
   1.303 +	RDebug::Printf("\t-e<number>\n\t: number of extra threads to launch, these threads run endlessly\n");
   1.304 +	}
   1.305 +
   1.306 +TInt E32Main()
   1.307 +	{
   1.308 +	// setup heap checking and clean up trap
   1.309 +	__UHEAP_MARK;
   1.310 +	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.311 +	RThread().SetPriority(EPriorityNormal);
   1.312 +	RProcess::Rendezvous(KErrNone);
   1.313 +
   1.314 +	// read arguments from command line
   1.315 +	TUint delay = 0;
   1.316 +	TInt32 functionTypeAsTInt32 = (TInt32)EDefaultDebugFunction;
   1.317 +	TUint extraThreads = 0;
   1.318 +	TRAPD(err, ParseCommandLineL(functionTypeAsTInt32, delay, extraThreads));
   1.319 +
   1.320 +	if(KErrNone == err)
   1.321 +		{
   1.322 +		// if the command line arguments were successfully read then call the appropriate function
   1.323 +		CallFunction((TDebugFunctionType)functionTypeAsTInt32, delay, extraThreads);
   1.324 +		}
   1.325 +
   1.326 +	// perform clean up and return any error which was recorded
   1.327 +	delete cleanup;
   1.328 +	__UHEAP_MARKEND;
   1.329 +	return err;
   1.330 +	}
   1.331 +