debugsrv/runmodedebug/rmdriver/src/d_rmd_breakpoints.cpp
branchRCL_3
changeset 21 52e343bb8f80
parent 20 ca8a1b6995f6
child 22 e26895079d7c
equal deleted inserted replaced
20:ca8a1b6995f6 21:52e343bb8f80
     1 // Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <e32def.h>
       
    18 #include <e32def_private.h>
       
    19 #include <e32cmn.h>
       
    20 #include <e32cmn_private.h>
       
    21 #include <u32std.h>
       
    22 #include <kernel/kernel.h>
       
    23 #include <kernel/kern_priv.h>
       
    24 #include <nk_trace.h>
       
    25 #include <arm.h>
       
    26 #include <kernel/cache.h>
       
    27 #include <platform.h>
       
    28 #include <nkern.h>
       
    29 #include <u32hal.h>
       
    30 
       
    31 #include <rm_debug_api.h>
       
    32 #include "d_rmd_breakpoints.h"
       
    33 #include "d_process_tracker.h"
       
    34 #include "d_rmd_stepping.h"
       
    35 #include "rm_debug_kerneldriver.h"	// needed to access DRM_DebugChannel
       
    36 #include "rm_debug_driver.h"
       
    37 #include "debug_utils.h"
       
    38 #include "debug_logging.h"
       
    39 
       
    40 using namespace Debug;
       
    41 
       
    42 /* @internalTechnology
       
    43  *
       
    44  * Checks whether aAddress is correctly aligned for placing a breakpoint of
       
    45  * cpu architecture aMode.
       
    46  *
       
    47  * @param aAddress - Virtual memory address to check
       
    48  * @param aMode - The CPU architecture mode of the breakpoint to be placed at aAddress
       
    49  * @return ETrue if aAddress is suitably aligned, EFalse otherwise.
       
    50  */
       
    51 TBool D_RMD_Breakpoints::Aligned(TUint32 aAddress, Debug::TArchitectureMode aMode)
       
    52 	{
       
    53 	switch(aMode)
       
    54 		{
       
    55 		case Debug::EArmMode:
       
    56 			// ARM breakpoints must be 32-bit aligned (lower two bits must be zero)
       
    57 			if (aAddress & 0x3)
       
    58 			{
       
    59 				// Not 32-bit aligned.
       
    60 				return EFalse;
       
    61 			}
       
    62 			break;
       
    63 		case Debug::EThumbMode:
       
    64 			// Thumb breakpoints must be 16-bit aligned (low bit must be zero)
       
    65 			if (aAddress & 0x1)
       
    66 			{
       
    67 				// Not 16-bit aligned
       
    68 				return EFalse;
       
    69 			}
       
    70 			break;
       
    71 		case Debug::EThumb2EEMode:
       
    72 			// Thumb-EE instructions are half-word aligned. See ARM ARM DDI0406A, section A3.2 Alignment Support
       
    73 			// Note that some instructions need to be word-aligned, but this function does not know which ones.
       
    74 			// It may also depend on the System Control register U bit.
       
    75 			if (aAddress & 0x1)
       
    76 			{
       
    77 				// Not 16-bit aligned
       
    78 				return EFalse;
       
    79 			}
       
    80 			break;
       
    81 		default:
       
    82 			{
       
    83 			// No idea
       
    84 			return EFalse;
       
    85 			}
       
    86 		}
       
    87 
       
    88 	// Must be OK
       
    89 	return ETrue;
       
    90 	};
       
    91 
       
    92 /* @internalTechnology
       
    93  *
       
    94  * Returns the size of a breakpoint of architecture aMode in bytes
       
    95  * 
       
    96  * @param aMode - The architure of the breakpoint
       
    97  * @return The size of the breakpoints in bytes. 0 if un-recognised architecture.
       
    98  */
       
    99 TInt D_RMD_Breakpoints::BreakSize(Debug::TArchitectureMode aMode)
       
   100 	{
       
   101 	switch(aMode)
       
   102 		{
       
   103 		case Debug::EArmMode:
       
   104 			{
       
   105 				return 4;
       
   106 			}
       
   107 		case Debug::EThumbMode:
       
   108 			{
       
   109 				return 2;
       
   110 			}
       
   111 		case Debug::EThumb2EEMode:
       
   112 			{
       
   113 			// Only needs to be two bytes in size.
       
   114 			return 2;
       
   115 			}
       
   116 		default:
       
   117 			{
       
   118 				// No idea
       
   119 				return 0;
       
   120 			}
       
   121 		}
       
   122 	};
       
   123 
       
   124 /* @internalTechnology
       
   125  *
       
   126  * Checks whether two TBreakEntrys overlap
       
   127  *
       
   128  * @param aFirst - A TBreakEntry with valid iAddress and iMode fields.
       
   129  * @param aSecond  - A TBreakEntry with valid iAddress and iMode fields.
       
   130  * @return ETrue if the aFirst and aSecond overlap or the overlap cannot be determined
       
   131  *         , EFalse otherwise
       
   132  */
       
   133 TBool D_RMD_Breakpoints::BreakpointsOverlap(TBreakEntry& aFirst, TBreakEntry& aSecond)
       
   134 	{
       
   135 	TInt firstSize = BreakSize(aFirst.iMode);
       
   136 	TInt secondSize = BreakSize(aSecond.iMode);
       
   137 
       
   138 	// Do we know the size of each breakpoint?
       
   139 	if ((firstSize <= 0) || (secondSize <= 0))
       
   140 		{
       
   141 		// We don't know the size of the breakpoint, so assume they overlap
       
   142 		return ETrue;
       
   143 		}
       
   144 
       
   145 	TInt firstStartAddress = aFirst.iAddress;
       
   146 	TInt secondStartAddress = aSecond.iAddress;
       
   147 	TInt firstEndAddress = firstStartAddress + firstSize - 1;
       
   148 	TInt secondEndAddress = secondStartAddress + secondSize - 1;
       
   149 
       
   150 	// If second breakpoint is past the end of the first then we're ok
       
   151 	if(firstEndAddress < secondStartAddress)
       
   152 		{
       
   153 		return EFalse;
       
   154 		}
       
   155 
       
   156 	// If first breakpoint is past the end of the second then we're ok
       
   157 	if(secondEndAddress < firstStartAddress)
       
   158 		{
       
   159 		return EFalse;
       
   160 		}
       
   161 
       
   162 	// The breakpoints overlap
       
   163 	return ETrue;
       
   164 	}
       
   165 
       
   166 /* @internalTechnology
       
   167  * 
       
   168  * Returns the breakpoint bitpattern to use for each architecture type
       
   169  *
       
   170  * @param aMode - the cpu architecture type
       
   171  * @return The bit-pattern to use for the specified architecture, or 0 if unsupported.
       
   172  */
       
   173 TUint32 D_RMD_Breakpoints::BreakInst(Debug::TArchitectureMode aMode)
       
   174 	{
       
   175 	switch(aMode)
       
   176 		{
       
   177 		case Debug::EArmMode:
       
   178 			{
       
   179 				return KArmBreakPoint;
       
   180 			}
       
   181 		case Debug::EThumbMode:
       
   182 			{
       
   183 				return KThumbBreakPoint;
       
   184 			}
       
   185 		case Debug::EThumb2EEMode:
       
   186 			{
       
   187 			return KT2EEBreakPoint;
       
   188 			}
       
   189 		default:
       
   190 			{
       
   191 				// No idea what the breakpoint should be
       
   192 				return 0;
       
   193 			}
       
   194 		}
       
   195 	};
       
   196 
       
   197 /**
       
   198 Constructor. Initialises its internal list of empty breakpoints.
       
   199 */
       
   200 D_RMD_Breakpoints::D_RMD_Breakpoints(DRM_DebugChannel* aChannel)
       
   201 : iBreakPointList(NUMBER_OF_TEMP_BREAKPOINTS, 0),
       
   202   iNextBreakId(NUMBER_OF_TEMP_BREAKPOINTS),
       
   203   iChannel(aChannel),
       
   204   iInitialised(EFalse)
       
   205 	{
       
   206 	iBreakPointList.Reset();	
       
   207 	TBreakEntry emptyTempBreak;
       
   208 	
       
   209 	for (TInt i = 0; i < NUMBER_OF_TEMP_BREAKPOINTS; i++)
       
   210 		{
       
   211 		emptyTempBreak.iBreakId = i;
       
   212 		
       
   213 		if (KErrNone != iBreakPointList.Append(emptyTempBreak))
       
   214 			{
       
   215 			LOG_MSG("D_RMD_Breakpoints::D_RMD_Breakpoints() - Error appending blank temp break entry");
       
   216 			}
       
   217 		}
       
   218 	}
       
   219 
       
   220 /**
       
   221 Destructor. Clears all the breakpoints in the system, deletes its internal list of breakpoints,
       
   222 and closes the exclusivity semaphore.
       
   223 */
       
   224 D_RMD_Breakpoints::~D_RMD_Breakpoints()
       
   225 	{
       
   226 	ClearAllBreakPoints();
       
   227 	
       
   228 	// close the breakpoint list and free the memory associated with it
       
   229 	iBreakPointList.Close();
       
   230 
       
   231 	if (iLock)
       
   232 		iLock->Close(NULL);
       
   233 	}
       
   234 
       
   235 /**
       
   236 Initialises the breakpoint list exclusion semaphore. This should be called once immediately after
       
   237 the constructor.
       
   238 
       
   239 @return KErrNone if successful, one of the other system wide error codes otherwise.
       
   240 */
       
   241 TInt D_RMD_Breakpoints::Init()
       
   242 	{
       
   243 	TInt err = KErrNone;
       
   244 
       
   245 	// Only create a semaphore if we are not initialised
       
   246 	if(!iInitialised)
       
   247 		{
       
   248 		// Initialise the semaphore ensuring exclusive access to the breakpoint list
       
   249 		err = Kern::SemaphoreCreate(iLock, _L("RM_DebugBreakpointLock"), 1 /* Initial count */);
       
   250 		if (err == KErrNone)
       
   251 			{
       
   252 			iInitialised = ETrue;
       
   253 			}
       
   254 		}
       
   255 	else
       
   256 		{
       
   257 		err = KErrNone;
       
   258 		}
       
   259 
       
   260 	return err;
       
   261 	}
       
   262 
       
   263 /** 
       
   264 Public member function which sets a thread-specific breakpoint in the specified thread
       
   265 and returns an opaque handle to the caller.
       
   266 
       
   267 Note 1:
       
   268 This function ensures exclusive access to the breakpoint data structures
       
   269 by using a semaphore to serialise access.
       
   270 
       
   271 @see priv_DoSetBreak
       
   272 
       
   273 Note 2:
       
   274 As implied by Note 1, the caller must have previously called Init() or this
       
   275 function will return KErrNotReady;
       
   276  
       
   277 @param aBreakId - Reference to a TUint32 into which the function will return a unique breakpoint Id.
       
   278 @param aThreadId - The thread Id in which to place the breakpoint
       
   279 @param aAddress - Address to place the breakpoint
       
   280 @param aMode - The cpu instruction set architecture type breakpoint (e.g. EArmMode or EThumbMode)
       
   281 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   282 */
       
   283 TInt D_RMD_Breakpoints::DoSetBreak(TInt32 &aBreakId, const TUint64 aId, const TBool aThreadSpecific, const TUint32 aAddress, const TArchitectureMode aMode)
       
   284 	{
       
   285 	// Ensure we have a valid semaphore
       
   286 	if (!iInitialised || !iLock)
       
   287 		{
       
   288 		return KErrNotReady;
       
   289 		}
       
   290 
       
   291 	// Acquire the lock
       
   292 	NKern::ThreadEnterCS();
       
   293 	Kern::SemaphoreWait(*iLock);
       
   294 
       
   295 	// Really do the work
       
   296 	TInt err = priv_DoSetBreak(aBreakId, aId, aThreadSpecific, aAddress,aMode);
       
   297 	
       
   298 	// Release the lock
       
   299 	Kern::SemaphoreSignal(*iLock);
       
   300 	NKern::ThreadLeaveCS();
       
   301 	
       
   302 	return err;
       
   303 	}
       
   304 /**
       
   305 Private member function which sets a thread-specific breakpoint in the specified thread
       
   306 and returns an opaque handle to the caller.
       
   307 
       
   308 @see DoSetBreak
       
   309 
       
   310 @param aBreakId - Reference to a TUint32 into which the function will return a unique breakpoint Id.
       
   311 @param aThreadId - The thread Id in which to place the breakpoint
       
   312 @param aAddress - Address to place the breakpoint
       
   313 @param aMode - The cpu instruction set architecture type breakpoint (e.g. EArmMode or EThumbMode)
       
   314 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   315 */
       
   316 TInt D_RMD_Breakpoints::priv_DoSetBreak(TInt32 &aBreakId, const TUint64 aId, const TBool aThreadSpecific, const TUint32 aAddress, const TArchitectureMode aMode)
       
   317 	{
       
   318 	LOG_MSG4("D_RMD_Breakpoints::priv_DoSetBreak(aThreadId = 0x%lx, aAddress = 0x%08x, aMode = %d)",aId,aAddress,aMode);
       
   319 
       
   320 	// EThumb2EEMode breakpoints are not supported
       
   321 	if (EThumb2EEMode == aMode)
       
   322 		{
       
   323 		LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - EThumb2EEMode breakpoints are not supported");
       
   324 		return KErrNotSupported;
       
   325 		}
       
   326 
       
   327 	// Check how many breakpoints we have in existence
       
   328 	if ((iBreakPointList.Count()+1) >= NUMBER_OF_MAX_BREAKPOINTS)
       
   329 		{
       
   330 		// Too many breakpoints are set!
       
   331 		LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - Too many breakpoints set");
       
   332 		return KErrOverflow;
       
   333 		}
       
   334 
       
   335 	// check the alignment of the breakpoint
       
   336 	if (!Aligned(aAddress,aMode))
       
   337 		{
       
   338 		LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - Unaligned address");
       
   339 		return KErrArgument;
       
   340 		}
       
   341 
       
   342 	// make sure there is not already a breakpoint at this address
       
   343 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
   344 		{
       
   345 		/* We need to check if the breakpoint overlaps the address at all,
       
   346 		 * and this depends upon the size of the two breakpoints as well as 
       
   347 		 * their address.
       
   348 		 */
       
   349 
       
   350 		// newInstSize = size in bytes of new breakpoint
       
   351 		TInt newInstSize = BreakSize(aMode);
       
   352 		if (newInstSize == 0)
       
   353 			{
       
   354 			LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - Unknown architecture type for new breakpoint");
       
   355 			return KErrNotSupported;
       
   356 			}
       
   357 
       
   358 		// oldInstSize = size in bytes of the existing breakpoint
       
   359 		TInt oldInstSize = BreakSize(iBreakPointList[i].iMode);
       
   360 		if (oldInstSize == 0)
       
   361 			{
       
   362 			LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - : Unknown architecture type of existing breakpoint");
       
   363 			return KErrNotSupported;
       
   364 			}
       
   365 
       
   366 		// Overlap checking - temp is used as the new breakpoint description for checking purposes only
       
   367 		TBreakEntry temp;
       
   368 
       
   369 		temp.iAddress = aAddress;
       
   370 		temp.iMode = aMode;
       
   371 
       
   372 		// do they overlap?
       
   373 		if ( BreakpointsOverlap(temp,iBreakPointList[i]) )
       
   374 			{
       
   375 			// Yes
       
   376 			if(iBreakPointList[i].iThreadSpecific && aThreadSpecific)
       
   377 				{
       
   378 				if(aId == iBreakPointList[i].iId)
       
   379 					{
       
   380 					LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - New thread specific breakpoint overlaps an existing thread specific breakpoint");
       
   381 					return KErrAlreadyExists;
       
   382 					}
       
   383 				}
       
   384 			else if(!iBreakPointList[i].iThreadSpecific && aThreadSpecific)
       
   385 				{
       
   386 				NKern::ThreadEnterCS();
       
   387 				DThread* thread = DebugUtils::OpenThreadHandle(aId);
       
   388 				TInt err = KErrNone;
       
   389 				if (!thread)
       
   390 					{
       
   391 					err = KErrNotFound;
       
   392 					}
       
   393 				if (!err && thread->iOwningProcess->iId == iBreakPointList[i].iId)
       
   394 					{
       
   395 					LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - New thread specific breakpoint overlaps an existing breakpoint");
       
   396 					err = KErrAlreadyExists;
       
   397 					}
       
   398 				thread->Close(NULL);
       
   399 				NKern::ThreadLeaveCS();
       
   400 				if (err) return err;
       
   401 				}
       
   402 			else if(iBreakPointList[i].iThreadSpecific && !aThreadSpecific)
       
   403 				{
       
   404 				NKern::ThreadEnterCS();
       
   405 				DThread* thread = DebugUtils::OpenThreadHandle(iBreakPointList[i].iId);
       
   406 				TInt err = KErrNone;
       
   407 				if (!thread)
       
   408 					{
       
   409 					err = KErrNotFound;
       
   410 					}
       
   411 				if (!err && thread->iOwningProcess->iId == aId)
       
   412 					{
       
   413 					LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - New breakpoint overlaps an existing thread specific breakpoint");
       
   414 					err = KErrAlreadyExists;
       
   415 					}
       
   416 				if (thread) thread->Close(NULL);
       
   417 				NKern::ThreadLeaveCS();
       
   418 				if (err) return err;
       
   419 				}
       
   420 			else // !iBreakPointList[i].iThreadSpecific && !aThreadSpecific
       
   421 				{
       
   422 				if(iBreakPointList[i].iId == aId)
       
   423 					{
       
   424 					LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - New breakpoint overlaps an existing breakpoint");
       
   425 					return KErrAlreadyExists;
       
   426 					}
       
   427 				}
       
   428 			}
       
   429 		}
       
   430 
       
   431 	// increment the break id
       
   432 	aBreakId = iNextBreakId++;	
       
   433 
       
   434 	// create the new breakpoint entry
       
   435 	TBreakEntry breakEntry(aBreakId, aId, aThreadSpecific, aAddress, aMode);
       
   436 
       
   437 	TInt err = priv_DoEnableBreak(breakEntry, ETrue);
       
   438 	if (KErrNone != err)
       
   439 		{
       
   440 		LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - Could not enable the breakpoint");
       
   441 		
       
   442 		return err;
       
   443 		}
       
   444 
       
   445 	err = iBreakPointList.Append(breakEntry);
       
   446 	if (err != KErrNone)
       
   447 		{
       
   448 		LOG_MSG("D_RMD_Breakpoints::priv_DoSetBreak() - Failed to append breakpoint");
       
   449 		}
       
   450 
       
   451 	LOG_MSG2("D_RMD_Breakpoints::priv_DoSetBreak(breakId = 0x%08x) done",aBreakId);
       
   452 
       
   453 	return err;
       
   454 	}
       
   455 
       
   456 /**
       
   457 Public member function which enables a previously set breakpoint.
       
   458 
       
   459 Note 1:
       
   460 This function ensures exclusive access to the breakpoint data structures
       
   461 by using a semaphore to serialise access.
       
   462 
       
   463 @see priv_DoEnableBreak
       
   464 
       
   465 Note 2:
       
   466 As implied by Note 1, the caller must have previously called Init() or this
       
   467 function will return KErrNotReady;
       
   468 
       
   469 Note 3
       
   470 Historically, this function accepted a reference to a TBreakEntry in the class' own
       
   471 iBreakPointList. It now checks whether the reference is to an element of its own list,
       
   472 or one invented by the caller.
       
   473 
       
   474 @param aEntry reference to a TBreakEntry datastructure describing the breakpoint to be re-enabled.
       
   475 @param aSaveOldInstruction ETrue preserves the instruction at the breakpoint address, EFalse otherwise.
       
   476 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   477 */
       
   478 TInt D_RMD_Breakpoints::DoEnableBreak(TBreakEntry &aEntry, TBool aSaveOldInstruction)
       
   479 	{
       
   480 	// Ensure we have a valid semaphore
       
   481 	if (!iInitialised || !iLock)
       
   482 		{
       
   483 		return KErrNotReady;
       
   484 		}
       
   485 
       
   486 	// Acquire the lock
       
   487 	NKern::ThreadEnterCS();
       
   488 	Kern::SemaphoreWait(*iLock);
       
   489 
       
   490 	// Really do the work
       
   491 	TInt err = priv_DoEnableBreak(aEntry,aSaveOldInstruction);
       
   492 	
       
   493 	// Release the lock
       
   494 	Kern::SemaphoreSignal(*iLock);
       
   495 	NKern::ThreadLeaveCS();
       
   496 
       
   497 	return err;
       
   498 	}
       
   499 
       
   500 /**
       
   501 Private member function which enables a previously set breakpoint, as per DoEnableBreak, but
       
   502 does not serialise access.
       
   503 
       
   504 @see DoEnableBreak
       
   505 
       
   506 @param aEntry reference to a TBreakEntry datastructure describing the breakpoint to be re-enabled.
       
   507 @param aSaveOldInstruction ETrue preserves the instruction at the breakpoint address, EFalse otherwise.
       
   508 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   509 */
       
   510 TInt D_RMD_Breakpoints::priv_DoEnableBreak(TBreakEntry &aEntry, TBool aSaveOldInstruction)
       
   511 	{
       
   512 	LOG_MSG("D_RMD_Breakpoints::DoEnableBreak()");
       
   513 
       
   514 	TUint32 inst = BreakInst(aEntry.iMode);	
       
   515 	TInt instSize = BreakSize(aEntry.iMode);
       
   516 	if (instSize == 0 || inst == 0)
       
   517 		{
       
   518 		// not supported
       
   519 		LOG_MSG("D_RMD_Breakpoints::priv_DoEnableBreak - unsupported breakpoint architecture");
       
   520 		return KErrNotSupported;
       
   521 		}
       
   522 
       
   523 	TInt err = KErrNone;
       
   524 
       
   525 	// Get thread id
       
   526 	TUint64 threadId = aEntry.iId + (aEntry.iThreadSpecific ? 0 : 1);
       
   527 	NKern::ThreadEnterCS();
       
   528 	DThread* threadObj = DebugUtils::OpenThreadHandle(threadId);
       
   529 	if (!threadObj)
       
   530 		{
       
   531 		LOG_MSG("D_RMD_Breakpoints::priv_DoEnableBreak - bad handle. Could not identify a threadObj");
       
   532 		NKern::ThreadLeaveCS();
       
   533 		return KErrBadHandle;
       
   534 		}
       
   535 
       
   536 	if (aSaveOldInstruction)
       
   537 		{
       
   538 		TUint32 instruction;
       
   539 
       
   540 		// read the instruction at the address so we can store it in the break entry for when we clear this breakpoint
       
   541 		// trap exceptions in case the address is invalid
       
   542 		XTRAPD(r, XT_DEFAULT, err = iChannel->TryToReadMemory(threadObj, (TAny *)aEntry.iAddress, (TAny *)&instruction, instSize));
       
   543 
       
   544 		//consider the leave as more important than the error code so store the leave if it's not KErrNone
       
   545 		if(KErrNone != r)
       
   546 			{
       
   547 			err = r;
       
   548 			}
       
   549 
       
   550 		if(KErrNone != err)
       
   551 			{
       
   552 			threadObj->Close(NULL);
       
   553 			NKern::ThreadLeaveCS();
       
   554 			LOG_MSG("D_RMD_Breakpoints::priv_DoEnableBreak() - failed to read memory");
       
   555 			return err;
       
   556 			}
       
   557 
       
   558 		aEntry.iInstruction.Copy((TUint8 *)&instruction, instSize);
       
   559 		}
       
   560 
       
   561 	TBool breakpointAlredySet = EFalse;
       
   562 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
   563 		{
       
   564 		if(iBreakPointList[i].iAddress == aEntry.iAddress && !iBreakPointList[i].iDisabledForStep )
       
   565 			{
       
   566 			breakpointAlredySet = ETrue;
       
   567 			break;
       
   568 			}
       
   569 		}
       
   570 	if(!breakpointAlredySet)
       
   571 		{
       
   572 	
       
   573 		LOG_MSG5("D_RMD_Breakpoints::DoEnableBreak() tId=0x%x, addr=0x%x, instSize=%d, inst=0x%x", 
       
   574 			threadObj->iId, aEntry.iAddress, instSize, instSize == 4 ? (TUint32)inst : (TUint16)inst );
       
   575 		XTRAPD(r, XT_DEFAULT, err = DebugSupport::ModifyCode(threadObj, aEntry.iAddress, instSize, inst, DebugSupport::EBreakpointGlobal));
       
   576 		if(r != DebugSupport::EBreakpointGlobal)
       
   577 			{
       
   578 			err = r;
       
   579 			}
       
   580 		}
       
   581 	else
       
   582 		{
       
   583 		LOG_MSG5("D_RMD_Breakpoints::DoEnableBreak() ALREADY SET: tId=0x%x, addr=0x%x, instSize=%d, inst=0x%x", 
       
   584 				threadObj->iId, aEntry.iAddress, instSize, instSize == 4 ? (TUint32)inst : (TUint16)inst );
       
   585 
       
   586 		}
       
   587 
       
   588 	// Close the thread handle which has been opened by OpenThreadHandle
       
   589 	threadObj->Close(NULL);
       
   590 	NKern::ThreadLeaveCS();
       
   591 	return err;
       
   592 	}
       
   593 
       
   594 /**
       
   595 Public member function which clears a previously set breakpoint.
       
   596 
       
   597 Note 1:
       
   598 This function ensures exclusive access to the breakpoint data structures
       
   599 by using a semaphore to serialise access.
       
   600 
       
   601 @see priv_DoClearBreak
       
   602 
       
   603 Note 2:
       
   604 As implied by Note 1, the caller must have previously called Init() or this
       
   605 function will return KErrNotReady;
       
   606 
       
   607 @param aBreakId A breakpoint Id as previously returned by DoSetBreak.
       
   608 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   609 */
       
   610 TInt D_RMD_Breakpoints::DoClearBreak(const TInt32 aBreakId, TBool aIgnoreTerminatedThreads)
       
   611 	{
       
   612 	// Ensure we have a valid semaphore
       
   613 	if (!iInitialised || !iLock)
       
   614 		{
       
   615 		return KErrNotReady;
       
   616 		}
       
   617 
       
   618 	// Acquire the lock
       
   619 	NKern::ThreadEnterCS();
       
   620 	Kern::SemaphoreWait(*iLock);
       
   621 
       
   622 	// Really do the work
       
   623 	TInt err = priv_DoClearBreak(aBreakId, aIgnoreTerminatedThreads);
       
   624 	
       
   625 	// Release the lock
       
   626 	Kern::SemaphoreSignal(*iLock);
       
   627 	NKern::ThreadLeaveCS();
       
   628 
       
   629 	return err;
       
   630 	}
       
   631 
       
   632 /**
       
   633 Private member function which clears a previously set breakpoint, as per DoClearBreak, but
       
   634 does not serialise access.
       
   635 
       
   636 @see DoClearBreak
       
   637 
       
   638 @param aBreakId A breakpoint Id as previously returned by DoSetBreak.
       
   639 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   640 */
       
   641 TInt D_RMD_Breakpoints::priv_DoClearBreak(const TInt32 aBreakId, TBool aIgnoreTerminatedThreads)
       
   642 	{
       
   643 	LOG_MSG3("D_RMD_Breakpoints::priv_DoClearBreak(0x%08x), aIgnoreTerminatedThreads=%d",
       
   644 	        aBreakId, aIgnoreTerminatedThreads);
       
   645 
       
   646 	// find the break entry matching this id.  note that the breakpoints are already sorted in ascending order by id
       
   647 	TBreakEntry entry;
       
   648 	entry.iBreakId = aBreakId;
       
   649 	TInt index = iBreakPointList.FindInSignedKeyOrder(entry);
       
   650 
       
   651 	TInt err = KErrNone;
       
   652 	if (index >= 0)
       
   653 		{	
       
   654  		// if this breakpoint was set in a library and that library has already been unloaded, don't try to clear it
       
   655 		if (!iBreakPointList[index].iObsoleteLibraryBreakpoint)
       
   656 			{
       
   657 			NKern::ThreadEnterCS();
       
   658 			DThread* threadObj = DebugUtils::OpenThreadHandle(iBreakPointList[index].iId + (iBreakPointList[index].iThreadSpecific ? 0 : 1));
       
   659 			if (threadObj)
       
   660 				{
       
   661 				LOG_MSG2("priv_DoClearBreak() OpenThreadHandle ret thread 0x%08x", threadObj->iId );
       
   662 				TBool needToCallCodeModifier = ETrue;
       
   663 				for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
   664 					{
       
   665 					if (i != index)
       
   666 						{
       
   667 						if ( BreakpointsOverlap(iBreakPointList[index],iBreakPointList[i]) )
       
   668 							{
       
   669 							needToCallCodeModifier = EFalse;
       
   670 							break;
       
   671 							}
       
   672 						}
       
   673 					}
       
   674 				if(needToCallCodeModifier)
       
   675 					{
       
   676 					XTRAPD(r, XT_DEFAULT, err = DebugSupport::RestoreCode(threadObj, iBreakPointList[index].iAddress));
       
   677 					if (r != KErrNone)
       
   678 						{
       
   679 						LOG_MSG2("D_RMD_Breakpoints::priv_DoClearBreak() - restore code trap harness returned error %d",r);
       
   680 						}
       
   681 					if (err == KErrNotFound)
       
   682 						{
       
   683 						LOG_MSG("restore code reported the breakpoint not found, continuing");
       
   684 						err = KErrNone;
       
   685 						}
       
   686 					else if (err != KErrNone)
       
   687 						{
       
   688 						LOG_MSG2("D_RMD_Breakpoints::priv_DoClearBreak() - restore code returned error %d",err);
       
   689 						}
       
   690 					err = (KErrNone == r) ? err : r;
       
   691 					}
       
   692 
       
   693 				// Close the thread handle opened by OpenThreadHandle
       
   694 				threadObj->Close(NULL);
       
   695 				}
       
   696 			else
       
   697 				{
       
   698 				LOG_MSG("D_RMD_Breakpoints::OpenThreadHandle ret null thread");
       
   699 				err = KErrBadHandle;
       
   700 				}
       
   701 			NKern::ThreadLeaveCS();
       
   702 			}
       
   703 		
       
   704 		LOG_MSG4("D_RMD_Breakpoints::priv_DoClearBreak() - Clearing breakpoint at address: %x, err: %d, ignore terminated: %d", iBreakPointList[index].iAddress, err, aIgnoreTerminatedThreads?1:0);
       
   705 		if ((aIgnoreTerminatedThreads && KErrBadHandle == err) || KErrNone == err)
       
   706 			{
       
   707 			// if this is a temp breakpoint, just clear out the values, otherwise remove it from the list
       
   708 			err = KErrNone;
       
   709 			if (index < NUMBER_OF_TEMP_BREAKPOINTS)
       
   710 				{
       
   711                 // if this is a temp breakpoint, just clear out the values, otherwise remove it from the list
       
   712                 LOG_MSG2("D_RMD_Breakpoints::priv_DoClearBreak() - Reseting temp breakpoint[%d]",index);
       
   713 				iBreakPointList[index].Reset();
       
   714 				}
       
   715 			else
       
   716 				{
       
   717 				LOG_MSG2("D_RMD_Breakpoints::priv_DoClearBreak() - Removing breakpoint[%d]",index);
       
   718 				iBreakPointList.Remove(index);
       
   719 				}			
       
   720 			}
       
   721 		else
       
   722 		    {
       
   723             LOG_MSG3("D_RMD_Breakpoints::priv_DoClearBreak() - *** Not removing breakpoint[%d] due to error=%d",index, err);
       
   724 		    }
       
   725 				
       
   726 		return err;
       
   727 		}
       
   728 
       
   729 	LOG_MSG2("D_RMD_Breakpoints::priv_DoClearBreak() - Break Id %d not found", aBreakId);
       
   730 
       
   731 	return KErrNotFound;
       
   732 	}
       
   733 
       
   734 /**
       
   735 Public member function which modifies a previously set breakpoint.
       
   736 
       
   737 Note 1:
       
   738 This function ensures exclusive access to the breakpoint data structures
       
   739 by using a semaphore to serialise access.
       
   740 
       
   741 @see priv_DoModifyBreak
       
   742 
       
   743 Note 2:
       
   744 As implied by Note 1, the caller must have previously called Init() or this
       
   745 function will return KErrNotReady;
       
   746 
       
   747 @param aBreakInfo A TModifyBreakInfo describing the breakpoint properties that are wanted.
       
   748 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   749 */
       
   750 TInt D_RMD_Breakpoints::DoModifyBreak(TModifyBreakInfo* aBreakInfo)
       
   751 	{
       
   752 	// Ensure we have a valid semaphore
       
   753 	if (!iInitialised || !iLock)
       
   754 		{
       
   755 		return KErrNotReady;
       
   756 		}
       
   757 
       
   758 	// Acquire the lock
       
   759 	NKern::ThreadEnterCS();
       
   760 	Kern::SemaphoreWait(*iLock); 
       
   761 
       
   762 	// Really do the work
       
   763 	TInt err = priv_DoModifyBreak(aBreakInfo);
       
   764 	
       
   765 	// Release the lock
       
   766 	Kern::SemaphoreSignal(*iLock);
       
   767 	NKern::ThreadLeaveCS();
       
   768 
       
   769 	return err;
       
   770 	}
       
   771 
       
   772 /**
       
   773 Private member function which modifies a previously set breakpoint, as per DoModifyBreak, but
       
   774 does not serialise access.
       
   775 
       
   776 @see DoModifyBreak
       
   777 
       
   778 @param aBreakInfo A TModifyBreakInfo describing the breakpoint properties that are wanted.
       
   779 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
   780 */
       
   781 TInt D_RMD_Breakpoints::priv_DoModifyBreak(TModifyBreakInfo* aBreakInfo)
       
   782 	{
       
   783 	LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak()");
       
   784 
       
   785 	// Check arguments
       
   786 	if (!aBreakInfo)
       
   787 		{
       
   788 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak() was passed a NULL argument");
       
   789 		return KErrArgument;
       
   790 		}
       
   791 
       
   792 	//User side memory is not accessible directly
       
   793 	TSetBreakInfo info;
       
   794 	TInt err = Kern::ThreadRawRead(iChannel->iClientThread, aBreakInfo, (TUint8*)&info, sizeof(TSetBreakInfo));
       
   795 	if (err != KErrNone)
       
   796 		{
       
   797 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak() was passed a bad argument");
       
   798 		return err;
       
   799 		}
       
   800 
       
   801 	// EThumb2EEMode breakpoints are not supported
       
   802 	if (EThumb2EEMode == info.iMode)
       
   803 		{
       
   804 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak() - EThumb2EEMode breakpoints are not supported");
       
   805 		return KErrNotSupported;
       
   806 		}
       
   807 
       
   808 	// find the break entry matching this id.  note that the breakpoints are already sorted in ascending order by id
       
   809 	TBreakEntry entry;
       
   810 	entry.iBreakId = (TUint32)info.iBreakId;
       
   811 	TInt index = iBreakPointList.FindInSignedKeyOrder(entry);
       
   812 	if (index < 0)
       
   813 		{
       
   814 		// Could not find the breakpoint
       
   815 		LOG_MSG2("D_RMD_Breakpoints::priv_DoModifyBreak() - Could not find the breakpoint id 0x%08x",(TUint32)info.iBreakId);
       
   816 		return KErrNotFound;
       
   817 		}
       
   818 
       
   819 	// first check its not obsolete
       
   820 	if (!iBreakPointList[index].iObsoleteLibraryBreakpoint)
       
   821 		{
       
   822 		// its still a valid breakpoint
       
   823 
       
   824 		// remove the old breakpoint
       
   825 		NKern::ThreadEnterCS();
       
   826 		DThread* threadObj = DebugUtils::OpenThreadHandle(iBreakPointList[index].iId);
       
   827 		if (threadObj)
       
   828 			{
       
   829 			LOG_MSG2("D_RMD_Breakpoints::priv_DoModifyBreak - Unsetting breakpoint at address 0x%08x",iBreakPointList[index].iAddress);
       
   830 
       
   831 			XTRAPD(r, XT_DEFAULT, err = DebugSupport::RestoreCode(threadObj, iBreakPointList[index].iAddress));
       
   832 			if (r != 0)
       
   833 				{
       
   834 				LOG_MSG("Failed to construct trap handler for DebugSupport::RestoreCode");
       
   835 				}
       
   836 
       
   837 			// Close the thread handle which has been opened by OpenThreadHandle
       
   838 			threadObj->Close(NULL);
       
   839 			NKern::ThreadLeaveCS();
       
   840 			}
       
   841 		else
       
   842 			{
       
   843 			// Bad handle
       
   844 			LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak - Could not identify the breakpoint thread id");
       
   845 			NKern::ThreadLeaveCS();
       
   846 			return KErrBadHandle;
       
   847 			}
       
   848 		}
       
   849 
       
   850 	// make sure there is not already a breakpoint at the new address
       
   851 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
   852 		{
       
   853 		// Ignore data for the breakpoint entry being modified.
       
   854 		if (i != index)
       
   855 			{
       
   856 			/* We need to check if the breakpoint overlaps the address at all,
       
   857 			 * and this depends upon the size of the two breakpoints as well as 
       
   858 			 * their address.
       
   859 			 */
       
   860 
       
   861 			// newInstSize = size in bytes of new breakpoint
       
   862 			TInt newInstSize = BreakSize(info.iMode);
       
   863 			if (newInstSize == 0)
       
   864 			{
       
   865 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak - Unknown architecture type for new breakpoint");
       
   866 				return KErrNotSupported;
       
   867 			}
       
   868 
       
   869 			// oldInstSize = size in bytes of the existing breakpoint
       
   870 			TInt oldInstSize = BreakSize(iBreakPointList[i].iMode);
       
   871 			if (oldInstSize == 0)
       
   872 			{
       
   873 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak - Unknown architecture type of existing breakpoint");
       
   874 				return KErrNotSupported;
       
   875 			}
       
   876 
       
   877 			// Overlap checking - temp is used as the new breakpoint description for checking purposes only
       
   878 			TBreakEntry temp;
       
   879 
       
   880 			temp.iAddress = info.iAddress;
       
   881 			temp.iMode = info.iMode;
       
   882 
       
   883 			// do they overlap?
       
   884 			if ( BreakpointsOverlap(temp,iBreakPointList[i]) )
       
   885 				{
       
   886 				// Yes
       
   887 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak() - New breakpoint overlaps an existing breakpoint");
       
   888 				return KErrAlreadyExists;
       
   889 				}
       
   890 			}
       
   891 		}
       
   892 
       
   893 	// Prepare iBreakPointList[index] with the new information, then set the breakpoint
       
   894 	iBreakPointList[index].iId = info.iId;
       
   895 	iBreakPointList[index].iAddress = info.iAddress;
       
   896 	iBreakPointList[index].iMode = info.iMode;
       
   897 
       
   898 	TBreakEntry& newBreakEntry = iBreakPointList[index];
       
   899 
       
   900 	// Decide the size of the breakpoint instruction
       
   901 	TUint32 inst = BreakInst(newBreakEntry.iMode);
       
   902 	TInt instSize = BreakSize(newBreakEntry.iMode);
       
   903 
       
   904 	if (inst == 0 || instSize == 0)
       
   905 		{
       
   906 		// Unsupported architecture
       
   907 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak - unsupported breakpoint architecture");
       
   908 		return KErrNotSupported;
       
   909 		}
       
   910 
       
   911 
       
   912 	//if thread id is 0xFFFFFFFF, then the breakpoint is not thread specific
       
   913 	if (newBreakEntry.iId != 0xFFFFFFFF)
       
   914 		{
       
   915 		newBreakEntry.iThreadSpecific = ETrue;
       
   916 		}
       
   917 
       
   918 	// Get thread id from the process that we are debugging
       
   919 	TProcessInfo * proc = NULL;
       
   920 	TUint64 threadId = NULL;
       
   921 
       
   922 	threadId = newBreakEntry.iId;
       
   923 
       
   924 	NKern::ThreadEnterCS();
       
   925 	DThread* threadObj = DebugUtils::OpenThreadHandle(threadId);
       
   926 	//if we don't have the right thread id for the address, 
       
   927 	//then try with the thread id of the process that we are debugging 	
       
   928 	if (!threadObj && iChannel->iDebugProcessList.Count())
       
   929 		{
       
   930 		proc = &iChannel->iDebugProcessList[0];
       
   931 		if (proc)
       
   932 			{
       
   933 			threadId = proc->iId+1;	
       
   934 			}
       
   935 		threadObj = DebugUtils::OpenThreadHandle(threadId);
       
   936 		}
       
   937 
       
   938 	if(!threadObj)
       
   939 		{
       
   940 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyBreak() - bad handle. Could not identify a threadObj");
       
   941 		NKern::ThreadLeaveCS();
       
   942 		return KErrBadHandle;
       
   943 		}
       
   944 
       
   945 	// save the old instruction
       
   946 	TUint32 instruction;
       
   947 
       
   948 	// read the instruction at the address so we can store it in the break entry for when we clear this breakpoint
       
   949 	// trap exceptions in case the address is invalid
       
   950 	XTRAPD(r, XT_DEFAULT, err = iChannel->TryToReadMemory(threadObj, (TAny *)newBreakEntry.iAddress, (TAny *)&instruction, instSize));
       
   951 
       
   952 	//consider the leave as more important than the error code so store the leave if it's not KErrNone
       
   953 	if(KErrNone != r)
       
   954 		{
       
   955 		err = r;
       
   956 		}
       
   957 	if(KErrNone != err)
       
   958 		{
       
   959 		threadObj->Close(NULL);
       
   960 		NKern::ThreadLeaveCS();
       
   961 		return err;
       
   962 		}
       
   963 
       
   964 	newBreakEntry.iInstruction.Copy((TUint8 *)&instruction, instSize);
       
   965 
       
   966 	newBreakEntry.iId = threadId; //set the thread ID here 
       
   967 	LOG_MSG3("ModifyCode2 instSize:%d, inst: 0x%08x", instSize, inst);
       
   968 	XTRAPD(s, XT_DEFAULT, err = DebugSupport::ModifyCode(threadObj, newBreakEntry.iAddress, instSize, inst, DebugSupport::EBreakpointGlobal));
       
   969 	if(s != DebugSupport::EBreakpointGlobal)
       
   970 		{
       
   971 		err = s;
       
   972 		}
       
   973 
       
   974 	// Close the thread handle which has been opened by OpenThreadHandle
       
   975 	threadObj->Close(NULL);
       
   976 	NKern::ThreadLeaveCS();
       
   977 	return err;
       
   978 	}	
       
   979 
       
   980 //
       
   981 // D_RMD_Breakpoints::DoModifyProcessBreak
       
   982 //
       
   983 TInt D_RMD_Breakpoints::DoModifyProcessBreak(TModifyProcessBreakInfo* aBreakInfo)
       
   984 	{
       
   985 	// Ensure we have a valid semaphore
       
   986 	if (!iInitialised || !iLock)
       
   987 		{
       
   988 		return KErrNotReady;
       
   989 		}
       
   990 
       
   991 	// Acquire the lock
       
   992 	NKern::ThreadEnterCS();
       
   993 	Kern::SemaphoreWait(*iLock); 
       
   994 
       
   995 	// Really do the work
       
   996 	TInt err = priv_DoModifyProcessBreak(aBreakInfo);
       
   997 	
       
   998 	// Release the lock
       
   999 	Kern::SemaphoreSignal(*iLock);
       
  1000 	NKern::ThreadLeaveCS();
       
  1001 
       
  1002 	return err;
       
  1003 	}
       
  1004 	
       
  1005 TInt D_RMD_Breakpoints::priv_DoModifyProcessBreak(TModifyProcessBreakInfo* aBreakInfo)
       
  1006 	{	
       
  1007 	LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak()");
       
  1008 
       
  1009 	// Check arguments
       
  1010 	if (!aBreakInfo)
       
  1011 		{
       
  1012 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() was passed a NULL argument");
       
  1013 		return KErrArgument;
       
  1014 		}
       
  1015 
       
  1016 	//User side memory is not accessible directly
       
  1017 	TSetBreakInfo info;
       
  1018 	TInt err = Kern::ThreadRawRead(iChannel->iClientThread, aBreakInfo, (TUint8*)&info, sizeof(TModifyProcessBreakInfo));
       
  1019 	if (err != KErrNone)
       
  1020 		{
       
  1021 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() was passed a bad argument");
       
  1022 		return err;
       
  1023 		}
       
  1024 
       
  1025 	// EThumb2EEMode breakpoints are not supported
       
  1026 	if (EThumb2EEMode == info.iMode)
       
  1027 		{
       
  1028 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - EThumb2EEMode breakpoints are not supported");
       
  1029 		return KErrNotSupported;
       
  1030 		}
       
  1031 
       
  1032 	// find the break entry matching this id.  note that the breakpoints are already sorted in ascending order by id
       
  1033 	TBreakEntry entry;
       
  1034 	entry.iBreakId = (TUint32)info.iBreakId;
       
  1035 	TInt index = iBreakPointList.FindInSignedKeyOrder(entry);
       
  1036 	if (index < 0)
       
  1037 		{
       
  1038 		// Could not find the breakpoint
       
  1039 		LOG_MSG2("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - Could not find the breakpoint id 0x%08x",(TUint32)info.iBreakId);
       
  1040 		return KErrNotFound;
       
  1041 		}
       
  1042 
       
  1043 	// first check its not obsolete
       
  1044 	if (!iBreakPointList[index].iObsoleteLibraryBreakpoint)
       
  1045 		{
       
  1046 		// its still a valid breakpoint
       
  1047 
       
  1048 		// remove the old breakpoint
       
  1049 		NKern::ThreadEnterCS();
       
  1050 		DProcess *process = DebugUtils::OpenProcessHandle(iBreakPointList[index].iId);
       
  1051 		DThread* threadObj = NULL;
       
  1052 		if(process)
       
  1053 			{
       
  1054 			threadObj = DebugUtils::OpenFirstThreadForProcess(process);
       
  1055 			process->Close(NULL);
       
  1056 			}
       
  1057 
       
  1058 		if (threadObj)
       
  1059 			{
       
  1060 			LOG_MSG2("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - Unsetting breakpoint at address 0x%08x",iBreakPointList[index].iAddress);
       
  1061 
       
  1062 			XTRAPD(r, XT_DEFAULT, /*err =*/ DebugSupport::RestoreCode(threadObj, iBreakPointList[index].iAddress)); // Any error here is not important. The semantics of ModifyBreakpoint are such that if it fails the previous breakpoint location is removed, which means that a further call to ModifyBreakpoint shouldn't fail because the CodeModifier doesn't know about it.
       
  1063 			if (r != 0)
       
  1064 				{
       
  1065 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - Failed to construct trap handler for DebugSupport::RestoreCode");
       
  1066 				}
       
  1067 
       
  1068 			// Close the thread handle which has been opened by OpenThreadHandle
       
  1069 			threadObj->Close(NULL);
       
  1070 			}
       
  1071 		else
       
  1072 			{
       
  1073 			// Bad handle
       
  1074 			LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - Could not identify the breakpoint process id");
       
  1075 			err = KErrBadHandle;
       
  1076 			}
       
  1077 		NKern::ThreadLeaveCS();
       
  1078 		if (err) return err;
       
  1079 		}
       
  1080 
       
  1081 	// make sure there is not already a breakpoint at the new address
       
  1082 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
  1083 		{
       
  1084 		// Ignore data for the breakpoint entry being modified.
       
  1085 		if (i != index)
       
  1086 			{
       
  1087 			/* We need to check if the breakpoint overlaps the address at all,
       
  1088 			 * and this depends upon the size of the two breakpoints as well as 
       
  1089 			 * their address.
       
  1090 			 */
       
  1091 
       
  1092 			// newInstSize = size in bytes of new breakpoint
       
  1093 			TInt newInstSize = BreakSize(info.iMode);
       
  1094 			if (newInstSize == 0)
       
  1095 				{
       
  1096 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - Unknown architecture type for new breakpoint");
       
  1097 				return KErrNotSupported;
       
  1098 				}
       
  1099 
       
  1100 			// oldInstSize = size in bytes of the existing breakpoint
       
  1101 			TInt oldInstSize = BreakSize(iBreakPointList[i].iMode);
       
  1102 			if (oldInstSize == 0)
       
  1103 				{
       
  1104 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - : Unknown architecture type of existing breakpoint");
       
  1105 				return KErrNotSupported;
       
  1106 				}
       
  1107 
       
  1108 			// Overlap checking - temp is used as the new breakpoint description for checking purposes only
       
  1109 			TBreakEntry temp;
       
  1110 
       
  1111 			temp.iAddress = info.iAddress;
       
  1112 			temp.iMode = info.iMode;
       
  1113 
       
  1114 			// do they overlap?
       
  1115 			if ( BreakpointsOverlap(temp,iBreakPointList[i]) )
       
  1116 				{
       
  1117 				// Yes
       
  1118 				LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - New breakpoint overlaps an existing breakpoint");
       
  1119 				return KErrAlreadyExists;
       
  1120 				}
       
  1121 			}
       
  1122 		}
       
  1123 
       
  1124 	// Prepare iBreakPointList[index] with the new information, then set the breakpoint
       
  1125 	iBreakPointList[index].iId = info.iId;
       
  1126 	iBreakPointList[index].iAddress = info.iAddress;
       
  1127 	iBreakPointList[index].iMode = info.iMode;
       
  1128 
       
  1129 	TBreakEntry& newBreakEntry = iBreakPointList[index];
       
  1130 
       
  1131 	// Decide the size of the breakpoint instruction
       
  1132 	TUint32 inst = BreakInst(newBreakEntry.iMode);
       
  1133 	TInt instSize = BreakSize(newBreakEntry.iMode);
       
  1134 
       
  1135 	if (inst == 0 || instSize == 0)
       
  1136 		{
       
  1137 		// Unsupported architecture
       
  1138 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - unsupported breakpoint architecture");
       
  1139 		return KErrNotSupported;
       
  1140 		}
       
  1141 
       
  1142 	newBreakEntry.iThreadSpecific = EFalse;
       
  1143 
       
  1144 	DThread* threadObj = NULL;
       
  1145 	NKern::ThreadEnterCS();
       
  1146 	DProcess* process = DebugUtils::OpenProcessHandle(newBreakEntry.iId);
       
  1147 	if (process)
       
  1148 		{
       
  1149 		threadObj = DebugUtils::OpenFirstThreadForProcess(process);
       
  1150 		if (!threadObj) err = KErrNotFound;
       
  1151 		process->Close(NULL);
       
  1152 		}
       
  1153 	else
       
  1154 		{
       
  1155 		LOG_MSG("D_RMD_Breakpoints::priv_DoModifyProcessBreak() - bad handle. Could not identify a process");
       
  1156 		err = KErrBadHandle;
       
  1157 		}
       
  1158 
       
  1159 	if (err)
       
  1160 		{
       
  1161 		NKern::ThreadLeaveCS();
       
  1162 		return err;
       
  1163 		}
       
  1164 
       
  1165 	// save the old instruction
       
  1166 	TUint32 instruction;
       
  1167 
       
  1168 	// read the instruction at the address so we can store it in the break entry for when we clear this breakpoint
       
  1169 	// trap exceptions in case the address is invalid
       
  1170 	XTRAPD(r, XT_DEFAULT, err = iChannel->TryToReadMemory(threadObj, (TAny *)newBreakEntry.iAddress, (TAny *)&instruction, instSize));
       
  1171 
       
  1172 	//consider the leave as more important than the error code so store the leave if it's not KErrNone
       
  1173 	if(KErrNone != r)
       
  1174 		{
       
  1175 		err = r;
       
  1176 		}
       
  1177 	if(KErrNone != err)
       
  1178 		{
       
  1179 		threadObj->Close(NULL);
       
  1180 		NKern::ThreadLeaveCS();
       
  1181 		return err;
       
  1182 		}
       
  1183 
       
  1184 	newBreakEntry.iInstruction.Copy((TUint8 *)&instruction, instSize);
       
  1185 
       
  1186 	XTRAPD(s, XT_DEFAULT, err = DebugSupport::ModifyCode(threadObj, newBreakEntry.iAddress, instSize, inst, DebugSupport::EBreakpointGlobal));
       
  1187 	if(s != DebugSupport::EBreakpointGlobal)
       
  1188 		{
       
  1189 		err = s;
       
  1190 		}
       
  1191 
       
  1192 	// Close the thread handle which has been opened by OpenThreadHandle
       
  1193 	threadObj->Close(NULL);
       
  1194 	NKern::ThreadLeaveCS();
       
  1195 	return err;
       
  1196 	}
       
  1197 
       
  1198 /**
       
  1199 Public member function which returns information about a previously set breakpoint.
       
  1200 
       
  1201 Note 1:
       
  1202 This function ensures exclusive access to the breakpoint data structures
       
  1203 by using a semaphore to serialise access.
       
  1204 
       
  1205 @see priv_DoBreakInfo
       
  1206 
       
  1207 Note 2:
       
  1208 As implied by Note 1, the caller must have previously called Init() or this
       
  1209 function will return KErrNotReady;
       
  1210 
       
  1211 @param aBreakInfo Address of aBreakInfo structure in user-side memory within the DSS client thread. CAN ONLY BE ACCESSED VIA Kern::ThreadRawRead()
       
  1212 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
  1213 */
       
  1214 TInt D_RMD_Breakpoints::DoBreakInfo(TGetBreakInfo* aBreakInfo)
       
  1215 	{
       
  1216 	// Ensure we have a valid semaphore
       
  1217 	if (!iInitialised || !iLock)
       
  1218 		{
       
  1219 		return KErrNotReady;
       
  1220 		}
       
  1221 
       
  1222 	// Acquire the lock
       
  1223 	NKern::ThreadEnterCS();
       
  1224 	Kern::SemaphoreWait(*iLock);
       
  1225 
       
  1226 	// Really do the work
       
  1227 	TInt err = priv_DoBreakInfo(aBreakInfo);
       
  1228 	
       
  1229 	// Release the lock
       
  1230 	Kern::SemaphoreSignal(*iLock);
       
  1231 	NKern::ThreadLeaveCS();
       
  1232 
       
  1233 	return err;
       
  1234 	}
       
  1235 
       
  1236 /**
       
  1237 Private member function function which returns information about a previously set breakpoint..
       
  1238 
       
  1239 @see DoBreakInfo
       
  1240 
       
  1241 @param aBreakInfo Address of aBreakInfo structure in user-side memory within the DSS client thread. CAN ONLY BE ACCESSED VIA Kern::ThreadRawRead()
       
  1242 @return KErrNone if successful, otherwise one of the other system wide error codes.
       
  1243 */
       
  1244 TInt D_RMD_Breakpoints::priv_DoBreakInfo(TGetBreakInfo* aBreakInfo)
       
  1245 	{
       
  1246 	LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo()");
       
  1247 
       
  1248 	if (!aBreakInfo)
       
  1249 		{
       
  1250 		LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() was passed a NULL argument");
       
  1251 
       
  1252 		return KErrArgument;
       
  1253 		}
       
  1254 
       
  1255 	//User side memory is not accessible directly
       
  1256 	TGetBreakInfo info;
       
  1257 	TInt err = Kern::ThreadRawRead(iChannel->iClientThread, aBreakInfo, (TUint8*)&info, sizeof(TGetBreakInfo));
       
  1258 	if (err != KErrNone)
       
  1259 		{
       
  1260 		LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() was passed a bad argument");
       
  1261 
       
  1262 		return err;
       
  1263 		}
       
  1264 
       
  1265 	// find the break entry matching this id.  note that the breakpoints are already sorted in ascending order by id
       
  1266 	TBreakEntry entry;
       
  1267 	entry.iBreakId = (TUint32)info.iBreakId;
       
  1268 	TInt index = iBreakPointList.FindInSignedKeyOrder(entry);
       
  1269 	
       
  1270 	if (index >=0)
       
  1271 		{
       
  1272 		// get the thread id for this breakpoint
       
  1273 		TUint64 threadId = iBreakPointList[index].iId;
       
  1274 
       
  1275 		err = Kern::ThreadRawWrite(iChannel->iClientThread,(TUint8*)info.iId,&threadId,sizeof(TUint64));
       
  1276 		if (err != KErrNone)
       
  1277 			{
       
  1278 			LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() - failed to return breakpoint iThreadId information");
       
  1279 			return err;
       
  1280 			}
       
  1281 
       
  1282 		// get the threadSpecific-ness
       
  1283 		TBool threadSpecific = iBreakPointList[index].iThreadSpecific;
       
  1284 
       
  1285 		err = Kern::ThreadRawWrite(iChannel->iClientThread,(TUint8*)info.iThreadSpecific,&threadSpecific,sizeof(TBool));
       
  1286 		if (err != KErrNone)
       
  1287 			{
       
  1288 			LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() - failed to return thread specific information");
       
  1289 			return err;
       
  1290 			}
       
  1291 
       
  1292 
       
  1293 		// get the address
       
  1294 		TUint32 address = iBreakPointList[index].iAddress;
       
  1295 
       
  1296 		err = Kern::ThreadRawWrite(iChannel->iClientThread,(TUint8*)info.iAddress,&address,sizeof(TUint32));
       
  1297 		if (err != KErrNone)
       
  1298 			{
       
  1299 			LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() - failed to return breakpoint iAddress information");
       
  1300 			return err;
       
  1301 			}
       
  1302 
       
  1303 
       
  1304 		// get the architecture
       
  1305 		TArchitectureMode mode = iBreakPointList[index].iMode;
       
  1306 
       
  1307 		err = Kern::ThreadRawWrite(iChannel->iClientThread,(TUint8*)info.iMode,&mode,sizeof(TUint32));
       
  1308 		if (err != KErrNone)
       
  1309 			{
       
  1310 			LOG_MSG("D_RMD_Breakpoints::priv_DoBreakInfo() - failed to return breakpoint iMode information");
       
  1311 			return err;
       
  1312 			}
       
  1313 
       
  1314 		return err;
       
  1315 		}
       
  1316 
       
  1317 	LOG_MSG2("D_RMD_Breakpoints::priv_DoBreakInfo - Could not find the breakpoint id specified 0x%08x", entry.iBreakId);
       
  1318 	return KErrNotFound;
       
  1319 	}
       
  1320 
       
  1321 /**
       
  1322 Public member function which clears all the breakpoints in the system. Generally used for shutting down
       
  1323 the debug device driver.
       
  1324 
       
  1325 Note 1:
       
  1326 This function ensures exclusive access to the breakpoint data structures
       
  1327 by using a semaphore to serialise access.
       
  1328 
       
  1329 @see priv_ClearAllBreakPoints
       
  1330 
       
  1331 Note 2:
       
  1332 As implied by Note 1, the caller must have previously called Init() or this
       
  1333 function will return KErrNotReady;
       
  1334 */
       
  1335 void D_RMD_Breakpoints::ClearAllBreakPoints()
       
  1336 	{
       
  1337 	// Ensure we have a valid semaphore
       
  1338 	if (!iInitialised || !iLock)
       
  1339 		{
       
  1340 		return;
       
  1341 		}
       
  1342 
       
  1343 	// Acquire the lock
       
  1344 	NKern::ThreadEnterCS();
       
  1345 	Kern::SemaphoreWait(*iLock);
       
  1346 
       
  1347 	// Really do the work
       
  1348 	priv_ClearAllBreakPoints();
       
  1349 	
       
  1350 	// Release the lock
       
  1351 	Kern::SemaphoreSignal(*iLock);
       
  1352 	NKern::ThreadLeaveCS();
       
  1353 	}
       
  1354 
       
  1355 /**
       
  1356 Private member function which clears all the breakpoints in the system. Generally used for shutting down
       
  1357 the debug device driver. 
       
  1358 
       
  1359 @see DoClearAllBreakPoints
       
  1360 */
       
  1361 void D_RMD_Breakpoints::priv_ClearAllBreakPoints()
       
  1362 	{
       
  1363 	LOG_MSG("D_RMD_Breakpoints::priv_ClearAllBreakPoints()");
       
  1364 
       
  1365 	TInt err = KErrNone;
       
  1366 	NKern::ThreadEnterCS();
       
  1367 	for (TInt i=0; i<iBreakPointList.Count(); i++)
       
  1368 		{
       
  1369 		if ((iBreakPointList[i].iAddress != 0) && !iBreakPointList[i].iObsoleteLibraryBreakpoint)
       
  1370 			{
       
  1371 			TUint32 id = iBreakPointList[i].iId + (iBreakPointList[i].iThreadSpecific ? 0 : 1);
       
  1372 	        LOG_MSG5(" Will try to clear breakpoint[%d] at address 0x%x, iId=0x%016lx, thrdSpec=%d", 
       
  1373 	                i, iBreakPointList[i].iAddress, iBreakPointList[i].iId, iBreakPointList[i].iThreadSpecific);
       
  1374 
       
  1375 			DThread *threadObj = DebugUtils::OpenThreadHandle(id);
       
  1376 			if (threadObj)
       
  1377 				{
       
  1378 				XTRAPD(r, XT_DEFAULT, err = DebugSupport::RestoreCode(threadObj, iBreakPointList[i].iAddress));
       
  1379 				err = (KErrNone == r) ? err : r;
       
  1380 				threadObj->Close(NULL);
       
  1381 				}
       
  1382 			else
       
  1383 				{
       
  1384                 LOG_MSG(" OpenThreadHandle returned NULL handle");
       
  1385 				err = KErrBadHandle;
       
  1386 				}
       
  1387 
       
  1388 			if (KErrNone != err)
       
  1389 				{
       
  1390 				LOG_MSG2("D_RMD_Breakpoints::priv_ClearAllBreakPoints() - Error 0x%08x while clearing breakpoint", err);
       
  1391 				}
       
  1392 			}
       
  1393 		else if(iBreakPointList[i].iAddress == 0)
       
  1394 		    {
       
  1395             LOG_MSG3("Breakpoint[%d]: address is 0, iId=0x%016lx", i, iBreakPointList[i].iId );		
       
  1396 		    }
       
  1397 		else
       
  1398 		    {
       
  1399             LOG_MSG4("Breakpoint[%d]: Obsoleted, address =0x%x, iId=0x%016lx", i, iBreakPointList[i].iAddress, iBreakPointList[i].iId );     
       
  1400 		    }
       
  1401 		}
       
  1402 	NKern::ThreadLeaveCS();
       
  1403 
       
  1404 	iBreakPointList.Reset();
       
  1405 	}
       
  1406 
       
  1407 /**
       
  1408 Public member function which disables the breakpoint at the specified address.
       
  1409 
       
  1410 Note 1:
       
  1411 This function ensures exclusive access to the breakpoint data structures
       
  1412 by using a semaphore to serialise access.
       
  1413 
       
  1414 @see priv_DisableBreakAtAddress
       
  1415 
       
  1416 Note 2:
       
  1417 As implied by Note 1, the caller must have previously called Init() or this
       
  1418 function will return KErrNotReady;
       
  1419 
       
  1420 @param aAddress Address at which to disable breakpoints (all threads)
       
  1421 @return KErrNone if successful, one of the other system wide error codes otherwise.
       
  1422 */
       
  1423 TInt D_RMD_Breakpoints::DisableBreakAtAddress(TUint32 aAddress)
       
  1424 	{
       
  1425 	// Ensure we have a valid semaphore
       
  1426 	if (!iInitialised || !iLock)
       
  1427 		{
       
  1428 		return KErrNotReady;
       
  1429 		}
       
  1430 
       
  1431 	// Acquire the lock
       
  1432 	NKern::ThreadEnterCS();
       
  1433 	Kern::SemaphoreWait(*iLock);
       
  1434 
       
  1435 	// Really do the work
       
  1436 	TInt err = priv_DisableBreakAtAddress(aAddress);
       
  1437 	
       
  1438 	// Release the lock
       
  1439 	Kern::SemaphoreSignal(*iLock);
       
  1440 	NKern::ThreadLeaveCS();
       
  1441 
       
  1442 	return err;
       
  1443 	}
       
  1444 
       
  1445 /**
       
  1446 Private member function which clears all the breakpoints in the system. Generally used for shutting down
       
  1447 the debug device driver. 
       
  1448 
       
  1449 @see DisableBreakAtAddress
       
  1450 
       
  1451 @param aAddress clears the breakpoint at the specified address
       
  1452 @return KErrNone if successful, one of the other system wide error codes otherwise.
       
  1453 */
       
  1454 TInt D_RMD_Breakpoints::priv_DisableBreakAtAddress(TUint32 aAddress)
       
  1455 	{
       
  1456 	LOG_MSG2("D_RMD_Breakpoints::priv_DisableBreakAtAddress(aAddress=0x%x)", aAddress);
       
  1457 
       
  1458 	TInt err = KErrNone;
       
  1459 
       
  1460 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
  1461 		{
       
  1462 		if (iBreakPointList[i].iAddress == aAddress)
       
  1463 			{
       
  1464 			iBreakPointList[i].iDisabledForStep = ETrue;
       
  1465 			LOG_MSG2("D_RMD_Breakpoints::priv_DisableBreakAtAddress - Disabling breakpoint at address 0x%x", iBreakPointList[i].iAddress);
       
  1466 
       
  1467 			//clear the breakpoint with code modifier
       
  1468 			//code modifier will restore the org instruction and also frees the shadow page if necessary
       
  1469 			TUint64 id = iBreakPointList[i].iId + (iBreakPointList[i].iThreadSpecific ? 0 : 1);
       
  1470 			DThread* threadObj = NULL;
       
  1471 			NKern::ThreadEnterCS();
       
  1472 			if(iBreakPointList[i].iThreadSpecific)
       
  1473 				{
       
  1474 				threadObj = DebugUtils::OpenThreadHandle(id);
       
  1475 				}
       
  1476 			else
       
  1477 				{
       
  1478 				DProcess* process = DebugUtils::OpenProcessHandle(iBreakPointList[i].iId);
       
  1479 				if(process)
       
  1480 					{
       
  1481 					threadObj = DebugUtils::OpenFirstThreadForProcess(process);
       
  1482 					process->Close(NULL);
       
  1483 					}
       
  1484 				}
       
  1485 
       
  1486 			if (threadObj)
       
  1487 				{
       
  1488 				XTRAPD(r, XT_DEFAULT, err = DebugSupport::RestoreCode(threadObj, aAddress));			
       
  1489 				if(KErrNone != err || KErrNone != r)
       
  1490 					{
       
  1491 					LOG_MSG3("Error from DebugSupport::RestoreCode: r: %d, err: %d", r, err);
       
  1492 					}
       
  1493 				err = (KErrNone == r) ? err : r;
       
  1494 				threadObj->Close(NULL);
       
  1495 				}
       
  1496 			else
       
  1497 				{
       
  1498 				err = KErrBadHandle;
       
  1499 				LOG_MSG2("Couldn't find thread for breakpoint id %d", iBreakPointList[i].iId);
       
  1500 				}
       
  1501 			NKern::ThreadLeaveCS();
       
  1502 			if (err) break;
       
  1503 			}
       
  1504 		}
       
  1505 		
       
  1506 	return err;
       
  1507 	}
       
  1508 
       
  1509 /**
       
  1510 Public member function which enables previously disabled breakpoints within a given thread.
       
  1511 
       
  1512 Note 1:
       
  1513 This function ensures exclusive access to the breakpoint data structures
       
  1514 by using a semaphore to serialise access. 
       
  1515 
       
  1516 @see priv_DoEnableDisabledBreak
       
  1517 
       
  1518 Note 2:
       
  1519 As implied by Note 1, the caller must have previously called Init() or this
       
  1520 function will return KErrNotReady;
       
  1521 
       
  1522 @param aThreadId Thread in which to enable all previously disabled breakpoints
       
  1523 @return KErrNone if successful, one of the system wide error codes otherwise.
       
  1524 */
       
  1525 TInt D_RMD_Breakpoints::DoEnableDisabledBreak(TUint64 aThreadId)
       
  1526 	{
       
  1527 	// Ensure we have a valid semaphore
       
  1528 	if (!iInitialised || !iLock)
       
  1529 		{
       
  1530 		return KErrNotReady;
       
  1531 		}
       
  1532 
       
  1533 	// Acquire the lock
       
  1534 	NKern::ThreadEnterCS();
       
  1535 	Kern::SemaphoreWait(*iLock);
       
  1536 
       
  1537 	// Really do the work
       
  1538 	TInt err = priv_DoEnableDisabledBreak(aThreadId);
       
  1539 	
       
  1540 	// Release the lock
       
  1541 	Kern::SemaphoreSignal(*iLock);
       
  1542 	NKern::ThreadLeaveCS();
       
  1543 
       
  1544 	return err;
       
  1545 	}
       
  1546 
       
  1547 /**
       
  1548 Private member function which enables previously disabled breakpoints within a given thread.
       
  1549 
       
  1550 @see DoEnableDisabledBreak
       
  1551 
       
  1552 @param aThreadId Thread in which to enable all previously disabled breakpoints
       
  1553 @return KErrNone if successful, one of the system wide error codes otherwise.
       
  1554 */
       
  1555 TInt D_RMD_Breakpoints::priv_DoEnableDisabledBreak(TUint64 aThreadId)
       
  1556 	{
       
  1557 	LOG_MSG("D_RMD_Breakpoints::priv_DoEnableDisabledBreak()");
       
  1558 	NKern::ThreadEnterCS();
       
  1559 	DThread* thread = DebugUtils::OpenThreadHandle(aThreadId);
       
  1560 	if(!thread)
       
  1561 		{
       
  1562 		LOG_MSG2("Thread: 0x%08x does not exist", aThreadId);
       
  1563 		NKern::ThreadLeaveCS();
       
  1564 		return KErrNotFound;
       
  1565 		}
       
  1566 	TUint64 processId = thread->iOwningProcess->iId;
       
  1567 	thread->Close(NULL);
       
  1568 	NKern::ThreadLeaveCS();
       
  1569 
       
  1570 	for (TInt i = NUMBER_OF_TEMP_BREAKPOINTS; i < iBreakPointList.Count(); i++)
       
  1571 		{
       
  1572 		TBool needsEnabling = EFalse;
       
  1573 		if(iBreakPointList[i].iDisabledForStep)
       
  1574 			{
       
  1575 			if(iBreakPointList[i].iThreadSpecific)
       
  1576 				{
       
  1577 				needsEnabling = (aThreadId == iBreakPointList[i].iId);
       
  1578 				}
       
  1579 			else
       
  1580 				{
       
  1581 				needsEnabling = (processId == iBreakPointList[i].iId);
       
  1582 				}
       
  1583 			}
       
  1584 		if (needsEnabling)
       
  1585 			{
       
  1586 			LOG_MSG2("Re-enabling breakpoint at address %x", iBreakPointList[i].iAddress);
       
  1587 			TInt err = priv_DoEnableBreak(iBreakPointList[i], EFalse);
       
  1588 			if(KErrNone != err)
       
  1589 				{
       
  1590 				LOG_MSG2("Error returned from DoEnableBreak: %d", err);
       
  1591 				iBreakPointList[i].iDisabledForStep = EFalse;
       
  1592 				return err;
       
  1593 				}
       
  1594 			}
       
  1595 		}
       
  1596 	
       
  1597 	return KErrNone;
       
  1598 	}
       
  1599 
       
  1600 /**
       
  1601 Public member function which removes all the breakpoints within a given thread.
       
  1602 
       
  1603 Note 1:
       
  1604 This function ensures exclusive access to the breakpoint data structures
       
  1605 by using a semaphore to serialise access.
       
  1606 
       
  1607 @see priv_DoRemoveThreadBreaks
       
  1608 
       
  1609 Note 2:
       
  1610 As implied by Note 1, the caller must have previously called Init() or this
       
  1611 function will return KErrNotReady;
       
  1612 
       
  1613 @param aThreadId Thread from which to remove all existing breakpoints
       
  1614 @return KErrNone if successful, one of the system wide error codes otherwise.
       
  1615 */
       
  1616 void D_RMD_Breakpoints::DoRemoveThreadBreaks(TUint64 aThreadId)
       
  1617 	{
       
  1618 	// Ensure we have a valid semaphore
       
  1619 	if (!iInitialised || !iLock)
       
  1620 		{
       
  1621 		return;
       
  1622 		}
       
  1623 
       
  1624 	// Acquire the lock
       
  1625 	NKern::ThreadEnterCS();
       
  1626 	Kern::SemaphoreWait(*iLock);
       
  1627 
       
  1628 	// Really do the work
       
  1629 	priv_DoRemoveThreadBreaks(aThreadId);
       
  1630 
       
  1631 	// Release the lock
       
  1632 	Kern::SemaphoreSignal(*iLock);
       
  1633 	NKern::ThreadLeaveCS();
       
  1634 	}
       
  1635 
       
  1636 /**
       
  1637 Private member function which removes all the breakpoints particular to a particular thread
       
  1638 
       
  1639 @see DoRemoveThreadBreaks
       
  1640 
       
  1641 @param aThreadId Thread from which to remove all existing breakpoints
       
  1642 @return KErrNone if successful, one of the system wide error codes otherwise.
       
  1643 */
       
  1644 void D_RMD_Breakpoints::priv_DoRemoveThreadBreaks(TUint64 aThreadId)
       
  1645 	{
       
  1646 	LOG_MSG2("D_RMD_Breakpoints::priv_DoRemoveThreadBreaks(aThreadId = 0x%lx)\n",aThreadId);
       
  1647 
       
  1648 	TInt err = KErrNone;
       
  1649 	TUint64 threadId;
       
  1650 
       
  1651 	for (TInt i=iBreakPointList.Count()-1; i >= 0; i--)
       
  1652 		{
       
  1653 		if ((iBreakPointList[i].iAddress != 0) && !iBreakPointList[i].iObsoleteLibraryBreakpoint)
       
  1654 			{
       
  1655 			threadId = iBreakPointList[i].iId + (iBreakPointList[i].iThreadSpecific ? 0 : 1);
       
  1656 			if (threadId == aThreadId)
       
  1657 				{
       
  1658 				LOG_MSG5("D_RMD_Breakpoints::priv_DoRemoveThreadBreaks() - Clearing breakpoint[%d],idx=%x at address 0x%08x, iId=0x%016lx", 
       
  1659 				        i, iBreakPointList[i].iBreakId, iBreakPointList[i].iAddress, iBreakPointList[i].iId );
       
  1660 
       
  1661 				err = priv_DoClearBreak(iBreakPointList[i].iBreakId, EFalse);
       
  1662 
       
  1663 				if (err != KErrNone)
       
  1664 					{
       
  1665 					LOG_MSG2("D_RMD_Breakpoints::priv_DoRemoveThreadBreaks()  - failed to remove break id 0x%08x\n",iBreakPointList[i].iBreakId);
       
  1666 					return;
       
  1667 					}
       
  1668 				}
       
  1669 			}
       
  1670         else if(iBreakPointList[i].iAddress == 0)
       
  1671             {
       
  1672             LOG_MSG3("Breakpoint[%d]: address is 0, iId=0x%016lx", i, iBreakPointList[i].iId );     
       
  1673             }
       
  1674         else
       
  1675             {
       
  1676             LOG_MSG4("Breakpoint[%d]: Obsoleted, address =0x%x, iId=0x%016lx", i, iBreakPointList[i].iAddress, iBreakPointList[i].iId );     
       
  1677             }		
       
  1678 		}	
       
  1679 	}
       
  1680 
       
  1681 // Remove the process breakpoints for process with PID aProcessId in the range [aCodeAddress, aCodeAddress + aCodeSize)
       
  1682 void D_RMD_Breakpoints::RemoveBreaksForProcess(TUint64 aProcessId, TUint32 aCodeAddress, TUint32 aCodeSize)
       
  1683 	{
       
  1684 	LOG_MSG4("D_RMD_Breakpoints::RemoveBreaksForProcess(), aProcId=0x%016lx, codeAddr=0x%x, codeSize=0x%x", 
       
  1685 	        aProcessId,aCodeAddress, aCodeSize);
       
  1686 	NKern::ThreadEnterCS();
       
  1687 	for (TInt i=iBreakPointList.Count() - 1; i>=0; i--)
       
  1688 		{
       
  1689         TBool remove = EFalse;
       
  1690 		TBreakEntry& breakEntry = iBreakPointList[i];
       
  1691 		
       
  1692 		if( breakEntry.iId == 0 || breakEntry.iAddress == 0 )
       
  1693 		    {
       
  1694             breakEntry.Reset();
       
  1695 		    continue;
       
  1696 		    }
       
  1697 		
       
  1698 		LOG_MSG5(" break[%d], iId=0x%016lx, threadSpec=%d, aProcessId=0x%016lx", 
       
  1699 		        i, breakEntry.iId, breakEntry.iThreadSpecific, aProcessId);
       
  1700 		
       
  1701 		if(!breakEntry.iThreadSpecific && breakEntry.iId == aProcessId)
       
  1702 		    {
       
  1703 		    remove = ETrue;
       
  1704 		    }
       
  1705 		else if(breakEntry.iThreadSpecific)
       
  1706 		    {
       
  1707             //breakEntry.iId is thread id. Get its pid, then check if aProcessId is same, then remove
       
  1708             DThread* thread = DebugUtils::OpenThreadHandle(breakEntry.iId);
       
  1709             if(!thread)
       
  1710                 {
       
  1711                 LOG_MSG2("Could not open handle to thread (aThreadId = 0x%016lx)",breakEntry.iId);
       
  1712                 continue;
       
  1713                 }
       
  1714             
       
  1715             LOG_MSG2(" thread->iOwningProcess->iId=0x%016lx", thread->iOwningProcess->iId );
       
  1716             
       
  1717             if( thread->iOwningProcess->iId == aProcessId )
       
  1718                 {
       
  1719                 LOG_MSG3("Thread spec breakpoint @ index[%d] matches aProcessId= 0x%016lx. Removing",i, aProcessId);
       
  1720                 remove = ETrue;
       
  1721                 }
       
  1722             
       
  1723             thread->Close(NULL);
       
  1724 		    }
       
  1725 		    
       
  1726         if ( remove && (breakEntry.iAddress >= aCodeAddress) && (breakEntry.iAddress < (aCodeAddress + aCodeSize)))
       
  1727             {
       
  1728             LOG_MSG2("Removing process breakpoint at address %x", (TUint32)breakEntry.iAddress);
       
  1729             TInt err = DoClearBreak(breakEntry.iBreakId, ETrue);
       
  1730             if(KErrNone != err)
       
  1731                 {
       
  1732                 LOG_MSG2("Error removing breakpoint: %d", err);
       
  1733                 }
       
  1734             }
       
  1735         else
       
  1736             {
       
  1737             LOG_MSG4("Not removing breakpoint at index[%d], id=0x%016lx, address=0x%x", 
       
  1738                     i, breakEntry.iId, (TUint32)breakEntry.iAddress);
       
  1739             }
       
  1740 		}
       
  1741 	NKern::ThreadLeaveCS();
       
  1742 	}
       
  1743 
       
  1744 // mark the breakpoints in the range [aCodeAddress, aCodeAddress + aCodeSize)
       
  1745 void D_RMD_Breakpoints::InvalidateLibraryBreakPoints(TUint32 aCodeAddress, TUint32 aCodeSize)
       
  1746 	{
       
  1747 	LOG_MSG3("D_RMD_Breakpoints::InvalidateLibraryBreakPoints(aCodeAddress=0x%x, aCodeSize=0x%x)",
       
  1748 	        aCodeAddress, aCodeSize );
       
  1749 	
       
  1750 	for (TInt i=0; i<iBreakPointList.Count(); i++)
       
  1751 		{
       
  1752 		if ((iBreakPointList[i].iAddress >= aCodeAddress) && (iBreakPointList[i].iAddress < (aCodeAddress + aCodeSize)))
       
  1753 			{
       
  1754 			LOG_MSG2("Obsoleting library breakpoint at address %x", iBreakPointList[i].iAddress);
       
  1755 			iBreakPointList[i].iObsoleteLibraryBreakpoint = ETrue;
       
  1756 			}
       
  1757 		}
       
  1758 	}
       
  1759 
       
  1760 TInt D_RMD_Breakpoints::BreakPointCount() const
       
  1761 	{
       
  1762 	return iBreakPointList.Count();
       
  1763 	}
       
  1764 
       
  1765 /**
       
  1766   Gets next breakpoint in list.
       
  1767   @param aBreakEntry The break entry to get the successor of. If NULL then returns the first entry.
       
  1768   @return A pointer to the next break entry, or NULL if the end of the list has been reached
       
  1769   */
       
  1770 TBreakEntry* D_RMD_Breakpoints::GetNextBreak(const TBreakEntry* aBreakEntry) const
       
  1771 	{
       
  1772 	if(!aBreakEntry)
       
  1773 		{
       
  1774 		return (TBreakEntry*)&(iBreakPointList[0]);
       
  1775 		}
       
  1776 	TInt index = iBreakPointList.FindInSignedKeyOrder(*aBreakEntry) + 1;
       
  1777 	return (index < BreakPointCount()) ? (TBreakEntry*)&(iBreakPointList[index]) : NULL;
       
  1778 	}
       
  1779 
       
  1780 TBool D_RMD_Breakpoints::IsTemporaryBreak(const TBreakEntry& aBreakEntry) const
       
  1781 	{
       
  1782 	// Ensure we have a valid semaphore
       
  1783 	if (!iInitialised || !iLock)
       
  1784 		{
       
  1785 		return EFalse;
       
  1786 		}
       
  1787 
       
  1788 	// Acquire the lock
       
  1789 	NKern::ThreadEnterCS();
       
  1790 	Kern::SemaphoreWait(*iLock);
       
  1791 
       
  1792 	// Really do the work
       
  1793 	TBool tempBreak = priv_IsTemporaryBreak(aBreakEntry);
       
  1794 	
       
  1795 	// Release the lock
       
  1796 	Kern::SemaphoreSignal(*iLock);
       
  1797 	NKern::ThreadLeaveCS();
       
  1798 	
       
  1799 	return tempBreak;
       
  1800 	}
       
  1801 
       
  1802 /**
       
  1803 Private member function which tells us if a breakpoint is temporary
       
  1804 
       
  1805 @see IsTemporaryBreak
       
  1806 
       
  1807 @param aBreakEntry
       
  1808 @return TBool indicating if the break is temporary or not
       
  1809 */
       
  1810 TBool D_RMD_Breakpoints::priv_IsTemporaryBreak(const TBreakEntry& aBreakEntry) const 
       
  1811 	{
       
  1812 	return aBreakEntry.iBreakId < NUMBER_OF_TEMP_BREAKPOINTS;
       
  1813 	}
       
  1814 
       
  1815 
       
  1816 // End of file - d_rmd_breakpoints.cpp