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