os/kernelhwsrv/kernel/eka/drivers/debug/rmdebug/d_target_process.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) 2006-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
// Purpose: The DProcessTracker object tracks which processes are being
sl@0
    15
// debugged. The DProcessTracker class uses a DTargetProcess object for
sl@0
    16
// each process being debugged.
sl@0
    17
// Note: Although TheDProcessTracker object is a global, it should be unique
sl@0
    18
// as only the Debug Security Server should load and use this driver.
sl@0
    19
// 
sl@0
    20
//
sl@0
    21
sl@0
    22
#include <e32def.h>
sl@0
    23
#include <e32def_private.h>
sl@0
    24
#include <e32cmn.h>
sl@0
    25
#include <e32cmn_private.h>
sl@0
    26
#include <kernel/kernel.h>
sl@0
    27
#include <kernel/kern_priv.h>
sl@0
    28
#include "nk_priv.h"
sl@0
    29
#include <rm_debug_api.h>
sl@0
    30
sl@0
    31
#include "d_target_process.h"
sl@0
    32
#include "debug_logging.h"
sl@0
    33
#include "debug_utils.h"
sl@0
    34
sl@0
    35
// ctor
sl@0
    36
DTargetProcess::DTargetProcess()
sl@0
    37
	:iProcessName(0,0,0)
sl@0
    38
	{
sl@0
    39
	}
sl@0
    40
sl@0
    41
// dtor
sl@0
    42
DTargetProcess::~DTargetProcess()
sl@0
    43
	{
sl@0
    44
	// Delete the space allocated for the name if any
sl@0
    45
	if (iProcessName.Ptr() != 0)
sl@0
    46
		{
sl@0
    47
		NKern::ThreadEnterCS();
sl@0
    48
		Kern::Free((TAny*)iProcessName.Ptr());
sl@0
    49
		NKern::ThreadLeaveCS();
sl@0
    50
		}
sl@0
    51
	//Reset the array and delete the objects that its members point to
sl@0
    52
	NKern::ThreadEnterCS();
sl@0
    53
	iAgentList.ResetAndDestroy();
sl@0
    54
	NKern::ThreadLeaveCS();
sl@0
    55
	}
sl@0
    56
sl@0
    57
// Compare two DTargetProcess items. They are the same if they have the same name.
sl@0
    58
TInt DTargetProcess::Compare(const DTargetProcess& aFirst, const DTargetProcess& aSecond)
sl@0
    59
	{
sl@0
    60
	return aFirst.iProcessName.Compare(aSecond.iProcessName);
sl@0
    61
	}
sl@0
    62
sl@0
    63
// Set the name of the process we are tracking
sl@0
    64
TInt DTargetProcess::SetProcessName(const TDesC8& aProcessName)
sl@0
    65
	{
sl@0
    66
	// Argument checking
sl@0
    67
	if (aProcessName.Length() < 1)
sl@0
    68
		{
sl@0
    69
		return KErrArgument;
sl@0
    70
		}
sl@0
    71
sl@0
    72
	// Allocate some memory to store the name
sl@0
    73
	TUint length = aProcessName.Length();
sl@0
    74
sl@0
    75
	NKern::ThreadEnterCS();
sl@0
    76
	TUint8* buffer = (TUint8*)Kern::AllocZ(length);
sl@0
    77
	NKern::ThreadLeaveCS();
sl@0
    78
	if (buffer==NULL)
sl@0
    79
		{
sl@0
    80
		// Out of memory
sl@0
    81
		return KErrNoMemory;
sl@0
    82
		}
sl@0
    83
sl@0
    84
	// Set iProcessName to use the alloc'd buffer
sl@0
    85
	iProcessName.Set(buffer,length,length);
sl@0
    86
sl@0
    87
	// Store aProcessName within this object
sl@0
    88
	iProcessName.Copy(aProcessName);
sl@0
    89
sl@0
    90
	return KErrNone;
sl@0
    91
	}
sl@0
    92
sl@0
    93
// Obtain the name of the process being tracked
sl@0
    94
const TPtr8& DTargetProcess::ProcessName(void)
sl@0
    95
	{
sl@0
    96
	return iProcessName;
sl@0
    97
	}
sl@0
    98
sl@0
    99
// Returns a pointer to the DDebugAgent with aAgentId.
sl@0
   100
// If the agent is not in the list, it returns NULL.
sl@0
   101
DDebugAgent* DTargetProcess::Agent(TUint64 aAgentId)
sl@0
   102
	{
sl@0
   103
	for(TInt i = 0; i < iAgentList.Count(); i++)
sl@0
   104
	{
sl@0
   105
		if (iAgentList[i]->Id() == aAgentId)
sl@0
   106
		{
sl@0
   107
			return iAgentList[i];
sl@0
   108
		}
sl@0
   109
	}
sl@0
   110
sl@0
   111
	// what do we return if we don't have any agents?
sl@0
   112
	return NULL;
sl@0
   113
	}
sl@0
   114
sl@0
   115
// Adds aAgentId as a tracking agent for this process.
sl@0
   116
TInt DTargetProcess::AddAgent(TUint64 aAgentId)
sl@0
   117
	{
sl@0
   118
	LOG_MSG("DTargetProcess::AddAgent()");
sl@0
   119
	DDebugAgent* agent = DDebugAgent::New(aAgentId);
sl@0
   120
	if(agent == NULL)
sl@0
   121
		{
sl@0
   122
		LOG_MSG("DTargetProcess::AddAgent() couldn't allocate memory for DDebugAgent");
sl@0
   123
		return KErrNoMemory;
sl@0
   124
		}
sl@0
   125
	return iAgentList.Insert(agent,0);
sl@0
   126
	}
sl@0
   127
sl@0
   128
// Stops tracking the process with this agent
sl@0
   129
TInt DTargetProcess::RemoveAgent(TUint64 aAgentId)
sl@0
   130
	{
sl@0
   131
	// We need to find and then remove the agent
sl@0
   132
	for(TUint i = 0; i < iAgentList.Count(); i++)
sl@0
   133
		{
sl@0
   134
		if (iAgentList[i]->Id() == aAgentId)
sl@0
   135
			{
sl@0
   136
			delete iAgentList[i];
sl@0
   137
			iAgentList.Remove(i);
sl@0
   138
			return KErrNone;
sl@0
   139
			}
sl@0
   140
		}
sl@0
   141
sl@0
   142
	return KErrNotFound;
sl@0
   143
	}
sl@0
   144
sl@0
   145
// Index through the agents by position
sl@0
   146
DDebugAgent* DTargetProcess::operator[](TInt aIndex)
sl@0
   147
	{
sl@0
   148
	return iAgentList[aIndex];
sl@0
   149
	}
sl@0
   150
sl@0
   151
// returns the number of agents tracking this process.
sl@0
   152
TInt DTargetProcess::AgentCount(void)
sl@0
   153
	{
sl@0
   154
	return iAgentList.Count();
sl@0
   155
	}
sl@0
   156
sl@0
   157
/**
sl@0
   158
  Resume the specified thread
sl@0
   159
sl@0
   160
  @param aThread thread to resume
sl@0
   161
sl@0
   162
  @return KErrNone if the thread has previously been suspended and is resumed,
sl@0
   163
  KErrNotFound if the thread has not previously been suspended
sl@0
   164
  */
sl@0
   165
TInt DTargetProcess::ResumeThread(DThread* aThread)
sl@0
   166
	{
sl@0
   167
	LOG_MSG2("DTargetProcess::ResumeSuspendedThread(): thread=0x%08x", aThread);
sl@0
   168
	TInt err1 = ResumeSuspendedThread(aThread);
sl@0
   169
	LOG_MSG2("DTargetProcess::ResumeSuspendedThread(): ret=%d)", err1); 
sl@0
   170
	TInt err2 = ResumeFrozenThread(aThread->iNThread);
sl@0
   171
	LOG_MSG2("DTargetProcess::ResumeFrozenThread(): ret=%d)", err2);
sl@0
   172
	//if resuming the suspended thread failed for an obscure reason return it
sl@0
   173
	if((err1 != KErrNotFound) && (err1 != KErrNone))
sl@0
   174
		{
sl@0
   175
		LOG_MSG2("DTargetProcess::ResumeThread() unexpected exit, err1: %d", err1);
sl@0
   176
		return err1;
sl@0
   177
		}
sl@0
   178
	//if resuming the frozen thread failed for an obscure reason return it
sl@0
   179
	if((err2 != KErrNotFound) && (err2 != KErrNone))
sl@0
   180
		{
sl@0
   181
		LOG_MSG2("DTargetProcess::ResumeThread() unexpected exit, err2: %d", err2);
sl@0
   182
		return err2;
sl@0
   183
		}
sl@0
   184
	// if resuming the suspended thread succeeded in both cases, we have a consistency problem
sl@0
   185
	if ((err1 == KErrNone) && (err2 == KErrNone))
sl@0
   186
		{
sl@0
   187
		LOG_MSG("DTargetProcess::ResumeThread() unexpected exit, err1 == err2 == KErrNone");
sl@0
   188
		}
sl@0
   189
sl@0
   190
	//if the thread was in neither list return KErrNotFound, otherwise KErrNone
sl@0
   191
	return ((err1 == KErrNone) || (err2 == KErrNone)) ? KErrNone : KErrNotFound;
sl@0
   192
	}
sl@0
   193
sl@0
   194
/**
sl@0
   195
  Resume the specified frozen thread
sl@0
   196
sl@0
   197
  @param aThread thread to resume
sl@0
   198
sl@0
   199
  @return KErrNone if the thread has previously been suspended and is resumed,
sl@0
   200
  KErrNotFound if the thread has not previously been suspended
sl@0
   201
  */
sl@0
   202
TInt DTargetProcess::ResumeFrozenThread(NThread& aThread)
sl@0
   203
	{
sl@0
   204
	for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
sl@0
   205
		{
sl@0
   206
		if(iFrozenThreadSemaphores[i]->iOwningThread == &aThread)
sl@0
   207
			{
sl@0
   208
			NKern::FSSignal(iFrozenThreadSemaphores[i]);
sl@0
   209
			NKern::ThreadEnterCS();
sl@0
   210
			delete iFrozenThreadSemaphores[i];
sl@0
   211
			NKern::ThreadLeaveCS();
sl@0
   212
			iFrozenThreadSemaphores.Remove(i);
sl@0
   213
			return KErrNone;
sl@0
   214
			}
sl@0
   215
		}
sl@0
   216
	return KErrNotFound;
sl@0
   217
	}
sl@0
   218
sl@0
   219
/**
sl@0
   220
  Resume the specified suspended thread
sl@0
   221
sl@0
   222
  @param aThread thread to resume
sl@0
   223
sl@0
   224
  @return KErrNone if the thread has previously been suspended and is resumed,
sl@0
   225
  KErrNotFound if the thread has not previously been suspended
sl@0
   226
  */
sl@0
   227
TInt DTargetProcess::ResumeSuspendedThread(DThread* aThread)
sl@0
   228
	{
sl@0
   229
	TUint64 threadId = (TUint64)aThread->iId;
sl@0
   230
	for(TInt i=0; i<iSuspendedThreads.Count(); i++)
sl@0
   231
		{
sl@0
   232
		if(iSuspendedThreads[i] == threadId)
sl@0
   233
			{
sl@0
   234
			iSuspendedThreads.Remove(i);
sl@0
   235
			LOG_MSG2("DTargetProcess::ResumeSuspendedThread()> Kern::ThreadResume() 0x%x", aThread);
sl@0
   236
			Kern::ThreadResume(*aThread);
sl@0
   237
			return KErrNone;
sl@0
   238
			}
sl@0
   239
		}
sl@0
   240
	return KErrNotFound;
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
  Suspend the specified thread
sl@0
   245
sl@0
   246
  @param aThread thread to suspend
sl@0
   247
sl@0
   248
  @param aFreezeThread suspend the thread on a Fast Semaphore if
sl@0
   249
  ETrue. EFalse means suspend by calling Kern::Suspend.
sl@0
   250
sl@0
   251
  @return KErrNone if the thread is successfully suspended,
sl@0
   252
  KErrAlreadyExists if the agent has already suspended the thread,
sl@0
   253
  or one of the other system wide error codes
sl@0
   254
  
sl@0
   255
   This function suspends a thread by calling Kern::Thread Suspend.
sl@0
   256
                                                                                                       
sl@0
   257
  An alternative means of suspending the _current_ thread only
sl@0
   258
  is by call DTargetProcess::FreezeThread. This will ensure that
sl@0
   259
  the current thread is suspended when exception processing for this
sl@0
   260
  thread completes (see rm_debug_eventhandler.cpp)
sl@0
   261
  
sl@0
   262
  */
sl@0
   263
TInt DTargetProcess::SuspendThread(DThread* aThread, TBool aFreezeThread)
sl@0
   264
	{
sl@0
   265
	// should check if this thread is already suspended/frozen and return if so
sl@0
   266
	// but just warn for the moment.
sl@0
   267
	if (CheckSuspended(aThread))
sl@0
   268
		{
sl@0
   269
		// thread was already suspended, don't bother doing it again
sl@0
   270
		LOG_MSG2("DTargetProcess::SuspendThread - Thread Id 0x%08x already suspended\n",aThread->iId);
sl@0
   271
		//return KErrAlreadyExists;	
sl@0
   272
		}
sl@0
   273
sl@0
   274
	return aFreezeThread ? FreezeThread() : DoSuspendThread(aThread);
sl@0
   275
	}
sl@0
   276
sl@0
   277
/**
sl@0
   278
  Freeze the current thread
sl@0
   279
sl@0
   280
  @return KErrNone if the thread is successfully suspended,
sl@0
   281
  KErrAlreadyExists if the agent has already suspended the thread,
sl@0
   282
  or one of the other system wide error codes
sl@0
   283
sl@0
   284
  This marks the current thread for waiting on a Fast Semaphore
sl@0
   285
  when exception handling for this thread has completed - see
sl@0
   286
  rm_debug_eventhandler.cpp for details.
sl@0
   287
  */
sl@0
   288
TInt DTargetProcess::FreezeThread()
sl@0
   289
	{
sl@0
   290
	// create and store a fast semaphore to stop the thread on
sl@0
   291
	NKern::ThreadEnterCS();
sl@0
   292
	NFastSemaphore* sem = new NFastSemaphore();
sl@0
   293
	NKern::ThreadLeaveCS();
sl@0
   294
	sem->iOwningThread = &(Kern::CurrentThread().iNThread);
sl@0
   295
	LOG_EVENT_MSG2("DTargetProcess::FreezeThread(): new NFastSemaphore() curr thread=0x%x", sem->iOwningThread);
sl@0
   296
	return iFrozenThreadSemaphores.Append(sem);
sl@0
   297
	}
sl@0
   298
sl@0
   299
/**
sl@0
   300
  Suspend the specified thread
sl@0
   301
sl@0
   302
  @param aThread thread to suspend
sl@0
   303
sl@0
   304
  @return KErrNone if the thread is successfully suspended,
sl@0
   305
  KErrAlreadyExists if the agent has already suspended the thread,
sl@0
   306
  or one of the other system wide error codes
sl@0
   307
  */
sl@0
   308
TInt DTargetProcess::DoSuspendThread(DThread* aThread)
sl@0
   309
	{
sl@0
   310
	TUint64 threadId = (TUint64)aThread->iId;
sl@0
   311
	
sl@0
   312
	// Don't suspend if this thread is already suspended (by FSWait or
sl@0
   313
	// Kern::ThreadSuspend
sl@0
   314
	if (CheckSuspended(aThread))
sl@0
   315
		{
sl@0
   316
		// thread was already suspended, don't bother doing it again
sl@0
   317
		LOG_MSG2("DTargetProcess::SuspendThread - Thread Id 0x%08x already suspended\n",threadId);
sl@0
   318
		return KErrAlreadyExists;	
sl@0
   319
		}
sl@0
   320
sl@0
   321
	// Add thread to the suspend list
sl@0
   322
	TInt err = iSuspendedThreads.Append(threadId);
sl@0
   323
	if(err == KErrNone)
sl@0
   324
		{
sl@0
   325
		Kern::ThreadSuspend(*aThread, 1);
sl@0
   326
		}
sl@0
   327
	return err;
sl@0
   328
	}
sl@0
   329
sl@0
   330
/**
sl@0
   331
 Waits the current thread on a Fast Semaphore.
sl@0
   332
sl@0
   333
 This is useful for situations where the current thread
sl@0
   334
 has hit a breakpoint within a critical section, and
sl@0
   335
 otherwise could not be suspended at this point.
sl@0
   336
sl@0
   337
 Note that the Fast Semaphore structure on which the thread
sl@0
   338
 waits must be a member data item of this class instance,
sl@0
   339
 as it needs to be FSSignal()'d by another thread to resume
sl@0
   340
 again.
sl@0
   341
 */
sl@0
   342
void DTargetProcess::FSWait()
sl@0
   343
	{
sl@0
   344
	LOG_MSG2("DTargetProcess::NotifyEvent(): number of attached agents: %d", AgentCount());
sl@0
   345
	NThread* currentThread = &(Kern::CurrentThread().iNThread);
sl@0
   346
	for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
sl@0
   347
		{
sl@0
   348
		if(iFrozenThreadSemaphores[i]->iOwningThread == currentThread)
sl@0
   349
			{
sl@0
   350
			LOG_MSG3("DTargetProcess::FSWait(): > FSWait frozen sem %d, curr thread=0x%x", i, currentThread);
sl@0
   351
			NKern::FSWait(iFrozenThreadSemaphores[i]);
sl@0
   352
			return;
sl@0
   353
			}
sl@0
   354
		}
sl@0
   355
	}
sl@0
   356
sl@0
   357
/**
sl@0
   358
  Checks that the thread has been suspended
sl@0
   359
sl@0
   360
  @param aThread thread to check suspended
sl@0
   361
sl@0
   362
  @return ETrue if the thread has been suspended,
sl@0
   363
  EFalse if the thread has not been suspended
sl@0
   364
  */
sl@0
   365
TBool DTargetProcess::CheckSuspended(DThread* aThread) const
sl@0
   366
	{
sl@0
   367
	if(!aThread)
sl@0
   368
		{
sl@0
   369
		return EFalse;
sl@0
   370
		}
sl@0
   371
	//check if the thread is in the suspended threads list
sl@0
   372
	for(TInt i=0; i<iSuspendedThreads.Count(); i++)
sl@0
   373
		{
sl@0
   374
		if(iSuspendedThreads[i] == (TUint64)aThread->iId)
sl@0
   375
			{
sl@0
   376
			return ETrue;
sl@0
   377
			}
sl@0
   378
		}
sl@0
   379
	// not in the suspended threads list so check in the frozen threads list
sl@0
   380
	NThread* nThread = &(aThread->iNThread);
sl@0
   381
	for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
sl@0
   382
		{
sl@0
   383
		if(iFrozenThreadSemaphores[i]->iOwningThread == nThread)
sl@0
   384
			{
sl@0
   385
			return ETrue;
sl@0
   386
			}
sl@0
   387
		}
sl@0
   388
	return EFalse;
sl@0
   389
	}
sl@0
   390
sl@0
   391
/*
sl@0
   392
@return ETrue if the debug driver has suspended any of the process' threads, EFalse otherwise
sl@0
   393
*/
sl@0
   394
TBool DTargetProcess::HasSuspendedThreads() const
sl@0
   395
	{
sl@0
   396
	return (iSuspendedThreads.Count() > 0) || (iFrozenThreadSemaphores.Count() > 0);
sl@0
   397
	}
sl@0
   398
sl@0
   399
void DTargetProcess::NotifyEvent(const TDriverEventInfo& aEventInfo)
sl@0
   400
	{
sl@0
   401
	// Stuff the event info into all the tracking agents event queues
sl@0
   402
	LOG_MSG2("DTargetProcess::NotifyEvent(): number of attached agents: %d", AgentCount());
sl@0
   403
sl@0
   404
	for(TInt i = 0; i < AgentCount(); i++)
sl@0
   405
		{
sl@0
   406
		// Index through all the relevant debug agents
sl@0
   407
		DDebugAgent* debugAgent = iAgentList[i];
sl@0
   408
		if(debugAgent != NULL)
sl@0
   409
			{
sl@0
   410
			debugAgent->NotifyEvent(aEventInfo);
sl@0
   411
			}
sl@0
   412
		}
sl@0
   413
	}
sl@0
   414