analyzetool/dynamicmemoryhook/src/analyzetoolallocator.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 49 7fdc9a71d314
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
    35 // RAnalyzeToolAllocator::RAnalyzeToolAllocator()
    35 // RAnalyzeToolAllocator::RAnalyzeToolAllocator()
    36 // C++ default constructor can NOT contain any code, that
    36 // C++ default constructor can NOT contain any code, that
    37 // might leave.
    37 // might leave.
    38 // -----------------------------------------------------------------------------
    38 // -----------------------------------------------------------------------------
    39 //
    39 //
    40 RAnalyzeToolAllocator::RAnalyzeToolAllocator( TBool aNotFirst,
    40 RAnalyzeToolAllocator::RAnalyzeToolAllocator( TBool aNotFirst, 
       
    41                                               RATStorageServer& aStorageServer, 
    41                                               RArray<TCodeblock>& aCodeblocks, 
    42                                               RArray<TCodeblock>& aCodeblocks, 
    42                                               RMutex& aMutex,
    43                                               RMutex& aMutex,
    43                                               TUint aProcessId,
    44                                               TUint aProcessId,
    44                                               RAnalyzeTool& aAnalyzeTool,
    45                                               RAnalyzeTool& aAnalyzeTool,
       
    46                                               TBool aStorageServerOpen,
    45                                               TUint32 aLogOption,
    47                                               TUint32 aLogOption,
    46                                               TUint32 aAllocCallStackSize,
    48                                               TUint32 aAllocCallStackSize,
    47                                               TUint32 aFreeCallStackSize,
    49                                               TUint32 aFreeCallStackSize ) :
    48                                               RATFileLog& aLogFile ) :
       
    49     RAnalyzeToolMemoryAllocator( aNotFirst ),
    50     RAnalyzeToolMemoryAllocator( aNotFirst ),
       
    51     iStorageServer( aStorageServer ), 
    50     iCodeblocks( aCodeblocks ), 
    52     iCodeblocks( aCodeblocks ), 
    51     iMutex( aMutex ),
    53     iMutex( aMutex ),
    52     iProcessId( aProcessId ),
    54     iProcessId( aProcessId ),
    53     iThreadArray( KATMaxCallstackLength ),
    55     iThreadArray( KATMaxCallstackLength ),
    54     iAnalyzeTool( aAnalyzeTool ),
    56     iAnalyzeTool( aAnalyzeTool ),
       
    57     iStorageServerOpen( aStorageServerOpen ),
    55     iLogOption( aLogOption ),
    58     iLogOption( aLogOption ),
    56     iAllocMaxCallStack( aAllocCallStackSize ),
    59     iAllocMaxCallStack( aAllocCallStackSize ),
    57     iFreeMaxCallStack( aFreeCallStackSize ),
    60     iFreeMaxCallStack( aFreeCallStackSize )
    58     iLogFile ( aLogFile )
       
    59     {
    61     {
    60     LOGSTR1( "ATMH RAnalyzeToolAllocator::RAnalyzeToolAllocator()" );
    62     LOGSTR1( "ATMH RAnalyzeToolAllocator::RAnalyzeToolAllocator()" );
    61     
    63     
    62     // Append thread to array of the users of this allocator
    64     // Append thread to array of the users of this allocator
    63     TThreadParamsBuf params;
    65     TThreadParamsBuf params;
    67         {
    69         {
    68         LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
    70         LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
    69         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
    71         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
    70         error = iThreadArray.Append( TThreadStack( RThread().Id(), 
    72         error = iThreadArray.Append( TThreadStack( RThread().Id(), 
    71                              params().iStackAddress + params().iStackSize ) );
    73                              params().iStackAddress + params().iStackSize ) );
    72 
       
    73         if ( iLogOption == EATLogToTraceFast )
       
    74         	{
       
    75             // log thread added
       
    76             ATFastLogThreadStarted( aProcessId, RThread().Id().operator TUint() ); 
       
    77         	}
       
    78         else if ( iLogOption == EATLogToFile )
       
    79         	{
       
    80             iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
       
    81         	}
       
    82         }
    74         }
    83     
    75     
    84     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) ); 
    76     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) ); 
    85     }
    77     }
    86 
    78 
    90 // -----------------------------------------------------------------------------
    82 // -----------------------------------------------------------------------------
    91 //
    83 //
    92 RAnalyzeToolAllocator::~RAnalyzeToolAllocator()
    84 RAnalyzeToolAllocator::~RAnalyzeToolAllocator()
    93     {
    85     {
    94     LOGSTR1( "ATMH RAnalyzeToolAllocator::~RAnalyzeToolAllocator()" );
    86     LOGSTR1( "ATMH RAnalyzeToolAllocator::~RAnalyzeToolAllocator()" );
    95     
       
    96     for( TInt i=0; i<iThreadArray.Count(); i++)
       
    97     	{
       
    98         // log thread removed
       
    99         if ( iLogOption == EATLogToTraceFast )
       
   100         	{
       
   101             ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
       
   102             }
       
   103         else if ( iLogOption == EATLogToFile )
       
   104         	{
       
   105             iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );
       
   106         	}
       
   107          }
       
   108     
       
   109     
    87     
   110     // Close the thread array 
    88     // Close the thread array 
   111     iThreadArray.Close();
    89     iThreadArray.Close();
   112     }
    90     }
   113     
    91     
   154     LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc()" );
   132     LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc()" );
   155     
   133     
   156     // Acquire the mutex
   134     // Acquire the mutex
   157     iMutex.Wait();
   135     iMutex.Wait();
   158     
   136     
   159     // get thread ID
       
   160     TUint threadId = RThread().Id();
       
   161     // Alloc memory from the original allocator
   137     // Alloc memory from the original allocator
   162     TAny* p = iAllocator->Alloc( aSize );
   138     TAny* p = iAllocator->Alloc( aSize );
   163     
   139     
   164     LOGSTR3( "ATMH RAnalyzeToolAllocator::Alloc() - aSize: %i, address: %x", 
   140     LOGSTR3( "ATMH RAnalyzeToolAllocator::Alloc() - aSize: %i, address: %x", 
   165              aSize,  (TUint32) p );
   141              aSize,  (TUint32) p );
   166     
   142     
   167 	// Reset the callstack
   143     // Don't collect call stack and log data
   168 	iCallStack.Reset();
   144     // if storage server not open or logging mode not fast.
   169 
   145     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   170 	// Find the current thread callstack start address
   146         {
   171 	TUint32 stackstart( 0 );
   147         // Reset the callstack
   172 	TBool found( FindCurrentThreadStack( stackstart ) );
   148         iCallStack.Reset();
   173 	LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   149     
   174 	
   150         // Find the current thread callstack start address
   175 	TUint32 _sp;
   151         TUint32 stackstart( 0 );
   176 	__asm
   152         TBool found( FindCurrentThreadStack( stackstart ) );
   177 		{
   153         LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   178 		mov [_sp], esp
   154         
   179 		}
   155         TUint32 _sp;
   180 	
   156         __asm
   181 	// Get codeblocks count
   157             {
   182 	TInt blocksCount( iCodeblocks.Count() );
   158             mov [_sp], esp
   183 	TInt error( KErrNone );
   159             }
   184 	TUint arrayCounter = 0;
   160         
   185 	
   161         // Get codeblocks count
   186 	for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   162         TInt blocksCount( iCodeblocks.Count() );
   187 		{
   163         TInt error( KErrNone );
   188 		TUint32 addr = (TUint32) *( (TUint32*) i );
   164         TUint arrayCounter = 0;
   189 		if ( ! IsAddressLoadedCode( addr ) )
   165         
   190 			continue;
   166         for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   191 		for ( TInt j = 0; j < blocksCount; j++ )
   167             {
   192 			{
   168             TUint32 addr = (TUint32) *( (TUint32*) i );
   193 			if ( iCodeblocks[j].CheckAddress( addr ) )
   169             if ( ! IsAddressLoadedCode( addr ) )
   194 				{
   170                 continue;
   195 				// To avoid recursive call to ReAlloc specifying granularity
   171             for ( TInt j = 0; j < blocksCount; j++ )
   196 				// Add address to the callstack
   172                 {
   197 				iCallStack[arrayCounter] = ( addr );
   173                 if ( iCodeblocks[j].CheckAddress( addr ) )
   198 				arrayCounter++;
   174                     {
   199 				break;
   175                     // To avoid recursive call to ReAlloc specifying granularity
   200 				}
   176                     // Add address to the callstack
   201 			}
   177                     iCallStack[arrayCounter] = ( addr );
   202 		if ( arrayCounter == KATMaxCallstackLength ||
   178                     arrayCounter++;
   203 			 arrayCounter == iAllocMaxCallStack )
   179                     break;
   204 			{
   180                     }
   205 			LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   181                 }
   206 			break;
   182             if ( arrayCounter == KATMaxCallstackLength ||
   207 			}
   183                  arrayCounter == iAllocMaxCallStack )
   208 		}
   184                 {
   209 	// Log the memory allocation information
   185                 LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   210 	if ( iLogOption == EATLogToTraceFast )
   186                 break;
   211 		{
   187                 }
   212 		// Using fast mode.
   188             }
   213 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
   189         // Log the memory allocation information
   214 		}
   190         if ( iLogOption == EATLogToTraceFast )
   215 	else if ( iLogOption == EATLogToFile )
   191             {
   216 		{
   192             // Using fast mode.
   217 		iLogFile.ATFileLogMemoryAllocated( (TUint32) p, iCallStack, aSize, threadId );
   193             ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize );
   218 		}
   194             }
   219    
   195         else
       
   196             {
       
   197             // Using storage server.
       
   198             error = iStorageServer.LogMemoryAllocated( (TUint32) p,
       
   199                                                        iCallStack,
       
   200                                                        aSize );
       
   201             if ( KErrNone != error )
       
   202                 {
       
   203                 LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
   204                 switch ( error )
       
   205                     {
       
   206                     case KErrNoMemory:
       
   207                     LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case"  );
       
   208                     if ( iStorageServerOpen )
       
   209                         {
       
   210                         iStorageServerOpen = EFalse;
       
   211                         LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer"  );
       
   212                         iStorageServer.Close();
       
   213                         }
       
   214                     break;
       
   215                     }
       
   216                 }
       
   217             }
       
   218         }
   220     // Release the mutex
   219     // Release the mutex
   221     iMutex.Signal();
   220     iMutex.Signal();
   222     
   221     
   223     return p;
   222     return p;
   224     }
   223     }
   234     LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc()" );
   233     LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc()" );
   235     
   234     
   236     // Acquire the mutex
   235     // Acquire the mutex
   237     iMutex.Wait();
   236     iMutex.Wait();
   238     
   237     
   239     // get thread ID
       
   240     TUint threadId = RThread().Id();
       
   241         
       
   242     // Alloc memory from the original allocator
   238     // Alloc memory from the original allocator
   243     TAny* p = iAllocator->Alloc( aSize );
   239     TAny* p = iAllocator->Alloc( aSize );
       
   240     
       
   241     // Don't collect call stack and log data
       
   242     // if storage server not open or logging mode not fast.
       
   243     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
       
   244         {
       
   245         // Reset the callstack
       
   246         iCallStack.Reset(); 
   244         
   247         
   245 
   248         // Find the current thread callstack start address
   246 	// Reset the callstack
   249         TUint32 stackstart( 0 );
   247 	iCallStack.Reset(); 
   250         TBool found( FindCurrentThreadStack( stackstart ) );
   248 	
   251         LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   249 	// Find the current thread callstack start address
   252         
   250 	TUint32 stackstart( 0 );
   253         // Get codeblocks count
   251 	TBool found( FindCurrentThreadStack( stackstart ) );
   254         TInt blocksCount( iCodeblocks.Count() );
   252 	LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   255         TInt error( KErrNone );
   253 	
   256         TUint arrayCounter = 0;
   254 	// Get codeblocks count
   257         
   255 	TInt blocksCount( iCodeblocks.Count() );
   258         for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   256 	TUint arrayCounter = 0;
   259             {
   257 	
   260             TUint32 addr = (TUint32) *( (TUint32*) i );
   258 	for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   261             if ( ! IsAddressLoadedCode( addr ) )
   259 		{
   262                 continue;
   260 		TUint32 addr = (TUint32) *( (TUint32*) i );
   263             for ( TInt j = 0; j < blocksCount; j++ )
   261 		if ( ! IsAddressLoadedCode( addr ) )
   264                 {
   262 			continue;
   265                 if ( iCodeblocks[j].CheckAddress( addr ) )
   263 		for ( TInt j = 0; j < blocksCount; j++ )
   266                     {
   264 			{
   267                     // To avoid recursive call to ReAlloc specifying granularity
   265 			if ( iCodeblocks[j].CheckAddress( addr ) )
   268                     // Add address to the callstack
   266 				{
   269                     iCallStack[arrayCounter] = ( addr );
   267 				// To avoid recursive call to ReAlloc specifying granularity
   270                     arrayCounter++;
   268 				// Add address to the callstack
   271                     break;
   269 				iCallStack[arrayCounter] = ( addr );
   272                     }
   270 				arrayCounter++;
   273                 }
   271 				break;
   274             if ( arrayCounter == KATMaxCallstackLength ||
   272 				}
   275                  arrayCounter == iAllocMaxCallStack )
   273 			}
   276                 {
   274 		if ( arrayCounter == KATMaxCallstackLength ||
   277                 LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   275 			 arrayCounter == iAllocMaxCallStack )
   278                 break;
   276 			{
   279                 }
   277 			LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   280             }
   278 			break;
   281         // Log the memory allocation information
   279 			}
   282         if ( iLogOption == EATLogToTraceFast )
   280 		}
   283             {
   281 	// Log the memory allocation information
   284             // Fast mode.
   282 	if ( iLogOption == EATLogToTraceFast )
   285             ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize );
   283 		{
   286             }
   284 		// Fast mode.
   287         else
   285 		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
   288             {
   286 		} 
   289             // Using storage server.
   287 	else if ( iLogOption == EATLogToFile )
   290             error = iStorageServer.LogMemoryAllocated( (TUint32) p, 
   288 		{
   291                                                         iCallStack, 
   289 	    iLogFile.ATFileLogMemoryAllocated( (TUint32) p, iCallStack, aSize, threadId );
   292                                                         aSize );
   290 		}
   293             if ( KErrNone != error )
   291 
   294                 {
       
   295                 LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
       
   296                 switch ( error )
       
   297                     {
       
   298                     case KErrNoMemory:
       
   299                     LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc() - KErrNoMemory case"  );
       
   300                     if ( iStorageServerOpen )
       
   301                         {
       
   302                         iStorageServerOpen = EFalse;
       
   303                         LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc() - close iStorageServer"  );
       
   304                         iStorageServer.Close();
       
   305                         }
       
   306                     break;
       
   307                     }
       
   308                 }
       
   309             }
       
   310         }
   292  
   311  
   293     // Release the mutex
   312     // Release the mutex
   294     iMutex.Signal(); 
   313     iMutex.Signal(); 
   295     
   314     
   296     // Return the allocatated memory
   315     // Return the allocatated memory
   308     LOGSTR1( "ATMH RAnalyzeToolAllocator::Free()" );
   327     LOGSTR1( "ATMH RAnalyzeToolAllocator::Free()" );
   309 
   328 
   310     // Acquire the mutex
   329     // Acquire the mutex
   311     iMutex.Wait();
   330     iMutex.Wait();
   312     
   331     
   313     // get thread ID
   332     // Don't collect or log data if storage server not open or logging mode not fast.
   314     TUint threadId = RThread().Id();
   333     if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   315     
   334         {
   316 	// Reset the callstack
   335         // Reset the callstack
   317 	iFreeCallStack.Reset();
   336         iFreeCallStack.Reset();
   318 	
   337         
   319 	if ( iFreeMaxCallStack > 0 )
   338         // Check if trace logging mode because free call stack is not used in other log options.
   320 		{
   339         if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
   321 		// Find the current thread callstack start address
   340                 && iFreeMaxCallStack > 0 )
   322 		TUint32 stackstart( 0 );
   341             {
   323 		TBool found( FindCurrentThreadStack( stackstart ) );
   342             // Find the current thread callstack start address
   324 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   343             TUint32 stackstart( 0 );
   325 		TUint32 _sp;
   344             TBool found( FindCurrentThreadStack( stackstart ) );
   326 		
   345             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   327 		#ifdef __WINS__
   346             TUint32 _sp;
   328 			__asm
   347             
   329 				{
   348             #ifdef __WINS__
   330 				mov [_sp], esp
   349                 __asm
   331 				}
   350                     {
   332 		#else
   351                     mov [_sp], esp
   333 			_sp = __current_sp();
   352                     }
   334 		#endif
   353             #else
   335 		
   354                 _sp = __current_sp();
   336 		// Get codeblocks count
   355             #endif
   337 		TInt blocksCount( iCodeblocks.Count() );
   356             
   338 		TUint arrayCounter = 0;
   357             // Get codeblocks count
   339 	
   358             TInt blocksCount( iCodeblocks.Count() );
   340 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   359             TUint arrayCounter = 0;
   341 			{
   360         
   342 			TUint32 addr = (TUint32) *( (TUint32*) i );
   361             for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   343 			if ( ! IsAddressLoadedCode( addr ) )
   362                 {
   344 				continue;
   363                 TUint32 addr = (TUint32) *( (TUint32*) i );
   345 			for ( TInt j = 0; j < blocksCount; j++ )
   364                 if ( ! IsAddressLoadedCode( addr ) )
   346 				{
   365                     continue;
   347 				if ( iCodeblocks[j].CheckAddress( addr ) )
   366                 for ( TInt j = 0; j < blocksCount; j++ )
   348 					{
   367                     {
   349 					// To avoid recursive call to ReAlloc specifying granularity
   368                     if ( iCodeblocks[j].CheckAddress( addr ) )
   350 					// Add address to the callstack
   369                         {
   351 					iFreeCallStack[arrayCounter] = addr;
   370                         // To avoid recursive call to ReAlloc specifying granularity
   352 					arrayCounter++;
   371                         // Add address to the callstack
   353 					break;
   372                         iFreeCallStack[arrayCounter] = addr;
   354 					}
   373                         arrayCounter++;
   355 				}
   374                         break;
   356 			if ( arrayCounter == KATMaxFreeCallstackLength ||
   375                         }
   357 				 arrayCounter == iFreeMaxCallStack )
   376                     }
   358 				{
   377                 if ( arrayCounter == KATMaxFreeCallstackLength ||
   359 				break;
   378                      arrayCounter == iFreeMaxCallStack )
   360 				}
   379                     {
   361 			}
   380                     break;
   362 		LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
   381                     }
   363 		}
   382                 }
   364 	
   383             LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
   365 	// Log freed memory.
   384             }
   366 	if ( iLogOption == EATLogToTraceFast )
   385         
   367 		{
   386         // Log freed memory.
   368 		// Using fast mode.
   387         if ( iLogOption == EATLogToTraceFast )
   369 		ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack, threadId );
   388             {
   370 		} 
   389             // Using fast mode.
   371 	else if ( iLogOption == EATLogToFile )
   390             ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
   372 		{
   391             }
   373 		iLogFile.ATFileLogMemoryFreed( (TUint32) aPtr, iFreeCallStack, threadId );
   392         else
   374 		}
   393             {
       
   394             // Using storage server.
       
   395             TInt err( iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ) );
       
   396             if ( err != KErrNone )
       
   397                 {
       
   398                 LOGSTR2( "ATMH > LogMemoryFreed err( %i )", err );
       
   399                 }
       
   400             }
       
   401         }
   375     
   402     
   376     // Free the memory using original allocator
   403     // Free the memory using original allocator
   377     iAllocator->Free( aPtr ); 
   404     iAllocator->Free( aPtr ); 
   378     
   405     
   379     LOGSTR2( "ATMH RAnalyzeToolAllocator::Free() - aPtr: %x", (TUint32)aPtr );
   406     LOGSTR2( "ATMH RAnalyzeToolAllocator::Free() - aPtr: %x", (TUint32)aPtr );
   411             {
   438             {
   412             LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
   439             LOGSTR2( "ATMH Thread stack address: %x", params().iStackAddress );
   413             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
   440             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
   414             iThreadArray.Append( TThreadStack( RThread().Id(), 
   441             iThreadArray.Append( TThreadStack( RThread().Id(), 
   415                     params().iStackAddress + params().iStackSize ) );
   442                     params().iStackAddress + params().iStackSize ) );
   416             if ( iLogOption == EATLogToTraceFast )
       
   417             	{
       
   418                 // log thread added
       
   419                 ATFastLogThreadStarted( RProcess().Id().operator TUint(), RThread().Id().operator TUint()); 
       
   420             	}
       
   421             else if ( iLogOption == EATLogToFile )
       
   422 				{
       
   423 				iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
       
   424 				}
       
   425             }
   443             }
   426         }
   444         }
   427     
   445     
   428     // Release the mutex
   446     // Release the mutex
   429     iMutex.Signal();
   447     iMutex.Signal();
   456         // Check if this is current thread
   474         // Check if this is current thread
   457         if ( iThreadArray[ i ].Match() )
   475         if ( iThreadArray[ i ].Match() )
   458             {
   476             {
   459             // Remove the thread
   477             // Remove the thread
   460             iThreadArray.Remove( i );
   478             iThreadArray.Remove( i );
   461             if ( iLogOption == EATLogToTraceFast )
       
   462             	{
       
   463                 // log thread removed
       
   464                 ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
       
   465             	}
       
   466             else if ( iLogOption == EATLogToFile )
       
   467 				{
       
   468 				iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );
       
   469 				}
       
   470             
       
   471             break;
   479             break;
   472             }
   480             }
   473         }
   481         }
   474     
   482     
   475     // Release the mutex
   483     // Release the mutex
   487     {
   495     {
   488     LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc()" );
   496     LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc()" );
   489 
   497 
   490     // Acquire the mutex
   498     // Acquire the mutex
   491     iMutex.Wait();
   499     iMutex.Wait();
   492     
       
   493     // get thread ID
       
   494     TUint threadId = RThread().Id();
       
   495 
   500 
   496     // Realloc the memory using original allocator
   501     // Realloc the memory using original allocator
   497     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   502     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   498     
   503     
   499     // NULL addresses are not in a process under test
   504     // NULL addresses are not in a process under test
   502         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   507         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   503                 (TUint32)aPtr, (TUint32)ptr );
   508                 (TUint32)aPtr, (TUint32)ptr );
   504         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   509         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   505                 aSize, aMode );
   510                 aSize, aMode );
   506 
   511 
   507 
   512         // Don't collect or log data if storage server not open or logging mode fast.
   508 		// Reset the callstack
   513         if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   509 		iReCallStack.Reset(); 
   514             {
   510 
   515             // Reset the callstack
   511 		// Find the current thread callstack start address
   516             iReCallStack.Reset(); 
   512 		TUint32 stackstart( 0 ); 
   517 
   513 		TBool found( FindCurrentThreadStack( stackstart ) );
   518             // Find the current thread callstack start address
   514 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   519             TUint32 stackstart( 0 ); 
   515 		
   520             TBool found( FindCurrentThreadStack( stackstart ) );
   516 		// Get current sp
   521             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   517 		TUint32 _sp( 0 );
   522             
   518 		__asm
   523             // Get current sp
   519 			{
   524             TUint32 _sp( 0 );
   520 			mov [_sp], esp
   525             __asm
   521 			}
   526                 {
   522 		
   527                 mov [_sp], esp
   523 		// Get codeblocks count
   528                 }
   524 		TInt blocksCount( iCodeblocks.Count() );
   529             
   525 		TInt error( KErrNone );
   530             // Get codeblocks count
   526 		TUint arrayCounter = 0;
   531             TInt blocksCount( iCodeblocks.Count() );
   527 		
   532             TInt error( KErrNone );
   528 		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   533             TUint arrayCounter = 0;
   529 			{
   534             
   530 			TUint32 addr = (TUint32) *( (TUint32*) i );
   535             for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   531 			if ( ! IsAddressLoadedCode( addr ) )
   536                 {
   532 				continue;
   537                 TUint32 addr = (TUint32) *( (TUint32*) i );
   533 			for ( TInt j = 0; j < blocksCount; j++ )
   538                 if ( ! IsAddressLoadedCode( addr ) )
   534 				{
   539                     continue;
   535 				if ( iCodeblocks[j].CheckAddress( addr ) )
   540                 for ( TInt j = 0; j < blocksCount; j++ )
   536 					{
   541                     {
   537 					// To avoid recursive call to ReAlloc specifying granularity
   542                     if ( iCodeblocks[j].CheckAddress( addr ) )
   538 					// Add address to the callstack
   543                         {
   539 					iReCallStack[arrayCounter] = addr;
   544                         // To avoid recursive call to ReAlloc specifying granularity
   540 					arrayCounter++;
   545                         // Add address to the callstack
   541 					break;
   546                         iReCallStack[arrayCounter] = addr;
   542 					}
   547                         arrayCounter++;
   543 				}
   548                         break;
   544 			if ( arrayCounter == KATMaxCallstackLength || 
   549                         }
   545 				 arrayCounter == iAllocMaxCallStack )
   550                     }
   546 				{
   551                 if ( arrayCounter == KATMaxCallstackLength || 
   547 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   552                      arrayCounter == iAllocMaxCallStack )
   548 				break;
   553                     {
   549 				}
   554                     LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   550 			}
   555                     break;
   551 
   556                     }
   552 		if ( iLogOption == EATLogToTraceFast )
   557                 }
   553 			{
   558             
   554 			// Using fast logging mode.
   559             // No need to report free if the aPtr was NULL
   555 			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   560             if ( aPtr != NULL )
   556 			}
   561                 {
   557 		else if ( iLogOption == EATLogToFile )
   562                 // Reset the free callstack
   558 			{
   563                 iFreeCallStack.Reset();
   559 			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   564                 
   560 			}	
   565                 // if trace logging mode(s) we also log call stack in free.
   561 		}
   566                 if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
       
   567                         && iFreeMaxCallStack > 0 )
       
   568                     {
       
   569                     for ( TInt i = 0; i < arrayCounter; i++ )
       
   570                         {
       
   571                         if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack )
       
   572                             {
       
   573                             break;
       
   574                             }
       
   575                         iFreeCallStack[i] = iReCallStack[i];
       
   576                         }
       
   577                     }
       
   578                 
       
   579                 // Try to remove old address from the storage server's
       
   580                 // leak array. If found it's removed from the array because system frees
       
   581                 // old address directly in the RHeap in ReAlloc case.
       
   582                 if ( iLogOption == EATLogToTraceFast )
       
   583                     {
       
   584                     ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
       
   585                     }
       
   586                 else
       
   587                     {
       
   588                     iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
       
   589                     }
       
   590                 }
       
   591             
       
   592             // Log the memory allocation information
       
   593             if ( iLogOption == EATLogToTraceFast )
       
   594                 {
       
   595                 // Using fast mode.
       
   596                 ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iFreeCallStack, aSize);
       
   597                 }
       
   598             else
       
   599                 {
       
   600                 // Using storage server.
       
   601                 error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, 
       
   602                                                             iReCallStack, 
       
   603                                                             aSize );
       
   604                 if ( KErrNone != error )
       
   605                     {
       
   606                     LOGSTR2( "ATMH LogMemoryAllocated ReAlloc error %i", error );
       
   607                     switch ( error )
       
   608                         {
       
   609                         case KErrNoMemory:
       
   610                         LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - KErrNoMemory case"  );
       
   611                         if ( iStorageServerOpen )
       
   612                             {
       
   613                             iStorageServerOpen = EFalse;
       
   614                             LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - close iStorageServer"  );
       
   615                             iStorageServer.Close();
       
   616                             }
       
   617                         break;
       
   618                         }
       
   619                     }
       
   620                 }
       
   621             }
       
   622         }
   562     
   623     
   563     // Release the mutex
   624     // Release the mutex
   564     iMutex.Signal();
   625     iMutex.Signal();
   565 
   626 
   566     // Return pointer to the reallocated cell
   627     // Return pointer to the reallocated cell
   578     {
   639     {
   579     LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc()" );
   640     LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc()" );
   580 
   641 
   581     // Acquire the mutex
   642     // Acquire the mutex
   582     iMutex.Wait();
   643     iMutex.Wait();
   583     
       
   584     // get thread ID
       
   585     TUint threadId = RThread().Id();
       
   586 
   644 
   587     // Realloc the memory using original allocator
   645     // Realloc the memory using original allocator
   588     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   646     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
   589 
   647 
   590     // NULL addresses are not in a process under test
   648     // NULL addresses are not in a process under test
   593         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   651         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aPtr: %x, ptr: %x", 
   594                 (TUint32)aPtr, (TUint32)ptr );
   652                 (TUint32)aPtr, (TUint32)ptr );
   595         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   653         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
   596                 aSize, aMode );
   654                 aSize, aMode );
   597 
   655 
   598 		// Reset the callstack
   656         // Don't collect or log data if storage server not open or logging mode fast.
   599 		iReCallStack.Reset(); 
   657         if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
   600 
   658             {
   601 		// Find the current thread callstack start address
   659             // Reset the callstack
   602 		TUint32 stackstart( 0 ); 
   660             iReCallStack.Reset(); 
   603 		TBool found( FindCurrentThreadStack( stackstart ) );
   661 
   604 		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   662             // Find the current thread callstack start address
   605 		
   663             TUint32 stackstart( 0 ); 
   606 		// Get codeblocks count
   664             TBool found( FindCurrentThreadStack( stackstart ) );
   607 		TInt blocksCount( iCodeblocks.Count() );
   665             LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
   608 
   666             
   609 		TUint arrayCounter = 0;
   667             // Get codeblocks count
   610 		
   668             TInt blocksCount( iCodeblocks.Count() );
   611 		for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   669             TInt error( KErrNone );
   612 			{
   670             TUint arrayCounter = 0;
   613 			TUint32 addr = (TUint32) *( (TUint32*) i );
   671             
   614 			if ( ! IsAddressLoadedCode( addr ) )
   672             for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
   615 				continue;
   673                 {
   616 			for ( TInt j = 0; j < blocksCount; j++ )
   674                 TUint32 addr = (TUint32) *( (TUint32*) i );
   617 				{
   675                 if ( ! IsAddressLoadedCode( addr ) )
   618 				if ( iCodeblocks[j].CheckAddress( addr ) )
   676                     continue;
   619 					{
   677                 for ( TInt j = 0; j < blocksCount; j++ )
   620 					// To avoid recursive call to ReAlloc specifying granularity
   678                     {
   621 					// Add address to the callstack
   679                     if ( iCodeblocks[j].CheckAddress( addr ) )
   622 					iReCallStack[arrayCounter] = ( addr );
   680                         {
   623 					arrayCounter++;
   681                         // To avoid recursive call to ReAlloc specifying granularity
   624 					break;
   682                         // Add address to the callstack
   625 					}
   683                         iReCallStack[arrayCounter] = ( addr );
   626 				}
   684                         arrayCounter++;
   627 			if ( arrayCounter == KATMaxCallstackLength || 
   685                         break;
   628 				 arrayCounter == iAllocMaxCallStack )
   686                         }
   629 				{
   687                     }
   630 				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   688                 if ( arrayCounter == KATMaxCallstackLength || 
   631 				break;
   689                      arrayCounter == iAllocMaxCallStack )
   632 				}
   690                     {
   633 			}
   691                     LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
   634 
   692                     break;
   635 		if ( iLogOption == EATLogToTraceFast )
   693                     }
   636 			{
   694                 }
   637 			// Using fast logging mode.
   695             
   638 			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   696             // No need to report free if the aPtr was NULL
   639 			}
   697             if ( aPtr != NULL )
   640 		else if ( iLogOption == EATLogToFile )
   698                 {
   641 			{
   699                 // Reset the free callstack
   642 			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
   700                 iFreeCallStack.Reset();
   643 			}
   701                 
   644 		
   702                 // if trace logging mode(s) we also log call stack with free.
   645 		
   703                 if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
   646 		}
   704                         && iFreeMaxCallStack > 0 )
       
   705                     {
       
   706                     for ( TInt i = 0; i < arrayCounter; i++ )
       
   707                         {
       
   708                         if ( i == iFreeCallStack.Count() )
       
   709                             {
       
   710                             break;
       
   711                             }
       
   712                         iFreeCallStack[i] = iReCallStack[i];
       
   713                         }
       
   714                     }
       
   715                 
       
   716                 // Try to remove old address from the storage server's
       
   717                 // leak array. If found it's removed from the array because system frees
       
   718                 // old address directly in the RHeap in ReAlloc case.
       
   719                 if ( iLogOption == EATLogToTraceFast )
       
   720                     {
       
   721                     ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
       
   722                     }
       
   723                 else
       
   724                     {
       
   725                     iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
       
   726                     }
       
   727                 }
       
   728         
       
   729             // Log the memory allocation information
       
   730             if ( iLogOption == EATLogToTraceFast )
       
   731                 {
       
   732                 // Using fast mode.
       
   733                 ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize );
       
   734                 }
       
   735             else
       
   736                 {
       
   737                 // Using storage server.
       
   738                 error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, 
       
   739                                                             iReCallStack, 
       
   740                                                             aSize );
       
   741                 if ( KErrNone != error )
       
   742                     {
       
   743                     LOGSTR2( "ATMH LogMemoryAllocated ReAlloc error %i", error );
       
   744                     switch ( error )
       
   745                         {
       
   746                         case KErrNoMemory:
       
   747                         LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - KErrNoMemory case"  );
       
   748                         if ( iStorageServerOpen )
       
   749                             {
       
   750                             iStorageServerOpen = EFalse;
       
   751                             LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - close iStorageServer"  );
       
   752                             iStorageServer.Close();
       
   753                             }
       
   754                         break;
       
   755                         }
       
   756                     }
       
   757                 }
       
   758             }
       
   759         }
   647 
   760 
   648     // Release the mutex
   761     // Release the mutex
   649     iMutex.Signal();
   762     iMutex.Signal();
   650 
   763 
   651     // Return pointer to the reallocated cell
   764     // Return pointer to the reallocated cell