1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/drivers/debug/rmdebug/d_target_process.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,414 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Purpose: The DProcessTracker object tracks which processes are being
1.18 +// debugged. The DProcessTracker class uses a DTargetProcess object for
1.19 +// each process being debugged.
1.20 +// Note: Although TheDProcessTracker object is a global, it should be unique
1.21 +// as only the Debug Security Server should load and use this driver.
1.22 +//
1.23 +//
1.24 +
1.25 +#include <e32def.h>
1.26 +#include <e32def_private.h>
1.27 +#include <e32cmn.h>
1.28 +#include <e32cmn_private.h>
1.29 +#include <kernel/kernel.h>
1.30 +#include <kernel/kern_priv.h>
1.31 +#include "nk_priv.h"
1.32 +#include <rm_debug_api.h>
1.33 +
1.34 +#include "d_target_process.h"
1.35 +#include "debug_logging.h"
1.36 +#include "debug_utils.h"
1.37 +
1.38 +// ctor
1.39 +DTargetProcess::DTargetProcess()
1.40 + :iProcessName(0,0,0)
1.41 + {
1.42 + }
1.43 +
1.44 +// dtor
1.45 +DTargetProcess::~DTargetProcess()
1.46 + {
1.47 + // Delete the space allocated for the name if any
1.48 + if (iProcessName.Ptr() != 0)
1.49 + {
1.50 + NKern::ThreadEnterCS();
1.51 + Kern::Free((TAny*)iProcessName.Ptr());
1.52 + NKern::ThreadLeaveCS();
1.53 + }
1.54 + //Reset the array and delete the objects that its members point to
1.55 + NKern::ThreadEnterCS();
1.56 + iAgentList.ResetAndDestroy();
1.57 + NKern::ThreadLeaveCS();
1.58 + }
1.59 +
1.60 +// Compare two DTargetProcess items. They are the same if they have the same name.
1.61 +TInt DTargetProcess::Compare(const DTargetProcess& aFirst, const DTargetProcess& aSecond)
1.62 + {
1.63 + return aFirst.iProcessName.Compare(aSecond.iProcessName);
1.64 + }
1.65 +
1.66 +// Set the name of the process we are tracking
1.67 +TInt DTargetProcess::SetProcessName(const TDesC8& aProcessName)
1.68 + {
1.69 + // Argument checking
1.70 + if (aProcessName.Length() < 1)
1.71 + {
1.72 + return KErrArgument;
1.73 + }
1.74 +
1.75 + // Allocate some memory to store the name
1.76 + TUint length = aProcessName.Length();
1.77 +
1.78 + NKern::ThreadEnterCS();
1.79 + TUint8* buffer = (TUint8*)Kern::AllocZ(length);
1.80 + NKern::ThreadLeaveCS();
1.81 + if (buffer==NULL)
1.82 + {
1.83 + // Out of memory
1.84 + return KErrNoMemory;
1.85 + }
1.86 +
1.87 + // Set iProcessName to use the alloc'd buffer
1.88 + iProcessName.Set(buffer,length,length);
1.89 +
1.90 + // Store aProcessName within this object
1.91 + iProcessName.Copy(aProcessName);
1.92 +
1.93 + return KErrNone;
1.94 + }
1.95 +
1.96 +// Obtain the name of the process being tracked
1.97 +const TPtr8& DTargetProcess::ProcessName(void)
1.98 + {
1.99 + return iProcessName;
1.100 + }
1.101 +
1.102 +// Returns a pointer to the DDebugAgent with aAgentId.
1.103 +// If the agent is not in the list, it returns NULL.
1.104 +DDebugAgent* DTargetProcess::Agent(TUint64 aAgentId)
1.105 + {
1.106 + for(TInt i = 0; i < iAgentList.Count(); i++)
1.107 + {
1.108 + if (iAgentList[i]->Id() == aAgentId)
1.109 + {
1.110 + return iAgentList[i];
1.111 + }
1.112 + }
1.113 +
1.114 + // what do we return if we don't have any agents?
1.115 + return NULL;
1.116 + }
1.117 +
1.118 +// Adds aAgentId as a tracking agent for this process.
1.119 +TInt DTargetProcess::AddAgent(TUint64 aAgentId)
1.120 + {
1.121 + LOG_MSG("DTargetProcess::AddAgent()");
1.122 + DDebugAgent* agent = DDebugAgent::New(aAgentId);
1.123 + if(agent == NULL)
1.124 + {
1.125 + LOG_MSG("DTargetProcess::AddAgent() couldn't allocate memory for DDebugAgent");
1.126 + return KErrNoMemory;
1.127 + }
1.128 + return iAgentList.Insert(agent,0);
1.129 + }
1.130 +
1.131 +// Stops tracking the process with this agent
1.132 +TInt DTargetProcess::RemoveAgent(TUint64 aAgentId)
1.133 + {
1.134 + // We need to find and then remove the agent
1.135 + for(TUint i = 0; i < iAgentList.Count(); i++)
1.136 + {
1.137 + if (iAgentList[i]->Id() == aAgentId)
1.138 + {
1.139 + delete iAgentList[i];
1.140 + iAgentList.Remove(i);
1.141 + return KErrNone;
1.142 + }
1.143 + }
1.144 +
1.145 + return KErrNotFound;
1.146 + }
1.147 +
1.148 +// Index through the agents by position
1.149 +DDebugAgent* DTargetProcess::operator[](TInt aIndex)
1.150 + {
1.151 + return iAgentList[aIndex];
1.152 + }
1.153 +
1.154 +// returns the number of agents tracking this process.
1.155 +TInt DTargetProcess::AgentCount(void)
1.156 + {
1.157 + return iAgentList.Count();
1.158 + }
1.159 +
1.160 +/**
1.161 + Resume the specified thread
1.162 +
1.163 + @param aThread thread to resume
1.164 +
1.165 + @return KErrNone if the thread has previously been suspended and is resumed,
1.166 + KErrNotFound if the thread has not previously been suspended
1.167 + */
1.168 +TInt DTargetProcess::ResumeThread(DThread* aThread)
1.169 + {
1.170 + LOG_MSG2("DTargetProcess::ResumeSuspendedThread(): thread=0x%08x", aThread);
1.171 + TInt err1 = ResumeSuspendedThread(aThread);
1.172 + LOG_MSG2("DTargetProcess::ResumeSuspendedThread(): ret=%d)", err1);
1.173 + TInt err2 = ResumeFrozenThread(aThread->iNThread);
1.174 + LOG_MSG2("DTargetProcess::ResumeFrozenThread(): ret=%d)", err2);
1.175 + //if resuming the suspended thread failed for an obscure reason return it
1.176 + if((err1 != KErrNotFound) && (err1 != KErrNone))
1.177 + {
1.178 + LOG_MSG2("DTargetProcess::ResumeThread() unexpected exit, err1: %d", err1);
1.179 + return err1;
1.180 + }
1.181 + //if resuming the frozen thread failed for an obscure reason return it
1.182 + if((err2 != KErrNotFound) && (err2 != KErrNone))
1.183 + {
1.184 + LOG_MSG2("DTargetProcess::ResumeThread() unexpected exit, err2: %d", err2);
1.185 + return err2;
1.186 + }
1.187 + // if resuming the suspended thread succeeded in both cases, we have a consistency problem
1.188 + if ((err1 == KErrNone) && (err2 == KErrNone))
1.189 + {
1.190 + LOG_MSG("DTargetProcess::ResumeThread() unexpected exit, err1 == err2 == KErrNone");
1.191 + }
1.192 +
1.193 + //if the thread was in neither list return KErrNotFound, otherwise KErrNone
1.194 + return ((err1 == KErrNone) || (err2 == KErrNone)) ? KErrNone : KErrNotFound;
1.195 + }
1.196 +
1.197 +/**
1.198 + Resume the specified frozen thread
1.199 +
1.200 + @param aThread thread to resume
1.201 +
1.202 + @return KErrNone if the thread has previously been suspended and is resumed,
1.203 + KErrNotFound if the thread has not previously been suspended
1.204 + */
1.205 +TInt DTargetProcess::ResumeFrozenThread(NThread& aThread)
1.206 + {
1.207 + for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
1.208 + {
1.209 + if(iFrozenThreadSemaphores[i]->iOwningThread == &aThread)
1.210 + {
1.211 + NKern::FSSignal(iFrozenThreadSemaphores[i]);
1.212 + NKern::ThreadEnterCS();
1.213 + delete iFrozenThreadSemaphores[i];
1.214 + NKern::ThreadLeaveCS();
1.215 + iFrozenThreadSemaphores.Remove(i);
1.216 + return KErrNone;
1.217 + }
1.218 + }
1.219 + return KErrNotFound;
1.220 + }
1.221 +
1.222 +/**
1.223 + Resume the specified suspended thread
1.224 +
1.225 + @param aThread thread to resume
1.226 +
1.227 + @return KErrNone if the thread has previously been suspended and is resumed,
1.228 + KErrNotFound if the thread has not previously been suspended
1.229 + */
1.230 +TInt DTargetProcess::ResumeSuspendedThread(DThread* aThread)
1.231 + {
1.232 + TUint64 threadId = (TUint64)aThread->iId;
1.233 + for(TInt i=0; i<iSuspendedThreads.Count(); i++)
1.234 + {
1.235 + if(iSuspendedThreads[i] == threadId)
1.236 + {
1.237 + iSuspendedThreads.Remove(i);
1.238 + LOG_MSG2("DTargetProcess::ResumeSuspendedThread()> Kern::ThreadResume() 0x%x", aThread);
1.239 + Kern::ThreadResume(*aThread);
1.240 + return KErrNone;
1.241 + }
1.242 + }
1.243 + return KErrNotFound;
1.244 + }
1.245 +
1.246 +/**
1.247 + Suspend the specified thread
1.248 +
1.249 + @param aThread thread to suspend
1.250 +
1.251 + @param aFreezeThread suspend the thread on a Fast Semaphore if
1.252 + ETrue. EFalse means suspend by calling Kern::Suspend.
1.253 +
1.254 + @return KErrNone if the thread is successfully suspended,
1.255 + KErrAlreadyExists if the agent has already suspended the thread,
1.256 + or one of the other system wide error codes
1.257 +
1.258 + This function suspends a thread by calling Kern::Thread Suspend.
1.259 +
1.260 + An alternative means of suspending the _current_ thread only
1.261 + is by call DTargetProcess::FreezeThread. This will ensure that
1.262 + the current thread is suspended when exception processing for this
1.263 + thread completes (see rm_debug_eventhandler.cpp)
1.264 +
1.265 + */
1.266 +TInt DTargetProcess::SuspendThread(DThread* aThread, TBool aFreezeThread)
1.267 + {
1.268 + // should check if this thread is already suspended/frozen and return if so
1.269 + // but just warn for the moment.
1.270 + if (CheckSuspended(aThread))
1.271 + {
1.272 + // thread was already suspended, don't bother doing it again
1.273 + LOG_MSG2("DTargetProcess::SuspendThread - Thread Id 0x%08x already suspended\n",aThread->iId);
1.274 + //return KErrAlreadyExists;
1.275 + }
1.276 +
1.277 + return aFreezeThread ? FreezeThread() : DoSuspendThread(aThread);
1.278 + }
1.279 +
1.280 +/**
1.281 + Freeze the current thread
1.282 +
1.283 + @return KErrNone if the thread is successfully suspended,
1.284 + KErrAlreadyExists if the agent has already suspended the thread,
1.285 + or one of the other system wide error codes
1.286 +
1.287 + This marks the current thread for waiting on a Fast Semaphore
1.288 + when exception handling for this thread has completed - see
1.289 + rm_debug_eventhandler.cpp for details.
1.290 + */
1.291 +TInt DTargetProcess::FreezeThread()
1.292 + {
1.293 + // create and store a fast semaphore to stop the thread on
1.294 + NKern::ThreadEnterCS();
1.295 + NFastSemaphore* sem = new NFastSemaphore();
1.296 + NKern::ThreadLeaveCS();
1.297 + sem->iOwningThread = &(Kern::CurrentThread().iNThread);
1.298 + LOG_EVENT_MSG2("DTargetProcess::FreezeThread(): new NFastSemaphore() curr thread=0x%x", sem->iOwningThread);
1.299 + return iFrozenThreadSemaphores.Append(sem);
1.300 + }
1.301 +
1.302 +/**
1.303 + Suspend the specified thread
1.304 +
1.305 + @param aThread thread to suspend
1.306 +
1.307 + @return KErrNone if the thread is successfully suspended,
1.308 + KErrAlreadyExists if the agent has already suspended the thread,
1.309 + or one of the other system wide error codes
1.310 + */
1.311 +TInt DTargetProcess::DoSuspendThread(DThread* aThread)
1.312 + {
1.313 + TUint64 threadId = (TUint64)aThread->iId;
1.314 +
1.315 + // Don't suspend if this thread is already suspended (by FSWait or
1.316 + // Kern::ThreadSuspend
1.317 + if (CheckSuspended(aThread))
1.318 + {
1.319 + // thread was already suspended, don't bother doing it again
1.320 + LOG_MSG2("DTargetProcess::SuspendThread - Thread Id 0x%08x already suspended\n",threadId);
1.321 + return KErrAlreadyExists;
1.322 + }
1.323 +
1.324 + // Add thread to the suspend list
1.325 + TInt err = iSuspendedThreads.Append(threadId);
1.326 + if(err == KErrNone)
1.327 + {
1.328 + Kern::ThreadSuspend(*aThread, 1);
1.329 + }
1.330 + return err;
1.331 + }
1.332 +
1.333 +/**
1.334 + Waits the current thread on a Fast Semaphore.
1.335 +
1.336 + This is useful for situations where the current thread
1.337 + has hit a breakpoint within a critical section, and
1.338 + otherwise could not be suspended at this point.
1.339 +
1.340 + Note that the Fast Semaphore structure on which the thread
1.341 + waits must be a member data item of this class instance,
1.342 + as it needs to be FSSignal()'d by another thread to resume
1.343 + again.
1.344 + */
1.345 +void DTargetProcess::FSWait()
1.346 + {
1.347 + LOG_MSG2("DTargetProcess::NotifyEvent(): number of attached agents: %d", AgentCount());
1.348 + NThread* currentThread = &(Kern::CurrentThread().iNThread);
1.349 + for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
1.350 + {
1.351 + if(iFrozenThreadSemaphores[i]->iOwningThread == currentThread)
1.352 + {
1.353 + LOG_MSG3("DTargetProcess::FSWait(): > FSWait frozen sem %d, curr thread=0x%x", i, currentThread);
1.354 + NKern::FSWait(iFrozenThreadSemaphores[i]);
1.355 + return;
1.356 + }
1.357 + }
1.358 + }
1.359 +
1.360 +/**
1.361 + Checks that the thread has been suspended
1.362 +
1.363 + @param aThread thread to check suspended
1.364 +
1.365 + @return ETrue if the thread has been suspended,
1.366 + EFalse if the thread has not been suspended
1.367 + */
1.368 +TBool DTargetProcess::CheckSuspended(DThread* aThread) const
1.369 + {
1.370 + if(!aThread)
1.371 + {
1.372 + return EFalse;
1.373 + }
1.374 + //check if the thread is in the suspended threads list
1.375 + for(TInt i=0; i<iSuspendedThreads.Count(); i++)
1.376 + {
1.377 + if(iSuspendedThreads[i] == (TUint64)aThread->iId)
1.378 + {
1.379 + return ETrue;
1.380 + }
1.381 + }
1.382 + // not in the suspended threads list so check in the frozen threads list
1.383 + NThread* nThread = &(aThread->iNThread);
1.384 + for(TInt i=0; i<iFrozenThreadSemaphores.Count(); i++)
1.385 + {
1.386 + if(iFrozenThreadSemaphores[i]->iOwningThread == nThread)
1.387 + {
1.388 + return ETrue;
1.389 + }
1.390 + }
1.391 + return EFalse;
1.392 + }
1.393 +
1.394 +/*
1.395 +@return ETrue if the debug driver has suspended any of the process' threads, EFalse otherwise
1.396 +*/
1.397 +TBool DTargetProcess::HasSuspendedThreads() const
1.398 + {
1.399 + return (iSuspendedThreads.Count() > 0) || (iFrozenThreadSemaphores.Count() > 0);
1.400 + }
1.401 +
1.402 +void DTargetProcess::NotifyEvent(const TDriverEventInfo& aEventInfo)
1.403 + {
1.404 + // Stuff the event info into all the tracking agents event queues
1.405 + LOG_MSG2("DTargetProcess::NotifyEvent(): number of attached agents: %d", AgentCount());
1.406 +
1.407 + for(TInt i = 0; i < AgentCount(); i++)
1.408 + {
1.409 + // Index through all the relevant debug agents
1.410 + DDebugAgent* debugAgent = iAgentList[i];
1.411 + if(debugAgent != NULL)
1.412 + {
1.413 + debugAgent->NotifyEvent(aEventInfo);
1.414 + }
1.415 + }
1.416 + }
1.417 +