dbgsrv/coredumpserver/server/src/coretargetobserver.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 // Copyright (c) 2007-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 "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 //
       
    15 
       
    16 
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include "coretargetobserver.h"
       
    24 
       
    25 CTargetObserver::CTargetObserver(RSecuritySvrSession &aSecSession, CCrashHandler &aHandler)
       
    26     : CActive(CActive::EPriorityStandard),
       
    27     iSecSess(aSecSession),
       
    28     iHandler(aHandler),
       
    29     iEventInfoPtr( (TUint8*)&iCrashEventInfo, 0, sizeof(TEventInfo) ) 
       
    30 	{
       
    31 	CActiveScheduler::Add(this);
       
    32 	}
       
    33 
       
    34 CTargetObserver* CTargetObserver::NewL(RSecuritySvrSession &aSecSession, CCrashHandler &aHandler, const TDesC &aProcessName)
       
    35 {
       
    36     LOG_MSG("->CTargetObserver::NewL()\n");
       
    37 	CTargetObserver* self = CTargetObserver::NewLC(aSecSession, aHandler, aProcessName);
       
    38 	CleanupStack::Pop(self);
       
    39     return self;
       
    40 }
       
    41 
       
    42 CTargetObserver* CTargetObserver::NewLC(RSecuritySvrSession &aSession, CCrashHandler &aHandler, const TDesC& aProcessName)
       
    43 	{
       
    44     LOG_MSG("->CTargetObserver::NewLC()\n");
       
    45 	CTargetObserver *self = new (ELeave) CTargetObserver(aSession, aHandler);
       
    46 	CleanupStack::PushL(self);
       
    47 	self->ConstructL(aProcessName);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 void CTargetObserver::ConstructL(const TDesC& aTargetName)
       
    52 	{
       
    53     LOG_MSG("->CTargetObserver::ConstructL()\n");
       
    54 
       
    55 	iTargetName.CreateL(aTargetName.Length());
       
    56 	iTargetName.Copy(aTargetName);
       
    57     
       
    58 	LOG_MSG("CTargetObserver::Observe -> AttachExecutable\n");
       
    59     User::LeaveIfError(iSecSess.AttachExecutable( iTargetName, EFalse));
       
    60 	}
       
    61 
       
    62 void CTargetObserver::SetCrashEventL(TEventType aType, TKernelEventAction aAction)
       
    63 {
       
    64 	LOG_MSG3("->CTargetObserver::SetCrashEventL(type=%d, action=%d)\n", aType, aAction );
       
    65     User::LeaveIfError( iSecSess.SetEventAction( iTargetName, aType, aAction ));
       
    66 }
       
    67 
       
    68 CTargetObserver::~CTargetObserver()
       
    69 	{
       
    70     LOG_MSG("->CTargetObserver::~CTargetObserver()\n");
       
    71 	Cancel();
       
    72 	iTargetName.Close();
       
    73     iThreadList.ResetAndDestroy();
       
    74 	}
       
    75 
       
    76 void CTargetObserver::DoCancel()
       
    77 	{
       
    78 	LOG_MSG( "CTargetObserver::DoCancel -> CancelGetEvent\n");
       
    79     TInt err = iSecSess.CancelGetEvent( iTargetName );
       
    80     if(err != KErrNone)
       
    81         {
       
    82         LOG_MSG2( "CTargetObserver::DoCancel - iSecSess.DetachExecutable returned:%d!", err);
       
    83         //panic client?? close DSS session?? 
       
    84         }
       
    85 
       
    86 	LOG_MSG("CTargetObserver::DoCancel -> iSecSess.DetachExecutable\n");
       
    87     err = iSecSess.DetachExecutable( iTargetName );
       
    88     if(err != KErrNone)
       
    89         {
       
    90         LOG_MSG2( "CTargetObserver::DoCancel iSecSess.DetachExecutable returned:%d!\n", err);
       
    91         //panic client?? close DSS session?? 
       
    92         }
       
    93 	}
       
    94 
       
    95 const TDesC& CTargetObserver::TargetName() const
       
    96 {
       
    97     return iTargetName;
       
    98 }
       
    99 
       
   100 TInt CTargetObserver::ThreadCount() const
       
   101 {
       
   102     return iThreadList.Count();
       
   103 }
       
   104 
       
   105 const TDesC& CTargetObserver::Thread(TInt aIndex) const
       
   106 {
       
   107     LOG_MSG2("CTargetObserver::Thread(%d)\n", aIndex);
       
   108     return *iThreadList[aIndex];
       
   109 }
       
   110 
       
   111 void CTargetObserver::AddThreadL(const TDesC &aThreadName)
       
   112 {
       
   113     LOG_MSG("->CTargetObserver::AddThreadL()\n");
       
   114 
       
   115     if(HasThread(aThreadName))
       
   116     {
       
   117         LOG_MSG("->CTargetObserver::AddThreadL() - already exists!\n");
       
   118         User::Leave(KErrAlreadyExists);
       
   119     }
       
   120     
       
   121     HBufC *thread;
       
   122     thread = HBufC::NewL(aThreadName.Length());
       
   123     *thread = aThreadName;
       
   124     iThreadList.AppendL(thread);
       
   125     LOG_MSG("CTargetObserver::AddThreadL - thread added\n");
       
   126 }
       
   127 
       
   128 TBool CTargetObserver::HasThread(const TDesC &aThreadName) const
       
   129 {
       
   130     LOG_MSG("->CTargetObserver::HasThread()");
       
   131     TInt count = iThreadList.Count();
       
   132     for(TInt i = 0; i < count; ++i)
       
   133     {
       
   134         if(iThreadList[i]->Des() == aThreadName)
       
   135         {
       
   136             LOG_MSG("CTargetObserver::HasThreadL - thread found");
       
   137             return ETrue;
       
   138         }
       
   139     }
       
   140     LOG_MSG("CTargetObserver::HasThreadL - thread not found");
       
   141     return EFalse;
       
   142 }
       
   143 
       
   144 void CTargetObserver::DelThreadL(const TDesC &aThreadName)
       
   145 {
       
   146     LOG_MSG("->CTargetObserver::DelThreadL()");
       
   147 
       
   148     TInt count = iThreadList.Count();
       
   149     for(TInt i = 0; i < count; ++i)
       
   150     {
       
   151         if(iThreadList[i]->Des() == aThreadName)
       
   152         {
       
   153             delete iThreadList[i];
       
   154             iThreadList.Remove(i);
       
   155             LOG_MSG("CTargetObserver::DelThreadL - thread deleted");
       
   156             return;
       
   157         }
       
   158     }
       
   159 
       
   160     LOG_MSG("CTargetObserver::DelThreadL - thread not found!");
       
   161     User::Leave(KErrNotFound);
       
   162 }
       
   163 
       
   164 void CTargetObserver::ClearThreads()
       
   165 {
       
   166     LOG_MSG("->CTargetObserver::ClearThreadsL()\n");
       
   167     iThreadList.ResetAndDestroy();
       
   168 }
       
   169 
       
   170 // RunL() completes a previously issued Observe call 
       
   171 void CTargetObserver::RunL()
       
   172 	{
       
   173     LOG_MSG2("->CTargetObserver::RunL(status:%d)", iStatus.Int());
       
   174 	User::LeaveIfError(iStatus.Int()); //something bad happened
       
   175 
       
   176     iCrashEventInfo.iEventTime.UniversalTime(); //not 100% exact time of the crash, but as soon as we are notified about it
       
   177 
       
   178     Observe(); 
       
   179 
       
   180     RThread thread;
       
   181     LOG_MSG2("CTargetObserver::RunL() - opening handle to crashed thread:%Lu\n", iCrashEventInfo.iThreadId);
       
   182     TInt err = thread.Open(iCrashEventInfo.iThreadId);
       
   183     if(err != KErrNone)
       
   184         {
       
   185         LOG_MSG2("CTargetObserver::RunL - unable to open thread handle! err:%d\n", err); 
       
   186         User::Leave(err);
       
   187         }
       
   188     CleanupClosePushL(thread); 
       
   189     
       
   190     if( (iThreadList.Count() == 0) || (HasThread(thread.FullName())) )
       
   191         {
       
   192         //crash event of the whole process or thread that we observe
       
   193         LOG_MSG("CTargetObserver::RunL() -> HandleCrashEventL()");
       
   194         iHandler.HandleCrashEventL(iCrashEventInfo);
       
   195         }
       
   196     else //crash event of thread that we don't care about
       
   197         {
       
   198         LOG_MSG("CTargetObserver::RunL() - resuming crashed thread");
       
   199         iSecSess.ResumeThread(iCrashEventInfo.iThreadId);
       
   200         }
       
   201     CleanupStack::PopAndDestroy(); //thread
       
   202 
       
   203 	}
       
   204 
       
   205 
       
   206 // Report any leave to the client if possible.
       
   207 TInt CTargetObserver::RunError(TInt aError)
       
   208 	{
       
   209 	return KErrNone;
       
   210 	}
       
   211 
       
   212 void CTargetObserver::Observe()
       
   213 	{
       
   214 	LOG_MSG("->CTargetObserver::Observe()\n");
       
   215 	iSecSess.GetEvent( iTargetName, iStatus, iEventInfoPtr );
       
   216     SetActive(); //wait for crash event
       
   217 	}