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