kerneltest/e32test/rm_debug/t_rmdebug_app.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include <e32base_private.h>
       
    18 #include <e32cmn.h>
       
    19 #include <e32cmn_private.h>
       
    20 #include <e32debug.h>
       
    21 #include <e32property.h> 
       
    22 #include "t_rmdebug_app.h"
       
    23 
       
    24 IMPORT_C extern void RMDebug_BranchTst2();
       
    25 
       
    26 LOCAL_C void ParseCommandLineL(TInt32& aFunctionType, TUint& aDelay, TUint& aExtraThreads)
       
    27 	{
       
    28 	// get the length of the command line arguments
       
    29 	TInt argc = User::CommandLineLength();
       
    30 
       
    31 	// allocate a buffer for the command line arguments and extract the data to it
       
    32 	HBufC* commandLine = HBufC::NewLC(argc);
       
    33 	TPtr commandLineBuffer = commandLine->Des();
       
    34 	User::CommandLine(commandLineBuffer);
       
    35 
       
    36 	// create a lexer and read through the command line
       
    37 	TLex lex(*commandLine);
       
    38 	while (!lex.Eos())
       
    39 		{
       
    40 		// expecting the first character to be a '-'
       
    41 		if (lex.Get() == '-')
       
    42 			{
       
    43 			TChar arg = lex.Get();
       
    44 			switch (arg)
       
    45 				{
       
    46 				case 'f':
       
    47 					// the digits following '-f' give the function type
       
    48 					User::LeaveIfError(lex.Val(aFunctionType));
       
    49 					break;
       
    50 				case 'd':
       
    51 					// the digits following '-d' give the delay
       
    52 					User::LeaveIfError(lex.Val(aDelay));
       
    53 					break;
       
    54 				case 'e':
       
    55 					// the digits following '-e' give the number of extra threads to launch
       
    56 					User::LeaveIfError(lex.Val(aExtraThreads));
       
    57 					break;
       
    58 				default:
       
    59 					// unknown argument so leave
       
    60 					User::Leave(KErrArgument);
       
    61 				}
       
    62 			lex.SkipSpace();
       
    63 			}
       
    64 		else
       
    65 			{
       
    66 			// unknown argument so leave
       
    67 			User::Leave(KErrArgument);
       
    68 			}
       
    69 		}
       
    70 
       
    71 	// do clean up
       
    72 	CleanupStack::PopAndDestroy(commandLine);
       
    73 	}
       
    74 
       
    75 typedef void (*TPfun)();
       
    76 
       
    77 // test function to call corresponding to EPrefetchAbortFunction
       
    78 void PrefetchAbort()
       
    79 	{
       
    80 	TPfun f = NULL;
       
    81 	f();
       
    82 	}
       
    83 
       
    84 // test function to call corresponding to EUserPanicFunction
       
    85 void UserPanic()
       
    86 	{
       
    87 	User::Panic(KUserPanic, KUserPanicCode);
       
    88 	}
       
    89 
       
    90 // calls self repeatedly until stack is used up. Slightly convoluted to prevent UREL optimising this out...
       
    91 TUint32 StackOverFlowFunction(TUint32 aInt=0)
       
    92 	{
       
    93 	TUint32 unusedArray[150];
       
    94 	for(TInt i=0; i<150; i++)
       
    95 		{
       
    96 		unusedArray[i] = StackOverFlowFunction(i);
       
    97 		}
       
    98 	return unusedArray[0];
       
    99 	}
       
   100 
       
   101 void DataAbort()
       
   102 	{
       
   103 	TInt* r = (TInt*) 0x1000;
       
   104 	*r = 0x42;              
       
   105 	}
       
   106 
       
   107 void UndefInstruction()
       
   108 	{
       
   109 	TUint32 undef = 0xE6000010;
       
   110 	TPfun f = (TPfun) &undef;
       
   111 	f();
       
   112 	}
       
   113 
       
   114 TInt DataRead()
       
   115 	{
       
   116 	TInt* r = (TInt*) 0x1000;
       
   117 	TInt rr = (TInt)*r;
       
   118 	//include the following line to ensure that rr doesn't get optimised out
       
   119 	RDebug::Printf("Shouldn't see this being printed out: %d", rr);
       
   120 
       
   121 	// Stop compilation warning. Should not get here anyway.
       
   122 	rr++;
       
   123 	return rr;
       
   124 	}
       
   125 
       
   126 void DataWrite()
       
   127 	{
       
   128 	TInt* r = (TInt*) 0x1000;
       
   129 	*r = 0x42;                
       
   130 	}
       
   131 
       
   132 void UserException()
       
   133 	{
       
   134 	User::RaiseException(EExcGeneral);
       
   135 	}
       
   136 
       
   137 void SpinForeverWithBreakPoint()
       
   138 	{
       
   139 
       
   140     // finding the process t_rmdebug2/t_rmdebug2_oem/t_rmdebug2_oem2
       
   141     // we find the process.SID to attach to the property
       
   142 	_LIT(KThreadWildCard, "t_rmdebug2*");
       
   143 
       
   144 	TInt err = KErrNone;
       
   145 	TUid propertySid = KNullUid;
       
   146 	TFindThread find(KThreadWildCard);
       
   147 	TFullName name;
       
   148 	TBool found = EFalse;
       
   149 	while(find.Next(name)==KErrNone && !found)
       
   150 		{
       
   151 		RThread thread;
       
   152 		err = thread.Open(find);
       
   153 		if (err == KErrNone)
       
   154 			{
       
   155 			RProcess process;
       
   156 			thread.Process(process);
       
   157 			TFullName fullname = thread.FullName();
       
   158 		    //RDebug::Printf("SID Search Match Found Name %lS Process ID%ld Thread Id %ld", &fullname, process.Id().Id(), thread.Id().Id());
       
   159 			found = ETrue;
       
   160 			//SID saved so that the property can be attached to
       
   161 			propertySid = process.SecureId();
       
   162 			process.Close();
       
   163 			}
       
   164 		thread.Close();
       
   165 	}
       
   166 
       
   167     //attach to the property to publish the address of the RMDebug_BranchTst2 with the correct SID value
       
   168 	RProperty integerProperty;
       
   169 	err = integerProperty.Attach(propertySid, EMyPropertyInteger, EOwnerThread);
       
   170 	if(KErrNone != err)
       
   171 		RDebug::Printf("Error Attach to the property %d", err);
       
   172 
       
   173 	TInt address = (TInt)&RMDebug_BranchTst2;
       
   174 	
       
   175 	// publish the address where the breakpoint would be set
       
   176 	err = integerProperty.Set(address);
       
   177 	if(KErrNone != err)
       
   178 		RDebug::Printf("Error Set of the property %d", err);
       
   179 	integerProperty.Close();
       
   180 	
       
   181 	//open semaphore to signal the fact we have reached the point where we have to set the property
       
   182 	RSemaphore globsem;
       
   183 	globsem.OpenGlobal(_L("RMDebugGlobSem"));
       
   184 	globsem.Signal();
       
   185 	globsem.Close();
       
   186 
       
   187 	RProcess thisProcess;
       
   188 	TFileName thisProcessName = thisProcess.FileName();
       
   189 	RDebug::Printf("App Process Name %lS process id %ld thread id %ld", &thisProcessName, thisProcess.Id().Id(), RThread().Id().Id());
       
   190 
       
   191 	TInt i=0;
       
   192 	RThread::Rendezvous(KErrNone);
       
   193 	while(i<0xffffffff)
       
   194 		{
       
   195 		RMDebug_BranchTst2();
       
   196 		User::After(10000);
       
   197 		}
       
   198 	}
       
   199 void SpinForever()
       
   200 	{
       
   201 	TInt i=0;
       
   202 	RThread::Rendezvous(KErrNone);
       
   203 	while(i<0xffffffff)
       
   204 		{
       
   205 		User::After(10000);
       
   206 		}
       
   207 	}
       
   208 
       
   209 void LaunchThreads(TUint aNumber)
       
   210 	{
       
   211 	_LIT(KDebugThreadName, "DebugThread");
       
   212 	const TUint KDebugThreadDefaultHeapSize=0x10000;
       
   213 	for(TInt i=0; i<aNumber; i++)
       
   214 		{
       
   215 		RThread thread;
       
   216 		RBuf threadName;
       
   217 		threadName.Create(KDebugThreadName().Length()+10); // the 10 is for appending i to the end of the name
       
   218 		threadName.Append(KDebugThreadName());
       
   219 		threadName.AppendNum(i);
       
   220 		TInt err = thread.Create(threadName, (TThreadFunction)SpinForever, KDefaultStackSize, KDebugThreadDefaultHeapSize, KDebugThreadDefaultHeapSize, NULL);
       
   221 		if(err != KErrNone)
       
   222 			{
       
   223 			RDebug::Printf("Couldn't create thread %d", err);
       
   224 			threadName.Close();
       
   225 			thread.Close();
       
   226 			break;
       
   227 			}
       
   228 		thread.SetPriority(EPriorityNormal);
       
   229 		TRequestStatus status;
       
   230 		thread.Rendezvous(status);
       
   231 		thread.Resume();
       
   232 		User::WaitForRequest(status);
       
   233 		thread.Close();
       
   234 		threadName.Close();
       
   235 		}
       
   236 	}
       
   237 
       
   238 void WaitFiveSecondsThenExit(void)
       
   239 	{
       
   240 	// wait for 5 seconds
       
   241 	User::After(5000000);
       
   242 	}
       
   243 
       
   244 // call the function corresponding to aFunctionType
       
   245 LOCAL_C void CallFunction(TDebugFunctionType aFunctionType, TUint aDelay, TUint aExtraThreads)
       
   246 	{
       
   247 	// pause for aDelay microseconds
       
   248 	User::After(aDelay);
       
   249 
       
   250 	// launch the extra threads
       
   251 	LaunchThreads(aExtraThreads);
       
   252 
       
   253 	// call appropriate function
       
   254 	switch( aFunctionType )
       
   255 		{
       
   256 		case EPrefetchAbortFunction:
       
   257 			PrefetchAbort();
       
   258 			break;
       
   259 		case EUserPanicFunction:
       
   260 			UserPanic();
       
   261 			break;
       
   262 		case EStackOverflowFunction:
       
   263 			StackOverFlowFunction();
       
   264 			break;
       
   265 		case EDataAbortFunction:
       
   266 			DataAbort();
       
   267 			break;
       
   268 		case EUndefInstructionFunction:
       
   269 			UndefInstruction();
       
   270 			break;
       
   271 		case EDataReadErrorFunction:
       
   272 			DataRead();
       
   273 			break;
       
   274 		case EDataWriteErrorFunction:
       
   275 			DataWrite();
       
   276 			break;
       
   277 		case EUserExceptionFunction:
       
   278 			UserException();
       
   279 			break;
       
   280 		case EWaitFiveSecondsThenExit:
       
   281 			WaitFiveSecondsThenExit();
       
   282 			break;
       
   283 		case ESpinForever:
       
   284 			SpinForever();
       
   285 			break;
       
   286 		case ESpinForeverWithBreakPoint:
       
   287 			SpinForeverWithBreakPoint();
       
   288 			break;
       
   289 		case EDefaultDebugFunction:
       
   290 		default:
       
   291 			break;
       
   292 		}
       
   293 	}
       
   294 
       
   295 void PrintHelp()
       
   296 	{
       
   297 	RDebug::Printf("Invoke with arguments:\n");
       
   298 	RDebug::Printf("\t-d<delay>\n\t: delay in microseconds before calling target function\n");
       
   299 	RDebug::Printf("\t-f<function-number>\n\t: enumerator from TDebugFunctionType representing function to call\n");
       
   300 	RDebug::Printf("\t-e<number>\n\t: number of extra threads to launch, these threads run endlessly\n");
       
   301 	}
       
   302 
       
   303 TInt E32Main()
       
   304 	{
       
   305 	// setup heap checking and clean up trap
       
   306 	__UHEAP_MARK;
       
   307 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   308 	RThread().SetPriority(EPriorityNormal);
       
   309 	RProcess::Rendezvous(KErrNone);
       
   310 
       
   311 	// read arguments from command line
       
   312 	TUint delay = 0;
       
   313 	TInt32 functionTypeAsTInt32 = (TInt32)EDefaultDebugFunction;
       
   314 	TUint extraThreads = 0;
       
   315 	TRAPD(err, ParseCommandLineL(functionTypeAsTInt32, delay, extraThreads));
       
   316 
       
   317 	if(KErrNone == err)
       
   318 		{
       
   319 		// if the command line arguments were successfully read then call the appropriate function
       
   320 		CallFunction((TDebugFunctionType)functionTypeAsTInt32, delay, extraThreads);
       
   321 		}
       
   322 
       
   323 	// perform clean up and return any error which was recorded
       
   324 	delete cleanup;
       
   325 	__UHEAP_MARKEND;
       
   326 	return err;
       
   327 	}
       
   328