memana/analyzetoolclient/kerneleventhandler/src/analyzetoolchannel.cpp
changeset 0 f0f2b8682603
equal deleted inserted replaced
-1:000000000000 0:f0f2b8682603
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Definitions for the class DAnalyzeToolChannel.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "analyzetoolchannel.h"
       
    21 #include "analyzetooldevice.h"
       
    22 #include "analyzetooleventhandler.h"
       
    23 
       
    24 #include <kernel/kern_priv.h>
       
    25 #ifdef __WINSCW__
       
    26 #include <emulator.h>
       
    27 #endif // __WINSCW__
       
    28 
       
    29 #include "atlog.h"
       
    30 
       
    31 // ================= MEMBER FUNCTIONS =========================================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // DAnalyzeToolChannel::DoCreate()
       
    35 // Creates the logical channel.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 TInt DAnalyzeToolChannel::DoCreate( TInt /*aUnit*/, 
       
    39     const TDesC8* /*aInfo*/, const TVersion &aVer )
       
    40     {
       
    41     LOGSTR1( "ATDD DAnalyzeToolChannel::DoCreate()" );
       
    42 
       
    43     // Check client version.
       
    44     if ( !Kern::QueryVersionSupported( KAnalyzeToolLddVersion(), aVer ) )
       
    45         {
       
    46         return KErrNotSupported;
       
    47         }
       
    48      
       
    49     TInt error = Kern::DynamicDfcQCreate( iOwnDfcQ, 
       
    50                                           KAnalyzeToolThreadPriority, 
       
    51                                           KAnalyzeToolThreadName );
       
    52 
       
    53     if ( KErrNone != error )
       
    54         {
       
    55         return error;
       
    56         }
       
    57 
       
    58     SetDfcQ( iOwnDfcQ );
       
    59     
       
    60     iMsgQ.Receive();
       
    61    
       
    62     // Create the event handler
       
    63     iEventHandler = new DAnalyzeToolEventHandler( iOwnDfcQ );
       
    64 
       
    65     // Check that everything is OK
       
    66     if ( !iEventHandler )
       
    67         {
       
    68         return KErrNoMemory;
       
    69         }
       
    70     
       
    71     // 2nd stage constructor for event handler
       
    72     return iEventHandler->Create( iDevice, Kern::CurrentProcess().iId );
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // DAnalyzeToolChannel::DAnalyzeToolChannel()
       
    77 // Constructor.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 DAnalyzeToolChannel::DAnalyzeToolChannel()
       
    81     {
       
    82     LOGSTR1( "ATDD DAnalyzeToolChannel::DAnalyzeToolChannel()" );
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // DAnalyzeToolChannel::~DAnalyzeToolChannel()
       
    87 // Destructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 DAnalyzeToolChannel::~DAnalyzeToolChannel()
       
    91     {
       
    92     LOGSTR1( "ATDD DAnalyzeToolChannel::~DAnalyzeToolChannel()" );
       
    93     
       
    94     if ( iEventHandler )
       
    95         {
       
    96         // Cancel all processing that we may be doing
       
    97         DoCancel();
       
    98         
       
    99         // Client code should use Close() instead the operator delete 
       
   100         // to destroy the event handler. 
       
   101         TInt error( iEventHandler->Close() );
       
   102         if ( KErrNone != error )
       
   103             {
       
   104             LOGSTR2( "ATDD iEventHandler->Close(%d)", error );
       
   105             }
       
   106         }
       
   107     #ifdef __WINSCW__
       
   108         iCodeSeg.Close();
       
   109     #endif // __WINSCW__
       
   110     
       
   111     // Destroy the queqe
       
   112     if ( iOwnDfcQ )
       
   113         {
       
   114         iOwnDfcQ->Destroy();
       
   115         }
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // DAnalyzeToolChannel::DoControl()
       
   120 // Handles a client request.
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TInt DAnalyzeToolChannel::DoControl( TInt aFunction, 
       
   124                                      TAny* a1, 
       
   125                                      TAny* /*a2*/, 
       
   126                                      TThreadMessage& aMessage )
       
   127     {
       
   128     LOGSTR1( "ATDD DAnalyzeToolChannel::Request()" );
       
   129     
       
   130     TInt ret( KErrNone );
       
   131 
       
   132     // Check the requested function
       
   133     switch (aFunction)
       
   134         {
       
   135         case RAnalyzeTool::EGetProcessInfo:
       
   136             ret = GetProcessInfo( a1, aMessage );
       
   137             break;
       
   138             
       
   139         case RAnalyzeTool::EGetCodesegInfo:
       
   140             ret = GetCodesegInfo( a1, aMessage );
       
   141             break;
       
   142 
       
   143         case RAnalyzeTool::EGetLibraryInfo:
       
   144             ret = GetLibraryInfo( a1, aMessage );
       
   145             break;
       
   146             
       
   147         case RAnalyzeTool::ECancelLibraryEvent:
       
   148             iEventHandler->CancelInformLibraryEvent();
       
   149             break;
       
   150             
       
   151         case RAnalyzeTool::ECurrentClientCount:
       
   152             ret = ClientCount( a1, aMessage );
       
   153             break;
       
   154 
       
   155         case RAnalyzeTool::EMainThreadAlloctor:
       
   156             ret = MainThreadAllocator( a1, aMessage );
       
   157             break;
       
   158         
       
   159         case RAnalyzeTool::EThreadStack:
       
   160              ret = ThreadStack( a1, aMessage );
       
   161              break;
       
   162              
       
   163         case RAnalyzeTool::EGetProcessHandle:
       
   164             ret = GetProcessHandleInfo( a1, aMessage );
       
   165             break;
       
   166         
       
   167         case RAnalyzeTool::EGetCurrentHandles:
       
   168             ret = GetCurrentHandleCount( a1, aMessage );
       
   169             break;
       
   170         case RAnalyzeTool::EGetMemoryModel:
       
   171             ret = GetMemoryModel( a1, aMessage );
       
   172             break;
       
   173             
       
   174         // Unsupported function. Panic
       
   175         default:
       
   176             Kern::PanicCurrentThread( KClientPanic, EPanicUnsupportedRequest );
       
   177             break;
       
   178         }
       
   179         
       
   180     return ret;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // DAnalyzeToolChannel::DoRequest()
       
   185 // Handles a client asynchronous request.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 TInt DAnalyzeToolChannel::DoRequest( TInt aFunction, 
       
   189                                      TRequestStatus* aStatus, 
       
   190                                      TAny* a1, 
       
   191                                      TAny* /*a2*/,
       
   192                                      TThreadMessage& aMessage )
       
   193     {
       
   194     LOGSTR1( "ATDD DAnalyzeToolChannel::DoRequest()" );
       
   195     
       
   196     // Check the requested function
       
   197     switch (aFunction)
       
   198         {
       
   199         case RAnalyzeTool::ELibraryEvent:
       
   200             iEventHandler->InformLibraryEvent( aStatus, a1, aMessage );
       
   201             break;
       
   202             
       
   203         // Unsupported function. Panic
       
   204         default:
       
   205             aMessage.PanicClient( KClientPanic, EPanicUnsupportedRequest );
       
   206             break;
       
   207         }
       
   208         
       
   209     return KErrNone;
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // DAnalyzeToolChannel::DoCancel()
       
   214 // Cancels outstanding asynchronous request.
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 void DAnalyzeToolChannel::DoCancel()
       
   218     {
       
   219     LOGSTR1( "ATDD DAnalyzeToolChannel::DoCancel()" );
       
   220     
       
   221     iEventHandler->CancelInformLibraryEvent();
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // DAnalyzeToolChannel::HandleMsg()
       
   226 // Processes a message for this logical channel.
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void DAnalyzeToolChannel::HandleMsg(TMessageBase* aMsg)
       
   230     {
       
   231     LOGSTR1( "ATDD DAnalyzeToolChannel::HandleMsg()" );
       
   232 
       
   233     TThreadMessage& message = *(TThreadMessage*)aMsg;
       
   234 
       
   235     // Get message type
       
   236     TInt id = message.iValue;
       
   237 
       
   238     // Decode the message type and dispatch it to the relevent handler function...
       
   239     if ( id == (TInt) ECloseMsg )
       
   240         {
       
   241         // Channel Close
       
   242         DoCancel();
       
   243         message.Complete( KErrNone, EFalse );
       
   244         }
       
   245     else if ( id == KMaxTInt )
       
   246         {
       
   247         // DoCancel
       
   248         DoCancel();
       
   249         message.Complete( KErrNone, ETrue );
       
   250         }
       
   251     else if ( id < 0 )
       
   252         {
       
   253         // DoRequest
       
   254         TRequestStatus* status = (TRequestStatus*) message.Ptr0();
       
   255         TInt error = DoRequest( ~id, status, message.Ptr1(), message.Ptr2(), message );
       
   256         if ( KErrNone != error )
       
   257             {
       
   258             Kern::RequestComplete( message.Client(), status, error);
       
   259             }
       
   260         message.Complete(KErrNone, ETrue );
       
   261         }
       
   262     else
       
   263         {
       
   264         // DoControl
       
   265         TInt ret = DoControl( id, message.Ptr0(), message.Ptr1(), message );
       
   266         message.Complete( ret, ETrue );
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // DAnalyzeToolChannel::GetProcessInfo()
       
   272 // Acquires current process information
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 TInt DAnalyzeToolChannel::GetProcessInfo( TAny* aProcessInfo, 
       
   276                                           TThreadMessage& aMessage )
       
   277     {
       
   278     LOGSTR1( "ATDD DAnalyzeToolChannel::GetProcessInfo()" );
       
   279 
       
   280     // Variable for reading parameters from user side
       
   281     TProcessIdentityParamsBuf params;
       
   282     
       
   283     // Reads a descriptor from a thread's process.
       
   284     TInt error = Kern::ThreadDesRead( aMessage.Client(), aProcessInfo, params, 0 );  
       
   285     
       
   286     if ( KErrNone != error )
       
   287         {
       
   288         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   289         return error;
       
   290         } 
       
   291 
       
   292     // Gets the current process
       
   293     Kern::Containers()[ EProcess ]->Wait();
       
   294     DProcess& process = *Kern::ProcessFromId( params().iProcessId );  
       
   295     Kern::Containers()[ EProcess ]->Signal();
       
   296     
       
   297     if ( NULL == &process )
       
   298         {        
       
   299         return KErrNotFound;
       
   300         }
       
   301     
       
   302     // Temporary variable for collecting information from the process
       
   303     TProcessIdentityParamsBuf info;
       
   304     
       
   305     // Collect needed information from the process
       
   306     process.AppendName( info().iProcessName );//lint !e64 !e1514
       
   307     
       
   308     // Gets the current thread
       
   309     Kern::Containers()[ EThread ]->Wait(); 
       
   310     DThread& thread = *Kern::ThreadFromId( params().iThreadId );
       
   311     Kern::Containers()[ EThread ]->Signal();
       
   312    
       
   313     if ( NULL == &thread )
       
   314         {
       
   315         return KErrNotFound;
       
   316         }     
       
   317     
       
   318     // Stack address of the main thread
       
   319     info().iStackAddress = thread.iUserStackRunAddress;
       
   320     info().iStackSize    = thread.iUserStackSize;  
       
   321     
       
   322     // Enters thread critical section and acquires code segment mutex.
       
   323     Kern::AccessCode();
       
   324         
       
   325     // Collect needed information from the process
       
   326     info().iDynamicCount = process.iDynamicCode.Count();
       
   327 
       
   328     // Temporary queue for acquiring the count of codesegments
       
   329     SDblQue queue;
       
   330 
       
   331     // Acquire the count of codesegments
       
   332     TInt codesegCount = process.TraverseCodeSegs( &queue, 
       
   333                                                   NULL, 
       
   334                                                   DCodeSeg::EMarkDebug, 
       
   335                                                   DProcess::ETraverseFlagAdd );
       
   336     
       
   337     #ifndef __WINSCW__
       
   338         info().iCodesegCount = codesegCount;    
       
   339     #else
       
   340     // Reset codesegment array
       
   341     iCodeSeg.Reset();
       
   342     
       
   343     if ( codesegCount > 0 )
       
   344         {
       
   345         SDblQueLink* link = queue.iA.iNext;
       
   346         TCodesegInfo codeinfo;
       
   347         // Iterate through codesegments
       
   348         for ( TInt i = 0; i < codesegCount; ++i, link = link->iNext )
       
   349             {
       
   350             DWin32CodeSeg* codeseg = 
       
   351                 (DWin32CodeSeg*)_LOFF( link, DCodeSeg, iTempLink );
       
   352 
       
   353             // Aqcuire codeseg information
       
   354             codeinfo.iFileEntryPoint = codeseg->iFileEntryPoint;
       
   355             codeinfo.iSize = codeseg->iSize;
       
   356             codeinfo.iFullName.Copy( codeseg->iRootName );
       
   357             codeinfo.iRunAddress = codeseg->iRunAddress;
       
   358             iCodeSeg.Append( codeinfo );
       
   359             }
       
   360         }
       
   361     
       
   362     // Add dependency codesegments
       
   363     DWin32CodeSeg* pcodeSeg = (DWin32CodeSeg*)process.iCodeSeg;
       
   364     
       
   365     // Get dependency codesegments
       
   366     GetModuleDependencies( pcodeSeg->iModuleHandle );
       
   367     
       
   368     // Set codesegment count
       
   369     info().iCodesegCount = iCodeSeg.Count();
       
   370     #endif
       
   371     
       
   372     // Removes all code segments from a queue and clear specified mark(s)
       
   373     DCodeSeg::EmptyQueue( queue, DCodeSeg::EMarkDebug );
       
   374 
       
   375     // Exits thread critical section and releases code segment mutex.
       
   376     Kern::EndAccessCode();
       
   377 
       
   378     // Writes a descriptor to a thread's process.
       
   379     error = Kern::ThreadDesWrite( aMessage.Client(), aProcessInfo, info, 0 );  
       
   380     
       
   381     if ( KErrNone != error )
       
   382         {
       
   383         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   384         return error;
       
   385         } 
       
   386    
       
   387     return KErrNone;
       
   388     }
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // DAnalyzeToolChannel::GetCodesegInfo()
       
   392 // Acquires codeseg information.
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 TInt DAnalyzeToolChannel::GetCodesegInfo( TAny* aCodesegInfo, 
       
   396                                           TThreadMessage& aMessage )
       
   397     {
       
   398     LOGSTR1( "ATDD DAnalyzeToolChannel::GetCodesegInfo()" );
       
   399 
       
   400     // Temporary variable for collecting information from the codeseg
       
   401     TCodesegInfoBuf params;
       
   402 
       
   403     TInt error( KErrArgument );
       
   404     
       
   405     // Reads a descriptor from a thread's process.
       
   406     error = Kern::ThreadDesRead( aMessage.Client(), aCodesegInfo, params, 0 );  
       
   407     
       
   408     if ( KErrNone != error )
       
   409         {
       
   410         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   411         return error;
       
   412         } 
       
   413 
       
   414     if ( params().iIndex < 0 )
       
   415         {
       
   416         return KErrArgument;
       
   417         }
       
   418     
       
   419     // Gets the current process
       
   420     Kern::Containers()[ EProcess ]->Wait();
       
   421     DProcess& process = *Kern::ProcessFromId( params().iProcessId );
       
   422     Kern::Containers()[ EProcess ]->Signal();
       
   423     
       
   424     if ( NULL == &process )
       
   425         {
       
   426         return KErrNotFound;
       
   427         }
       
   428     
       
   429     // Temporary variable for collecting information 
       
   430     TCodesegInfoBuf output;
       
   431 
       
   432     // Enters thread critical section and acquires code segment mutex.
       
   433     Kern::AccessCode();
       
   434 
       
   435     #ifndef __WINSCW__
       
   436     // Temporary queue for acquiring the codesegments
       
   437     SDblQue queue;
       
   438     
       
   439     // Acquire the codesegments
       
   440     TInt actcount = process.TraverseCodeSegs( &queue, 
       
   441                                               NULL, 
       
   442                                               DCodeSeg::EMarkDebug, 
       
   443                                               DProcess::ETraverseFlagAdd );
       
   444     if ( actcount >= params().iIndex )
       
   445         {
       
   446         LOGSTR1( "ATDD DAnalyzeToolChannel::GetCodesegInfo() - actcount >= params.iIndex" );
       
   447         SDblQueLink* link = queue.iA.iNext;
       
   448         
       
   449         // Iterate through codesegments
       
   450         for (TInt i = 0; i < actcount; ++i, link = link->iNext)
       
   451             {
       
   452             DCodeSeg* codeseg = _LOFF( link, DCodeSeg, iTempLink );
       
   453 
       
   454             // Is the codesegments which information client wants
       
   455             if ( i == params().iIndex )
       
   456                 {
       
   457                 // Aqcuire codeseg information
       
   458                 output().iFileEntryPoint = codeseg->iFileEntryPoint;
       
   459                 output().iSize = codeseg->iSize;
       
   460                 output().iFullName.Copy( codeseg->iRootName );
       
   461                 output().iRunAddress = codeseg->iRunAddress;
       
   462                 error = codeseg->GetMemoryInfo( output().iMemoryInfo, &process );
       
   463                 
       
   464                 if ( KErrNone == error )
       
   465                     {
       
   466                     // Writes a descriptor to a thread's process.
       
   467                     error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   468                                                   aCodesegInfo, 
       
   469                                                   output, 
       
   470                                                   0 );   
       
   471                     if ( KErrNone != error )
       
   472                         {
       
   473                         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   474                         } 
       
   475                     }
       
   476                 break;
       
   477                 }
       
   478             }
       
   479         }
       
   480     // Removes all code segments from a queue and clear specified mark(s).
       
   481     DCodeSeg::EmptyQueue( queue, DCodeSeg::EMarkDebug );
       
   482     
       
   483     // Exits thread critical section and releases code segment mutex.
       
   484     Kern::EndAccessCode();
       
   485     
       
   486     return error;
       
   487     #else // WINSCW
       
   488     
       
   489     if ( iCodeSeg.Count() > params().iIndex )
       
   490         {
       
   491         // Aqcuire codeseg information
       
   492         output().iSize = iCodeSeg[params().iIndex].iSize;
       
   493         output().iFullName.Copy( iCodeSeg[params().iIndex].iFullName );
       
   494         output().iRunAddress = iCodeSeg[params().iIndex].iRunAddress;
       
   495         
       
   496         // Writes a descriptor to a thread's process.
       
   497         error = Kern::ThreadDesWrite( aMessage.Client(), aCodesegInfo, output, 0 ); 
       
   498         
       
   499         if ( KErrNone != error )
       
   500             {
       
   501             LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   502             }
       
   503         }
       
   504     
       
   505     // Exits thread critical section and releases code segment mutex.
       
   506     Kern::EndAccessCode();
       
   507     
       
   508     return error;
       
   509     #endif
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // DAnalyzeToolChannel::GetLibraryInfo()
       
   514 // Acquires library information.
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 TInt DAnalyzeToolChannel::GetLibraryInfo( TAny* aLibraryInfo, 
       
   518                                           TThreadMessage& aMessage )
       
   519     {
       
   520     LOGSTR1( "ATDD DAnalyzeToolChannel::GetLibraryInfo()" );
       
   521 
       
   522     // Temporary variable for reading informationfrom the user side
       
   523     TLibraryInfoBuf params;
       
   524 
       
   525     // Reads a descriptor from a thread's process.
       
   526     TInt error = Kern::ThreadDesRead( aMessage.Client(), aLibraryInfo, params, 0 );  
       
   527     
       
   528     if ( KErrNone != error )
       
   529         {
       
   530         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   531         return error;
       
   532         } 
       
   533 
       
   534     if ( params().iIndex < 0 )
       
   535         {
       
   536         return KErrArgument;
       
   537         }
       
   538    
       
   539     // Gets the current process
       
   540     Kern::Containers()[ EProcess ]->Wait();
       
   541     DProcess& process = *Kern::ProcessFromId( params().iProcessId );
       
   542     Kern::Containers()[ EProcess ]->Signal();
       
   543     
       
   544     if ( NULL == &process )
       
   545         {
       
   546         return KErrNotFound;
       
   547         }
       
   548     
       
   549     // Temporary variable for collecting information from the library
       
   550     TLibraryInfoBuf output;
       
   551         
       
   552     // Enters thread critical section and acquires code segment mutex.
       
   553     Kern::AccessCode();
       
   554 
       
   555     // Iterate to find the right library
       
   556     if ( params().iIndex < process.iDynamicCode.Count() )
       
   557         {
       
   558         // Acquire entry to the codeseg
       
   559         SCodeSegEntry entry = process.iDynamicCode[ params().iIndex ];
       
   560         
       
   561         // Acquire library information
       
   562         entry.iLib->AppendName( output().iLibraryName );//lint !e64 !e1514
       
   563         output().iRunAddress = entry.iSeg->iRunAddress;
       
   564         output().iSize = entry.iSeg->iSize;
       
   565         
       
   566         // Writes a descriptor to a thread's process.
       
   567         error = Kern::ThreadDesWrite( aMessage.Client(), aLibraryInfo, output, 0 ); 
       
   568         
       
   569         if ( KErrNone != error )
       
   570             {
       
   571             LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   572             } 
       
   573         
       
   574         // Exits thread critical section and releases code segment mutex.
       
   575         Kern::EndAccessCode();
       
   576         
       
   577         return error;
       
   578         }
       
   579     else
       
   580         {
       
   581         // Exits thread critical section and releases code segment mutex.
       
   582         Kern::EndAccessCode();
       
   583         
       
   584         return KErrArgument;
       
   585         }
       
   586     }
       
   587 
       
   588 // -----------------------------------------------------------------------------
       
   589 // DAnalyzeToolChannel::MainThreadAllocator()
       
   590 // Acquires information about process main thread RAllocator
       
   591 // -----------------------------------------------------------------------------
       
   592 //
       
   593 TInt DAnalyzeToolChannel::MainThreadAllocator( TAny* aMainThreadParams, 
       
   594                                                TThreadMessage& aMessage )
       
   595     {
       
   596     LOGSTR1( "ATDD DAnalyzeToolChannel::MainThreadAllocator()" );
       
   597 
       
   598     // Temporary variable for reading client side parameters
       
   599     TMainThreadParamsBuf params;
       
   600 
       
   601     // Reads a descriptor from a thread's process.
       
   602     TInt error = Kern::ThreadDesRead( aMessage.Client(), 
       
   603                                       aMainThreadParams, 
       
   604                                       params, 
       
   605                                       0 );  
       
   606     
       
   607     if ( KErrNone != error )
       
   608         {
       
   609         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   610         return error;
       
   611         } 
       
   612     
       
   613     // Gets the current process
       
   614     Kern::Containers()[ EProcess ]->Wait();
       
   615     DProcess& process = *Kern::ProcessFromId( params().iProcessId );
       
   616     Kern::Containers()[ EProcess ]->Signal();
       
   617     
       
   618     if ( NULL == &process )
       
   619         {
       
   620         return KErrNotFound;
       
   621         }
       
   622 
       
   623     // Gets the current process
       
   624     Kern::AccessCode();
       
   625 
       
   626     // Temporary variable for collecting information from the RAllocator
       
   627     TMainThreadParamsBuf output;
       
   628 
       
   629     // Aqcuire a reference to the main thread RAllocator
       
   630     output().iAllocator = process.FirstThread()->iAllocator;
       
   631 
       
   632     // Is this only thread in the process
       
   633     output().iAlone = process.iThreadQ.First()->Alone();
       
   634 
       
   635     // Exits thread critical section and releases code segment mutex.
       
   636     Kern::EndAccessCode();
       
   637 
       
   638     // Writes a descriptor to a thread's process.
       
   639     error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   640                                   aMainThreadParams, 
       
   641                                   output, 
       
   642                                   0 ); 
       
   643     
       
   644     if ( KErrNone != error )
       
   645         {
       
   646         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   647         } 
       
   648 
       
   649     return error;
       
   650     }    
       
   651 
       
   652 // -----------------------------------------------------------------------------
       
   653 // DAnalyzeToolChannel::ThreadStack()
       
   654 // Acquires main thread stack address
       
   655 // -----------------------------------------------------------------------------
       
   656 //
       
   657 TInt DAnalyzeToolChannel::ThreadStack( TAny* aThreadStack, 
       
   658                                        TThreadMessage& aMessage )
       
   659     {
       
   660     LOGSTR1( "ATDD DAnalyzeToolChannel::ThreadStack()" );
       
   661 
       
   662     // Temporary variable for reading client side parameters
       
   663     TThreadParamsBuf params;
       
   664 
       
   665     // Reads a descriptor from a thread's process.
       
   666     TInt error = Kern::ThreadDesRead( aMessage.Client(), 
       
   667                                       aThreadStack, 
       
   668                                       params, 
       
   669                                       0 );  
       
   670     
       
   671     if ( KErrNone != error )
       
   672         {
       
   673         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   674         return error;
       
   675         } 
       
   676     
       
   677     // Gets the current process
       
   678     Kern::Containers()[ EThread ]->Wait();
       
   679     DThread& thread = *Kern::ThreadFromId( params().iThreadId );
       
   680     Kern::Containers()[ EThread ]->Signal();
       
   681     
       
   682     if ( NULL == &thread )
       
   683         {
       
   684         return KErrNotFound;
       
   685         }
       
   686     
       
   687     // Gets the current process
       
   688     Kern::AccessCode();
       
   689 
       
   690     // Temporary variable for collecting information from the RAllocator
       
   691     TThreadParamsBuf output;
       
   692 
       
   693     // Stack address of the main thread
       
   694     output().iStackAddress = thread.iUserStackRunAddress;
       
   695     output().iStackSize    = thread.iUserStackSize;
       
   696 
       
   697     // Exits thread critical section and releases code segment mutex.
       
   698     Kern::EndAccessCode();
       
   699 
       
   700     // Writes a descriptor to a thread's process.
       
   701     error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   702                                   aThreadStack, 
       
   703                                   output, 
       
   704                                   0 ); 
       
   705     
       
   706     if ( KErrNone != error )
       
   707         {
       
   708         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   709         } 
       
   710     
       
   711     return error;
       
   712     }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // DAnalyzeToolChannel::GetProcessHandleInfo()
       
   716 // Acquires information about process global handles
       
   717 // -----------------------------------------------------------------------------
       
   718 //
       
   719 TInt DAnalyzeToolChannel::GetProcessHandleInfo( TAny* aProcessHandleInfo,
       
   720                                                 TThreadMessage& aMessage )
       
   721     {
       
   722     LOGSTR1( "ATDD DAnalyzeToolChannel::GetProcessHandleInfo()" );
       
   723 
       
   724     // Temporary variable for collecting information from the codeseg
       
   725     TProcessHandleInfoBuf params;
       
   726 
       
   727     // Reads a descriptor from a thread's process.
       
   728     TInt error = Kern::ThreadDesRead( aMessage.Client(), 
       
   729                                       aProcessHandleInfo, 
       
   730                                       params, 
       
   731                                       0 );  
       
   732     
       
   733     if ( KErrNone != error )
       
   734         {
       
   735         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   736         return error;
       
   737         } 
       
   738 
       
   739     // Gets the current process
       
   740     Kern::Containers()[ EProcess ]->Wait();
       
   741     DProcess& process = *Kern::ProcessFromId( params().iProcessId );
       
   742     Kern::Containers()[ EProcess ]->Signal();
       
   743     
       
   744     if ( NULL == &process )
       
   745         {
       
   746         return KErrNotFound;
       
   747         }
       
   748     
       
   749     // Variable holding wanted information
       
   750     TProcessHandleInfoBuf output;
       
   751 
       
   752     // Enters thread critical section and acquires code segment mutex.
       
   753     Kern::AccessCode();
       
   754 
       
   755     // Get the process thread queue.
       
   756     SDblQue queue = process.iThreadQ;
       
   757     error = KErrNotFound;
       
   758         
       
   759     // Tests whether this doubly linked list is empty.
       
   760     if ( !queue.IsEmpty() )
       
   761         {
       
   762         // Gets a pointer to the first item in this doubly linked list.
       
   763         SDblQueLink* link = queue.First();
       
   764         DThread* thread = _LOFF( link, DThread, iProcessLink );
       
   765 
       
   766         if ( thread )
       
   767             {
       
   768             
       
   769 #ifdef MCL_ROBJECTIX
       
   770             TInt threadHandles( thread->iHandles.ActiveCount() );
       
   771 #else
       
   772             TInt threadHandles( thread->iHandles->ActiveCount() );
       
   773 #endif
       
   774             
       
   775             // Aqcuire thread information
       
   776             //thread->AppendName( output.iThreadName );
       
   777             output().iUserStackRunAddress = thread->iUserStackRunAddress;
       
   778             output().iUserStackSize = thread->iUserStackSize;
       
   779             output().iThreadHandleCount = threadHandles;
       
   780             
       
   781 #ifdef MCL_ROBJECTIX
       
   782             RObjectIx objectIx = process.iHandles;
       
   783             output().iProcessHandleCount = objectIx.ActiveCount();
       
   784 #else
       
   785             DObjectIx* objectIx = process.iHandles;
       
   786             output().iProcessHandleCount = objectIx->ActiveCount();
       
   787 #endif
       
   788                         
       
   789             // Writes a descriptor to a thread's process.
       
   790             error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   791                                           aProcessHandleInfo, 
       
   792                                           output, 
       
   793                                           0 ); 
       
   794             
       
   795             if ( KErrNone != error )
       
   796                 {
       
   797                 LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   798                 } 
       
   799             }
       
   800         }
       
   801 
       
   802     // Exits thread critical section and releases code segment mutex.
       
   803     Kern::EndAccessCode();
       
   804     
       
   805     return error;
       
   806     }
       
   807 
       
   808 // -----------------------------------------------------------------------------
       
   809 // DAnalyzeToolChannel::GetCurrentHandleCount()
       
   810 // Acquires a process's current handle count
       
   811 // -----------------------------------------------------------------------------
       
   812 //
       
   813 TInt DAnalyzeToolChannel::GetCurrentHandleCount( TAny* aProcessHandles,
       
   814                                                  TThreadMessage& aMessage )
       
   815     {
       
   816     LOGSTR1( "ATDD DAnalyzeToolChannel::GetCurrentHandleCount()" );
       
   817 
       
   818     // Temporary variable for collecting information from the codeseg
       
   819     TATProcessHandlesBuf params;
       
   820     
       
   821     // Reads a descriptor from a thread's process.
       
   822     TInt error = Kern::ThreadDesRead( aMessage.Client(), 
       
   823                                       aProcessHandles, 
       
   824                                       params, 
       
   825                                       0 );
       
   826     
       
   827     if ( KErrNone != error )
       
   828         {
       
   829         LOGSTR2( "ATDD ThreadDesRead error %d", error );
       
   830         return error;
       
   831         } 
       
   832     
       
   833     // Gets the current process
       
   834     Kern::Containers()[ EProcess ]->Wait();
       
   835     DProcess* process = Kern::ProcessFromId( params().iProcessId );
       
   836     Kern::Containers()[ EProcess ]->Signal();
       
   837     
       
   838     if ( NULL == process )
       
   839         {
       
   840         return KErrNotFound;
       
   841         }
       
   842 
       
   843     // Variable holding wanted information
       
   844     TATProcessHandlesBuf output;
       
   845     
       
   846     // Enters thread critical section and acquires code segment mutex.
       
   847     Kern::AccessCode();
       
   848     
       
   849     SDblQue queue = process->iThreadQ;
       
   850     SDblQueLink* link = queue.First();
       
   851     TInt threadHandles( 0 );
       
   852     
       
   853     // Iterate through current processes's threads
       
   854     while ( link != queue.Last() )
       
   855         {
       
   856         DThread* thread = _LOFF( link, DThread, iProcessLink );
       
   857         
       
   858 #ifdef MCL_ROBJECTIX
       
   859         threadHandles += thread->iHandles.ActiveCount();
       
   860 #else
       
   861         threadHandles += thread->iHandles->ActiveCount();
       
   862 #endif
       
   863         
       
   864         link = link->iNext;
       
   865         }
       
   866 
       
   867     if ( link == queue.Last() )
       
   868         {
       
   869         DThread* thread = _LOFF( link, DThread, iProcessLink );
       
   870 
       
   871 #ifdef MCL_ROBJECTIX
       
   872         threadHandles += thread->iHandles.ActiveCount();
       
   873 #else
       
   874         threadHandles += thread->iHandles->ActiveCount();
       
   875 #endif
       
   876         }
       
   877     
       
   878     output().iCurrentHandleCount = threadHandles;
       
   879     
       
   880     // Writes a descriptor to a thread's process.
       
   881     error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   882                                   aProcessHandles, 
       
   883                                   output, 
       
   884                                   0 ); 
       
   885     
       
   886     if ( KErrNone != error )
       
   887         {
       
   888         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   889         } 
       
   890     
       
   891     // Exits thread critical section and releases code segment mutex.
       
   892     Kern::EndAccessCode();
       
   893     
       
   894     return error;
       
   895     }
       
   896 
       
   897 // -----------------------------------------------------------------------------
       
   898 // DAnalyzeToolChannel::ClientCount()
       
   899 // Acquires the count of current device driver users.
       
   900 // -----------------------------------------------------------------------------
       
   901 //
       
   902 TInt DAnalyzeToolChannel::ClientCount( TAny* aClientCount,
       
   903                                        TThreadMessage& aMessage )
       
   904     {
       
   905     LOGSTR1( "ATDD DAnalyzeToolChannel::ClientCount()" );
       
   906     
       
   907     // Enters thread critical section and acquires code segment mutex.
       
   908     Kern::AccessCode();
       
   909     
       
   910     // Variable holding wanted information
       
   911     TClientCountBuf output;
       
   912     
       
   913     // Get the number of DLogicalChannelBase objects currently in existence which
       
   914     // have been created from this LDD.
       
   915     output().iClientCount = DLogicalChannelBase::iDevice->iOpenChannels;
       
   916     LOGSTR2( "ATDD > iOpenChannels count: %d", output().iClientCount ); 
       
   917     
       
   918     // Writes a descriptor to a thread's process.
       
   919     TInt error = Kern::ThreadDesWrite( aMessage.Client(), 
       
   920                                        aClientCount, 
       
   921                                        output, 
       
   922                                        0 ); 
       
   923     
       
   924     if ( KErrNone != error )
       
   925         {
       
   926         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
   927         } 
       
   928     
       
   929     // Exits thread critical section and releases code segment mutex.
       
   930     Kern::EndAccessCode();
       
   931     
       
   932     return error;
       
   933     }
       
   934 
       
   935 #ifdef __WINSCW__
       
   936 // -----------------------------------------------------------------------------
       
   937 // DAnalyzeToolChannel::GetModuleDependencies()
       
   938 // Get module dependencies
       
   939 // -----------------------------------------------------------------------------
       
   940 //
       
   941 void DAnalyzeToolChannel::GetModuleDependencies( HMODULE aModule )
       
   942     {
       
   943     LOGSTR1( "ATDD DAnalyzeToolChannel::GetModuleDependencies()" );
       
   944 
       
   945     Emulator::TModule etm( aModule );
       
   946     TUint32 dllSize( 0 );
       
   947     // Temporary variable for collecting information from the codeseg
       
   948     TCodesegInfo info;
       
   949     TBool found( EFalse );
       
   950     
       
   951     const IMAGE_IMPORT_DESCRIPTOR* imports = etm.Imports();
       
   952     while( imports->Characteristics != 0 )
       
   953         {
       
   954         // Reset flag
       
   955         found = EFalse;
       
   956         
       
   957         // Get dll name
       
   958         const TUint8* nameAddr = ( const TUint8* )( imports->Name + ( TInt )etm.iBase );
       
   959         TPtrC8 namePtr( nameAddr );     
       
   960         
       
   961         // Get dll run address
       
   962         Emulator::TModule imp_etm( ( PCSTR )etm.Translate( imports->Name ) );        
       
   963         const TUint8* runAddr = ( const TUint8* )imp_etm.iBase;
       
   964         
       
   965         // Get dll size
       
   966         const IMAGE_NT_HEADERS32* ntHeader = imp_etm.NtHeader();
       
   967         dllSize = ntHeader->OptionalHeader.SizeOfImage;       
       
   968         
       
   969         // Check if DLL already exists in codesegment list
       
   970         for( TInt i = 0; i < iCodeSeg.Count(); i++ )
       
   971             {
       
   972             if ( iCodeSeg[i].iFullName.Compare( namePtr ) == KErrNone )
       
   973                 {
       
   974                 found = ETrue;
       
   975                 break;
       
   976                 }
       
   977             }
       
   978         
       
   979         if ( !found )
       
   980             {
       
   981             info.iSize = dllSize;
       
   982             info.iFullName.Copy( namePtr );
       
   983             info.iRunAddress = (TUint32) runAddr;  
       
   984             // Append codesegment to array
       
   985             iCodeSeg.Append( info );
       
   986             }
       
   987         imports++;
       
   988         }
       
   989     }
       
   990   
       
   991 #endif // __WINSCW__
       
   992 
       
   993 // -----------------------------------------------------------------------------
       
   994 // DAnalyzeToolChannel::GetMemoryModel()
       
   995 // Acquires memory model system uses.
       
   996 // -----------------------------------------------------------------------------
       
   997 //
       
   998 TInt DAnalyzeToolChannel::GetMemoryModel(TAny* aMemoryModel,
       
   999                                         TThreadMessage& aMessage)
       
  1000     {
       
  1001     LOGSTR1( "ATDD DAnalyzeToolChannel::GetMemoryModel()" );
       
  1002    
       
  1003     // Model buffer.
       
  1004     TATMemoryModelBuf model;
       
  1005     // Get current model.
       
  1006     model().iMemoryModel = (TUint32) Kern::HalFunction( EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL );
       
  1007     model().iMemoryModel &= EMemModelTypeMask; // Mask out other stuff.
       
  1008     // Write it to client side.
       
  1009     TInt error = Kern::ThreadDesWrite( aMessage.Client(),
       
  1010                                         aMemoryModel,
       
  1011                                         model,
       
  1012                                         0);
       
  1013     if ( error != KErrNone )
       
  1014         {
       
  1015         LOGSTR2( "ATDD ThreadDesWrite error %d", error );
       
  1016         }
       
  1017     return error;
       
  1018     }
       
  1019 // End of File