analyzetool/dynamicmemoryhook/src/analyzetoolmainallocator.cpp
branchRCL_3
changeset 49 7fdc9a71d314
parent 19 da2cedce4920
child 59 8ad140f3dd41
equal deleted inserted replaced
46:e26895079d7c 49:7fdc9a71d314
    20 #include "atlog.h"
    20 #include "atlog.h"
    21 #include "analyzetooleventhandler.h"
    21 #include "analyzetooleventhandler.h"
    22 #include "analyzetoolmemoryallocator.h"
    22 #include "analyzetoolmemoryallocator.h"
    23 #include "analyzetoolpanics.pan"
    23 #include "analyzetoolpanics.pan"
    24 #include "analyzetoolfastlog.h"
    24 #include "analyzetoolfastlog.h"
       
    25 #include "analyzetoolfilelog.h"
    25 #include <e32svr.h>
    26 #include <e32svr.h>
       
    27 #ifndef __WINSCW__ 
       
    28 #include <e32rom.h>
       
    29 #endif
       
    30 #include <sysutil.h>
    26 
    31 
    27 
    32 
    28 // CONSTANTS
    33 // CONSTANTS
    29 
    34 
    30 // The name of the memoryhook dll
    35 // The name of the memoryhook dll
    31 _LIT8( KMemoryHook, "AToolMemoryHook.dll" );
    36 _LIT8( KMemoryHook, "AToolMemoryHook.dll" );
    32 
    37 
    33 // The name of the storage server dll
       
    34 _LIT8( KStorageServer, "AToolStorageServerClnt.dll" );
       
    35 
       
    36 // Length of the callstack address
    38 // Length of the callstack address
    37 const TUint32 KAddressLength = 4;
    39 const TUint32 KAddressLength = 4;
    38 
    40 
    39 // Thread count
    41 // Thread count
    40 const TInt KThreadCount = 1;
    42 const TInt KThreadCount = 1;
       
    43 
       
    44 // separator that replaces \n character in sw version
       
    45 _LIT( KSeparator, "@" );
       
    46 // new line character
       
    47 _LIT( KNewLine16, "\n" );
       
    48 _LIT( KSpace16, " " );
       
    49 
    41 
    50 
    42 // -----------------------------------------------------------------------------
    51 // -----------------------------------------------------------------------------
    43 // RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()
    52 // RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()
    44 // C++ default constructor can NOT contain any code, that
    53 // C++ default constructor can NOT contain any code, that
    45 // might leave.
    54 // might leave.
    46 // -----------------------------------------------------------------------------
    55 // -----------------------------------------------------------------------------
    47 //
    56 //
    48 RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator( TBool aNotFirst, 
    57 RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator( TBool aNotFirst, 
    49     const TFileName aFileName, TUint32 aLogOption, TUint32 aIsDebug,
    58     const TFileName& aFileName, const TPath& aFilePath, TUint32 aLogOption, TUint32 aIsDebug,
    50     TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize ) :
    59     TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize,
       
    60     const TDesC8& aAtoolVersion, const TDesC8& aApiVersion ) :
    51     RAnalyzeToolMemoryAllocator( aNotFirst ),
    61     RAnalyzeToolMemoryAllocator( aNotFirst ),
    52     iAnalyzeToolOpen( EFalse ),
    62     iAnalyzeToolOpen( EFalse ),
    53     iDeviceDriverLoaded( EFalse ),
    63     iDeviceDriverLoaded( EFalse ),
    54     iCodeblocks( KATMaxCallstackLength ),
    64     iCodeblocks( KATMaxCallstackLength ),
    55     iThreadArray( KATMaxCallstackLength ),
    65     iThreadArray( KATMaxCallstackLength ),
    60     {
    70     {
    61     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()" );
    71     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()" );
    62 
    72 
    63     // Basic error variable used in method.
    73     // Basic error variable used in method.
    64     TInt error( KErrNone );
    74     TInt error( KErrNone );
    65     
    75         
    66     // Connect to the storage server if logging mode not fast trace.
       
    67     if ( iLogOption != EATLogToTraceFast )
       
    68         {
       
    69         error = iStorageServer.Connect();
       
    70 
       
    71         LOGSTR2( "ATMH Opening RATStorageServer error %i", error );
       
    72     
       
    73         if ( KErrNone == error )
       
    74             {
       
    75             iStorageServerOpen = ETrue;
       
    76             }
       
    77         else
       
    78             {
       
    79             iStorageServerOpen = EFalse;
       
    80             }
       
    81     
       
    82         if ( KErrNone == error )
       
    83             {
       
    84             // Make the storage server handle shared between threads
       
    85             error = iStorageServer.ShareAuto();
       
    86             }
       
    87     
       
    88         LOGSTR2( "ATMH Sharing RATStorageServer error %i", error );
       
    89         }
       
    90     
       
    91     // Create mutex for schedule access to shared resources
    76     // Create mutex for schedule access to shared resources
    92     error = iMutex.CreateLocal();
    77     error = iMutex.CreateLocal();
    93 
    78 
    94     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantCreateMutex ) );
    79     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantCreateMutex ) );
    95 
    80 
   133         else
   118         else
   134             LOGSTR2( "ATMH AnalyzeTool GetMemoryModel error: %i", error );
   119             LOGSTR2( "ATMH AnalyzeTool GetMemoryModel error: %i", error );
   135         }
   120         }
   136     
   121     
   137     // Retrieve the initial process information
   122     // Retrieve the initial process information
   138     LogProcessInformation( aFileName, aLogOption, aIsDebug );
   123     LogProcessInformation( aFileName, aFilePath, aLogOption, aIsDebug, aAtoolVersion, aApiVersion );
       
   124 
       
   125     // log version of ATApp, ATApi, S60 version and ROM checksum
       
   126     LogDeviceInfo();
   139 
   127 
   140     // Create handler for receiving kernel events
   128     // Create handler for receiving kernel events
   141     iEventHandler = new CLibraryEventHandler( iAnalyzeTool,
   129     iEventHandler = new CLibraryEventHandler( iAnalyzeTool,
   142                                               iCodeblocks,
   130                                               iCodeblocks,
   143                                               iStorageServer,
       
   144                                               iProcessId,
   131                                               iProcessId,
   145                                               iMutex, 
   132                                               iMutex, 
   146                                               *this,
   133                                               *this,
   147                                               aLogOption);
   134                                               aLogOption,
       
   135                                               iLogFile );
   148 
   136 
   149     __ASSERT_ALWAYS( iEventHandler != NULL, AssertPanic( ENoMemory ) );
   137     __ASSERT_ALWAYS( iEventHandler != NULL, AssertPanic( ENoMemory ) );
   150     }
   138     }
   151 
   139 
   152 // -----------------------------------------------------------------------------
   140 // -----------------------------------------------------------------------------
   195             // There was no other users -> unload the device driver
   183             // There was no other users -> unload the device driver
   196             TInt error = User::FreeLogicalDevice( KAnalyzeToolLddName );
   184             TInt error = User::FreeLogicalDevice( KAnalyzeToolLddName );
   197             LOGSTR2( "ATMH Unloading ldd error: %i", error );
   185             LOGSTR2( "ATMH Unloading ldd error: %i", error );
   198             }
   186             }
   199         }
   187         }
   200     
   188     for( TInt i=0; i<iThreadArray.Count(); i++)
       
   189     	{
       
   190         // log thread removed
       
   191 		if ( iLogOption == EATLogToTraceFast )
       
   192 			{
       
   193 			ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
       
   194 			}
       
   195 		else if ( iLogOption == EATLogToFile )
       
   196 			{
       
   197 		    iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );		
       
   198 			}
       
   199     	}
       
   200 
   201     // Close the thread array
   201     // Close the thread array
   202     iThreadArray.Close();
   202     iThreadArray.Close();
   203 
   203 
   204     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   204     if ( iLogOption == EATLogToTraceFast )
   205         {
   205         {
   206         if ( iLogOption == EATLogToTraceFast )
   206         LOGSTR1( "ATMH ATFastLogProcessEnded()" );
   207             {
   207         ATFastLogProcessEnded( iProcessId, handleLeakCount );
   208             LOGSTR1( "ATMH ATFastLogProcessEnded()" );
   208         }
   209             ATFastLogProcessEnded( iProcessId, handleLeakCount );
   209     else if ( iLogOption == EATLogToFile )
   210             }
   210     	{
   211         else
   211         iLogFile.ATFileLogProcessEnded( handleLeakCount );
   212             {
   212     	}
   213             iStorageServerOpen = EFalse;
   213     
   214             // Inform that process has ended and close the handle
   214 
   215             LOGSTR1( "ATMH iStorageServer.LogProcessEnded()" );
       
   216             iStorageServer.LogProcessEnded( iProcessId, handleLeakCount );
       
   217             // Close the handle
       
   218             iStorageServer.Close();
       
   219             }
       
   220         }
       
   221     
       
   222     // Close the mutex
   215     // Close the mutex
   223     iMutex.Close();
   216     iMutex.Close();
   224     }
   217     }
   225 
   218 
   226 // -----------------------------------------------------------------------------
   219 // -----------------------------------------------------------------------------
   285     {
   278     {
   286     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()" );
   279     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()" );
   287 
   280 
   288     // Acquire the mutex
   281     // Acquire the mutex
   289     iMutex.Wait();
   282     iMutex.Wait();
       
   283     
       
   284     // get thread ID
       
   285     TUint threadId = RThread().Id();
   290 
   286 
   291     // Alloc memory from the original allocator
   287     // Alloc memory from the original allocator
   292     TAny* p = iAllocator->Alloc( aSize );
   288     TAny* p = iAllocator->Alloc( aSize );
   293 
   289 
   294     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
   290     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
   295             aSize,  (TUint32) p );
   291             aSize,  (TUint32) p );
   296 
   292 
   297     // Don't collect or log data if storage server not open or logging mode fast.
   293 
   298     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   294 	TInt error( KErrNone );
   299         {
   295 			
   300         TInt error( KErrNone );
   296 	// Check if eventhandler is started already
   301         
   297 	if ( !iEventHandler->IsStarted() )
   302         // Check if eventhandler is started already
   298 		{
   303         if ( !iEventHandler->IsStarted() )
   299 		// Install the eventhandler if needed
   304             {
   300 		InstallEventHandler();
   305             // Install the eventhandler if needed
   301 		}
   306             InstallEventHandler();
   302 	
   307             }
   303 	// Reset the callstack
   308         
   304 	iCallStack.Reset();
   309         // Reset the callstack
   305 
   310         iCallStack.Reset();
   306 	// If we don't want any call stack to be saved skip the next part
   311 
   307 	if( iAllocMaxCallStack > 0 )
   312         // If we don't want any call stack to be saved skip the next part
   308 		{
   313         if( iAllocMaxCallStack > 0 )
   309 		// Find the current thread callstack start address
   314             {
   310 		TUint32 stackstart( 0 );
   315             // Find the current thread callstack start address
   311 		TBool found( FindCurrentThreadStack( stackstart ) );
   316             TUint32 stackstart( 0 );
   312 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   317             TBool found( FindCurrentThreadStack( stackstart ) );
   313 					
   318             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   314 		// Returns the value of the stack pointer at the 
   319             
   315 		// current point in your program.
   320             // Returns the value of the stack pointer at the 
   316 		TUint32 _sp;
   321             // current point in your program.
   317 		__asm
   322             TUint32 _sp;
   318 			{
   323             __asm
   319 			mov [_sp], esp
   324                 {
   320 			}
   325                 mov [_sp], esp
   321 		
   326                 }
   322 		// Get process loaded code segments count
   327             
   323 		TInt blocksCount( iCodeblocks.Count() );
   328             // Get process loaded code segments count
   324 		TUint arrayCounter = 0;
   329             TInt blocksCount( iCodeblocks.Count() );
   325 		
   330             TUint arrayCounter = 0;
   326 		// Iterate through callstack to find wanted callstack addresses
   331             
   327 		// - Start: current stack address
   332             // Iterate through callstack to find wanted callstack addresses
   328 		// - Stop: stack start address(Run-address of user stack)
   333             // - Start: current stack address
   329 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   334             // - Stop: stack start address(Run-address of user stack)
   330 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   335             // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   331 			{
   336             for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   332 			TUint32 addr = (TUint32) *( (TUint32*) i );
   337                 {
   333 			
   338                 TUint32 addr = (TUint32) *( (TUint32*) i );
   334 			// Checks is the given address in loaded code memory area.
   339                 
   335 			if ( !IsAddressLoadedCode( addr ) )
   340                 // Checks is the given address in loaded code memory area.
   336 				continue;
   341                 if ( !IsAddressLoadedCode( addr ) )
   337 			
   342                     continue;
   338 			// Iterate through array of code blocks to check if address is in code segment area 
   343                 
   339 			for ( TInt j = 0; j < blocksCount; j++ )
   344                 // Iterate through array of code blocks to check if address is in code segment area 
   340 				{
   345                 for ( TInt j = 0; j < blocksCount; j++ )
   341 				// Checks if the given address is in this memory block area
   346                     {
   342 				if ( iCodeblocks[j].CheckAddress( addr ) )
   347                     // Checks if the given address is in this memory block area
   343 					{
   348                     if ( iCodeblocks[j].CheckAddress( addr ) )
   344 					// To avoid recursive call to ReAlloc specifying granularity
   349                         {
   345 					// Add address to the callstack
   350                         // To avoid recursive call to ReAlloc specifying granularity
   346 					iCallStack[arrayCounter] = ( addr );
   351                         // Add address to the callstack
   347 					arrayCounter++;
   352                         iCallStack[arrayCounter] = ( addr );
   348 					break;
   353                         arrayCounter++;
   349 					}
   354                         break;
   350 				}
   355                         }
   351 			
   356                     }
   352 			// Checks if the wanted callstack items are gathered
   357                 
   353 			if ( arrayCounter == KATMaxCallstackLength ||
   358                 // Checks if the wanted callstack items are gathered
   354 				 arrayCounter == iAllocMaxCallStack )
   359                 if ( arrayCounter == KATMaxCallstackLength ||
   355 				{
   360                      arrayCounter == iAllocMaxCallStack )
   356 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   361                     {
   357 				break;
   362                     LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   358 				}
   363                     break;
   359 			} 
   364                     }
   360 		}
   365                 } 
   361 
   366             }
   362 	// Log the memory allocation information
   367 
   363 	if ( iLogOption == EATLogToTraceFast )
   368         // Log the memory allocation information
   364 		{
   369         if ( iLogOption == EATLogToTraceFast )
   365 		// Using fast mode.
   370             {
   366 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p , iCallStack, aSize, threadId );
   371             // Using fast mode.
   367 		} 
   372             ATFastLogMemoryAllocated( iProcessId, (TUint32) p , iCallStack, aSize );
   368 	else if ( iLogOption == EATLogToFile )
   373             }
   369 		{
   374         else
   370 		iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId );
   375             {
   371 		}
   376             // Using storage server.
   372 	
   377             error = iStorageServer.LogMemoryAllocated( (TUint32) p,
       
   378                                                        iCallStack,
       
   379                                                        aSize );
       
   380             if ( KErrNone != error )
       
   381                 {
       
   382                 switch ( error )
       
   383                     {
       
   384                     case KErrNoMemory:
       
   385                         {
       
   386                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case"  );
       
   387                         // Check if eventhandler is active
       
   388                         if ( iEventHandler->IsActive() )
       
   389                             {
       
   390                             // Cancel iEventHandler because not needed anymore
       
   391                             iEventHandler->Cancel();
       
   392                             }
       
   393                         if ( iStorageServerOpen )
       
   394                             {
       
   395                             // Close storage server
       
   396                             iStorageServerOpen = EFalse;
       
   397                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer"  );
       
   398                             iStorageServer.Close();
       
   399                             }
       
   400                         break;
       
   401                         }
       
   402                     default:
       
   403                         {
       
   404                         LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
   405                         break;
       
   406                         }
       
   407                     }
       
   408                 }
       
   409             }
       
   410         }
       
   411     // Release the mutex
   373     // Release the mutex
   412     iMutex.Signal();
   374     iMutex.Signal();
   413 
   375 
   414     return p;
   376     return p;
   415     }
   377     }
   424     {
   386     {
   425     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()"  );
   387     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()"  );
   426 
   388 
   427     // acquire the mutex
   389     // acquire the mutex
   428     iMutex.Wait();
   390     iMutex.Wait();
       
   391     
       
   392     // get thread ID
       
   393     TUint threadId = RThread().Id();
   429 
   394 
   430     // Alloc memory from the original allocator
   395     // Alloc memory from the original allocator
   431     TAny* p = iAllocator->Alloc( aSize );
   396     TAny* p = iAllocator->Alloc( aSize );
   432 
   397 
   433     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
   398     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
   434             aSize,  (TUint32) p );
   399             aSize,  (TUint32) p );
   435 
   400 
   436     TInt error( KErrNone );
   401 
   437     
   402 	// Check if eventhandler is active already
   438     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   403 	// IsActive might return false value if a tested software has created many
   439         {
   404 	// threads which install own CActiveScheduler.
   440         // Check if eventhandler is active already
   405 	if ( !iEventHandler->IsStarted() )
   441         // IsActive might return false value if a tested software has created many
   406 		{
   442         // threads which install own CActiveScheduler.
   407 		// Install the eventhandler if needed
   443         if ( !iEventHandler->IsStarted() )
   408 		InstallEventHandler();
   444             {
   409 		}
   445             // Install the eventhandler if needed
   410 	
   446             InstallEventHandler();
   411 	// Reset the callstack
   447             }
   412 	iCallStack.Reset();
   448         
   413 	
   449         // Reset the callstack
   414 	// If we don't want any call stack to be saved skip the next part
   450         iCallStack.Reset();
   415 	if( iAllocMaxCallStack > 0 )
   451         
   416 		{
   452         // If we don't want any call stack to be saved skip the next part
   417 		// Find the current thread callstack start address
   453         if( iAllocMaxCallStack > 0 )
   418 		TUint32 stackstart( 0 );
   454             {
   419 		TBool found( FindCurrentThreadStack( stackstart ) );
   455             // Find the current thread callstack start address
   420 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   456             TUint32 stackstart( 0 );
   421 		
   457             TBool found( FindCurrentThreadStack( stackstart ) );
   422 		// Get process loaded code segments count
   458             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   423 		TInt blocksCount( iCodeblocks.Count() );            
   459             
   424 		TUint arrayCounter = 0;
   460             // Get process loaded code segments count
   425 		
   461             TInt blocksCount( iCodeblocks.Count() );            
   426 		// Iterate through callstack to find wanted callstack addresses
   462             TUint arrayCounter = 0;
   427 		// - Start: current stack address(__current_sp(): Returns the value of the 
   463             
   428 		//      stack pointer at the current point in your program.)
   464             // Iterate through callstack to find wanted callstack addresses
   429 		// - Stop: stack start address(Run-address of user stack)
   465             // - Start: current stack address(__current_sp(): Returns the value of the 
   430 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   466             //      stack pointer at the current point in your program.)
   431 		for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   467             // - Stop: stack start address(Run-address of user stack)
   432 			{
   468             // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   433 			TUint32 addr = (TUint32) *( (TUint32*) i );
   469             for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   434 			
   470                 {
   435 			// Checks is the given address in loaded code memory area.
   471                 TUint32 addr = (TUint32) *( (TUint32*) i );
   436 			if ( !IsAddressLoadedCode( addr ) )
   472                 
   437 				continue;
   473                 // Checks is the given address in loaded code memory area.
   438 			
   474                 if ( !IsAddressLoadedCode( addr ) )
   439 			// Iterate through array of code blocks to check if address is in code segment area 
   475                     continue;
   440 			for ( TInt j = 0; j < blocksCount; j++ )
   476                 
   441 				{
   477                 // Iterate through array of code blocks to check if address is in code segment area 
   442 				// Checks if the given address is in this memory block area
   478                 for ( TInt j = 0; j < blocksCount; j++ )
   443 				if ( iCodeblocks[j].CheckAddress( addr ) )
   479                     {
   444 					{
   480                     // Checks if the given address is in this memory block area
   445 					// To avoid recursive call to ReAlloc specifying granularity
   481                     if ( iCodeblocks[j].CheckAddress( addr ) )
   446 					// Add address to the callstack
   482                         {
   447 					iCallStack[arrayCounter] = ( addr );
   483                         // To avoid recursive call to ReAlloc specifying granularity
   448 					arrayCounter++;
   484                         // Add address to the callstack
   449 					break;
   485                         iCallStack[arrayCounter] = ( addr );
   450 					}
   486                         arrayCounter++;
   451 				}
   487                         break;
   452 			
   488                         }
   453 			// Checks if the wanted callstack items are gathered
   489                     }
   454 			if ( arrayCounter == KATMaxCallstackLength ||
   490                 
   455 				 arrayCounter == iAllocMaxCallStack )
   491                 // Checks if the wanted callstack items are gathered
   456 				{
   492                 if ( arrayCounter == KATMaxCallstackLength ||
   457 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   493                      arrayCounter == iAllocMaxCallStack )
   458 				break;
   494                     {
   459 				}
   495                     LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   460 			}
   496                     break;
   461 		}
   497                     }
   462 	// Log the memory allocation information
   498                 }
   463 	if ( iLogOption == EATLogToTraceFast )
   499             }
   464 		{
   500         // Log the memory allocation information
   465 		// Using fast mode.
   501         if ( iLogOption == EATLogToTraceFast )
   466 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
   502             {
   467 		}
   503             // Using fast mode.
   468 	else if ( iLogOption == EATLogToFile )
   504             ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize );
   469 		{
   505             }
   470 		iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId );
   506         else
   471 		}
   507             {
       
   508             // Using storage server.
       
   509             error = iStorageServer.LogMemoryAllocated( (TUint32) p,
       
   510                                                        iCallStack,
       
   511                                                        aSize );
       
   512             if ( KErrNone != error )
       
   513                 {
       
   514                 switch ( error )
       
   515                     {
       
   516                     case KErrNoMemory:
       
   517                         {
       
   518                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case"  );
       
   519                         // Check if eventhandler is active
       
   520                         if ( iEventHandler->IsActive() )
       
   521                             {
       
   522                             // Cancel ieventhandler because not needed anymore
       
   523                             iEventHandler->Cancel();
       
   524                             }
       
   525                         if ( iStorageServerOpen )
       
   526                             {
       
   527                             // Close storage server
       
   528                             iStorageServerOpen = EFalse;
       
   529                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer"  );
       
   530                             iStorageServer.Close();
       
   531                             }
       
   532                         break;
       
   533                         }
       
   534                     default:
       
   535                         {
       
   536                         LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
   537                         break;
       
   538                         }
       
   539                     }
       
   540                 }
       
   541             }
       
   542         }
       
   543     
   472     
   544     // Release the mutex
   473     // Release the mutex
   545     iMutex.Signal();
   474     iMutex.Signal();
   546 
   475 
   547     // Return the allocatated memory
   476     // Return the allocatated memory
   559     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Free()" );
   488     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Free()" );
   560 
   489 
   561     // Acquire the mutex
   490     // Acquire the mutex
   562     iMutex.Wait();
   491     iMutex.Wait();
   563     
   492     
   564     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   493     // get thread ID
   565         {
   494     TUint threadId = RThread().Id();
   566         // Reset the callstack
   495 
   567         iFreeCallStack.Reset();
   496 	// Reset the callstack
   568         
   497 	iFreeCallStack.Reset();
   569         // Check if trace logging mode
   498 	
   570         // Also if we don't want any call stack to be stored skip the next part
   499 	// Check if trace logging mode
   571         if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
   500 	// Also if we don't want any call stack to be stored skip the next part
   572                 && iFreeMaxCallStack > 0 )
   501 	if ( iFreeMaxCallStack > 0 )
   573             {
   502 		{
   574             // Find the current thread callstack start address
   503 		// Find the current thread callstack start address
   575             TUint32 stackstart( 0 );
   504 		TUint32 stackstart( 0 );
   576             TBool found( FindCurrentThreadStack( stackstart ) );
   505 		TBool found( FindCurrentThreadStack( stackstart ) );
   577             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   506 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   578             TUint32 _sp;
   507 		TUint32 _sp;
   579             
   508 		
   580             // Returns the value of the stack pointer at the 
   509 		// Returns the value of the stack pointer at the 
   581             // current point in your program.
   510 		// current point in your program.
   582             #ifdef __WINS__
   511 		#ifdef __WINS__
   583                 __asm
   512 			__asm
   584                     {
   513 				{
   585                     mov [_sp], esp
   514 				mov [_sp], esp
   586                     }
   515 				}
   587             #else
   516 		#else
   588                 _sp = __current_sp();
   517 			_sp = __current_sp();
   589             #endif
   518 		#endif
   590             
   519 		
   591             // Get process loaded code segments count
   520 		// Get process loaded code segments count
   592             TInt blocksCount( iCodeblocks.Count() );
   521 		TInt blocksCount( iCodeblocks.Count() );
   593             TUint arrayCounter = 0;
   522 		TUint arrayCounter = 0;
   594             
   523 		
   595             // Iterate through callstack to find wanted callstack addresses
   524 		// Iterate through callstack to find wanted callstack addresses
   596             // - Start: current stack address
   525 		// - Start: current stack address
   597             // - Stop: stack start address(Run-address of user stack)
   526 		// - Stop: stack start address(Run-address of user stack)
   598             // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)            
   527 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)            
   599             for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   528 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   600                 {
   529 			{
   601                 TUint32 addr = (TUint32) *( (TUint32*) i );
   530 			TUint32 addr = (TUint32) *( (TUint32*) i );
   602                 // Checks is the given address in loaded code memory area.
   531 			// Checks is the given address in loaded code memory area.
   603                 if ( ! IsAddressLoadedCode( addr ) )
   532 			if ( ! IsAddressLoadedCode( addr ) )
   604                     continue;
   533 				continue;
   605                 
   534 			
   606                 // Iterate through array of code blocks to check if address is in code segment area 
   535 			// Iterate through array of code blocks to check if address is in code segment area 
   607                 for ( TInt j = 0; j < blocksCount; j++ )
   536 			for ( TInt j = 0; j < blocksCount; j++ )
   608                     {
   537 				{
   609                     // Checks if the given address is in this memory block area
   538 				// Checks if the given address is in this memory block area
   610                     if ( iCodeblocks[j].CheckAddress( addr ) )
   539 				if ( iCodeblocks[j].CheckAddress( addr ) )
   611                         {
   540 					{
   612                         // To avoid recursive call to ReAlloc specifying granularity
   541 					// To avoid recursive call to ReAlloc specifying granularity
   613                         // Add address to the callstack
   542 					// Add address to the callstack
   614                         iFreeCallStack[arrayCounter] = addr;
   543 					iFreeCallStack[arrayCounter] = addr;
   615                         arrayCounter++;
   544 					arrayCounter++;
   616                         break;
   545 					break;
   617                         }
   546 					}
   618                     }
   547 				}
   619                 // Checks if the wanted callstack items are gathered
   548 			// Checks if the wanted callstack items are gathered
   620                 if ( arrayCounter == KATMaxFreeCallstackLength ||
   549 			if ( arrayCounter == KATMaxFreeCallstackLength ||
   621                      arrayCounter == iFreeMaxCallStack )
   550 				 arrayCounter == iFreeMaxCallStack )
   622                     {
   551 				{
   623                     break;
   552 				break;
   624                     }
   553 				}
   625                 }
   554 			}
   626             LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
   555 		LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
   627             }
   556 		}
   628         // Log the memory free information.
   557 	// Log the memory free information.
   629         if ( iLogOption == EATLogToTraceFast )
   558 	if ( iLogOption == EATLogToTraceFast )
   630             {
   559 		{
   631             // Using fast mode.
   560 		// Using fast mode.
   632             ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
   561 		ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack, threadId );
   633             }
   562 		}
   634         else
   563     else if ( iLogOption == EATLogToFile )
   635             {
   564     	{
   636             // Using storage server.
   565         iLogFile.ATFileLogMemoryFreed( (TUint32) aPtr, iFreeCallStack, threadId );
   637             TInt err( iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ) );
   566     	}
   638             if ( err != KErrNone )
   567      
   639                 {
       
   640                 LOGSTR2( "ATMH > LogMemoryFreed err( %i )", err );
       
   641                 }
       
   642             }
       
   643         }
       
   644     
       
   645     // Free the memory using original allocator
   568     // Free the memory using original allocator
   646     iAllocator->Free( aPtr );
   569     iAllocator->Free( aPtr );
   647 
   570 
   648     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::Free() - aPtr: %x", (TUint32)aPtr );
   571     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::Free() - aPtr: %x", (TUint32)aPtr );
   649 
   572 
   680             {
   603             {
   681             LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
   604             LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
   682             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
   605             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
   683             iThreadArray.Append( TThreadStack( RThread().Id(),
   606             iThreadArray.Append( TThreadStack( RThread().Id(),
   684                     params().iStackAddress + params().iStackSize ) );
   607                     params().iStackAddress + params().iStackSize ) );
       
   608             if ( iLogOption == EATLogToTraceFast )
       
   609             	{
       
   610                 // log thread added
       
   611                 ATFastLogThreadStarted( RProcess().Id().operator TUint() , RThread().Id().operator TUint() ); 
       
   612             	}
       
   613             else if ( iLogOption == EATLogToFile )
       
   614             	{
       
   615                 iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
       
   616             	}
   685             }
   617             }
   686         }
   618         }
   687 
   619 
   688     // Release the mutex
   620     // Release the mutex
   689     iMutex.Signal();
   621     iMutex.Signal();
   716         // Check if this is current thread
   648         // Check if this is current thread
   717         if ( iThreadArray[ i ].Match() )
   649         if ( iThreadArray[ i ].Match() )
   718             {
   650             {
   719             // Remove the thread
   651             // Remove the thread
   720             iThreadArray.Remove( i );
   652             iThreadArray.Remove( i );
       
   653             if ( iLogOption == EATLogToTraceFast )
       
   654             	{
       
   655                 // log thread removed
       
   656                 ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
       
   657             	}
       
   658             else if ( iLogOption == EATLogToFile )
       
   659             	{
       
   660                 iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );
       
   661             	}
   721             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - thread removed" );
   662             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - thread removed" );
   722             break;
   663             break;
   723             }
   664             }
   724         }
   665         }
   725     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - about to mutex signal" );
   666     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - about to mutex signal" );
   738     {
   679     {
   739     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
   680     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
   740 
   681 
   741     // Acquire the mutex
   682     // Acquire the mutex
   742     iMutex.Wait();
   683     iMutex.Wait();
       
   684     
       
   685     // get thread ID
       
   686     TUint threadId = RThread().Id();
   743 
   687 
   744     // Realloc the memory using original allocator
   688     // Realloc the memory using original allocator
   745     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   689     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   746     
   690     
   747     // NULL addresses are not in a process under test
   691     // NULL addresses are not in a process under test
   750         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   694         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   751                 (TUint32)aPtr, (TUint32)ptr );
   695                 (TUint32)aPtr, (TUint32)ptr );
   752         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   696         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   753                 aSize, aMode );
   697                 aSize, aMode );
   754       
   698       
   755         // Don't collect or log data if storage server not open or logging mode is not fast.
   699 
   756         if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   700 	TInt error( KErrNone );
   757             {
   701 	TUint arrayCounter = 0;
   758             TInt error( KErrNone );
   702 	
   759             TUint arrayCounter = 0;
   703 	// Reset the callstack
   760             
   704 	iReCallStack.Reset();
   761             // Reset the callstack
   705 	
   762             iReCallStack.Reset();
   706 	// If we don't want any call stack to be saved skip the next part
   763             
   707 	if( iAllocMaxCallStack > 0 )
   764             // If we don't want any call stack to be saved skip the next part
   708 		{
   765             if( iAllocMaxCallStack > 0 )
   709 		// Find the current thread callstack start address
   766                 {
   710 		TUint32 stackstart( 0 );
   767                 // Find the current thread callstack start address
   711 		TBool found( FindCurrentThreadStack( stackstart ) );
   768                 TUint32 stackstart( 0 );
   712 		LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
   769                 TBool found( FindCurrentThreadStack( stackstart ) );
   713 
   770                 LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
   714 		// Returns the value of the stack pointer at the 
   771     
   715 		// current point in your program.
   772                 // Returns the value of the stack pointer at the 
   716 		TUint32 _sp( 0 );
   773                 // current point in your program.
   717 		__asm
   774                 TUint32 _sp( 0 );
   718 			{
   775                 __asm
   719 			mov [_sp], esp
   776                     {
   720 			}
   777                     mov [_sp], esp
   721 		
   778                     }
   722 		// Get process loaded code segments count
   779                 
   723 		TInt blocksCount( iCodeblocks.Count() );
   780                 // Get process loaded code segments count
   724 		
   781                 TInt blocksCount( iCodeblocks.Count() );
   725 		// Iterate through callstack to find wanted callstack addresses
   782                 
   726 		// - Start: current stack address
   783                 // Iterate through callstack to find wanted callstack addresses
   727 		// - Stop: stack start address(Run-address of user stack)
   784                 // - Start: current stack address
   728 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)                
   785                 // - Stop: stack start address(Run-address of user stack)
   729 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   786                 // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)                
   730 			{
   787                 for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   731 			TUint32 addr = (TUint32) *( (TUint32*) i );
   788                     {
   732 			// Checks is the given address in loaded code memory area.
   789                     TUint32 addr = (TUint32) *( (TUint32*) i );
   733 			if ( ! IsAddressLoadedCode( addr ) )
   790                     // Checks is the given address in loaded code memory area.
   734 				continue;
   791                     if ( ! IsAddressLoadedCode( addr ) )
   735 			
   792                         continue;
   736 			// Iterate through array of code blocks to check if address is in code segment area 
   793                     
   737 			for ( TInt j = 0; j < blocksCount; j++ )
   794                     // Iterate through array of code blocks to check if address is in code segment area 
   738 				{
   795                     for ( TInt j = 0; j < blocksCount; j++ )
   739 				// Checks if the given address is in this memory block area
   796                         {
   740 				if ( iCodeblocks[j].CheckAddress( addr ) )
   797                         // Checks if the given address is in this memory block area
   741 					{
   798                         if ( iCodeblocks[j].CheckAddress( addr ) )
   742 					// To avoid recursive call to ReAlloc specifying granularity
   799                             {
   743 					// Add address to the callstack
   800                             // To avoid recursive call to ReAlloc specifying granularity
   744 					iReCallStack[arrayCounter] = addr;
   801                             // Add address to the callstack
   745 					arrayCounter++;
   802                             iReCallStack[arrayCounter] = addr;
   746 					break;
   803                             arrayCounter++;
   747 					}
   804                             break;
   748 				}
   805                             }
   749 			// Checks if the wanted callstack items are gathered
   806                         }
   750 			if ( arrayCounter == KATMaxCallstackLength || 
   807                     // Checks if the wanted callstack items are gathered
   751 				 arrayCounter == iAllocMaxCallStack )
   808                     if ( arrayCounter == KATMaxCallstackLength || 
   752 				{
   809                          arrayCounter == iAllocMaxCallStack )
   753 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   810                         {
   754 				break;
   811                         LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   755 				}
   812                         break;
   756 			}
   813                         }
   757 		}
   814                     }
   758 	
   815                 }
   759 	
   816             
   760 	// Log the memory reallocation information
   817             // No need to report free if the aPtr was NULL
   761 	if ( iLogOption == EATLogToTraceFast )
   818             if ( aPtr != NULL )
   762 		{
   819                 {
   763 		// Using fast logging mode.
   820                 // Reset the free callstack
   764 		ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   821                 iFreeCallStack.Reset();
   765 		}
   822                 
   766 	else if ( iLogOption == EATLogToFile )
   823                 // Check that logging mode is trace/trace fast so we use free call stack 
   767 		{
   824                 // and call stack size bigger than zero
   768 		iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   825                 if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast ) && iFreeMaxCallStack > 0 )
   769 		}
   826                     {
   770        
   827                     for ( TInt i = 0; i < arrayCounter; i++ )
       
   828                         {
       
   829                         if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack )
       
   830                             {
       
   831                             break;
       
   832                             }
       
   833                         iFreeCallStack[i] = iReCallStack[i];
       
   834                         }
       
   835                     }
       
   836                 // Try to remove old address from the storage server's
       
   837                 // leak array. If found. it's removed from the array because system frees
       
   838                 // old address directly in the RHeap in ReAlloc case.
       
   839                 if ( iLogOption == EATLogToTraceFast )
       
   840                     {
       
   841                     ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
       
   842                     }
       
   843                 else
       
   844                     {
       
   845                     iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
       
   846                     }
       
   847                 }
       
   848             // Log the memory allocation information
       
   849             if ( iLogOption == EATLogToTraceFast )
       
   850                 {
       
   851                 // Using fast logging mode.
       
   852                 ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize );
       
   853                 }
       
   854             else
       
   855                 {
       
   856                 // Using storage server.
       
   857                 error = iStorageServer.LogMemoryAllocated( (TUint32) ptr,
       
   858                                                            iReCallStack,
       
   859                                                            aSize );
       
   860                 if ( KErrNone != error )
       
   861                     {
       
   862                     switch ( error )
       
   863                         {
       
   864                         case KErrNoMemory:
       
   865                             {
       
   866                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - KErrNoMemory case"  );
       
   867                             // Check if eventhandler is active
       
   868                             if ( iEventHandler->IsActive() )
       
   869                                 {
       
   870                                 // Cancel iEventHandler because not needed anymore
       
   871                                 iEventHandler->Cancel();
       
   872                                 }
       
   873                             if ( iStorageServerOpen )
       
   874                                 {
       
   875                                 // Close storage server
       
   876                                 iStorageServerOpen = EFalse;
       
   877                                 LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - close iStorageServer"  );
       
   878                                 iStorageServer.Close();
       
   879                                 }
       
   880                             break;
       
   881                             }
       
   882                         default:
       
   883                             {
       
   884                             LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
   885                             break;
       
   886                             }
       
   887                         }
       
   888                     }
       
   889                 }
       
   890             }
       
   891         }
   771         }
   892     
   772     
   893     // Release the mutex
   773     // Release the mutex
   894     iMutex.Signal();
   774     iMutex.Signal();
   895 
   775 
   909     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
   789     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
   910 
   790 
   911     // Acquire the mutex
   791     // Acquire the mutex
   912     iMutex.Wait();
   792     iMutex.Wait();
   913 
   793 
       
   794     // get thread ID
       
   795     TUint threadId = RThread().Id();
       
   796     
   914     // Realloc the memory using original allocator
   797     // Realloc the memory using original allocator
   915     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   798     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   916     
   799     
   917     TInt error( KErrNone );
       
   918     TUint arrayCounter = 0;
   800     TUint arrayCounter = 0;
   919     
   801     
   920     // NULL addresses are not in a process under test
   802     // NULL addresses are not in a process under test
   921     if ( ptr && !( aMode & ENeverMove ) )
   803     if ( ptr && !( aMode & ENeverMove ) )
   922         {
   804         {
   923         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   805         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   924                 (TUint32)aPtr, (TUint32)ptr );
   806                 (TUint32)aPtr, (TUint32)ptr );
   925         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   807         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   926                 aSize, aMode );
   808                 aSize, aMode );
   927  
   809  
   928         // Don't collect or log data if storage server not open or logging mode is not fast.
   810 		// Reset the callstack
   929         if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   811 		iReCallStack.Reset();
   930             {
   812 		
   931             // Reset the callstack
   813 		// If we don't want any call stack to be saved skip the next part
   932             iReCallStack.Reset();
   814 		if( iAllocMaxCallStack > 0 )
   933             
   815 			{
   934             // If we don't want any call stack to be saved skip the next part
   816 			// Find the current thread callstack start address
   935             if( iAllocMaxCallStack > 0 )
   817 			TUint32 stackstart( 0 );
   936                 {
   818 			TBool found( FindCurrentThreadStack( stackstart ) );
   937                 // Find the current thread callstack start address
   819 			LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
   938                 TUint32 stackstart( 0 );
   820 			
   939                 TBool found( FindCurrentThreadStack( stackstart ) );
   821 			// Get process loaded code segments count
   940                 LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
   822 			TInt blocksCount( iCodeblocks.Count() );
   941                 
   823 			
   942                 // Get process loaded code segments count
   824 			// Iterate through callstack to find wanted callstack addresses
   943                 TInt blocksCount( iCodeblocks.Count() );
   825 			// - Start: current stack address(__current_sp(): Returns the value of the 
   944                 
   826 			//      stack pointer at the current point in your program.)
   945                 // Iterate through callstack to find wanted callstack addresses
   827 			// - Stop: stack start address(Run-address of user stack)
   946                 // - Start: current stack address(__current_sp(): Returns the value of the 
   828 			// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   947                 //      stack pointer at the current point in your program.)
   829 			for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   948                 // - Stop: stack start address(Run-address of user stack)
   830 				{
   949                 // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
   831 				TUint32 addr = (TUint32) *( (TUint32*) i );
   950                 for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   832 				
   951                     {
   833 				// Checks is the given address in loaded code memory area.
   952                     TUint32 addr = (TUint32) *( (TUint32*) i );
   834 				if ( !IsAddressLoadedCode( addr ) )
   953                     
   835 					continue;
   954                     // Checks is the given address in loaded code memory area.
   836 							
   955                     if ( !IsAddressLoadedCode( addr ) )
   837 				// Iterate through array of code blocks to check if address is in code segment area 
   956                         continue;
   838 				for ( TInt j = 0; j < blocksCount; j++ )
   957                                 
   839 					{
   958                     // Iterate through array of code blocks to check if address is in code segment area 
   840 					// Checks if the given address is in this memory block area
   959                     for ( TInt j = 0; j < blocksCount; j++ )
   841 					if ( iCodeblocks[j].CheckAddress( addr ) )
   960                         {
   842 						{
   961                         // Checks if the given address is in this memory block area
   843 						// To avoid recursive call to ReAlloc specifying granularity
   962                         if ( iCodeblocks[j].CheckAddress( addr ) )
   844 						// Add address to the callstack
   963                             {
   845 						iReCallStack[arrayCounter] = ( addr );
   964                             // To avoid recursive call to ReAlloc specifying granularity
   846 						arrayCounter++;
   965                             // Add address to the callstack
   847 						break;
   966                             iReCallStack[arrayCounter] = ( addr );
   848 						}
   967                             arrayCounter++;
   849 					}
   968                             break;
   850 				// Checks if the wanted callstack items are gathered
   969                             }
   851 				if ( arrayCounter == KATMaxCallstackLength || 
   970                         }
   852 					 arrayCounter == iAllocMaxCallStack )
   971                     // Checks if the wanted callstack items are gathered
   853 					{
   972                     if ( arrayCounter == KATMaxCallstackLength || 
   854 					LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   973                          arrayCounter == iAllocMaxCallStack )
   855 					break;
   974                         {
   856 					}
   975                         LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   857 				}
   976                         break;
   858 			}
   977                         }
   859 		
   978                     }
   860 		if ( iLogOption == EATLogToTraceFast )
   979                 }
   861 			{
   980             
   862 			// Using fast logging mode.
   981             // No need to report free if the aPtr was NULL
   863 			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   982             if ( aPtr != NULL )
   864 			}
   983                 {
   865 		else if ( iLogOption == EATLogToFile )
   984                 // Reset the free callstack
   866 			{
   985                 iFreeCallStack.Reset();
   867 			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   986                 
   868 			} 
   987                 // Check that logging mode is trace/trace fast so we use free call stack 
       
   988                 // and call stack size bigger than zero
       
   989                 if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
       
   990                         && iFreeMaxCallStack > 0 )
       
   991                     {
       
   992                     for ( TInt i = 0; i < arrayCounter; i++ )
       
   993                         {
       
   994                         if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack )
       
   995                             {
       
   996                             break;
       
   997                             }
       
   998                         iFreeCallStack[i] = ( iReCallStack[i] );
       
   999                         }
       
  1000                     }
       
  1001                 
       
  1002                 // Try to remove old address from the storage server's
       
  1003                 // leak array. If found. it's removed from the array because system frees
       
  1004                 // old address directly in the RHeap in ReAlloc case.
       
  1005                 if ( iLogOption == EATLogToTraceFast )
       
  1006                     {
       
  1007                     ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
       
  1008                     }
       
  1009                 else
       
  1010                     {
       
  1011                     iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
       
  1012                     }
       
  1013                 }
       
  1014             
       
  1015             // Log the memory allocation information
       
  1016             if ( iLogOption == EATLogToTraceFast )
       
  1017                 {
       
  1018                 // Using fast logging mode.
       
  1019                 ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize );
       
  1020                 }
       
  1021             else
       
  1022                 {
       
  1023                 // Using storage server.
       
  1024                 error = iStorageServer.LogMemoryAllocated( (TUint32) ptr,
       
  1025                                                         iReCallStack,
       
  1026                                                         aSize );
       
  1027                 if ( KErrNone != error )
       
  1028                     {
       
  1029                     switch ( error )
       
  1030                         {
       
  1031                         case KErrNoMemory:
       
  1032                             {
       
  1033                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - KErrNoMemory case"  );
       
  1034                             // Check if eventhandler is active
       
  1035                             if ( iEventHandler->IsActive() )
       
  1036                                 {
       
  1037                                 // Cancel iEventHandler because not needed anymore
       
  1038                                 iEventHandler->Cancel();
       
  1039                                 }
       
  1040                             if ( iStorageServerOpen )
       
  1041                                 {
       
  1042                                 // Close storage server
       
  1043                                 iStorageServerOpen = EFalse;
       
  1044                                 LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - close iStorageServer"  );
       
  1045                                 iStorageServer.Close();
       
  1046                                 }
       
  1047                             break;
       
  1048                             }
       
  1049                         default:
       
  1050                             {
       
  1051                             LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
  1052                             break;
       
  1053                             }
       
  1054                         }
       
  1055                     }
       
  1056                 }
       
  1057             }
       
  1058         }
   869         }
  1059 
   870 
  1060     // Release the mutex
   871     // Release the mutex
  1061     iMutex.Signal();
   872     iMutex.Signal();
  1062 
   873 
  1247         // Check if this is current thread
  1058         // Check if this is current thread
  1248         if ( iThreadArray[ i ].Match( aThreadId ) )
  1059         if ( iThreadArray[ i ].Match( aThreadId ) )
  1249             {
  1060             {
  1250             // Remove the thread
  1061             // Remove the thread
  1251             iThreadArray.Remove( i );
  1062             iThreadArray.Remove( i );
       
  1063             if ( iLogOption == EATLogToTraceFast )
       
  1064             	{
       
  1065                 // log thread removed
       
  1066                 ATFastLogThreadEnded( RProcess().Id().operator TUint(), aThreadId ); 
       
  1067             	}
       
  1068             else if ( iLogOption == EATLogToFile )
       
  1069 				{
       
  1070                 iLogFile.ATFileLogThreadEnded( aThreadId );
       
  1071 				}
  1252             LOGSTR1( "ATMH > thread removed" );
  1072             LOGSTR1( "ATMH > thread removed" );
  1253             break;
  1073             break;
  1254             }
  1074             }
  1255         }
  1075         }
  1256 
  1076 
  1283 // -----------------------------------------------------------------------------
  1103 // -----------------------------------------------------------------------------
  1284 // RAnalyzeToolMainAllocator::LogProcessInformation()
  1104 // RAnalyzeToolMainAllocator::LogProcessInformation()
  1285 // Retrieve and log the process initial information
  1105 // Retrieve and log the process initial information
  1286 // -----------------------------------------------------------------------------
  1106 // -----------------------------------------------------------------------------
  1287 //
  1107 //
  1288 void RAnalyzeToolMainAllocator::LogProcessInformation( const TFileName aFileName,
  1108 void RAnalyzeToolMainAllocator::LogProcessInformation( const TFileName& aFileName, const TPath& aFilePath,
  1289     TUint32 aLogOption, TUint32 aIsDebug )
  1109     TUint32 aLogOption, TUint32 aIsDebug, const TDesC8& aAtoolVersion, const TDesC8& aApiVersion )
  1290     {
  1110     {
  1291     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation()" );
  1111     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation()" );
  1292     
  1112     
  1293     // Create local variable and retrieve the process information
  1113     // Create local variable and retrieve the process information
  1294     TProcessIdentityParamsBuf params;
  1114     TProcessIdentityParamsBuf params;
  1307         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
  1127         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
  1308 
  1128 
  1309         // Append thread to array of the users of this allocator
  1129         // Append thread to array of the users of this allocator
  1310         error = iThreadArray.Append(
  1130         error = iThreadArray.Append(
  1311         TThreadStack( RThread().Id(), params().iStackAddress + params().iStackSize) );
  1131         TThreadStack( RThread().Id(), params().iStackAddress + params().iStackSize) );
       
  1132         
  1312 
  1133 
  1313         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) );
  1134         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) );
  1314 
  1135         
  1315         // Log process information
  1136         // Using fast logging mode.
  1316         if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
  1137         if ( iLogOption == EATLogToTraceFast )
  1317             {
  1138             {          
  1318             if ( iLogOption == EATLogToTraceFast )
  1139             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogProcessStarted() #1" );
       
  1140             // Log process information
       
  1141             ATFastLogProcessStarted( params().iProcessName, iProcessId, aIsDebug, aAtoolVersion, aApiVersion );    
       
  1142             // log thread added
       
  1143             ATFastLogThreadStarted( iProcessId, RThread().Id().operator TUint() );
       
  1144             }
       
  1145         else if ( iLogOption == EATLogToFile )
       
  1146             {
       
  1147             // Open a file server session and a file.
       
  1148             error = iLogFile.OpenFsAndFile( aFileName, aFilePath, params().iProcessName );
       
  1149 
       
  1150             // Return without logging, if an error occured
       
  1151             if ( error != KErrNone )
  1319                 {
  1152                 {
  1320                 // Using fast logging mode.
  1153                 // TODO
  1321                 LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogProcessStarted() #1" );
       
  1322                 ATFastLogProcessStarted( params().iProcessName, iProcessId, aIsDebug );                
       
  1323                 }
  1154                 }
  1324             else
  1155             
  1325                 {
  1156             //if everything is ok, add file version in the begining of file
  1326                 // Using storage server.
  1157             iLogFile.ATFileLogVersion();
  1327                 LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #1" );
  1158             //log process start
  1328                 error = iStorageServer.LogProcessStarted(
  1159             iLogFile.ATFileLogProcessStarted( params().iProcessName, iProcessId, aIsDebug, aAtoolVersion, aApiVersion ); 
  1329                         aFileName,
  1160             // log thread added
  1330                         params().iProcessName,
  1161             iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
  1331                         iProcessId, 
  1162             }
  1332                         aLogOption, 
  1163                       
  1333                         aIsDebug );
       
  1334                 }
       
  1335             }
       
  1336 
       
  1337         LOGSTR2( "ATMH LogProcessStarted error %i", error );
  1164         LOGSTR2( "ATMH LogProcessStarted error %i", error );
  1338 
  1165 
  1339         // Iterate through process codesegments
  1166         // Iterate through process codesegments
  1340         for( TInt i = 0; i < params().iCodesegCount; i++ )
  1167         for( TInt i = 0; i < params().iCodesegCount; i++ )
  1341             {
  1168             {
  1347 
  1174 
  1348             LOGSTR2( "ATMH GetCodesegInfo error %i", error );
  1175             LOGSTR2( "ATMH GetCodesegInfo error %i", error );
  1349             if ( KErrNone == error )
  1176             if ( KErrNone == error )
  1350                 {
  1177                 {
  1351                 // Don't log AnalyzeTool libraries
  1178                 // Don't log AnalyzeTool libraries
  1352                 if ( 0 != codeinfo().iFullName.CompareC( KMemoryHook ) &&
  1179                 if ( 0 != codeinfo().iFullName.CompareC( KMemoryHook ) )
  1353                      0 != codeinfo().iFullName.CompareC( KStorageServer ) )
       
  1354                     {
  1180                     {
  1355                     // Log the loaded codesegment(s)
  1181                     // Log the loaded codesegment(s)
  1356                     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
  1182 
       
  1183                     if ( iLogOption == EATLogToTraceFast )
  1357                         {
  1184                         {
  1358                         if ( iLogOption == EATLogToTraceFast )
  1185                         // Using fast logging mode.
  1359                             {
  1186                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogDllLoaded() #2" );
  1360                             // Using fast logging mode.
  1187                         ATFastLogDllLoaded( iProcessId,
  1361                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogDllLoaded() #2" );
  1188                                 codeinfo().iFullName,
  1362                             ATFastLogDllLoaded( iProcessId,
  1189                                 codeinfo().iRunAddress,
  1363                                     codeinfo().iFullName,
  1190                                 codeinfo().iRunAddress + codeinfo().iSize );
  1364                                     codeinfo().iRunAddress,
       
  1365                                     codeinfo().iRunAddress + codeinfo().iSize );
       
  1366                             }
       
  1367                         else
       
  1368                             {
       
  1369                             // Using storage server.
       
  1370                             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #2" );
       
  1371                             error = iStorageServer.LogDllLoaded(
       
  1372                                     codeinfo().iFullName,
       
  1373                                     codeinfo().iRunAddress,
       
  1374                                     codeinfo().iRunAddress + codeinfo().iSize );
       
  1375                             }
       
  1376                         }
  1191                         }
       
  1192                     else if ( iLogOption == EATLogToFile )
       
  1193 						{
       
  1194                         iLogFile.ATFileLogDllLoaded( 
       
  1195 								codeinfo().iFullName,
       
  1196                                 codeinfo().iRunAddress,
       
  1197                                 codeinfo().iRunAddress + codeinfo().iSize );
       
  1198 						}
       
  1199                     
  1377 
  1200 
  1378                     LOGSTR2( "ATMH LogDllLoaded error %i", error );
  1201                     LOGSTR2( "ATMH LogDllLoaded error %i", error );
  1379 
  1202 
  1380                     // Check that everything is OK
  1203                     // Check that everything is OK
  1381                     if ( KErrNone == error )
  1204                     if ( KErrNone == error )
  1401             error = iAnalyzeTool.GetLibraryInfo( info );
  1224             error = iAnalyzeTool.GetLibraryInfo( info );
  1402             LOGSTR2( "ATMH GetLibraryInfo error %i", error );
  1225             LOGSTR2( "ATMH GetLibraryInfo error %i", error );
  1403             if ( KErrNone == error )
  1226             if ( KErrNone == error )
  1404                 {
  1227                 {
  1405                 // Log the loaded dynamic codesegment(s)
  1228                 // Log the loaded dynamic codesegment(s)
  1406                 if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
  1229 
       
  1230                 if ( iLogOption == EATLogToTraceFast )
  1407                     {
  1231                     {
  1408                     if ( iLogOption == EATLogToTraceFast )
  1232                     // Using fast logging mode.
  1409                         {
  1233                     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - - ATFastLogDllLoaded()#3" );
  1410                         // Using fast logging mode.
  1234                     ATFastLogDllLoaded( iProcessId,
  1411                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - - ATFastLogDllLoaded()#3" );
  1235                             info().iLibraryName,
  1412                         ATFastLogDllLoaded( iProcessId,
  1236                             info().iRunAddress,
  1413                                 info().iLibraryName,
  1237                             info().iRunAddress + info().iSize );
  1414                                 info().iRunAddress,
       
  1415                                 info().iRunAddress + info().iSize );
       
  1416                         }
       
  1417                     else
       
  1418                         {
       
  1419                         // Using storage server.
       
  1420                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #3" );
       
  1421                         error = iStorageServer.LogDllLoaded(
       
  1422                                 info().iLibraryName,
       
  1423                                 info().iRunAddress,
       
  1424                                 info().iRunAddress + info().iSize );
       
  1425                         }
       
  1426                     }
  1238                     }
  1427 
  1239                 else if ( iLogOption == EATLogToFile )
  1428 
  1240                 	{
       
  1241                 	iLogFile.ATFileLogDllLoaded( 
       
  1242                 			info().iLibraryName,
       
  1243                 			info().iRunAddress,
       
  1244                 			info().iRunAddress + info().iSize );
       
  1245                 	}
       
  1246      
  1429                 LOGSTR2( "ATMH LogDllLoaded error %i", error );
  1247                 LOGSTR2( "ATMH LogDllLoaded error %i", error );
  1430 
  1248 
  1431                 if ( KErrNone == error )
  1249                 if ( KErrNone == error )
  1432                     {
  1250                     {
  1433                     // Append the codesegment to the array
  1251                     // Append the codesegment to the array
  1439                 }
  1257                 }
  1440             }
  1258             }
  1441         }
  1259         }
  1442     }
  1260     }
  1443 
  1261 
       
  1262 
  1444 // -----------------------------------------------------------------------------
  1263 // -----------------------------------------------------------------------------
  1445 // RAnalyzeToolMainAllocator::FindCurrentThreadStack()
  1264 // RAnalyzeToolMainAllocator::FindCurrentThreadStack()
  1446 // Find the current thread which is using the heap
  1265 // Find the current thread which is using the heap
  1447 // -----------------------------------------------------------------------------
  1266 // -----------------------------------------------------------------------------
  1448 //
  1267 //
  1502     // Active eventhalder is not active, trying to start it
  1321     // Active eventhalder is not active, trying to start it
  1503     if ( NULL != CActiveScheduler::Current() )
  1322     if ( NULL != CActiveScheduler::Current() )
  1504         {
  1323         {
  1505         iEventHandler->Start();
  1324         iEventHandler->Start();
  1506         }
  1325         }
       
  1326       }
       
  1327 
       
  1328 // -----------------------------------------------------------------------------
       
  1329 // RAnalyzeToolMainAllocator::LogDeviceInfo()
       
  1330 // Logs iversion of ATApp, ATApi, S60 version and ROM checksum 
       
  1331 // at the startup of hooked application
       
  1332 // -----------------------------------------------------------------------------
       
  1333 //
       
  1334 void RAnalyzeToolMainAllocator::LogDeviceInfo()
       
  1335     {
       
  1336     //get s60 version
       
  1337     TBuf<KSysUtilVersionTextLength> bufS60;
       
  1338     if (SysUtil::GetSWVersion(bufS60) == KErrNone)
       
  1339         {
       
  1340         TInt index(0);
       
  1341         //replace newlines in string with '@'
       
  1342         //while ((index = bufS60.Find(KNewLine)) != KSysUtilVersionTextLength && index != KErrNotFound)
       
  1343         while ( (index = bufS60.Find(KNewLine16)) != KErrNotFound)
       
  1344             {
       
  1345             bufS60.Replace(index, 1, KSeparator);
       
  1346             }
       
  1347         while ( (index = bufS60.Find(KSpace16)) != KErrNotFound)
       
  1348 			{
       
  1349 			bufS60.Replace(index, 1, KSeparator);
       
  1350 			}
       
  1351         }
       
  1352 
       
  1353     TBuf<KHexa32Length> bufChecksum;
       
  1354     
       
  1355 #ifndef __WINSCW__
       
  1356     TRomHeader* romHeader = (TRomHeader*) UserSvr::RomHeaderAddress();
       
  1357     if (romHeader)
       
  1358         {
       
  1359         bufChecksum.Format(_L("%08x"), romHeader->iCheckSum);
       
  1360         }
       
  1361 #endif
       
  1362     
       
  1363     TBuf8<KSysUtilVersionTextLength> s60Version;
       
  1364     s60Version.Copy(bufS60);
       
  1365     
       
  1366     TBuf8<KHexa32Length> romChecksum;
       
  1367     romChecksum.Copy(bufChecksum);
       
  1368     
       
  1369     //only fastlog implementation
       
  1370     if (iLogOption == EATLogToTraceFast)
       
  1371     	{  
       
  1372         ATFastLogDeviceInfo(s60Version, romChecksum);
       
  1373         }
       
  1374     else if ( iLogOption == EATLogToFile )
       
  1375     	{
       
  1376         iLogFile.ATFileLogDeviceInfo( s60Version, romChecksum );
       
  1377     	}
       
  1378     
  1507     }
  1379     }
  1508 
  1380 
  1509 // End of File
  1381 // End of File