os/kernelhwsrv/kernel/eka/drivers/debug/common/debug_utils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/drivers/debug/common/debug_utils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,104 @@
     1.4 +// Copyright (c) 2004-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: Implementation of static functions for use by debug driver classes
    1.18 +//
    1.19 +
    1.20 +#include "debug_logging.h"
    1.21 +#include "debug_utils.h"
    1.22 +
    1.23 +/**
    1.24 + * Given a thread ID, return a handle to the corresponding DThread. If the returned
    1.25 + * pointer is non-NULL, it is the responsibility of the caller to close the handle.
    1.26 + * 
    1.27 + * @post if a non-NULL value is returned then a handle to the thread has been
    1.28 + * opened on the callers behalf
    1.29 + * @param aThreadId ID of the thread to return a handle for
    1.30 + * @return a DThread* to the appropriate thread, or NULL if a handle could not be
    1.31 + * opened to the specified thread
    1.32 + */
    1.33 +DThread* DebugUtils::OpenThreadHandle(const TUint64 aThreadId)
    1.34 +	{
    1.35 +	LOG_MSG("DebugUtils::OpenThreadHandle()");
    1.36 +
    1.37 +	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
    1.38 +	DObjectCon& threads = *Kern::Containers()[EThread];  // Get containing holding threads
    1.39 +	threads.Wait();  // Obtain the container mutex so the list does get changed under us
    1.40 +
    1.41 +	DThread* thread = Kern::ThreadFromId(aThreadId);
    1.42 +
    1.43 +	// Open a handle to the thread so that it doesn't exit while we are processing
    1.44 +	if (thread)
    1.45 +		{
    1.46 +		// if opening a handle fails then set thread to NULL
    1.47 +		if(KErrNone != thread->Open())
    1.48 +			{
    1.49 +			LOG_MSG2("\tCould not open handle to thread %d", (TUint32)aThreadId);
    1.50 +			thread = NULL;
    1.51 +			}
    1.52 +		}
    1.53 +	else
    1.54 +		{
    1.55 +		LOG_MSG2("\tThread with ID %d is NULL", (TUint32)aThreadId);
    1.56 +		}
    1.57 +
    1.58 +	threads.Signal();  // Release the container mutex
    1.59 +	NKern::ThreadLeaveCS();  // End of critical section
    1.60 +
    1.61 +	return thread;
    1.62 +	}
    1.63 +
    1.64 +/**
    1.65 + * Given a process ID, return a handle to the corresponding DProcess. If the returned
    1.66 + * pointer is non-NULL, it is the responsibility of the caller to close the handle.
    1.67 + * 
    1.68 + * @post if a non-NULL value is returned then a handle to the process has been
    1.69 + * opened on the callers behalf
    1.70 + * @param aProcessId ID of the process to return a handle for
    1.71 + * @return a DProcess* to the appropriate process, or NULL if a handle could not be
    1.72 + * opened to the specified process
    1.73 + */
    1.74 +DProcess* DebugUtils::OpenProcessHandle(const TUint64 aProcessId)
    1.75 +	{
    1.76 +	// Commenting out this message as it gets printed out every time a RDebug::Printf statement is caught by the driver,
    1.77 +	// which makes looking at the serial cable output irritating. Replaced it with LOG_MSG statements below to indicate if
    1.78 +	// something amiss happened. By default then this function prints nothing out.
    1.79 +	//LOG_MSG("DebugUtils::OpenProcessHandle()");
    1.80 +
    1.81 +	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
    1.82 +	DObjectCon& processes = *Kern::Containers()[EProcess];  // Get containing holding threads
    1.83 +	processes.Wait();  // Obtain the container mutex so the list does get changed under us
    1.84 +
    1.85 +	DProcess* process = Kern::ProcessFromId(aProcessId);
    1.86 +
    1.87 +	// Open a handle to the process so that it doesn't exit while we are processing
    1.88 +	if (process)
    1.89 +		{
    1.90 +		// if opening a handle fails then set process to NULL
    1.91 +		if(KErrNone != process->Open())
    1.92 +			{
    1.93 +			LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not open handle for 0x%016lx", aProcessId);
    1.94 +			process = NULL;
    1.95 +			}
    1.96 +		}
    1.97 +	else
    1.98 +		{
    1.99 +		LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not find process for 0x%016lx", aProcessId);
   1.100 +		}
   1.101 +
   1.102 +	processes.Signal();  // Release the container mutex
   1.103 +	NKern::ThreadLeaveCS();  // End of critical section
   1.104 +
   1.105 +	return process;
   1.106 +	}
   1.107 +