perfsrv/analyzetool/dynamicmemoryhook/src/analyzetoolmainallocator.cpp
changeset 48 516af714ebb4
equal deleted inserted replaced
45:185201be11b0 48:516af714ebb4
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Definitions for the class RAnalyzeToolMainAllocator.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "analyzetoolmainallocator.h"
       
    20 #include "atlog.h"
       
    21 #include "analyzetooleventhandler.h"
       
    22 #include "analyzetoolmemoryallocator.h"
       
    23 #include "analyzetoolpanics.pan"
       
    24 #include "analyzetoolfastlog.h"
       
    25 #include "analyzetoolfilelog.h"
       
    26 #include <e32svr.h>
       
    27 #ifndef __WINSCW__ 
       
    28 #include <e32rom.h>
       
    29 #endif
       
    30 #include <sysutil.h>
       
    31 
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // The name of the memoryhook dll
       
    36 _LIT8( KMemoryHook, "AToolMemoryHook.dll" );
       
    37 
       
    38 // Length of the callstack address
       
    39 const TUint32 KAddressLength = 4;
       
    40 
       
    41 // Thread count
       
    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 
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()
       
    53 // C++ default constructor can NOT contain any code, that
       
    54 // might leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator( TBool aNotFirst, 
       
    58     const TFileName& aFileName, const TPath& aFilePath, TUint32 aLogOption, TUint32 aIsDebug,
       
    59     TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize,
       
    60     const TDesC8& aAtoolVersion, const TDesC8& aApiVersion ) :
       
    61     RAnalyzeToolMemoryAllocator( aNotFirst ),
       
    62     iAnalyzeToolOpen( EFalse ),
       
    63     iDeviceDriverLoaded( EFalse ),
       
    64     iCodeblocks( KATMaxCallstackLength ),
       
    65     iThreadArray( KATMaxCallstackLength ),
       
    66     iLogOption( aLogOption ),
       
    67     iProcessId( RProcess().Id().operator TUint() ),
       
    68     iAllocMaxCallStack( aAllocCallStackSize ),
       
    69     iFreeMaxCallStack( aFreeCallStackSize )
       
    70     {
       
    71     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator()" );
       
    72 
       
    73     // Basic error variable used in method.
       
    74     TInt error( KErrNone );
       
    75         
       
    76     // Create mutex for schedule access to shared resources
       
    77     error = iMutex.CreateLocal();
       
    78 
       
    79     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantCreateMutex ) );
       
    80 
       
    81     LOGSTR2( "ATMH Creating mutex error %i", error );
       
    82 
       
    83     // Load the kernel side device driver
       
    84     error = User::LoadLogicalDevice( KAnalyzeToolLddName );
       
    85 
       
    86     if ( error != KErrNone && error != KErrAlreadyExists )
       
    87         {
       
    88         __ASSERT_ALWAYS( EFalse, AssertPanic( ECantLoadDeviceDriver ) );
       
    89         }
       
    90     else
       
    91         {
       
    92         iDeviceDriverLoaded = ETrue;
       
    93         }
       
    94 
       
    95     LOGSTR2( "ATMH Loading device driver error %i", error );
       
    96 
       
    97     // Open handle to the kernel sidedevice driver
       
    98     error = iAnalyzeTool.Open();
       
    99 
       
   100     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantConnectDeviceDriver ) );
       
   101 
       
   102     if ( KErrNone == error )
       
   103         {
       
   104         iAnalyzeToolOpen = ETrue;
       
   105         }
       
   106 
       
   107     LOGSTR2( "ATMH Opening RAnalyzeTool handle %i error", error );
       
   108 
       
   109     // Set memory model by asking kernel side device driver
       
   110     if ( iAnalyzeToolOpen )
       
   111         {
       
   112         TATMemoryModelBuf model;
       
   113         if ( KErrNone == iAnalyzeTool.GetMemoryModel( model ) )
       
   114             {
       
   115             iMemoryModel = model().iMemoryModel;
       
   116             LOGSTR2( "ATMH AnalyzeTool MemoryModel: %i", iMemoryModel );
       
   117             }
       
   118         else
       
   119             LOGSTR2( "ATMH AnalyzeTool GetMemoryModel error: %i", error );
       
   120         }
       
   121     
       
   122     // Retrieve the initial process information
       
   123     LogProcessInformation( aFileName, aFilePath, aLogOption, aIsDebug, aAtoolVersion, aApiVersion );
       
   124 
       
   125     // log version of ATApp, ATApi, S60 version and ROM checksum
       
   126     LogDeviceInfo();
       
   127 
       
   128     // Create handler for receiving kernel events
       
   129     iEventHandler = new CLibraryEventHandler( iAnalyzeTool,
       
   130                                               iCodeblocks,
       
   131                                               iProcessId,
       
   132                                               iMutex, 
       
   133                                               *this,
       
   134                                               aLogOption,
       
   135                                               iLogFile );
       
   136 
       
   137     __ASSERT_ALWAYS( iEventHandler != NULL, AssertPanic( ENoMemory ) );
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // RAnalyzeToolMainAllocator::~RAnalyzeToolMainAllocator()
       
   142 // Destructor.
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 RAnalyzeToolMainAllocator::~RAnalyzeToolMainAllocator()
       
   146     {
       
   147     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::~RAnalyzeToolMainAllocator()" );
       
   148 
       
   149     TUint handleLeakCount( 0 );
       
   150     if ( iAnalyzeToolOpen && iThreadArray.Count() > 0 )
       
   151         {
       
   152         TProcessHandleInfoBuf params;
       
   153         params().iProcessId = iProcessId;
       
   154         TInt error( iAnalyzeTool.GetProcessHandleInfo( params ) );
       
   155         handleLeakCount = params().iThreadHandleCount;
       
   156         }
       
   157 
       
   158     // Close handle for process memory blocks
       
   159     iCodeblocks.Close();
       
   160 
       
   161     // Delete the eventhandler
       
   162     delete iEventHandler;
       
   163 
       
   164     // The count of device driver users
       
   165     TClientCountBuf count;
       
   166 
       
   167     // Check the flag
       
   168     if ( iAnalyzeToolOpen )
       
   169         {
       
   170         TInt error  = iAnalyzeTool.ClientCount( count );
       
   171         LOGSTR2( "ATMH closing analyze tool handle error: %i", error );
       
   172         iAnalyzeTool.Close();
       
   173         }
       
   174 
       
   175     // Check the flag
       
   176     if ( iDeviceDriverLoaded )
       
   177         {
       
   178         LOGSTR2( "ATMH device driver client count: %i", count().iClientCount );
       
   179 
       
   180         // Check if there is another user for device driver
       
   181         if ( count().iClientCount <= 1 )
       
   182             {
       
   183             // There was no other users -> unload the device driver
       
   184             TInt error = User::FreeLogicalDevice( KAnalyzeToolLddName );
       
   185             LOGSTR2( "ATMH Unloading ldd error: %i", error );
       
   186             }
       
   187         }
       
   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
       
   202     iThreadArray.Close();
       
   203 
       
   204     if ( iLogOption == EATLogToTraceFast )
       
   205         {
       
   206         LOGSTR1( "ATMH ATFastLogProcessEnded()" );
       
   207         ATFastLogProcessEnded( iProcessId, handleLeakCount );
       
   208         }
       
   209     else if ( iLogOption == EATLogToFile )
       
   210     	{
       
   211         iLogFile.ATFileLogProcessEnded( handleLeakCount );
       
   212     	}
       
   213     
       
   214 
       
   215     // Close the mutex
       
   216     iMutex.Close();
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // RAnalyzeToolMainAllocator::ShareHeap()
       
   221 // Share heap with other thread
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void RAnalyzeToolMainAllocator::ShareHeap()
       
   225     {
       
   226     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ShareHeap()" );
       
   227 
       
   228     // Call the overwrited Open function
       
   229     Open();
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // RAnalyzeToolMainAllocator::Uninstall()
       
   234 // Uninstalls the current allocator
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void RAnalyzeToolMainAllocator::Uninstall()
       
   238     {
       
   239     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Uninstall()" );
       
   240 
       
   241     // Acquire the mutex
       
   242     iMutex.Wait();
       
   243     
       
   244     TMainThreadParamsBuf params;
       
   245     params().iProcessId = iProcessId;
       
   246     iAnalyzeTool.MainThreadAlloctor( params );
       
   247     
       
   248     // Release the mutex
       
   249     iMutex.Signal();
       
   250     
       
   251     // Check if this is shared allocator between threads
       
   252     if ( iThreadArray.Count() > KThreadCount && !params().iAlone )
       
   253         {
       
   254         // Close the shared allocator
       
   255         Close();
       
   256         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Uninstall() - Close called" );
       
   257         return;
       
   258         }
       
   259 
       
   260 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_3 )
       
   261     #ifndef __WINS__ 
       
   262     // Remove dummy Tls handle
       
   263     UserSvr::DllFreeTls( KDummyHandle );
       
   264     #endif
       
   265 #endif
       
   266     
       
   267     // Since this is the last thread using this allocator it can be deleted
       
   268     delete this;
       
   269     }
       
   270 
       
   271 #ifdef __WINS__
       
   272 // -----------------------------------------------------------------------------
       
   273 // RAnalyzeToolMainAllocator::Alloc() WINS version
       
   274 // Allocates a cell of specified size from the heap.
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 UEXPORT_C TAny* RAnalyzeToolMainAllocator::Alloc( TInt aSize )
       
   278     {
       
   279     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()" );
       
   280 
       
   281     // Acquire the mutex
       
   282     iMutex.Wait();
       
   283     
       
   284     // get thread ID
       
   285     TUint threadId = RThread().Id();
       
   286 
       
   287     // Alloc memory from the original allocator
       
   288     TAny* p = iAllocator->Alloc( aSize );
       
   289 
       
   290     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
       
   291             aSize,  (TUint32) p );
       
   292 
       
   293 
       
   294 	TInt error( KErrNone );
       
   295 			
       
   296 	// Check if eventhandler is started already
       
   297 	if ( !iEventHandler->IsStarted() )
       
   298 		{
       
   299 		// Install the eventhandler if needed
       
   300 		InstallEventHandler();
       
   301 		}
       
   302 	
       
   303 	// Reset the callstack
       
   304 	iCallStack.Reset();
       
   305 
       
   306 	// If we don't want any call stack to be saved skip the next part
       
   307 	if( iAllocMaxCallStack > 0 )
       
   308 		{
       
   309 		// Find the current thread callstack start address
       
   310 		TUint32 stackstart( 0 );
       
   311 		TBool found( FindCurrentThreadStack( stackstart ) );
       
   312 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
       
   313 					
       
   314 		// Returns the value of the stack pointer at the 
       
   315 		// current point in your program.
       
   316 		TUint32 _sp;
       
   317 		__asm
       
   318 			{
       
   319 			mov [_sp], esp
       
   320 			}
       
   321 		
       
   322 		// Get process loaded code segments count
       
   323 		TInt blocksCount( iCodeblocks.Count() );
       
   324 		TUint arrayCounter = 0;
       
   325 		
       
   326 		// Iterate through callstack to find wanted callstack addresses
       
   327 		// - Start: current stack address
       
   328 		// - Stop: stack start address(Run-address of user stack)
       
   329 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
       
   330 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
       
   331 			{
       
   332 			TUint32 addr = (TUint32) *( (TUint32*) i );
       
   333 			
       
   334 			// Checks is the given address in loaded code memory area.
       
   335 			if ( !IsAddressLoadedCode( addr ) )
       
   336 				continue;
       
   337 			
       
   338 			// Iterate through array of code blocks to check if address is in code segment area 
       
   339 			for ( TInt j = 0; j < blocksCount; j++ )
       
   340 				{
       
   341 				// Checks if the given address is in this memory block area
       
   342 				if ( iCodeblocks[j].CheckAddress( addr ) )
       
   343 					{
       
   344 					// To avoid recursive call to ReAlloc specifying granularity
       
   345 					// Add address to the callstack
       
   346 					iCallStack[arrayCounter] = ( addr );
       
   347 					arrayCounter++;
       
   348 					break;
       
   349 					}
       
   350 				}
       
   351 			
       
   352 			// Checks if the wanted callstack items are gathered
       
   353 			if ( arrayCounter == KATMaxCallstackLength ||
       
   354 				 arrayCounter == iAllocMaxCallStack )
       
   355 				{
       
   356 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
       
   357 				break;
       
   358 				}
       
   359 			} 
       
   360 		}
       
   361 
       
   362 	// Log the memory allocation information
       
   363 	if ( iLogOption == EATLogToTraceFast )
       
   364 		{
       
   365 		// Using fast mode.
       
   366 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p , iCallStack, aSize, threadId );
       
   367 		} 
       
   368 	else if ( iLogOption == EATLogToFile )
       
   369 		{
       
   370 		iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId );
       
   371 		}
       
   372 	
       
   373     // Release the mutex
       
   374     iMutex.Signal();
       
   375 
       
   376     return p;
       
   377     }
       
   378 #else
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // RAnalyzeToolMainAllocator::Alloc() ARMV5 version
       
   382 // Allocates a cell of specified size from the heap.
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 TAny* RAnalyzeToolMainAllocator::Alloc( TInt aSize )
       
   386     {
       
   387     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc()"  );
       
   388 
       
   389     // acquire the mutex
       
   390     iMutex.Wait();
       
   391     
       
   392     // get thread ID
       
   393     TUint threadId = RThread().Id();
       
   394 
       
   395     // Alloc memory from the original allocator
       
   396     TAny* p = iAllocator->Alloc( aSize );
       
   397 
       
   398     LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", 
       
   399             aSize,  (TUint32) p );
       
   400 
       
   401 
       
   402 	// Check if eventhandler is active already
       
   403 	// IsActive might return false value if a tested software has created many
       
   404 	// threads which install own CActiveScheduler.
       
   405 	if ( !iEventHandler->IsStarted() )
       
   406 		{
       
   407 		// Install the eventhandler if needed
       
   408 		InstallEventHandler();
       
   409 		}
       
   410 	
       
   411 	// Reset the callstack
       
   412 	iCallStack.Reset();
       
   413 	
       
   414 	// If we don't want any call stack to be saved skip the next part
       
   415 	if( iAllocMaxCallStack > 0 )
       
   416 		{
       
   417 		// Find the current thread callstack start address
       
   418 		TUint32 stackstart( 0 );
       
   419 		TBool found( FindCurrentThreadStack( stackstart ) );
       
   420 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
       
   421 		
       
   422 		// Get process loaded code segments count
       
   423 		TInt blocksCount( iCodeblocks.Count() );            
       
   424 		TUint arrayCounter = 0;
       
   425 		
       
   426 		// Iterate through callstack to find wanted callstack addresses
       
   427 		// - Start: current stack address(__current_sp(): Returns the value of the 
       
   428 		//      stack pointer at the current point in your program.)
       
   429 		// - Stop: stack start address(Run-address of user stack)
       
   430 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
       
   431 		for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
       
   432 			{
       
   433 			TUint32 addr = (TUint32) *( (TUint32*) i );
       
   434 			
       
   435 			// Checks is the given address in loaded code memory area.
       
   436 			if ( !IsAddressLoadedCode( addr ) )
       
   437 				continue;
       
   438 			
       
   439 			// Iterate through array of code blocks to check if address is in code segment area 
       
   440 			for ( TInt j = 0; j < blocksCount; j++ )
       
   441 				{
       
   442 				// Checks if the given address is in this memory block area
       
   443 				if ( iCodeblocks[j].CheckAddress( addr ) )
       
   444 					{
       
   445 					// To avoid recursive call to ReAlloc specifying granularity
       
   446 					// Add address to the callstack
       
   447 					iCallStack[arrayCounter] = ( addr );
       
   448 					arrayCounter++;
       
   449 					break;
       
   450 					}
       
   451 				}
       
   452 			
       
   453 			// Checks if the wanted callstack items are gathered
       
   454 			if ( arrayCounter == KATMaxCallstackLength ||
       
   455 				 arrayCounter == iAllocMaxCallStack )
       
   456 				{
       
   457 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
       
   458 				break;
       
   459 				}
       
   460 			}
       
   461 		}
       
   462 	// Log the memory allocation information
       
   463 	if ( iLogOption == EATLogToTraceFast )
       
   464 		{
       
   465 		// Using fast mode.
       
   466 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
       
   467 		}
       
   468 	else if ( iLogOption == EATLogToFile )
       
   469 		{
       
   470 		iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId );
       
   471 		}
       
   472     
       
   473     // Release the mutex
       
   474     iMutex.Signal();
       
   475 
       
   476     // Return the allocatated memory
       
   477     return p;
       
   478     }
       
   479 #endif // __WINS__
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // RAnalyzeToolMainAllocator::Free()
       
   483 // Frees the allocated memory
       
   484 // -----------------------------------------------------------------------------
       
   485 //
       
   486 void RAnalyzeToolMainAllocator::Free( TAny* aPtr )
       
   487     {
       
   488     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Free()" );
       
   489 
       
   490     // Acquire the mutex
       
   491     iMutex.Wait();
       
   492     
       
   493     // get thread ID
       
   494     TUint threadId = RThread().Id();
       
   495 
       
   496 	// Reset the callstack
       
   497 	iFreeCallStack.Reset();
       
   498 	
       
   499 	// Check if trace logging mode
       
   500 	// Also if we don't want any call stack to be stored skip the next part
       
   501 	if ( iFreeMaxCallStack > 0 )
       
   502 		{
       
   503 		// Find the current thread callstack start address
       
   504 		TUint32 stackstart( 0 );
       
   505 		TBool found( FindCurrentThreadStack( stackstart ) );
       
   506 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
       
   507 		TUint32 _sp;
       
   508 		
       
   509 		// Returns the value of the stack pointer at the 
       
   510 		// current point in your program.
       
   511 		#ifdef __WINS__
       
   512 			__asm
       
   513 				{
       
   514 				mov [_sp], esp
       
   515 				}
       
   516 		#else
       
   517 			_sp = __current_sp();
       
   518 		#endif
       
   519 		
       
   520 		// Get process loaded code segments count
       
   521 		TInt blocksCount( iCodeblocks.Count() );
       
   522 		TUint arrayCounter = 0;
       
   523 		
       
   524 		// Iterate through callstack to find wanted callstack addresses
       
   525 		// - Start: current stack address
       
   526 		// - 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)            
       
   528 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
       
   529 			{
       
   530 			TUint32 addr = (TUint32) *( (TUint32*) i );
       
   531 			// Checks is the given address in loaded code memory area.
       
   532 			if ( ! IsAddressLoadedCode( addr ) )
       
   533 				continue;
       
   534 			
       
   535 			// Iterate through array of code blocks to check if address is in code segment area 
       
   536 			for ( TInt j = 0; j < blocksCount; j++ )
       
   537 				{
       
   538 				// Checks if the given address is in this memory block area
       
   539 				if ( iCodeblocks[j].CheckAddress( addr ) )
       
   540 					{
       
   541 					// To avoid recursive call to ReAlloc specifying granularity
       
   542 					// Add address to the callstack
       
   543 					iFreeCallStack[arrayCounter] = addr;
       
   544 					arrayCounter++;
       
   545 					break;
       
   546 					}
       
   547 				}
       
   548 			// Checks if the wanted callstack items are gathered
       
   549 			if ( arrayCounter == KATMaxFreeCallstackLength ||
       
   550 				 arrayCounter == iFreeMaxCallStack )
       
   551 				{
       
   552 				break;
       
   553 				}
       
   554 			}
       
   555 		LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
       
   556 		}
       
   557 	// Log the memory free information.
       
   558 	if ( iLogOption == EATLogToTraceFast )
       
   559 		{
       
   560 		// Using fast mode.
       
   561 		ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack, threadId );
       
   562 		}
       
   563     else if ( iLogOption == EATLogToFile )
       
   564     	{
       
   565         iLogFile.ATFileLogMemoryFreed( (TUint32) aPtr, iFreeCallStack, threadId );
       
   566     	}
       
   567      
       
   568     // Free the memory using original allocator
       
   569     iAllocator->Free( aPtr );
       
   570 
       
   571     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::Free() - aPtr: %x", (TUint32)aPtr );
       
   572 
       
   573     // Release the mutex
       
   574     iMutex.Signal();
       
   575     }
       
   576 
       
   577 // -----------------------------------------------------------------------------
       
   578 // RAnalyzeToolMainAllocator::Open()
       
   579 // Opens this heap for shared access. Opening the heap increases
       
   580 // the heap's access count by one.
       
   581 // -----------------------------------------------------------------------------
       
   582 //
       
   583 TInt RAnalyzeToolMainAllocator::Open()
       
   584     {
       
   585     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Open() " );
       
   586 
       
   587     // Acquire the mutex
       
   588     iMutex.Wait();
       
   589 
       
   590     // Share the memory using original allocator
       
   591     TInt error = iAllocator->Open();
       
   592 
       
   593     // If everything is OK add thread to the array which use this allocator
       
   594     if ( KErrNone == error )
       
   595         {
       
   596         TThreadParamsBuf params;
       
   597         params().iThreadId = RThread().Id().operator TUint();
       
   598         error = iAnalyzeTool.ThreadStack( params );
       
   599 
       
   600         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) );
       
   601 
       
   602         if ( KErrNone == error )
       
   603             {
       
   604             LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
       
   605             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
       
   606             iThreadArray.Append( TThreadStack( RThread().Id(),
       
   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             	}
       
   617             }
       
   618         }
       
   619 
       
   620     // Release the mutex
       
   621     iMutex.Signal();
       
   622 
       
   623     // Return the error code
       
   624     return error;
       
   625     }
       
   626 
       
   627 // -----------------------------------------------------------------------------
       
   628 // RAnalyzeToolMainAllocator::Close()
       
   629 // Closes this shared heap. Closing the heap decreases the heap's
       
   630 // access count by one.
       
   631 // -----------------------------------------------------------------------------
       
   632 //
       
   633 void RAnalyzeToolMainAllocator::Close()
       
   634     {
       
   635     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close()" );
       
   636 
       
   637     // Acquire the mutex
       
   638     iMutex.Wait();
       
   639 
       
   640     // Close the memory using original allocator
       
   641     iAllocator->Close();
       
   642     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - allocator closed" );
       
   643     TInt count = iThreadArray.Count();
       
   644 
       
   645     // Iterate through array of threads to remove current thread
       
   646     for ( TInt i = 0; i < count; i++ )
       
   647         {
       
   648         // Check if this is current thread
       
   649         if ( iThreadArray[ i ].Match() )
       
   650             {
       
   651             // Remove the thread
       
   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             	}
       
   662             LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - thread removed" );
       
   663             break;
       
   664             }
       
   665         }
       
   666     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - about to mutex signal" );
       
   667     // Release the mutex
       
   668     iMutex.Signal();
       
   669     }
       
   670 
       
   671 #ifdef __WINS__
       
   672 
       
   673 // -----------------------------------------------------------------------------
       
   674 // RAnalyzeToolMainAllocator::ReAlloc()
       
   675 // Increases or decreases the size of an existing cell.
       
   676 // -----------------------------------------------------------------------------
       
   677 //
       
   678 TAny* RAnalyzeToolMainAllocator::ReAlloc( TAny* aPtr, TInt aSize, TInt aMode )
       
   679     {
       
   680     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
       
   681 
       
   682     // Acquire the mutex
       
   683     iMutex.Wait();
       
   684     
       
   685     // get thread ID
       
   686     TUint threadId = RThread().Id();
       
   687 
       
   688     // Realloc the memory using original allocator
       
   689     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
       
   690     
       
   691     // NULL addresses are not in a process under test
       
   692     if ( ptr && !( aMode & ENeverMove ) )
       
   693         {
       
   694         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
       
   695                 (TUint32)aPtr, (TUint32)ptr );
       
   696         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
       
   697                 aSize, aMode );
       
   698       
       
   699 
       
   700 	TInt error( KErrNone );
       
   701 	TUint arrayCounter = 0;
       
   702 	
       
   703 	// Reset the callstack
       
   704 	iReCallStack.Reset();
       
   705 	
       
   706 	// If we don't want any call stack to be saved skip the next part
       
   707 	if( iAllocMaxCallStack > 0 )
       
   708 		{
       
   709 		// Find the current thread callstack start address
       
   710 		TUint32 stackstart( 0 );
       
   711 		TBool found( FindCurrentThreadStack( stackstart ) );
       
   712 		LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
       
   713 
       
   714 		// Returns the value of the stack pointer at the 
       
   715 		// current point in your program.
       
   716 		TUint32 _sp( 0 );
       
   717 		__asm
       
   718 			{
       
   719 			mov [_sp], esp
       
   720 			}
       
   721 		
       
   722 		// Get process loaded code segments count
       
   723 		TInt blocksCount( iCodeblocks.Count() );
       
   724 		
       
   725 		// Iterate through callstack to find wanted callstack addresses
       
   726 		// - Start: current stack address
       
   727 		// - Stop: stack start address(Run-address of user stack)
       
   728 		// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)                
       
   729 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
       
   730 			{
       
   731 			TUint32 addr = (TUint32) *( (TUint32*) i );
       
   732 			// Checks is the given address in loaded code memory area.
       
   733 			if ( ! IsAddressLoadedCode( addr ) )
       
   734 				continue;
       
   735 			
       
   736 			// Iterate through array of code blocks to check if address is in code segment area 
       
   737 			for ( TInt j = 0; j < blocksCount; j++ )
       
   738 				{
       
   739 				// Checks if the given address is in this memory block area
       
   740 				if ( iCodeblocks[j].CheckAddress( addr ) )
       
   741 					{
       
   742 					// To avoid recursive call to ReAlloc specifying granularity
       
   743 					// Add address to the callstack
       
   744 					iReCallStack[arrayCounter] = addr;
       
   745 					arrayCounter++;
       
   746 					break;
       
   747 					}
       
   748 				}
       
   749 			// Checks if the wanted callstack items are gathered
       
   750 			if ( arrayCounter == KATMaxCallstackLength || 
       
   751 				 arrayCounter == iAllocMaxCallStack )
       
   752 				{
       
   753 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
       
   754 				break;
       
   755 				}
       
   756 			}
       
   757 		}
       
   758 	
       
   759 	
       
   760 	// Log the memory reallocation information
       
   761 	if ( iLogOption == EATLogToTraceFast )
       
   762 		{
       
   763 		// Using fast logging mode.
       
   764 		ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
       
   765 		}
       
   766 	else if ( iLogOption == EATLogToFile )
       
   767 		{
       
   768 		iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
       
   769 		}
       
   770        
       
   771         }
       
   772     
       
   773     // Release the mutex
       
   774     iMutex.Signal();
       
   775 
       
   776     // Return pointer to the reallocated cell
       
   777     return ptr;
       
   778     }
       
   779 
       
   780 #else
       
   781 
       
   782 // -----------------------------------------------------------------------------
       
   783 // RAnalyzeToolMainAllocator::ReAlloc()
       
   784 // Increases or decreases the size of an existing cell.
       
   785 // -----------------------------------------------------------------------------
       
   786 //
       
   787 TAny* RAnalyzeToolMainAllocator::ReAlloc( TAny* aPtr, TInt aSize, TInt aMode )
       
   788     {
       
   789     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc()" );
       
   790 
       
   791     // Acquire the mutex
       
   792     iMutex.Wait();
       
   793 
       
   794     // get thread ID
       
   795     TUint threadId = RThread().Id();
       
   796     
       
   797     // Realloc the memory using original allocator
       
   798     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
       
   799     
       
   800     TUint arrayCounter = 0;
       
   801     
       
   802     // NULL addresses are not in a process under test
       
   803     if ( ptr && !( aMode & ENeverMove ) )
       
   804         {
       
   805         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
       
   806                 (TUint32)aPtr, (TUint32)ptr );
       
   807         LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", 
       
   808                 aSize, aMode );
       
   809  
       
   810 		// Reset the callstack
       
   811 		iReCallStack.Reset();
       
   812 		
       
   813 		// If we don't want any call stack to be saved skip the next part
       
   814 		if( iAllocMaxCallStack > 0 )
       
   815 			{
       
   816 			// Find the current thread callstack start address
       
   817 			TUint32 stackstart( 0 );
       
   818 			TBool found( FindCurrentThreadStack( stackstart ) );
       
   819 			LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found );
       
   820 			
       
   821 			// Get process loaded code segments count
       
   822 			TInt blocksCount( iCodeblocks.Count() );
       
   823 			
       
   824 			// Iterate through callstack to find wanted callstack addresses
       
   825 			// - Start: current stack address(__current_sp(): Returns the value of the 
       
   826 			//      stack pointer at the current point in your program.)
       
   827 			// - Stop: stack start address(Run-address of user stack)
       
   828 			// - Add: address length(The word size in the current system is 32 bits, which is 4 bytes)
       
   829 			for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
       
   830 				{
       
   831 				TUint32 addr = (TUint32) *( (TUint32*) i );
       
   832 				
       
   833 				// Checks is the given address in loaded code memory area.
       
   834 				if ( !IsAddressLoadedCode( addr ) )
       
   835 					continue;
       
   836 							
       
   837 				// Iterate through array of code blocks to check if address is in code segment area 
       
   838 				for ( TInt j = 0; j < blocksCount; j++ )
       
   839 					{
       
   840 					// Checks if the given address is in this memory block area
       
   841 					if ( iCodeblocks[j].CheckAddress( addr ) )
       
   842 						{
       
   843 						// To avoid recursive call to ReAlloc specifying granularity
       
   844 						// Add address to the callstack
       
   845 						iReCallStack[arrayCounter] = ( addr );
       
   846 						arrayCounter++;
       
   847 						break;
       
   848 						}
       
   849 					}
       
   850 				// Checks if the wanted callstack items are gathered
       
   851 				if ( arrayCounter == KATMaxCallstackLength || 
       
   852 					 arrayCounter == iAllocMaxCallStack )
       
   853 					{
       
   854 					LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
       
   855 					break;
       
   856 					}
       
   857 				}
       
   858 			}
       
   859 		
       
   860 		if ( iLogOption == EATLogToTraceFast )
       
   861 			{
       
   862 			// Using fast logging mode.
       
   863 			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
       
   864 			}
       
   865 		else if ( iLogOption == EATLogToFile )
       
   866 			{
       
   867 			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
       
   868 			} 
       
   869         }
       
   870 
       
   871     // Release the mutex
       
   872     iMutex.Signal();
       
   873 
       
   874     // Return pointer to the reallocated cell
       
   875     return ptr;
       
   876     }
       
   877 
       
   878 #endif // __WINS__
       
   879 
       
   880 // -----------------------------------------------------------------------------
       
   881 // RAnalyzeToolMainAllocator::Compress()
       
   882 // The function frees excess committed space from the top of the heap.
       
   883 // The size of the heap is never reduced below the minimum size
       
   884 // specified during creation of the heap.
       
   885 // -----------------------------------------------------------------------------
       
   886 //
       
   887 TInt RAnalyzeToolMainAllocator::Compress()
       
   888     {
       
   889     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Compress()" );
       
   890 
       
   891     // Acquire the mutex
       
   892     iMutex.Wait();
       
   893 
       
   894     // Compress the memory using original allocator
       
   895     TInt compress = iAllocator->Compress();
       
   896 
       
   897     // Release the mutex
       
   898     iMutex.Signal();
       
   899 
       
   900     // Return the space reclaimed
       
   901     return compress;
       
   902     }
       
   903 
       
   904 // -----------------------------------------------------------------------------
       
   905 // RAnalyzeToolMainAllocator::Reset()
       
   906 // Frees all allocated cells on this heap.
       
   907 // -----------------------------------------------------------------------------
       
   908 //
       
   909 void RAnalyzeToolMainAllocator::Reset()
       
   910     {
       
   911     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Reset()" );
       
   912 
       
   913     // Acquire the mutex
       
   914     iMutex.Wait();
       
   915 
       
   916     // Reset the memory using original allocator
       
   917     iAllocator->Reset();
       
   918 
       
   919     // Release the mutex
       
   920     iMutex.Signal();
       
   921     }
       
   922 
       
   923 // -----------------------------------------------------------------------------
       
   924 // RAnalyzeToolMainAllocator::AllocSize()
       
   925 // Gets the number of cells allocated on this heap, and
       
   926 // the total space allocated to them.
       
   927 // -----------------------------------------------------------------------------
       
   928 //
       
   929 TInt RAnalyzeToolMainAllocator::AllocSize( TInt& aTotalAllocSize ) const
       
   930     {
       
   931     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::AllocSize()" );
       
   932     
       
   933     // Acquire the mutex
       
   934     iMutex.Wait();
       
   935     
       
   936     // Acquire the memory information using original allocator
       
   937     TInt size = iAllocator->AllocSize( aTotalAllocSize );
       
   938     
       
   939     // Release the mutex
       
   940     iMutex.Signal();
       
   941     
       
   942     // Return the number of cells allocated on this heap.
       
   943     return size;
       
   944     }
       
   945 
       
   946 // -----------------------------------------------------------------------------
       
   947 // RAnalyzeToolMainAllocator::Available()
       
   948 // Gets the total free space currently available on the heap and the
       
   949 // space available in the largest free block. The space available
       
   950 // represents the total space which can be allocated. Note that
       
   951 // compressing the heap may reduce the total free space available
       
   952 // and the space available in the largest free block.
       
   953 // -----------------------------------------------------------------------------
       
   954 //
       
   955 TInt RAnalyzeToolMainAllocator::Available( TInt& aBiggestBlock ) const
       
   956     {
       
   957     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Available()" );
       
   958     
       
   959     // Acquire the mutex
       
   960     iMutex.Wait();
       
   961     
       
   962     // Acquire the memory information using original allocator
       
   963     TInt available = iAllocator->Available( aBiggestBlock );
       
   964     
       
   965     // Release the mutex
       
   966     iMutex.Signal();
       
   967     
       
   968     // Return the total free space currently available on the heap
       
   969     return available;
       
   970     }
       
   971 
       
   972 // -----------------------------------------------------------------------------
       
   973 // RAnalyzeToolMainAllocator::AllocLen()
       
   974 // Gets the length of the available space in the specified
       
   975 // allocated cell.
       
   976 // -----------------------------------------------------------------------------
       
   977 //
       
   978 TInt RAnalyzeToolMainAllocator::AllocLen( const TAny* aCell ) const
       
   979     {
       
   980     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::AllocLen()" );
       
   981     
       
   982     // Acquire the mutex
       
   983     iMutex.Wait();
       
   984     
       
   985     // Acquire the memory information using original allocator
       
   986     TInt len = iAllocator->AllocLen( aCell );
       
   987     
       
   988     // Release the mutex
       
   989     iMutex.Signal();
       
   990     
       
   991     // Return the length of the available space in the allocated cell.
       
   992     return len;
       
   993     }
       
   994 
       
   995 // -----------------------------------------------------------------------------
       
   996 // RAnalyzeToolMainAllocator::DebugFunction()
       
   997 // Invocates specified debug funtionality.
       
   998 // -----------------------------------------------------------------------------
       
   999 //
       
  1000 TInt RAnalyzeToolMainAllocator::DebugFunction( TInt aFunc, TAny* a1, TAny* a2 )
       
  1001     {
       
  1002     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::DebugFunction() %i", aFunc );
       
  1003     
       
  1004     // Acquire the mutex
       
  1005     iMutex.Wait();
       
  1006     
       
  1007     // Invocate debug funtion using original allocator
       
  1008     TInt debug = iAllocator->DebugFunction( aFunc, a1, a2 );
       
  1009     
       
  1010     switch( aFunc )
       
  1011         {  
       
  1012         case EMarkEnd:
       
  1013             {
       
  1014             // Disables the __UHEAP_MARKEND macro
       
  1015             LOGSTR1( "ATMH __UHEAP_MARKEND macro called" );
       
  1016             if ( debug > 0 )
       
  1017                 {
       
  1018                 LOGSTR2( "ATMH __UHEAP_MARKEND detects leaks: %d", debug );
       
  1019                 // Because there is leaks the alloc panic will occur but
       
  1020                 // lets return a zero to pretend that everything is OK
       
  1021                 debug = 0;
       
  1022                 }
       
  1023             }
       
  1024         break;
       
  1025         
       
  1026         default:
       
  1027             {
       
  1028             }
       
  1029         break;
       
  1030         }
       
  1031     
       
  1032     // Release the mutex
       
  1033     iMutex.Signal();
       
  1034     
       
  1035     // Return information of the debug function success
       
  1036     return debug;
       
  1037     }
       
  1038 
       
  1039 // -----------------------------------------------------------------------------
       
  1040 // RAnalyzeToolMainAllocator::RemoveKilledThread()
       
  1041 // Remove killed thread from threads array.
       
  1042 // -----------------------------------------------------------------------------
       
  1043 //
       
  1044 void RAnalyzeToolMainAllocator::RemoveKilledThread( const TUint aThreadId  )
       
  1045     {
       
  1046     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::RemoveKilledThread(%i)", 
       
  1047             aThreadId );
       
  1048     
       
  1049     // Acquire the mutex
       
  1050     iMutex.Wait();
       
  1051     
       
  1052     // Iterate through array of threads to remove current thread
       
  1053     TInt count( iThreadArray.Count() );
       
  1054     LOGSTR2( "ATMH > iThreadArray.Count() %i", count );
       
  1055     
       
  1056     for ( TInt i = 0; i < count; i++ )
       
  1057         {
       
  1058         // Check if this is current thread
       
  1059         if ( iThreadArray[ i ].Match( aThreadId ) )
       
  1060             {
       
  1061             // Remove the thread
       
  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 				}
       
  1072             LOGSTR1( "ATMH > thread removed" );
       
  1073             break;
       
  1074             }
       
  1075         }
       
  1076 
       
  1077     // Release the mutex
       
  1078     iMutex.Signal();
       
  1079     }
       
  1080 // -----------------------------------------------------------------------------
       
  1081 // RAnalyzeToolMainAllocator::Extension_()
       
  1082 // Extension function
       
  1083 // -----------------------------------------------------------------------------
       
  1084 //
       
  1085 TInt RAnalyzeToolMainAllocator::Extension_( TUint aExtensionId, TAny*& a0,
       
  1086     TAny* a1)
       
  1087     {
       
  1088     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Extension_()" );
       
  1089     
       
  1090     // Acquire the mutex
       
  1091     iMutex.Wait();
       
  1092     
       
  1093     // Invocate extension funtion using original allocator
       
  1094     TInt ext = RAllocator::Extension_( aExtensionId, a0, a1 );
       
  1095     
       
  1096     // Release the mutex
       
  1097     iMutex.Signal();
       
  1098     
       
  1099     // Return information of the extension function success
       
  1100     return ext;
       
  1101     }
       
  1102 
       
  1103 // -----------------------------------------------------------------------------
       
  1104 // RAnalyzeToolMainAllocator::LogProcessInformation()
       
  1105 // Retrieve and log the process initial information
       
  1106 // -----------------------------------------------------------------------------
       
  1107 //
       
  1108 void RAnalyzeToolMainAllocator::LogProcessInformation( const TFileName& aFileName, const TPath& aFilePath,
       
  1109     TUint32 aLogOption, TUint32 aIsDebug, const TDesC8& aAtoolVersion, const TDesC8& aApiVersion )
       
  1110     {
       
  1111     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation()" );
       
  1112     
       
  1113     // Create local variable and retrieve the process information
       
  1114     TProcessIdentityParamsBuf params;
       
  1115     params().iProcessId = iProcessId;
       
  1116     params().iThreadId = RThread().Id().operator TUint();
       
  1117     TInt error = iAnalyzeTool.GetProcessInfo( params );
       
  1118     
       
  1119     LOGSTR2( "ATMH GetProcessInfo %i error", error );
       
  1120     
       
  1121     if ( KErrNone == error )
       
  1122         {
       
  1123         LOGSTR2( "ATMH Process %i", iProcessId );
       
  1124 
       
  1125         // Store stack start address
       
  1126         LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
       
  1127         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
       
  1128 
       
  1129         // Append thread to array of the users of this allocator
       
  1130         error = iThreadArray.Append(
       
  1131         TThreadStack( RThread().Id(), params().iStackAddress + params().iStackSize) );
       
  1132         
       
  1133 
       
  1134         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) );
       
  1135         
       
  1136         // Using fast logging mode.
       
  1137         if ( iLogOption == EATLogToTraceFast )
       
  1138             {          
       
  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 )
       
  1152                 {
       
  1153                 // TODO
       
  1154                 }
       
  1155             
       
  1156             //if everything is ok, add file version in the begining of file
       
  1157             iLogFile.ATFileLogVersion();
       
  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 );
       
  1165 
       
  1166         // Iterate through process codesegments
       
  1167         for( TInt i = 0; i < params().iCodesegCount; i++ )
       
  1168             {
       
  1169             // Create local variable and retrieve codesegment info
       
  1170             TCodesegInfoBuf codeinfo;
       
  1171             codeinfo().iProcessId = iProcessId;
       
  1172             codeinfo().iIndex = i;
       
  1173             error = iAnalyzeTool.GetCodesegInfo( codeinfo );
       
  1174 
       
  1175             LOGSTR2( "ATMH GetCodesegInfo error %i", error );
       
  1176             if ( KErrNone == error )
       
  1177                 {
       
  1178                 // Don't log AnalyzeTool libraries
       
  1179                 if ( 0 != codeinfo().iFullName.CompareC( KMemoryHook ) )
       
  1180                     {
       
  1181                     // Log the loaded codesegment(s)
       
  1182 
       
  1183                     if ( iLogOption == EATLogToTraceFast )
       
  1184                         {
       
  1185                         // Using fast logging mode.
       
  1186                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogDllLoaded() #2" );
       
  1187                         ATFastLogDllLoaded( iProcessId,
       
  1188                                 codeinfo().iFullName,
       
  1189                                 codeinfo().iRunAddress,
       
  1190                                 codeinfo().iRunAddress + codeinfo().iSize );
       
  1191                         }
       
  1192                     else if ( iLogOption == EATLogToFile )
       
  1193 						{
       
  1194                         iLogFile.ATFileLogDllLoaded( 
       
  1195 								codeinfo().iFullName,
       
  1196                                 codeinfo().iRunAddress,
       
  1197                                 codeinfo().iRunAddress + codeinfo().iSize );
       
  1198 						}
       
  1199                     
       
  1200 
       
  1201                     LOGSTR2( "ATMH LogDllLoaded error %i", error );
       
  1202 
       
  1203                     // Check that everything is OK
       
  1204                     if ( KErrNone == error )
       
  1205                         {
       
  1206                         // Append the codesegment to the array
       
  1207                         error = iCodeblocks.Append(
       
  1208                                         TCodeblock( codeinfo().iRunAddress,
       
  1209                                         codeinfo().iSize,
       
  1210                                         codeinfo().iFullName ) );
       
  1211                         LOGSTR2( "ATMH Append error %i", error );
       
  1212                         }
       
  1213                     }
       
  1214                 }
       
  1215             }
       
  1216 
       
  1217         // Iterate through process dynamic codesegments
       
  1218         for ( TInt i = 0; i < params().iDynamicCount; i++ )
       
  1219             {
       
  1220             // Create local variable and retrieve codesegment info
       
  1221             TLibraryInfoBuf info;
       
  1222             info().iProcessId = iProcessId;
       
  1223             info().iIndex = i;
       
  1224             error = iAnalyzeTool.GetLibraryInfo( info );
       
  1225             LOGSTR2( "ATMH GetLibraryInfo error %i", error );
       
  1226             if ( KErrNone == error )
       
  1227                 {
       
  1228                 // Log the loaded dynamic codesegment(s)
       
  1229 
       
  1230                 if ( iLogOption == EATLogToTraceFast )
       
  1231                     {
       
  1232                     // Using fast logging mode.
       
  1233                     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - - ATFastLogDllLoaded()#3" );
       
  1234                     ATFastLogDllLoaded( iProcessId,
       
  1235                             info().iLibraryName,
       
  1236                             info().iRunAddress,
       
  1237                             info().iRunAddress + info().iSize );
       
  1238                     }
       
  1239                 else if ( iLogOption == EATLogToFile )
       
  1240                 	{
       
  1241                 	iLogFile.ATFileLogDllLoaded( 
       
  1242                 			info().iLibraryName,
       
  1243                 			info().iRunAddress,
       
  1244                 			info().iRunAddress + info().iSize );
       
  1245                 	}
       
  1246      
       
  1247                 LOGSTR2( "ATMH LogDllLoaded error %i", error );
       
  1248 
       
  1249                 if ( KErrNone == error )
       
  1250                     {
       
  1251                     // Append the codesegment to the array
       
  1252                     error = iCodeblocks.Append(
       
  1253                             TCodeblock( info().iRunAddress, 
       
  1254                                         info().iSize, info().iLibraryName ) );
       
  1255                     LOGSTR2( "ATMH Append error %i", error );
       
  1256                     }
       
  1257                 }
       
  1258             }
       
  1259         }
       
  1260     }
       
  1261 
       
  1262 
       
  1263 // -----------------------------------------------------------------------------
       
  1264 // RAnalyzeToolMainAllocator::FindCurrentThreadStack()
       
  1265 // Find the current thread which is using the heap
       
  1266 // -----------------------------------------------------------------------------
       
  1267 //
       
  1268 TBool RAnalyzeToolMainAllocator::FindCurrentThreadStack( TUint32& aStackStart )
       
  1269     {
       
  1270     LOGSTR2( "ATMH RAnalyzeToolMainAllocator::FindCurrentThreadStack(), count( %i )",
       
  1271             iThreadArray.Count() );
       
  1272     
       
  1273     // Flag for indicating that right thread has been found
       
  1274     TBool found( EFalse );
       
  1275     // If threre is only one thread it must be the right thread
       
  1276     if ( iThreadArray.Count() == KThreadCount )
       
  1277         {
       
  1278         if ( !iThreadArray[ 0 ].ThreadStackStart( aStackStart ) )
       
  1279             {
       
  1280             // This MUST BE the right thread
       
  1281             //__ASSERT_ALWAYS( EFalse, AssertPanic( ECantFindRightThread ) );
       
  1282             }
       
  1283         else if ( iThreadArray[ 0 ].ThreadStackStart( aStackStart ) )
       
  1284             {
       
  1285             found = ETrue;
       
  1286             }
       
  1287         }
       
  1288     else
       
  1289         {
       
  1290         // Iterate through array to find right thread
       
  1291         TInt count = iThreadArray.Count();
       
  1292 
       
  1293         for ( TInt i = 0; i < count; i++ )
       
  1294             {
       
  1295             // Check if this is the right thread
       
  1296             if ( iThreadArray[ i ].ThreadStackStart( aStackStart ) )
       
  1297                 {
       
  1298                 // Right thread found. Mark the flag
       
  1299                 found = ETrue;
       
  1300                 break;
       
  1301                 }
       
  1302             }
       
  1303         // If right thread was not found the panic must be raised
       
  1304         if ( !found )
       
  1305             {
       
  1306             //__ASSERT_ALWAYS( EFalse, AssertPanic( ECantFindRightThread ) );
       
  1307             }
       
  1308         }
       
  1309     return found;
       
  1310     }
       
  1311 
       
  1312 // -----------------------------------------------------------------------------
       
  1313 // RAnalyzeToolMainAllocator::InstallEventHandler()
       
  1314 // Installs the eventhandler, if possible
       
  1315 // -----------------------------------------------------------------------------
       
  1316 //
       
  1317 void RAnalyzeToolMainAllocator::InstallEventHandler()
       
  1318     {
       
  1319     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::InstallEventHandler()" );
       
  1320     
       
  1321     // Active eventhalder is not active, trying to start it
       
  1322     if ( NULL != CActiveScheduler::Current() )
       
  1323         {
       
  1324         iEventHandler->Start();
       
  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     
       
  1379     }
       
  1380 
       
  1381 // End of File