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