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