memspy/CommandLine/Source/MemSpyCommandLine.cpp
changeset 0 a03f92240627
child 20 ca8a1b6995f6
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "MemSpyCommandLine.h"
       
    19 
       
    20 // System includes
       
    21 #include <bacline.h>
       
    22 #include <bautils.h>
       
    23 #include <memspyengineclientinterface.h>
       
    24 #include <memspy/engine/memspyenginehelpersysmemtrackerconfig.h>
       
    25 
       
    26 // User includes
       
    27 #include "MemSpyCommands.h"
       
    28 
       
    29 
       
    30 CMemSpyCommandLine::CMemSpyCommandLine()
       
    31     {
       
    32     }
       
    33 
       
    34 
       
    35 CMemSpyCommandLine::~CMemSpyCommandLine()
       
    36     {
       
    37     if ( iMemSpy )
       
    38         {
       
    39         iMemSpy->Close();
       
    40         }
       
    41     delete iMemSpy;
       
    42     iFsSession.Close();
       
    43     }
       
    44 
       
    45 
       
    46 void CMemSpyCommandLine::ConstructL()
       
    47     {
       
    48     User::LeaveIfError( iFsSession.Connect() );
       
    49     iMemSpy = new(ELeave) RMemSpyEngineClientInterface();
       
    50     ConnectToMemSpyL();
       
    51     }
       
    52 
       
    53 
       
    54 CMemSpyCommandLine* CMemSpyCommandLine::NewLC()
       
    55     {
       
    56     CMemSpyCommandLine* self = new(ELeave) CMemSpyCommandLine();
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL();
       
    59     return self;
       
    60     }
       
    61 
       
    62 
       
    63 void CMemSpyCommandLine::PerformBatchL( const TDesC& aFileName )
       
    64     {
       
    65     TInt err = KErrNone;
       
    66     RFile file;
       
    67     err = file.Open( iFsSession, aFileName, EFileRead );
       
    68     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformBatchL() - START - this: 0x%08x, openErr: %d, fileName: %S"), this, err, &aFileName ) );
       
    69     User::LeaveIfError( err );
       
    70 
       
    71     CleanupClosePushL( file );
       
    72     CDesCArray* lines = ReadLinesL( file );
       
    73     CleanupStack::PopAndDestroy( &file );
       
    74     CleanupStack::PushL( lines );
       
    75     
       
    76     const TInt count = lines->Count();
       
    77     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - got %d lines", count ) );
       
    78     iIsBatch = ETrue;
       
    79     for( TInt i=0; i<count; i++ )
       
    80         {
       
    81         const TPtrC pLine( (*lines)[ i ] );
       
    82         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - processing line[%03d] \"%S\""), i, &pLine ) );
       
    83     
       
    84         // Must be at least 3 chars big, i.e. '[' and <command> and then ']'
       
    85         if  ( pLine.Length() <= 2 || pLine[ 0 ] != '[' )
       
    86             {
       
    87             TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - ignoring line: \"%S\""), &pLine ) );
       
    88             }
       
    89         else if  ( pLine[0] == '[' )
       
    90             {
       
    91             // Try to find end of command...
       
    92             const TInt posOfClosingArgChar = pLine.Locate( ']' );
       
    93             if  ( posOfClosingArgChar >= 2 )
       
    94                 {
       
    95                 // Get command
       
    96                 const TPtrC pCommand( pLine.Mid( 1, posOfClosingArgChar - 1 ) );
       
    97                 TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - got command: %S"), &pCommand ) );
       
    98 
       
    99                 // Next, try to get any args
       
   100                 CDesCArrayFlat* args = new(ELeave) CDesCArrayFlat( 2 );
       
   101                 CleanupStack::PushL( args );
       
   102 
       
   103                 // There must be a mandatory space between closing ] and start of args...
       
   104                 // E.g.:
       
   105                 //
       
   106                 //  [CMD] ARG
       
   107                 //
       
   108                 const TInt remainderLength = pLine.Length() - posOfClosingArgChar;
       
   109                 if  ( remainderLength > 1 )
       
   110                     {
       
   111                     const TPtrC remainder( pLine.Mid( posOfClosingArgChar + 1 ) );
       
   112                     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - got remainder: %S"), &pLine ) );
       
   113 
       
   114                     // Extract arguments separated by tabs or space characters
       
   115                     // and store in arguments array
       
   116                     HBufC* argText = HBufC::NewLC( pLine.Length() + 1 );
       
   117                     TPtr pArgText( argText->Des() );
       
   118                     for( TInt j=0; j<remainder.Length(); j++ )
       
   119                         {
       
   120                         const TChar c( remainder[ j ] );
       
   121                         //
       
   122                         if  ( c == '\t' || c == ' ' )
       
   123                             {
       
   124                             pArgText.Trim();
       
   125                             if  ( pArgText.Length() )
       
   126                                 {
       
   127                                 TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - arg[%02d] %S"), args->Count(), &pArgText ) );
       
   128                                 args->AppendL( pArgText );
       
   129                                 pArgText.Zero();
       
   130                                 }
       
   131                             }
       
   132                         else
       
   133                             {
       
   134                             pArgText.Append( c );
       
   135                             }
       
   136                         }
       
   137 
       
   138                     // Save leftovers...
       
   139                     pArgText.Trim();
       
   140                     if  ( pArgText.Length() )
       
   141                         {
       
   142                         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - arg[%02d] %S"), args->Count(), &pArgText ) );
       
   143                         args->AppendL( pArgText );
       
   144                         }
       
   145 
       
   146                     CleanupStack::PopAndDestroy( argText );
       
   147                     }
       
   148 
       
   149                 // Now we can perform the operation!
       
   150                 PerformSingleOpL( pCommand, *args );
       
   151 
       
   152                 CleanupStack::PopAndDestroy( args );
       
   153                 }
       
   154             }
       
   155 
       
   156         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - processing line: \"%S\""), &pLine ) );
       
   157         }
       
   158 
       
   159     iIsBatch = EFalse;
       
   160     
       
   161     CleanupStack::PopAndDestroy( lines );
       
   162     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformBatchL() - END - this: 0x%08x, fileName: %S"), this, &aFileName ) );
       
   163     }
       
   164 
       
   165 
       
   166 void CMemSpyCommandLine::PerformOpL( const CCommandLineArguments& aCommandLine )
       
   167     {
       
   168     const TInt count = aCommandLine.Count();
       
   169     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - START - arg count: %d, this: 0x%08x", count, this ) );
       
   170 
       
   171     if  ( count >= 1 )
       
   172         {
       
   173         // Get main command
       
   174         TBuf<KMemSpyCommandLineMaxLength> command;
       
   175         command.Copy( aCommandLine.Arg( 1 ) );
       
   176         command.UpperCase();
       
   177 
       
   178         // Extract arguments into array
       
   179         CDesCArrayFlat* args = new(ELeave) CDesCArrayFlat(4);
       
   180         CleanupStack::PushL( args );
       
   181         for( TInt i=2; i<count; i++ )
       
   182             {
       
   183             args->AppendL( aCommandLine.Arg( i ) );
       
   184             }
       
   185 
       
   186         // Perform op
       
   187         PerformSingleOpL( command, *args );
       
   188 
       
   189         // Tidy up
       
   190         CleanupStack::PopAndDestroy( args );
       
   191         }
       
   192     else
       
   193         {
       
   194         User::Leave( KErrUnderflow );
       
   195         }
       
   196     }
       
   197 
       
   198 
       
   199 void CMemSpyCommandLine::PerformSingleOpL( const TDesC& aCommand, const CDesCArray& aParameters )
       
   200     {
       
   201     // Record duration
       
   202     TTime timeStart; 
       
   203     timeStart.HomeTime();
       
   204     const TInt paramCount = aParameters.Count();
       
   205 
       
   206 #ifdef _DEBUG
       
   207     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - START - command: %S, paramCount: %d, this: 0x%08x"), &aCommand, paramCount, this ) );
       
   208     for( TInt i=0; i<paramCount; i++ )
       
   209         {
       
   210         const TPtrC pParam( aParameters[ i ] );
       
   211         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - param[%02d] = [%S]"), i, &pParam ) );
       
   212         }
       
   213 #else
       
   214     RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d}..."), &aCommand, paramCount );
       
   215 #endif
       
   216 
       
   217     TFileName batchFile;
       
   218     batchFile.Append( aCommand );
       
   219     
       
   220     TInt err = KErrNotSupported;
       
   221     if  ( aCommand.CompareF( KMemSpyCmdSWMTForceUpdate ) == 0 )
       
   222         {
       
   223         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - SWMT_ForceUpdate", this ) );
       
   224         if ( paramCount > 0 )
       
   225             {
       
   226             TInt categories( 0 );
       
   227             TName threadNameFilter;
       
   228             TRAP( err, ParseSWMTParametersL( aParameters, categories, threadNameFilter ) );            
       
   229             if ( !err )
       
   230                 {
       
   231                 err = iMemSpy->SystemWideMemoryTrackerCategoriesSet( categories );
       
   232                 if ( !err && threadNameFilter.Length() > 0 )
       
   233                     {
       
   234                     err = iMemSpy->SystemWideMemoryTrackerThreadFilterSet( threadNameFilter );
       
   235                     }
       
   236                 }
       
   237             }
       
   238         if ( !err )
       
   239             {
       
   240             err = iMemSpy->PerformOperation( EMemSpyClientServerOpSystemWideMemoryTrackingForceUpdate );
       
   241             }
       
   242         }
       
   243     else if ( aCommand.CompareF( KMemSpyCmdSWMTReset ) == 0 )
       
   244         {
       
   245         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - SWMT_Reset", this ) );
       
   246         err = iMemSpy->PerformOperation( EMemSpyClientServerOpSystemWideMemoryTrackingReset );
       
   247         }
       
   248     else if ( aCommand.CompareF( KMemSpyCmdHeapDumpKernel ) == 0 )
       
   249         {
       
   250         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_DumpKernel", this ) );
       
   251         err = iMemSpy->PerformOperation( EMemSpyClientServerOpHeapData, KMemSpyClientServerThreadIdKernel );
       
   252         }
       
   253     else if ( aCommand.CompareF( KMemSpyCmdHeapCompact ) == 0 )
       
   254         {
       
   255         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_Compact", this ) );
       
   256         err = iMemSpy->PerformOperation( EMemSpyClientServerOpHeapInfoCompact );
       
   257         }
       
   258     else if ( aCommand.CompareF( KMemSpyCmdContainer ) == 0 )
       
   259         {
       
   260         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Container", this ) );
       
   261         err = iMemSpy->PerformOperation( EMemSpyClientServerOpEnumerateKernelContainerAll );
       
   262         }
       
   263     else if ( aCommand.CompareF( KMemSpyCmdBitmapsSave ) == 0 )
       
   264         {
       
   265         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Bitmaps_Save", this ) );
       
   266         err = iMemSpy->SaveAllBitmaps();
       
   267         }
       
   268     else if ( aCommand.CompareF( KMemSpyCmdRamDisableAknIconCache ) == 0 )
       
   269         {
       
   270         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Ram_DisableAknIconCache", this ) );
       
   271         err = iMemSpy->DisableAknIconCache();
       
   272         }
       
   273     else if ( aCommand.CompareF( KMemSpyCmdOutputToFile ) == 0 )
       
   274         {
       
   275         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Output_ToFile", this ) );
       
   276         err = iMemSpy->SwitchOutputModeFile();
       
   277         }
       
   278     else if ( aCommand.CompareF( KMemSpyCmdOutputToTrace ) == 0 )
       
   279         {
       
   280         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Output_ToTrace", this ) );
       
   281         err = iMemSpy->SwitchOutputModeTrace();
       
   282         }
       
   283     else if ( aCommand.CompareF( KMemSpyCmdUiSendToBackground ) == 0 )
       
   284         {
       
   285         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - UI_Background", this ) );
       
   286         err = iMemSpy->SendToBackground();
       
   287         }
       
   288     else if ( aCommand.CompareF( KMemSpyCmdUiBringToForeground ) == 0 )
       
   289         {
       
   290         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - UI_Foreground", this ) );
       
   291         err = iMemSpy->BringToForeground();
       
   292         }
       
   293     else if ( aCommand.CompareF( KMemSpyCmdUiExit ) == 0 )
       
   294         {
       
   295         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - UI_Exit", this ) );
       
   296         err = iMemSpy->Exit();
       
   297         }
       
   298     else if ( aCommand.CompareF( KMemSpyCmdHeapDump ) == 0 )
       
   299         {
       
   300         if  ( paramCount == 0 )
       
   301             {
       
   302             // Dump heap data for all threads
       
   303             TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_Dump (all threads)", this ) );
       
   304             err = iMemSpy->PerformOperation( EMemSpyClientServerOpHeapData );
       
   305             }
       
   306         else if ( paramCount >= 1 )
       
   307             {
       
   308             // Dump heap data for named thread
       
   309             const TPtrC pThreadName( aParameters[ 0 ] );
       
   310             TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_Dump (%S)"), this, &pThreadName ) );
       
   311             err = iMemSpy->PerformOperation( EMemSpyClientServerOpHeapData, pThreadName );
       
   312             }
       
   313         }
       
   314     else if ( aCommand.CompareF( KMemSpyCmdOpenFile ) == 0 )
       
   315         {
       
   316         if  ( paramCount == 0 )
       
   317             {
       
   318             // Dump heap data for all threads
       
   319             TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - OpenFile (all threads)", this ) );
       
   320             err = iMemSpy->PerformOperation( EMemSpyClientServerOpOpenFiles );
       
   321             }
       
   322         else if ( paramCount >= 1 )
       
   323             {
       
   324             // Dump heap data for named thread
       
   325             const TPtrC pThreadName( aParameters[ 0 ] );
       
   326             TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - OpenFile (%S)"), this, &pThreadName ) );
       
   327             err = iMemSpy->PerformOperation( EMemSpyClientServerOpOpenFiles, pThreadName );
       
   328             }
       
   329         }
       
   330     else if ( !iIsBatch && FindBatchFile( batchFile ) == KErrNone )
       
   331         {
       
   332         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Batch file: %S"), this, &batchFile ) );
       
   333         PerformBatchL( batchFile );
       
   334         }
       
   335     else
       
   336         {
       
   337         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Unsupported Command: %S"), this, &aCommand ) );
       
   338         }
       
   339 
       
   340     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - END - err: %d, this: 0x%08x, cmd: %S" ), err, this, &aCommand ) );
       
   341 
       
   342     // Calculate duration
       
   343     TTime timeEnd;
       
   344     timeEnd.HomeTime();
       
   345     TTimeIntervalSeconds interval( 0 );
       
   346     timeEnd.SecondsFrom( timeStart, interval );
       
   347 
       
   348     // Print some info
       
   349     if  ( err != KErrNone )
       
   350         {
       
   351         RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d} {%08d sec} => ERROR: %d"), &aCommand, paramCount, interval.Int(), err );
       
   352         }
       
   353     else
       
   354         {
       
   355         RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d} {%08d sec} => OK"), &aCommand, paramCount, interval.Int() );
       
   356         }
       
   357 
       
   358     // Spacer
       
   359     RDebug::Printf( " " );
       
   360     }
       
   361 
       
   362 
       
   363 void CMemSpyCommandLine::ConnectToMemSpyL()
       
   364     {
       
   365     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - START - this: 0x%08x", this ) );
       
   366 
       
   367     TInt err = iMemSpy->Connect();
       
   368     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - connect #1 err: %d, this: 0x%08x", err, this ) );
       
   369 
       
   370     if  ( err == KErrNotFound )
       
   371         {
       
   372         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - launching MemSpy... - this: 0x%08x", this ) );
       
   373         LaunchMemSpyL();
       
   374 
       
   375         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - launched MemSpy - this: 0x%08x", this ) );
       
   376         err = iMemSpy->Connect();
       
   377 
       
   378         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - connect #2 err: %d, this: 0x%08x", err, this ) );
       
   379         }
       
   380 
       
   381     User::LeaveIfError( err );
       
   382 
       
   383     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - END - ok - this: 0x%08x", this ) );
       
   384     }
       
   385 
       
   386 
       
   387 void CMemSpyCommandLine::LaunchMemSpyL()
       
   388     {
       
   389     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - START - this: 0x%08x", this ) );
       
   390 
       
   391     TInt err = KErrGeneral;
       
   392     RProcess proc;
       
   393 
       
   394     // First try with s60 UI
       
   395     err = proc.Create( KMemSpyProcessName1, KNullDesC );
       
   396     if  ( err == KErrNone )
       
   397         {
       
   398         TFullName fullName;
       
   399         proc.FullName( fullName );
       
   400         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Create S60 UI process successfully... - this: 0x%08x, name: %S"), this, &fullName ) );
       
   401 
       
   402         TRequestStatus status;
       
   403         proc.Rendezvous( status );
       
   404         proc.Resume();
       
   405 
       
   406         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - MemSpy resumed, waiting for Rendezvous... - this: 0x%08x", this ) );
       
   407         User::WaitForRequest( status );
       
   408         err = status.Int();
       
   409         proc.Close();
       
   410 
       
   411         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Rendezvous complete: %d, this: 0x%08x", err, this ) );
       
   412         }
       
   413     if  ( err != KErrNone )
       
   414         {
       
   415         // Try console UI
       
   416         err = proc.Create( KMemSpyProcessName2, KNullDesC );
       
   417         if  ( err == KErrNone )
       
   418             {
       
   419             TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Create Console UI process successfully... - this: 0x%08x", this ) );
       
   420 
       
   421             TRequestStatus status;
       
   422             proc.Rendezvous( status );
       
   423             proc.Resume();
       
   424 
       
   425             TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - MemSpy resumed, waiting for Rendezvous... - this: 0x%08x", this ) );
       
   426 
       
   427             User::WaitForRequest( status );
       
   428             err = status.Int();
       
   429             proc.Close();
       
   430 
       
   431             TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Rendezvous complete: %d, this: 0x%08x", err, this ) );
       
   432             }
       
   433         }
       
   434 
       
   435     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - final error: %d, this: 0x%08x", err, this ) );
       
   436     User::LeaveIfError( err );
       
   437     User::After( 10 * 1000000 );
       
   438 
       
   439     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - END - ok - this: 0x%08x", this ) );
       
   440     }
       
   441 
       
   442 
       
   443 CDesCArray* CMemSpyCommandLine::ReadLinesL( RFile& aFile )
       
   444     {
       
   445     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - START - this: 0x%08x", this ) );
       
   446     CDesCArrayFlat* lines = new(ELeave) CDesCArrayFlat( 10 );
       
   447     CleanupStack::PushL( lines );
       
   448 
       
   449     TInt size = 0;
       
   450     User::LeaveIfError( aFile.Size( size ) );
       
   451     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x, file size: %d", this, size ) );
       
   452 
       
   453     // Read file
       
   454     HBufC8* narrowBuf = HBufC8::NewLC( size );
       
   455     TPtr8 pBufNarrow( narrowBuf->Des() );
       
   456     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x, reading file..." ) );
       
   457     User::LeaveIfError( aFile.Read( pBufNarrow ) );
       
   458     if  ( pBufNarrow.Length() <= 2 )
       
   459         {
       
   460         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - BAD FILE LENGTH", this ) );
       
   461         User::Leave( KErrCorrupt );
       
   462         }
       
   463 
       
   464     // Look for BOM and convert to unicode
       
   465     HBufC* unicodeText = HBufC::NewL( size );
       
   466     TPtr pUnicodeText( unicodeText->Des() );
       
   467     if  ( pBufNarrow[0] == 0xFF && pBufNarrow[1] == 0xFE )
       
   468         {
       
   469         // It's unicode... and we don't want the BOM, hence -2
       
   470         const TInt textLength = size - 2;
       
   471         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - UNICODE TEXT - textLength: %d", this, textLength / 2 ) );
       
   472         Mem::Copy( (TUint8 *)pUnicodeText.Ptr(), pBufNarrow.Ptr() + 2, textLength );
       
   473         pUnicodeText.SetLength( textLength / 2 );
       
   474         }
       
   475     else
       
   476         {
       
   477         // It's ASCII, convert it to unicode...
       
   478         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - ASCII TEXT - textLength: %d", this, size ) );
       
   479         for (TInt i = 0; i<size; i++ )
       
   480             {
       
   481             pUnicodeText.Append( pBufNarrow[ i ] );
       
   482             }
       
   483         }
       
   484 
       
   485     // Discard old narrow text
       
   486     CleanupStack::PopAndDestroy( narrowBuf );
       
   487     CleanupStack::PushL( unicodeText );
       
   488 
       
   489     // Try to extract lines of text...
       
   490     HBufC* line = HBufC::NewLC( 1024 );
       
   491     TPtr pLine( line->Des() );
       
   492     const TInt length = unicodeText->Length();
       
   493     for( TInt i=0; i<length; i++ )
       
   494         {
       
   495         const TChar c( pUnicodeText[ i ] );
       
   496         //
       
   497         if  ( c == '\r' || c == '\n' )
       
   498             {
       
   499             pLine.Trim();
       
   500             if  ( pLine.Length() )
       
   501                 {
       
   502                 TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - LINE[%03d] %S"), this, lines->Count(), line ) );
       
   503                 lines->AppendL( pLine );
       
   504                 }
       
   505 
       
   506             pLine.Zero();
       
   507             }
       
   508         else
       
   509             {
       
   510             pLine.Append( c );
       
   511             }
       
   512         }
       
   513 
       
   514     // Save last line, just in cae it didn't end with a CR/LF
       
   515     pLine.Trim();
       
   516     if ( pLine.Length() )
       
   517         {
       
   518         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - LINE[%03d] %S"), this, lines->Count(), line ) );
       
   519         lines->AppendL( pLine );
       
   520         }
       
   521 
       
   522     CleanupStack::PopAndDestroy( 2, unicodeText ); // line & unicodeText
       
   523     CleanupStack::Pop( lines );
       
   524     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - END - this: 0x%08x", this ) );
       
   525 
       
   526     return lines;
       
   527     }
       
   528 
       
   529 
       
   530 void CMemSpyCommandLine::ParseSWMTParametersL( const CDesCArray& aParameters, TInt& aCategories, TDes& aFilter )
       
   531     {
       
   532     TInt result(0);
       
   533     // Check if the first parameter is a number.
       
   534     // In that case other parameters are ignored.
       
   535     TLex lex( aParameters[ 0 ] );
       
   536     if ( lex.Val( result ) != KErrNone )
       
   537         {
       
   538         // Parameters were given in text form:
       
   539         const TInt count( aParameters.Count() );
       
   540         for ( TInt i = 0; i < count ; i++ )
       
   541             {
       
   542             if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeHeap ) == 0 )
       
   543                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserHeap |
       
   544                           TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHeap;
       
   545             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeChunk ) == 0 )
       
   546                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryLocalChunks |
       
   547                           TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalChunks;
       
   548             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeCode ) == 0 )
       
   549                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMLoadedCode;
       
   550             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeStack ) == 0 )
       
   551                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserStacks;
       
   552             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeGlobalData ) == 0 )
       
   553                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalData;
       
   554             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeRamDrive ) == 0 )
       
   555                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMDrive;
       
   556             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeOpenFile ) == 0 )
       
   557                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryOpenFiles;
       
   558             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeDiskSpace ) == 0 )
       
   559                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryDiskusage;
       
   560             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeFbserv ) == 0 ) // enables both FABS and BITM 
       
   561                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryBitmapHandles;
       
   562             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeHandleGeneric ) == 0 ) // enables both HGEN and HPAS
       
   563                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHandles;
       
   564             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeFileServerCache ) == 0 )
       
   565                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryFileServerCache;
       
   566             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeSystemMemory ) == 0 )
       
   567                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategorySystemMemory;
       
   568             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeWindowGroup ) == 0 )
       
   569                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryWindowGroups;
       
   570             else if ( aParameters[i].Find( KMemSpyCmdSWMTTypeHeapFilter ) == 0 )
       
   571                 {
       
   572                 aFilter.Copy( aParameters[i].Right( aParameters[i].Length() -11 ) );
       
   573                 }
       
   574             else
       
   575                 User::Leave( KErrNotSupported );
       
   576             }
       
   577         }
       
   578     else if ( aParameters.Count() > 1 && aParameters[1].Find( KMemSpyCmdSWMTTypeHeapFilter ) == 0 )
       
   579         {
       
   580         aFilter.Copy( aParameters[1].Right( aParameters[1].Length() -11 ) );
       
   581         }
       
   582     aCategories = result;
       
   583     }
       
   584 
       
   585 
       
   586 TInt CMemSpyCommandLine::FindBatchFile( TDes &aFileName )
       
   587     {
       
   588     if ( BaflUtils::FileExists( iFsSession, aFileName )) return KErrNone;
       
   589     if ( !FindFile( aFileName, _L("\\") ) ) return KErrNone;
       
   590     if ( !FindFile( aFileName, _L("\\data\\") ) ) return KErrNone;
       
   591     if ( !FindFile( aFileName, _L("\\documents\\") ) ) return KErrNone;
       
   592     if ( !FindFile( aFileName, _L("\\temp\\") ) ) return KErrNone;
       
   593     if ( !FindFile( aFileName, _L("\\system\\temp\\") ) ) return KErrNone;
       
   594     // if ( !FindFile( aFileName, _L("\\private\\1000484b\\") ) ) return KErrNone; // Mail folder KErrPermissionDenied  
       
   595     return KErrNotFound;
       
   596     }
       
   597 
       
   598 
       
   599 TInt CMemSpyCommandLine::FindFile( TDes &aFileName, const TDesC &aDirPath )
       
   600     {
       
   601     TFindFile fileFinder( iFsSession );
       
   602     TInt err = fileFinder.FindByDir( aFileName, aDirPath );
       
   603     if ( !err )
       
   604         {
       
   605         aFileName.Copy( fileFinder.File() );
       
   606         }
       
   607     return err;
       
   608     }
       
   609