os/kernelhwsrv/kernel/eka/drivers/debug/common/debug_utils.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Purpose: Implementation of static functions for use by debug driver classes
sl@0
    15
//
sl@0
    16
sl@0
    17
#include "debug_logging.h"
sl@0
    18
#include "debug_utils.h"
sl@0
    19
sl@0
    20
/**
sl@0
    21
 * Given a thread ID, return a handle to the corresponding DThread. If the returned
sl@0
    22
 * pointer is non-NULL, it is the responsibility of the caller to close the handle.
sl@0
    23
 * 
sl@0
    24
 * @post if a non-NULL value is returned then a handle to the thread has been
sl@0
    25
 * opened on the callers behalf
sl@0
    26
 * @param aThreadId ID of the thread to return a handle for
sl@0
    27
 * @return a DThread* to the appropriate thread, or NULL if a handle could not be
sl@0
    28
 * opened to the specified thread
sl@0
    29
 */
sl@0
    30
DThread* DebugUtils::OpenThreadHandle(const TUint64 aThreadId)
sl@0
    31
	{
sl@0
    32
	LOG_MSG("DebugUtils::OpenThreadHandle()");
sl@0
    33
sl@0
    34
	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
sl@0
    35
	DObjectCon& threads = *Kern::Containers()[EThread];  // Get containing holding threads
sl@0
    36
	threads.Wait();  // Obtain the container mutex so the list does get changed under us
sl@0
    37
sl@0
    38
	DThread* thread = Kern::ThreadFromId(aThreadId);
sl@0
    39
sl@0
    40
	// Open a handle to the thread so that it doesn't exit while we are processing
sl@0
    41
	if (thread)
sl@0
    42
		{
sl@0
    43
		// if opening a handle fails then set thread to NULL
sl@0
    44
		if(KErrNone != thread->Open())
sl@0
    45
			{
sl@0
    46
			LOG_MSG2("\tCould not open handle to thread %d", (TUint32)aThreadId);
sl@0
    47
			thread = NULL;
sl@0
    48
			}
sl@0
    49
		}
sl@0
    50
	else
sl@0
    51
		{
sl@0
    52
		LOG_MSG2("\tThread with ID %d is NULL", (TUint32)aThreadId);
sl@0
    53
		}
sl@0
    54
sl@0
    55
	threads.Signal();  // Release the container mutex
sl@0
    56
	NKern::ThreadLeaveCS();  // End of critical section
sl@0
    57
sl@0
    58
	return thread;
sl@0
    59
	}
sl@0
    60
sl@0
    61
/**
sl@0
    62
 * Given a process ID, return a handle to the corresponding DProcess. If the returned
sl@0
    63
 * pointer is non-NULL, it is the responsibility of the caller to close the handle.
sl@0
    64
 * 
sl@0
    65
 * @post if a non-NULL value is returned then a handle to the process has been
sl@0
    66
 * opened on the callers behalf
sl@0
    67
 * @param aProcessId ID of the process to return a handle for
sl@0
    68
 * @return a DProcess* to the appropriate process, or NULL if a handle could not be
sl@0
    69
 * opened to the specified process
sl@0
    70
 */
sl@0
    71
DProcess* DebugUtils::OpenProcessHandle(const TUint64 aProcessId)
sl@0
    72
	{
sl@0
    73
	// Commenting out this message as it gets printed out every time a RDebug::Printf statement is caught by the driver,
sl@0
    74
	// which makes looking at the serial cable output irritating. Replaced it with LOG_MSG statements below to indicate if
sl@0
    75
	// something amiss happened. By default then this function prints nothing out.
sl@0
    76
	//LOG_MSG("DebugUtils::OpenProcessHandle()");
sl@0
    77
sl@0
    78
	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
sl@0
    79
	DObjectCon& processes = *Kern::Containers()[EProcess];  // Get containing holding threads
sl@0
    80
	processes.Wait();  // Obtain the container mutex so the list does get changed under us
sl@0
    81
sl@0
    82
	DProcess* process = Kern::ProcessFromId(aProcessId);
sl@0
    83
sl@0
    84
	// Open a handle to the process so that it doesn't exit while we are processing
sl@0
    85
	if (process)
sl@0
    86
		{
sl@0
    87
		// if opening a handle fails then set process to NULL
sl@0
    88
		if(KErrNone != process->Open())
sl@0
    89
			{
sl@0
    90
			LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not open handle for 0x%016lx", aProcessId);
sl@0
    91
			process = NULL;
sl@0
    92
			}
sl@0
    93
		}
sl@0
    94
	else
sl@0
    95
		{
sl@0
    96
		LOG_MSG2("DebugUtils::OpenProcessHandle(): Could not find process for 0x%016lx", aProcessId);
sl@0
    97
		}
sl@0
    98
sl@0
    99
	processes.Signal();  // Release the container mutex
sl@0
   100
	NKern::ThreadLeaveCS();  // End of critical section
sl@0
   101
sl@0
   102
	return process;
sl@0
   103
	}
sl@0
   104