os/kernelhwsrv/userlibandfileserver/fileserver/runtests/runtests.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 // f32\runtests\runtests.cpp
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32std_private.h>
    20 #include <e32def_private.h>
    21 #include <e32svr.h>
    22 #include <e32ldr.h>
    23 #include <hal.h>
    24 #include "f32file.h"
    25 #include "f32dbg.h"
    26 #include "runtests.h"
    27 #include <u32hal.h>
    28 
    29 #define __PANIC(r) Panic(__LINE__,r)
    30 
    31 //#define _RUN_FOREVER_
    32 //#define _PANIC_ON_FAILURE_
    33 
    34 const TInt KDefaultTimeout=1200;		// 20 minutes
    35 const TInt KBackgroundTimeout=3000000;	// 3 seconds
    36 const TInt KDestroyTimeout=1000000;		// 1 second
    37 
    38 _LIT(KLitBackslash,"\\");
    39 _LIT(KDebugMessage, "Testing finished, and the system will be panicked");
    40 #ifdef __EPOC32__
    41 _LIT(KLitDefaultTestPath,"Z:\\TEST");
    42 #else
    43 _LIT(KLitDefaultTestPath,"");
    44 #endif
    45 
    46 #ifdef _DEBUG
    47 _LIT(KBuildType, "UDEB");
    48 #else
    49 _LIT(KBuildType, "UREL");
    50 #endif
    51 
    52 typedef RArray<TUint> RProcIdList;
    53 
    54 GLDEF_D TPath TheTestPath = KLitDefaultTestPath();
    55 GLDEF_D RFs TheFs;
    56 GLDEF_D RLoader TheLoaderSession;
    57 GLDEF_D TDesC8* TheTestList;
    58 GLDEF_D RTimer TheTimer;
    59 GLDEF_D TBuf<256> TheProcessCommand=KNullDesC();
    60 GLDEF_D TInt TheTimeOut = KDefaultTimeout;
    61 GLDEF_D TInt TheCurrentProcessList;
    62 GLDEF_D RProcIdList ProcLists[2];
    63 GLDEF_D RTimer IdleWaitTimer;
    64 GLDEF_D RTimer DestructionTimer;
    65 GLDEF_D RProperty CurrTest;
    66 TBool ShowTimings = 0;
    67 TInt TickPeriod = 15625;
    68 TBool CleanUpProcesses = EFalse;
    69 
    70 #ifdef _ENABLE_BTRACE_ANALYSIS_
    71 
    72 // BTrace analysis forward declarations
    73 const TInt KDefaultBTraceLevel = 0;
    74 TBool BtraceAnalysis = EFalse;
    75 TInt BTraceAnalysisLevel;
    76 TInt BTraceAnalyseSetup();
    77 void BTraceAnalyseEnd();
    78 void BTraceAnalyse(TInt aAnalysisLevel);
    79 
    80 #endif //_ENABLE_BTRACE_ANALYSIS_
    81 
    82 _LIT(KLitPanicCategory,"RUNTESTS-");
    83 _LIT(KLitLogPreamble,"RUNTESTS: ");
    84 
    85 void LogMsg(TRefByValue<const TDesC> aFmt,...);
    86 
    87 void DisableSimulatedFailure()
    88 	{
    89 	// Turn off simulated failure mechanisms for all base servers
    90 	TheFs.SetAllocFailure(KAllocFailureOff);	// F32 heap failure
    91 	TheFs.SetErrorCondition(KErrNone, 0);		// F32 other failure
    92 	TheLoaderSession.DebugFunction(ELoaderDebug_SetHeapFail, 0, 0, 0);	// Loader heap failure
    93 	TheLoaderSession.DebugFunction(ELoaderDebug_SetRFsFail, KErrNone, 0, 0);	// Loader RFs failure
    94 
    95 	// make sure kernel heap debug is off
    96 	__KHEAP_TOTAL_RESET;
    97 	}
    98 
    99 GLDEF_C void Panic(TInt aLine, TInt aReason)
   100 	{
   101 	TBuf<16> cat=KLitPanicCategory();
   102 	cat.AppendNum(aLine);
   103 	User::Panic(cat,aReason);
   104 	}
   105 
   106 TInt CloseAndWait(RHandleBase aH, TRequestStatus *aN = NULL)
   107 	{
   108 	TRequestStatus tempS;
   109 	if(!aN)
   110 		{
   111 		// Create a destruction notifier if none was supplied.
   112 		aH.NotifyDestruction(tempS);
   113 		aN = &tempS;
   114 		}
   115 	if (*aN!=KRequestPending)
   116 		{
   117 		User::WaitForRequest(*aN);
   118 		aH.Close();
   119 		return KErrNoMemory;
   120 		}
   121 	TRequestStatus t;
   122 	DestructionTimer.After(t, KDestroyTimeout);
   123 	aH.Close();
   124 	User::WaitForRequest(*aN, t);
   125 	if (*aN != KRequestPending)
   126 		{
   127 		DestructionTimer.Cancel();
   128 		User::WaitForRequest(t);
   129 		return KErrNone;
   130 		}
   131 	User::CancelMiscNotifier(*aN);
   132 	User::WaitForRequest(*aN);
   133 	return KErrTimedOut;
   134 	}
   135 
   136 void CloseWaitAndWarn(RHandleBase aH, TRequestStatus *aN = NULL)
   137 	{
   138 	TFullName fn(aH.FullName());
   139 	TInt r = CloseAndWait(aH, aN);
   140 	if (r == KErrNoMemory)
   141 		LogMsg(_L("WARNING OOM checking destruction of %S"), &fn);
   142 	else if (r == KErrTimedOut)
   143 		LogMsg(_L("ERROR Destruction of %S timed out"), &fn);
   144 	}
   145 
   146 TInt InitIdleWait()
   147 	{
   148 	TInt r = IdleWaitTimer.CreateLocal();
   149 	if (r!=KErrNone)
   150 		return r;
   151 	return KErrNone;
   152 	}
   153 
   154 void WaitForIdle()
   155 	{
   156 	TRequestStatus idle_req;
   157 	TRequestStatus timer_req;
   158 	IdleWaitTimer.After(timer_req, KBackgroundTimeout);
   159 	User::NotifyOnIdle(idle_req);
   160 	User::WaitForRequest(idle_req, timer_req);
   161 	if (idle_req != KRequestPending)
   162 		{
   163 		IdleWaitTimer.Cancel();
   164 		User::WaitForRequest(timer_req);
   165 		}
   166 	else
   167 		{
   168 		User::CancelMiscNotifier(idle_req);
   169 		User::WaitForRequest(idle_req);
   170 		LogMsg(_L("WARNING Excessive Background Activity Detected"));
   171 		}
   172 	}
   173 
   174 TBool IntentionallyPersistent(RProcess aProcess)
   175 	{
   176 	TInt v;
   177 	TInt r = RProperty::Get(aProcess.SecureId(), KRuntestsIntentionalPersistenceKey, v);
   178 	if (r==KErrNone && TUint(v)==KRuntestsIntentionalPersistenceValue)
   179 		return ETrue;
   180 	return EFalse;
   181 	}
   182 
   183 TInt GetProcessListThread(TAny* a)
   184 	{
   185 	RProcIdList& pl = *(RProcIdList*)a;
   186 	TFindProcess fp(_L("*"));
   187 	TFullName fn;
   188 	TInt r = KErrNone;
   189 	while (r==KErrNone && fp.Next(fn)==KErrNone)
   190 		{
   191 		RProcess p;
   192 		r = p.Open(fp, EOwnerThread);
   193 		if (r==KErrNone)
   194 			{
   195 			TUint id = (TUint)p.Id();
   196 			r = pl.Append(id);
   197 			p.Close();
   198 			}
   199 		}
   200 	return r;
   201 	}
   202 
   203 TInt GetProcessList(RProcIdList& aList)
   204 	{
   205 	aList.Reset();
   206 	RThread t;
   207 	TRequestStatus s;
   208 	TInt r = t.Create(KNullDesC, &GetProcessListThread, 0x1000, NULL, &aList);
   209 	if (r==KErrNone)
   210 		{
   211 		t.Logon(s);
   212 		t.SetPriority(EPriorityAbsoluteHigh);
   213 		if (s==KRequestPending)
   214 			t.Resume();
   215 		User::WaitForRequest(s);
   216 		r=s.Int();
   217 		if (t.ExitType()==EExitPending)
   218 			{
   219 			t.Kill(0);
   220 			WaitForIdle();
   221 			}
   222 		else if (t.ExitType()!=EExitKill)
   223 			{
   224 			r = -99;
   225 			}
   226 		CloseWaitAndWarn(t);
   227 		}
   228 	aList.Sort();
   229 	return r;
   230 	}
   231 
   232 TBool ParseNumber(TLex& aLex, TUint& aNumber, TBool isTime)
   233 	{
   234 	TPtrC numberDes = aLex.NextToken();
   235 	TInt len = numberDes.Length();
   236 	if (len == 0)
   237 		{
   238 		return EFalse;
   239 		}
   240 
   241 	aNumber = 0;
   242 	TInt magnitude = 1;
   243 	TChar c = numberDes[len-1];
   244 	if (isTime)
   245 		{
   246 		switch (c)
   247 			{
   248 			case 'h':
   249 			case 'H':
   250 				len -= 1;
   251 				magnitude = 3600;
   252 				break;
   253 
   254 			case 'm':
   255 			case 'M':
   256 				len -= 1;
   257 				/*FALLTHRU*/
   258 			default:
   259 				magnitude = 60;
   260 				break;
   261 
   262 			case 's':
   263 			case 'S':
   264 				len -= 1;
   265 				magnitude = 1;
   266 				break;
   267 			}
   268 		}
   269 
   270 	for (TInt i = len-1; i >= 0; --i)
   271 		{
   272 		c = numberDes[i];
   273 		if (c < '0' || c > '9')
   274 			__PANIC(KErrArgument);
   275 		aNumber += ((TInt)c-'0')*magnitude;
   276 		magnitude *= 10;
   277 		}
   278 
   279 	return ETrue;
   280 	}
   281 
   282 void GetTimeOut(TLex& aLex)
   283 //
   284 //
   285 //
   286 	{
   287 	TheTimeOut = KDefaultTimeout;
   288 	TUint timeOut = 0;
   289 	if (ParseNumber(aLex, timeOut, ETrue))
   290 		{
   291 		TheTimeOut = timeOut;
   292 		}
   293 	}
   294 
   295 #ifdef _ENABLE_BTRACE_ANALYSIS_
   296 
   297 void GetAnalysisLevel(TLex& aLex)
   298 	{
   299 	BTraceAnalysisLevel = KDefaultBTraceLevel;
   300 	TUint level;
   301 	if (ParseNumber(aLex, level, EFalse))
   302 		{
   303 		BTraceAnalysisLevel = level;
   304 		}
   305 	}
   306 #endif //_ENABLE_BTRACE_ANALYSIS_
   307 
   308 void LogMsg(TRefByValue<const TDesC> aFmt,...)
   309 	{
   310 	VA_LIST list;
   311 	VA_START(list,aFmt);
   312 	TBuf<0x100> buf=KLitLogPreamble();
   313 	buf.AppendFormatList(aFmt,list);
   314 	RDebug::Print(_L("%S"),&buf);
   315 	}
   316 
   317 _LIT(KLitError, "Error ");
   318 TBool LogProcess(TUint aId, TBool aInit)
   319 	{
   320 	TFullName pn;
   321 	TFileName fn;
   322 	RProcess p;
   323 	TBool killed = EFalse;
   324 	TInt r = p.Open(TProcessId(aId));
   325 	if (r==KErrNone)
   326 		{
   327 		if (IntentionallyPersistent(p))
   328 			{
   329 			p.Close();
   330 			return killed;
   331 			}
   332 		pn = p.FullName();
   333 		fn = p.FileName();
   334 		if (!aInit && CleanUpProcesses && p.ExitType()==EExitPending)
   335 			{// p is a left over process so terminate it.
   336 			killed = ETrue;
   337 			TRequestStatus status;
   338 			p.Logon(status);
   339 			p.Kill(KErrNone);	// Kill with KErrNone to suppress extra debug output from kernel.
   340 			User::WaitForRequest(status);
   341 			CloseAndWait(p);
   342 			}
   343 		else
   344 			{
   345 			p.Close();
   346 			}
   347 		}
   348 	else
   349 		{
   350 		pn = KLitError;
   351 		pn.AppendNum(r);
   352 		}
   353 	if (aInit)
   354 		LogMsg(_L("Running process id=%d: %S (%S)"),aId,&pn,&fn);
   355 	else
   356 		{
   357 		if(killed)
   358 			LogMsg(_L("ERROR Leftover process was killed id=%d: %S (%S)"),aId,&pn,&fn);
   359 		else
   360 			LogMsg(_L("ERROR Leftover process id=%d: %S (%S)"),aId,&pn,&fn);
   361 		}
   362 	return killed;
   363 	}
   364 
   365 void ListProcesses()
   366 	{
   367 	RProcIdList& cur_list = ProcLists[TheCurrentProcessList];
   368 	TInt cc = cur_list.Count();
   369 	TInt ci;
   370 	for (ci=0; ci<cc; ++ci)
   371 		{
   372 		LogProcess(cur_list[ci], ETrue);
   373 		}
   374 	}
   375 
   376 void CheckProcesses()
   377 	{
   378 	RProcIdList& cur_list = ProcLists[TheCurrentProcessList];
   379 	RProcIdList& new_list = ProcLists[1-TheCurrentProcessList];
   380 	TInt r = GetProcessList(new_list);
   381 	if (r!=KErrNone)
   382 		{
   383 		LogMsg(_L("WARNING Problem getting process list, error %d"),r);
   384 		return;
   385 		}
   386 
   387 	TInt cc = cur_list.Count();
   388 	TInt nc = new_list.Count();
   389 	TInt ci = 0;
   390 	TInt ni = 0;
   391 	while (ci<cc || ni<nc)
   392 		{
   393 		TUint id1=0;
   394 		TUint id2=0;
   395 		if (ci<cc)
   396 			id1 = cur_list[ci];
   397 		if (ni<nc)
   398 			id2 = new_list[ni];
   399 		if (ci==cc)
   400 			{
   401 			// extra process has appeared so kill it and output an error message.
   402 			if (LogProcess(id2, EFalse))
   403 				{// Remove from list as we don't want it to be considered as vanished when the next test completes.
   404 				new_list.Remove(ni);
   405 				nc--;
   406 				}
   407 			else
   408 				{// Extra process was left running so just move onto the next one.
   409 				ni++;
   410 				}
   411 			}
   412 		else if (ni<nc && id1==id2)
   413 			{
   414 			// process remains
   415 			++ci, ++ni;
   416 			}
   417 		else
   418 			{
   419 			// process has disappeared
   420 			LogMsg(_L("WARNING Vanished process, id=%d"),id1);
   421 			++ci;
   422 			}
   423 		}
   424 
   425 	// current list = new list
   426 	TheCurrentProcessList = 1 - TheCurrentProcessList;
   427 
   428 	// throw away the old list
   429 	cur_list.Reset();
   430 	}
   431 
   432 _LIT8 (KLitRemark,"REM");
   433 _LIT8 (KLitAtRemark,"@REM");
   434 
   435 void ProcessLine(const TDesC8& aLine)
   436 	{
   437 	TLex8 lex(aLine);
   438 	TPtrC8 testname=lex.NextToken();
   439 	if (testname.Length()>=2 && testname[0]=='/' && testname[1]=='/')
   440 		return;
   441 	// ignore this line if it begins with rem or @rem 
   442 	if (testname.CompareF(KLitRemark) == 0 || testname.CompareF(KLitAtRemark) == 0)
   443 		return;
   444 	TFileName testnameU;
   445 	testnameU.Copy(testname);
   446 	TFileName fullpathname;
   447 	if (testnameU.Locate(TChar('\\'))==KErrNotFound)
   448 		fullpathname=TheTestPath;
   449 	fullpathname+=testnameU;
   450 	if (testname.Locate(TChar('.'))==KErrNotFound)
   451 		fullpathname+=_L(".EXE");
   452 	TInt r;
   453 
   454 	RFile file;
   455 	r=file.Open(TheFs,fullpathname,EFileRead);
   456 	if (r!=KErrNone)
   457 		{
   458 		// Remove path to let loader locate exe
   459 		fullpathname = fullpathname.Mid(fullpathname.LocateReverse('\\')+1);
   460 		}
   461 	else
   462 		file.Close();
   463 
   464 	RProcess p;
   465 	if(TheProcessCommand==KNullDesC)
   466 		{
   467 		TheProcessCommand.Copy(lex.Remainder());
   468 		r=p.Create(fullpathname, TheProcessCommand);
   469 		TheProcessCommand=KNullDesC();
   470 		}
   471 	else
   472 		r=p.Create(fullpathname, TheProcessCommand);
   473 	if (r!=KErrNone)
   474 		{
   475 		LogMsg(_L("Test %S ERROR Could not load file, error %d"),&fullpathname,r);
   476 		return;
   477 		}
   478 	else
   479 		{
   480 		LogMsg(_L("Started test %S"),&fullpathname);
   481 		}
   482 	TRequestStatus ds;
   483 	p.NotifyDestruction(ds);	// allocate the destruction notifier early so that it doesn't get flagged as a leak by kernel heap checking in e.g., efile (DEF133800)
   484 	CurrTest.Set(p.FileName());
   485 	User::After(100000);		// allow latency measurements to be output
   486 	p.SetJustInTime(EFalse);	// we don't want the automatic test run to be halted by the debugger
   487 	TRequestStatus ps;
   488 	p.Rendezvous(ps);
   489 	TInt time_remain = TheTimeOut;
   490 	TRequestStatus ts;
   491 	TUint start = User::TickCount();
   492 	p.Resume();
   493 	TBool persist = EFalse;
   494 	TBool timer_running = EFalse;
   495 	FOREVER
   496 		{
   497 		TInt nsec = Min(time_remain, 1800);
   498 		if (!timer_running)
   499 			TheTimer.After(ts, nsec*1000000);
   500 		timer_running = ETrue;
   501 		User::WaitForRequest(ps,ts);
   502 		if (ps!=KRequestPending)
   503 			{
   504 			if (p.ExitType()==EExitPending)
   505 				{
   506 				// rendezvous completed but process not terminated
   507 				if (!IntentionallyPersistent(p))
   508 					{
   509 					// not persistent - wait for process to terminate
   510 					p.Logon(ps);
   511 					continue;
   512 					}
   513 				persist = ETrue;
   514 				}
   515 			break;
   516 			}
   517 		timer_running = EFalse;
   518 		time_remain -= nsec;
   519 		if (time_remain==0)
   520 			{
   521 			LogMsg(_L("Going to kill test %S: it's taken %u seconds, which is too long"),&fullpathname,TheTimeOut);
   522 			p.Kill(0);
   523 			User::WaitForRequest(ps);
   524 			p.Logon(ps);
   525 			User::WaitForRequest(ps);
   526 
   527 			CloseWaitAndWarn(p, &ds);
   528 			RDebug::Print(_L("\n"));
   529 			LogMsg(_L("Test %S TIMEOUT"),&fullpathname);
   530 			return;
   531 			}
   532 		else
   533 			{
   534 			LogMsg(_L("Taken %u seconds so far"),TheTimeOut-time_remain);
   535 			}
   536 		}
   537 	TUint end = User::TickCount();
   538 	if(timer_running)
   539 		{
   540 		TheTimer.Cancel();
   541 		User::WaitForRequest(ts);
   542 		}
   543 
   544 #ifdef _ENABLE_BTRACE_ANALYSIS_
   545 	//
   546 	//
   547 	//
   548 	if (BtraceAnalysis)
   549 		{// Analyse BTrace buffer
   550 		BTraceAnalyse(BTraceAnalysisLevel);
   551 		}
   552 #endif //_ENABLE_BTRACE_ANALYSIS_
   553 
   554 	TBuf<32> exitCat=p.ExitCategory();
   555 	TExitType exitType=p.ExitType();
   556 	TInt exitReason=p.ExitReason();
   557 	if (persist || (exitType==EExitKill && exitReason==KErrNone))
   558 		{
   559 		TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
   560 		if(ShowTimings)
   561 			{
   562 			LogMsg(_L("Test %S OK - Seconds Taken: %u"),&fullpathname, time);
   563 			}
   564 		else
   565 			{
   566 			LogMsg(_L("Test %S OK"),&fullpathname);
   567 			}
   568 		if (persist)
   569 			{
   570 			// We do not need this destruction notifier so cancel it.
   571 			User::CancelMiscNotifier(ds);
   572 			User::WaitForRequest(ds);
   573 			
   574 			p.Close();
   575 			}
   576 		else
   577 			{
   578 			CloseWaitAndWarn(p, &ds);
   579 			}
   580 		return;
   581 		}
   582 	LogMsg(_L("Test %S FAIL - Exit code %d,%d,%S"),&fullpathname,exitType,exitReason,&exitCat);
   583 	CloseWaitAndWarn(p, &ds);
   584 #if defined(_PANIC_ON_FAILURE_)
   585 	__PANIC(KErrGeneral);
   586 #endif
   587 	}
   588 
   589 void ProcessTestList()
   590 	{
   591 	TUint start = User::TickCount();
   592 
   593 	TLex8 llex(*TheTestList);
   594 	while(!llex.Eos())
   595 		{
   596 		llex.SkipSpace();
   597 		llex.Mark();
   598 		while(!llex.Eos() && llex.Peek()!='\n' && llex.Peek()!='\r')
   599 			llex.Inc();
   600 		TPtrC8 line=llex.MarkedToken();
   601 		if (line.Length()!=0)
   602 			ProcessLine(line);
   603 
   604 		// allow cleanup to complete before starting the next test
   605 		WaitForIdle();
   606 
   607 		// make sure simulated failure is off
   608 		DisableSimulatedFailure();
   609 
   610 		// check for leftover processes
   611 		CheckProcesses();
   612 		
   613 		// Reset the demand paging cache to its default size.
   614 		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize, 0, 0);
   615 		}
   616 
   617 	TUint end = User::TickCount();
   618 	TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
   619 	LogMsg(_L("Elapsed Seconds: %u"), time);
   620 	}
   621 
   622 void Help()
   623 	{
   624 	RDebug::Print(_L("Runtests test list [-x where tests are] [-d drive letter to test] [-t timeout] [-p] [-st] [-c]"));
   625 	RDebug::Print(_L("where -p sets runtests to be system permanent, -st enables timing information,"));
   626 	RDebug::Print(_L("-c enables left over processes to be cleaned up"));
   627 	}
   628 
   629 GLDEF_C TInt E32Main()
   630 	{
   631 	HAL::Get(HAL::ESystemTickPeriod, TickPeriod);
   632 	RThread().SetPriority(EPriorityAbsoluteHigh);
   633 	TBuf<0x100> cmd;
   634 	User::CommandLine(cmd);
   635 	TFileName thisfile=RProcess().FileName();
   636 	TLex lex(cmd);
   637 	TPtrC token=lex.NextToken();
   638 	if (token.MatchF(thisfile)==0)
   639 		{
   640 		token.Set(lex.NextToken());
   641 		}
   642 	if (token.Length()==0)
   643 		{//	__PANIC(0);
   644 		Help();
   645 		return 0;
   646 		}
   647 	TFileName listfilename=token;
   648 	while (!lex.Eos())
   649 		{
   650 		token.Set(lex.NextToken());
   651 		if (token.Length()==0)
   652 			break;	// ignore trailing whitespace
   653 		else if (token==_L("-x"))
   654 			{
   655 			token.Set(lex.NextToken());
   656 			TheTestPath = token;
   657 			}
   658 		else if (token==_L("-d"))
   659 			{
   660 			token.Set(lex.NextToken());
   661 			TheProcessCommand = token;
   662 			}
   663 		else if (token==_L("-t"))
   664 			{
   665 			GetTimeOut(lex);
   666 			}
   667 		else if (token==_L("-p"))
   668 			{
   669 			User::SetCritical(User::ESystemPermanent);
   670 			}
   671 		else if (token==_L("-st"))
   672 			ShowTimings = 1;
   673 		
   674 #ifdef _ENABLE_BTRACE_ANALYSIS_		
   675 		else if (token == _L("-a"))
   676 			{
   677 			BtraceAnalysis = ETrue;
   678 			GetAnalysisLevel(lex);
   679 			TInt r = BTraceAnalyseSetup();
   680 			if (r != KErrNone)
   681 				{
   682 				RDebug::Print(_L("ERROR - Couldn't open BTrace driver (Code %d)"), r);
   683 				return 0;
   684 				}
   685 			}
   686 #endif //_ENABLE_BTRACE_ANALYSIS_
   687 
   688 		else if (token == _L("-c"))
   689 			CleanUpProcesses = ETrue;
   690 		else
   691 			{
   692 			RDebug::Print(_L("Unknown option %S"), &token);
   693 			Help();
   694 			return 0;
   695 			}
   696 		}
   697 		
   698 	RDebug::Print(_L("TPTT= %S \n"), &TheProcessCommand);											
   699 	RDebug::Print(_L("TTL= %S \n"), &listfilename);
   700 	RDebug::Print(_L("TTP= %S \n"), &TheTestPath);
   701 	RDebug::Print(_L("TO= %d seconds\n"), TheTimeOut);
   702 
   703 	TInt l=TheTestPath.Length();
   704 	if (l > 0 && TheTestPath[l-1]!='\\')
   705 		TheTestPath+=KLitBackslash;
   706 	if (listfilename.Locate(TChar('\\'))==KErrNotFound)
   707 		listfilename.Insert(0,TheTestPath);
   708 	TInt r=TheFs.Connect();
   709 	if (r!=KErrNone)
   710 		__PANIC(r);
   711 	r = TheLoaderSession.Connect();
   712 	if (r!=KErrNone)
   713 		__PANIC(r);
   714 	DisableSimulatedFailure();
   715 	r=TheFs.SetSessionPath(_L("Z:\\test\\"));
   716 	if (r!=KErrNone)
   717 		__PANIC(r);
   718 	r=TheTimer.CreateLocal();
   719 	if (r!=KErrNone)
   720 		__PANIC(r);
   721 	RFile listfile;
   722 	r=listfile.Open(TheFs,listfilename,EFileRead|EFileShareAny);
   723 	if (r!=KErrNone)
   724 		__PANIC(r);
   725 	TInt listfilesize;
   726 	r=listfile.Size(listfilesize);
   727 	if (r!=KErrNone)
   728 		__PANIC(r);
   729 	HBufC8* pL=HBufC8::New(listfilesize);
   730 	if (!pL)
   731 		__PANIC(KErrNoMemory);
   732 	TPtr8 ptr=pL->Des();
   733 	TheTestList=pL;
   734 	r=listfile.Read(ptr);
   735 	if (r!=KErrNone)
   736 		__PANIC(r);
   737 	listfile.Close();
   738 	LogMsg(_L("Running test script %S"),&listfilename);
   739 	LogMsg(_L("Build %S"),&KBuildType);
   740 	LogMsg(_L("Path to test %S"),&TheProcessCommand);
   741 
   742 	r = RProperty::Define(	KRuntestsCurrentTestKey,
   743 							RProperty::EText,
   744 							TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
   745 							TSecurityPolicy(RProcess().SecureId()),
   746 							512
   747 						);
   748 	if (r!=KErrNone && r!=KErrAlreadyExists)
   749 		__PANIC(r);
   750 	r = CurrTest.Attach(RProcess().SecureId(), KRuntestsCurrentTestKey);
   751 	if (r!=KErrNone)
   752 		__PANIC(r);
   753 	r = CurrTest.Set(KNullDesC);
   754 	if (r!=KErrNone)
   755 		__PANIC(r);
   756 
   757 	r = DestructionTimer.CreateLocal();
   758 	if (r!=KErrNone)
   759 		__PANIC(r);
   760 	TheCurrentProcessList = 0;
   761 	r = GetProcessList(ProcLists[0]);
   762 	if (r!=KErrNone)
   763 		__PANIC(r);
   764 	ListProcesses();
   765 	r = InitIdleWait();
   766 	if (r!=KErrNone)
   767 		__PANIC(r);
   768 #if defined(_RUN_FOREVER_)
   769 	FOREVER
   770 #endif
   771 	ProcessTestList();
   772 	r = CurrTest.Set(KNullDesC);
   773 	if (r!=KErrNone)
   774 		__PANIC(r);
   775 	CurrTest.Close();
   776 	User::After(1000000);	// allow latency measurements to be output before exiting
   777 	LogMsg(_L("Completed test script %S"),&listfilename);
   778 	TheLoaderSession.Close();
   779 	TheFs.Close();
   780 	TheTimer.Close();
   781 	IdleWaitTimer.Close();
   782 	DestructionTimer.Close();
   783 
   784 #ifdef _ENABLE_BTRACE_ANALYSIS_
   785 	BTraceAnalyseEnd();
   786 #endif //_ENABLE_BTRACE_ANALYSIS_
   787 	if(User::Critical()==User::ESystemPermanent)
   788 		RDebug::Print(KDebugMessage);
   789 	return KErrNone;
   790 	}