kernel/eka/drivers/debug/rmdebug/d_process_tracker.cpp
changeset 0 a41df078684a
child 21 e7d2d738d3c2
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Purpose: The DProcessTracker object tracks which processes are being
       
    15 // debugged. The DProcessTracker class uses a DTargetProcess object for
       
    16 // each process being debugged.
       
    17 // Note: Although TheDProcessTracker object is a global, it will be unique
       
    18 // as only the Debug Security Server can load and use rm_debug.ldd.
       
    19 // 
       
    20 //
       
    21 
       
    22 #include <e32def.h>
       
    23 #include <e32def_private.h>
       
    24 #include <e32cmn.h>
       
    25 #include <e32cmn_private.h>
       
    26 #include <kernel/kernel.h>
       
    27 #include <kernel/kern_priv.h>
       
    28 
       
    29 #include <rm_debug_api.h>
       
    30 #include "debug_logging.h"
       
    31 #include "d_process_tracker.h"
       
    32 #include "debug_utils.h"
       
    33 
       
    34 // Global Run-mode debugged process tracking object
       
    35 DProcessTracker TheDProcessTracker;
       
    36 
       
    37 // ctor
       
    38 DProcessTracker::DProcessTracker()
       
    39 	{
       
    40 	}
       
    41 
       
    42 /**
       
    43  * dtor
       
    44  * @internalTechnology
       
    45  */
       
    46 DProcessTracker::~DProcessTracker()
       
    47 	{
       
    48 	// Forget about all the iProcesses
       
    49 	iProcesses.ResetAndDestroy();
       
    50 	}
       
    51 
       
    52 /**
       
    53  * @internalTechnology
       
    54  *
       
    55  * Creates and stores an internal mapping of debug agent to debugged process.
       
    56  * Note that an individual process may be mapped to a number of debug agents.
       
    57  *
       
    58  * @param aProcessName - The fullly qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe
       
    59  * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id()
       
    60  * @return KErrNone if there are no errors. KErrArgument if the processname is too long/short for a valid filepath.
       
    61  *  KErrNoMemory if there is insufficient memory.
       
    62  */
       
    63 TInt DProcessTracker::AttachProcess(const TDesC8& aProcessName,TUint64 aAgentId)
       
    64 	{
       
    65 	LOG_MSG("DProcessTracker::AttachProcess()");
       
    66 
       
    67 	// Valid ProcessName?
       
    68 	if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath)
       
    69 		{
       
    70 		return KErrArgument;
       
    71 		}
       
    72 
       
    73 	// Create an DTargetProcess to store
       
    74 	DTargetProcess* tmpProcess = new DTargetProcess;
       
    75 	if (tmpProcess == 0)
       
    76 		{
       
    77 		return KErrNoMemory;
       
    78 		}
       
    79 
       
    80 	// Set the name
       
    81 	TInt err = KErrNone;
       
    82 	err = tmpProcess->SetProcessName(aProcessName);
       
    83 	if (err != KErrNone)
       
    84 		{
       
    85 		return err;
       
    86 		}
       
    87 
       
    88 	// Is this process being debugged (ie already attached?)
       
    89 	TInt index;
       
    90 	TBool found = EFalse;
       
    91 	for(index=0;index<iProcesses.Count();index++)
       
    92 		{
       
    93 		const TPtr8& tmpPtr8(iProcesses[index]->ProcessName() );
       
    94 
       
    95 		if ( tmpPtr8.CompareF(aProcessName) == 0)
       
    96 			{
       
    97 			found = ETrue;
       
    98 			break;
       
    99 			}
       
   100 		}
       
   101 
       
   102 	if (found)
       
   103 		{
       
   104 		// Yes, it is being debugged
       
   105 
       
   106 		// Add the agent to the list of agents for this process
       
   107 		iProcesses[index]->AddAgent(aAgentId);
       
   108 
       
   109 		return KErrNone;
       
   110 		}
       
   111 	else
       
   112 		{
       
   113 		// No, it is not being debugged
       
   114 			
       
   115 		// Add the agent to the list of agents for this process
       
   116 		tmpProcess->AddAgent(aAgentId);
       
   117 
       
   118 		// Add the process to the list of processes being debugged
       
   119 		return iProcesses.Insert(tmpProcess,0);
       
   120 		}
       
   121 	}
       
   122 
       
   123 /**
       
   124  * @internalTechnology
       
   125  * 
       
   126  * Removes a previously created mapping between a debug agent and a debugged process,
       
   127  * as created by AttachProcess.
       
   128  *
       
   129  * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe
       
   130  * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id()
       
   131  * @return KErrNone if there are no problems. KErrArgument if the processname is too long/short for a valid filepath.
       
   132  * KErrNotFound if the mapping does not exist (and therefore cannot be removed).
       
   133  */
       
   134 TInt DProcessTracker::DetachProcess(const TDesC8& aProcessName, TUint64 aAgentId)
       
   135 	{
       
   136 	// Valid ProcessName?
       
   137 	if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath)
       
   138 		{
       
   139 		return KErrArgument;
       
   140 		};
       
   141 
       
   142 	// Are we debugging this process?
       
   143 	TInt i;
       
   144 	TBool found = EFalse;
       
   145 	DTargetProcess* foundProcess = 0;
       
   146 	for(i=0;i<iProcesses.Count();i++)
       
   147 		{
       
   148 		foundProcess = iProcesses[i];
       
   149 
       
   150 		const TPtr8& tmpPtr8( foundProcess->ProcessName() );
       
   151 
       
   152 		if ( tmpPtr8.CompareF(aProcessName) == 0)
       
   153 			{
       
   154 			found = ETrue;
       
   155 			break;
       
   156 			}
       
   157 		}
       
   158 
       
   159 	if (found == EFalse)
       
   160 		{
       
   161 		return KErrNotFound;
       
   162 		}
       
   163 
       
   164 	// remove the agent from the process
       
   165 	iProcesses[i]->RemoveAgent(aAgentId);
       
   166 
       
   167 	// Found it, are there any more attached agents, or suspended threads in the process?
       
   168 	if ((iProcesses[i]->AgentCount() == 0) && !iProcesses[i]->HasSuspendedThreads() )
       
   169 		{
       
   170 		// Delete the process as no more agents are still attached
       
   171 		delete iProcesses[i];
       
   172 
       
   173 		// Remove the now obsolete pointer from our array.
       
   174 		iProcesses.Remove(i);
       
   175 		}
       
   176 
       
   177 	return KErrNone;
       
   178 	}
       
   179 
       
   180 /**
       
   181  * @internalTechnology
       
   182  *
       
   183  * Detachs a debug agent from every process being debugged. Used when a debug agent is being detached
       
   184  * from the debug security server and has not supplied a specific process name from which to detach.
       
   185  */
       
   186 TInt DProcessTracker::DetachAgent(const TUint64 aAgentId)
       
   187 	{
       
   188 	// Remove this agent from all the processes being tracked.
       
   189 	for(TInt i=0;i<iProcesses.Count();i++)
       
   190 		{
       
   191 		// remove the agent from the process (we don't care about the return code)
       
   192 		iProcesses[i]->RemoveAgent(aAgentId);
       
   193 		}
       
   194 
       
   195 	// Increment down through the array as we then don't have to worry about
       
   196 	// missing entries which have been shifted after deletes.
       
   197 	// The initial value of i correspnds to the index of the final element 
       
   198 	// in the array.
       
   199 	for(TInt i = iProcesses.Count()-1; i>=0; i--)
       
   200 		{
       
   201 		if (iProcesses[i]->AgentCount() == 0)
       
   202 			{
       
   203 			// No agents remain for this process. Delete the
       
   204 			// process object and remove the pointer from the array
       
   205 			delete iProcesses[i];
       
   206 			iProcesses.Remove(i);
       
   207 			}
       
   208 		}
       
   209 	return KErrNone;
       
   210 	}
       
   211 
       
   212 /**
       
   213  * @internalTechnology
       
   214  *
       
   215  * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process
       
   216  * with all the relevant debug agents interested in that process, as determined
       
   217  * by AttachProcess.
       
   218  *
       
   219  * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe
       
   220  * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated
       
   221  * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid.
       
   222  */
       
   223 DTargetProcess* DProcessTracker::FindProcess(const TDesC8& aProcessName)
       
   224 	{
       
   225 	// Valid ProcessName?
       
   226 	if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath)
       
   227 		{
       
   228 		return 0;	// not found
       
   229 		};
       
   230 
       
   231 	// Can we find this in the array?
       
   232 	TInt i;
       
   233 	TBool found = EFalse;
       
   234 	DTargetProcess* foundProcess = 0;
       
   235 	for(i=0;i<iProcesses.Count();i++)
       
   236 		{
       
   237 		foundProcess = iProcesses[i];
       
   238 
       
   239 		const TPtr8& tmpPtr8( foundProcess->ProcessName() );
       
   240 
       
   241 		if ( tmpPtr8.CompareF(aProcessName) == 0)
       
   242 			{
       
   243 			found = ETrue;
       
   244 			break;
       
   245 			}
       
   246 		}
       
   247 
       
   248 	if (found == EFalse)
       
   249 		{
       
   250 		return 0;	// not found
       
   251 		}
       
   252 
       
   253 	return foundProcess;
       
   254 	}
       
   255 
       
   256 /**
       
   257  * @internalTechnology
       
   258  *
       
   259  * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process
       
   260  * with all the relevant debug agents interested in that process, as determined
       
   261  * by AttachProcess.
       
   262  *
       
   263  * Note: This does not attempt an exact match, because the AddProcess event does not provide
       
   264  * a fully-qualified path, it provides something like [t_rmdebug_security0.exe].
       
   265  *
       
   266  * So for the purposes of dealing with this event, we need a "fuzzier" match which does not use the complete
       
   267  * path.
       
   268  *
       
   269  * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe
       
   270  * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated
       
   271  * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid.
       
   272  */
       
   273 DTargetProcess*	DProcessTracker::FuzzyFindProcess(const TDesC8& aProcessName)
       
   274 	{
       
   275 
       
   276 	// Valid ProcessName?
       
   277 	if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath)
       
   278 		{
       
   279 		return 0;	// not found
       
   280 		};
       
   281 
       
   282 	// Can we find this in the array?
       
   283 	TInt i;
       
   284 	TBool found = EFalse;
       
   285 	DTargetProcess* foundProcess = 0;
       
   286 	for(i=0;i<iProcesses.Count();i++)
       
   287 		{
       
   288 		foundProcess = iProcesses[i];
       
   289 
       
   290 		const TPtr8& tmpPtr8( foundProcess->ProcessName() );
       
   291 
       
   292 		if ( tmpPtr8.CompareF(aProcessName) == 0)
       
   293 			{
       
   294 			found = ETrue;
       
   295 			break;
       
   296 			}
       
   297 		else
       
   298 			{
       
   299 			// need to compare centre of this string
       
   300 			//
       
   301 			// e.g. 
       
   302 			//		z:\sys\bin\foobar.exe
       
   303 			// might be seen as:
       
   304 			//		foobar.exe
       
   305 			//
       
   306 			// Algorithm is start at the right side of foundProcess->ProcessName
       
   307 			// move left until we have some backslash, then finish.
       
   308 			TInt right= tmpPtr8.Size() - 1;
       
   309 			TInt left = right;
       
   310 
       
   311 			// search for the rightmost backslash
       
   312 			while(left > 0)
       
   313 				{
       
   314 				if(tmpPtr8[left] == (TUint8)'\\')
       
   315 					break;
       
   316 				
       
   317 				--left;	// move left one character
       
   318 				}
       
   319 			// now we have
       
   320 			// left = index of rightmost backslash in foundProcess->ProcessName()
       
   321 			// right = index of rightmost character in foundProcess->ProcessName()
       
   322 
       
   323 			// We must expect that the size of names matches
       
   324 			TInt foundSize = right - left;	// == sizeof("foobar.exe")
       
   325 			TInt suppliedSize = aProcessName.Size();		
       
   326 
       
   327 			if (foundSize != suppliedSize)
       
   328 				{
       
   329 				// must be something else
       
   330 				break;
       
   331 				}
       
   332 
       
   333 			for(TInt i=0;i< foundSize;i++)
       
   334 				{
       
   335 				if (tmpPtr8[left+i] != aProcessName[1+i])
       
   336 					{
       
   337 					break;
       
   338 					}
       
   339 				}
       
   340 			// All the characters match if we get here
       
   341 			found = ETrue;
       
   342 			}
       
   343 		}
       
   344 
       
   345 	if (found == EFalse)
       
   346 		{
       
   347 		return 0;	// not found
       
   348 		}
       
   349 
       
   350 	return foundProcess;
       
   351 	}
       
   352 
       
   353 TBool DProcessTracker::CheckSuspended(DThread* aTargetThread) const
       
   354 	{
       
   355 	//get the file name and return if NULL
       
   356 	HBuf* name = GetFileName(aTargetThread);
       
   357 	if(!name)
       
   358 		{
       
   359 		return EFalse;
       
   360 		}
       
   361 
       
   362 	//iterate through the processes trying to match the name, and check suspended if found
       
   363 	for(TInt i=0; i<iProcesses.Count(); i++)
       
   364 		{
       
   365 		if(iProcesses[i]->ProcessName().CompareF(*name) == 0)
       
   366 			{
       
   367 			return iProcesses[i]->CheckSuspended(aTargetThread);
       
   368 			}
       
   369 		}
       
   370 
       
   371 	//couldn't find the process so return EFalse
       
   372 	return EFalse;
       
   373 	}
       
   374 
       
   375 TBool DProcessTracker::CheckSuspended(const TUint64 aTargetThreadId) const
       
   376 	{
       
   377 	//get a handle to the thread and return false if it's NULL
       
   378 	DThread* thread = DebugUtils::OpenThreadHandle(aTargetThreadId);
       
   379 	if(!thread)
       
   380 		{
       
   381 		return EFalse;
       
   382 		}
       
   383 
       
   384 	//check if the thread's suspended and then close the thread handle and return
       
   385 	TBool suspended = CheckSuspended(thread);
       
   386 	thread->Close(NULL);
       
   387 	return suspended;
       
   388 	}
       
   389 
       
   390 /**
       
   391   Attempts to suspend the specified thread
       
   392 
       
   393   @param aTargetThread thread to suspend
       
   394 
       
   395   @return KErrNone on success, KErrAlreadyExists if the thread is already suspended,
       
   396   or one of the other system wide error codes
       
   397   */
       
   398 TInt DProcessTracker::SuspendThread(DThread* aTargetThread, TBool aFreezeThread)
       
   399 	{
       
   400 	LOG_MSG3("DProcessTracker::SuspendThread() Requesting suspend for: 0x%08x, freeze thread: %d", aTargetThread->iId, aFreezeThread?1:0);
       
   401 
       
   402 	//get the file name and return if NULL
       
   403 	HBuf* name = GetFileName(aTargetThread);
       
   404 	if(!name)
       
   405 		{
       
   406 		return KErrNotFound;
       
   407 		}
       
   408 
       
   409 	//iterate through the processes trying to match the name, try to suspend the thread if found
       
   410 	for(TInt i=0; i<iProcesses.Count(); i++)
       
   411 		{
       
   412 		if(iProcesses[i]->ProcessName().CompareF(*name) == 0)
       
   413 			{
       
   414 			return iProcesses[i]->SuspendThread(aTargetThread, aFreezeThread);
       
   415 			}
       
   416 		}
       
   417 
       
   418 	//couldn't find process so return error
       
   419 	return KErrPermissionDenied;
       
   420 	}
       
   421 
       
   422 void DProcessTracker::FSWait()
       
   423 	{
       
   424 	for(TInt i=0; i<iProcesses.Count(); i++)
       
   425 		{
       
   426 		iProcesses[i]->FSWait();
       
   427 		}
       
   428 	}
       
   429 
       
   430 /**
       
   431   Attempts to resume the specified thread
       
   432 
       
   433   @param aTargetThread thread to resume
       
   434 
       
   435   @return KErrNone on success, KErrInUse if the thread is not suspended,
       
   436   or one of the other system wide error codes
       
   437   */
       
   438 TInt DProcessTracker::ResumeThread(DThread* aTargetThread)
       
   439 	{
       
   440 	LOG_MSG2("DProcessTracker::ResumeThread() Requesting resume for: 0x%08x", aTargetThread->iId);
       
   441 
       
   442 	//get the file name and return if NULL
       
   443 	HBuf* name = GetFileName(aTargetThread);
       
   444 	if(!name)
       
   445 		{
       
   446 		return KErrNotFound;
       
   447 		}
       
   448 
       
   449 	//iterate through the processes trying to match the name, try to resume the thread if found
       
   450 	for(TInt i=0; i<iProcesses.Count(); i++)
       
   451 		{
       
   452 		if(iProcesses[i]->ProcessName().CompareF(*name) == 0)
       
   453 			{
       
   454 			return iProcesses[i]->ResumeThread(aTargetThread);
       
   455 			}
       
   456 		}
       
   457 
       
   458 	//couldn't find process so return error
       
   459 	return KErrPermissionDenied;
       
   460 	}
       
   461 
       
   462 /**
       
   463   Get a thread's originating file name
       
   464 
       
   465   @param aThread the thread to get the file name for
       
   466 
       
   467   @return a pointer to the thread's file name, if there are problems accessing
       
   468   the file name then NULL will be returned
       
   469   */
       
   470 HBuf* DProcessTracker::GetFileName(DThread* aThread) const
       
   471 	{
       
   472 	//check if the thread is NULL and return if so
       
   473 	if(!aThread)
       
   474 		{
       
   475 		return NULL;
       
   476 		}
       
   477 
       
   478 	//get the owning process and return if it is NULL
       
   479 	DProcess* process = aThread->iOwningProcess;
       
   480 	if(!process)
       
   481 		{
       
   482 		return NULL;
       
   483 		}
       
   484 
       
   485 	//get the process' code seg and return if it is NULL
       
   486 	DCodeSeg* codeSeg = process->iCodeSeg;
       
   487 	if(!codeSeg)
       
   488 		{
       
   489 		return NULL;
       
   490 		}
       
   491 
       
   492 	//return the code seg's stored file name (which could theoretically be NULL)
       
   493 	return codeSeg->iFileName;
       
   494 	}
       
   495