debugsrv/runmodedebug/rmdebug_test/rm_debug/basic_tests/t_rmdebug2.cpp
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     1 // Copyright (c) 2006-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 // Tests the functionality of the run mode debug device driver.
       
    15 //
       
    16 //
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <e32base_private.h>
       
    20 #include <e32cons.h>
       
    21 #include <e32test.h>
       
    22 #include <e32ldr.h>
       
    23 #include <e32cmn.h>
       
    24 #include <e32cmn_private.h>
       
    25 #include <f32dbg.h>
       
    26 #include <f32file.h>
       
    27 #include <hal.h>
       
    28 #include <u32hal.h>
       
    29 #include <e32property.h>
       
    30 
       
    31 #include "t_rmdebug_dll.h"
       
    32 
       
    33 #include <rm_debug_api.h>
       
    34 #include "d_rmdebugthread2.h"
       
    35 #include "t_rmdebug2.h"
       
    36 #include "t_rmdebug_app.h"
       
    37 
       
    38 #ifdef __MARM_ARMV4__
       
    39 #include "d_rmdebug_step_test_armv4.h"
       
    40 #endif
       
    41 
       
    42 #ifdef __MARM_ARMV5__
       
    43 #include "d_rmdebug_step_test.h"
       
    44 #include "d_rmdebug_bkpt_test.h"
       
    45 #endif
       
    46 
       
    47 #include "d_demand_paging.h"
       
    48 
       
    49 #ifdef KERNEL_OOM_TESTING
       
    50 	#ifdef USER_OOM_TESTING
       
    51 		#error "Cannot define both KERNEL_OOM_TESTING and USER_OOM_TESTING"
       
    52 	#endif
       
    53 #endif
       
    54 
       
    55 #if  defined (NO_DEBUGTOKEN)  || defined (SOMECAPS_DEBUGTOKEN) || defined (FEWCAPS_DEBUGTOKEN)
       
    56 _LIT8(KCrashDummyData, "This is a sample write");
       
    57 #endif
       
    58 
       
    59 using namespace Debug;
       
    60 
       
    61 const TVersion securityServerVersion(0,1,1);
       
    62 
       
    63 const TVersion testVersion(2,1,0);
       
    64 
       
    65 IMPORT_C TInt StartDebugThread(RThread& aServerThread, const TDesC& aDebugThreadName);
       
    66  
       
    67 extern TInt TestData;
       
    68 extern TTestFunction FunctionChooser;
       
    69 extern TBuf8<SYMBIAN_RMDBG_MEMORYSIZE> gMemoryAccessBytes;
       
    70 
       
    71 IMPORT_C TInt TestFunction();
       
    72 IMPORT_C void TestPagedCode();
       
    73 IMPORT_C extern TInt RMDebugDemandPagingTest();
       
    74 
       
    75 // Device driver name
       
    76 _LIT(KDebugDriverFileName,"rm_debug.ldd");
       
    77 _LIT(KRMDebugAppName, "t_rmdebug_app");
       
    78 
       
    79 
       
    80 #if defined(NO_DEBUGTOKEN)
       
    81     _LIT(KTestName, "T_RMDEBUG2");
       
    82 #elif defined(SOMECAPS_DEBUGTOKEN)
       
    83     _LIT(KTestName, "T_RMDEBUG2_OEM");
       
    84 #elif defined(FEWCAPS_DEBUGTOKEN)
       
    85     _LIT(KTestName, "T_RMDEBUG2_OEM2");
       
    86 #elif defined(ALLCAPS_DEBUGTOKEN)
       
    87     _LIT(KTestName, "T_RMDEBUG2_ALLCAPS");
       
    88 #endif
       
    89 
       
    90 #define TIMED_WAIT(request, timeoutInMs) CRunModeAgent::TimedWait(request, timeoutInMs, __LINE__)
       
    91 
       
    92 LOCAL_D RTest test(KTestName);
       
    93 
       
    94 TBool gUseDelay;
       
    95 
       
    96 CRunModeAgent::CRunModeAgent()
       
    97 //
       
    98 // CRunModeAgent constructor
       
    99 //
       
   100 	{
       
   101 	FillArray();
       
   102 	RProcess thisProcess;
       
   103 	iFileName = thisProcess.FileName();
       
   104 	thisProcess.Close();
       
   105 	}
       
   106 
       
   107 CRunModeAgent* CRunModeAgent::NewL()
       
   108 //
       
   109 // CRunModeAgent::NewL
       
   110 //
       
   111 	{
       
   112 	CRunModeAgent* self = new(ELeave) CRunModeAgent();
       
   113 
       
   114   	self->ConstructL();
       
   115 
       
   116 	return self;
       
   117 	}
       
   118 
       
   119 CRunModeAgent::~CRunModeAgent()
       
   120 //
       
   121 // CRunModeAgent destructor
       
   122 //
       
   123 	{
       
   124     iTimer.Close();
       
   125     iRunCountSubscribe.Close();
       
   126     
       
   127 	User::FreeLogicalDevice(KDebugDriverFileName);
       
   128 	iServSession.Close();
       
   129 	iDebugThread.Close();
       
   130 	}
       
   131 
       
   132 void CRunModeAgent::ConstructL()
       
   133 //
       
   134 // CRunModeAgent::ConstructL
       
   135 //
       
   136 	{
       
   137 	// nothing to do here
       
   138 	}
       
   139 
       
   140 void CRunModeAgent::SetupAndAttachToDSS()
       
   141 //
       
   142 // CRunModeAgent::SetupAndAttachToDSS
       
   143 //
       
   144 	{
       
   145 	TInt err = StartDebugThread(iDebugThread, KDebugThreadName);
       
   146 
       
   147 	// get the thread id for use in the tests
       
   148 	iThreadID = iDebugThread.Id();
       
   149 
       
   150 	if (err != KErrNone)
       
   151 		{
       
   152 		User::Panic(_L("Can't start debug thread"), err);
       
   153 		}
       
   154 
       
   155     err = iRunCountSubscribe.Attach( RProcess().SecureId(), CDebugServThread::ERMDBGRunCountProperty);
       
   156     if (err != KErrNone)
       
   157         {
       
   158         User::Panic(_L("Can't attach to RProperty iRunCountSubscribe"), err);
       
   159         }
       
   160 
       
   161     err = iTimer.CreateLocal(); 
       
   162     if (err != KErrNone)
       
   163         {
       
   164         User::Panic(_L("Can't create RTimer::CreateLocal()"), err);
       
   165         }
       
   166     
       
   167 	err = iServSession.Connect(securityServerVersion);
       
   168 	if (err != KErrNone)
       
   169 		{
       
   170 		User::Panic(_L("Can't open server session"), err);
       
   171 		}
       
   172 	}
       
   173 
       
   174 CRunModeAgent *RunModeAgent;
       
   175 
       
   176 // helper function to check whether the listing of type aListId is supported for a scope of aListScope
       
   177 TBool CRunModeAgent::ListingSupported(const TListId aListId, const TListScope aListScope)
       
   178 	{
       
   179 	TTag tag = GetTag(ETagHeaderList, aListId);
       
   180 
       
   181 	return (tag.iValue) & aListScope;
       
   182 	}
       
   183 
       
   184 //---------------------------------------------
       
   185 //! @SYMTestCaseID KBase-T-RMDEBUG2-0426
       
   186 //! @SYMTestType
       
   187 //! @SYMPREQ PREQ1426
       
   188 //! @SYMTestCaseDesc Test getting the list of XIP libraries
       
   189 //! @SYMTestActions The XIP library list should be successfully obtained
       
   190 //! @SYMTestExpectedResults The specified ldd file should be present in the obtained listing
       
   191 //! @SYMTestPriority High
       
   192 //! @SYMTestStatus Implemented
       
   193 //---------------------------------------------
       
   194 void CRunModeAgent::TestGetXipLibrariesList()
       
   195 	{
       
   196 	test.Next(_L("TestGetXipLibrariesList\n"));
       
   197 
       
   198 	test(ListingSupported(EXipLibraries, EScopeGlobal));
       
   199 	test(!ListingSupported(EXipLibraries, EScopeProcessSpecific));
       
   200 	test(!ListingSupported(EXipLibraries, EScopeThreadSpecific));
       
   201 
       
   202 	//allocate a very small buffer so the GetList call initially fails
       
   203 	RBuf8 buffer;
       
   204 	test(KErrNone == buffer.Create(1));
       
   205 	TUint32 size = 0;
       
   206 
       
   207 	//get the list data
       
   208 	DoGetList(EXipLibraries, EScopeGlobal, buffer, size);
       
   209 
       
   210 	//search the buffer for entry corresponding to the debug kernel driver
       
   211 	//which should be in the rom
       
   212 	_LIT(KRmDebugLddName, "z:\\sys\\bin\\rm_debug.ldd");
       
   213 
       
   214 	//iterate through the buffer and set found to ETrue if we find the driver
       
   215 	TBool found = EFalse;
       
   216 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
   217 	const TUint8* ptrEnd = ptr + size;
       
   218 	while(ptr < ptrEnd)
       
   219 		{
       
   220 		TXipLibraryListEntry& xipLibrary = *(TXipLibraryListEntry*)ptr;
       
   221 
       
   222 		//get the name of the library
       
   223 		TPtr name(&xipLibrary.iName[0], xipLibrary.iNameLength, xipLibrary.iNameLength);
       
   224 		if(name.CompareF(KRmDebugLddName()) == 0)
       
   225 			{
       
   226 			//found the library but continue reading the rest of the buffer to
       
   227 			//check nothing bad happens towards the end
       
   228 			found = ETrue;
       
   229 			}
       
   230 		//move pointer on to next library
       
   231 		ptr += Align4(xipLibrary.GetSize());
       
   232 		}
       
   233 	test(found);
       
   234 
       
   235 	//do cleanup
       
   236 	buffer.Close();
       
   237 	}
       
   238 
       
   239 //---------------------------------------------
       
   240 //! @SYMTestCaseID KBase-T-RMDEBUG2-0427
       
   241 //! @SYMTestType
       
   242 //! @SYMPREQ PREQ1426
       
   243 //! @SYMTestCaseDesc Test getting the list of executables
       
   244 //! @SYMTestActions The list of debuggable executable files should be obtained
       
   245 //! @SYMTestExpectedResults The client exe should appear in the list
       
   246 //! @SYMTestPriority High
       
   247 //! @SYMTestStatus Implemented
       
   248 //---------------------------------------------
       
   249 void CRunModeAgent::TestGetExecutablesList()
       
   250 	{
       
   251 	test.Next(_L("TestGetExecutablesList\n"));
       
   252 
       
   253 	test(ListingSupported(EExecutables, EScopeGlobal));
       
   254 	test(!ListingSupported(EExecutables, EScopeProcessSpecific));
       
   255 	test(!ListingSupported(EExecutables, EScopeThreadSpecific));
       
   256 
       
   257 	//allocate a very small buffer so the GetList call initially fails
       
   258 	RBuf8 buffer;
       
   259 	test(KErrNone == buffer.Create(1));
       
   260 	TUint32 size = 0;
       
   261 
       
   262 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   263 
       
   264 	//get the list data
       
   265 	DoGetList(EExecutables, EScopeGlobal, buffer, size);
       
   266 
       
   267 	//get this process' name
       
   268 	RProcess thisProcess;
       
   269 	TFileName thisProcessName = thisProcess.FileName();
       
   270 
       
   271 	//look through the buffer and check if the target debug thread is there
       
   272 	TBool found = EFalse;
       
   273 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
   274 	const TUint8* ptrEnd = ptr + size;
       
   275 	while(ptr < ptrEnd)
       
   276 		{
       
   277 		TExecutablesListEntry& entry = *(TExecutablesListEntry*)ptr;
       
   278 		//get name
       
   279 		TPtr name(&entry.iName[0], entry.iNameLength, entry.iNameLength);
       
   280 		if( (entry.iIsActivelyDebugged != 0) && (0 == thisProcessName.CompareF(name)) )
       
   281 			{
       
   282 			//found this process and asserted it is being actively debugged
       
   283 			found = ETrue;
       
   284 			}
       
   285 		//move pointer on to next entry
       
   286 		ptr += Align4(entry.GetSize());
       
   287 		}
       
   288 	test(found);
       
   289 
       
   290 	//clean up
       
   291 	buffer.Close();
       
   292 
       
   293 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   294 	}
       
   295 
       
   296 //---------------------------------------------
       
   297 //! @SYMTestCaseID KBase-T-RMDEBUG2-0428
       
   298 //! @SYMTestType
       
   299 //! @SYMPREQ PREQ1426
       
   300 //! @SYMTestCaseDesc Test error conditions for the GetList calls
       
   301 //! @SYMTestActions Multiple calls to test calling GetList with bad arguments
       
   302 //! @SYMTestExpectedResults All tests should fail with the appropriate error codes
       
   303 //! @SYMTestPriority High
       
   304 //! @SYMTestStatus Implemented
       
   305 //---------------------------------------------
       
   306 void CRunModeAgent::TestGetListInvalidData()
       
   307 	{
       
   308 	test.Next(_L("TestGetListInvalidData\n"));
       
   309 
       
   310 	//allocate a buffer, the size should not matter as expecting all calls to fail
       
   311 	RBuf8 buffer;
       
   312 	test(KErrNone == buffer.Create(1));
       
   313 	TUint32 size = 0;
       
   314 
       
   315 	//test what happens if we ask for an unsupported list type globally
       
   316 	test(KErrNotSupported == iServSession.GetList((TListId)1234, buffer, size));
       
   317 
       
   318 	//test what happens if we ask for an unsupported list type
       
   319 	test(KErrNotSupported == iServSession.GetList(RThread().Id(), (TListId)1234, buffer, size));
       
   320 
       
   321 	//test what happens if we try to get a non-global libraries list
       
   322 	test(KErrArgument == iServSession.GetList(RThread().Id(), EXipLibraries, buffer, size));
       
   323 
       
   324 	//test what happens if we try to get a non-global executables list
       
   325 	test(KErrArgument == iServSession.GetList(RThread().Id(), EExecutables, buffer, size));
       
   326 
       
   327 	//test what happens if we try to get a non-global process list
       
   328 	test(KErrArgument == iServSession.GetList(RThread().Id(), EProcesses, buffer, size));
       
   329 
       
   330 	//check that using a process id fails
       
   331 	test(KErrArgument == iServSession.GetList(RProcess().Id(), EProcesses, buffer, size));
       
   332 
       
   333 	//check that specifying a non-existant thread id fails
       
   334 	test(KErrArgument == iServSession.GetList((TThreadId)0x12345678, EThreads, buffer, size));
       
   335 
       
   336 	//check that specifying a non-existant process id fails
       
   337 	test(KErrArgument == iServSession.GetList((TProcessId)0x12345678, EThreads, buffer, size));
       
   338 
       
   339 	//check that specifying a non-existant thread id fails
       
   340 	test(KErrArgument == iServSession.GetList((TThreadId)0x12345678, ECodeSegs, buffer, size));
       
   341 
       
   342 	//check that specifying a non-existant process id fails
       
   343 	test(KErrArgument == iServSession.GetList((TProcessId)0x12345678, ECodeSegs, buffer, size));
       
   344 
       
   345 	//cleanup
       
   346 	buffer.Close();
       
   347 	}
       
   348 
       
   349 //---------------------------------------------
       
   350 //! @SYMTestCaseID KBase-T-RMDEBUG2-0429
       
   351 //! @SYMTestType
       
   352 //! @SYMPREQ PREQ1426
       
   353 //! @SYMTestCaseDesc Test getting the process list
       
   354 //! @SYMTestActions Get the process listing
       
   355 //! @SYMTestExpectedResults The process listing should be successfully obtained and the current process should be present in the list
       
   356 //! @SYMTestPriority High
       
   357 //! @SYMTestStatus Implemented
       
   358 //---------------------------------------------
       
   359 void CRunModeAgent::TestGetProcessList()
       
   360 	{
       
   361 	test.Next(_L("TestGetProcessList\n"));
       
   362 
       
   363 	test(ListingSupported(EProcesses, EScopeGlobal));
       
   364 	test(!ListingSupported(EProcesses, EScopeProcessSpecific));
       
   365 	test(!ListingSupported(EProcesses, EScopeThreadSpecific));
       
   366 
       
   367 	//allocate a very small buffer so the GetList call fails
       
   368 	RBuf8 buffer;
       
   369 	test(KErrNone == buffer.Create(1));
       
   370 	TUint32 size = 0;
       
   371 
       
   372 	//get the list data
       
   373 	DoGetList(EProcesses, EScopeGlobal, buffer, size);
       
   374 
       
   375 	//initialise data about the target debug thread to compare the kernel's data against
       
   376 	RProcess thisProcess;
       
   377 	TFileName thisProcessName = thisProcess.FileName();
       
   378 	TUint32 processId = thisProcess.Id().Id();
       
   379 
       
   380 	//look through the buffer and check if the target debug thread is there
       
   381 	TBool found = EFalse;
       
   382 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
   383 	const TUint8* ptrEnd = ptr + size;
       
   384 	while(ptr < ptrEnd)
       
   385 		{
       
   386 		TProcessListEntry& entry = *(TProcessListEntry*)ptr;
       
   387 		if( (RProcess().Id().Id() == entry.iProcessId) &&
       
   388 			(0 == thisProcessName.CompareF(TPtr(&(entry.iNames[0]), entry.iFileNameLength, entry.iFileNameLength))) &&
       
   389 		 	(0 == thisProcess.FullName().CompareF(TPtr(&(entry.iNames[0]) + entry.iFileNameLength, entry.iDynamicNameLength, entry.iDynamicNameLength))) &&
       
   390 			0x4321bbbb /* Magic */ == entry.iUid3)
       
   391 			{
       
   392 			//if all match then we've found it
       
   393 			found = ETrue;
       
   394 			}
       
   395 		ptr += Align4(entry.GetSize());
       
   396 		}
       
   397 
       
   398 	//check whether the expected result happened
       
   399 	test(found);
       
   400 
       
   401 	//clean up
       
   402 	buffer.Close();
       
   403 	}
       
   404 
       
   405 //---------------------------------------------
       
   406 //! @SYMTestCaseID KBase-T-RMDEBUG2-0430
       
   407 //! @SYMTestType
       
   408 //! @SYMPREQ PREQ1426
       
   409 //! @SYMTestCaseDesc Test getting the thread list
       
   410 //! @SYMTestActions Get the thread listing globally and for a specified thread or process
       
   411 //! @SYMTestExpectedResults The thread listings should all be successfully obtained and the current thread should be present in all listings
       
   412 //! @SYMTestPriority High
       
   413 //! @SYMTestStatus Implemented
       
   414 //---------------------------------------------
       
   415 void CRunModeAgent::TestGetThreadList()
       
   416 	{
       
   417 	test.Next(_L("TestGetThreadList\n"));
       
   418 
       
   419 	test(ListingSupported(EThreads, EScopeGlobal));
       
   420 	test(ListingSupported(EThreads, EScopeProcessSpecific));
       
   421 	test(ListingSupported(EThreads, EScopeThreadSpecific));
       
   422 
       
   423 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   424 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
   425 
       
   426 	TBool found = EFalse;
       
   427 	
       
   428 	/* We need these loops because on some system the kernel run mode debugger does not 
       
   429 	 immediately present the thread in the thread list. 
       
   430 	 */
       
   431 	
       
   432 	for(TInt retryCount = 0; retryCount < 10 && !found; retryCount++ )
       
   433 		{
       
   434 		//test getting this process's thread list, ETrue as should find the target debug thread
       
   435 		User::After(50000);
       
   436 		found = DoTestGetThreadList(ETrue, EScopeProcessSpecific, RProcess().Id().Id());
       
   437 		}
       
   438 	test( found );
       
   439 	found = EFalse;
       
   440 
       
   441 	for(TInt retryCount = 0; retryCount < 10 && !found; retryCount++ )
       
   442 		{
       
   443 		//test getting the global list, ETrue as should find the target debug thread
       
   444 		User::After(50000);
       
   445 		found = DoTestGetThreadList(ETrue, EScopeGlobal);
       
   446 		}
       
   447 	test( found );
       
   448 
       
   449 	found = EFalse;
       
   450 	for(TInt retryCount = 0; retryCount < 10 && !found; retryCount++ )
       
   451 		{
       
   452 		//test getting this thread's thread list, ETrue as should find the target debug thread
       
   453 		User::After(50000);
       
   454 		found = DoTestGetThreadList(ETrue, EScopeThreadSpecific, RThread().Id().Id());
       
   455 		}
       
   456 	test( found );
       
   457 
       
   458 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
   459 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   460 	}
       
   461 			
       
   462 TBool CRunModeAgent::DoTestGetThreadList(const TBool aShouldPass, const TListScope aListScope, const TUint64 aTargetId)
       
   463 	{
       
   464 	//create data to pass
       
   465 	RBuf8 buffer;
       
   466 	TUint32 size = 0;
       
   467 
       
   468 	//perform the call to get the thread list
       
   469 	DoGetList(EThreads, aListScope, buffer, size, aTargetId);
       
   470 
       
   471 	//initialise data about the target debug thread to compare the kernel's data against
       
   472 	TFileName name = iDebugThread.FullName();
       
   473 	RProcess thisProcess;
       
   474 	TUint64 processId = thisProcess.Id();
       
   475 	TUint64 threadId = iDebugThread.Id();
       
   476 
       
   477 	//look through the buffer and check if the target debug thread is there
       
   478 	TBool found = EFalse;
       
   479 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
   480 	const TUint8* ptrEnd = ptr + size;
       
   481 	while(ptr < ptrEnd)
       
   482 		{
       
   483 		TThreadListEntry* entry = (TThreadListEntry*)ptr;
       
   484 		TPtr entryName(&(entry->iName[0]), entry->iNameLength, entry->iNameLength);
       
   485 
       
   486 		if( (threadId == entry->iThreadId) && (processId == entry->iProcessId) && (0 == name.CompareF(entryName)) )
       
   487 			{
       
   488 			test(entry->iSupervisorStackBaseValid);
       
   489 			test(entry->iSupervisorStackSizeValid);
       
   490 			//if all match then we've found it
       
   491 			found = ETrue;
       
   492 			break;
       
   493 			}
       
   494 
       
   495 		ptr += Align4(entry->GetSize());
       
   496 		}
       
   497 
       
   498 	//clean up
       
   499 	buffer.Close();
       
   500 	return found;
       
   501 
       
   502 	}
       
   503 
       
   504 //---------------------------------------------
       
   505 //! @SYMTestCaseID KBase-T-RMDEBUG2-0431
       
   506 //! @SYMTestType
       
   507 //! @SYMPREQ PREQ1426
       
   508 //! @SYMTestCaseDesc Test getting the code segment list
       
   509 //! @SYMTestActions Get the code segment list global and for a specified thread
       
   510 //! @SYMTestExpectedResults The listings should be returned successfully
       
   511 //! @SYMTestPriority High
       
   512 //! @SYMTestStatus Implemented
       
   513 //---------------------------------------------
       
   514 void CRunModeAgent::TestGetCodeSegsList()
       
   515 	{
       
   516 	test.Next(_L("TestGetCodeSegsList\n"));
       
   517 
       
   518 	test(ListingSupported(ECodeSegs, EScopeGlobal));
       
   519 	test(ListingSupported(ECodeSegs, EScopeProcessSpecific));
       
   520 	test(ListingSupported(ECodeSegs, EScopeThreadSpecific));
       
   521 
       
   522 	// Cannot perform this test with OEM2 debug token, as the t_rmdebug2 app
       
   523 	// needs AllFiles, and the OEM2 debug token does not authorise this.
       
   524 	// It seems reasonable to suppose that it would work anyway
       
   525 
       
   526 #ifndef FEWCAPS_DEBUGTOKEN
       
   527 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   528 
       
   529  	//test getting the global list, ETrue as should find this process' main codeSeg
       
   530 	DoTestGetCodeSegsList(ETrue, EScopeGlobal);
       
   531 
       
   532 	//test getting this process' codeSegs, ETrue as should find this process' main codeSeg
       
   533 	DoTestGetCodeSegsList(ETrue, EScopeProcessSpecific, RProcess().Id().Id());
       
   534 
       
   535 	//test getting this thread's codeSegs, ETrue as should find this process' main codeSeg
       
   536 	DoTestGetCodeSegsList(ETrue, EScopeThreadSpecific, RThread().Id().Id());
       
   537 
       
   538 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   539 #endif // FEWCAPS_DEBUGTOKEN
       
   540 
       
   541 	}
       
   542 
       
   543 void CRunModeAgent::DoTestGetCodeSegsList(const TBool aShouldPass, const TListScope aListScope, const TUint64 aTargetId)
       
   544 	{
       
   545 	//create data to pass
       
   546 	RBuf8 buffer;
       
   547 	TUint32 size = 0;
       
   548 
       
   549 	//perform the call to get the Code segs
       
   550 	DoGetList(ECodeSegs, aListScope, buffer, size, aTargetId);
       
   551 
       
   552 	//create memoryInfo to contain info about this process
       
   553 	RProcess thisProcess;
       
   554 	TModuleMemoryInfo memoryInfo;
       
   555 	test(KErrNone == thisProcess.GetMemoryInfo(memoryInfo));
       
   556 
       
   557 	// check whether this process came from a file in ROM so we know whether to
       
   558 	// expect the code seg to be XIP or not.
       
   559 	RFs fs;
       
   560 	test(KErrNone == fs.Connect());
       
   561 	TBool thisFileIsInRom = EFalse;
       
   562 	if(fs.IsFileInRom(iFileName))
       
   563 		{
       
   564 		thisFileIsInRom = ETrue;
       
   565 		}
       
   566 
       
   567 	//look through the buffer to find this process' main code seg
       
   568 	TBool found = EFalse;
       
   569 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
   570 	const TUint8* ptrEnd = ptr + size;
       
   571 	while(ptr < ptrEnd)
       
   572 		{
       
   573 		TCodeSegListEntry* codeSeg = (TCodeSegListEntry*)ptr;
       
   574 
       
   575 		if( (codeSeg->iIsXip == thisFileIsInRom) && (0 == iFileName.CompareF(TPtr(&(codeSeg->iName[0]), codeSeg->iNameLength, codeSeg->iNameLength))) )
       
   576 			{
       
   577 			if( (memoryInfo.iCodeBase == codeSeg->iCodeBase) &&
       
   578 					(memoryInfo.iCodeSize == codeSeg->iCodeSize) &&
       
   579 					(memoryInfo.iConstDataSize == codeSeg->iConstDataSize) &&
       
   580 					(memoryInfo.iInitialisedDataBase == codeSeg->iInitialisedDataBase) &&
       
   581 					(memoryInfo.iInitialisedDataSize == codeSeg->iInitialisedDataSize) &&
       
   582 					(memoryInfo.iUninitialisedDataSize == codeSeg->iUninitialisedDataSize))
       
   583 				{
       
   584 				//all matched so means we've found the codeSeg we're looking for
       
   585 				found = ETrue;
       
   586 				}
       
   587 			}
       
   588 		ptr += Align4(codeSeg->GetSize());
       
   589 		}
       
   590 
       
   591 	//check whether the result was as expected
       
   592 	test(found == aShouldPass);
       
   593 
       
   594 	// only care about rm_debug.ldd if we have global scope (belongs to the system not this process)
       
   595 	if (aListScope == EScopeGlobal)
       
   596 	{
       
   597 		// Search for rm_debug.ldd library and check its UID3 is correct
       
   598 		found = EFalse;
       
   599 
       
   600 _LIT(KRMDebugDriverFileName,"Z:\\sys\bin\\rm_debug.ldd");
       
   601 
       
   602 		TFileName rmdebugFilename(KRMDebugDriverFileName);
       
   603 
       
   604 		// reset the Ptr
       
   605 		ptr = (TUint8*)buffer.Ptr();
       
   606 		ptrEnd = ptr+size;
       
   607 		while(ptr < ptrEnd)
       
   608 		{
       
   609 			TCodeSegListEntry* codeSeg = (TCodeSegListEntry*)ptr;
       
   610 
       
   611 			if( rmdebugFilename.CompareF(TPtr(&(codeSeg->iName[0]), codeSeg->iNameLength, codeSeg->iNameLength)))
       
   612 				{
       
   613 				if(codeSeg->iUid3 == 0x101f7157 /* Magic */)
       
   614 					{
       
   615 					//all matched so means we've found the codeSeg we're looking for
       
   616 					found = ETrue;
       
   617 					}
       
   618 				}
       
   619 			ptr += Align4(codeSeg->GetSize());
       
   620 		}
       
   621 		test((TUint32)found == (TUint32)ETrue);
       
   622 	}
       
   623 
       
   624 	//clean up
       
   625 	buffer.Close();
       
   626 
       
   627 	}
       
   628 
       
   629 
       
   630 /**
       
   631  * Get a list from the run mode debug system. Most list calls will initially return KErrTooBig, 
       
   632  * since the initial size of the buffer is 0. However it is sometimes valid for a list to be empty
       
   633  * given its filtering and scope. These calls should return KErrNone.
       
   634  */
       
   635 void CRunModeAgent::DoGetList(const TListId aListId, const TListScope aListScope, RBuf8& aBuffer, TUint32& aSize, const TUint64 aTargetId)
       
   636 	{
       
   637 	//close the buffer in case there's stuff allocated in it
       
   638 	aBuffer.Close();
       
   639 	//initialise it to be one byte big, which will guarantee data won't fit in it
       
   640 	test(KErrNone == aBuffer.Create(1));
       
   641 	aSize = 0;
       
   642 	
       
   643 	TInt ret = KErrNone;
       
   644 	//should pass this test (assuming we've passed in sensible arguments above...)
       
   645 	if(EScopeGlobal == aListScope)
       
   646 		{
       
   647 		ret = iServSession.GetList(aListId, aBuffer, aSize);
       
   648 		}
       
   649 	else if(EScopeThreadSpecific == aListScope)
       
   650 		{
       
   651 		ret = iServSession.GetList((TThreadId)aTargetId, aListId, aBuffer, aSize);
       
   652 		}
       
   653 	else if(EScopeProcessSpecific == aListScope)
       
   654 		{
       
   655 		ret = iServSession.GetList((TProcessId)aTargetId, aListId, aBuffer, aSize);
       
   656 		}
       
   657 	else
       
   658 		{
       
   659 		// unknown list scope
       
   660 		test(0);
       
   661 		}
       
   662 
       
   663 	if( KErrNone == ret )
       
   664 		{
       
   665 		/* In the case that there is no data, just return and let the caller check
       
   666 		the buffer. It is valid for a caller to not expect any data to be returned.
       
   667 		*/
       
   668 		return;
       
   669 		}
       
   670 	
       
   671 	// The only other allowed return is KErrTooBig
       
   672 	test( ret == KErrTooBig );
       
   673 
       
   674 	//keep allocating larger buffers, beginning with the aSize returned by the above call,
       
   675 	//and hopefully we'll eventually make a large enough one
       
   676 	test(KErrNone == aBuffer.ReAlloc(aSize));
       
   677 
       
   678 	for(;;)
       
   679 		{
       
   680 		TInt err = KErrNone;
       
   681 		if(EScopeGlobal == aListScope)
       
   682 			{
       
   683 			err = iServSession.GetList(aListId, aBuffer, aSize);
       
   684 			}
       
   685 		else if(EScopeThreadSpecific == aListScope)
       
   686 			{
       
   687 			err = iServSession.GetList((TThreadId)aTargetId, aListId, aBuffer, aSize);
       
   688 			}
       
   689 		else if(EScopeProcessSpecific == aListScope)
       
   690 			{
       
   691 			err = iServSession.GetList((TProcessId)aTargetId, aListId, aBuffer, aSize);
       
   692 			}
       
   693 		else
       
   694 			{
       
   695 			// unknown list scope
       
   696 			test(0);
       
   697 			}
       
   698 		if(err == KErrTooBig)
       
   699 			{
       
   700 			//wasn't big enough so double it
       
   701 			aSize = aSize << 1;
       
   702 			err = aBuffer.ReAlloc(aSize);
       
   703 			if(err != KErrNone)
       
   704 				{
       
   705 				//print out a message if couldn't allocate memory and quit
       
   706 				test.Printf(_L("Out ot memory when attempting to allocate %d bytes."), aSize);
       
   707 				test(KErrNone == err);
       
   708 				}
       
   709 
       
   710 			RDebug::Printf(" List size =%d", aSize );
       
   711 			}
       
   712 		else
       
   713 			{
       
   714 			test(KErrNone == err);
       
   715 			test(aBuffer.Length() == aSize);
       
   716 			//break out of the loop if the list has been successfully read in
       
   717 			break;
       
   718 			}
       
   719 		}
       
   720 	}
       
   721 
       
   722 //---------------------------------------------
       
   723 //! @SYMTestCaseID KBase-T-RMDEBUG2-0432
       
   724 //! @SYMTestType
       
   725 //! @SYMPREQ PREQ1426
       
   726 //! @SYMTestCaseDesc Test reading and writing memory
       
   727 //! @SYMTestActions Multiple calls to read and write memory, with various sizes and at various locations.
       
   728 //!	Also test that bad input values cause appropriate errors to be returned.
       
   729 //! @SYMTestExpectedResults All tests should pass and the target process should be left unaffected
       
   730 //! @SYMTestPriority High
       
   731 //! @SYMTestStatus Implemented
       
   732 //---------------------------------------------
       
   733 void CRunModeAgent::TestMemoryAccess()
       
   734 {
       
   735 	TInt err;
       
   736 
       
   737 	test.Next(_L("TestMemoryAccess - Read Memory\n"));
       
   738 
       
   739 	//initialise buffer
       
   740 	gMemoryAccessBytes.SetLength(0);
       
   741 	for (TInt i=0; i<SYMBIAN_RMDBG_MEMORYSIZE; i++)
       
   742 		{
       
   743 		gMemoryAccessBytes.Append(i);
       
   744 		}
       
   745 
       
   746 	TUint32 address = (TUint32)(&gMemoryAccessBytes[0]);
       
   747 	TUint32 dataSize = SYMBIAN_RMDBG_MEMORYSIZE;
       
   748 
       
   749 	//create size for buffer that is rounded up to nearest 4 bytes if not
       
   750 	//already 4 byte aligned
       
   751 	TUint32 size = dataSize;
       
   752 	if(size % 4 != 0)
       
   753 		{
       
   754 		size += (4 - (size % 4));
       
   755 		}
       
   756 
       
   757 	RBuf8 dataBlock;
       
   758 	err = dataBlock.Create(size);
       
   759 	test(err==KErrNone);
       
   760 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   761 
       
   762 	//suspend the thread prior to memory operations
       
   763 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
   764 
       
   765 	err = iServSession.ReadMemory(iThreadID, address, size, dataBlock, EAccess32, EEndLE8);
       
   766 	test(err==KErrNone);
       
   767 
       
   768 	for (TInt i=0; i<dataSize; i++)
       
   769 		{
       
   770 		test(dataBlock.Ptr()[i] == gMemoryAccessBytes[i]);
       
   771 		}
       
   772 
       
   773 	test.Next(_L("TestMemoryAccess - Write Memory\n"));
       
   774 
       
   775 	// Now reset the buffer
       
   776 	for (TInt i=0; i<dataSize; i++)
       
   777 		{
       
   778 		gMemoryAccessBytes[i] = 0;
       
   779 		}
       
   780 
       
   781 	// Write our data into the buffer
       
   782 	err = iServSession.WriteMemory(iThreadID, address, size, dataBlock, EAccess32, EEndLE8);
       
   783 	test(err==KErrNone);
       
   784 
       
   785 	for (TInt i=0; i<dataSize; i++)
       
   786 		{
       
   787 		test(dataBlock.Ptr()[i] == gMemoryAccessBytes[i]);
       
   788 		}
       
   789 
       
   790 	//final test that everything's not been going wrong
       
   791 	test(gMemoryAccessBytes[5] != 0);
       
   792 
       
   793 	test.Next(_L("TestMemoryAccess - Invalid arguments\n"));
       
   794 	test.Printf(_L("This test may emit crash-like information. This is intended.\n"));
       
   795 
       
   796 	//test address that is not 32 bit aligned
       
   797 	err = iServSession.ReadMemory(iThreadID, address + 1, size, dataBlock, EAccess32, EEndLE8);
       
   798 	test(err == KErrArgument);
       
   799 
       
   800 	//test size that is not multiple of 4 bytes
       
   801 	err = iServSession.WriteMemory(iThreadID, address, size + 2, dataBlock, EAccess32, EEndLE8);
       
   802 	test(err == KErrArgument);
       
   803 
       
   804 	//test size > max block size
       
   805 	err = iServSession.ReadMemory(iThreadID, address, (1<<15), dataBlock, EAccess32, EEndLE8);
       
   806 	test(err == KErrArgument);
       
   807 
       
   808 	//test access size == 2 bytes
       
   809 	err = iServSession.ReadMemory(iThreadID, address, size, dataBlock, EAccess16, EEndLE8);
       
   810 	test(err == KErrNotSupported);
       
   811 
       
   812 	//test access size == 1 byte
       
   813 	err = iServSession.WriteMemory(iThreadID, address, size, dataBlock, EAccess8, EEndLE8);
       
   814 	test(err == KErrNotSupported);
       
   815 
       
   816 	//test endianess == EEndBE8
       
   817 	err = iServSession.ReadMemory(iThreadID, address, size, dataBlock, EAccess32, EEndBE8);
       
   818 	test(err == KErrNotSupported);
       
   819 
       
   820 	//test endianess == EEndBE32
       
   821 	err = iServSession.WriteMemory(iThreadID, address, size, dataBlock, EAccess32, EEndBE32);
       
   822 	test(err == KErrNotSupported);
       
   823 
       
   824 	//test reading off end of memory
       
   825 	err = iServSession.ReadMemory(iThreadID, 0xffffff00, 0x00000101, dataBlock, EAccess32, EEndLE8);
       
   826 	test(err == KErrArgument);
       
   827 
       
   828 	//The following three tests check that edge conditions in the range check are handled correctly.
       
   829 	err = iServSession.ReadMemory(iThreadID, 0xffffff00, 0x000000FF, dataBlock, EAccess32, EEndLE8);
       
   830 	test(err == KErrArgument);
       
   831 
       
   832 	err = iServSession.ReadMemory(iThreadID, 0xffffff00, 0x000000F0, dataBlock, EAccess32, EEndLE8);
       
   833 	test(err == KErrBadDescriptor);
       
   834 
       
   835 	//Third range check test. Check that range check is handled correctly even when base + size wraps to 0.
       
   836 	err = iServSession.ReadMemory(iThreadID, 0xffffff00, 0x00000100, dataBlock, EAccess32, EEndLE8);
       
   837 	test(err == KErrBadDescriptor);
       
   838 	//end of range check tests
       
   839 
       
   840 	//test size == 0
       
   841 	err = iServSession.WriteMemory(iThreadID, address, 0, dataBlock, EAccess32, EEndLE8);
       
   842 	test(err == KErrArgument);
       
   843 
       
   844 	//attempt to write to address outside of process data segments,
       
   845 	//this address corresponds to the vectors so shouldn't be able to write
       
   846 	err = iServSession.WriteMemory(iThreadID, 0xffff0000, size, dataBlock, EAccess32, EEndLE8);
       
   847 	test(err == KErrBadDescriptor);
       
   848 
       
   849 	//attempt to read and write to address in process code segment
       
   850 
       
   851 	//open a handle to the thread
       
   852 	RThread debugThread;
       
   853 	test(debugThread.Open(iThreadID) == KErrNone);
       
   854 
       
   855 	//get a reference to the debug process
       
   856 	RProcess debugProcess;
       
   857 	test(debugThread.Process(debugProcess) == KErrNone);
       
   858 
       
   859 	//get the memory info for the process
       
   860 	TProcessMemoryInfo info;
       
   861 	test(debugProcess.GetMemoryInfo(info) == KErrNone);
       
   862 
       
   863 	address = info.iCodeBase;
       
   864 	if(size <= info.iCodeSize)
       
   865 		{
       
   866 		test(KErrNone == iServSession.ReadMemory(iThreadID, address, size, dataBlock, EAccess32, EEndLE8));
       
   867 		test(KErrBadDescriptor == iServSession.WriteMemory(iThreadID, address, size, dataBlock, EAccess32, EEndLE8));
       
   868 		}
       
   869 
       
   870 	// Some performance tests now
       
   871 	TUint32 bytesRead = 0;
       
   872 
       
   873 	// Allocate a data buffer
       
   874 	TUint32* p = (TUint32*)User::Alloc(size);
       
   875 	test(p != 0);
       
   876 
       
   877 	TInt nanokernel_tick_period;
       
   878 	HAL::Get(HAL::ENanoTickPeriod, nanokernel_tick_period);
       
   879 	test (nanokernel_tick_period != 0);
       
   880 
       
   881 	static const TInt KOneMillion = 1000000;
       
   882 
       
   883 	TInt nkTicksPerSecond = KOneMillion/nanokernel_tick_period;
       
   884 
       
   885 	TUint32 stopTickCount = User::NTickCount() + nkTicksPerSecond;
       
   886 
       
   887 	while (User::NTickCount() < stopTickCount)
       
   888 		{
       
   889 		err = iServSession.ReadMemory(iThreadID, (TUint32)p, size, dataBlock, EAccess32, EEndLE8);
       
   890 		test(err==KErrNone);
       
   891 
       
   892 		// Increase the count of bytes read
       
   893 		bytesRead += size;
       
   894 		}
       
   895 
       
   896 	test(bytesRead != 0);
       
   897 	iMemoryReadKbytesPerSecond = bytesRead/1024;
       
   898 
       
   899 	// write memory test
       
   900 	TUint32 bytesWritten = 0;
       
   901 
       
   902 	stopTickCount = User::NTickCount() + nkTicksPerSecond;
       
   903 
       
   904 	while (User::NTickCount() < stopTickCount)
       
   905 		{
       
   906 		err = iServSession.WriteMemory(iThreadID, (TUint32)p, size, dataBlock, EAccess32, EEndLE8);
       
   907 		test(err==KErrNone);
       
   908 
       
   909 		// Increase the count of bytes read
       
   910 		bytesWritten += size;
       
   911 		}
       
   912 
       
   913 	test (bytesWritten != 0);
       
   914 	iMemoryWriteKbytesPerSecond = bytesWritten/1024;
       
   915 
       
   916 	User::Free(p);
       
   917 
       
   918 	//resume the thread
       
   919 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
   920 
       
   921 	debugThread.Close();
       
   922 	dataBlock.Close();
       
   923 
       
   924 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   925 	}
       
   926 
       
   927 //---------------------------------------------
       
   928 //! @SYMTestCaseID KBase-T-RMDEBUG2-0433
       
   929 //! @SYMTestType
       
   930 //! @SYMPREQ PREQ1426
       
   931 //! @SYMTestCaseDesc Test suspending and resuming threads
       
   932 //! @SYMTestActions Multiple calls to suspend and resume threads with and without attaching to the thread
       
   933 //! @SYMTestExpectedResults All tests should pass and the target process should be left unaffected
       
   934 //! @SYMTestPriority High
       
   935 //! @SYMTestStatus Implemented
       
   936 //---------------------------------------------
       
   937 void CRunModeAgent::TestSuspendResume()
       
   938 	{
       
   939 	TInt err;
       
   940 
       
   941 	test.Next(_L("TestSuspendResume - Suspend\n"));
       
   942 
       
   943 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   944 
       
   945 	err = iServSession.SuspendThread(iThreadID);
       
   946 	test(err==KErrNone);
       
   947 	err = TestRunCountSame( iRunCountSubscribe, iTimer );
       
   948 	test( KErrNone == err );
       
   949 	
       
   950 	// Resume the thread
       
   951 	test.Next(_L("TestSuspendResume - Resume\n"));
       
   952 	err = iServSession.ResumeThread(iThreadID);
       
   953 	test(err==KErrNone);
       
   954 
       
   955 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   956 
       
   957 	err = WaitForRunCountChange( iRunCountSubscribe, iTimer );
       
   958 	test(KErrNone == err );
       
   959 	
       
   960 	// check that agent can resume thread which it previously detached from
       
   961 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   962 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
   963 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   964 
       
   965 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   966 	err = TestRunCountSame( iRunCountSubscribe, iTimer );
       
   967 	test( KErrNone == err );
       
   968 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
   969 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   970 
       
   971 	err = WaitForRunCountChange( iRunCountSubscribe, iTimer );
       
   972 	test( KErrNone == err );
       
   973 	
       
   974 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   975 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
   976 	err = TestRunCountSame( iRunCountSubscribe, iTimer );
       
   977 	test( KErrNone == err );
       
   978 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   979 	err = TestRunCountSame( iRunCountSubscribe, iTimer );
       
   980 	test( KErrNone == err );
       
   981 	    
       
   982 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
   983 	err = TestRunCountSame( iRunCountSubscribe, iTimer );
       
   984 	test( KErrNone == err );
       
   985 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
   986 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
   987 	
       
   988 	err = WaitForRunCountChange( iRunCountSubscribe, iTimer );
       
   989     test( KErrNone == err );
       
   990 	}
       
   991 
       
   992 //---------------------------------------------
       
   993 //! @SYMTestCaseID KBase-T-RMDEBUG2-0434
       
   994 //! @SYMTestType
       
   995 //! @SYMPREQ PREQ1426
       
   996 //! @SYMTestCaseDesc Test getting the debug functionality from the driver
       
   997 //! @SYMTestActions Get the size and contents of the debug functionality block
       
   998 //! @SYMTestExpectedResults All tests should pass and the expected data should appear in the functionality block
       
   999 //! @SYMTestPriority High
       
  1000 //! @SYMTestStatus Implemented
       
  1001 //---------------------------------------------
       
  1002 void CRunModeAgent::TestDebugFunctionality()
       
  1003 	{
       
  1004 
       
  1005 	TInt err;
       
  1006 
       
  1007 	test.Next(_L("TestDebugFunctionality - GetDebugFunctionalityBufSize\n"));
       
  1008 
       
  1009 	TUint32 bufsize = 0;	// Safe default size
       
  1010 
       
  1011 	// Get functionality block size
       
  1012 	err = iServSession.GetDebugFunctionalityBufSize(&bufsize);
       
  1013 	test(err==KErrNone);
       
  1014 	test.Next(_L("TestDebugFunctionality - GetDebugFunctionality\n"));
       
  1015 
       
  1016 	// Ensure we have a finite buffer size
       
  1017 	test(bufsize!=0);
       
  1018 
       
  1019 	// Allocate space for the functionality data
       
  1020 	HBufC8* dftext = HBufC8::NewLC(bufsize);
       
  1021 
       
  1022 	// create an empty TPtr8 refering to dftext
       
  1023 	TPtr8 dftextPtr(dftext->Des());
       
  1024 
       
  1025 	// Get the functionality block
       
  1026 	err = iServSession.GetDebugFunctionality(dftextPtr);
       
  1027 	test(err==KErrNone);
       
  1028 
       
  1029 	// Check that the first entry is correct
       
  1030 	TTagHeader RefHdr =
       
  1031 	{
       
  1032 		ETagHeaderIdCore,ECoreLast,
       
  1033 	};
       
  1034 
       
  1035 	// First header passed from rm_debug.ldd
       
  1036 	TTagHeader* TestHdr = (TTagHeader*)dftextPtr.Ptr();
       
  1037 
       
  1038 	// Check
       
  1039 	test(RefHdr.iTagHdrId==TestHdr->iTagHdrId);
       
  1040 	// this test might fail if the agent is used with a Debug Security Server different from
       
  1041 	// the one it was compiled against. So removing it for now.
       
  1042 	//test(RefHdr.iNumTags==TestHdr->iNumTags);
       
  1043 
       
  1044 	// read a value from the data to check it has come through as expected
       
  1045 	TTagHeader* header = GetTagHdr(dftext->Des(), ETagHeaderIdApiConstants);
       
  1046 	test(header != NULL);
       
  1047 	TTag* tag = GetTag(header, EApiConstantsTEventInfoSize);
       
  1048 	test(tag != NULL);
       
  1049 	// this test might fail if the agent is used with a Debug Security Server different from
       
  1050 	// the one it was compiled against. So removing it for now.
       
  1051 	//test(sizeof(TEventInfo) == tag->iValue);
       
  1052 
       
  1053 	// Remove our temporary buffer
       
  1054 	CleanupStack::PopAndDestroy(dftext);
       
  1055 	}
       
  1056 
       
  1057 //---------------------------------------------
       
  1058 //! @SYMTestCaseID KBase-T-RMDEBUG2-0435
       
  1059 //! @SYMTestType
       
  1060 //! @SYMPREQ PREQ1426
       
  1061 //! @SYMTestCaseDesc Test setting and clearing consecutive breakpoints
       
  1062 //! @SYMTestActions Set and clear consecutive breakpoints of all combinations of breakpoint types
       
  1063 //! @SYMTestExpectedResults All breakpoints should be set and cleared without error
       
  1064 //! @SYMTestPriority High
       
  1065 //! @SYMTestStatus Implemented
       
  1066 //---------------------------------------------
       
  1067 void CRunModeAgent::TestConsecutiveBreakPoints()
       
  1068 	{
       
  1069 	test.Next(_L("TestConsecutiveBreakPoints\n"));
       
  1070 
       
  1071 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  1072 
       
  1073 	// just a temporary structure for storing info about a breakpoint
       
  1074 	struct TBreakPoint
       
  1075 		{
       
  1076 	public:
       
  1077 		TBreakPoint()
       
  1078 			:iId(0),
       
  1079 			iMode((TArchitectureMode)0),
       
  1080 			iAddress(0)
       
  1081 			{}
       
  1082 		TBreakId iId;
       
  1083 		TArchitectureMode iMode;
       
  1084 		TUint32 iAddress;
       
  1085 		inline TInt Size() { return (EArmMode == iMode) ? 4 : 2; }
       
  1086 		};
       
  1087 
       
  1088 	//an address in the target debug thread
       
  1089 	TUint32 address = (TUint32)(&TestFunction);
       
  1090 
       
  1091 	// there are six orders in which three breakpoints can be set, these are looped
       
  1092 	// through below to check setting and clearing consecutive breakpoints works
       
  1093 	TUint8 order[6][3] =
       
  1094 		{
       
  1095 			{0,1,2},
       
  1096 			{0,2,1},
       
  1097 			{1,0,2},
       
  1098 			{1,2,0},
       
  1099 			{2,0,1},
       
  1100 			{2,1,0}
       
  1101 		};
       
  1102 
       
  1103 	// The following code checks that setting and clearing consecutive breakpoints works correctly:
       
  1104 	// It checks that setting all combinations of three arm and thumb breakpoints succeeds, and check that the
       
  1105 	// breakpoints can be set in any order, and then cleared in any order
       
  1106 
       
  1107 	// the 3 least significant bits of i control whether each of the three breakpoints should be arm or thumb
       
  1108 	for(TInt i=0; i<8; i++)
       
  1109 		{
       
  1110 		// controls the order in which the breakpoints should be set
       
  1111 		for(TInt j=0; j<6; j++)
       
  1112 			{
       
  1113 			// create the three breakpoints and set their modes
       
  1114 			TBreakPoint bp[3];
       
  1115 			bp[0].iMode = (i&1) ? EArmMode : EThumbMode;
       
  1116 			bp[1].iMode = (i&2) ? EArmMode : EThumbMode;
       
  1117 			bp[2].iMode = (i&4) ? EArmMode : EThumbMode;
       
  1118 
       
  1119 			// set the address of each of the breakpoints
       
  1120 			bp[0].iAddress = address;
       
  1121 			if(EArmMode == bp[0].iMode)
       
  1122 				{ // if an arm breakpoint then must be on a four byte boundary
       
  1123 				bp[0].iAddress = Align4(bp[0].iAddress);
       
  1124 				}
       
  1125 			bp[1].iAddress = bp[0].iAddress + bp[0].Size();
       
  1126 			if(EArmMode == bp[1].iMode)
       
  1127 				{ // if an arm breakpoint then must be on a four byte boundary
       
  1128 				bp[1].iAddress = Align4(bp[1].iAddress);
       
  1129 				}
       
  1130 			bp[2].iAddress = bp[1].iAddress + bp[1].Size();
       
  1131 			if(EArmMode == bp[2].iMode)
       
  1132 				{ // if an arm breakpoint then must be on a four byte boundary
       
  1133 				bp[2].iAddress = Align4(bp[2].iAddress);
       
  1134 				}
       
  1135 			for(TInt k=0; k<6; k++)
       
  1136 				{
       
  1137 				// set the three breakpoints in the order defined by j and then clear them in the order defined by k
       
  1138 				test(KErrNone==iServSession.SetBreak(bp[order[j][0]].iId, iThreadID, bp[order[j][0]].iAddress, bp[order[j][0]].iMode));
       
  1139 				test(KErrNone==iServSession.SetBreak(bp[order[j][1]].iId, iThreadID, bp[order[j][1]].iAddress, bp[order[j][1]].iMode));
       
  1140 				test(KErrNone==iServSession.SetBreak(bp[order[j][2]].iId, iThreadID, bp[order[j][2]].iAddress, bp[order[j][2]].iMode));
       
  1141 				test(KErrNone==iServSession.ClearBreak(bp[order[k][0]].iId));
       
  1142 				test(KErrNone==iServSession.ClearBreak(bp[order[k][1]].iId));
       
  1143 				test(KErrNone==iServSession.ClearBreak(bp[order[k][2]].iId));
       
  1144 				}
       
  1145 			}
       
  1146 		}
       
  1147 
       
  1148 	// resume the thread
       
  1149 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  1150 	}
       
  1151 
       
  1152 //---------------------------------------------
       
  1153 //! @SYMTestCaseID KBase-T-RMDEBUG2-0436
       
  1154 //! @SYMTestType
       
  1155 //! @SYMPREQ PREQ1426
       
  1156 //! @SYMTestCaseDesc Test breakpoint functionality
       
  1157 //! @SYMTestActions Multiple calls to set and clear breakpoints. Checking bad input produces appropriate errors.
       
  1158 //! @SYMTestExpectedResults All tests should pass and the target debug thread should be left unaffected
       
  1159 //! @SYMTestPriority High
       
  1160 //! @SYMTestStatus Implemented
       
  1161 //---------------------------------------------
       
  1162 void CRunModeAgent::TestBreakPoints()
       
  1163 	{
       
  1164 	TInt err;
       
  1165 
       
  1166 	test.Next(_L("TestBreakPoints - Set\n"));
       
  1167 
       
  1168 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1169 
       
  1170 	TestConsecutiveBreakPoints();
       
  1171 
       
  1172 	//an address in the target debug thread
       
  1173 	TUint32 address = (TUint32)(&TestFunction);
       
  1174 
       
  1175 	/*
       
  1176 	 * Ensure that breakpoint operations don't
       
  1177 	 * affect memory read/write by checking that reads/writes
       
  1178 	 * in locations containing breakpoints don't change behaviour
       
  1179 	 * because of the breakpoints.
       
  1180 	 */
       
  1181 
       
  1182 	TUint32 size = SYMBIAN_RMDBG_MEMORYSIZE;
       
  1183 
       
  1184 	RBuf8 originalDataBlock;
       
  1185 	err = originalDataBlock.Create(size);
       
  1186 	test(err==KErrNone);
       
  1187 
       
  1188 	//suspend the thread
       
  1189 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  1190 
       
  1191 	err = iServSession.ReadMemory(iThreadID, address, size, originalDataBlock, EAccess32, EEndLE8);
       
  1192 	test(err==KErrNone);
       
  1193 
       
  1194 	// Test data block for comparison
       
  1195 	RBuf8 testDataBlock;
       
  1196 	err = testDataBlock.Create(size);
       
  1197 	test(err==KErrNone);
       
  1198 
       
  1199 	/*
       
  1200 	 * set an arm breakpoint
       
  1201 	 */
       
  1202 	test.Next(_L("TestBreakPoints - set an arm breakpoint1"));
       
  1203 
       
  1204 	TBreakId armBreakId = 0;
       
  1205 	err = iServSession.SetBreak(armBreakId, iThreadID, address, EArmMode);
       
  1206 	test(err == KErrNone);
       
  1207 
       
  1208 	// Ensure that memory read is not corrupted
       
  1209 	test.Next(_L("TestBreakPoints - read mem 2"));
       
  1210 	err = iServSession.ReadMemory(iThreadID, address, size, testDataBlock, EAccess32, EEndLE8);
       
  1211 	test(err==KErrNone);
       
  1212 
       
  1213 	test (testDataBlock == originalDataBlock);
       
  1214 
       
  1215 	/*
       
  1216 	 * set a thumb breakpoint
       
  1217 	 */
       
  1218 	test.Next(_L("TestBreak- set a thumb breakpoint1"));
       
  1219 	TBreakId thumbBreakId = 0;
       
  1220 	err = iServSession.SetBreak(thumbBreakId, iThreadID, address+4, EThumbMode);
       
  1221 	test(err == KErrNone);
       
  1222 
       
  1223 	/*
       
  1224 	 * set a thumb2EE breakpoint
       
  1225 	 */
       
  1226 	test.Next(_L("TestBreak- set a thumb2EE breakpoint"));
       
  1227 
       
  1228 	TBreakId thumb2EEBreakId = 0;
       
  1229 	err = iServSession.SetBreak(thumb2EEBreakId, iThreadID, address+8, EThumb2EEMode);
       
  1230 	test(err == KErrNotSupported);
       
  1231 
       
  1232 	/*
       
  1233 	 * overlapping breakpoint (same address/threadId/mode)
       
  1234 	 */
       
  1235 	test.Next(_L("TestBreak- set overlapping breakpoint 1"));
       
  1236 	TBreakId overlapBreakId = 0;
       
  1237 	err = iServSession.SetBreak(overlapBreakId, iThreadID, address, EArmMode);
       
  1238 	test(err == KErrAlreadyExists);
       
  1239 
       
  1240 	/*
       
  1241 	 * overlapping breakpoint (different address/same threadId/different mode)
       
  1242 	 *
       
  1243 	 * address - EArmBreakpoint
       
  1244 	 * address+2 - EThumbBreakpoint
       
  1245 	 */
       
  1246 	test.Next(_L("TestBreak- set overlapping breakpoint 2"));
       
  1247 	TBreakId overlap2BreakId = 0;
       
  1248 	err = iServSession.SetBreak(overlap2BreakId, iThreadID, address+2, EThumbMode);
       
  1249 	test(err == KErrAlreadyExists);
       
  1250 
       
  1251 	/*
       
  1252 	 * Un-aligned address (arm)
       
  1253 	 */
       
  1254 	test.Next(_L("TestBreak- set Un-aligned address (arm)"));
       
  1255 	TBreakId armUnalignedBreakId = 0;
       
  1256 	err = iServSession.SetBreak(armUnalignedBreakId, iThreadID, address+6, EArmMode);
       
  1257 	test(err == KErrArgument);
       
  1258 
       
  1259 	/*
       
  1260 	 * Un-aligned address (thumb)
       
  1261 	 */
       
  1262 	test.Next(_L("TestBreak- set Un-aligned address (thumb)"));
       
  1263 	TBreakId thumbUnalignedBreakId = 0;
       
  1264 	err = iServSession.SetBreak(thumbUnalignedBreakId, iThreadID, address+7, EThumbMode);
       
  1265 	test(err == KErrArgument);
       
  1266 
       
  1267 	/*
       
  1268 	 * Invalid address (arm)
       
  1269 	 */
       
  1270 	test.Next(_L("TestBreak- set Invalid address (arm)"));
       
  1271 	TBreakId armBadAddressBreakId = 0;
       
  1272 	err = iServSession.SetBreak(armBadAddressBreakId, iThreadID, 0 /* address */, EThumbMode);
       
  1273 	test(err == KErrBadDescriptor);
       
  1274 
       
  1275 	/*
       
  1276 	 * Different thread, same address. Should fail for the same process, but succeed
       
  1277 	 * for a different process.
       
  1278 	 */
       
  1279 
       
  1280 	/*
       
  1281 	 * Invalid thread
       
  1282 	 */
       
  1283 	TBreakId invalidThreadBreakId = 0;
       
  1284 	err = iServSession.SetBreak(invalidThreadBreakId, 0xbabababa, address, EThumbMode);
       
  1285 	test(err == KErrPermissionDenied);
       
  1286 
       
  1287 	// Clear the ARM breakpoint
       
  1288 	err = iServSession.ClearBreak(armBreakId);
       
  1289 	test(err == KErrNone);
       
  1290 
       
  1291 	// Clear the Thumb breakpoint
       
  1292 	err = iServSession.ClearBreak(thumbBreakId);
       
  1293 	test(err == KErrNone);
       
  1294 
       
  1295 	// to do : two threads at the same address
       
  1296 	// to do : two processes at the same address
       
  1297 
       
  1298 	// Ensure that memory read is not corrupted after clearing the breakpoints
       
  1299 	err = iServSession.ReadMemory(iThreadID, address, size, testDataBlock, EAccess32, EEndLE8);
       
  1300 	test(err==KErrNone);
       
  1301 
       
  1302 	test (testDataBlock == originalDataBlock);
       
  1303 
       
  1304 	/*
       
  1305 	 * How fast can we set breakpoints?
       
  1306 	 *
       
  1307 	 * Measure the time by setting/clearing breakpoints for 1 second.
       
  1308      */
       
  1309 	TInt nanokernel_tick_period;
       
  1310 	HAL::Get(HAL::ENanoTickPeriod, nanokernel_tick_period);
       
  1311 	test (nanokernel_tick_period != 0);
       
  1312 
       
  1313 	TInt nkTicksPerSecond = HelpTicksPerSecond();
       
  1314 
       
  1315 	TInt breaksPerSecond = 0;
       
  1316 
       
  1317 	TUint32 stopTickCount = User::NTickCount() + nkTicksPerSecond;
       
  1318 
       
  1319 	while (User::NTickCount() < stopTickCount)
       
  1320 		{
       
  1321 		// set the breakpoint
       
  1322 		TBreakId armBreakId = 0;
       
  1323 		err = iServSession.SetBreak(armBreakId, iThreadID, address, EArmMode);
       
  1324 		test(err == KErrNone);
       
  1325 
       
  1326 		// Clear the breakpoint
       
  1327 		err = iServSession.ClearBreak(armBreakId);
       
  1328 		test(err == KErrNone);
       
  1329 
       
  1330 		// Update the count of breakpoints
       
  1331 		breaksPerSecond++;
       
  1332 
       
  1333 		// Gone wrong if we wrap to negative breakpoints (cannot set 2billion/second!)
       
  1334 		test(breaksPerSecond >0);
       
  1335 		}
       
  1336 
       
  1337 	// Store the results for later
       
  1338 	iBreakpointsPerSecond = breaksPerSecond;
       
  1339 
       
  1340 	/*
       
  1341 	 * How many breakpoints can we set?
       
  1342 	 */
       
  1343 
       
  1344 	TBool done = EFalse;
       
  1345 
       
  1346 	// We assume all the breakpoints id's are issued in ascending order
       
  1347 	TInt maxBreakPoints = 0;
       
  1348 
       
  1349 	// Temporary buffer
       
  1350 	RArray<TBreakId> breakIdList;
       
  1351 
       
  1352 	TUint32 testAddress = address;
       
  1353 
       
  1354 	while(!done)
       
  1355 		{
       
  1356 		TBreakId breakId = 0;
       
  1357 
       
  1358 		// set the breakpoint
       
  1359 		testAddress += 4;	// ensure the addresses don't overlap
       
  1360 
       
  1361 		err = iServSession.SetBreak(breakId, iThreadID, testAddress, EArmMode);
       
  1362 		test (err == KErrNone || err == KErrOverflow);
       
  1363 		if (err != KErrNone)
       
  1364 			{
       
  1365 			// we've reached the limit of the number of breaks we can set
       
  1366 			done = ETrue;
       
  1367 			break;
       
  1368 			}
       
  1369 
       
  1370 		// store the id of this breakpoint
       
  1371 		breakIdList.Append(breakId);
       
  1372 
       
  1373 		// Increase the count of breakpoints
       
  1374 		maxBreakPoints++;
       
  1375 		test(maxBreakPoints > 0);
       
  1376 		}
       
  1377 
       
  1378 	// How many breakpoints can we set?
       
  1379 	iMaxBreakpoints = maxBreakPoints;
       
  1380 
       
  1381 	// now clear all those breakpoints again
       
  1382 	while(breakIdList.Count() != 0)
       
  1383 		{
       
  1384 		// Place it into a TBreakId
       
  1385 		TBreakId id = breakIdList[0];
       
  1386 
       
  1387 		err = iServSession.ClearBreak(id);
       
  1388 		test(err == KErrNone);
       
  1389 
       
  1390 		// next id
       
  1391 		breakIdList.Remove(0);
       
  1392 		}
       
  1393 
       
  1394 	breakIdList.Close();
       
  1395 
       
  1396 	// close our temporary buffers
       
  1397 	originalDataBlock.Close();
       
  1398 	testDataBlock.Close();
       
  1399 
       
  1400 	err = iServSession.ResumeThread(iThreadID);
       
  1401 	test (err == KErrNone);
       
  1402 
       
  1403 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  1404 	}
       
  1405 
       
  1406 //---------------------------------------------
       
  1407 //! @SYMTestCaseID KBase-T-RMDEBUG2-0437
       
  1408 //! @SYMTestType
       
  1409 //! @SYMPREQ PREQ1426
       
  1410 //! @SYMTestCaseDesc Test modifying breakpoints
       
  1411 //! @SYMTestActions Several calls to modify breakpoints
       
  1412 //! @SYMTestExpectedResults Valid requests should result in the breakpoints being changed, invalid requests should return errors
       
  1413 //! @SYMTestPriority High
       
  1414 //! @SYMTestStatus Implemented
       
  1415 //---------------------------------------------
       
  1416 void CRunModeAgent::TestModifyBreak()
       
  1417 	{
       
  1418 	test.Next(_L("TestModifyBreak\n"));
       
  1419 
       
  1420 	DoTestModifyBreak(ETrue);
       
  1421 	DoTestModifyBreak(EFalse);
       
  1422 	}
       
  1423 
       
  1424 void CRunModeAgent::DoTestModifyBreak(TBool aThreadSpecific)
       
  1425 	{
       
  1426 	test.Printf(_L("DoTestModifyBreak: aThreadSpecific: %d\n"), aThreadSpecific?1:0);
       
  1427 
       
  1428 	TInt err;
       
  1429 
       
  1430 	RProcess process;
       
  1431 	TProcessId processId = process.Id();
       
  1432 	process.Close();
       
  1433 
       
  1434 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1435 
       
  1436 	//suspend the thread
       
  1437 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  1438 
       
  1439 	//an address in the target debug thread
       
  1440 	TUint32 address = (TUint32)(&TestFunction);
       
  1441 
       
  1442 	//set an arm mode break point
       
  1443 	TBreakId armBreakId = 0;
       
  1444 	err = aThreadSpecific
       
  1445 		? iServSession.SetBreak(armBreakId, iThreadID, address, EArmMode)
       
  1446 		: iServSession.SetProcessBreak(armBreakId, processId, address, EArmMode);
       
  1447 	test(err == KErrNone);
       
  1448 
       
  1449 	/*
       
  1450 	 * Invalid thread
       
  1451 	 */
       
  1452 	err = aThreadSpecific
       
  1453 		? iServSession.ModifyBreak(armBreakId, 0xbabababa, address, EArmMode)
       
  1454 		: iServSession.ModifyProcessBreak(armBreakId, 0xbabababa, address, EArmMode);
       
  1455 	test(err == KErrPermissionDenied);
       
  1456 
       
  1457 	/*
       
  1458 	 * Valid address
       
  1459 	 */
       
  1460 	err = aThreadSpecific
       
  1461 		? iServSession.ModifyBreak(armBreakId, iThreadID, address+4, EArmMode)
       
  1462 		: iServSession.ModifyProcessBreak(armBreakId, processId, address+4, EArmMode);
       
  1463 	test(err == KErrNone);
       
  1464 
       
  1465 	/*
       
  1466 	 * Invalid address
       
  1467 	 */
       
  1468 	err = aThreadSpecific
       
  1469 		? iServSession.ModifyBreak(armBreakId, iThreadID, 0, EArmMode)
       
  1470 		: iServSession.ModifyProcessBreak(armBreakId, processId, 0, EArmMode);
       
  1471 	test(err == KErrBadDescriptor);
       
  1472 
       
  1473 	/*
       
  1474 	 * Thumb mode
       
  1475 	 */
       
  1476 	err = aThreadSpecific
       
  1477 		? iServSession.ModifyBreak(armBreakId, iThreadID, address, EThumbMode)
       
  1478 		: iServSession.ModifyProcessBreak(armBreakId, processId, address, EThumbMode);
       
  1479 	test(err == KErrNone);
       
  1480 
       
  1481 	/*
       
  1482 	 * Thumb2EE mode
       
  1483 	 */
       
  1484 	err = aThreadSpecific
       
  1485 		? iServSession.ModifyBreak(armBreakId, iThreadID, address, EThumb2EEMode)
       
  1486 		: iServSession.ModifyProcessBreak(armBreakId, processId, address, EThumb2EEMode);
       
  1487 	test(err == KErrNotSupported);
       
  1488 
       
  1489 	/*
       
  1490 	 * Arm mode
       
  1491 	 */
       
  1492 	err = aThreadSpecific
       
  1493 		? iServSession.ModifyBreak(armBreakId, iThreadID, address, EArmMode)
       
  1494 		: iServSession.ModifyProcessBreak(armBreakId, processId, address, EArmMode);
       
  1495 	test(err == KErrNone);
       
  1496 
       
  1497 	// Finally, clear the breakpoint
       
  1498 	err = iServSession.ClearBreak(armBreakId);
       
  1499 	test(err == KErrNone);
       
  1500 
       
  1501 	//resume the thread
       
  1502 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  1503 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  1504 	}
       
  1505 
       
  1506 //---------------------------------------------
       
  1507 //! @SYMTestCaseID KBase-T-RMDEBUG2-0438
       
  1508 //! @SYMTestType
       
  1509 //! @SYMPREQ PREQ1426
       
  1510 //! @SYMTestCaseDesc Test extracting information about breakpoints
       
  1511 //! @SYMTestActions Several calls to get information about breakpoints
       
  1512 //! @SYMTestExpectedResults All tests should pass and the target process should be left unaffected
       
  1513 //! @SYMTestPriority High
       
  1514 //! @SYMTestStatus Implemented
       
  1515 //---------------------------------------------
       
  1516 void CRunModeAgent::TestBreakInfo()
       
  1517 	{
       
  1518 	test.Next(_L("TestBreakInfo\n"));
       
  1519 
       
  1520 	DoTestBreakInfo(ETrue);
       
  1521 	DoTestBreakInfo(EFalse);
       
  1522 	}
       
  1523 
       
  1524 void CRunModeAgent::DoTestBreakInfo(TBool aThreadSpecific)
       
  1525 	{
       
  1526 	test.Printf(_L("DoTestModifyBreak: aThreadSpecific: %d\n"), aThreadSpecific?1:0);
       
  1527 
       
  1528 	TInt err;
       
  1529 
       
  1530 	RProcess process;
       
  1531 	TProcessId processId = process.Id();
       
  1532 	process.Close();
       
  1533 
       
  1534 	//an address in the target debug thread
       
  1535 	TUint32 address = (TUint32)(&TestFunction);
       
  1536 
       
  1537 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1538 
       
  1539 	//suspend thread
       
  1540 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  1541 
       
  1542 	//set an arm mode break point
       
  1543 	TBreakId armBreakId = 0;
       
  1544 	err = aThreadSpecific
       
  1545 		? iServSession.SetBreak(armBreakId, iThreadID, address, EArmMode)
       
  1546 		: iServSession.SetProcessBreak(armBreakId, processId, address, EArmMode);
       
  1547 	test(err == KErrNone);
       
  1548 
       
  1549 	// Read back the information and check it is correct
       
  1550 	TThreadId testThreadId = TThreadId(0);
       
  1551 	TProcessId testProcessId = TProcessId(0);
       
  1552 	TUint32 testAddress = 0;
       
  1553 	TArchitectureMode testMode = EArmMode;
       
  1554 
       
  1555 	err = aThreadSpecific
       
  1556 		? iServSession.BreakInfo(armBreakId,testThreadId,testAddress, testMode)
       
  1557 		: iServSession.ProcessBreakInfo(armBreakId, testProcessId, testAddress, testMode);
       
  1558 	test (err == KErrNone);
       
  1559 	test (aThreadSpecific ? (testThreadId == iThreadID) : (testProcessId == processId));
       
  1560 	test (testAddress == address);
       
  1561 	test (testMode == EArmMode);
       
  1562 
       
  1563 	//change the address
       
  1564 	TUint32 changeAddress = address + 64;
       
  1565 	err = aThreadSpecific
       
  1566 		? iServSession.ModifyBreak(armBreakId, iThreadID, changeAddress,EArmMode)
       
  1567 		: iServSession.ModifyProcessBreak(armBreakId, processId, changeAddress, EArmMode);
       
  1568 	test(err == KErrNone);
       
  1569 
       
  1570 	// Check the address has changed
       
  1571 	err = aThreadSpecific
       
  1572 		? iServSession.BreakInfo(armBreakId,testThreadId,testAddress, testMode)
       
  1573 		: iServSession.ProcessBreakInfo(armBreakId, testProcessId, testAddress, testMode);
       
  1574 	test (err == KErrNone);
       
  1575 	test (testAddress == changeAddress);
       
  1576 
       
  1577 	// change the architecture type
       
  1578 	TArchitectureMode checkMode = EThumbMode;
       
  1579 	err = aThreadSpecific
       
  1580 		? iServSession.ModifyBreak(armBreakId, iThreadID, address,checkMode)
       
  1581 		: iServSession.ModifyProcessBreak(armBreakId, processId, address, checkMode);
       
  1582 	test (err == KErrNone);
       
  1583 
       
  1584 	// Check the mode has changed
       
  1585 	err = aThreadSpecific
       
  1586 		? iServSession.BreakInfo(armBreakId,testThreadId,testAddress,testMode)
       
  1587 		: iServSession.ProcessBreakInfo(armBreakId, testProcessId, testAddress, testMode);
       
  1588 	test (err == KErrNone);
       
  1589 	test (testMode == checkMode);
       
  1590 
       
  1591 	// clear the breakpoint again
       
  1592 	err = iServSession.ClearBreak(armBreakId);
       
  1593 	test (err == KErrNone);
       
  1594 
       
  1595 	//resume thread
       
  1596 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  1597 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  1598 	}
       
  1599 
       
  1600 // Needed for the RunToBreak test
       
  1601 IMPORT_C extern void RMDebug_BranchTst1();
       
  1602 IMPORT_C extern void RMDebug_BranchTst2();
       
  1603 
       
  1604 //---------------------------------------------
       
  1605 //! @SYMTestCaseID KBase-T-RMDEBUG2-0439
       
  1606 //! @SYMTestType
       
  1607 //! @SYMPREQ PREQ1426
       
  1608 //! @SYMTestCaseDesc Test hitting various types of breakpoints
       
  1609 //! @SYMTestActions Several calls to register to observe breakpoints and to hit breakpoints of different types
       
  1610 //! @SYMTestExpectedResults All tests should pass and the target process should be left unaffected
       
  1611 //! @SYMTestPriority High
       
  1612 //! @SYMTestStatus Implemented
       
  1613 //---------------------------------------------
       
  1614 void CRunModeAgent::TestRunToBreak()
       
  1615 	{
       
  1616 	test.Next(_L("TestRunToBreak\n"));
       
  1617 
       
  1618 	DoTestRunToBreak(ETrue);
       
  1619 	DoTestRunToBreak(EFalse);
       
  1620 	}
       
  1621 
       
  1622 void CRunModeAgent::DoTestRunToBreak(TBool aThreadSpecific)
       
  1623 	{
       
  1624 	test.Printf(_L("DoTestRunToBreak: aThreadSpecific: %d\n"), aThreadSpecific?1:0);
       
  1625 
       
  1626 	TInt err = KErrNone;
       
  1627 
       
  1628 	RProcess process;
       
  1629 	TProcessId processId = process.Id();
       
  1630 	process.Close();
       
  1631 
       
  1632 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1633 	// we should suspend the thread first, then set the breakpoint
       
  1634 	err = iServSession.SuspendThread(iThreadID);
       
  1635 	test (err == KErrNone);
       
  1636 
       
  1637 	// Try to set the breakpoint
       
  1638 	TBreakId armBreakId;
       
  1639 	TUint32 address = (TUint32)(&RMDebug_BranchTst1);
       
  1640 
       
  1641 	err = aThreadSpecific
       
  1642 		? iServSession.SetBreak(armBreakId,iThreadID,address,EArmMode)
       
  1643 		: iServSession.SetProcessBreak(armBreakId, processId, address, EArmMode);
       
  1644 	test(err == KErrNone);
       
  1645 
       
  1646 	err = aThreadSpecific
       
  1647 		? iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionContinue)
       
  1648 		: iServSession.SetEventAction(iFileName,EEventsProcessBreakPoint, EActionContinue);
       
  1649 	test (err == KErrNone);
       
  1650 
       
  1651 	// Continue the thread
       
  1652 	err = iServSession.ResumeThread(iThreadID);
       
  1653 	test (err == KErrNone);
       
  1654 
       
  1655 	// wait for the breakpoint to be hit
       
  1656 	TEventInfo info;
       
  1657 	static TRequestStatus status;
       
  1658 
       
  1659 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  1660 
       
  1661 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  1662 
       
  1663 	// Wait for notification of the breakpoint hit event
       
  1664 	User::WaitForRequest(status);
       
  1665 	test(status==KErrNone);
       
  1666 
       
  1667 	// info should now be filled with the details
       
  1668 	test(info.iEventType == (aThreadSpecific ? EEventsBreakPoint : EEventsProcessBreakPoint));
       
  1669 	test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address);
       
  1670 	test(info.iProcessIdValid);
       
  1671 	test(info.iThreadIdValid);
       
  1672 
       
  1673 	// Not interested in breakpoint events any more
       
  1674 	err = aThreadSpecific
       
  1675 		? iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionIgnore)
       
  1676 		: iServSession.SetEventAction(iFileName, EEventsProcessBreakPoint, EActionIgnore);
       
  1677 	test (err == KErrNone);
       
  1678 
       
  1679 	// Clear the breakpoint again
       
  1680 	err = iServSession.ClearBreak(armBreakId);
       
  1681 	test(err == KErrNone);
       
  1682 
       
  1683 	// continue the thread again
       
  1684 	err = iServSession.ResumeThread(iThreadID);
       
  1685 	test (err == KErrNone);
       
  1686 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  1687 	}
       
  1688 
       
  1689 //---------------------------------------------
       
  1690 //! @SYMTestCaseID KBASE-rmdebug2-2704
       
  1691 //! @SYMTestType
       
  1692 //! @SYMPREQ PREQ1426
       
  1693 //! @SYMTestCaseDesc Test breakpoints in a loop
       
  1694 //! @SYMTestActions Several calls to register to verify breakpoints are stopping at correct address
       
  1695 //! @SYMTestExpectedResults All tests should pass and the target thread should be left unaffected
       
  1696 //! @SYMTestPriority High
       
  1697 //! @SYMTestStatus Implemented
       
  1698 //---------------------------------------------
       
  1699 void CRunModeAgent::TestBreakPointsInLoop()
       
  1700 	{
       
  1701 	test.Next(_L("TestBreakPointsInLoop\n"));
       
  1702 
       
  1703 	DoTestBreakPointsInLoop(ETrue);
       
  1704 	DoTestBreakPointsInLoop(EFalse);
       
  1705 	}
       
  1706 
       
  1707 void CRunModeAgent::DoTestBreakPointsInLoop(TBool aThreadSpecific)
       
  1708 	{
       
  1709 	test.Printf(_L("DoTestBreakPointsInLoop: aThreadSpecific: %d\n"), aThreadSpecific?1:0);
       
  1710 
       
  1711 	TInt err = KErrNone;
       
  1712 	TProcessId processId = RProcess().Id(); 
       
  1713 
       
  1714 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1715 	
       
  1716 	// We should suspend the thread first, then set the breakpoint
       
  1717 	err = iServSession.SuspendThread(iThreadID);
       
  1718 	test (err == KErrNone);
       
  1719 
       
  1720 	// 2 breakpoints are sufficient to find issues with hitting breakpoints in a loop
       
  1721 	const TInt numOfBreakPointsInLoop = 2;
       
  1722 
       
  1723 	TBreakId armBreakId[numOfBreakPointsInLoop];
       
  1724 	TUint32 address[numOfBreakPointsInLoop];
       
  1725    	
       
  1726 	TUint32 entryAddress = (TUint32)(&RMDebug_Bkpt_Test_Entry);
       
  1727 	TBreakId entryArmBreakId;
       
  1728 
       
  1729 	// Copy breakpoint address's in array
       
  1730 	address[0] = (TUint32)(&RMDebug_Bkpt_Test_Loop_Break_1);
       
  1731 	address[1] = (TUint32)(&RMDebug_Bkpt_Test_Loop_Break_2);
       
  1732 
       
  1733 	err = aThreadSpecific
       
  1734 		? iServSession.SetBreak(entryArmBreakId,iThreadID,entryAddress,EArmMode)
       
  1735 		: iServSession.SetProcessBreak(entryArmBreakId, processId, entryAddress, EArmMode);
       
  1736 	test(err == KErrNone);
       
  1737 
       
  1738 	// Try to set the breakpoints inside loop
       
  1739 	for (TInt i = 0; i < numOfBreakPointsInLoop; i++)
       
  1740 		{
       
  1741 		err = aThreadSpecific
       
  1742 			? iServSession.SetBreak(armBreakId[i],iThreadID,address[i],EArmMode)
       
  1743 			: iServSession.SetProcessBreak(armBreakId[i], processId, address[i], EArmMode);
       
  1744 		test(err == KErrNone);
       
  1745 		}
       
  1746 
       
  1747 	err = aThreadSpecific
       
  1748 		? iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionSuspend)
       
  1749 		: iServSession.SetEventAction(iFileName,EEventsProcessBreakPoint, EActionSuspend);
       
  1750 	test (err == KErrNone);
       
  1751 
       
  1752 	// Continue the thread
       
  1753 	err = iServSession.ResumeThread(iThreadID);
       
  1754 	test (err == KErrNone);
       
  1755 
       
  1756 	// Wait for the breakpoint to be hit
       
  1757 	TEventInfo info;
       
  1758 	TRequestStatus status;
       
  1759 
       
  1760 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  1761 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  1762 
       
  1763 	// Wait for notification of breakpoint event
       
  1764 	User::WaitForRequest(status);
       
  1765 	test(status==KErrNone);
       
  1766 
       
  1767 	// Info should now be filled with the details
       
  1768 	test(info.iEventType == (aThreadSpecific ? EEventsBreakPoint : EEventsProcessBreakPoint));
       
  1769 
       
  1770 	// Have we stopped at the correct breakpoint?
       
  1771 	test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == entryAddress);
       
  1772 	test(info.iProcessIdValid);
       
  1773 	test(info.iThreadIdValid);
       
  1774 
       
  1775 	// Don't require the entry breakpoint anymore
       
  1776 	err = iServSession.ClearBreak(entryArmBreakId);
       
  1777 	test(err == KErrNone);
       
  1778 	
       
  1779 	// Stress the system by setting loop count to 100
       
  1780 	const TUint32 loopCount = 100;
       
  1781 
       
  1782 	for (TInt i = 0; i < loopCount; i++)
       
  1783 		{
       
  1784 		// Continue the thread
       
  1785 		err = iServSession.ResumeThread(iThreadID);
       
  1786 		test (err == KErrNone);
       
  1787 
       
  1788 		// Wait for the breakpoint to be hit
       
  1789 		iServSession.GetEvent(iFileName,status,infoPtr);
       
  1790 		
       
  1791 		// Wait for notification of the breakpoint hit event
       
  1792 		User::WaitForRequest(status);
       
  1793 		test(status==KErrNone);
       
  1794 		
       
  1795 		// Info should now be filled with the details
       
  1796 		test(info.iEventType == (aThreadSpecific ? EEventsBreakPoint : EEventsProcessBreakPoint));
       
  1797 		
       
  1798 		// Have we stopped at the correct breakpoint?
       
  1799 		test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address[i%numOfBreakPointsInLoop]);
       
  1800 		
       
  1801 		// Check process and thread id too
       
  1802 		test(info.iProcessIdValid);
       
  1803 		test(info.iThreadIdValid);
       
  1804 		}
       
  1805 
       
  1806 	// Not interested in breakpoint events any more
       
  1807 	err = aThreadSpecific
       
  1808 		? iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionIgnore)
       
  1809 		: iServSession.SetEventAction(iFileName, EEventsProcessBreakPoint, EActionIgnore);
       
  1810 	test (err == KErrNone);
       
  1811 
       
  1812 	// Clear breakpoints
       
  1813 	for (TInt i = 0; i < numOfBreakPointsInLoop; i++)
       
  1814 		{
       
  1815 		err = iServSession.ClearBreak(armBreakId[i]);
       
  1816 		test(err == KErrNone);
       
  1817 		}
       
  1818 	
       
  1819 	// Continue the thread again
       
  1820 	err = iServSession.ResumeThread(iThreadID);
       
  1821 	test (err == KErrNone);
       
  1822 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  1823 	}
       
  1824 
       
  1825 //----------------------------------------------------------------------------------------------
       
  1826 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0440
       
  1827 //! @SYMTestType
       
  1828 //! @SYMPREQ            PREQ1426
       
  1829 //! @SYMTestCaseDesc    Test access to target user-side registers.
       
  1830 //! @SYMTestActions     Suspends a target thread, and reads/writes target thread register contents
       
  1831 //!
       
  1832 //! @SYMTestExpectedResults KErrNone. Should access target registers without problems.
       
  1833 //! @SYMTestPriority        High
       
  1834 //! @SYMTestStatus          Implemented
       
  1835 //----------------------------------------------------------------------------------------------
       
  1836 
       
  1837 void CRunModeAgent::TestRegisterAccess()
       
  1838 	{
       
  1839 	TInt err;
       
  1840 
       
  1841 	test.Next(_L("TestRegisterAccess - Read\n"));
       
  1842 
       
  1843 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  1844 
       
  1845 	//suspend the thread to read registers
       
  1846 	err = iServSession.SuspendThread(iThreadID);
       
  1847 	test(err==KErrNone);
       
  1848 
       
  1849 	//we'll try to read/write registers ERegisterR0 - ERegisterCPSR and ERegisterR13_IRQ
       
  1850 	//this way should get valid register values back, invalid ones and not supported ones, and it
       
  1851 	//means that the register IDs are not completely contiguous
       
  1852 
       
  1853 	TInt firstRegister = 0;
       
  1854 	TInt lastRegister = 17;
       
  1855 	TInt numberOfRegisters = (lastRegister - firstRegister) + 1;
       
  1856 
       
  1857 	RBuf8 ids;
       
  1858 	err = ids.Create(numberOfRegisters * sizeof(TRegisterInfo));
       
  1859 	test(err == KErrNone);
       
  1860 
       
  1861 	for(TInt i=0; i<numberOfRegisters - 1; i++)
       
  1862 		{
       
  1863 		TRegisterInfo reg = (TRegisterInfo)((i + firstRegister)<<8);
       
  1864 		ids.Append(reinterpret_cast<const TUint8*>(&reg), sizeof(TRegisterInfo));
       
  1865 		}
       
  1866 
       
  1867 	TRegisterInfo reg = ERegisterR13Irq;
       
  1868 	ids.Append(reinterpret_cast<const TUint8*>(&reg), sizeof(TRegisterInfo));
       
  1869 
       
  1870 	//create a buffer to store the register values in
       
  1871 	RBuf8 originalValues;
       
  1872 	err = originalValues.Create(numberOfRegisters*sizeof(TUint32));
       
  1873 	test(err == KErrNone);
       
  1874 
       
  1875 	//create a buffer to store the register flags in
       
  1876 	RBuf8 originalFlags;
       
  1877 	err = originalFlags.Create(numberOfRegisters*sizeof(TUint8));
       
  1878 	test(err == KErrNone);
       
  1879 
       
  1880 	//read register values
       
  1881 	err = iServSession.ReadRegisters(iThreadID, ids, originalValues, originalFlags);
       
  1882 	test(err == KErrNone);
       
  1883 
       
  1884 	//create a buffer containing data to write into the registers
       
  1885 	RBuf8 tempValues;
       
  1886 	err = tempValues.Create(numberOfRegisters*sizeof(TUint32));
       
  1887 	test(err == KErrNone);
       
  1888 
       
  1889 	TUint cpsrId = 16;
       
  1890 	for(TUint8 i=0; i<numberOfRegisters*sizeof(TUint32); i++)
       
  1891 		{
       
  1892 		if(i/sizeof(TUint32) == cpsrId)
       
  1893 			{
       
  1894 			//For the CPSR we wish to write data that makes sense - for USR mode we are
       
  1895 			//allowed change all except the mode, ie. we must stay in usr mode. We try that here
       
  1896 			//(allowedCPSRValue[4:0] = 10000) thus not changing the mode.
       
  1897 			TUint32 allowedCPSRValue = 0x50000010;
       
  1898 			tempValues.Append((TUint8*)&allowedCPSRValue, 4);
       
  1899 			i += 3;
       
  1900 			}
       
  1901 		else
       
  1902 			{
       
  1903 			tempValues.Append(&i, 1);
       
  1904 			}
       
  1905 		}
       
  1906 
       
  1907 	test.Next(_L("TestRegisterAccess - Write\n"));
       
  1908 
       
  1909 	//create a buffer to store the register flags in
       
  1910 	RBuf8 tempWriteFlags;
       
  1911 	err = tempWriteFlags.Create(numberOfRegisters*sizeof(TUint8));
       
  1912 	test(err == KErrNone);
       
  1913 
       
  1914 	//write the temp data into the registers
       
  1915 	err = iServSession.WriteRegisters(iThreadID, ids, tempValues, tempWriteFlags);
       
  1916 	test(err == KErrNone);
       
  1917 
       
  1918 	//create another buffer to store the register flags in
       
  1919 	RBuf8 tempReadFlags;
       
  1920 	err = tempReadFlags.Create(numberOfRegisters*sizeof(TUint8));
       
  1921 	test(err == KErrNone);
       
  1922 
       
  1923 	RBuf8 tempReadValues;
       
  1924 	err = tempReadValues.Create(numberOfRegisters*sizeof(TUint32));
       
  1925 	test(err == KErrNone);
       
  1926 
       
  1927 	//read the temp data out again
       
  1928 	err = iServSession.ReadRegisters(iThreadID, ids, tempReadValues, tempReadFlags);
       
  1929 	test(err == KErrNone);
       
  1930 
       
  1931 	//check values are correct
       
  1932 	for(TInt i=0; i<numberOfRegisters; i++)
       
  1933 		{
       
  1934 		TRegisterFlag writeFlag;
       
  1935 		err = GetFlag(tempWriteFlags, i, writeFlag);
       
  1936 		test(err == KErrNone);
       
  1937 
       
  1938 		TRegisterFlag readFlag;
       
  1939 		err = GetFlag(tempReadFlags, i, readFlag);
       
  1940 		test(err == KErrNone);
       
  1941 
       
  1942 		if((writeFlag == EValid) && (readFlag == EValid))
       
  1943 			{
       
  1944 			TUint8 offset = i * sizeof(TUint32);
       
  1945 			for(TUint j = offset; j< offset + sizeof(TUint32); j++)
       
  1946 				{
       
  1947 				test(tempValues.Ptr()[j] == tempReadValues.Ptr()[j]);
       
  1948 				}
       
  1949 			}
       
  1950 		}
       
  1951 
       
  1952 	//write the original data into the registers
       
  1953 	err = iServSession.WriteRegisters(iThreadID, ids, originalValues, originalFlags);
       
  1954 	test(err == KErrNone);
       
  1955 
       
  1956 	//read the data out again
       
  1957 	err = iServSession.ReadRegisters(iThreadID, ids, tempValues, tempReadFlags);
       
  1958 	test(err == KErrNone);
       
  1959 
       
  1960 	//check values are correct
       
  1961 	for(TInt i=0; i<numberOfRegisters; i++)
       
  1962 		{
       
  1963 		TRegisterFlag writeFlag;
       
  1964 		err = GetFlag(originalFlags, i, writeFlag);
       
  1965 		test(err == KErrNone);
       
  1966 
       
  1967 		TRegisterFlag readFlag;
       
  1968 		err = GetFlag(tempReadFlags, i, readFlag);
       
  1969 		test(err == KErrNone);
       
  1970 
       
  1971 		if((writeFlag == EValid) && (readFlag == EValid))
       
  1972 			{
       
  1973 			TUint8 offset = i * sizeof(TUint32);
       
  1974 			for(TUint j = offset; j< offset + sizeof(TUint32); j++)
       
  1975 				{
       
  1976 				test(tempValues.Ptr()[j] == originalValues.Ptr()[j]);
       
  1977 				}
       
  1978 			}
       
  1979 		}
       
  1980 
       
  1981 	test.Next(_L("TestRegisterAccess - Invalid data\n"));
       
  1982 
       
  1983 	//create a buffer of max size 1
       
  1984 	RBuf8 emptyBuffer;
       
  1985 	emptyBuffer.Create(1);
       
  1986 
       
  1987 	//test register IDs buffer not being a multiple of sizeof(TRegisterInfo)
       
  1988 	err = iServSession.ReadRegisters(iThreadID, emptyBuffer, tempValues, tempReadFlags);
       
  1989 	test(err == KErrArgument);
       
  1990 
       
  1991 	//test register values buffer not being a multiple of sizeof(TUint32)
       
  1992 	err = iServSession.ReadRegisters(iThreadID, ids, emptyBuffer, tempReadFlags);
       
  1993 	test(err == KErrArgument);
       
  1994 
       
  1995 	//test flags buffer being representing different number of registers from other two
       
  1996 	err = iServSession.ReadRegisters(iThreadID, ids, tempValues, emptyBuffer);
       
  1997 	test(err == KErrArgument);
       
  1998 
       
  1999 	//set max length to 0
       
  2000 	emptyBuffer.ReAlloc(0);
       
  2001 
       
  2002 	//test ids buffer being of 0 max length
       
  2003 	err = iServSession.ReadRegisters(iThreadID, emptyBuffer, tempValues, tempReadFlags);
       
  2004 	test(err == KErrArgument);
       
  2005 
       
  2006 	//do cleanup
       
  2007 	emptyBuffer.Close();
       
  2008 	tempValues.Close();
       
  2009 	tempWriteFlags.Close();
       
  2010 	tempReadFlags.Close();
       
  2011 	tempReadValues.Close();
       
  2012 
       
  2013 	test.Next(_L("TestRegisterAccess - Setting PC value\n"));
       
  2014 
       
  2015 	//create buffer containing PC register ID
       
  2016 	RBuf8 pcId;
       
  2017 	err = pcId.Create(sizeof(TRegisterInfo));
       
  2018 	test(err == KErrNone);
       
  2019 	TRegisterInfo reg1 = (TRegisterInfo)0x00000f00;
       
  2020 	pcId.Append(reinterpret_cast<const TUint8*>(&reg1), sizeof(TRegisterInfo));
       
  2021 
       
  2022 	//create buffer containing desired PC value
       
  2023 	RBuf8 pcValue;
       
  2024 	err = pcValue.Create(sizeof(TUint32));
       
  2025 	test(err == KErrNone);
       
  2026 	TUint32 address = (TUint32)(&TestFunction);
       
  2027 	pcValue.Append(reinterpret_cast<const TUint8*>(&address), sizeof(TUint32));
       
  2028 
       
  2029 	//craete buffer for PC flag value
       
  2030 	RBuf8 pcFlag;
       
  2031 	err = pcFlag.Create(sizeof(TUint8));
       
  2032 
       
  2033 	//write the new PC value
       
  2034 	err = iServSession.WriteRegisters(iThreadID, pcId, pcValue, pcFlag);
       
  2035 	test(err==KErrNone);
       
  2036 
       
  2037 	//get the flag and check the PC value was written ok
       
  2038 	TRegisterFlag flag = ENotSupported;
       
  2039 	err = GetFlag(pcFlag, 0, flag);
       
  2040 	test(err==KErrNone);
       
  2041 	test( flag == EValid);
       
  2042 	if(flag == EValid)
       
  2043 		{
       
  2044 		/* The PC value was changed to execute the function TestFunction.
       
  2045 		* TestFunction changes the value of TestData to a given value and 
       
  2046 		* then calls RMDebug_BranchTst1.
       
  2047 		* We place a breakpoint on RMDebug_BranchTst1 so that to we are able 
       
  2048 		* to test the value of TestData.
       
  2049 		*/
       
  2050 
       
  2051 		test(KErrNone == iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionSuspend));
       
  2052 		TBreakId armBreakId;
       
  2053 		TUint32 address = (TUint32)(&RMDebug_BranchTst1);
       
  2054 		test(KErrNone == iServSession.SetBreak(armBreakId,iThreadID,address,EArmMode));
       
  2055 
       
  2056 		// Continue the thread
       
  2057 		test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  2058 
       
  2059 		// wait for the breakpoint to be hit
       
  2060 		TEventInfo info;
       
  2061 		static TRequestStatus status;
       
  2062 
       
  2063 		TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  2064 		iServSession.GetEvent(iFileName,status,infoPtr);
       
  2065 
       
  2066 		// Wait for notification of the breakpoint hit event
       
  2067 		User::WaitForRequest(status);
       
  2068 		test(status==KErrNone);
       
  2069 
       
  2070 		// info should now be filled with the details
       
  2071 		test(info.iEventType == EEventsBreakPoint);
       
  2072 		test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address);
       
  2073 		test(info.iProcessIdValid);
       
  2074 		test(info.iThreadIdValid);
       
  2075 
       
  2076 		test(KErrNone == iServSession.ClearBreak(armBreakId));
       
  2077 
       
  2078 		// Finally test the value
       
  2079 		test(TestData == 0xffeeddcc);
       
  2080 		}
       
  2081 
       
  2082 	//Make sure we cannot change the CPSR
       
  2083 	test.Next(_L("Verifying we cannot change the CPSR mode from USR Mode"));
       
  2084 
       
  2085 	TUint32 disallowedCpsr = 0x50000013;
       
  2086 
       
  2087 	RBuf8 cpsrRegId;
       
  2088 	err = cpsrRegId.Create(sizeof(TUint32));
       
  2089 	test(err == KErrNone);
       
  2090 
       
  2091 	TRegisterInfo cpsr = (TRegisterInfo)((cpsrId + firstRegister)<<8);
       
  2092 	cpsrRegId.Append(reinterpret_cast<const TUint8*>(&cpsr), sizeof(TRegisterInfo));
       
  2093 
       
  2094 	RBuf8 cpsrRegFlags;
       
  2095 	err = cpsrRegFlags.Create(sizeof(TUint8));
       
  2096 	test(err == KErrNone);
       
  2097 
       
  2098 	RBuf8 cpsrVal;
       
  2099 	err = cpsrVal.Create(sizeof(TUint32));
       
  2100 	test(err == KErrNone);
       
  2101 
       
  2102 	cpsrVal.Append((TUint8*)&disallowedCpsr, 4);
       
  2103 
       
  2104 	//attempt to write disallowed CPSR in
       
  2105 	err = iServSession.WriteRegisters(iThreadID, cpsrRegId, cpsrVal, cpsrRegFlags);
       
  2106 	test(err == KErrNone);
       
  2107 
       
  2108 	RBuf8 cpsrReadVal;
       
  2109 	err = cpsrReadVal.Create(sizeof(TUint32));
       
  2110 	test(err == KErrNone);
       
  2111 
       
  2112 	//Read back the CPSR
       
  2113 	err = iServSession.ReadRegisters(iThreadID, cpsrRegId, cpsrReadVal, cpsrRegFlags);
       
  2114 	test(err == KErrNone);
       
  2115 
       
  2116 	//Make sure we havent switched modes ie. its not what we wrote
       
  2117 	TUint32* readVal = (TUint32*)cpsrReadVal.Ptr();
       
  2118 	test(*readVal != disallowedCpsr);
       
  2119 
       
  2120 	cpsrRegId.Close();
       
  2121 	cpsrRegFlags.Close();
       
  2122 	cpsrVal.Close();
       
  2123 	cpsrReadVal.Close();
       
  2124 
       
  2125 	//write the original values back into here
       
  2126 	err = iServSession.WriteRegisters(iThreadID, ids, originalValues, originalFlags);
       
  2127 	test(err == KErrNone);
       
  2128 	
       
  2129 	test(KErrNone == SwitchTestFunction(EDefaultFunction));
       
  2130 
       
  2131 	// Resume the thread
       
  2132 	err = iServSession.ResumeThread(iThreadID);
       
  2133 	test(err==KErrNone);
       
  2134 
       
  2135 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2136 
       
  2137 	//do cleanup
       
  2138 	pcId.Close();
       
  2139 	pcValue.Close();
       
  2140 	pcFlag.Close();
       
  2141 	ids.Close();
       
  2142 	originalValues.Close();
       
  2143 	originalFlags.Close();
       
  2144 	}
       
  2145 
       
  2146 //----------------------------------------------------------------------------------------------
       
  2147 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0441
       
  2148 //! @SYMTestType
       
  2149 //! @SYMPREQ            PREQ1426
       
  2150 //! @SYMTestCaseDesc    Test registration/de-registration of debug interest in target exe with the Debug Security Server
       
  2151 //! @SYMTestActions     As per description
       
  2152 //!
       
  2153 //! @SYMTestExpectedResults KErrNone.
       
  2154 //! @SYMTestPriority        High
       
  2155 //! @SYMTestStatus          Implemented
       
  2156 //----------------------------------------------------------------------------------------------
       
  2157 
       
  2158 void CRunModeAgent::TestAttachExecutable()
       
  2159 	{
       
  2160 
       
  2161 	test.Next(_L("TestAttachExecutable - Attach\n"));
       
  2162 
       
  2163 	//attach to process passively
       
  2164 	test(KErrNone == iServSession.AttachExecutable(iFileName, ETrue));
       
  2165 
       
  2166 	//make a thread id for a non-existent thread
       
  2167 	TThreadId threadId(0x12345678);
       
  2168 
       
  2169 	//get a handle to the target thread
       
  2170 	RThread targetThread;
       
  2171 	TInt err = targetThread.Open(threadId);
       
  2172 	test(err != KErrNone);
       
  2173 
       
  2174 	//not registered for this thread's process (as it doesn't exist)
       
  2175 	//so should fail security check
       
  2176 	err = iServSession.ResumeThread(threadId);
       
  2177 	test(err==KErrPermissionDenied || err==KErrNotFound); // newer DSS returns the more-descriptive KErrNotFound here
       
  2178 
       
  2179 	//try to attach to the same process (and fail)
       
  2180 	test(KErrAlreadyExists == iServSession.AttachExecutable(iFileName, EFalse));
       
  2181 
       
  2182 	test.Next(_L("TestAttachExecutable - Detach\n"));
       
  2183 
       
  2184 	//detach from process
       
  2185 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2186 
       
  2187 	//attach non-passively
       
  2188 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  2189 
       
  2190 	//not registered for this thread's process (as it doesn't exist)
       
  2191 	//so should fail security check
       
  2192 	err = iServSession.ResumeThread(0x12345678);
       
  2193 	test(err==KErrPermissionDenied || err==KErrNotFound); // newer DSS returns the more-descriptive KErrNotFound here
       
  2194 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2195 	}
       
  2196 
       
  2197 //----------------------------------------------------------------------------------------------
       
  2198 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0442
       
  2199 //! @SYMTestType
       
  2200 //! @SYMPREQ            PREQ1426
       
  2201 //! @SYMTestCaseDesc    Tests single-stepping target threads.
       
  2202 //! @SYMTestActions     Steps target thread assembly level instructions, mainly branch/change PC
       
  2203 //!
       
  2204 //! @SYMTestExpectedResults KErrNone.
       
  2205 //! @SYMTestPriority        High
       
  2206 //! @SYMTestStatus          Implemented
       
  2207 //----------------------------------------------------------------------------------------------
       
  2208 
       
  2209 void CRunModeAgent::TestStep()
       
  2210 	{
       
  2211 	test.Next(_L("TestStep\n"));
       
  2212 
       
  2213 	DoTestStep(EFalse);
       
  2214 	DoTestStep(ETrue);
       
  2215 	}
       
  2216 
       
  2217 void CRunModeAgent::DoTestStep(TBool aThreadSpecific)
       
  2218 	{
       
  2219 	test.Printf(_L("DoTestStep: aThreadSpecific: %d\n"), aThreadSpecific?1:0);
       
  2220 
       
  2221 	TInt err = KErrNone;
       
  2222 
       
  2223 	RProcess process;
       
  2224 	TProcessId processId = process.Id();
       
  2225 	process.Close();
       
  2226 
       
  2227 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  2228 	//set the target thread to execute the stepping functions
       
  2229 	test(KErrNone == SwitchTestFunction(EStepFunction, EFalse));
       
  2230 
       
  2231 	
       
  2232 	err = iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionContinue);
       
  2233 	test (err == KErrNone);
       
  2234 
       
  2235 	if(!aThreadSpecific)
       
  2236 		{
       
  2237 		err = iServSession.SetEventAction(iFileName, EEventsProcessBreakPoint, EActionContinue);
       
  2238 		test (err == KErrNone);
       
  2239 		}
       
  2240 
       
  2241 	TUint32	startAddress;
       
  2242 	TUint32	endAddress;
       
  2243 
       
  2244 	/*
       
  2245 	 * RMDebug_StepTest_Non_PC_Modifying
       
  2246 	 */
       
  2247 	test.Next(_L("TestStep - Non-PC modifying\n"));
       
  2248 
       
  2249 	startAddress = (TUint32)(&RMDebug_StepTest_Non_PC_Modifying);
       
  2250 
       
  2251 	endAddress = (TUint32)(&RMDebug_StepTest_Non_PC_Modifying_OK);
       
  2252 
       
  2253 	err = aThreadSpecific
       
  2254 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2255 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2256 	test(err==KErrNone);
       
  2257 
       
  2258 	/*
       
  2259 	 * RMDebug_StepTest_Branch
       
  2260 	 */
       
  2261 	test.Next(_L("TestStep - Branch\n"));
       
  2262 
       
  2263 	startAddress = (TUint32)(&RMDebug_StepTest_Branch);
       
  2264 
       
  2265 	endAddress = (TUint32)(&RMDebug_StepTest_Branch_1);
       
  2266 
       
  2267 	err = aThreadSpecific
       
  2268 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2269 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2270 	test(err==KErrNone);
       
  2271 
       
  2272 	/*
       
  2273 	 * RMDebug_StepTest_Branch_And_Link
       
  2274 	 */
       
  2275 	test.Next(_L("TestStep - Branch_And_Link\n"));
       
  2276 
       
  2277 	startAddress = (TUint32)(&RMDebug_StepTest_Branch_And_Link_1);
       
  2278 
       
  2279 	endAddress = (TUint32)(&RMDebug_StepTest_Branch_And_Link_2);
       
  2280 
       
  2281 	err = aThreadSpecific
       
  2282 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2283 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2284 	test(err==KErrNone);
       
  2285 
       
  2286 	/*
       
  2287 	 * RMDebug_StepTest_MOV_PC
       
  2288 	 */
       
  2289 	test.Next(_L("TestStep - MOV PC,X\n"));
       
  2290 
       
  2291 	startAddress = (TUint32)(&RMDebug_StepTest_MOV_PC_1);
       
  2292 
       
  2293 	endAddress = (TUint32)(&RMDebug_StepTest_MOV_PC_2);
       
  2294 
       
  2295 	err = aThreadSpecific
       
  2296 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2297 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2298 	test(err==KErrNone);
       
  2299 
       
  2300 	/*
       
  2301 	 * RMDebug_StepTest_LDR_PC
       
  2302 	 */
       
  2303 	test.Next(_L("TestStep - LDR PC\n"));
       
  2304 
       
  2305 	startAddress = (TUint32)(&RMDebug_StepTest_LDR_PC);
       
  2306 
       
  2307 	endAddress = (TUint32)(&RMDebug_StepTest_LDR_PC_1);
       
  2308 
       
  2309 	err = aThreadSpecific
       
  2310 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2311 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2312 	test(err==KErrNone);
       
  2313 
       
  2314 // thumb and interworking tests are not supported on armv4
       
  2315 #ifdef __MARM_ARMV5__
       
  2316 
       
  2317 	/*
       
  2318 	 * RMDebug_StepTest_Thumb_Non_PC_Modifying
       
  2319 	 */
       
  2320 	test.Next(_L("TestStep - Thumb Non PC-Modifying\n"));
       
  2321 
       
  2322 	startAddress = (TUint32)(&RMDebug_StepTest_Thumb_Non_PC_Modifying_1);
       
  2323 
       
  2324 	endAddress = (TUint32)(&RMDebug_StepTest_Thumb_Non_PC_Modifying_2);
       
  2325 
       
  2326 	err = aThreadSpecific
       
  2327 		? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2328 		: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2329 	test(err==KErrNone);
       
  2330 
       
  2331 	/*
       
  2332 	 * RMDebug_StepTest_Thumb_Branch
       
  2333 	 */
       
  2334 	test.Next(_L("TestStep - Thumb Branch\n"));
       
  2335 
       
  2336 	startAddress = (TUint32)(&RMDebug_StepTest_Thumb_Branch_1);
       
  2337 
       
  2338 	endAddress = (TUint32)(&RMDebug_StepTest_Thumb_Branch_2);
       
  2339 
       
  2340 	err = aThreadSpecific
       
  2341 		? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2342 		: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2343 	test(err==KErrNone);
       
  2344 
       
  2345 	/*
       
  2346 	 * RMDebug_StepTest_Thumb_Branch_And_Link
       
  2347 	 */
       
  2348 	test.Next(_L("TestStep - Thumb Branch_And_Link\n"));
       
  2349 
       
  2350 	startAddress = (TUint32)(&RMDebug_StepTest_Thumb_Branch_And_Link_2);
       
  2351 
       
  2352 	endAddress = (TUint32)(&RMDebug_StepTest_Thumb_Branch_And_Link_3);
       
  2353 
       
  2354 	TInt muid=0;
       
  2355     test(HAL::Get(HAL::EMachineUid, muid)==KErrNone);
       
  2356 
       
  2357 	// check if running on ARMv7 core
       
  2358 	if(muid==HAL::EMachineUid_OmapH6 || muid==HAL::EMachineUid_OmapZoom || muid==HAL::EMachineUid_EmuBoard)
       
  2359         {
       
  2360         // Note: ARMv7 treats BL instructions as single 32-bit instructions
       
  2361         err = aThreadSpecific
       
  2362 		? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2363 		: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2364         }
       
  2365     else
       
  2366 	    {
       
  2367         // Note: Due to the fact that the stepper treats BL instructions
       
  2368 		// as two instructions (as the hardware does), then we must step
       
  2369 		// the first half instruction first)
       
  2370 
       
  2371 		err = aThreadSpecific
       
  2372 		? HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1)
       
  2373 		: HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1, EFalse, processId);
       
  2374 		test(err==KErrNone);
       
  2375 
       
  2376 	// Now we actually do the BL
       
  2377 	err = aThreadSpecific
       
  2378 		? HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1)
       
  2379 		: HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1, EFalse, processId);
       
  2380         }
       
  2381 	test(err==KErrNone);
       
  2382 
       
  2383 	/*
       
  2384 	 * RMDebug_StepTest_Thumb_Back_Branch_And_Link
       
  2385 	 */
       
  2386 	test.Next(_L("TestStep - Thumb Back_Branch_And_Link\n"));
       
  2387 
       
  2388 	startAddress = (TUint32)(&RMDebug_StepTest_Thumb_Back_Branch_And_Link_2);
       
  2389 
       
  2390 	endAddress = (TUint32)(&RMDebug_StepTest_Thumb_Back_Branch_And_Link_3);
       
  2391 
       
  2392 	// check if running on ARMv7 core
       
  2393 	if(muid==HAL::EMachineUid_OmapH6 || muid==HAL::EMachineUid_OmapZoom || muid==HAL::EMachineUid_EmuBoard)
       
  2394 		{
       
  2395 		// Note: ARMv7 treats BL instructions as single 32-bit instructions
       
  2396 		err = aThreadSpecific
       
  2397 			? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2398 			: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2399 		}
       
  2400 	else
       
  2401 		{
       
  2402 		// Note: Due to the fact that the stepper treats BL instructions
       
  2403 		// as two instructions (as the hardware does), then we must step
       
  2404 		// the first half instruction first)
       
  2405 
       
  2406 		err = aThreadSpecific
       
  2407 	   		? HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1)
       
  2408 			: HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1, EFalse, processId);
       
  2409 		test(err==KErrNone);
       
  2410 
       
  2411 	   	// Now we actually do the BL
       
  2412 		err = aThreadSpecific
       
  2413    			? HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1)
       
  2414 			: HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1, EFalse, processId);
       
  2415 		}
       
  2416 	test(err==KErrNone);
       
  2417 
       
  2418 	/*
       
  2419 	 * RMDebug_StepTest_Thumb_AddPC
       
  2420 	 */
       
  2421 	test.Next(_L("TestStep - Thumb ADD PC, PC, R0\n"));
       
  2422 
       
  2423 	startAddress = (TUint32)(&RMDebug_StepTest_Thumb_AddPC_2);
       
  2424 
       
  2425 	endAddress = (TUint32)(&RMDebug_StepTest_Thumb_AddPC_3);
       
  2426 
       
  2427 	err = aThreadSpecific
       
  2428 		? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2429 		: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2430 	test(err==KErrNone);
       
  2431 
       
  2432 	/*
       
  2433 	 * RMDebug_StepTest_Interwork ARM to Thumb
       
  2434 	 */
       
  2435 	test.Next(_L("TestStep - Interworking ARM to Thumb - BLX \n"));
       
  2436 
       
  2437 	startAddress = (TUint32)(&RMDebug_StepTest_Interwork_1);
       
  2438 
       
  2439 	endAddress = (TUint32)(&RMDebug_StepTest_Interwork_2);
       
  2440 
       
  2441 	err = aThreadSpecific // nb initial breakpoint in ARM code
       
  2442 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1)
       
  2443 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,1, EFalse, processId);
       
  2444 
       
  2445 	test(err==KErrNone);
       
  2446 
       
  2447 	/*
       
  2448 	 * RMDebug_StepTest_Interwork Thumb to ARM
       
  2449 	 */
       
  2450 	test.Next(_L("TestStep - Interworking Thumb to ARM - BLX\n"));
       
  2451 
       
  2452 	startAddress = (TUint32)(&RMDebug_StepTest_Interwork_2);
       
  2453 
       
  2454 	endAddress = (TUint32)(&RMDebug_StepTest_Interwork_3);
       
  2455 
       
  2456 	// check if running on ARMv7 core
       
  2457 	if(muid==HAL::EMachineUid_OmapH6 || muid==HAL::EMachineUid_OmapZoom || muid==HAL::EMachineUid_EmuBoard)
       
  2458         {
       
  2459         // ARMv7 treats BLX instructions as single 32-bit instructions
       
  2460         err = aThreadSpecific
       
  2461 		? HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1)
       
  2462 		: HelpTestStep(iThreadID,startAddress,endAddress,EThumbMode,1, EFalse, processId);
       
  2463         }
       
  2464     else
       
  2465         {
       
  2466     	// Stepper treats this as a two-stage instruction (just like the hardware)
       
  2467 	err = aThreadSpecific
       
  2468 		? HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1)
       
  2469 		: HelpTestStep(iThreadID,startAddress,startAddress+2,EThumbMode,1, EFalse, processId);
       
  2470 	test(err == KErrNone);
       
  2471 
       
  2472 	err = aThreadSpecific
       
  2473 		? HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1)
       
  2474 		: HelpTestStep(iThreadID,startAddress+2,endAddress,EThumbMode,1, EFalse, processId);
       
  2475         }
       
  2476 	test(err == KErrNone);
       
  2477 
       
  2478 #endif // __MARM_ARMV5__
       
  2479 
       
  2480 	/*
       
  2481 	 * Test multiple-step of ARM code
       
  2482 	 */
       
  2483 	test.Next(_L("TestStep - ARM Multiple instruction step\n"));
       
  2484 
       
  2485 	startAddress = (TUint32)(&RMDebug_StepTest_ARM_Step_Multiple);
       
  2486 
       
  2487 	endAddress = (TUint32)(&RMDebug_StepTest_ARM_Step_Multiple_1);
       
  2488 
       
  2489 	err = aThreadSpecific
       
  2490 		? HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,5)
       
  2491 		: HelpTestStep(iThreadID,startAddress,endAddress,EArmMode,5, EFalse, processId);
       
  2492 	test(err == KErrNone);
       
  2493 	// stepping performance
       
  2494 	test.Next(_L("TestStep - Steps per second\n"));
       
  2495 
       
  2496 	// run until we reach RMDebug_StepTest_Count_1
       
  2497 	TBreakId stepBreakId;
       
  2498 	startAddress = (TUint32)(&RMDebug_StepTest_Count_1);
       
  2499 	endAddress = (TUint32)(&RMDebug_StepTest_Count_2);
       
  2500 
       
  2501 	err = aThreadSpecific
       
  2502 		? HelpTestStepSetBreak(stepBreakId,iThreadID,startAddress,EArmMode)
       
  2503 		: HelpTestStepSetBreak(stepBreakId,iThreadID,startAddress,EArmMode,EFalse,processId);
       
  2504 	test (err == KErrNone);
       
  2505 
       
  2506 	// wait until we hit the breakpoint
       
  2507 	TEventInfo info;
       
  2508 	err = HelpTestStepWaitForBreak(iFileName,info);
       
  2509 	test (err == KErrNone);
       
  2510 
       
  2511 	// Now clear the breakpoint
       
  2512 	err = iServSession.ClearBreak(stepBreakId);
       
  2513 	test(err == KErrNone);
       
  2514 
       
  2515 	if(aThreadSpecific)
       
  2516 		{
       
  2517 		// now step the code
       
  2518 		TInt stepsPerSecond = 0;
       
  2519 
       
  2520 		TUint32 stopTickCount = User::NTickCount() + HelpTicksPerSecond();
       
  2521 
       
  2522 		while (User::NTickCount() < stopTickCount)
       
  2523 			{
       
  2524 			err = iServSession.Step(iThreadID,1);
       
  2525 			test (err == KErrNone);
       
  2526 
       
  2527 			// we need to wait now until the step completes before asking for the next step
       
  2528 				{
       
  2529 				TEventInfo info;
       
  2530 				static TRequestStatus status;
       
  2531 
       
  2532 				TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  2533 
       
  2534 				iServSession.GetEvent(iFileName,status,infoPtr);
       
  2535 
       
  2536 				// Wait for notification of the breakpoint hit event
       
  2537 				User::WaitForRequest(status);
       
  2538 				test(status==KErrNone);
       
  2539 				}
       
  2540 
       
  2541 			// Update the count of steps
       
  2542 			stepsPerSecond += 1;
       
  2543 
       
  2544 			// Gone wrong if we do too many
       
  2545 			test(stepsPerSecond < 10000);
       
  2546 			}
       
  2547 
       
  2548 		iStepsPerSecond = stepsPerSecond;
       
  2549 		test(iStepsPerSecond != 0);
       
  2550 		}
       
  2551 
       
  2552 	// finally resume the thread
       
  2553 	err = iServSession.ResumeThread(iThreadID);
       
  2554 	test (err == KErrNone);
       
  2555 
       
  2556 	err = iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionIgnore);
       
  2557 	test (err == KErrNone);
       
  2558 
       
  2559 	if(!aThreadSpecific)
       
  2560 		{
       
  2561 		err = iServSession.SetEventAction(iFileName, EEventsProcessBreakPoint, EActionIgnore);
       
  2562 		test (err == KErrNone);
       
  2563 		}
       
  2564 
       
  2565 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2566 	}
       
  2567 
       
  2568 //----------------------------------------------------------------------------------------------
       
  2569 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0443
       
  2570 //! @SYMTestType
       
  2571 //! @SYMPREQ            PREQ1426
       
  2572 //! @SYMTestCaseDesc    Tests registration and occurrence of target thread event (in this case panic)
       
  2573 //! @SYMTestActions     Registers for a panic in the target thread, causes it, and catches the panic notification.
       
  2574 //!
       
  2575 //! @SYMTestExpectedResults KErrNone.
       
  2576 //! @SYMTestPriority        High
       
  2577 //! @SYMTestStatus          Implemented
       
  2578 //----------------------------------------------------------------------------------------------
       
  2579 
       
  2580 void CRunModeAgent::TestEvents()
       
  2581 	{
       
  2582 	TInt err = KErrNone;
       
  2583 
       
  2584 	test.Next(_L("TestEvents\n"));
       
  2585 
       
  2586 	TInt panicReason = 12345;
       
  2587 
       
  2588 	test.Printf(_L("Thread t_rmdebug.exe::DebugThread should panic with reason %d.\n"), panicReason);
       
  2589 
       
  2590 	//attach non-passively
       
  2591 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  2592 
       
  2593 	RThread threadToPanic;
       
  2594 	test(KErrNone == StartDebugThread(threadToPanic, _L("EventsThread")));
       
  2595 	TThreadId threadToPanicId = threadToPanic.Id();
       
  2596 	TEventInfo info;
       
  2597 
       
  2598 	// Set things up to wait for a thread kill event
       
  2599 	err = iServSession.SetEventAction(iFileName, EEventsKillThread, EActionContinue);
       
  2600 	test(err==KErrNone);
       
  2601 
       
  2602 	// Wait for an event to occur in this process - nothing should have happened yet.
       
  2603 	static TRequestStatus status;
       
  2604 
       
  2605 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  2606 
       
  2607 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  2608 
       
  2609 	// Test Request cancellation
       
  2610 	err = iServSession.CancelGetEvent(iFileName);
       
  2611 	test (err==KErrNone);
       
  2612 
       
  2613 	// Again wait for an event to occur in our process - we will provoke the
       
  2614 	// thread kill event by panic'ing the test thread.
       
  2615 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  2616 
       
  2617 	// Panic the debug thread to cause a thread kill event
       
  2618 	threadToPanic.Panic(_L("t_rmdebug panic thread test"), panicReason);
       
  2619 
       
  2620 	// Wait for notification of the Thread Kill event
       
  2621 	User::WaitForRequest(status);
       
  2622 	test(status==KErrNone);
       
  2623 
       
  2624 	// Check we are really recieving information about the panic
       
  2625 	test(info.iProcessIdValid);
       
  2626 	test(info.iThreadIdValid);
       
  2627 	test(info.iProcessId==RProcess().Id());
       
  2628 	test(info.iThreadId==threadToPanicId);
       
  2629 	test(info.iEventType==EEventsKillThread);
       
  2630 	test(info.iThreadKillInfo.iExitType==EExitPanic);
       
  2631 
       
  2632 	// Ignore other panic events
       
  2633 	err = iServSession.SetEventAction(iFileName, EEventsKillThread, EActionIgnore);
       
  2634 	test(err==KErrNone);
       
  2635 
       
  2636 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2637 	}
       
  2638 
       
  2639 //----------------------------------------------------------------------------------------------
       
  2640 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0444
       
  2641 //! @SYMTestType
       
  2642 //! @SYMPREQ            PREQ1426
       
  2643 //! @SYMTestCaseDesc    Tests registration and occurence of target thread events in separate process.
       
  2644 //! @SYMTestActions     Registers for a hardware exception and kill thread events, and receives them.
       
  2645 //!
       
  2646 //! @SYMTestExpectedResults KErrNone.
       
  2647 //! @SYMTestPriority        High
       
  2648 //! @SYMTestStatus          Implemented
       
  2649 //----------------------------------------------------------------------------------------------
       
  2650 void CRunModeAgent::TestEventsForExternalProcess()
       
  2651 	{
       
  2652 	test.Next(_L("TestEventsForExternalProcess\n"));
       
  2653 
       
  2654 	for(TInt main=0; main<3; main++)
       
  2655 		{
       
  2656 		for(TInt extra=0; extra<3; extra++)
       
  2657 			{
       
  2658 			TestEventsWithExtraThreads((TKernelEventAction)main, (TKernelEventAction)extra, 0);
       
  2659 			TestEventsWithExtraThreads((TKernelEventAction)main, (TKernelEventAction)extra, 2);
       
  2660 			}
       
  2661 		}
       
  2662 	}
       
  2663 
       
  2664 void CRunModeAgent::TestEventsWithExtraThreads(TKernelEventAction aActionMain, TKernelEventAction aActionExtra, TUint32 aExtraThreads)
       
  2665 	{
       
  2666 	const TInt KNumberOfTypes = 8;
       
  2667 	struct TEventStruct
       
  2668 		{
       
  2669 		public:
       
  2670 		TDebugFunctionType iDebugFunctionType;
       
  2671 		TEventType iEventType;
       
  2672 		};
       
  2673 
       
  2674 	TEventStruct type[KNumberOfTypes] =
       
  2675 		{
       
  2676 			{EStackOverflowFunction, EEventsHwExc},
       
  2677 			{EUserPanicFunction, EEventsKillThread},
       
  2678 			{EPrefetchAbortFunction, EEventsHwExc},
       
  2679 			{EDataAbortFunction, EEventsHwExc},
       
  2680 			{EUndefInstructionFunction, EEventsHwExc},
       
  2681 			{EDataReadErrorFunction, EEventsHwExc},
       
  2682 			{EDataWriteErrorFunction, EEventsHwExc},
       
  2683 			{EUserExceptionFunction, EEventsSwExc},
       
  2684 		};
       
  2685 
       
  2686 	for(TInt j=0; j<KNumberOfTypes; j++)
       
  2687 		{
       
  2688 		if( gUseDelay ) User::After(500000);
       
  2689 
       
  2690 		RDebug::Printf("CRunModeAgent::TestEventsWithExtraThreads type: %d, main action: %d, extra action: %d, extraThreads: %d", 
       
  2691 			j, (TUint32)aActionMain, (TUint32)aActionExtra, aExtraThreads);
       
  2692 
       
  2693 		// do this check as it seems to hard to do these cases with the current set up
       
  2694 		if(EEventsKillThread == type[j].iEventType)
       
  2695 			{
       
  2696 			if(EActionSuspend != aActionMain)
       
  2697 				{
       
  2698 				if(aActionMain != aActionExtra)
       
  2699 					{
       
  2700 					return;
       
  2701 					}
       
  2702 				}
       
  2703 			}
       
  2704 		// attach to KRMDebugTestApplication
       
  2705 		test(KErrNone == iServSession.AttachExecutable(KRMDebugTestApplication, EFalse));
       
  2706 
       
  2707 		// Set things up to wait for the expected exception in KRMDebugTestApplication
       
  2708 		test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication, type[j].iEventType, aActionMain));
       
  2709 
       
  2710 		if(EActionSuspend != aActionMain)
       
  2711 			{
       
  2712 			test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication, EEventsKillThread, aActionExtra));
       
  2713 			}
       
  2714 
       
  2715 		// declare a TRequestStatus object for asynchronous calls
       
  2716 		TRequestStatus status;
       
  2717 
       
  2718 		TEventInfo info;
       
  2719 		TPtr8 infoBuffer = TPtr8((TUint8*)&info,0,sizeof(TEventInfo));
       
  2720 		if(EActionIgnore != aActionMain)
       
  2721 			{
       
  2722 			iServSession.GetEvent(KRMDebugTestApplication(), status, infoBuffer);
       
  2723 			}
       
  2724 
       
  2725 		// launch the target process to trigger the expected exception
       
  2726 		RProcess targetProcess;
       
  2727 		test(KErrNone == LaunchProcess(targetProcess, KRMDebugTestApplication(), type[j].iDebugFunctionType, 0, aExtraThreads));
       
  2728 		TProcessId processId(targetProcess.Id());
       
  2729 		targetProcess.Close();
       
  2730 
       
  2731 		if(EActionIgnore != aActionMain)
       
  2732 			{
       
  2733 			// wait for notification of the exception
       
  2734 			User::WaitForRequest(status);
       
  2735 			test(KErrNone == status.Int());
       
  2736 
       
  2737 			// check that this is the event we were expecting
       
  2738 			test(info.iProcessIdValid);
       
  2739 			test(info.iThreadIdValid);
       
  2740 			test(info.iProcessId==processId);
       
  2741 			test(info.iEventType==type[j].iEventType);
       
  2742 			}
       
  2743 
       
  2744 		if(EActionSuspend == aActionMain)
       
  2745 			{
       
  2746 			//RDebug::Printf("CRunModeAgent::TestEventsWithExtraThreads EActionSuspend == aActionMain, j=%d", j);
       
  2747 			// read the thread list, partly to check the call works, and partly to check the thread still exists
       
  2748 			test(ThreadExistsForProcess(info.iThreadId, info.iProcessId));
       
  2749 
       
  2750 			// register to catch all the thread kills which will occur
       
  2751 			test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication, EEventsKillThread, aActionExtra));
       
  2752 			// we specified EActionSuspend earlier so need to call resume on this thread
       
  2753 			test(KErrNone == iServSession.ResumeThread(info.iThreadId));
       
  2754 			}
       
  2755 
       
  2756 		// find out how many threads there are in the process and catch all the thread kill events,
       
  2757 		// the number of kill thread events should correspond to the number of extra threads launched,
       
  2758 		// plus one if the main thread panicked with a Sw/Hw exception
       
  2759 		if(EActionIgnore != aActionExtra)
       
  2760 			{
       
  2761 			TInt dyingThreads = aExtraThreads + ( (type[j].iEventType != EEventsKillThread) ? 1 : 0);
       
  2762 			for(TInt k=0; k<dyingThreads; k++)
       
  2763 				{
       
  2764 				//RDebug::Printf("CRunModeAgent::TestEventsWithExtraThreads dyingThreads, k=%d, j=%d", k,j);
       
  2765 				iServSession.GetEvent(KRMDebugTestApplication(), status, infoBuffer);
       
  2766 
       
  2767 				// wait for notification of the kill thread
       
  2768 				User::WaitForRequest(status);
       
  2769 				test(KErrNone == status.Int());
       
  2770 
       
  2771 				// check that this is the event we were expecting
       
  2772 				test(info.iProcessIdValid);
       
  2773 				test(info.iThreadIdValid);
       
  2774 				test(info.iProcessId==processId);
       
  2775 				test(info.iEventType==EEventsKillThread);
       
  2776 				if(EActionSuspend == aActionExtra)
       
  2777 					{
       
  2778 					// do some calls to check listings work ok at this stage
       
  2779 					test(ProcessExists(info.iProcessId));
       
  2780 					test(ThreadExistsForProcess(info.iThreadId, info.iProcessId));
       
  2781 					// we specified EActionSuspend earlier so need to call resume on this thread
       
  2782 					test(KErrNone == iServSession.ResumeThread(info.iThreadId));
       
  2783 					}
       
  2784 				}
       
  2785 			}
       
  2786 
       
  2787 		if( gUseDelay ) User::After(500000);
       
  2788 
       
  2789 		// reset the thread kill event
       
  2790 		test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication(), EEventsKillThread, EActionIgnore));
       
  2791 
       
  2792 		// reset events for KRMDebugTestApplication
       
  2793 		test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication(), type[j].iEventType, EActionIgnore));
       
  2794 
       
  2795 		// finished debugging KRMDebugTestApplication so detach
       
  2796 		test(KErrNone == iServSession.DetachExecutable(KRMDebugTestApplication()));
       
  2797 	
       
  2798 		// want to validate that the process has really exited, i.e. we're not accidentally keeping a handle to it...
       
  2799 		TInt waitCount = 10;
       
  2800 		while((waitCount-- > 0) && ProcessExists(processId))
       
  2801 			{
       
  2802 			/* Wait a little while and try again, just in case the process is still being removed.
       
  2803 			This can happen on a very busy system or when a popup for the events is still active
       
  2804 			*/
       
  2805 			RDebug::Printf("CRunModeAgent::TestEventsWithExtraThreads. ProcessExists(id=%d), waiting count exit=%d", 
       
  2806 				I64LOW(processId), waitCount);
       
  2807 			User::After(50000);
       
  2808 			}
       
  2809 		test(!ProcessExists(processId));
       
  2810 		}
       
  2811 	}
       
  2812 
       
  2813 // helper function to check whether a thread with id aThreadId exists in the process with id aProcessId
       
  2814 TBool CRunModeAgent::ThreadExistsForProcess(const TThreadId aThreadId, const TProcessId aProcessId)
       
  2815 	{
       
  2816 	RThread lThread;
       
  2817 	TInt ret = lThread.Open( aThreadId.Id() );
       
  2818 
       
  2819 	if( ret != KErrNone )
       
  2820 		{
       
  2821 		RDebug::Printf("ThreadExistsForProcess: thread id=%d opening returned %d",
       
  2822 			I64LOW( aThreadId.Id() ), ret );
       
  2823 		lThread.Close();
       
  2824 		return EFalse;
       
  2825 		}
       
  2826 
       
  2827 	RProcess lProcess;
       
  2828 	ret = lThread.Process( lProcess );
       
  2829 
       
  2830 	lThread.Close();
       
  2831 
       
  2832 	if( ret != KErrNone )
       
  2833 		{
       
  2834 		RDebug::Printf("ThreadExistsForProcess: proc opening returned %d", ret );
       
  2835 		ret = KErrNotFound;
       
  2836 		}
       
  2837 	else if( lProcess.Id() != aProcessId )
       
  2838 		{
       
  2839 		RDebug::Printf("ThreadExistsForProcess: lProcess.Id()(%d)!= aProcessId(%d)",
       
  2840 				I64LOW(lProcess.Id().Id()), I64LOW(aProcessId.Id()));
       
  2841 		ret = KErrNotFound;
       
  2842 		}
       
  2843 
       
  2844 	lProcess.Close();
       
  2845 	
       
  2846 	return ( ret == KErrNone );
       
  2847 	}
       
  2848 
       
  2849 // helper function to check whether a process with id aProcessId exists
       
  2850 TBool CRunModeAgent::ProcessExists(const TProcessId aProcessId)
       
  2851 	{
       
  2852 	TUint32 size;
       
  2853 	RBuf8 buffer;
       
  2854 	test(KErrNone == buffer.Create(1024));
       
  2855 	TInt err = iServSession.GetList(EProcesses, buffer, size);
       
  2856 	while(KErrTooBig == err)
       
  2857 		{
       
  2858 		size*=2;
       
  2859 		test(size<=47*1024); // 256 TProcessListEntrys is about 46KB. (256 is max num processes)
       
  2860 		test(KErrNone == buffer.ReAlloc(size));
       
  2861 		err = iServSession.GetList(EProcesses, buffer, size);
       
  2862 		}
       
  2863 	test(KErrNone == err);
       
  2864 
       
  2865 	//look through the buffer and check if the target debug thread is there
       
  2866 	TUint8* ptr = (TUint8*)buffer.Ptr();
       
  2867 	const TUint8* ptrEnd = ptr + size;
       
  2868 	while(ptr < ptrEnd)
       
  2869 		{
       
  2870 		TProcessListEntry& entry = *(TProcessListEntry*)ptr;
       
  2871 		if(aProcessId.Id() == entry.iProcessId)
       
  2872 			{
       
  2873 			buffer.Close();
       
  2874 			return ETrue;
       
  2875 			}
       
  2876 		ptr += Align4(entry.GetSize());
       
  2877 		}
       
  2878 	buffer.Close();
       
  2879 	return EFalse;
       
  2880 	}
       
  2881 
       
  2882 //----------------------------------------------------------------------------------------------
       
  2883 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0445
       
  2884 //! @SYMTestType
       
  2885 //! @SYMPREQ            PREQ1426
       
  2886 //! @SYMTestCaseDesc    Tests basic debug functions work on demand-paged target threads
       
  2887 //! @SYMTestActions     Checks it can r/w memory, set breakpoints etc in a demand paged target.
       
  2888 //!
       
  2889 //! @SYMTestExpectedResults KErrNone.
       
  2890 //! @SYMTestPriority        High
       
  2891 //! @SYMTestStatus          Implemented
       
  2892 //----------------------------------------------------------------------------------------------
       
  2893 
       
  2894 void CRunModeAgent::TestDemandPaging(void)
       
  2895 	{
       
  2896 	test.Next(_L("TestDemandPaging\n"));
       
  2897 
       
  2898 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  2899 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  2900 
       
  2901 	// get the address of a function in code that will be paged in
       
  2902 	TUint32 address = (TUint32)(&RMDebugDemandPagingTest);
       
  2903 	const TUint32 armInstSize = 4;
       
  2904 
       
  2905 	// read the memory at &RMDebugDemandPagingTest to check that reading memory in demand paged code works
       
  2906 	TUint32 demandPagedInst = 0;
       
  2907 	TPtr8 demandPagedInstBuf((TUint8*)&demandPagedInst, armInstSize);
       
  2908 	test(KErrNone == iServSession.ReadMemory(iThreadID, address, armInstSize, demandPagedInstBuf, EAccess32, EEndLE8));
       
  2909 
       
  2910 	// this is the MOVS instruction that we expect to find in RMDebugDemandPagingTest
       
  2911 	TUint32 expectedDemandPagedInst = 0xe1b02000;
       
  2912 
       
  2913 	// check that the instruction we read is as expected
       
  2914 	test(demandPagedInst == expectedDemandPagedInst);
       
  2915 
       
  2916 	// set event action for break points
       
  2917 	test(KErrNone == iServSession.SetEventAction(RProcess().FileName(), EEventsBreakPoint, EActionContinue));
       
  2918 
       
  2919 	// set an arm breakpoint on RMDebugDemandPagingTest
       
  2920 	TBreakId armBreakId = 0;
       
  2921 	test(KErrNone == iServSession.SetBreak(armBreakId, iThreadID, address, EArmMode));
       
  2922 
       
  2923 	// Ensure that after setting the breakpoint the memory read returns the correct value
       
  2924 	TUint32 demandPagedInstWithBreakPoint = 0;
       
  2925 	TPtr8 spinForeverInstWithBreakPointBuf((TUint8*)&demandPagedInstWithBreakPoint, armInstSize);
       
  2926 	test(KErrNone == iServSession.ReadMemory(iThreadID, address, armInstSize, spinForeverInstWithBreakPointBuf, EAccess32, EEndLE8));
       
  2927 	test(demandPagedInst == demandPagedInstWithBreakPoint);
       
  2928 
       
  2929 	// switch the target thread to run the demand paging function
       
  2930 	test(KErrNone == SwitchTestFunction(EDemandPagingFunction));
       
  2931 
       
  2932 	// set up event watcher to catch breakpoint being hit in demand paged code
       
  2933 	TEventInfo info;
       
  2934 	static TRequestStatus status;
       
  2935 	TPtr8 infoPtr((TUint8*)&info,sizeof(TEventInfo));
       
  2936 	iServSession.GetEvent(RProcess().FileName(), status, infoPtr);
       
  2937 
       
  2938 	// resume the thread
       
  2939 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  2940 	// wait for notification of the breakpoint hit event
       
  2941 	User::WaitForRequest(status);
       
  2942 	test(status==KErrNone);
       
  2943 
       
  2944 	// info should now be filled with the details
       
  2945 	test(info.iProcessIdValid);
       
  2946 	test(info.iThreadIdValid);
       
  2947 	test(info.iEventType == EEventsBreakPoint);
       
  2948 	test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address);
       
  2949 
       
  2950 	// remove the break point and resume the thread
       
  2951 	test(KErrNone == iServSession.ClearBreak(armBreakId));
       
  2952 
       
  2953 	// switch the target thread to run the default function
       
  2954 	test(KErrNone == SwitchTestFunction(EDefaultFunction));
       
  2955 
       
  2956 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  2957 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  2958 	}
       
  2959 
       
  2960 // Names of some test programs used for testing security
       
  2961 _LIT(KRMDebugSecurity0FileName,"z:\\sys\\bin\\t_rmdebug_security0.exe"); // Debuggable
       
  2962 _LIT(KRMDebugSecurity1FileName,"z:\\sys\\bin\\t_rmdebug_security1.exe"); // Not debuggable
       
  2963 
       
  2964 #if defined (NO_DEBUGTOKEN) || defined (SOMECAPS_DEBUGTOKEN) || defined(FEWCAPS_DEBUGTOKEN)
       
  2965 _LIT(KRMDebugSecurity2FileName,"z:\\sys\\bin\\t_rmdebug_security2.exe"); // AllFiles
       
  2966 #endif
       
  2967 
       
  2968 _LIT(KRMDebugSecurity3FileName,"z:\\sys\\bin\\t_rmdebug_security3.exe"); // TCB AllFiles
       
  2969 
       
  2970 // include the test header file here
       
  2971 #include "rm_debug_kerneldriver.h"
       
  2972 
       
  2973 //----------------------------------------------------------------------------------------------
       
  2974 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0446
       
  2975 //! @SYMTestType
       
  2976 //! @SYMPREQ            PREQ1426
       
  2977 //! @SYMTestCaseDesc    Tests Debug Device Driver is locked to the SID of the Debug Security Svr.
       
  2978 //! @SYMTestActions     Loads rm-debug.ldd and tries to open a handle to it. This should fail.
       
  2979 //!
       
  2980 //! @SYMTestExpectedResults KErrPermissionDenied.
       
  2981 //! @SYMTestPriority        High
       
  2982 //! @SYMTestStatus          Implemented
       
  2983 //----------------------------------------------------------------------------------------------
       
  2984 
       
  2985 void CRunModeAgent::TestDriverSecurity(void)
       
  2986 	{
       
  2987 	test.Next(_L("TestDriverSecurity\n"));
       
  2988 
       
  2989 	RRM_DebugDriver kernelDriver;
       
  2990 
       
  2991 	// Load the debug device driver
       
  2992 	TInt err = User::LoadLogicalDevice( KDebugDriverFileName );
       
  2993 	test((KErrNone == err) || (KErrAlreadyExists == err));
       
  2994 
       
  2995 	// we were allowed to load the driver, or its already loaded.
       
  2996 
       
  2997 	// Try to open a handle to the driver - this should return KErrPermissionDenied as we don't have the DSS SID
       
  2998 	TRM_DebugDriverInfo driverInfo;
       
  2999 	driverInfo.iUserLibraryEnd = 0;
       
  3000 	err = kernelDriver.Open(driverInfo);
       
  3001 	test((err == KErrInUse) || (err == KErrPermissionDenied));
       
  3002 
       
  3003 	}
       
  3004 
       
  3005 //----------------------------------------------------------------------------------------------
       
  3006 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0447
       
  3007 //! @SYMTestType
       
  3008 //! @SYMPREQ            PREQ1426
       
  3009 //! @SYMTestCaseDesc    Tests Debug driver can only be access via the DSS. Also tests DSS cannot
       
  3010 //!						be subverted. Tests functionality of two representative OEM Debug Tokens.
       
  3011 //! @SYMTestActions     Tries to open rm_debug.ldd (should fail). Tries to debug various processes
       
  3012 //!						(only debuggable one should succeed). Checks that DSS behaves correctly
       
  3013 //!						when different versions are passed in to Connect().
       
  3014 //!
       
  3015 //! @SYMTestExpectedResults KErrPermissionDenied.
       
  3016 //! @SYMTestPriority        High
       
  3017 //! @SYMTestStatus          Implemented
       
  3018 //----------------------------------------------------------------------------------------------
       
  3019 
       
  3020 void CRunModeAgent::TestSecurity(void)
       
  3021 	{
       
  3022 	// Things to test
       
  3023 	//
       
  3024 	// try to use debug driver directly ( should have the wrong UID/SID value!)
       
  3025 	test.Next(_L("TestSecurity - Bypass Debug Security Server to Debug Device Driver - DSS running\n"));
       
  3026 
       
  3027 	// Things to test
       
  3028 	//
       
  3029 	// Load the debug device driver
       
  3030 	RRM_DebugDriver kernelDriver;
       
  3031 	TInt err = User::LoadLogicalDevice( KDebugDriverFileName );
       
  3032 	test((KErrNone == err) || (KErrAlreadyExists == err));
       
  3033 
       
  3034 	// we were allowed to load the driver, or its already loaded.
       
  3035 
       
  3036 	// Try to open handle a to the driver - this should return KErrPermission/KErrInUse as we don't have the DSS SID
       
  3037 	// and we expect the DSS to already be using it.
       
  3038 	TRM_DebugDriverInfo driverInfo;
       
  3039 	driverInfo.iUserLibraryEnd = 0;
       
  3040 	err = kernelDriver.Open(driverInfo);
       
  3041 	test(err == KErrInUse);
       
  3042 
       
  3043 	// Try requesting an unsupported version of DSS
       
  3044 	test.Next(_L("TestSecurity - requesting unsupported versions of DSS\n"));
       
  3045 	RSecuritySvrSession dss;
       
  3046 	err = dss.Connect(TVersion(999999, 0, 0));
       
  3047 	test(err == KErrNotSupported); // Prior to DEF142018 this would crash, causing a KErrServerTerminated
       
  3048 	err = dss.Connect(TVersion(KDebugServMajorVersionNumber, 999999, 0));
       
  3049 	test(err == KErrNotSupported); // Explicitly asking for a minor version should give KErrNotSupported too if it's newer than what's running.
       
  3050 	err = dss.Connect(TVersion(KDebugServMajorVersionNumber, 0, 0));
       
  3051 	test(err == KErrNone); // But the correct major version and no explicit minor version should always succeed
       
  3052 	dss.Close();
       
  3053 	
       
  3054 	//
       
  3055 	// Attach to the Debug Security Server (passive)
       
  3056 	//
       
  3057 	test.Next(_L("TestSecurity - Attach to the Debug Security Server (passive)\n"));
       
  3058 
       
  3059 	_LIT(KSecurityServerProcessName, "z:\\sys\\bin\\rm_debug_svr.exe");
       
  3060 
       
  3061 	test(KErrPermissionDenied == iServSession.AttachExecutable(KSecurityServerProcessName, ETrue));
       
  3062 
       
  3063 	//
       
  3064 	// Attach to the Debug Security Server (active)
       
  3065 	//
       
  3066 	test.Next(_L("TestSecurity - Attach to the Debug Security Server (active)\n"));
       
  3067 
       
  3068 	test(KErrPermissionDenied == iServSession.AttachExecutable(KSecurityServerProcessName, EFalse));
       
  3069 
       
  3070 	//
       
  3071 	// Attach to Process 0
       
  3072 	//
       
  3073 	// Target: Debuggable
       
  3074 	//
       
  3075 	test.Next(_L("TestSecurity - Attach to test process 0\n"));
       
  3076 
       
  3077 	// Agent can debug the target app as it is marked debuggable - ie capabilities are ignored)
       
  3078 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity0FileName,ETrue);
       
  3079 
       
  3080 	//
       
  3081 	// Attach to Process - 1
       
  3082 	//
       
  3083 	// Target: Non-debuggable for ordinary debug agent, debuggable for OEM/OEM2 token authorised agent
       
  3084 	//
       
  3085 	// Note: This target app has no PlatSec capabilities
       
  3086 	//
       
  3087 	// Agent cannot debug the app unless it has an OEM/OEM2 Debug Token
       
  3088 
       
  3089 
       
  3090 #ifdef NO_DEBUGTOKEN
       
  3091 	test.Next(_L("TestSecurity NO_DEBUGTOKEN - Attach to test process 1\n"));
       
  3092 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity1FileName,EFalse);
       
  3093 #endif
       
  3094 
       
  3095 #ifdef SOMECAPS_DEBUGTOKEN
       
  3096 	test.Next(_L("TestSecurity SOMECAPS_DEBUGTOKEN - Attach to test process 1\n"));
       
  3097 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity1FileName,ETrue);
       
  3098 #endif
       
  3099 
       
  3100 #ifdef FEWCAPS_DEBUGTOKEN
       
  3101 	test.Next(_L("TestSecurity FEWCAPS_DEBUGTOKEN - Attach to test process 1\n"));
       
  3102 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity1FileName,ETrue);
       
  3103 #endif
       
  3104 
       
  3105 	//
       
  3106 	// Attach to Process - 2
       
  3107 	//
       
  3108 	// Target: Non-debuggable for ordinary debug agent, non-debuggable for OEM2 authorised agent (insufficient caps)
       
  3109 	//
       
  3110 	// Note: This target app has AllFiles capability
       
  3111 	//
       
  3112 	// Agent cannot debug the app unless it has an OEM Debug Token
       
  3113 
       
  3114 
       
  3115 #ifdef NO_DEBUGTOKEN
       
  3116 	test.Next(_L("TestSecurity NO_DEBUGTOKEN - Attach to test process 2\n"));
       
  3117 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity2FileName,EFalse);
       
  3118 #endif
       
  3119 
       
  3120 #ifdef SOMECAPS_DEBUGTOKEN
       
  3121 	test.Next(_L("TestSecurity SOMECAPS_DEBUGTOKEN - Attach to test process 2\n"));
       
  3122 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity2FileName,ETrue);
       
  3123 #endif
       
  3124 
       
  3125 #ifdef FEWCAPS_DEBUGTOKEN
       
  3126 	test.Next(_L("TestSecurity FEWCAPS_DEBUGTOKEN - Attach to test process 2\n"));
       
  3127 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity2FileName,EFalse);
       
  3128 #endif
       
  3129 
       
  3130 	//
       
  3131 	// Attach to Process - 3
       
  3132 	//
       
  3133 	// Target: Non-debuggable for ordinary debug agent, non-debuggable for OEM authorised agent (insufficient caps)
       
  3134 	//
       
  3135 	// Note: This target app has AllFiles and TCB and NetworkControl capabilities
       
  3136 	//
       
  3137 
       
  3138 #if  defined (NO_DEBUGTOKEN)  || defined (SOMECAPS_DEBUGTOKEN) || defined (FEWCAPS_DEBUGTOKEN)
       
  3139 	test.Next(_L("TestSecurity - Attach to test process 3 : Should not be able to debug it\n"));
       
  3140 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity3FileName,EFalse);
       
  3141 #else
       
  3142 	test.Next(_L("TestSecurity - Attach to test process 3 : Should be able to debug it\n"));
       
  3143 	HelpTestSecurityAttachDetachExecutable(KRMDebugSecurity3FileName,ETrue);
       
  3144 #endif
       
  3145 	}
       
  3146 
       
  3147 //----------------------------------------------------------------------------------------------
       
  3148 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0543
       
  3149 //! @SYMTestType
       
  3150 //! @SYMPREQ            PREQ1426
       
  3151 //! @SYMTestCaseDesc    Validates that a dll can be built which #include's the rm_debug_api.h header, i.e. rm_debug_api.h contains no static data.
       
  3152 //! @SYMTestActions     Calls a dummy function in t_rmdebug_dll.dll which implies the dll has been built correctly.
       
  3153 //!
       
  3154 //! @SYMTestExpectedResults KErrNone.
       
  3155 //! @SYMTestPriority        High
       
  3156 //! @SYMTestStatus          Implemented
       
  3157 //----------------------------------------------------------------------------------------------
       
  3158 void CRunModeAgent::TestDllUsage(void)
       
  3159 	{
       
  3160 	test.Next(_L("TestDllUsage\n"));
       
  3161 	test(KUidDebugSecurityServer == GetDSSUid());
       
  3162 	}
       
  3163 
       
  3164 //----------------------------------------------------------------------------------------------
       
  3165 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0812
       
  3166 //! @SYMTestType
       
  3167 //! @SYMPREQ            PREQ1700
       
  3168 //! @SYMTestCaseDesc    Writes a known data to the crash flash and validates the data written
       
  3169 //!						using the read operation and finally erase the data. In the absence
       
  3170 //!						of an OEM debug token, access to the crash partition should not be allowed
       
  3171 //! @SYMTestActions     Invoke the flash write method in DSS and call the read method in DSS
       
  3172 //!						to validate the data is written correctly and then erase the written area
       
  3173 //!
       
  3174 //! @SYMTestExpectedResults KErrNone.
       
  3175 //! @SYMTestPriority        High
       
  3176 //! @SYMTestStatus          Implemented
       
  3177 //----------------------------------------------------------------------------------------------
       
  3178 void CRunModeAgent::TestCrashFlash(void)
       
  3179 	{
       
  3180 #if  defined (NO_DEBUGTOKEN)  || defined (FEWCAPS_DEBUGTOKEN)
       
  3181 
       
  3182 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-006 Testing We cannot Erase the Crash Flash with insufficient privileges"));
       
  3183 
       
  3184 	TUint32 size = 0;
       
  3185 	TInt err = iServSession.EraseCrashLog(0, 1);
       
  3186 	test(KErrPermissionDenied == err);
       
  3187 
       
  3188 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-005 Testing We can't Write to the Crash Flash with insufficient privileges"));
       
  3189 
       
  3190 	err = iServSession.WriteCrashConfig(0, KCrashDummyData, size);
       
  3191 	test(KErrPermissionDenied == err);
       
  3192 	test(size == 0);
       
  3193 
       
  3194 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-008 Testing We can't Read from the Crash Flash with insufficient privileges"));
       
  3195 
       
  3196 	TUint32 readSize = 0x10;
       
  3197 	RBuf8 buf;
       
  3198 	buf.CleanupClosePushL();
       
  3199 	err = buf.Create(readSize);
       
  3200 
       
  3201 	test(err == KErrNone);
       
  3202 
       
  3203 	err = iServSession.ReadCrashLog(0, buf, readSize);
       
  3204 	test(KErrPermissionDenied == err);
       
  3205 
       
  3206 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-004 Testing Writing To an invalid location"));
       
  3207 
       
  3208 	TUint32 writeSize = 0;
       
  3209 	err = iServSession.WriteCrashConfig(0xFFFFFFFF, KCrashDummyData, writeSize);
       
  3210 
       
  3211 	test(err == KErrPermissionDenied);
       
  3212 
       
  3213 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-003 Testing Reading from an invalid location"));
       
  3214 
       
  3215 	buf.FillZ();
       
  3216 	err = iServSession.ReadCrashLog(0, buf, writeSize);
       
  3217 
       
  3218 	test(err == KErrPermissionDenied);
       
  3219 
       
  3220 	CleanupStack::PopAndDestroy(&buf);
       
  3221 
       
  3222 #endif
       
  3223 
       
  3224 #ifdef SOMECAPS_DEBUGTOKEN
       
  3225 
       
  3226 	TInt err = KErrNone;
       
  3227 
       
  3228 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-007 Testing We can Erase the Crash Flash with sufficient privileges"));
       
  3229 
       
  3230 	err = iServSession.EraseCrashLog(0, 1);
       
  3231 
       
  3232 	// For platforms where NAND flash is not currently supported we get a KErrNotSupported - this is still a pass
       
  3233 	if (KErrNotSupported == err)
       
  3234 		{
       
  3235 		test.Printf(_L("Nand flash not supported - continue"));
       
  3236 		return;
       
  3237 		}
       
  3238 
       
  3239  	//For platforms without a flash partition we get KErrNotFound - this is still a pass
       
  3240  	if(KErrNotFound == err)
       
  3241  		{
       
  3242  		test.Printf(_L("Platform has no flash partition - continue"));
       
  3243  		return;
       
  3244  		}
       
  3245 
       
  3246 	test(KErrNone == err);
       
  3247 
       
  3248 	//Read back the start of the block to make sure its 0xFFFFFFFF
       
  3249 	const TUint numBytesToCheck = 0x80;  //We dont know the block size
       
  3250 	TBuf8<numBytesToCheck> eraseCheck;
       
  3251 	eraseCheck.SetLength(numBytesToCheck);
       
  3252 
       
  3253 	err = iServSession.ReadCrashLog(0, eraseCheck, numBytesToCheck);
       
  3254 	test(err == KErrNone);
       
  3255 
       
  3256 	TBool dataIsOk = ETrue;
       
  3257 	for(TUint cnt = 0; cnt < numBytesToCheck; cnt++)
       
  3258 		{
       
  3259 		if(eraseCheck[cnt] != 0xFF)
       
  3260 			{
       
  3261 			dataIsOk = EFalse;
       
  3262 			}
       
  3263 		}
       
  3264 
       
  3265 	test(dataIsOk);
       
  3266 
       
  3267 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-002 Testing We can Write to the Crash Flash with sufficient privileges"));
       
  3268 
       
  3269 	TUint32 writeSize = 0;
       
  3270 	err = iServSession.WriteCrashConfig(0, KCrashDummyData, writeSize);
       
  3271 
       
  3272 	test(writeSize == KCrashDummyData().Length());
       
  3273 
       
  3274 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-001 Testing We can Read from the Crash Flash with sufficient privileges"));
       
  3275 
       
  3276 	RBuf8 buf;
       
  3277 	buf.CleanupClosePushL();
       
  3278 	err = buf.Create(writeSize);
       
  3279 
       
  3280 	test(err == KErrNone);
       
  3281 
       
  3282 	buf.FillZ();
       
  3283 
       
  3284 	err = iServSession.ReadCrashLog(0, buf, writeSize);
       
  3285 
       
  3286 	test(0 == buf.Compare(KCrashDummyData));
       
  3287 
       
  3288 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-004 Testing Writing To an invalid location"));
       
  3289 
       
  3290 	writeSize = 0;
       
  3291 	err = iServSession.WriteCrashConfig(0xFFFFFFFF, KCrashDummyData, writeSize);
       
  3292 
       
  3293 	test(err == KErrArgument);
       
  3294 
       
  3295 	test.Next(_L("@SYMTestCaseID:DT-debug-securityserver-003 Testing Reading from an invalid location"));
       
  3296 
       
  3297 	buf.FillZ();
       
  3298 	err = iServSession.ReadCrashLog(0xFFFFFFFF, buf, writeSize);
       
  3299 
       
  3300 	test(err == KErrArgument);
       
  3301 
       
  3302 	CleanupStack::PopAndDestroy(&buf);
       
  3303 
       
  3304 #endif
       
  3305 	}
       
  3306 //----------------------------------------------------------------------------------------------
       
  3307 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0735
       
  3308 //! @SYMTestType
       
  3309 //! @SYMPREQ            PREQ1426
       
  3310 //! @SYMTestCaseDesc    Tests the Kill Process functionality. Only can kill a debuggable process.
       
  3311 //! @SYMTestActions     Launches a debuggable and non-debuggable process and tries to kill both.
       
  3312 //!
       
  3313 //! @SYMTestExpectedResults KErrNone.
       
  3314 //! @SYMTestPriority        High
       
  3315 //! @SYMTestStatus          Implemented
       
  3316 //----------------------------------------------------------------------------------------------
       
  3317 void CRunModeAgent::TestKillProcess(void)
       
  3318 	{
       
  3319 	test.Next(_L("TestKillProcess\n"));
       
  3320 
       
  3321 	// Kill a debuggable process
       
  3322 
       
  3323 	// check that killing a process is supported
       
  3324 	TTag tag = GetTag(ETagHeaderIdKillObjects, EFunctionalityKillProcess);
       
  3325 	test(tag.iValue);
       
  3326 	// check that killing a thread is not supported
       
  3327 	tag = GetTag(ETagHeaderIdKillObjects, EFunctionalityKillThread);
       
  3328 	test(!tag.iValue);
       
  3329 
       
  3330 	// attach first!
       
  3331 	TInt err = iServSession.AttachExecutable(KRMDebugTestApplication, EFalse /* Active */);
       
  3332 	test(err == KErrNone);
       
  3333 
       
  3334 	// first launch a debuggable process
       
  3335 	RProcess process;
       
  3336 	err = LaunchProcess(process, KRMDebugTestApplication(),ESpinForever, 0, 0);
       
  3337 	test (err == KErrNone);
       
  3338 
       
  3339 	// try to find the process in the list
       
  3340 _LIT(KRMDebugAppName, "t_rmdebug_app");
       
  3341 
       
  3342 	TBool found = ProcessExists(KRMDebugAppName);
       
  3343 	test (found);
       
  3344 
       
  3345 	TInt processId = process.Id();
       
  3346 	process.Close();
       
  3347 
       
  3348 	// program now running, so try to kill it
       
  3349 	err = iServSession.KillProcess(processId, 0 /* kill reason */);
       
  3350 	test(err == KErrNone);
       
  3351 
       
  3352 	User::After(2000000);	// should die within two seconds.
       
  3353 
       
  3354 	// can we still find it? Should be gone
       
  3355 	found = ProcessExists(KRMDebugAppName);
       
  3356 	test (!found);
       
  3357 
       
  3358 	// release the program again.
       
  3359 	err = iServSession.DetachExecutable(KRMDebugTestApplication);
       
  3360 	test(err == KErrNone);
       
  3361 
       
  3362 	// Try to kill a non-debuggable process and fail.
       
  3363 
       
  3364 	// first launch a non-debuggable process
       
  3365 	RProcess process2;
       
  3366 	err = LaunchProcess(process2, KRMDebugSecurity1FileName(),ESpinForever, 0, 0);
       
  3367 	test (err == KErrNone);
       
  3368 
       
  3369 	// try to find the process in the list
       
  3370 _LIT(KRMDebugAppName2, "t_rmdebug_security1");
       
  3371 
       
  3372 	TBool found2 = ProcessExists(KRMDebugAppName2);
       
  3373 	test (found2);
       
  3374 
       
  3375 	TInt process2Id = process2.Id();
       
  3376 	process2.Close();
       
  3377 
       
  3378 	// program now running, so try to kill it
       
  3379 	err = iServSession.KillProcess(process2Id, 0 /* kill reason */);
       
  3380 	test(err == KErrPermissionDenied);
       
  3381 
       
  3382 	User::After(2000000);	// should die within two seconds if it is going to die.
       
  3383 
       
  3384 	// can we still find it? Should be still around!
       
  3385 	found2 = ProcessExists(KRMDebugAppName2);
       
  3386 	test (found2);
       
  3387 
       
  3388 	}
       
  3389 
       
  3390 //----------------------------------------------------------------------------------------------
       
  3391 //! @SYMTestCaseID      KBase-T-RMDEBUG2-1388
       
  3392 //! @SYMTestType
       
  3393 //! @SYMPREQ            PREQ1426
       
  3394 //! @SYMTestCaseDesc    Tests the correct operation of the AddProcess and Remove Process
       
  3395 //! @SYMTestActions     1. Registers for AddProcess and Remove Process events
       
  3396 //!                     2. Starts a test process z:\sys\bin\t_rmdebug_security0.exe
       
  3397 //!                     3. Wait for the AddProcess event to be reported
       
  3398 //!                     4. Kill the newly started test process
       
  3399 //!                     5. Wait for the RemoveProcess event to be reported
       
  3400 //!                     6. Tell the DSS it is no longer interested in AddProcess and RemoveProcess events
       
  3401 //!
       
  3402 //! @SYMTestExpectedResults KErrNone.
       
  3403 //! @SYMTestPriority        High
       
  3404 //! @SYMTestStatus          Implemented
       
  3405 //----------------------------------------------------------------------------------------------
       
  3406 
       
  3407 void CRunModeAgent::TestAddRemoveProcessEvents()
       
  3408 	{
       
  3409 	test.Next(_L("TestAddRemoveProcessEvents\n"));
       
  3410 
       
  3411 	// attach to a process (e.g. one of the simple security test programs)
       
  3412 	// launch the security program
       
  3413 	// wait for the add event
       
  3414 	// continue the program.
       
  3415 	// wait for the remove event
       
  3416 	// detach process
       
  3417 
       
  3418 	test(KErrNone == iServSession.AttachExecutable(KRMDebugSecurity0FileName, EFalse));
       
  3419 
       
  3420 	test(KErrNone == iServSession.SetEventAction(KRMDebugSecurity0FileName,EEventsAddProcess, EActionContinue));
       
  3421 
       
  3422 	test(KErrNone == iServSession.SetEventAction(KRMDebugSecurity0FileName,EEventsRemoveProcess, EActionContinue));
       
  3423 
       
  3424 	// Creator thread ID of the current thread (to be creator of test application)
       
  3425 	TInt creatorThreadId = RThread().Id();
       
  3426 
       
  3427 	RProcess process;
       
  3428 	TInt err = process.Create(KRMDebugSecurity0FileName, KNullDesC, EOwnerProcess);
       
  3429 	test (err == KErrNone);
       
  3430 
       
  3431 	// Rendezvous with process
       
  3432 	TRequestStatus status;
       
  3433 	process.Rendezvous(status);
       
  3434 
       
  3435 	// Start the test program
       
  3436 	process.Resume();
       
  3437 	User::WaitForRequest(status);
       
  3438 	test(status==KErrNone);
       
  3439 
       
  3440 	// Wait for the addprocess event
       
  3441 	TEventInfo info;
       
  3442 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  3443 
       
  3444 	iServSession.GetEvent(KRMDebugSecurity0FileName,status,infoPtr);
       
  3445 
       
  3446 	// Wait for notification of the addprocess hit event
       
  3447 	User::WaitForRequest(status);
       
  3448 	test(status==KErrNone);
       
  3449 
       
  3450 	// Check this was the right kind of event
       
  3451 	test(info.iEventType == EEventsAddProcess);
       
  3452 
       
  3453 	const TInt uid3offset = 2;
       
  3454 
       
  3455 	// Get UID3 for current process
       
  3456 	TUint32 Uid3 = process.Type()[uid3offset].iUid;
       
  3457 
       
  3458 	// Check correct UID3 is returned from the driver
       
  3459     test(info.iAddProcessInfo.iUid3 == Uid3);
       
  3460 
       
  3461     // Check correct creator ID for test application is returned from the driver
       
  3462     test(info.iAddProcessInfo.iCreatorThreadId == creatorThreadId);
       
  3463 
       
  3464 	// Kill the process, as we don't need it anymore
       
  3465 	process.Kill(KErrNone);
       
  3466 
       
  3467 	// Wait for the remove process event
       
  3468 	iServSession.GetEvent(KRMDebugSecurity0FileName,status,infoPtr);
       
  3469 
       
  3470 	// Wait for notification of the remove process hit event
       
  3471 	User::WaitForRequest(status);
       
  3472 	test(status==KErrNone);
       
  3473 
       
  3474 	// Check this was the right kind of event
       
  3475 	test(info.iEventType == EEventsRemoveProcess);
       
  3476 
       
  3477 	test(KErrNone == iServSession.SetEventAction(KRMDebugSecurity0FileName,EEventsRemoveProcess, EActionIgnore));
       
  3478 
       
  3479 	test(KErrNone == iServSession.SetEventAction(KRMDebugSecurity0FileName,EEventsAddProcess, EActionIgnore));
       
  3480 
       
  3481 	test(KErrNone == iServSession.DetachExecutable(KRMDebugSecurity0FileName));
       
  3482 
       
  3483 	}
       
  3484 
       
  3485 //----------------------------------------------------------------------------------------------
       
  3486 //! @SYMTestCaseID      KBase-T-RMDEBUG2-0736
       
  3487 //! @SYMTestType
       
  3488 //! @SYMPREQ            PREQ1426
       
  3489 //! @SYMTestCaseDesc    Checks that process break points can be set, and that they can co-exist alongside thread breakpoints
       
  3490 //! @SYMTestActions     Checks that process break points can be set, and that they can co-exist alongside thread breakpoints
       
  3491 //!
       
  3492 //! @SYMTestExpectedResults KErrNone.
       
  3493 //! @SYMTestPriority        High
       
  3494 //! @SYMTestStatus          Implemented
       
  3495 //----------------------------------------------------------------------------------------------
       
  3496 void CRunModeAgent::TestProcessBreakPoints(void)
       
  3497 	{
       
  3498 	test.Next(_L("TestProcessBreakPoints\n"));
       
  3499 
       
  3500 	// check that process breakpoints are supported
       
  3501 	TTag tag = GetTag(ETagHeaderIdBreakpoints, EBreakpointProcess);
       
  3502 	test(tag.iValue);
       
  3503 
       
  3504 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  3505 	test(KErrNone == iServSession.SuspendThread(iThreadID));
       
  3506 
       
  3507 	// Try to set the breakpoint
       
  3508 	TBreakId breakId;
       
  3509 	TUint32 address = (TUint32)(&RMDebug_BranchTst1);
       
  3510 	RProcess process;
       
  3511 	TProcessId processId = process.Id();
       
  3512 	process.Close();
       
  3513 
       
  3514 	test(KErrNone == iServSession.SetProcessBreak(breakId, processId, address, EArmMode));
       
  3515 	test(KErrAlreadyExists == iServSession.SetBreak(breakId, iThreadID, address, EArmMode));
       
  3516 	test(KErrAlreadyExists == iServSession.SetBreak(breakId, iThreadID, address, EThumbMode));
       
  3517 	test(KErrAlreadyExists == iServSession.SetProcessBreak(breakId, processId, address, EArmMode));
       
  3518 	test(KErrAlreadyExists == iServSession.SetProcessBreak(breakId, processId, address, EThumbMode));
       
  3519 	test(KErrNone == iServSession.ClearBreak(breakId));
       
  3520 
       
  3521 	test(KErrNone == iServSession.SetBreak(breakId, iThreadID, address, EArmMode));
       
  3522 	test(KErrAlreadyExists == iServSession.SetProcessBreak(breakId, processId, address, EArmMode));
       
  3523 	test(KErrAlreadyExists == iServSession.SetProcessBreak(breakId, processId, address, EThumbMode));
       
  3524 	test(KErrNone == iServSession.ClearBreak(breakId));
       
  3525 
       
  3526 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  3527 
       
  3528 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  3529 	}
       
  3530 
       
  3531 //----------------------------------------------------------------------------------------------
       
  3532 //! @SYMTestCaseID      KBase-T-RMDEBUG2-1309
       
  3533 //! @SYMTestType
       
  3534 //! @SYMPREQ            PREQ1426
       
  3535 //! @SYMTestCaseDesc    Checks that in the case of multiple low priority events (user traces in this case) we can still receive higher
       
  3536 //!				priority events should the buffer reach a critical level
       
  3537 //! @SYMTestActions     Run to first breakpoint in our test code. Then multiple trace events are issued. We should still be able to hit
       
  3538 //!				the second breakpoint
       
  3539 //!
       
  3540 //! @SYMTestExpectedResults KErrNone.
       
  3541 //! @SYMTestPriority        High
       
  3542 //! @SYMTestStatus          Implemented
       
  3543 //----------------------------------------------------------------------------------------------
       
  3544 
       
  3545 void CRunModeAgent::TestMultipleTraceEvents(void)
       
  3546 	{
       
  3547 	test.Next(_L("TestMultipleTraceEvents\n"));
       
  3548 
       
  3549 	//attach to target debug process
       
  3550 	test(KErrNone == iServSession.AttachExecutable(iFileName, EFalse));
       
  3551 
       
  3552 	//set the target thread to execute the trace test function
       
  3553 	test(KErrNone == SwitchTestFunction(EMultipleTraceCalls, EFalse));
       
  3554 	
       
  3555 	
       
  3556 
       
  3557 	//register interest in BP's & trace events and trace ignored events
       
  3558 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionSuspend));
       
  3559 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsUserTrace, EActionContinue));
       
  3560 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsUserTracesLost, EActionContinue));
       
  3561 
       
  3562 	// Try to set the breakpoints
       
  3563 	TBreakId armBreakId;
       
  3564 	TBreakId armBreakId2;
       
  3565 	TUint32 address = (TUint32)(&RMDebug_BranchTst1);
       
  3566 	TUint32 address2 = (TUint32)(&RMDebug_StepTest_Non_PC_Modifying);
       
  3567 
       
  3568 	test(KErrNone == iServSession.SetBreak(armBreakId,iThreadID,address,EArmMode));
       
  3569 	test(KErrNone == iServSession.SetBreak(armBreakId2,iThreadID,address2,EArmMode));
       
  3570 
       
  3571 	// Continue the thread
       
  3572 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  3573 
       
  3574 	// wait for the breakpoint to be hit
       
  3575 	TEventInfo info;
       
  3576 	static TRequestStatus status;
       
  3577 
       
  3578 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  3579 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  3580 
       
  3581 	// Wait for notification of the 1st breakpoint hit event
       
  3582 	User::WaitForRequest(status);
       
  3583 	test(status==KErrNone);
       
  3584 
       
  3585 	// info should now be filled with the details
       
  3586 	test(info.iEventType == EEventsBreakPoint);
       
  3587 	test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address);
       
  3588 	test(info.iProcessIdValid);
       
  3589 	test(info.iThreadIdValid);
       
  3590 
       
  3591 	// Continue the thread
       
  3592 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  3593 
       
  3594 	//Now we try to hit the second breakpoint. This will occur after a number of trace calls. If we hit this breakpoint it
       
  3595 	//means many trace calls are not preventing us hitting breakpoints.
       
  3596 	iServSession.GetEvent(iFileName,status,infoPtr);
       
  3597 
       
  3598 	// Wait for notification of the 2nd breakpoint hit event
       
  3599 	User::WaitForRequest(status);
       
  3600 	test(status==KErrNone);
       
  3601 
       
  3602 	TBool receivedTracesLost = EFalse;
       
  3603 
       
  3604 	while(info.iEventType == EEventsUserTrace || info.iEventType == EEventsUserTracesLost)
       
  3605 		{
       
  3606 		//ensure we get told traces are being thrown away - we generate enough to flood the buffer
       
  3607 		if(info.iEventType == EEventsUserTracesLost)
       
  3608 			{
       
  3609 			receivedTracesLost = ETrue;
       
  3610 
       
  3611 			// Now stop the target thread from generating trace events
       
  3612 			test(KErrNone == SwitchTestFunction(EDoNothing, EFalse));
       
  3613 			break;
       
  3614 			}
       
  3615 		else
       
  3616 			{
       
  3617 			// Its EEventsUserTrace, so delay us in getting the next event so that it will be more 
       
  3618 			// likely to get a EEventsUserTracesLost next time. 
       
  3619 			// This is important on SMP since the platform can process lots of events, and thus
       
  3620 			// withouth the delay it is difficult for this test to reproduce the abnormal situation of 
       
  3621 			// lost trace packets
       
  3622 			User::After(200000);
       
  3623 			}
       
  3624 
       
  3625 		iServSession.GetEvent(iFileName,status,infoPtr);
       
  3626 
       
  3627 		// Wait for notification of the 2nd breakpoint hit event
       
  3628 		User::WaitForRequest(status);
       
  3629 		test(status==KErrNone);
       
  3630 		}
       
  3631 
       
  3632 	//make sure we got told traces were lost
       
  3633 	test(receivedTracesLost != EFalse);
       
  3634 
       
  3635 	//dont care for breakpoints or trace events no more
       
  3636 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsBreakPoint, EActionIgnore));
       
  3637 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsUserTrace, EActionIgnore));
       
  3638 	test(KErrNone == iServSession.SetEventAction(iFileName,EEventsUserTracesLost, EActionIgnore));
       
  3639 
       
  3640 	//clear the breaks we set
       
  3641 	test(KErrNone == iServSession.ClearBreak(armBreakId));
       
  3642 	test(KErrNone == iServSession.ClearBreak(armBreakId2));
       
  3643 
       
  3644 	// Continue the thread
       
  3645 	test(KErrNone == iServSession.ResumeThread(iThreadID));
       
  3646 
       
  3647 	//attach to target debug process
       
  3648 	test(KErrNone == iServSession.DetachExecutable(iFileName));
       
  3649 
       
  3650 	}
       
  3651 
       
  3652 //----------------------------------------------------------------------------------------------
       
  3653 //! @SYMTestCaseID KBase-T-RMDEBUG2-2441
       
  3654 //! @SYMTestType
       
  3655 //! @SYMPREQ PREQ1426
       
  3656 //! @SYMTestCaseDesc Test clearing of a process breakpoint once the process has been killed.
       
  3657 //! @SYMTestActions Creates a new process then tries to set a process breakpoint and then kills the process which should clear the previously set breakpoint. Then repeat the step once again.
       
  3658 //! @SYMTestExpectedResults KErrNone
       
  3659 //! @SYMTestPriority High
       
  3660 //! @SYMTestStatus Implemented
       
  3661 //----------------------------------------------------------------------------------------------
       
  3662 
       
  3663 void CRunModeAgent::TestProcessKillBreakpoint(void)
       
  3664 	{
       
  3665 	test.Next(_L("TestProcessKillBreakpoint\n"));
       
  3666 
       
  3667 	DoTestProcessKillBreakpoint();
       
  3668 	// called once again
       
  3669 	// to check if we can set the breakpoint once again after the process gets killed
       
  3670 	DoTestProcessKillBreakpoint();
       
  3671 
       
  3672 	// And do it a couple more times, there was a leaked process handle that didn't show up
       
  3673 	// until the third or fourth time this code was run
       
  3674 	DoTestProcessKillBreakpoint();
       
  3675 	DoTestProcessKillBreakpoint();
       
  3676 	}
       
  3677 
       
  3678 void CRunModeAgent::DoTestProcessKillBreakpoint()
       
  3679 	{
       
  3680 	test.Printf(_L("\nDoTestProcessKillBreakpoint\n"));
       
  3681 
       
  3682 	// check that killing a process is supported
       
  3683 	TTag tag = GetTag(ETagHeaderIdKillObjects, EFunctionalityKillProcess);
       
  3684 	test(tag.iValue);
       
  3685 	// check that killing a thread is not supported
       
  3686 	tag = GetTag(ETagHeaderIdKillObjects, EFunctionalityKillThread);
       
  3687 	test(!tag.iValue);
       
  3688 
       
  3689 	// attach first!
       
  3690 	test ( KErrNone == iServSession.AttachExecutable(KRMDebugTestApplication, EFalse/*  Active */));
       
  3691 
       
  3692 	RProcess processDebug;
       
  3693 	TThreadId dontCare;
       
  3694 	LaunchDebugProcessAndSetBreakpoint(processDebug, dontCare);
       
  3695 
       
  3696 	// Not interested in breakpoint events any more
       
  3697 	test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication, EEventsProcessBreakPoint, EActionIgnore));
       
  3698 
       
  3699 	// program now running, so try to kill it which should clear all the breakpoints
       
  3700 	test(KErrNone == iServSession.KillProcess(processDebug.Id(), 0  /* kill reason */ ));
       
  3701 
       
  3702 	TRequestStatus stat;
       
  3703 	processDebug.NotifyDestruction(stat);
       
  3704 	processDebug.Close();
       
  3705 	TIMED_WAIT(stat, 1000);
       
  3706 
       
  3707 	// release the program again
       
  3708 	test(KErrNone == iServSession.DetachExecutable(KRMDebugTestApplication));
       
  3709 	}
       
  3710 
       
  3711 void CRunModeAgent::LaunchDebugProcessAndSetBreakpoint(RProcess& aResultProcess, TThreadId& aResultThread)
       
  3712 	{
       
  3713 	// define a property to pass on the address from the other process we would try to debug
       
  3714 	static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
  3715 	TInt err = RProperty::Define(RProcess().SecureId(), EMyPropertyInteger, RProperty::EInt, KAllowAllPolicy, KAllowAllPolicy);
       
  3716 	test (err == KErrNone || err == KErrAlreadyExists);
       
  3717 
       
  3718 	RSemaphore addressGlobSem;
       
  3719 	//define a global semaphore to synchronise with debuggable process publishing the property
       
  3720 	err = addressGlobSem.CreateGlobal(_L("RMDebugGlobSem"), 0);
       
  3721 	test (err == KErrNone);
       
  3722 
       
  3723 	// first launch a debuggable process
       
  3724 	RProcess& processDebug(aResultProcess);
       
  3725 	test ( KErrNone == LaunchProcess(processDebug, KRMDebugTestApplication(),ESpinForeverWithBreakPoint, 0, 0));
       
  3726 
       
  3727 	// try to find the process in the list
       
  3728 	TBool found = ProcessExists(KRMDebugAppName);
       
  3729 	test (found);
       
  3730 
       
  3731 	//search for the main thread created
       
  3732    _LIT(KThreadWildCard, "t_rmdebug_app*");
       
  3733 	TProcessId processDebugId = processDebug.Id();
       
  3734 	TThreadId& threadDebugId(aResultThread);
       
  3735 
       
  3736    	TFindThread find(KThreadWildCard);
       
  3737 	TFullName name;
       
  3738 	found = EFalse;
       
  3739 	while(find.Next(name)==KErrNone && !found)
       
  3740 		{
       
  3741 		RThread thread;
       
  3742 		err = thread.Open(find);
       
  3743        	if (err == KErrNone)
       
  3744 			{
       
  3745 			RProcess process;
       
  3746 			thread.Process(process);
       
  3747 			if (((TUint32)process.Id() == processDebugId))
       
  3748 				{
       
  3749 				TFullName fullname = thread.FullName();
       
  3750 				test.Printf(_L("Match Found Name: %S Process id: %ld Thread id: %ld\n"), &fullname, process.Id().Id(), thread.Id().Id());
       
  3751 				found = ETrue;
       
  3752 				threadDebugId = thread.Id();
       
  3753 				}
       
  3754 			process.Close();
       
  3755 			}
       
  3756 		thread.Close();
       
  3757    		}
       
  3758 
       
  3759 	test (found); //check if we actually found the thread we want to debug
       
  3760 
       
  3761 	//waiting on semaphore to be sure that the property is set
       
  3762 	addressGlobSem.Wait();
       
  3763 
       
  3764 	//get the value(property) for the breakpoint address for the process to debug
       
  3765 	TInt address;
       
  3766 	test(KErrNone == RProperty::Get(RProcess().SecureId(), EMyPropertyInteger, address));
       
  3767 
       
  3768 	test.Printf(_L("Address retrieved to set breakpoint 0x%08x\n"), address);
       
  3769 
       
  3770 	//suspend the thread before we set a breakpoint
       
  3771 	test (KErrNone == iServSession.SuspendThread(threadDebugId));
       
  3772 
       
  3773 	//set a process breakpoint
       
  3774 	TBreakId breakId;
       
  3775 	test(KErrNone == iServSession.SetProcessBreak(breakId, processDebugId, address, EArmMode));
       
  3776 
       
  3777 	test(KErrNone == iServSession.SetEventAction(KRMDebugTestApplication, EEventsProcessBreakPoint, EActionContinue));
       
  3778 
       
  3779 	//resume the thread now
       
  3780 	test(KErrNone == iServSession.ResumeThread(threadDebugId));
       
  3781 
       
  3782 	// wait for the breakpoint to be hit
       
  3783 	TRequestStatus status;
       
  3784 	TEventInfo info;
       
  3785 	TPtr8 infoPtr((TUint8*)&info,0,sizeof(TEventInfo));
       
  3786 	iServSession.GetEvent(KRMDebugTestApplication,status,infoPtr);
       
  3787 	// Wait for notification of the breakpoint hit event
       
  3788 	TIMED_WAIT(status, 2000);
       
  3789 	test(status==KErrNone);
       
  3790 
       
  3791 	// info should now be filled with the details
       
  3792 	test(info.iEventType ==  EEventsProcessBreakPoint);
       
  3793 	test(info.iThreadBreakPointInfo.iRmdArmExcInfo.iR15 == address);
       
  3794 	test(info.iProcessIdValid);
       
  3795 	test(info.iThreadIdValid);
       
  3796 
       
  3797 	addressGlobSem.Close();
       
  3798 	}
       
  3799 
       
  3800 void CRunModeAgent::HelpTestSecurityAttachDetachExecutable(const TDesC& aProcessName, TBool aExpectSuccess)
       
  3801 	{
       
  3802 	RProcess process;
       
  3803 	TInt err = process.Create(aProcessName, KNullDesC, EOwnerProcess);
       
  3804 	test (err == KErrNone);
       
  3805 
       
  3806 	// rendezvous with process
       
  3807 	TRequestStatus status;
       
  3808 	process.Rendezvous(status);
       
  3809 
       
  3810 	// start the test program
       
  3811 	process.Resume();
       
  3812 	User::WaitForRequest(status);
       
  3813 	test(status==KErrNone);
       
  3814 
       
  3815 	// attach to the program (passively)
       
  3816 	err = iServSession.AttachExecutable(aProcessName, EFalse);
       
  3817 
       
  3818 	if( gUseDelay ) User::After(500000);
       
  3819 
       
  3820 	// Do we expect to successfully attach
       
  3821 	if (aExpectSuccess)
       
  3822 	{
       
  3823 		// Yes
       
  3824 		test(KErrNone == err);
       
  3825 
       
  3826 		// Now detach again
       
  3827 		test(KErrNone == iServSession.DetachExecutable(aProcessName));
       
  3828 		if( gUseDelay ) User::After(500000);
       
  3829 	}
       
  3830 	else
       
  3831 	{
       
  3832 		// No
       
  3833 		test(KErrPermissionDenied == err);
       
  3834 
       
  3835 		// Just to be sure, try active attachment
       
  3836 		test(KErrPermissionDenied == iServSession.AttachExecutable(aProcessName, ETrue));
       
  3837 		if( gUseDelay ) User::After(500000);
       
  3838 	}
       
  3839 
       
  3840 	// Kill the process, as we don't need it anymore
       
  3841 	process.Kill(KErrNone);
       
  3842 	if( gUseDelay ) User::After(500000);
       
  3843 	}
       
  3844 
       
  3845 void CRunModeAgent::ReportPerformance(void)
       
  3846 //
       
  3847 // Reports performance metrics from all the tests
       
  3848 //
       
  3849 	{
       
  3850 	test.Printf(_L("\nPerformance\n"));
       
  3851 	test.Printf(_L("========================\n"));
       
  3852 
       
  3853 	// Memory
       
  3854 	test.Printf(_L("Memory read: %d KBytes/sec\n"),iMemoryReadKbytesPerSecond);
       
  3855 	test.Printf(_L("Memory write: %d KBytes/sec\n"),iMemoryWriteKbytesPerSecond);
       
  3856 
       
  3857 	// Registers
       
  3858 	// to do
       
  3859 
       
  3860 	// events
       
  3861 	// to do
       
  3862 
       
  3863 	// Breakpoints
       
  3864 	test.Printf(_L("Breakpoint set/clear: %d/sec\n"),iBreakpointsPerSecond);
       
  3865 	test.Printf(_L("Maximum number of breakpoints: %d\n"),iMaxBreakpoints);
       
  3866 
       
  3867 	// Stepping
       
  3868 	test.Printf(_L("Stepping speed: %d/sec\n"),iStepsPerSecond);
       
  3869 
       
  3870 	// Runtime
       
  3871 	TInt ticks = HelpGetTestTicks();
       
  3872 	test (ticks != 0);
       
  3873 
       
  3874 	TInt nkTicksPerSecond = HelpTicksPerSecond();
       
  3875 	test (nkTicksPerSecond != 0);
       
  3876 
       
  3877 	test.Printf(_L("Total test runtime: %d seconds\n"),ticks/nkTicksPerSecond);
       
  3878 
       
  3879 	// Final sizes of executables/rom/ram etc
       
  3880 	// to do
       
  3881 
       
  3882 	test.Printf(_L("\n"));
       
  3883 	}
       
  3884 
       
  3885 /**
       
  3886  * Helper code for the stepping tests. Sets a breakpoint in a running thread.
       
  3887  * It suspends the thread, sets the breakpoint, and resumes the thread.
       
  3888  *
       
  3889  * @param aBreakId - Reference to a TBreakId which will be set when the breakpoint is set
       
  3890  * @param aThreadId - The thread id for which we should set the breakpoint.
       
  3891  * @param aBreakAddress - The address to set the breakpoint
       
  3892  * @param aMode - The architecture of the breakpoint to be set (ARM/Thumb/Thumb2EE)
       
  3893  * @return KErrNone if successful. One of the other system wide error codes otherwise.
       
  3894  */
       
  3895 TInt CRunModeAgent::HelpTestStepSetBreak(TBreakId& aBreakId, TThreadId aThreadId, const TUint32 aBreakAddress, TArchitectureMode aMode, TBool aThreadSpecific, TProcessId aProcessId)
       
  3896 	{
       
  3897 	TInt err = KErrNone;
       
  3898 
       
  3899 
       
  3900 	// Set the breakpoint
       
  3901 	err = aThreadSpecific
       
  3902 		? iServSession.SetBreak(aBreakId,aThreadId,aBreakAddress,aMode)
       
  3903 		: iServSession.SetProcessBreak(aBreakId, aProcessId, aBreakAddress, aMode);
       
  3904 	if (err != KErrNone)
       
  3905 		{
       
  3906 		test.Printf(_L("HelpTestStepSetBreak - Failed to set breakpoint\n"));
       
  3907 		return err;
       
  3908 		}
       
  3909 
       
  3910 	// Continue the thread
       
  3911 	err = iServSession.ResumeThread(aThreadId);
       
  3912 	if (err != KErrNone)
       
  3913 		{
       
  3914 		test.Printf(_L("HelpTestStepSetBreak - Failed to resume thread\n"));
       
  3915 		return err;
       
  3916 		}
       
  3917 
       
  3918 	return KErrNone;
       
  3919 	}
       
  3920 
       
  3921 /**
       
  3922  * Helper code for the stepping tests. Clears a breakpoint in a running thread.
       
  3923  * It suspends the thread, clears the breakpoint, and resumes the thread.
       
  3924  *
       
  3925  * @param aBreakId - Reference to a TBreakId which will be set when the breakpoint is set
       
  3926  * @return KErrNone if successful. One of the other system wide error codes otherwise.
       
  3927  */
       
  3928 TInt CRunModeAgent::HelpTestStepClearBreak(TBreakId aBreakId, const TThreadId aThreadId, TBool aThreadSpecific)
       
  3929 	{
       
  3930 	TInt err = KErrNone;
       
  3931 
       
  3932 	// Find out what thread id we need to suspend
       
  3933 	TThreadId threadId;
       
  3934 	TProcessId processId;
       
  3935 	TUint32 address;
       
  3936 	TArchitectureMode mode;
       
  3937 
       
  3938 	err = aThreadSpecific
       
  3939 		? iServSession.BreakInfo(aBreakId, threadId, address, mode)
       
  3940 		: iServSession.ProcessBreakInfo(aBreakId, processId, address, mode);
       
  3941 	if (err != KErrNone )
       
  3942 		{
       
  3943 		test.Printf(_L("HelpTestStepClearBreak - failed to obtain information for breakpoint\n"));
       
  3944 		return err;
       
  3945 		}
       
  3946 	if(aThreadSpecific && aThreadId != threadId)
       
  3947 		{
       
  3948 		test.Printf(_L("HelpTestStepClearBreak - mismatched thread Ids\n"));
       
  3949 		return KErrGeneral;
       
  3950 		}
       
  3951 
       
  3952 	// Clear the breakpoint
       
  3953 	err = iServSession.ClearBreak(aBreakId);
       
  3954 	if (err != KErrNone)
       
  3955 		{
       
  3956 		test.Printf(_L("HelpTestStepClearBreak - failed to clear breakpoint\n"));
       
  3957 		return err;
       
  3958 		}
       
  3959 
       
  3960 	return KErrNone;
       
  3961 	}
       
  3962 
       
  3963 /**
       
  3964  * Helper code for the stepping tests. Waits for a previously set breakpoint to be hit.
       
  3965  *
       
  3966  * @param aProcessName - The name of the process in which the breakpoint is set. E.g. z:\sys\bin\app.exe
       
  3967  * @param aEventInfo - The event information block which is filled in when the breakpoint is hit.
       
  3968  * @return KErrNone if successful. One of the other system wide error codes otherwise.
       
  3969  */
       
  3970 TInt CRunModeAgent::HelpTestStepWaitForBreak(const TDesC& aProcessName, TEventInfo& aEventInfo)
       
  3971 	{
       
  3972 	static TRequestStatus status;
       
  3973 
       
  3974 	TPtr8 infoPtr((TUint8*)&aEventInfo,0,sizeof(TEventInfo));
       
  3975 
       
  3976 	iServSession.GetEvent(aProcessName,status,infoPtr);
       
  3977 
       
  3978 	// Wait for notification of the breakpoint hit event
       
  3979 	User::WaitForRequest(status);
       
  3980 	if (status == KErrNone)
       
  3981 		{
       
  3982 		return KErrNone;
       
  3983 		}
       
  3984 	else
       
  3985 		{
       
  3986 		return KErrGeneral;
       
  3987 		}
       
  3988 	}
       
  3989 
       
  3990 /**
       
  3991  * Helper code for the stepping tests. Reads the current target PC for a given thread.
       
  3992  *
       
  3993  * @param aThreadId - Thread id for which to read the current target PC.
       
  3994  * @param aPc - Reference to a TUint32 which will be set to the current target PC.
       
  3995  * @return KErrNone if successful. One of the other system wide error codes otherwise.
       
  3996  */
       
  3997 TInt CRunModeAgent::HelpTestStepReadPC(TThreadId aThreadId, TUint32& aPC)
       
  3998 	{
       
  3999 	TInt err = KErrNone;
       
  4000 
       
  4001 	//create buffer containing PC register ID
       
  4002 	RBuf8 pcId;
       
  4003 	err = pcId.Create(sizeof(TRegisterInfo));
       
  4004 	if (err != KErrNone)
       
  4005 		{
       
  4006 		return err;
       
  4007 		}
       
  4008 
       
  4009 	TRegisterInfo reg1 = (TRegisterInfo)0x00000f00;
       
  4010 	pcId.Append(reinterpret_cast<const TUint8*>(&reg1), sizeof(TRegisterInfo));
       
  4011 
       
  4012 	//create buffer containing desired PC value
       
  4013 	TPtr8 pcValue((TUint8*)&aPC,4,4);
       
  4014 
       
  4015 	//create buffer for PC flag value
       
  4016 	RBuf8 pcFlag;
       
  4017 	err = pcFlag.Create(sizeof(TUint8));
       
  4018 
       
  4019 	//read the new PC value
       
  4020 	err = iServSession.ReadRegisters(aThreadId, pcId, pcValue, pcFlag);
       
  4021 	if (err != KErrNone)
       
  4022 		{
       
  4023 		//delete temporary buffers
       
  4024 		pcId.Close();
       
  4025 		pcFlag.Close();
       
  4026 		return err;
       
  4027 		}
       
  4028 
       
  4029 	//get the flag and check the PC value was read ok
       
  4030 	TRegisterFlag flag = ENotSupported;
       
  4031 	err = GetFlag(pcFlag, 0, flag);
       
  4032 	if (err != KErrNone)
       
  4033 		{
       
  4034 		//delete temporary buffers
       
  4035 		pcId.Close();
       
  4036 		pcFlag.Close();
       
  4037 		return err;
       
  4038 		}
       
  4039 
       
  4040 	if (flag == EValid)
       
  4041 		{
       
  4042 		//delete temporary buffers
       
  4043 		pcId.Close();
       
  4044 		pcFlag.Close();
       
  4045 		return KErrNone;
       
  4046 		}
       
  4047 	else
       
  4048 		{
       
  4049 		//delete temporary buffers
       
  4050 		pcId.Close();
       
  4051 		pcFlag.Close();
       
  4052 		return err;
       
  4053 		}
       
  4054 	}
       
  4055 
       
  4056 /**
       
  4057  * Helper code for the stepping tests. Single steps a given thread from aStartAddress to aEndAddress. Note
       
  4058  * that it reaches aStartAddress by setting a breakpoint at that address and waiting until it is hit.
       
  4059  *
       
  4060  * @param aThreadId - Thread id for which to read the current target PC.
       
  4061  * @param aStartAddress - The target address at which stepping will start.
       
  4062  * @param aEndAddress - The target address at which stepping will end.
       
  4063  * @param aMode - The architecture of the breakpoint which must be set at the start address (ARM/Thumb/Thumb2EE).
       
  4064  * @return KErrNone if successful. One of the other system wide error codes otherwise.
       
  4065  */
       
  4066 TInt CRunModeAgent::HelpTestStep(TThreadId aThreadId, TUint32 aStartAddress, TUint32 aEndAddress, TArchitectureMode aMode, TUint aNumSteps, TBool aThreadSpecific, TProcessId aProcessId)
       
  4067 	{
       
  4068 	TInt err = KErrNone;
       
  4069 
       
  4070 	// Ensure that the supplied addresses are word/half-word aligned as appropriate.
       
  4071 	if (aMode == EArmMode)
       
  4072 		{
       
  4073 		// ARM breakpoints must be word-aligned (2 lsb must be zero)
       
  4074 		aStartAddress &= 0xFFFFFFFC;
       
  4075 		aEndAddress &= 0xFFFFFFFC;
       
  4076 		}
       
  4077 	else if (aMode == EThumbMode)
       
  4078 		{
       
  4079 		// Thumb breakpoints must be half-word aligned (lsb must be zero)
       
  4080 		aStartAddress &= 0xFFFFFFFE;
       
  4081 		aEndAddress	 &= 0xFFFFFFFE;
       
  4082 		}
       
  4083 	else if (aMode == EThumb2EEMode)
       
  4084 	{
       
  4085 		// Thumb2EE breakpoints are not currently supported
       
  4086 		return KErrNotSupported;
       
  4087 	}
       
  4088 
       
  4089 	// Set breakpoint at the start address
       
  4090 	TBreakId tempBreakId;
       
  4091 	TEventInfo info;
       
  4092 
       
  4093 	err = HelpTestStepSetBreak(tempBreakId,aThreadId,aStartAddress,aMode,aThreadSpecific,aProcessId);
       
  4094 	if (err != KErrNone)
       
  4095 		{
       
  4096 		test.Printf(_L("HelpTestStep - Failed to set breakpoint at aStartAddress 0x%08x\n"),aStartAddress);
       
  4097 		return err;
       
  4098 		}
       
  4099 
       
  4100 	// wait for the breakpoint to be hit
       
  4101 	err = HelpTestStepWaitForBreak(iFileName,info);
       
  4102 	if (err != KErrNone)
       
  4103 		{
       
  4104 		test.Printf(_L("HelpTestStep - Failed to hit the breakpoint at aStartAddress 0x%08x\n"),aStartAddress);
       
  4105 		return err;
       
  4106 		}
       
  4107 
       
  4108 	// Check the PC == aStartAddress
       
  4109 	TUint32 pc = 0;
       
  4110 	err = HelpTestStepReadPC(aThreadId,pc);
       
  4111 	if (err != KErrNone)
       
  4112 		{
       
  4113 		test.Printf(_L("HelpTestStep - Failed to read the PC after hitting breakpoint at aStartAddress 0x%08x\n"),aStartAddress);
       
  4114 		return err;
       
  4115 		}
       
  4116 
       
  4117 	if (pc != aStartAddress)
       
  4118 		{
       
  4119 		test.Printf(_L("HelpTestStep - Incorrect PC value after hitting breakpoint (expected 0x%08x actual 0x%08x)\n"),aStartAddress,pc);
       
  4120 		return KErrGeneral;
       
  4121 		}
       
  4122 
       
  4123 	err = iServSession.Step(aThreadId,aNumSteps);
       
  4124 	if (err != KErrNone)
       
  4125 		{
       
  4126 		test.Printf(_L("HelpTestStep - Failed to do step from 0x%08x to 0x%08x\n"),aStartAddress,aEndAddress,aNumSteps);
       
  4127 		return err;
       
  4128 		}
       
  4129 
       
  4130 	// only one 'completed step' event in the buffer.
       
  4131 	err = HelpTestStepWaitForBreak(iFileName,info);
       
  4132 	if (err != KErrNone)
       
  4133 		{
       
  4134 		test.Printf(_L("HelpTestStep - Could not read breakpoint event info after stepping"));
       
  4135 		return err;
       
  4136 		}
       
  4137 	// end
       
  4138 
       
  4139 	// Check PC == aEndAddress
       
  4140 	err = HelpTestStepReadPC(aThreadId,pc);
       
  4141 	if (err != KErrNone)
       
  4142 		{
       
  4143 		test.Printf(_L("HelpTestStep - failed read the PC after stepping\n"));
       
  4144 		return err;
       
  4145 		}
       
  4146 	if (pc != aEndAddress)
       
  4147 		{
       
  4148 		test.Printf(_L("HelpTestStep - Incorrect PC value after stepping (expected 0x%08x actual 0x%08x)\n"),aEndAddress,pc);
       
  4149 		return KErrGeneral;
       
  4150 		}
       
  4151 
       
  4152 	// Clear the breakpoint
       
  4153 	err = HelpTestStepClearBreak(tempBreakId, aThreadId, aThreadSpecific);
       
  4154 	if (err != KErrNone)
       
  4155 		{
       
  4156 		test.Printf(_L("HelpTestStep - failed to clear temporary breakpoint\n"));
       
  4157 		return err;
       
  4158 		}
       
  4159 
       
  4160 	return KErrNone;
       
  4161 	}
       
  4162 
       
  4163 /**
       
  4164  * Helper code for the stepping tests. Returns the number of nanokernel ticks in one second.
       
  4165  *
       
  4166  * @return Number of nanokernel ticks. 0 if unsuccesful.
       
  4167  */
       
  4168 TInt CRunModeAgent::HelpTicksPerSecond(void)
       
  4169 	{
       
  4170 	TInt nanokernel_tick_period;
       
  4171 	HAL::Get(HAL::ENanoTickPeriod, nanokernel_tick_period);
       
  4172 
       
  4173 	ASSERT(nanokernel_tick_period != 0);
       
  4174 
       
  4175 	static const TInt KOneMillion = 1000000;
       
  4176 
       
  4177 	return KOneMillion/nanokernel_tick_period;
       
  4178 	}
       
  4179 
       
  4180 /**
       
  4181   Given aTestNumber runs the appropriate test inside heap markers
       
  4182 
       
  4183   @param aTestNumber test to run, corresponds to an entry in iTestArray
       
  4184 
       
  4185   @panic Panic if aTestNumber is not in valid range
       
  4186   */
       
  4187 void CRunModeAgent::RunTest(TInt aTestNumber)
       
  4188 	{
       
  4189 	if( (aTestNumber<0) || (aTestNumber>=KMaxTests) )
       
  4190 		{
       
  4191 		User::Panic(_L("Test number out of range"), aTestNumber);
       
  4192 		}
       
  4193 	__UHEAP_MARK;
       
  4194 	(this->*(iTestArray[aTestNumber].iFunctionPtr))();
       
  4195 	__UHEAP_MARKEND;
       
  4196 	}
       
  4197 
       
  4198 void CRunModeAgent::PrintVersion()
       
  4199 	{
       
  4200 	test.Printf(_L("\nt_rmdebug2.exe\nVersion: %S\n"), &(testVersion.Name()));
       
  4201 	test.Printf(_L("Press any key...\n"));
       
  4202 	test.Getch();
       
  4203 	}
       
  4204 
       
  4205 void CRunModeAgent::PrintUsage()
       
  4206 	{
       
  4207 	test.Printf(_L("Invoke with arguments:\n"));
       
  4208 	test.Printf(_L("-r: run specified tests in reverse order\n"));
       
  4209 	test.Printf(_L("-h: display usage information\n"));
       
  4210 	test.Printf(_L("-v: display version\n"));
       
  4211 	test.Printf(_L("-d: use delays\n"));
       
  4212 	test.Printf(_L("<number>: test number to run, can specify more than one from the following list:\n"));
       
  4213 	test.Printf(_L("Press any key for list...\n"));
       
  4214 	test.Getch();
       
  4215 	// if there are too many of these they won't fit on the screen! Stick another Getch() in if there get too many
       
  4216 	for(TInt i=0; i<KMaxTests; i++)
       
  4217 		{
       
  4218 		test.Printf(_L("%2d: %S\n"), i, &(iTestArray[i].iFunctionName));
       
  4219 		}
       
  4220 	test.Printf(_L("Press any key...\n"));
       
  4221 	test.Getch();
       
  4222 	}
       
  4223 
       
  4224 /**
       
  4225   Parse the command line, see CRunModeAgent::PrintUsage for syntax
       
  4226   */
       
  4227 void CRunModeAgent::ParseCommandLineL(TUint32& aMode, RArray<TInt>& aTests)
       
  4228 	{
       
  4229 	// get the length of the command line arguments
       
  4230 	TInt argc = User::CommandLineLength();
       
  4231 
       
  4232 	// allocate a buffer for the command line arguments and extract the data to it
       
  4233 	HBufC* commandLine = HBufC::NewLC(argc);
       
  4234 	TPtr commandLineBuffer = commandLine->Des();
       
  4235 	User::CommandLine(commandLineBuffer);
       
  4236 
       
  4237 	// reset mode
       
  4238 	aMode = (TTestMode)0;
       
  4239 
       
  4240 	// create a lexer and read through the command line
       
  4241 	TLex lex(*commandLine);
       
  4242 	while (!lex.Eos())
       
  4243 		{
       
  4244 		// expecting the first character to be a '-'
       
  4245 		if (lex.Get() == '-')
       
  4246 			{
       
  4247 			TChar arg = lex.Get();
       
  4248 			switch (arg)
       
  4249 				{
       
  4250 				case 'v':
       
  4251 					//print out the help
       
  4252 					aMode |= EModeVersion;
       
  4253 					break;
       
  4254 				case 'h':
       
  4255 					//print out the help
       
  4256 					aMode |= EModeHelp;
       
  4257 					break;
       
  4258 				case 'r':
       
  4259 					//store the fact that we want to run in reverse
       
  4260 					aMode |= EModeReverse;
       
  4261 					break;
       
  4262 				case 'd':
       
  4263 					//store the fact that we want to run in reverse
       
  4264 					gUseDelay = EFalse;
       
  4265 					RDebug::Printf("Not using delays");
       
  4266 					break;
       
  4267 				default:
       
  4268 					// unknown argument so leave
       
  4269 					User::Leave(KErrArgument);
       
  4270 				}
       
  4271 			}
       
  4272 		else
       
  4273 			{
       
  4274 			lex.UnGet();
       
  4275 			TInt testNumber;
       
  4276 			User::LeaveIfError(lex.Val(testNumber));
       
  4277 			if( (testNumber<0) || (testNumber>=KMaxTests) )
       
  4278 				{
       
  4279 				User::Leave(KErrArgument);
       
  4280 				}
       
  4281 			aTests.AppendL(testNumber);
       
  4282 			}
       
  4283 		lex.SkipSpace();
       
  4284 		}
       
  4285 	// if no tests specified then run them all
       
  4286 	if(aTests.Count() == 0)
       
  4287 		{
       
  4288 		aMode |= EModeAll;
       
  4289 		}
       
  4290 
       
  4291 	// do clean up
       
  4292 	CleanupStack::PopAndDestroy(commandLine);
       
  4293 	}
       
  4294 
       
  4295 void CRunModeAgent::ClientAppL()
       
  4296 //
       
  4297 // Performs each test in turn
       
  4298 //
       
  4299 	{
       
  4300 	test.Start(_L("ClientAppL"));
       
  4301 
       
  4302 	RArray<TInt> testsToRun;
       
  4303 	TUint32 testMode = 0;
       
  4304 	ParseCommandLineL(testMode, testsToRun);
       
  4305 
       
  4306 	//if help or version mode specified then just print out the relevant stuff and quit
       
  4307 	if((testMode & EModeHelp) || (testMode & EModeVersion))
       
  4308 		{
       
  4309 		if(testMode & EModeHelp)
       
  4310 			{
       
  4311 			PrintUsage();
       
  4312 			}
       
  4313 		if(testMode & EModeVersion)
       
  4314 			{
       
  4315 			PrintVersion();
       
  4316 			}
       
  4317 		test.End();
       
  4318 		return;
       
  4319 		}
       
  4320 
       
  4321 	if(testMode & EModeAll)
       
  4322 		{
       
  4323 		for(TInt i=0; i<KMaxTests; i++)
       
  4324 			{
       
  4325 			testsToRun.AppendL(i);
       
  4326 			}
       
  4327 		}
       
  4328 
       
  4329 	// if EModeReverse specified then reverse the array elements
       
  4330 	TInt numberOfTests = testsToRun.Count();
       
  4331 	if(testMode & EModeReverse)
       
  4332 		{
       
  4333 		for(TInt i=0; i<(numberOfTests>>1); i++)
       
  4334 			{
       
  4335 			TInt temp = testsToRun[i];
       
  4336 			testsToRun[i] = testsToRun[numberOfTests - (i+1)];
       
  4337 			testsToRun[numberOfTests - (i+1)] = temp;
       
  4338 			}
       
  4339 		}
       
  4340 
       
  4341 	__UHEAP_MARK;
       
  4342 	SetupAndAttachToDSS();
       
  4343 	__UHEAP_MARKEND;
       
  4344 
       
  4345 	HelpStartTestTimer();
       
  4346 	for(TInt i=0; i<numberOfTests; i++)
       
  4347 		{
       
  4348 		RunTest(testsToRun[i]);
       
  4349 		if( gUseDelay ) User::After(500000);
       
  4350 		}
       
  4351 	testsToRun.Close();
       
  4352 
       
  4353 	HelpStopTestTimer();
       
  4354 
       
  4355 	ReportPerformance();
       
  4356 
       
  4357 	test.End();
       
  4358 	}
       
  4359 
       
  4360 /**
       
  4361   Fill the test array with pointers to each test.
       
  4362   */
       
  4363 void CRunModeAgent::FillArray()
       
  4364 	{
       
  4365 	iTestArray[0].iFunctionPtr = &CRunModeAgent::TestDriverSecurity;
       
  4366 	iTestArray[0].iFunctionName = _L("TestDriverSecurity");
       
  4367 	iTestArray[1].iFunctionPtr = &CRunModeAgent::TestDllUsage;
       
  4368 	iTestArray[1].iFunctionName = _L("TestDllUsage");
       
  4369 	iTestArray[2].iFunctionPtr = &CRunModeAgent::TestSecurity;
       
  4370 	iTestArray[2].iFunctionName = _L("TestSecurity");
       
  4371 	iTestArray[3].iFunctionPtr = &CRunModeAgent::TestAttachExecutable;
       
  4372 	iTestArray[3].iFunctionName = _L("TestAttachExecutable");
       
  4373 	iTestArray[4].iFunctionPtr = &CRunModeAgent::TestGetExecutablesList;
       
  4374 	iTestArray[4].iFunctionName = _L("TestGetExecutablesList");
       
  4375 	iTestArray[5].iFunctionPtr = &CRunModeAgent::TestGetProcessList;
       
  4376 	iTestArray[5].iFunctionName = _L("TestGetProcessList");
       
  4377 	iTestArray[6].iFunctionPtr = &CRunModeAgent::TestGetXipLibrariesList;
       
  4378 	iTestArray[6].iFunctionName = _L("TestGetXipLibrariesList");
       
  4379 	iTestArray[7].iFunctionPtr = &CRunModeAgent::TestGetThreadList;
       
  4380 	iTestArray[7].iFunctionName = _L("TestGetThreadList");
       
  4381 	iTestArray[8].iFunctionPtr = &CRunModeAgent::TestGetCodeSegsList;
       
  4382 	iTestArray[8].iFunctionName = _L("TestGetCodeSegsList");
       
  4383 	iTestArray[9].iFunctionPtr = &CRunModeAgent::TestGetListInvalidData;
       
  4384 	iTestArray[9].iFunctionName = _L("TestGetListInvalidData");
       
  4385 	iTestArray[10].iFunctionPtr = &CRunModeAgent::TestMemoryAccess;
       
  4386 	iTestArray[10].iFunctionName = _L("TestMemoryAccess");
       
  4387 	iTestArray[11].iFunctionPtr = &CRunModeAgent::TestDebugFunctionality;
       
  4388 	iTestArray[11].iFunctionName = _L("TestDebugFunctionality");
       
  4389 	iTestArray[12].iFunctionPtr = &CRunModeAgent::TestSuspendResume;
       
  4390 	iTestArray[12].iFunctionName = _L("TestSuspendResume");
       
  4391 	iTestArray[13].iFunctionPtr = &CRunModeAgent::TestBreakPoints;
       
  4392 	iTestArray[13].iFunctionName = _L("TestBreakPoints");
       
  4393 	iTestArray[14].iFunctionPtr = &CRunModeAgent::TestModifyBreak;
       
  4394 	iTestArray[14].iFunctionName = _L("TestModifyBreak");
       
  4395 	iTestArray[15].iFunctionPtr = &CRunModeAgent::TestBreakInfo;
       
  4396 	iTestArray[15].iFunctionName = _L("TestBreakInfo");
       
  4397 	iTestArray[16].iFunctionPtr = &CRunModeAgent::TestRunToBreak;
       
  4398 	iTestArray[16].iFunctionName = _L("TestRunToBreak");
       
  4399 	iTestArray[17].iFunctionPtr = &CRunModeAgent::TestBreakPointsInLoop;
       
  4400 	iTestArray[17].iFunctionName = _L("TestBreakPointsInLoop");
       
  4401 	iTestArray[18].iFunctionPtr = &CRunModeAgent::TestRegisterAccess;
       
  4402 	iTestArray[18].iFunctionName = _L("TestRegisterAccess");
       
  4403 	iTestArray[19].iFunctionPtr = &CRunModeAgent::TestStep;
       
  4404 	iTestArray[19].iFunctionName = _L("TestStep");
       
  4405 	iTestArray[20].iFunctionPtr = &CRunModeAgent::TestDemandPaging;
       
  4406 	iTestArray[20].iFunctionName = _L("TestDemandPaging");
       
  4407 	iTestArray[21].iFunctionPtr = &CRunModeAgent::TestEventsForExternalProcess;
       
  4408 	iTestArray[21].iFunctionName = _L("TestEventsForExternalProcess");
       
  4409 	iTestArray[22].iFunctionPtr = &CRunModeAgent::TestEvents;
       
  4410 	iTestArray[22].iFunctionName = _L("TestEvents");
       
  4411 	iTestArray[23].iFunctionPtr = &CRunModeAgent::TestKillProcess;
       
  4412 	iTestArray[23].iFunctionName = _L("TestKillProcess");
       
  4413 	iTestArray[24].iFunctionPtr = &CRunModeAgent::TestProcessBreakPoints;
       
  4414 	iTestArray[24].iFunctionName = _L("TestProcessBreakPoints");
       
  4415 	iTestArray[25].iFunctionPtr = &CRunModeAgent::TestMultipleTraceEvents;
       
  4416 	iTestArray[25].iFunctionName = _L("TestMultipleTraceEvents");
       
  4417 	iTestArray[26].iFunctionPtr = &CRunModeAgent::TestAddRemoveProcessEvents;
       
  4418 	iTestArray[26].iFunctionName = _L("TestAddRemoveProcessEvents");
       
  4419 	iTestArray[27].iFunctionPtr = &CRunModeAgent::TestCrashFlash;
       
  4420 	iTestArray[27].iFunctionName = _L("TestCrashFlash");
       
  4421 	iTestArray[28].iFunctionPtr = &CRunModeAgent::TestProcessKillBreakpoint;
       
  4422 	iTestArray[28].iFunctionName = _L("TestProcessKillBreakpoint");
       
  4423 	iTestArray[29].iFunctionPtr = &CRunModeAgent::TestAttachToAll;
       
  4424 	iTestArray[29].iFunctionName = _L("TestAttachToAll");
       
  4425 	iTestArray[30].iFunctionPtr = &CRunModeAgent::TestResumeBreakpointsRepeatedly;
       
  4426 	iTestArray[30].iFunctionName = _L("TestResumeBreakpointsRepeatedly");
       
  4427 
       
  4428 	};
       
  4429 
       
  4430 GLDEF_C TInt E32Main()
       
  4431 //
       
  4432 // Entry point for run mode debug driver test
       
  4433 //
       
  4434 	{
       
  4435    TInt ret = KErrNone;
       
  4436 
       
  4437 	// client
       
  4438 	CTrapCleanup* trap = CTrapCleanup::New();
       
  4439 	if (!trap)
       
  4440 		return KErrNoMemory;
       
  4441    	test.Title();
       
  4442    RunModeAgent = CRunModeAgent::NewL();
       
  4443    if (RunModeAgent != NULL)
       
  4444        {
       
  4445         __UHEAP_MARK;
       
  4446 	    TRAP(ret,RunModeAgent->ClientAppL());
       
  4447 	    __UHEAP_MARKEND;
       
  4448 
       
  4449 	    delete RunModeAgent;
       
  4450        }
       
  4451 
       
  4452 	delete trap;
       
  4453 
       
  4454 	return ret;
       
  4455 	}
       
  4456 
       
  4457 /**
       
  4458 Helper function to get the aOffset'th value from aFlags
       
  4459 
       
  4460 @param aFlags descriptor containing TRegisterFlag type flags
       
  4461 @param aOffset index of flag value to extract from aFlags
       
  4462 @param aFlagValue the flag value if function returned successfully
       
  4463 
       
  4464 @return KErrNone if value was read successfully, KErrTooBig if aOffset is
       
  4465         greater than aFlags.Length()
       
  4466 */
       
  4467 TInt CRunModeAgent::GetFlag(const TDes8& aFlags, const TUint aOffset, TRegisterFlag &aFlagValue) const
       
  4468 	{
       
  4469 	//get pointer to data
       
  4470 	const TUint8 *ptr = aFlags.Ptr();
       
  4471 
       
  4472 	//check aOffset is valid
       
  4473 	TUint length = aFlags.Length();
       
  4474 	if(aOffset >= length)
       
  4475 		return KErrTooBig;
       
  4476 
       
  4477 	//get flag value
       
  4478 	aFlagValue = (TRegisterFlag)ptr[aOffset];
       
  4479 	return KErrNone;
       
  4480 	}
       
  4481 
       
  4482 /**
       
  4483   Helper function to set the value of FunctionChooser in the target debug thread.
       
  4484 
       
  4485   @param aTestFunction TTestFunction enum to set FunctionChooser to
       
  4486 
       
  4487   @return KErrNone if the value was set correctly, or one of the other system wide error codes
       
  4488   */
       
  4489 TInt CRunModeAgent::SwitchTestFunction(TTestFunction aTestFunction, const TBool aResume)
       
  4490 	{
       
  4491 	//suspend the target thread
       
  4492 	TInt suspendError = iServSession.SuspendThread(iThreadID);
       
  4493 	if(! ( (suspendError == KErrNone) || (suspendError == KErrAlreadyExists) ) )
       
  4494 		{
       
  4495 		//the thread is not suspended so exit
       
  4496 		return suspendError;
       
  4497 		}
       
  4498 
       
  4499 	//get the address of FunctionChooser
       
  4500 	TUint32 functionChooserAddress = (TUint32)&FunctionChooser;
       
  4501 	//put the new value for FunctionChooser into a descriptor
       
  4502 	TPtr8 functionBuf((TUint8*)&aTestFunction, sizeof(TTestFunction), sizeof(TTestFunction));
       
  4503 	//write the new value into the target thread
       
  4504 	TInt writeError = iServSession.WriteMemory(iThreadID, functionChooserAddress, sizeof(TTestFunction), functionBuf, EAccess32, EEndLE8);
       
  4505 
       
  4506 	if( (KErrNone == suspendError) && aResume )
       
  4507 		{
       
  4508 		//if this function suspended the target thread then we need to resume it
       
  4509 		TInt resumeError = iServSession.ResumeThread(iThreadID);
       
  4510 		if(KErrNone != resumeError)
       
  4511 			{
       
  4512 			//resuming failed so return the error
       
  4513 			return resumeError;
       
  4514 			}
       
  4515 		}
       
  4516 
       
  4517 	//suspending and resuming was successful so return the error code from the WriteMemory call
       
  4518 	return writeError;
       
  4519 	}
       
  4520 
       
  4521 /**
       
  4522   Launch a separate process to debug.
       
  4523 
       
  4524   @param aProcess the RProcess object to use to create the process
       
  4525   @param aFileName file name of the executable to create the process from
       
  4526   @param aFunctionType function that the target process should call on execution
       
  4527   @param aDelay delay before the new process should call the function represented by aFunctionType
       
  4528   @param aExtraThreads number of extra threads to create in the child process
       
  4529 
       
  4530   @return KErrNone on success, or one of the other system wide error codes
       
  4531   */
       
  4532 TInt CRunModeAgent::LaunchProcess(RProcess& aProcess, const TDesC& aFileName, TDebugFunctionType aFunctionType, TUint32 aDelay, TUint32 aExtraThreads)
       
  4533 	{
       
  4534 	// at the moment we support two arguments, this number might have to be increased to support arguments
       
  4535 	const TUint KMaxCommandLineLength = 32;
       
  4536 
       
  4537 	// create a command line buffer
       
  4538 	RBuf commandLine;
       
  4539 	commandLine.Create(KMaxCommandLineLength);
       
  4540 
       
  4541 	// append the command line arguments to the buffer
       
  4542 	_LIT(KFArg, "-f");
       
  4543 	commandLine.Append(KFArg());
       
  4544 	commandLine.AppendNum(aFunctionType);
       
  4545 
       
  4546 	_LIT(KSpace, " ");
       
  4547 	commandLine.Append(KSpace());
       
  4548 
       
  4549 	_LIT(KDArg, "-d");
       
  4550 	commandLine.Append(KDArg());
       
  4551 	commandLine.AppendNum(aDelay);
       
  4552 
       
  4553 	commandLine.Append(KSpace());
       
  4554 
       
  4555 	_LIT(KEArg, "-e");
       
  4556 	commandLine.Append(KEArg());
       
  4557 	commandLine.AppendNum(aExtraThreads);
       
  4558 
       
  4559 	// create the new process, matching on file name only, not specifying uid values
       
  4560 	TInt err = aProcess.Create(aFileName, commandLine);	// owned by the process
       
  4561 
       
  4562 	// check that there was no error raised
       
  4563 	if(err != KErrNone)
       
  4564 		{
       
  4565 		commandLine.Close();
       
  4566 		return err;
       
  4567 		}
       
  4568 
       
  4569 	TRequestStatus status = KRequestPending;
       
  4570 	aProcess.Rendezvous(status);
       
  4571 
       
  4572 	commandLine.Close();	// after target thread starts
       
  4573 
       
  4574 	if(KRequestPending != status.Int())
       
  4575 		{
       
  4576 		// startup failed so kill the process
       
  4577 		aProcess.Kill(KErrNone);
       
  4578 		return status.Int();
       
  4579 		}
       
  4580 	else
       
  4581 		{
       
  4582 		// start up succeeded so resume the process
       
  4583 		aProcess.Resume();
       
  4584 		User::WaitForRequest(status);
       
  4585 		if(KErrNone != status.Int())
       
  4586 			{
       
  4587 			aProcess.Kill(KErrNone);
       
  4588 			}
       
  4589 		return status.Int();
       
  4590 		}
       
  4591 	}
       
  4592 
       
  4593 /**
       
  4594   Helper function to read a tag header from a debug functionality block
       
  4595 
       
  4596   @param aDebugFunctionalityBlock block to read header from
       
  4597   @param aTagHdrId header type to find
       
  4598 
       
  4599   @return pointer to the header, or NULL if not available
       
  4600   */
       
  4601 TTagHeader* CRunModeAgent::GetTagHdr(const TDesC8& aDebugFunctionalityBlock, const TTagHeaderId aTagHdrId) const
       
  4602 	{
       
  4603 	TUint8* ptr = (TUint8*) aDebugFunctionalityBlock.Ptr();
       
  4604 	TUint8* blockEnd = ptr + aDebugFunctionalityBlock.Size();
       
  4605 
       
  4606 	while(ptr < blockEnd)
       
  4607 		{
       
  4608 		TTagHeader* header = (TTagHeader*)ptr;
       
  4609 		if(header->iTagHdrId == aTagHdrId)
       
  4610 			{
       
  4611 			return header;
       
  4612 			}
       
  4613 		ptr += sizeof(TTagHeader) + (header->iNumTags * sizeof(TTag));
       
  4614 		}
       
  4615 	return NULL;
       
  4616 	}
       
  4617 
       
  4618 /**
       
  4619   Helper function to read a tag from a debug functionality block
       
  4620 
       
  4621   @param aTagHdr pointer to a tag header in a debug functionality block
       
  4622   @param aElement element to return from the header's data
       
  4623 
       
  4624   @return pointer to the tag, or NULL if not available
       
  4625   */
       
  4626 TTag* CRunModeAgent::GetTag(const TTagHeader* aTagHdr, const TInt aElement) const
       
  4627 	{
       
  4628 	TUint8* ptr = (TUint8*)aTagHdr + sizeof(TTagHeader);
       
  4629 	TUint8* blockEnd = ptr + (aTagHdr->iNumTags * sizeof(TTag));
       
  4630 
       
  4631 	while(ptr < blockEnd)
       
  4632 		{
       
  4633 		TTag* tag = (TTag*)ptr;
       
  4634 		if(tag->iTagId == aElement)
       
  4635 			{
       
  4636 			return tag;
       
  4637 			}
       
  4638 		ptr += sizeof(TTag);
       
  4639 		}
       
  4640 	return NULL;
       
  4641 	}
       
  4642 
       
  4643 TTag CRunModeAgent::GetTag(const TTagHeaderId aTagHdrId, const TInt aElement)
       
  4644 	{
       
  4645 	TUint32 bufsize = 0;	// Safe default size
       
  4646 
       
  4647 	// Get functionality block size
       
  4648 	test(KErrNone == iServSession.GetDebugFunctionalityBufSize(&bufsize));
       
  4649 
       
  4650 	// Ensure we have a finite buffer size
       
  4651 	test(bufsize!=0);
       
  4652 
       
  4653 	// Allocate space for the functionality data
       
  4654 	HBufC8* dftext = HBufC8::NewLC(bufsize);
       
  4655 
       
  4656 	// create an empty TPtr8 refering to dftext
       
  4657 	TPtr8 dftextPtr(dftext->Des());
       
  4658 
       
  4659 	// Get the functionality block
       
  4660 	test(KErrNone == iServSession.GetDebugFunctionality(dftextPtr));
       
  4661 
       
  4662 	// read a value from the data to check it has come through as expected
       
  4663 	TTagHeader* header = GetTagHdr(dftext->Des(), aTagHdrId);
       
  4664 	test(header != NULL);
       
  4665 	TTag* tag = GetTag(header, aElement);
       
  4666 	test(tag != NULL);
       
  4667 
       
  4668 	TTag tagToReturn = *tag;
       
  4669 
       
  4670 	// Remove our temporary buffer
       
  4671 	CleanupStack::PopAndDestroy(dftext);
       
  4672 
       
  4673 	return tagToReturn;
       
  4674 	}
       
  4675 
       
  4676 /**
       
  4677   Helper function which returns a Boolean indicating with a process with the
       
  4678   specified name is currently running.
       
  4679 
       
  4680   @param aProcessName - Name of the process to find
       
  4681   @return ETrue if found, EFalse otherwise
       
  4682   */
       
  4683 TBool CRunModeAgent::ProcessExists(const TDesC& aProcessName)
       
  4684 	{
       
  4685 	TInt    err=KErrNone;
       
  4686 	TBool	found = FALSE;
       
  4687 
       
  4688 _LIT(KWildCard,"*");
       
  4689 
       
  4690 	TFindProcess find(KWildCard);
       
  4691 	TFullName name;
       
  4692 	while(find.Next(name)==KErrNone)
       
  4693 		{
       
  4694 		RProcess process;
       
  4695 		err = process.Open(find);
       
  4696 		if (err == KErrNone)
       
  4697 			{
       
  4698 			if (name.Find(aProcessName) != KErrNotFound)
       
  4699 				{
       
  4700 					found = TRUE;
       
  4701 				}
       
  4702 			process.Close();
       
  4703 			}
       
  4704 	   }
       
  4705 
       
  4706 	return found;
       
  4707 	}
       
  4708 
       
  4709 TInt PanicFn(TAny*)
       
  4710 	{
       
  4711 	User::Panic(_L("trmdebug_dummy"), 123);
       
  4712 	return 0;
       
  4713 	}
       
  4714 
       
  4715 void CRunModeAgent::TestAttachToAll()
       
  4716 	{
       
  4717 	test.Next(_L("TestAttachToAll - Attach\n"));
       
  4718 
       
  4719 #ifdef ALLCAPS_DEBUGTOKEN
       
  4720 	test.Next(_L("---- First AttachAll \n"));
       
  4721 	test(iServSession.AttachAll() == KErrNone);
       
  4722 	test.Next(_L("---- Second AttachAll \n"));	
       
  4723 	test(iServSession.AttachAll() == KErrAlreadyExists); // Don't think an agent should be allowed to AttachToAll more than once
       
  4724 	
       
  4725 	test.Next(_L("---- DetachAll\n"));
       
  4726 	test(iServSession.DetachAll() == KErrNone);
       
  4727 	test.Next(_L("---- AttachAll again\n"));
       
  4728 	test(iServSession.AttachAll() == KErrNone);
       
  4729 	
       
  4730 	test.Next(_L("---- Suspend thread\n"));
       
  4731 	test( iServSession.SuspendThread(iThreadID) == KErrNone);
       
  4732 
       
  4733 	// Check that AttachAll picks up thread crashes without needing to be explicitly attached
       
  4734 	test.Next(_L("---- Attach all SetEventAction\n"));
       
  4735 	TInt err = iServSession.SetEventAction(EEventsKillThread, EActionSuspend);
       
  4736 	test(err == KErrNone);
       
  4737 
       
  4738 	test.Next(_L("---- Create DebugThread2\n"));
       
  4739 	// Set up the thread
       
  4740 	RThread threadToPanic;
       
  4741 	err = threadToPanic.Create(_L("DebugThread2"), &PanicFn, 8192, NULL, NULL);
       
  4742 	test(err == KErrNone);
       
  4743 	TRequestStatus undertakerStat;
       
  4744 	threadToPanic.Logon(undertakerStat);
       
  4745 	test(undertakerStat.Int() == KRequestPending);
       
  4746 
       
  4747 	// Start listening for events
       
  4748 	TRequestStatus stat;
       
  4749 	TEventInfo info;
       
  4750 	TPckg<TEventInfo> infoPkg(info);
       
  4751 	test.Next(_L("Attach all get event and then resume thread DebugThread2\n"));
       
  4752 	
       
  4753 	iServSession.GetEvent(stat, infoPkg);
       
  4754 	
       
  4755 	threadToPanic.Resume();
       
  4756 	
       
  4757 	test.Printf(_L("Waiting for DebugThread2 panic event to be picked up by AttachToAll\n"));
       
  4758 	User::WaitForRequest(stat);
       
  4759 	test(stat.Int() == KErrNone);
       
  4760 	test(info.iThreadId == threadToPanic.Id());
       
  4761 	test(info.iEventType == EEventsKillThread);
       
  4762 	test(info.iThreadKillInfo.iExitType == EExitPanic);
       
  4763 
       
  4764 	test(undertakerStat.Int() == KRequestPending); // This shouldn't get completed until after we call iServSession.ResumeThread below
       
  4765 
       
  4766 	// Now resume the thread and wait for the Logon to complete
       
  4767 	test.Next(_L("---- Attach all resume panic thread and then wait for event after DSS has handled it\n"));
       
  4768 	err = iServSession.ResumeThread(threadToPanic.Id());
       
  4769 	test(err == KErrNone);
       
  4770 	User::WaitForRequest(undertakerStat);
       
  4771 	test(undertakerStat.Int() == 123); // The panic reason set in PanicFn is 123
       
  4772 
       
  4773 	// And clean up, 
       
  4774 	ResetAttachToAll(threadToPanic);
       
  4775 	//still attached to all
       
  4776 	
       
  4777 	// Test that an explicit attach eclipses an AttachAll, and the AttachAll session 
       
  4778 	// doesn't see the events for specifically attached executables
       
  4779 	test.Next(_L(" ---- ExplicitAttachBeatsAttachAll\n"));
       
  4780 
       
  4781 	// We shouldn't see this event because of sess2
       
  4782 	err = iServSession.SetEventAction(EEventsStartThread, EActionContinue); 
       
  4783 	test(err == KErrNone);
       
  4784 	iServSession.GetEvent(stat, infoPkg);
       
  4785 	test(stat.Int() == KRequestPending);
       
  4786 
       
  4787 	test.Next(_L("---- New sec session\n"));
       
  4788 	RSecuritySvrSession sess2;
       
  4789 	test(sess2.Connect(securityServerVersion) == KErrNone);
       
  4790 	test.Next(_L("---- New sec session Attach executable \n"));
       
  4791 	test(sess2.AttachExecutable(iFileName, EFalse) == KErrNone);
       
  4792 	err = sess2.SetEventAction(iFileName, EEventsKillThread, EActionSuspend);
       
  4793 	test(err == KErrNone);
       
  4794 	// The EActionSuspend above should trump this EActionContinue
       
  4795 	err = iServSession.SetEventAction(EEventsKillThread, EActionContinue); 
       
  4796 	test(err == KErrNone);
       
  4797 
       
  4798 	test.Next(_L("---- New sec session create DebugThread3\n"));
       
  4799 	err = threadToPanic.Create(_L("DebugThread3"), &PanicFn, 8192, NULL, NULL);
       
  4800 	test(err == KErrNone);
       
  4801 	
       
  4802 	// The attach executable above leads the DSS to launch the token, which results 
       
  4803 	// in a start thread event, and since we have done an attach all and a get event, 
       
  4804 	// the TReqStat will be completed accordingly for this token start event. 
       
  4805 	
       
  4806 	threadToPanic.Logon(undertakerStat);
       
  4807 	test(undertakerStat.Int() == KRequestPending); 
       
  4808 
       
  4809 	TRequestStatus sess2stat;
       
  4810 	TEventInfo sess2event;
       
  4811 	TPckg<TEventInfo> sess2eventPkg(sess2event);
       
  4812 	test.Next(_L("---- New sec session get event, TReqStat\n") );
       
  4813 	RDebug::Printf(" undertakerStat=0x%x, sess2stat = 0x%x, Pkg=0x%x", 
       
  4814 			&undertakerStat, &sess2stat, &sess2eventPkg);
       
  4815 	
       
  4816 	sess2.GetEvent(iFileName, sess2stat, sess2eventPkg);
       
  4817 	// sess2 didn't ask for EEventsStartThread so we should still be pending at this point	
       
  4818 	test(sess2stat == KRequestPending); 
       
  4819 
       
  4820 	test.Next(_L("---- New sec session resume thread and wait for kill event\n"));
       
  4821 	threadToPanic.Resume();
       
  4822 	User::WaitForRequest(sess2stat);
       
  4823 
       
  4824 	test(sess2stat.Int() == KErrNone);
       
  4825 	test(sess2event.iThreadId == threadToPanic.Id());
       
  4826 	test(sess2event.iEventType == EEventsKillThread);
       
  4827 	test(sess2event.iThreadKillInfo.iExitType == EExitPanic);
       
  4828 
       
  4829 	// the EActionSuspend that sess2 specified should ensure this doesn't get completed
       
  4830 	test(undertakerStat == KRequestPending);
       
  4831 
       
  4832 	// Now resume the thread and wait for the Logon to complete
       
  4833 	test.Next(_L("---- ExplicitAttachBeatsAttachAll resume thread 3 after kill\n"));
       
  4834 	err = sess2.ResumeThread(threadToPanic.Id());
       
  4835 	test(err == KErrNone);
       
  4836 	User::WaitForRequest(undertakerStat);
       
  4837 	test(undertakerStat.Int() == 123); // The panic reason set in PanicFn is 123
       
  4838 
       
  4839 	// And clean up
       
  4840 	ResetAttachToAll(threadToPanic, &stat, &sess2);
       
  4841 	test.Next(_L("---- Finishing ExplicitAttachBeatsAttachAll > sess2.Close\n"));
       
  4842 	
       
  4843 	sess2.Close();
       
  4844 #if 0
       
  4845 	//TODO allow this by changing from agent pid to session ids in DSS.
       
  4846 	// This will allow a client to have more than one session and call attachall
       
  4847 	
       
  4848 	// Test that a second AttachAll eclipses the first
       
  4849 	// Commented out since not sure we require this
       
  4850 
       
  4851 	test.Next(_L("SecondAttachAllBeatsAttachAll"));
       
  4852 	
       
  4853 	//TODO fix detachall in ResetAttachToAll
       
  4854 	test(iServSession.AttachAll() == KErrNone);
       
  4855 	test(sess2.AttachAll() == KErrNone);
       
  4856 	err = iServSession.SetEventAction(EEventsKillThread, EActionSuspend);
       
  4857 	test(err == KErrNone);
       
  4858 	err = sess2.SetEventAction(EEventsKillThread, EActionSuspend);
       
  4859 	test(err == KErrNone);
       
  4860 	err = threadToPanic.Create(_L("DebugThread4"), &PanicFn, 8192, NULL, NULL);
       
  4861 	test(err == KErrNone);
       
  4862 	iServSession.GetEvent(stat, infoPkg);
       
  4863 	test(stat.Int() == KRequestPending);
       
  4864 	sess2.GetEvent(sess2stat, sess2eventPkg);
       
  4865 	test(sess2stat.Int() == KRequestPending);
       
  4866 
       
  4867 	threadToPanic.Resume();
       
  4868 	User::WaitForRequest(sess2stat);
       
  4869 	test(sess2event.iThreadId == threadToPanic.Id());
       
  4870 	test(sess2event.iEventType == EEventsKillThread);
       
  4871 	test(sess2event.iThreadKillInfo.iExitType == EExitPanic);
       
  4872 	test(stat.Int() == KRequestPending); // Shouldn't see the killthread event because of sess2
       
  4873 
       
  4874 	// And cleanup
       
  4875 	ResetAttachToAll(threadToPanic, &stat, &sess2);
       
  4876 	//TODO fixme test(sess2.DetachAll() == KErrNone);
       
  4877 #endif
       
  4878 
       
  4879 #else
       
  4880 	test(iServSession.AttachAll() == KErrPermissionDenied);
       
  4881 #endif
       
  4882 	}
       
  4883 
       
  4884 void CRunModeAgent::ResetAttachToAll(RThread& aTestThread, TRequestStatus* aFirstSessionStat, RSecuritySvrSession* aSecondSession)
       
  4885 	{
       
  4886 	
       
  4887 	aTestThread.Close();
       
  4888 
       
  4889 	RDebug::Printf("---- ResetAttachToAll : > iServSession.SetEventAction Ignore for Kill and StartThread");
       
  4890 	TInt err = iServSession.SetEventAction(EEventsKillThread, EActionIgnore);
       
  4891 	test(err == KErrNone);
       
  4892 	err = iServSession.SetEventAction(EEventsStartThread, EActionIgnore);
       
  4893 	test(err == KErrNone);
       
  4894 
       
  4895 
       
  4896 	if (aFirstSessionStat)
       
  4897 		{
       
  4898 		RDebug::Printf("---- ResetAttachToAll : > iServSession.CancelGetEvent");
       
  4899 		iServSession.CancelGetEvent();
       
  4900 		
       
  4901 		RDebug::Printf("---- ResetAttachToAll : > User::WaitForRequest(*aFirstSessionStat);");
       
  4902 		User::WaitForRequest(*aFirstSessionStat);
       
  4903 		
       
  4904 		User::After(1000000);
       
  4905 		RDebug::Printf("---- ResetAttachToAll : > iServSession.DetachAll");
       
  4906 		test(iServSession.DetachAll() == KErrNone);
       
  4907 		}
       
  4908 
       
  4909 	if (aSecondSession != NULL)
       
  4910 		{
       
  4911 		User::After(1000000);
       
  4912 		RDebug::Printf("---- ResetAttachToAll : > aSecondSession.SetEventAction kill ignore");
       
  4913 		err = aSecondSession->SetEventAction(iFileName, EEventsKillThread, EActionIgnore);
       
  4914 		User::After(1000000);
       
  4915 		test(err == KErrNone);
       
  4916 		RDebug::Printf("---- ResetAttachToAll : > aSecondSession.SetEventAction start thrd ignore");
       
  4917 		err = aSecondSession->SetEventAction(iFileName, EEventsStartThread, EActionIgnore);
       
  4918 		User::After(1000000);
       
  4919 		test(err == KErrNone);
       
  4920 		RDebug::Printf("---- ResetAttachToAll : > aSecondSession.DetachExecutable");
       
  4921 		err = aSecondSession->DetachExecutable(iFileName);
       
  4922 		User::After(1000000);
       
  4923 		test( err == KErrNone);
       
  4924 		}
       
  4925 	}
       
  4926 
       
  4927 void CRunModeAgent::TestResumeBreakpointsRepeatedly()
       
  4928 	{
       
  4929 	test.Next(_L("TestResumeBreakpointsRepeatedly\n"));
       
  4930 	test(iServSession.AttachExecutable(KRMDebugTestApplication, EFalse/*  Active */) == KErrNone);
       
  4931 
       
  4932 	RProcess debugProcess;
       
  4933 	TThreadId debugThreadId;
       
  4934 	LaunchDebugProcessAndSetBreakpoint(debugProcess, debugThreadId);
       
  4935 	test(iServSession.ResumeThread(debugThreadId) == KErrNone);
       
  4936 	
       
  4937 	// Let the thread die naturally (at least from DSS's point of view)
       
  4938 	debugProcess.Kill(0);
       
  4939 	debugProcess.Close();
       
  4940 
       
  4941 	test.Printf(_L("Closing iServSession\n"));
       
  4942 	iServSession.Close();
       
  4943 	//User::After(1000000); // I hate myself...
       
  4944 	test(iServSession.Connect(securityServerVersion) == KErrNone);
       
  4945 	test(iServSession.AttachExecutable(KRMDebugTestApplication, EFalse/*  Active */) == KErrNone);
       
  4946 
       
  4947 	test.Printf(_L("Launching process for second time\n"));
       
  4948 
       
  4949 	LaunchDebugProcessAndSetBreakpoint(debugProcess, debugThreadId);
       
  4950 	test(iServSession.ResumeThread(debugThreadId) == KErrNone);
       
  4951 	debugProcess.Kill(0);
       
  4952 	debugProcess.Close();
       
  4953 
       
  4954 	/*test.Printf(_L("Launching process for third time\n"));
       
  4955 	debugProcess.Kill(0);
       
  4956 	debugProcess.Close();
       
  4957 	iServSession.Close();
       
  4958 	User::After(1000000); // I hate myself...
       
  4959 	test(iServSession.Connect(securityServerVersion) == KErrNone);
       
  4960 	test(iServSession.AttachExecutable(KRMDebugTestApplication, EFalse/ *  Active * /) == KErrNone);
       
  4961 	*/
       
  4962 
       
  4963 	test(KErrNone == iServSession.DetachExecutable(KRMDebugTestApplication));
       
  4964 	}
       
  4965 
       
  4966 void CRunModeAgent::TimedWait(TRequestStatus& aStatus, TInt aTimeoutInMs, TInt aLineNumber)
       
  4967 	{
       
  4968 	RTimer timer;
       
  4969 	TInt err = timer.CreateLocal();
       
  4970 	test(err == KErrNone);
       
  4971 
       
  4972 	TRequestStatus timerstat;
       
  4973 	timer.After(timerstat, aTimeoutInMs*1000);
       
  4974 	User::WaitForRequest(aStatus, timerstat);
       
  4975 	if (timerstat != KRequestPending)
       
  4976 		{
       
  4977 		test.Panic(_L("Timed out at line %d\n"), aLineNumber);
       
  4978 		}
       
  4979 	else
       
  4980 		{
       
  4981 		timer.Cancel();
       
  4982 		User::WaitForRequest(timerstat);
       
  4983 		}
       
  4984 	timer.Close();
       
  4985 	}