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