os/kernelhwsrv/kernel/eka/drivers/debug/rmdebug/rm_debug_eventhandler.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.
     1 // Copyright (c) 2004-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 // Kernel Event handler for Run Mode Debug.
    15 //
    16 
    17 #include <e32def.h>
    18 #include <e32def_private.h>
    19 #include <e32cmn.h>
    20 #include <e32cmn_private.h>
    21 #include <kernel/arm/arm.h>
    22 #include <kernel/kernel.h>
    23 #include <kernel/kern_priv.h>
    24 #include <nk_trace.h>
    25 
    26 #include <rm_debug_api.h>
    27 #include "debug_logging.h"
    28 #include "d_process_tracker.h"
    29 #include "d_rmd_stepping.h"
    30 #include "rm_debug_kerneldriver.h"
    31 #include "rm_debug_driver.h"
    32 #include "rm_debug_eventhandler.h"
    33 
    34 
    35 DRM_DebugEventHandler::DRM_DebugEventHandler()
    36 	:	DKernelEventHandler(EventHandler, this)
    37 {
    38 	LOG_MSG("DRM_DebugEventHandler::DRM_DebugEventHandler()");
    39 
    40 	for(TInt i=0; i<EEventLimit; i++)
    41 		{
    42 		iEventHandlers[i] = &DRM_DebugChannel::HandleUnsupportedEvent;
    43 		}
    44 	iEventHandlers[EEventUserTrace] = &DRM_DebugChannel::HandleUserTrace;
    45 	iEventHandlers[EEventRemoveLibrary] = &DRM_DebugChannel::RemoveLibrary;
    46 	iEventHandlers[EEventAddLibrary] = &DRM_DebugChannel::AddLibrary;
    47 	iEventHandlers[EEventRemoveProcess] = &DRM_DebugChannel::RemoveProcess;
    48 	iEventHandlers[EEventStartThread] = &DRM_DebugChannel::StartThread;
    49 	iEventHandlers[EEventSwExc] = &DRM_DebugChannel::HandleSwException;
    50 	iEventHandlers[EEventHwExc] = &DRM_DebugChannel::HandleHwException;
    51 	iEventHandlers[EEventKillThread] = &DRM_DebugChannel::HandleEventKillThread;
    52 	iEventHandlers[EEventAddProcess] = &DRM_DebugChannel::HandleAddProcessEvent;
    53 	iEventHandlers[EEventRemoveProcess] = &DRM_DebugChannel::HandleRemoveProcessEvent;
    54 }
    55 
    56 TInt DRM_DebugEventHandler::Create(DLogicalDevice* aDevice, DLogicalChannel* aChannel, DThread* aClient)
    57 {
    58 	LOG_MSG("DRM_DebugEventHandler::Create()");
    59 
    60 	TInt err;
    61 	err = aDevice->Open();
    62 	if (err != KErrNone)
    63 		return err;
    64 	iDevice = aDevice;
    65 	
    66 	iChannel = (DRM_DebugChannel*)aChannel; //Don't add ref the channel, since channel closes the event handler before it ever gets destroyed.
    67 
    68 	err = aClient->Open();
    69 	if (err != KErrNone)
    70 		return err;
    71 	iClientThread = aClient;
    72 
    73 	// Use a semaphore to protect our data structures from concurrent access.
    74 	err = Kern::SemaphoreCreate(iLock, _L("RM_DebugEventHandlerLock"), 1 /* Initial count */);
    75 	if (err != KErrNone)
    76 		return err;
    77 
    78 
    79 	return Add();
    80 }
    81 
    82 
    83 DRM_DebugEventHandler::~DRM_DebugEventHandler()
    84 {
    85 	LOG_MSG("DRM_DebugEventHandler::~DRM_DebugEventHandler()");
    86 
    87 	if (iLock)
    88 		iLock->Close(NULL);
    89 	
    90 	if (iDevice)
    91 		iDevice->Close(NULL);	
    92 	
    93 	if (iClientThread)
    94 		Kern::SafeClose((DObject*&)iClientThread, NULL);
    95 		
    96 }
    97 
    98 
    99 TInt DRM_DebugEventHandler::Start()
   100 {
   101 	LOG_MSG("DRM_DebugEventHandler::Start()");
   102 
   103 	iTracking = ETrue;
   104 
   105 	return KErrNone;
   106 }
   107 
   108 
   109 TInt DRM_DebugEventHandler::Stop()
   110 {
   111 	LOG_MSG("DRM_DebugEventHandler::Stop()");
   112 
   113 	iTracking = EFalse;
   114 
   115 	return KErrNone;
   116 }
   117 
   118 
   119 TUint DRM_DebugEventHandler::EventHandler(TKernelEvent aType, TAny* a1, TAny* a2, TAny* aThis)
   120 {
   121 	return ((DRM_DebugEventHandler*)aThis)->HandleEvent(aType, a1, a2);
   122 }
   123 
   124 
   125 
   126 TUint DRM_DebugEventHandler::HandleEvent(TKernelEvent aType, TAny* a1, TAny* a2)
   127 	{
   128 	if((!iTracking) || (aType > (TUint32)EEventLimit))
   129 		{
   130 		return ERunNext;
   131 		}
   132 	return HandleSpecificEvent(aType,a1,a2) ? EExcHandled : ERunNext;
   133 	}
   134 
   135 TBool DRM_DebugEventHandler::HandleSpecificEvent(TKernelEvent aType, TAny* a1, TAny* a2)
   136 	{
   137 	TBool ret = EFalse;
   138 
   139 	NKern::ThreadEnterCS();
   140 	Kern::SemaphoreWait(*iLock);
   141 	
   142 	if (iChannel)
   143 		{
   144 		ret = (iChannel->*(iEventHandlers[aType]))(a1, a2);
   145 		}
   146 
   147 	Kern::SemaphoreSignal(*iLock);
   148 	NKern::ThreadLeaveCS();
   149 
   150 	switch(aType)
   151 		{
   152 		case EEventHwExc:
   153 		case EEventKillThread:
   154 			{
   155 			LOG_MSG2("DRM_DebugEventHandler::HandleEvent() -> FSWait(), kernel event type: %d", (TUint32)aType);
   156 			TheDProcessTracker.FSWait();
   157 			LOG_MSG("DRM_DebugEventHandler::HandleEvent() <- FSWait()");
   158 			break;
   159 			}
   160 		default:
   161 			break;
   162 		}
   163 	return ret;
   164 	}
   165