sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Purpose: The DProcessTracker object tracks which processes are being sl@0: // debugged. The DProcessTracker class uses a DTargetProcess object for sl@0: // each process being debugged. sl@0: // Note: Although TheDProcessTracker object is a global, it will be unique sl@0: // as only the Debug Security Server can load and use rm_debug.ldd. sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include "debug_logging.h" sl@0: #include "d_process_tracker.h" sl@0: #include "debug_utils.h" sl@0: sl@0: // Global Run-mode debugged process tracking object sl@0: DProcessTracker TheDProcessTracker; sl@0: sl@0: // ctor sl@0: DProcessTracker::DProcessTracker() sl@0: { sl@0: } sl@0: sl@0: /** sl@0: * dtor sl@0: * @internalTechnology sl@0: */ sl@0: DProcessTracker::~DProcessTracker() sl@0: { sl@0: // Forget about all the iProcesses sl@0: iProcesses.ResetAndDestroy(); sl@0: } sl@0: sl@0: /** sl@0: * @internalTechnology sl@0: * sl@0: * Creates and stores an internal mapping of debug agent to debugged process. sl@0: * Note that an individual process may be mapped to a number of debug agents. sl@0: * sl@0: * @param aProcessName - The fullly qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe sl@0: * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id() sl@0: * @return KErrNone if there are no errors. KErrArgument if the processname is too long/short for a valid filepath. sl@0: * KErrNoMemory if there is insufficient memory. sl@0: */ sl@0: TInt DProcessTracker::AttachProcess(const TDesC8& aProcessName,TUint64 aAgentId) sl@0: { sl@0: LOG_MSG("DProcessTracker::AttachProcess()"); sl@0: sl@0: // Valid ProcessName? sl@0: if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) sl@0: { sl@0: return KErrArgument; sl@0: } sl@0: sl@0: // Create an DTargetProcess to store sl@0: DTargetProcess* tmpProcess = new DTargetProcess; sl@0: if (tmpProcess == 0) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: // Set the name sl@0: TInt err = KErrNone; sl@0: err = tmpProcess->SetProcessName(aProcessName); sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: sl@0: // Is this process being debugged (ie already attached?) sl@0: TInt index; sl@0: TBool found = EFalse; sl@0: for(index=0;indexProcessName() ); sl@0: sl@0: if ( tmpPtr8.CompareF(aProcessName) == 0) sl@0: { sl@0: found = ETrue; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (found) sl@0: { sl@0: // Yes, it is being debugged sl@0: sl@0: // Add the agent to the list of agents for this process sl@0: iProcesses[index]->AddAgent(aAgentId); sl@0: sl@0: return KErrNone; sl@0: } sl@0: else sl@0: { sl@0: // No, it is not being debugged sl@0: sl@0: // Add the agent to the list of agents for this process sl@0: tmpProcess->AddAgent(aAgentId); sl@0: sl@0: // Add the process to the list of processes being debugged sl@0: return iProcesses.Insert(tmpProcess,0); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * @internalTechnology sl@0: * sl@0: * Removes a previously created mapping between a debug agent and a debugged process, sl@0: * as created by AttachProcess. sl@0: * sl@0: * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe sl@0: * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id() sl@0: * @return KErrNone if there are no problems. KErrArgument if the processname is too long/short for a valid filepath. sl@0: * KErrNotFound if the mapping does not exist (and therefore cannot be removed). sl@0: */ sl@0: TInt DProcessTracker::DetachProcess(const TDesC8& aProcessName, TUint64 aAgentId) sl@0: { sl@0: // Valid ProcessName? sl@0: if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) sl@0: { sl@0: return KErrArgument; sl@0: }; sl@0: sl@0: // Are we debugging this process? sl@0: TInt i; sl@0: TBool found = EFalse; sl@0: DTargetProcess* foundProcess = 0; sl@0: for(i=0;iProcessName() ); sl@0: sl@0: if ( tmpPtr8.CompareF(aProcessName) == 0) sl@0: { sl@0: found = ETrue; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (found == EFalse) sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: sl@0: // remove the agent from the process sl@0: iProcesses[i]->RemoveAgent(aAgentId); sl@0: sl@0: // Found it, are there any more attached agents, or suspended threads in the process? sl@0: if ((iProcesses[i]->AgentCount() == 0) && !iProcesses[i]->HasSuspendedThreads() ) sl@0: { sl@0: // Delete the process as no more agents are still attached sl@0: delete iProcesses[i]; sl@0: sl@0: // Remove the now obsolete pointer from our array. sl@0: iProcesses.Remove(i); sl@0: } sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** sl@0: * @internalTechnology sl@0: * sl@0: * Detachs a debug agent from every process being debugged. Used when a debug agent is being detached sl@0: * from the debug security server and has not supplied a specific process name from which to detach. sl@0: */ sl@0: TInt DProcessTracker::DetachAgent(const TUint64 aAgentId) sl@0: { sl@0: // Remove this agent from all the processes being tracked. sl@0: for(TInt i=0;iRemoveAgent(aAgentId); sl@0: } sl@0: sl@0: // Increment down through the array as we then don't have to worry about sl@0: // missing entries which have been shifted after deletes. sl@0: // The initial value of i correspnds to the index of the final element sl@0: // in the array. sl@0: for(TInt i = iProcesses.Count()-1; i>=0; i--) sl@0: { sl@0: if (iProcesses[i]->AgentCount() == 0) sl@0: { sl@0: // No agents remain for this process. Delete the sl@0: // process object and remove the pointer from the array sl@0: delete iProcesses[i]; sl@0: iProcesses.Remove(i); sl@0: } sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** sl@0: * @internalTechnology sl@0: * sl@0: * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process sl@0: * with all the relevant debug agents interested in that process, as determined sl@0: * by AttachProcess. sl@0: * sl@0: * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe sl@0: * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated sl@0: * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid. sl@0: */ sl@0: DTargetProcess* DProcessTracker::FindProcess(const TDesC8& aProcessName) sl@0: { sl@0: // Valid ProcessName? sl@0: if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) sl@0: { sl@0: return 0; // not found sl@0: }; sl@0: sl@0: // Can we find this in the array? sl@0: TInt i; sl@0: TBool found = EFalse; sl@0: DTargetProcess* foundProcess = 0; sl@0: for(i=0;iProcessName() ); sl@0: sl@0: if ( tmpPtr8.CompareF(aProcessName) == 0) sl@0: { sl@0: found = ETrue; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (found == EFalse) sl@0: { sl@0: return 0; // not found sl@0: } sl@0: sl@0: return foundProcess; sl@0: } sl@0: sl@0: /** sl@0: * @internalTechnology sl@0: * sl@0: * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process sl@0: * with all the relevant debug agents interested in that process, as determined sl@0: * by AttachProcess. sl@0: * sl@0: * Note: This does not attempt an exact match, because the AddProcess event does not provide sl@0: * a fully-qualified path, it provides something like [t_rmdebug_security0.exe]. sl@0: * sl@0: * So for the purposes of dealing with this event, we need a "fuzzier" match which does not use the complete sl@0: * path. sl@0: * sl@0: * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe sl@0: * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated sl@0: * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid. sl@0: */ sl@0: DTargetProcess* DProcessTracker::FuzzyFindProcess(const TDesC8& aProcessName) sl@0: { sl@0: sl@0: // Valid ProcessName? sl@0: if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) sl@0: { sl@0: return 0; // not found sl@0: }; sl@0: sl@0: // Can we find this in the array? sl@0: TInt i; sl@0: TBool found = EFalse; sl@0: DTargetProcess* foundProcess = 0; sl@0: for(i=0;iProcessName() ); sl@0: sl@0: if ( tmpPtr8.CompareF(aProcessName) == 0) sl@0: { sl@0: found = ETrue; sl@0: break; sl@0: } sl@0: else sl@0: { sl@0: // need to compare centre of this string sl@0: // sl@0: // e.g. sl@0: // z:\sys\bin\foobar.exe sl@0: // might be seen as: sl@0: // foobar.exe sl@0: // sl@0: // Algorithm is start at the right side of foundProcess->ProcessName sl@0: // move left until we have some backslash, then finish. sl@0: TInt right= tmpPtr8.Size() - 1; sl@0: TInt left = right; sl@0: sl@0: // search for the rightmost backslash sl@0: while(left > 0) sl@0: { sl@0: if(tmpPtr8[left] == (TUint8)'\\') sl@0: break; sl@0: sl@0: --left; // move left one character sl@0: } sl@0: // now we have sl@0: // left = index of rightmost backslash in foundProcess->ProcessName() sl@0: // right = index of rightmost character in foundProcess->ProcessName() sl@0: sl@0: // We must expect that the size of names matches sl@0: TInt foundSize = right - left; // == sizeof("foobar.exe") sl@0: TInt suppliedSize = aProcessName.Size(); sl@0: sl@0: if (foundSize != suppliedSize) sl@0: { sl@0: // must be something else sl@0: break; sl@0: } sl@0: sl@0: for(TInt i=0;i< foundSize;i++) sl@0: { sl@0: if (tmpPtr8[left+i] != aProcessName[1+i]) sl@0: { sl@0: break; sl@0: } sl@0: } sl@0: // All the characters match if we get here sl@0: found = ETrue; sl@0: } sl@0: } sl@0: sl@0: if (found == EFalse) sl@0: { sl@0: return 0; // not found sl@0: } sl@0: sl@0: return foundProcess; sl@0: } sl@0: sl@0: TBool DProcessTracker::CheckSuspended(DThread* aTargetThread) const sl@0: { sl@0: //get the file name and return if NULL sl@0: HBuf* name = GetFileName(aTargetThread); sl@0: if(!name) sl@0: { sl@0: return EFalse; sl@0: } sl@0: sl@0: //iterate through the processes trying to match the name, and check suspended if found sl@0: for(TInt i=0; iProcessName().CompareF(*name) == 0) sl@0: { sl@0: return iProcesses[i]->CheckSuspended(aTargetThread); sl@0: } sl@0: } sl@0: sl@0: //couldn't find the process so return EFalse sl@0: return EFalse; sl@0: } sl@0: sl@0: TBool DProcessTracker::CheckSuspended(const TUint64 aTargetThreadId) const sl@0: { sl@0: //get a handle to the thread and return false if it's NULL sl@0: DThread* thread = DebugUtils::OpenThreadHandle(aTargetThreadId); sl@0: if(!thread) sl@0: { sl@0: return EFalse; sl@0: } sl@0: sl@0: //check if the thread's suspended and then close the thread handle and return sl@0: TBool suspended = CheckSuspended(thread); sl@0: thread->Close(NULL); sl@0: return suspended; sl@0: } sl@0: sl@0: /** sl@0: Attempts to suspend the specified thread sl@0: sl@0: @param aTargetThread thread to suspend sl@0: sl@0: @return KErrNone on success, KErrAlreadyExists if the thread is already suspended, sl@0: or one of the other system wide error codes sl@0: */ sl@0: TInt DProcessTracker::SuspendThread(DThread* aTargetThread, TBool aFreezeThread) sl@0: { sl@0: LOG_MSG3("DProcessTracker::SuspendThread() Requesting suspend for: 0x%08x, freeze thread: %d", aTargetThread->iId, aFreezeThread?1:0); sl@0: sl@0: //get the file name and return if NULL sl@0: HBuf* name = GetFileName(aTargetThread); sl@0: if(!name) sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: sl@0: //iterate through the processes trying to match the name, try to suspend the thread if found sl@0: for(TInt i=0; iProcessName().CompareF(*name) == 0) sl@0: { sl@0: return iProcesses[i]->SuspendThread(aTargetThread, aFreezeThread); sl@0: } sl@0: } sl@0: sl@0: //couldn't find process so return error sl@0: return KErrPermissionDenied; sl@0: } sl@0: sl@0: void DProcessTracker::FSWait() sl@0: { sl@0: for(TInt i=0; iFSWait(); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Attempts to resume the specified thread sl@0: sl@0: @param aTargetThread thread to resume sl@0: sl@0: @return KErrNone on success, KErrInUse if the thread is not suspended, sl@0: or one of the other system wide error codes sl@0: */ sl@0: TInt DProcessTracker::ResumeThread(DThread* aTargetThread) sl@0: { sl@0: LOG_MSG2("DProcessTracker::ResumeThread() Requesting resume for: 0x%08x", aTargetThread->iId); sl@0: sl@0: //get the file name and return if NULL sl@0: HBuf* name = GetFileName(aTargetThread); sl@0: if(!name) sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: sl@0: //iterate through the processes trying to match the name, try to resume the thread if found sl@0: for(TInt i=0; iProcessName().CompareF(*name) == 0) sl@0: { sl@0: return iProcesses[i]->ResumeThread(aTargetThread); sl@0: } sl@0: } sl@0: sl@0: //couldn't find process so return error sl@0: return KErrPermissionDenied; sl@0: } sl@0: sl@0: /** sl@0: Get a thread's originating file name sl@0: sl@0: @param aThread the thread to get the file name for sl@0: sl@0: @return a pointer to the thread's file name, if there are problems accessing sl@0: the file name then NULL will be returned sl@0: */ sl@0: HBuf* DProcessTracker::GetFileName(DThread* aThread) const sl@0: { sl@0: //check if the thread is NULL and return if so sl@0: if(!aThread) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: //get the owning process and return if it is NULL sl@0: DProcess* process = aThread->iOwningProcess; sl@0: if(!process) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: //get the process' code seg and return if it is NULL sl@0: DCodeSeg* codeSeg = process->iCodeSeg; sl@0: if(!codeSeg) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: //return the code seg's stored file name (which could theoretically be NULL) sl@0: return codeSeg->iFileName; sl@0: } sl@0: