debugsrv/runmodedebug/rmdriver/src/debug_utils.cpp
branchRCL_3
changeset 20 ca8a1b6995f6
equal deleted inserted replaced
19:07b41fa8d1dd 20:ca8a1b6995f6
       
     1 // Copyright (c) 2004-2010 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: Implementation of static functions for use by debug driver classes
       
    15 //
       
    16 
       
    17 #include "debug_logging.h"
       
    18 #include "debug_utils.h"
       
    19 
       
    20 /**
       
    21  * Given a thread ID, return a handle to the corresponding DThread. If the returned
       
    22  * pointer is non-NULL, it is the responsibility of the caller to close the handle.
       
    23  * 
       
    24  * @pre caller must be in thread critical section
       
    25  * @post if a non-NULL value is returned then a handle to the thread has been
       
    26  * opened on the callers behalf
       
    27  * @param aThreadId ID of the thread to return a handle for
       
    28  * @return a DThread* to the appropriate thread, or NULL if a handle could not be
       
    29  * opened to the specified thread
       
    30  */
       
    31 DThread* DebugUtils::OpenThreadHandle(TUint64 aThreadId)
       
    32 	{
       
    33 	__ASSERT_CRITICAL;
       
    34 	LOG_MSG2("DebugUtils::OpenThreadHandle(0x%lx)", aThreadId);
       
    35 
       
    36 	DObjectCon& threads = *Kern::Containers()[EThread];  // Get containing holding threads
       
    37 	threads.Wait();  // Obtain the container mutex so the list does get changed under us
       
    38 
       
    39 	DThread* thread = Kern::ThreadFromId(aThreadId);
       
    40 
       
    41 	// Open a handle to the thread so that it doesn't exit while we are processing
       
    42 	if (thread)
       
    43 		{
       
    44 		// if opening a handle fails then set thread to NULL
       
    45 		if(KErrNone != thread->Open())
       
    46 			{
       
    47 			LOG_MSG2("\tCould not open handle to thread %d", (TUint32)aThreadId);
       
    48 			thread = NULL;
       
    49 			}
       
    50 		}
       
    51 	else
       
    52 		{
       
    53 		LOG_MSG2("\tThread with ID %d is NULL", (TUint32)aThreadId);
       
    54 		}
       
    55 
       
    56 	threads.Signal();  // Release the container mutex
       
    57 
       
    58 	return thread;
       
    59 	}
       
    60 
       
    61 /**
       
    62  * Given a process ID, return a handle to the corresponding DProcess. If the returned
       
    63  * pointer is non-NULL, it is the responsibility of the caller to close the handle.
       
    64  * 
       
    65  * @post if a non-NULL value is returned then a handle to the process has been
       
    66  * opened on the callers behalf
       
    67  * @param aProcessId ID of the process to return a handle for
       
    68  * @return a DProcess* to the appropriate process, or NULL if a handle could not be
       
    69  * opened to the specified process
       
    70  */
       
    71 DProcess* DebugUtils::OpenProcessHandle(const TUint64 aProcessId)
       
    72 	{
       
    73 	// Commenting out this message as it gets printed out every time a RDebug::Printf statement is caught by the driver,
       
    74 	// which makes looking at the serial cable output irritating. Replaced it with LOG_MSG statements below to indicate if
       
    75 	// something amiss happened. By default then this function prints nothing out.
       
    76 	//LOG_MSG("DebugUtils::OpenProcessHandle()");
       
    77 
       
    78 	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
       
    79 	DObjectCon& processes = *Kern::Containers()[EProcess];  // Get containing holding threads
       
    80 	processes.Wait();  // Obtain the container mutex so the list does get changed under us
       
    81 
       
    82 	DProcess* process = Kern::ProcessFromId(aProcessId);
       
    83 
       
    84 	// Open a handle to the process so that it doesn't exit while we are processing
       
    85 	if (process)
       
    86 		{
       
    87 		// if opening a handle fails then set process to NULL
       
    88 		if(KErrNone != process->Open())
       
    89 			{
       
    90 			LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not open handle for 0x%lx", aProcessId);
       
    91 			process = NULL;
       
    92 			}
       
    93 		}
       
    94 	else
       
    95 		{
       
    96 		LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not find process for 0x%lx", aProcessId);
       
    97 		}
       
    98 
       
    99 	processes.Signal();  // Release the container mutex
       
   100 	NKern::ThreadLeaveCS();  // End of critical section
       
   101 
       
   102 	return process;
       
   103 	}
       
   104 
       
   105 /**
       
   106  * Opens a reference to the first thread of the given process. Returns NULL if
       
   107  * there are no threads remaining in the process or if the thread couldn't be opened.
       
   108  * 
       
   109  * @pre Caller must be in thread context, in critical section, no fast mutexes held.
       
   110  * @post if result is non-NULL caller is responsible for closing the handle
       
   111  * @param aProcess The process whose first thread is to be opened
       
   112  * @return an Open()ed pointer to the first thread in the process, or NULL.
       
   113  */
       
   114 DThread* DebugUtils::OpenFirstThreadForProcess(DProcess* aProcess)
       
   115 	{
       
   116 	__ASSERT_CRITICAL;
       
   117 	// Copied from memspy's DMemSpyDriverOSAdaptionDProcess::OpenFirstThread()
       
   118 
       
   119 	// It appears that the system lock needs to be held while manipulating the iThreadQ
       
   120 	DThread* result = NULL;
       
   121 	NKern::LockSystem();
       
   122 	// We don't use DProcess::FirstThread() as that doesn't appear to do any checking of whether the list is empty, ie if there are no threads at all
       
   123 	SDblQueLink* threadLink = aProcess->iThreadQ.First();
       
   124 	if (threadLink != NULL && threadLink != &aProcess->iThreadQ.iA)
       
   125 		{
       
   126 		result = _LOFF(threadLink,DThread,iProcessLink);
       
   127 		if (result->Open() != KErrNone)
       
   128 			{
       
   129 			result = NULL;
       
   130 			}
       
   131 		}
       
   132 	NKern::UnlockSystem();
       
   133     return result;
       
   134     }