perfsrv/memspy/CommandLine/Source/MemSpyCommandLine.cpp
changeset 48 516af714ebb4
child 55 f2950aff7424
equal deleted inserted replaced
45:185201be11b0 48:516af714ebb4
       
     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 <memspysession.h>
       
    25 #include <memspy/engine/memspyenginehelpersysmemtrackerconfig.h>
       
    26 
       
    27 // User includes
       
    28 #include "MemSpyCommands.h"
       
    29 
       
    30 /*
       
    31 CMemSpyCommandLine::CMemSpyCommandLine()
       
    32     {	
       
    33     }
       
    34 */
       
    35 
       
    36 CMemSpyCommandLine::CMemSpyCommandLine( CConsoleBase& aConsole )
       
    37 	: CActive( EPriorityHigh ), iConsole( aConsole )
       
    38     {	
       
    39 	CActiveScheduler::Add( this );
       
    40     }
       
    41 
       
    42 CMemSpyCommandLine::~CMemSpyCommandLine()
       
    43     {
       
    44 	Cancel();
       
    45 	
       
    46     if ( iMemSpySession )
       
    47         {
       
    48         iMemSpySession->Close();
       
    49         }
       
    50     delete iMemSpySession;
       
    51     iFsSession.Close();
       
    52     }
       
    53 
       
    54 
       
    55 void CMemSpyCommandLine::ConstructL()
       
    56     {
       
    57     User::LeaveIfError( iFsSession.Connect() );   
       
    58     iMemSpySession = new(ELeave) RMemSpySession();
       
    59     ConnectToMemSpyL();                    
       
    60     }
       
    61 
       
    62 CMemSpyCommandLine* CMemSpyCommandLine::NewLC( CConsoleBase& aConsole )
       
    63     {
       
    64     CMemSpyCommandLine* self = new(ELeave) CMemSpyCommandLine( aConsole );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     return self;
       
    68     }
       
    69 
       
    70 void CMemSpyCommandLine::PerformOpL( const CCommandLineArguments& aCommandLine )
       
    71     {
       
    72     const TInt count = aCommandLine.Count();
       
    73     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformOpL() - START - arg count: %d, this: 0x%08x", count, this ) );
       
    74 
       
    75     if  ( count >= 1 )
       
    76         {
       
    77         // Get main command
       
    78         TBuf<KMemSpyCommandLineMaxLength> command;
       
    79         command.Copy( aCommandLine.Arg( 1 ) );
       
    80         command.UpperCase();
       
    81 
       
    82         // Extract arguments into array
       
    83         CDesCArrayFlat* args = new(ELeave) CDesCArrayFlat(4);
       
    84         CleanupStack::PushL( args );
       
    85         for( TInt i=2; i<count; i++ )
       
    86             {
       
    87             args->AppendL( aCommandLine.Arg( i ) );
       
    88             }
       
    89 
       
    90         // Perform op
       
    91         PerformSingleOpL( command, *args );
       
    92 
       
    93         // Tidy up
       
    94         CleanupStack::PopAndDestroy( args );
       
    95         }
       
    96     else
       
    97         {
       
    98         User::Leave( KErrUnderflow );
       
    99         }
       
   100     }
       
   101 
       
   102 
       
   103 void CMemSpyCommandLine::PerformSingleOpL( const TDesC& aCommand, const CDesCArray& aParameters )
       
   104     {
       
   105     // Record duration
       
   106     TTime timeStart; 
       
   107     timeStart.HomeTime();
       
   108     const TInt paramCount = aParameters.Count();
       
   109 
       
   110 #ifdef _DEBUG
       
   111     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - START - command: %S, paramCount: %d, this: 0x%08x"), &aCommand, paramCount, this ) );
       
   112     for( TInt i=0; i<paramCount; i++ )
       
   113         {
       
   114         const TPtrC pParam( aParameters[ i ] );
       
   115         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - param[%02d] = [%S]"), i, &pParam ) );
       
   116         }
       
   117 #else
       
   118     RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d}..."), &aCommand, paramCount );
       
   119 #endif
       
   120 
       
   121     TFileName batchFile;
       
   122     batchFile.Append( aCommand );
       
   123     
       
   124     TInt err = KErrNotSupported;
       
   125     TInt error = KErrNotSupported;
       
   126     
       
   127     // --- HELP
       
   128     if ( aCommand.CompareF( KMemSpyCmdHelp1) == 0 || 
       
   129     	 aCommand.CompareF( KMemSpyCmdHelp2) == 0 ||
       
   130     	 aCommand.CompareF( KMemSpyCmdHelp3) == 0 ||
       
   131     	 aCommand.CompareF( KMemSpyCmdHelp4) == 0 )
       
   132     	{
       
   133 		iConsole.Write( KHelpMessage );
       
   134 		iConsole.Write( KMemSpyCLINewLine );		
       
   135 		iConsole.Write( KHelpOutputCommand );
       
   136 		iConsole.Write( KHelpOutputToFileCommand );
       
   137 		iConsole.Write( KHelpHeapDumpCommand );
       
   138 		iConsole.Write( KHelpSwmtCommand );
       
   139 		iConsole.Write( KHelpKillServerCommand );
       
   140 		iConsole.Write( KMemSpyCLINewLine );
       
   141 		iConsole.Write( KHelpCommand );
       
   142 
       
   143 	    // Show input prompt.
       
   144 	    iCommandPromptPos = iConsole.CursorPos();
       
   145 	    RedrawInputPrompt();
       
   146 	    WaitForInput();
       
   147 	    
       
   148 	    CActiveScheduler::Start();
       
   149     	}
       
   150     // --- OUTPUT
       
   151     //TODO: directory option to be added
       
   152     else if  ( aCommand.CompareF( KMemSpyCmdOutput ) == 0 )	//change output mode   
       
   153     	{    						
       
   154 		if( paramCount >= 1 )
       
   155 			{
       
   156 			if( aParameters[0].CompareF( KMemSpyCmdOutputParameterFile ) == 0 )
       
   157 				{
       
   158 				if( paramCount == 2 )
       
   159 					{
       
   160 					TBuf<KMaxFileName> directory;
       
   161 					directory.Copy( aParameters[1] );
       
   162 					iMemSpySession->SwitchOutputToFileL( directory );
       
   163 					}
       
   164 				else
       
   165 					{
       
   166 					iMemSpySession->SwitchOutputToFileL( KNullDesC );
       
   167 					}
       
   168 				}
       
   169 			else if( aParameters[0].CompareF( KMemSpyCmdOutputParameterTrace ) == 0)
       
   170 				{
       
   171 				TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Output Trace", this ) );
       
   172 				iMemSpySession->SwitchOutputToTraceL();
       
   173 				}
       
   174 			}		           
       
   175     	}    	
       
   176     // --- HEAP DUMP    
       
   177     else if ( aCommand.CompareF( KMemSpyCmdHeapDump) == 0 )    	
       
   178 		{		
       
   179 		RedrawStatusMessage( KHeapDumpMessage );
       
   180 		
       
   181 		if( paramCount == 0 ) // no parameter - dump all heap data + kernel heap at the end
       
   182 			{		
       
   183 		
       
   184 			TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_Dump (all threads)", this ) );			
       
   185 			// Dump heap data for all threads - Thread agnostic operation					
       
   186 			iMemSpySession->OutputHeapData();
       
   187 			// Dump kernel heap data
       
   188 			iMemSpySession->OutputThreadHeapDataL( KMemSpyClientServerThreadIdKernel );					
       
   189 			}
       
   190 		else if( paramCount >= 1)
       
   191 			{
       
   192 			if( aParameters[0].CompareF( KMemSpyCmdHeapDumpParameterAll ) == 0 )
       
   193 				{
       
   194 				iMemSpySession->OutputHeapData();				
       
   195 				iMemSpySession->OutputThreadHeapDataL( KMemSpyClientServerThreadIdKernel );				
       
   196 				}
       
   197 			else if( aParameters[0].CompareF( KMemSpyCmdHeapDumpParameterKernel ) == 0 )
       
   198 				{
       
   199 				TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_DumpKernel", this ) );
       
   200 				iMemSpySession->OutputThreadHeapDataL( KMemSpyClientServerThreadIdKernel );				
       
   201 				}
       
   202 			else
       
   203 				{				
       
   204 				// Dump heap data for named thread - filter
       
   205 				const TPtrC pThreadName( aParameters[0] );
       
   206 				TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - Heap_Dump (%S)"), this, &pThreadName ) );				
       
   207 				iMemSpySession->OutputThreadHeapDataL( pThreadName );
       
   208 				}
       
   209   			}
       
   210 		}
       
   211     
       
   212     // --- SYSTEM WIDE MEMORY TRACKING    
       
   213     else if( aCommand.CompareF( KMemSpyCmdSwmt ) == 0 )
       
   214     	{    
       
   215 		RedrawStatusMessage( KSWMTMessage );
       
   216     		
       
   217 		TInt categories( 0 );
       
   218 		TName threadNameFilter;
       
   219 		
       
   220 		if( paramCount == 0 ) //default state -> "dumpnow" command with "all" categories
       
   221 			{
       
   222 			TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - this: 0x%08x - dumpnow command", this ) );
       
   223 			TInt category = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryAll;
       
   224 			iMemSpySession->SetSwmtCategoriesL( category );			
       
   225 			iMemSpySession->ForceSwmtUpdateL();			
       
   226 			}
       
   227 		else if( paramCount >= 1)
       
   228 			{
       
   229 			const TPtrC pParam( aParameters[0] );
       
   230 			if( pParam.CompareF( KMemSpyCmdSwmtParameterStarttimer) == 0 ) // "starttimer" - start tracking
       
   231 				{
       
   232 				TInt result(0);
       
   233 				categories = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryAll;
       
   234 				iMemSpySession->SetSwmtTimerIntervalL( KMemSpySysMemTrackerConfigMinTimerPeriod );
       
   235 				
       
   236 				if( paramCount >= 2 ) // user gave some optional parameters - <categories> or <value in seconds>
       
   237 					{					
       
   238 					TLex lex( aParameters[1] );
       
   239 				    if ( lex.Val( result ) == KErrNone ) //if 2nd parameter is not number, then parse parameters
       
   240 				    	{
       
   241 						if( result >= KMemSpySysMemTrackerConfigMinTimerPeriod && result <= KMemSpySysMemTrackerConfigMaxTimerPeriod )
       
   242 							{
       
   243 							iMemSpySession->SetSwmtTimerIntervalL( result );							;
       
   244 							}											
       
   245 				    	}				   
       
   246 				    TRAP( err, ParseSWMTParametersL( aParameters, categories, threadNameFilter) );
       
   247 					}																				
       
   248 				
       
   249 				//if( !err )
       
   250 				//	{
       
   251 					/*
       
   252 					_LIT( KPressS, "Press 's' to stop the timer " );
       
   253 					iConsole.Write( KPressS );
       
   254 					
       
   255 					iCommandPromptPos = iConsole.CursorPos();					
       
   256 					RedrawInputPrompt();					
       
   257 					WaitForInput();
       
   258 					*/
       
   259 					    
       
   260 					iMemSpySession->StartSwmtTimerL();
       
   261 					
       
   262 					//CActiveScheduler::Start();									
       
   263 				//	}	
       
   264 				}
       
   265 			else if( pParam.CompareF( KMemSpyCmdSwmtParameterStoptimer) == 0 ) // "stoptime" - stop tracking
       
   266 				{
       
   267 				iMemSpySession->StopSwmtTimerL();
       
   268 				}
       
   269 			else if( pParam.CompareF( KMemSpyCmdSwmtParameterDumpnow ) == 0 ) // "dumpnow" - runs one tracking cycle (CmdSWMT_ForceUpdate before)
       
   270 				{
       
   271 				categories = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryAll;
       
   272 				if( paramCount >= 2 ) // user gave some optional parameters - <categories>
       
   273 					{
       
   274 					TRAP( err, ParseSWMTParametersL( aParameters, categories, threadNameFilter) );
       
   275 					}				
       
   276 																
       
   277 				if( !err )
       
   278 					{
       
   279 					iMemSpySession->SetSwmtCategoriesL( categories );
       
   280 					iMemSpySession->ForceSwmtUpdateL();
       
   281 					}												
       
   282 				}							
       
   283 			else //no parameters ("starttimer / stoptimer / dumpnow"), just categories / thread filter
       
   284 				 //so dumpnow is used as default with category / thread specified
       
   285 				{
       
   286 				TRAP( err, ParseSWMTParametersL( aParameters, categories, threadNameFilter) );
       
   287 				if( !err )
       
   288 					{
       
   289 					iMemSpySession->SetSwmtCategoriesL( categories );
       
   290 					if( threadNameFilter.Length() > 0 )
       
   291 						{
       
   292 						iMemSpySession->SetSwmtFilter( threadNameFilter );
       
   293 						}
       
   294 					}								
       
   295 					iMemSpySession->ForceSwmtUpdateL();				
       
   296 				}
       
   297 			}
       
   298     	}
       
   299     // --- KILL SERVER
       
   300     else if ( aCommand.CompareF( KMemSpyCmdKillServer ) == 0 )
       
   301     	{    
       
   302     	}
       
   303     
       
   304    // RedrawStatusMessage();   
       
   305     
       
   306     TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::PerformSingleOpL() - END - err: %d, this: 0x%08x, cmd: %S" ), err, this, &aCommand ) );
       
   307 
       
   308     // Calculate duration
       
   309     TTime timeEnd;
       
   310     timeEnd.HomeTime();
       
   311     TTimeIntervalSeconds interval( 0 );
       
   312     timeEnd.SecondsFrom( timeStart, interval );
       
   313 
       
   314     // Print some info
       
   315     if  ( err != KErrNone )
       
   316         {
       
   317         RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d} {%08d sec} => ERROR: %d"), &aCommand, paramCount, interval.Int(), err );
       
   318         }
       
   319     else
       
   320         {
       
   321         RDebug::Print( _L("[MemSpyCmdLine] COMMAND: [%S] {%02d} {%08d sec} => OK"), &aCommand, paramCount, interval.Int() );
       
   322         }
       
   323 
       
   324     // Spacer
       
   325     RDebug::Printf( " " );
       
   326     }
       
   327 
       
   328 
       
   329 void CMemSpyCommandLine::ConnectToMemSpyL()
       
   330     {
       
   331     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - START - this: 0x%08x", this ) );
       
   332 
       
   333     TInt err = iMemSpySession->Connect();
       
   334     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - connect #1 err: %d, this: 0x%08x", err, this ) );
       
   335 
       
   336     if  ( err == KErrNotFound )
       
   337         {
       
   338         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - launching MemSpy... - this: 0x%08x", this ) );
       
   339         LaunchMemSpyL();
       
   340 
       
   341         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - launched MemSpy - this: 0x%08x", this ) );
       
   342         err = iMemSpy->Connect();
       
   343 
       
   344         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - connect #2 err: %d, this: 0x%08x", err, this ) );
       
   345         }
       
   346 
       
   347     User::LeaveIfError( err );
       
   348 
       
   349     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ConnectToMemSpyL() - END - ok - this: 0x%08x", this ) );
       
   350     }
       
   351 
       
   352 
       
   353 void CMemSpyCommandLine::LaunchMemSpyL()
       
   354     {
       
   355     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - START - this: 0x%08x", this ) );
       
   356 
       
   357     TInt err = KErrGeneral;
       
   358     RProcess proc;
       
   359     
       
   360     // Try to run server first
       
   361     err = proc.Create( KMemSpyProcessName0, KNullDesC );
       
   362     if ( err == KErrNone )
       
   363     	{
       
   364 		TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Create server process successfully... - this: 0x%08x", this ) );
       
   365 
       
   366 		TRequestStatus status;
       
   367 		proc.Rendezvous( status );
       
   368 		proc.Resume();
       
   369 
       
   370 		TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - MemSpy resumed, waiting for Rendezvous... - this: 0x%08x", this ) );
       
   371 
       
   372 		User::WaitForRequest( status );
       
   373 		err = status.Int();
       
   374 		proc.Close();
       
   375 
       
   376 		TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Rendezvous complete: %d, this: 0x%08x", err, this ) );
       
   377     	}
       
   378 
       
   379     // If server is not available, try with s60 UI
       
   380     if ( err != KErrNone )
       
   381     	{
       
   382 		err = proc.Create( KMemSpyProcessName1, KNullDesC );
       
   383 		if  ( err == KErrNone )
       
   384 			{
       
   385 			TFullName fullName;
       
   386 			proc.FullName( fullName );
       
   387 			TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Create S60 UI process successfully... - this: 0x%08x, name: %S"), this, &fullName ) );
       
   388 	
       
   389 			TRequestStatus status;
       
   390 			proc.Rendezvous( status );
       
   391 			proc.Resume();
       
   392 	
       
   393 			TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - MemSpy resumed, waiting for Rendezvous... - this: 0x%08x", this ) );
       
   394 			User::WaitForRequest( status );
       
   395 			err = status.Int();
       
   396 			proc.Close();
       
   397 	
       
   398 			TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - Rendezvous complete: %d, this: 0x%08x", err, this ) );
       
   399 			}
       
   400     	}
       
   401 
       
   402     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - final error: %d, this: 0x%08x", err, this ) );
       
   403     User::LeaveIfError( err );
       
   404     User::After( 10 * 1000000 );
       
   405 
       
   406     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::LaunchMemSpyL() - END - ok - this: 0x%08x", this ) );
       
   407     }
       
   408 
       
   409 
       
   410 CDesCArray* CMemSpyCommandLine::ReadLinesL( RFile& aFile )
       
   411     {
       
   412     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - START - this: 0x%08x", this ) );
       
   413     CDesCArrayFlat* lines = new(ELeave) CDesCArrayFlat( 10 );
       
   414     CleanupStack::PushL( lines );
       
   415 
       
   416     TInt size = 0;
       
   417     User::LeaveIfError( aFile.Size( size ) );
       
   418     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x, file size: %d", this, size ) );
       
   419 
       
   420     // Read file
       
   421     HBufC8* narrowBuf = HBufC8::NewLC( size );
       
   422     TPtr8 pBufNarrow( narrowBuf->Des() );
       
   423     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x, reading file..." ) );
       
   424     User::LeaveIfError( aFile.Read( pBufNarrow ) );
       
   425     if  ( pBufNarrow.Length() <= 2 )
       
   426         {
       
   427         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - BAD FILE LENGTH", this ) );
       
   428         User::Leave( KErrCorrupt );
       
   429         }
       
   430 
       
   431     // Look for BOM and convert to unicode
       
   432     HBufC* unicodeText = HBufC::NewL( size );
       
   433     TPtr pUnicodeText( unicodeText->Des() );
       
   434     if  ( pBufNarrow[0] == 0xFF && pBufNarrow[1] == 0xFE )
       
   435         {
       
   436         // It's unicode... and we don't want the BOM, hence -2
       
   437         const TInt textLength = size - 2;
       
   438         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - UNICODE TEXT - textLength: %d", this, textLength / 2 ) );
       
   439         Mem::Copy( (TUint8 *)pUnicodeText.Ptr(), pBufNarrow.Ptr() + 2, textLength );
       
   440         pUnicodeText.SetLength( textLength / 2 );
       
   441         }
       
   442     else
       
   443         {
       
   444         // It's ASCII, convert it to unicode...
       
   445         TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - ASCII TEXT - textLength: %d", this, size ) );
       
   446         for (TInt i = 0; i<size; i++ )
       
   447             {
       
   448             pUnicodeText.Append( pBufNarrow[ i ] );
       
   449             }
       
   450         }
       
   451 
       
   452     // Discard old narrow text
       
   453     CleanupStack::PopAndDestroy( narrowBuf );
       
   454     CleanupStack::PushL( unicodeText );
       
   455 
       
   456     // Try to extract lines of text...
       
   457     HBufC* line = HBufC::NewLC( 1024 );
       
   458     TPtr pLine( line->Des() );
       
   459     const TInt length = unicodeText->Length();
       
   460     for( TInt i=0; i<length; i++ )
       
   461         {
       
   462         const TChar c( pUnicodeText[ i ] );
       
   463         //
       
   464         if  ( c == '\r' || c == '\n' )
       
   465             {
       
   466             pLine.Trim();
       
   467             if  ( pLine.Length() )
       
   468                 {
       
   469                 TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - LINE[%03d] %S"), this, lines->Count(), line ) );
       
   470                 lines->AppendL( pLine );
       
   471                 }
       
   472 
       
   473             pLine.Zero();
       
   474             }
       
   475         else
       
   476             {
       
   477             pLine.Append( c );
       
   478             }
       
   479         }
       
   480 
       
   481     // Save last line, just in cae it didn't end with a CR/LF
       
   482     pLine.Trim();
       
   483     if ( pLine.Length() )
       
   484         {
       
   485         TRACE( RDebug::Print( _L("[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - this: 0x%08x - LINE[%03d] %S"), this, lines->Count(), line ) );
       
   486         lines->AppendL( pLine );
       
   487         }
       
   488 
       
   489     CleanupStack::PopAndDestroy( 2, unicodeText ); // line & unicodeText
       
   490     CleanupStack::Pop( lines );
       
   491     TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine::ReadLinesL() - END - this: 0x%08x", this ) );
       
   492 
       
   493     return lines;
       
   494     }
       
   495 
       
   496 
       
   497 void CMemSpyCommandLine::ParseSWMTParametersL( const CDesCArray& aParameters, TInt& aCategories, TDes& aFilter )
       
   498     {
       
   499     TInt result(0);
       
   500     // Check if the first parameter is a number.
       
   501     // In that case other parameters are ignored.
       
   502     TLex lex( aParameters[ 0 ] );
       
   503     if ( lex.Val( result ) != KErrNone )
       
   504         {		
       
   505         // Parameters were given in text form:
       
   506         const TInt count( aParameters.Count() );
       
   507         for ( TInt i = 0; i < count ; i++ )
       
   508             {
       
   509 			lex = aParameters[ i ]; //check if num.
       
   510             if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeHeap ) == 0 )
       
   511                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserHeap |
       
   512                           TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHeap;
       
   513             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeChunk ) == 0 )
       
   514                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryLocalChunks |
       
   515                           TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalChunks;
       
   516             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeCode ) == 0 )
       
   517                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMLoadedCode;
       
   518             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeStack ) == 0 )
       
   519                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserStacks;
       
   520             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeGlobalData ) == 0 )
       
   521                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalData;
       
   522             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeRamDrive ) == 0 )
       
   523                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMDrive;
       
   524             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeOpenFile ) == 0 )
       
   525                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryOpenFiles;
       
   526             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeDiskSpace ) == 0 )
       
   527                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryDiskusage;
       
   528             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeFbserv ) == 0 ) // enables both FABS and BITM 
       
   529                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryBitmapHandles;
       
   530             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeHandleGeneric ) == 0 ) // enables both HGEN and HPAS
       
   531                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHandles;
       
   532             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeFileServerCache ) == 0 )
       
   533                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryFileServerCache;
       
   534             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeSystemMemory ) == 0 )
       
   535                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategorySystemMemory;
       
   536             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeWindowGroup ) == 0 )            	
       
   537                 result |= TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryWindowGroups;            
       
   538             else if ( aParameters[i].CompareF( KMemSpyCmdSWMTTypeAll) == 0 ) //"all" category added
       
   539             	result = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryAll;
       
   540             else if ( aParameters[i].CompareF( KMemSpyCmdSwmtParameterDumpnow) == 0 || 
       
   541             		aParameters[i].CompareF( KMemSpyCmdSwmtParameterStarttimer) == 0 || 
       
   542             		aParameters[i].CompareF( KMemSpyCmdSwmtParameterStoptimer) == 0 )
       
   543             	{    
       
   544 				TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine:: command parameter") );
       
   545             	}
       
   546             else if ( lex.Val( result ) == KErrNone )
       
   547             	{
       
   548 				TRACE( RDebug::Printf( "[MemSpyCmdLine] CMemSpyCommandLine:: number - timer period") );
       
   549             	}
       
   550             else// if ( aParameters[i].Find( KMemSpyCmdSWMTTypeHeapFilter ) == 0 )
       
   551                 {				
       
   552                 aFilter.Copy( aParameters[i].Right( aParameters[i].Length() -11 ) );              
       
   553                 }
       
   554           /*  else
       
   555             	{
       
   556                 //User::Leave( KErrNotSupported );            	            
       
   557             	}*/
       
   558             }
       
   559         }
       
   560     else if ( aParameters.Count() > 1 )//&& aParameters[1].Find( KMemSpyCmdSWMTTypeHeapFilter ) == 0 )
       
   561         {
       
   562         aFilter.Copy( aParameters[1].Right( aParameters[1].Length() -11 ) );
       
   563         }
       
   564     aCategories = result;
       
   565     }
       
   566 
       
   567 
       
   568 TInt CMemSpyCommandLine::FindBatchFile( TDes &aFileName )
       
   569     {
       
   570     if ( BaflUtils::FileExists( iFsSession, aFileName )) return KErrNone;
       
   571     if ( !FindFile( aFileName, _L("\\") ) ) return KErrNone;
       
   572     if ( !FindFile( aFileName, _L("\\data\\") ) ) return KErrNone;
       
   573     if ( !FindFile( aFileName, _L("\\documents\\") ) ) return KErrNone;
       
   574     if ( !FindFile( aFileName, _L("\\temp\\") ) ) return KErrNone;
       
   575     if ( !FindFile( aFileName, _L("\\system\\temp\\") ) ) return KErrNone;
       
   576     // if ( !FindFile( aFileName, _L("\\private\\1000484b\\") ) ) return KErrNone; // Mail folder KErrPermissionDenied  
       
   577     return KErrNotFound;
       
   578     }
       
   579 
       
   580 
       
   581 TInt CMemSpyCommandLine::FindFile( TDes &aFileName, const TDesC &aDirPath )
       
   582     {
       
   583     TFindFile fileFinder( iFsSession );
       
   584     TInt err = fileFinder.FindByDir( aFileName, aDirPath );
       
   585     if ( !err )
       
   586         {
       
   587         aFileName.Copy( fileFinder.File() );
       
   588         }
       
   589     return err;
       
   590     }
       
   591 
       
   592 
       
   593 //CLI status messages methods
       
   594 void CMemSpyCommandLine::RedrawInputPrompt()
       
   595     {
       
   596     iConsole.SetCursorPosAbs( iCommandPromptPos );
       
   597     iConsole.ClearToEndOfLine();
       
   598     iConsole.Printf( KMemSpyCLIInputPrompt, &iCommandBuffer );
       
   599     }
       
   600 
       
   601 
       
   602 void CMemSpyCommandLine::RedrawStatusMessage()
       
   603     {
       
   604     RedrawStatusMessage( KNullDesC );
       
   605     }
       
   606 
       
   607 
       
   608 void CMemSpyCommandLine::RedrawStatusMessage( const TDesC& aMessage )
       
   609     {
       
   610     iConsole.SetCursorPosAbs( iStatusMessagePos );
       
   611     iConsole.ClearToEndOfLine();
       
   612     iConsole.Write( aMessage );
       
   613     iConsole.Write( KMemSpyCLINewLine );
       
   614     }
       
   615 
       
   616 void CMemSpyCommandLine::WaitForInput()
       
   617     {
       
   618     ASSERT( !IsActive() );
       
   619     iConsole.Read( iStatus );
       
   620     SetActive();
       
   621     }
       
   622 
       
   623 void CMemSpyCommandLine::DoCancel()
       
   624     {
       
   625     iConsole.ReadCancel();
       
   626     }
       
   627 
       
   628 void CMemSpyCommandLine::RunL()
       
   629     {
       
   630     TKeyCode key = iConsole.KeyCode();
       
   631     //
       
   632     if  ( key == EKeyEnter || key == KMemSpyUiS60KeyCodeButtonOk || key == KMemSpyUiS60KeyCodeRockerEnter )
       
   633         {
       
   634         TRAP_IGNORE( ProcessCommandBufferL() );
       
   635         }
       
   636     else
       
   637         {
       
   638         TChar character( key );
       
   639         if  ( character.IsPrint() )
       
   640             {
       
   641             if  ( iCommandBuffer.Length() < iCommandBuffer.MaxLength() )
       
   642                 {
       
   643                 iCommandBuffer.Append( TChar( key ) );
       
   644                 }
       
   645 
       
   646             RedrawInputPrompt();
       
   647             }
       
   648         }
       
   649 
       
   650     WaitForInput();
       
   651     }
       
   652 
       
   653 TInt CMemSpyCommandLine::RunError( TInt aError )
       
   654 	{	
       
   655 	return KErrNone;
       
   656 	}
       
   657 
       
   658 void CMemSpyCommandLine::ProcessCommandBufferL()
       
   659     {
       
   660     iCommandBuffer.Trim();
       
   661     //
       
   662 #ifdef _DEBUG
       
   663     RDebug::Print( _L("[MCon] CMemSpyConsoleMenu::ProcessCommandBufferL() - cmd: [%S]"), &iCommandBuffer );
       
   664 #endif
       
   665     //
       
   666     TBool validCommand = EFalse;
       
   667     if  ( iCommandBuffer.Length() == 1 )
       
   668         {
       
   669         // Reset if not recognised...
       
   670         validCommand = ETrue;
       
   671 
       
   672         const TChar cmd = iCommandBuffer[ 0 ]; 
       
   673         switch( cmd )
       
   674             {
       
   675         	case 's':
       
   676         	case 'S':
       
   677         		{
       
   678         		iMemSpy->PerformOperation( EMemSpyClientServerOpSystemWideMemoryTrackingTimerStop );
       
   679         		
       
   680         		CActiveScheduler::Stop();
       
   681         		return;
       
   682         		}
       
   683         	case 'c':
       
   684         	case 'C':
       
   685         		CActiveScheduler::Stop();
       
   686         		return;            
       
   687         	default:
       
   688         		validCommand = EFalse;
       
   689         		break;
       
   690             }
       
   691         }    
       
   692     if  ( !validCommand )
       
   693         {
       
   694         _LIT( KInvalidEntry, "*** ERROR - Invalid Command ***" );
       
   695         RedrawStatusMessage( KInvalidEntry );
       
   696         RedrawInputPrompt();
       
   697         }
       
   698     }