diff -r 7fdc9a71d314 -r 8ad140f3dd41 analyzetool/dynamicmemoryhook/src/analyzetoolmainallocator.cpp --- a/analyzetool/dynamicmemoryhook/src/analyzetoolmainallocator.cpp Wed Sep 15 13:53:27 2010 +0300 +++ b/analyzetool/dynamicmemoryhook/src/analyzetoolmainallocator.cpp Wed Oct 13 16:17:58 2010 +0300 @@ -22,12 +22,7 @@ #include "analyzetoolmemoryallocator.h" #include "analyzetoolpanics.pan" #include "analyzetoolfastlog.h" -#include "analyzetoolfilelog.h" #include -#ifndef __WINSCW__ -#include -#endif -#include // CONSTANTS @@ -35,19 +30,15 @@ // The name of the memoryhook dll _LIT8( KMemoryHook, "AToolMemoryHook.dll" ); +// The name of the storage server dll +_LIT8( KStorageServer, "AToolStorageServerClnt.dll" ); + // Length of the callstack address const TUint32 KAddressLength = 4; // Thread count const TInt KThreadCount = 1; -// separator that replaces \n character in sw version -_LIT( KSeparator, "@" ); -// new line character -_LIT( KNewLine16, "\n" ); -_LIT( KSpace16, " " ); - - // ----------------------------------------------------------------------------- // RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator() // C++ default constructor can NOT contain any code, that @@ -55,9 +46,8 @@ // ----------------------------------------------------------------------------- // RAnalyzeToolMainAllocator::RAnalyzeToolMainAllocator( TBool aNotFirst, - const TFileName& aFileName, const TPath& aFilePath, TUint32 aLogOption, TUint32 aIsDebug, - TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize, - const TDesC8& aAtoolVersion, const TDesC8& aApiVersion ) : + const TFileName aFileName, TUint32 aLogOption, TUint32 aIsDebug, + TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize ) : RAnalyzeToolMemoryAllocator( aNotFirst ), iAnalyzeToolOpen( EFalse ), iDeviceDriverLoaded( EFalse ), @@ -72,7 +62,32 @@ // Basic error variable used in method. TInt error( KErrNone ); - + + // Connect to the storage server if logging mode not fast trace. + if ( iLogOption != EATLogToTraceFast ) + { + error = iStorageServer.Connect(); + + LOGSTR2( "ATMH Opening RATStorageServer error %i", error ); + + if ( KErrNone == error ) + { + iStorageServerOpen = ETrue; + } + else + { + iStorageServerOpen = EFalse; + } + + if ( KErrNone == error ) + { + // Make the storage server handle shared between threads + error = iStorageServer.ShareAuto(); + } + + LOGSTR2( "ATMH Sharing RATStorageServer error %i", error ); + } + // Create mutex for schedule access to shared resources error = iMutex.CreateLocal(); @@ -120,19 +135,16 @@ } // Retrieve the initial process information - LogProcessInformation( aFileName, aFilePath, aLogOption, aIsDebug, aAtoolVersion, aApiVersion ); - - // log version of ATApp, ATApi, S60 version and ROM checksum - LogDeviceInfo(); + LogProcessInformation( aFileName, aLogOption, aIsDebug ); // Create handler for receiving kernel events iEventHandler = new CLibraryEventHandler( iAnalyzeTool, iCodeblocks, + iStorageServer, iProcessId, iMutex, *this, - aLogOption, - iLogFile ); + aLogOption); __ASSERT_ALWAYS( iEventHandler != NULL, AssertPanic( ENoMemory ) ); } @@ -185,33 +197,28 @@ LOGSTR2( "ATMH Unloading ldd error: %i", error ); } } - for( TInt i=0; iAlloc( aSize ); @@ -290,86 +294,120 @@ LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", aSize, (TUint32) p ); - - TInt error( KErrNone ); - - // Check if eventhandler is started already - if ( !iEventHandler->IsStarted() ) - { - // Install the eventhandler if needed - InstallEventHandler(); - } - - // Reset the callstack - iCallStack.Reset(); + // Don't collect or log data if storage server not open or logging mode fast. + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + TInt error( KErrNone ); + + // Check if eventhandler is started already + if ( !iEventHandler->IsStarted() ) + { + // Install the eventhandler if needed + InstallEventHandler(); + } + + // Reset the callstack + iCallStack.Reset(); - // If we don't want any call stack to be saved skip the next part - if( iAllocMaxCallStack > 0 ) - { - // Find the current thread callstack start address - TUint32 stackstart( 0 ); - TBool found( FindCurrentThreadStack( stackstart ) ); - LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); - - // Returns the value of the stack pointer at the - // current point in your program. - TUint32 _sp; - __asm - { - mov [_sp], esp - } - - // Get process loaded code segments count - TInt blocksCount( iCodeblocks.Count() ); - TUint arrayCounter = 0; - - // Iterate through callstack to find wanted callstack addresses - // - Start: current stack address - // - Stop: stack start address(Run-address of user stack) - // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) - for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 - { - TUint32 addr = (TUint32) *( (TUint32*) i ); - - // Checks is the given address in loaded code memory area. - if ( !IsAddressLoadedCode( addr ) ) - continue; - - // Iterate through array of code blocks to check if address is in code segment area - for ( TInt j = 0; j < blocksCount; j++ ) - { - // Checks if the given address is in this memory block area - if ( iCodeblocks[j].CheckAddress( addr ) ) - { - // To avoid recursive call to ReAlloc specifying granularity - // Add address to the callstack - iCallStack[arrayCounter] = ( addr ); - arrayCounter++; - break; - } - } - - // Checks if the wanted callstack items are gathered - if ( arrayCounter == KATMaxCallstackLength || - arrayCounter == iAllocMaxCallStack ) - { - LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); - break; - } - } - } + // If we don't want any call stack to be saved skip the next part + if( iAllocMaxCallStack > 0 ) + { + // Find the current thread callstack start address + TUint32 stackstart( 0 ); + TBool found( FindCurrentThreadStack( stackstart ) ); + LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); + + // Returns the value of the stack pointer at the + // current point in your program. + TUint32 _sp; + __asm + { + mov [_sp], esp + } + + // Get process loaded code segments count + TInt blocksCount( iCodeblocks.Count() ); + TUint arrayCounter = 0; + + // Iterate through callstack to find wanted callstack addresses + // - Start: current stack address + // - Stop: stack start address(Run-address of user stack) + // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) + for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 + { + TUint32 addr = (TUint32) *( (TUint32*) i ); + + // Checks is the given address in loaded code memory area. + if ( !IsAddressLoadedCode( addr ) ) + continue; + + // Iterate through array of code blocks to check if address is in code segment area + for ( TInt j = 0; j < blocksCount; j++ ) + { + // Checks if the given address is in this memory block area + if ( iCodeblocks[j].CheckAddress( addr ) ) + { + // To avoid recursive call to ReAlloc specifying granularity + // Add address to the callstack + iCallStack[arrayCounter] = ( addr ); + arrayCounter++; + break; + } + } + + // Checks if the wanted callstack items are gathered + if ( arrayCounter == KATMaxCallstackLength || + arrayCounter == iAllocMaxCallStack ) + { + LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); + break; + } + } + } - // Log the memory allocation information - if ( iLogOption == EATLogToTraceFast ) - { - // Using fast mode. - ATFastLogMemoryAllocated( iProcessId, (TUint32) p , iCallStack, aSize, threadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId ); - } - + // Log the memory allocation information + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast mode. + ATFastLogMemoryAllocated( iProcessId, (TUint32) p , iCallStack, aSize ); + } + else + { + // Using storage server. + error = iStorageServer.LogMemoryAllocated( (TUint32) p, + iCallStack, + aSize ); + if ( KErrNone != error ) + { + switch ( error ) + { + case KErrNoMemory: + { + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case" ); + // Check if eventhandler is active + if ( iEventHandler->IsActive() ) + { + // Cancel iEventHandler because not needed anymore + iEventHandler->Cancel(); + } + if ( iStorageServerOpen ) + { + // Close storage server + iStorageServerOpen = EFalse; + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer" ); + iStorageServer.Close(); + } + break; + } + default: + { + LOGSTR2( "ATMH LogMemoryAllocated error %i", error ); + break; + } + } + } + } + } // Release the mutex iMutex.Signal(); @@ -388,9 +426,6 @@ // acquire the mutex iMutex.Wait(); - - // get thread ID - TUint threadId = RThread().Id(); // Alloc memory from the original allocator TAny* p = iAllocator->Alloc( aSize ); @@ -398,77 +433,113 @@ LOGSTR3( "ATMH RAnalyzeToolMainAllocator::Alloc() - aSize: %i, address: %x", aSize, (TUint32) p ); - - // Check if eventhandler is active already - // IsActive might return false value if a tested software has created many - // threads which install own CActiveScheduler. - if ( !iEventHandler->IsStarted() ) - { - // Install the eventhandler if needed - InstallEventHandler(); - } - - // Reset the callstack - iCallStack.Reset(); - - // If we don't want any call stack to be saved skip the next part - if( iAllocMaxCallStack > 0 ) - { - // Find the current thread callstack start address - TUint32 stackstart( 0 ); - TBool found( FindCurrentThreadStack( stackstart ) ); - LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); - - // Get process loaded code segments count - TInt blocksCount( iCodeblocks.Count() ); - TUint arrayCounter = 0; - - // Iterate through callstack to find wanted callstack addresses - // - Start: current stack address(__current_sp(): Returns the value of the - // stack pointer at the current point in your program.) - // - Stop: stack start address(Run-address of user stack) - // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) - for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 - { - TUint32 addr = (TUint32) *( (TUint32*) i ); - - // Checks is the given address in loaded code memory area. - if ( !IsAddressLoadedCode( addr ) ) - continue; - - // Iterate through array of code blocks to check if address is in code segment area - for ( TInt j = 0; j < blocksCount; j++ ) - { - // Checks if the given address is in this memory block area - if ( iCodeblocks[j].CheckAddress( addr ) ) - { - // To avoid recursive call to ReAlloc specifying granularity - // Add address to the callstack - iCallStack[arrayCounter] = ( addr ); - arrayCounter++; - break; - } - } - - // Checks if the wanted callstack items are gathered - if ( arrayCounter == KATMaxCallstackLength || - arrayCounter == iAllocMaxCallStack ) - { - LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); - break; - } - } - } - // Log the memory allocation information - if ( iLogOption == EATLogToTraceFast ) - { - // Using fast mode. - ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogMemoryAllocated( (TUint32) p , iCallStack, aSize, threadId ); - } + TInt error( KErrNone ); + + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + // Check if eventhandler is active already + // IsActive might return false value if a tested software has created many + // threads which install own CActiveScheduler. + if ( !iEventHandler->IsStarted() ) + { + // Install the eventhandler if needed + InstallEventHandler(); + } + + // Reset the callstack + iCallStack.Reset(); + + // If we don't want any call stack to be saved skip the next part + if( iAllocMaxCallStack > 0 ) + { + // Find the current thread callstack start address + TUint32 stackstart( 0 ); + TBool found( FindCurrentThreadStack( stackstart ) ); + LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); + + // Get process loaded code segments count + TInt blocksCount( iCodeblocks.Count() ); + TUint arrayCounter = 0; + + // Iterate through callstack to find wanted callstack addresses + // - Start: current stack address(__current_sp(): Returns the value of the + // stack pointer at the current point in your program.) + // - Stop: stack start address(Run-address of user stack) + // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) + for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 + { + TUint32 addr = (TUint32) *( (TUint32*) i ); + + // Checks is the given address in loaded code memory area. + if ( !IsAddressLoadedCode( addr ) ) + continue; + + // Iterate through array of code blocks to check if address is in code segment area + for ( TInt j = 0; j < blocksCount; j++ ) + { + // Checks if the given address is in this memory block area + if ( iCodeblocks[j].CheckAddress( addr ) ) + { + // To avoid recursive call to ReAlloc specifying granularity + // Add address to the callstack + iCallStack[arrayCounter] = ( addr ); + arrayCounter++; + break; + } + } + + // Checks if the wanted callstack items are gathered + if ( arrayCounter == KATMaxCallstackLength || + arrayCounter == iAllocMaxCallStack ) + { + LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); + break; + } + } + } + // Log the memory allocation information + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast mode. + ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize ); + } + else + { + // Using storage server. + error = iStorageServer.LogMemoryAllocated( (TUint32) p, + iCallStack, + aSize ); + if ( KErrNone != error ) + { + switch ( error ) + { + case KErrNoMemory: + { + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case" ); + // Check if eventhandler is active + if ( iEventHandler->IsActive() ) + { + // Cancel ieventhandler because not needed anymore + iEventHandler->Cancel(); + } + if ( iStorageServerOpen ) + { + // Close storage server + iStorageServerOpen = EFalse; + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer" ); + iStorageServer.Close(); + } + break; + } + default: + { + LOGSTR2( "ATMH LogMemoryAllocated error %i", error ); + break; + } + } + } + } + } // Release the mutex iMutex.Signal(); @@ -490,81 +561,87 @@ // Acquire the mutex iMutex.Wait(); - // get thread ID - TUint threadId = RThread().Id(); - - // Reset the callstack - iFreeCallStack.Reset(); - - // Check if trace logging mode - // Also if we don't want any call stack to be stored skip the next part - if ( iFreeMaxCallStack > 0 ) - { - // Find the current thread callstack start address - TUint32 stackstart( 0 ); - TBool found( FindCurrentThreadStack( stackstart ) ); - LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); - TUint32 _sp; - - // Returns the value of the stack pointer at the - // current point in your program. - #ifdef __WINS__ - __asm - { - mov [_sp], esp - } - #else - _sp = __current_sp(); - #endif - - // Get process loaded code segments count - TInt blocksCount( iCodeblocks.Count() ); - TUint arrayCounter = 0; - - // Iterate through callstack to find wanted callstack addresses - // - Start: current stack address - // - Stop: stack start address(Run-address of user stack) - // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) - for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 - { - TUint32 addr = (TUint32) *( (TUint32*) i ); - // Checks is the given address in loaded code memory area. - if ( ! IsAddressLoadedCode( addr ) ) - continue; - - // Iterate through array of code blocks to check if address is in code segment area - for ( TInt j = 0; j < blocksCount; j++ ) - { - // Checks if the given address is in this memory block area - if ( iCodeblocks[j].CheckAddress( addr ) ) - { - // To avoid recursive call to ReAlloc specifying granularity - // Add address to the callstack - iFreeCallStack[arrayCounter] = addr; - arrayCounter++; - break; - } - } - // Checks if the wanted callstack items are gathered - if ( arrayCounter == KATMaxFreeCallstackLength || - arrayCounter == iFreeMaxCallStack ) - { - break; - } - } - LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter ); - } - // Log the memory free information. - if ( iLogOption == EATLogToTraceFast ) - { - // Using fast mode. - ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack, threadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogMemoryFreed( (TUint32) aPtr, iFreeCallStack, threadId ); - } - + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + // Reset the callstack + iFreeCallStack.Reset(); + + // Check if trace logging mode + // Also if we don't want any call stack to be stored skip the next part + if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast ) + && iFreeMaxCallStack > 0 ) + { + // Find the current thread callstack start address + TUint32 stackstart( 0 ); + TBool found( FindCurrentThreadStack( stackstart ) ); + LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found ); + TUint32 _sp; + + // Returns the value of the stack pointer at the + // current point in your program. + #ifdef __WINS__ + __asm + { + mov [_sp], esp + } + #else + _sp = __current_sp(); + #endif + + // Get process loaded code segments count + TInt blocksCount( iCodeblocks.Count() ); + TUint arrayCounter = 0; + + // Iterate through callstack to find wanted callstack addresses + // - Start: current stack address + // - Stop: stack start address(Run-address of user stack) + // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) + for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 + { + TUint32 addr = (TUint32) *( (TUint32*) i ); + // Checks is the given address in loaded code memory area. + if ( ! IsAddressLoadedCode( addr ) ) + continue; + + // Iterate through array of code blocks to check if address is in code segment area + for ( TInt j = 0; j < blocksCount; j++ ) + { + // Checks if the given address is in this memory block area + if ( iCodeblocks[j].CheckAddress( addr ) ) + { + // To avoid recursive call to ReAlloc specifying granularity + // Add address to the callstack + iFreeCallStack[arrayCounter] = addr; + arrayCounter++; + break; + } + } + // Checks if the wanted callstack items are gathered + if ( arrayCounter == KATMaxFreeCallstackLength || + arrayCounter == iFreeMaxCallStack ) + { + break; + } + } + LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter ); + } + // Log the memory free information. + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast mode. + ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack ); + } + else + { + // Using storage server. + TInt err( iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ) ); + if ( err != KErrNone ) + { + LOGSTR2( "ATMH > LogMemoryFreed err( %i )", err ); + } + } + } + // Free the memory using original allocator iAllocator->Free( aPtr ); @@ -605,15 +682,6 @@ LOGSTR2( "ATMH Thread stack size: %x", params().iStackSize ); iThreadArray.Append( TThreadStack( RThread().Id(), params().iStackAddress + params().iStackSize ) ); - if ( iLogOption == EATLogToTraceFast ) - { - // log thread added - ATFastLogThreadStarted( RProcess().Id().operator TUint() , RThread().Id().operator TUint() ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() ); - } } } @@ -650,15 +718,6 @@ { // Remove the thread iThreadArray.Remove( i ); - if ( iLogOption == EATLogToTraceFast ) - { - // log thread removed - ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() ); - } LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Close() - thread removed" ); break; } @@ -681,9 +740,6 @@ // Acquire the mutex iMutex.Wait(); - - // get thread ID - TUint threadId = RThread().Id(); // Realloc the memory using original allocator TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode ); @@ -696,78 +752,142 @@ LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", aSize, aMode ); - - TInt error( KErrNone ); - TUint arrayCounter = 0; - - // Reset the callstack - iReCallStack.Reset(); - - // If we don't want any call stack to be saved skip the next part - if( iAllocMaxCallStack > 0 ) - { - // Find the current thread callstack start address - TUint32 stackstart( 0 ); - TBool found( FindCurrentThreadStack( stackstart ) ); - LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found ); - - // Returns the value of the stack pointer at the - // current point in your program. - TUint32 _sp( 0 ); - __asm - { - mov [_sp], esp - } - - // Get process loaded code segments count - TInt blocksCount( iCodeblocks.Count() ); - - // Iterate through callstack to find wanted callstack addresses - // - Start: current stack address - // - Stop: stack start address(Run-address of user stack) - // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) - for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 - { - TUint32 addr = (TUint32) *( (TUint32*) i ); - // Checks is the given address in loaded code memory area. - if ( ! IsAddressLoadedCode( addr ) ) - continue; - - // Iterate through array of code blocks to check if address is in code segment area - for ( TInt j = 0; j < blocksCount; j++ ) - { - // Checks if the given address is in this memory block area - if ( iCodeblocks[j].CheckAddress( addr ) ) - { - // To avoid recursive call to ReAlloc specifying granularity - // Add address to the callstack - iReCallStack[arrayCounter] = addr; - arrayCounter++; - break; - } - } - // Checks if the wanted callstack items are gathered - if ( arrayCounter == KATMaxCallstackLength || - arrayCounter == iAllocMaxCallStack ) - { - LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); - break; - } - } - } - - - // Log the memory reallocation information - if ( iLogOption == EATLogToTraceFast ) - { - // Using fast logging mode. - ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId ); - } - + // Don't collect or log data if storage server not open or logging mode is not fast. + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + TInt error( KErrNone ); + TUint arrayCounter = 0; + + // Reset the callstack + iReCallStack.Reset(); + + // If we don't want any call stack to be saved skip the next part + if( iAllocMaxCallStack > 0 ) + { + // Find the current thread callstack start address + TUint32 stackstart( 0 ); + TBool found( FindCurrentThreadStack( stackstart ) ); + LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found ); + + // Returns the value of the stack pointer at the + // current point in your program. + TUint32 _sp( 0 ); + __asm + { + mov [_sp], esp + } + + // Get process loaded code segments count + TInt blocksCount( iCodeblocks.Count() ); + + // Iterate through callstack to find wanted callstack addresses + // - Start: current stack address + // - Stop: stack start address(Run-address of user stack) + // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) + for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 + { + TUint32 addr = (TUint32) *( (TUint32*) i ); + // Checks is the given address in loaded code memory area. + if ( ! IsAddressLoadedCode( addr ) ) + continue; + + // Iterate through array of code blocks to check if address is in code segment area + for ( TInt j = 0; j < blocksCount; j++ ) + { + // Checks if the given address is in this memory block area + if ( iCodeblocks[j].CheckAddress( addr ) ) + { + // To avoid recursive call to ReAlloc specifying granularity + // Add address to the callstack + iReCallStack[arrayCounter] = addr; + arrayCounter++; + break; + } + } + // Checks if the wanted callstack items are gathered + if ( arrayCounter == KATMaxCallstackLength || + arrayCounter == iAllocMaxCallStack ) + { + LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); + break; + } + } + } + + // No need to report free if the aPtr was NULL + if ( aPtr != NULL ) + { + // Reset the free callstack + iFreeCallStack.Reset(); + + // Check that logging mode is trace/trace fast so we use free call stack + // and call stack size bigger than zero + if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast ) && iFreeMaxCallStack > 0 ) + { + for ( TInt i = 0; i < arrayCounter; i++ ) + { + if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack ) + { + break; + } + iFreeCallStack[i] = iReCallStack[i]; + } + } + // Try to remove old address from the storage server's + // leak array. If found. it's removed from the array because system frees + // old address directly in the RHeap in ReAlloc case. + if ( iLogOption == EATLogToTraceFast ) + { + ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack ); + } + else + { + iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ); + } + } + // Log the memory allocation information + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast logging mode. + ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize ); + } + else + { + // Using storage server. + error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, + iReCallStack, + aSize ); + if ( KErrNone != error ) + { + switch ( error ) + { + case KErrNoMemory: + { + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - KErrNoMemory case" ); + // Check if eventhandler is active + if ( iEventHandler->IsActive() ) + { + // Cancel iEventHandler because not needed anymore + iEventHandler->Cancel(); + } + if ( iStorageServerOpen ) + { + // Close storage server + iStorageServerOpen = EFalse; + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - close iStorageServer" ); + iStorageServer.Close(); + } + break; + } + default: + { + LOGSTR2( "ATMH LogMemoryAllocated error %i", error ); + break; + } + } + } + } + } } // Release the mutex @@ -791,12 +911,10 @@ // Acquire the mutex iMutex.Wait(); - // get thread ID - TUint threadId = RThread().Id(); - // Realloc the memory using original allocator TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode ); + TInt error( KErrNone ); TUint arrayCounter = 0; // NULL addresses are not in a process under test @@ -807,65 +925,136 @@ LOGSTR3( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - aSize: %i, aMode: %i", aSize, aMode ); - // Reset the callstack - iReCallStack.Reset(); - - // If we don't want any call stack to be saved skip the next part - if( iAllocMaxCallStack > 0 ) - { - // Find the current thread callstack start address - TUint32 stackstart( 0 ); - TBool found( FindCurrentThreadStack( stackstart ) ); - LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found ); - - // Get process loaded code segments count - TInt blocksCount( iCodeblocks.Count() ); - - // Iterate through callstack to find wanted callstack addresses - // - Start: current stack address(__current_sp(): Returns the value of the - // stack pointer at the current point in your program.) - // - Stop: stack start address(Run-address of user stack) - // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) - for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 - { - TUint32 addr = (TUint32) *( (TUint32*) i ); - - // Checks is the given address in loaded code memory area. - if ( !IsAddressLoadedCode( addr ) ) - continue; - - // Iterate through array of code blocks to check if address is in code segment area - for ( TInt j = 0; j < blocksCount; j++ ) - { - // Checks if the given address is in this memory block area - if ( iCodeblocks[j].CheckAddress( addr ) ) - { - // To avoid recursive call to ReAlloc specifying granularity - // Add address to the callstack - iReCallStack[arrayCounter] = ( addr ); - arrayCounter++; - break; - } - } - // Checks if the wanted callstack items are gathered - if ( arrayCounter == KATMaxCallstackLength || - arrayCounter == iAllocMaxCallStack ) - { - LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); - break; - } - } - } - - if ( iLogOption == EATLogToTraceFast ) - { - // Using fast logging mode. - ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId ); - } + // Don't collect or log data if storage server not open or logging mode is not fast. + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + // Reset the callstack + iReCallStack.Reset(); + + // If we don't want any call stack to be saved skip the next part + if( iAllocMaxCallStack > 0 ) + { + // Find the current thread callstack start address + TUint32 stackstart( 0 ); + TBool found( FindCurrentThreadStack( stackstart ) ); + LOGSTR3( "ATMH > stackstart: %x , find = %i", stackstart, found ); + + // Get process loaded code segments count + TInt blocksCount( iCodeblocks.Count() ); + + // Iterate through callstack to find wanted callstack addresses + // - Start: current stack address(__current_sp(): Returns the value of the + // stack pointer at the current point in your program.) + // - Stop: stack start address(Run-address of user stack) + // - Add: address length(The word size in the current system is 32 bits, which is 4 bytes) + for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348 + { + TUint32 addr = (TUint32) *( (TUint32*) i ); + + // Checks is the given address in loaded code memory area. + if ( !IsAddressLoadedCode( addr ) ) + continue; + + // Iterate through array of code blocks to check if address is in code segment area + for ( TInt j = 0; j < blocksCount; j++ ) + { + // Checks if the given address is in this memory block area + if ( iCodeblocks[j].CheckAddress( addr ) ) + { + // To avoid recursive call to ReAlloc specifying granularity + // Add address to the callstack + iReCallStack[arrayCounter] = ( addr ); + arrayCounter++; + break; + } + } + // Checks if the wanted callstack items are gathered + if ( arrayCounter == KATMaxCallstackLength || + arrayCounter == iAllocMaxCallStack ) + { + LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter ); + break; + } + } + } + + // No need to report free if the aPtr was NULL + if ( aPtr != NULL ) + { + // Reset the free callstack + iFreeCallStack.Reset(); + + // Check that logging mode is trace/trace fast so we use free call stack + // and call stack size bigger than zero + if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast ) + && iFreeMaxCallStack > 0 ) + { + for ( TInt i = 0; i < arrayCounter; i++ ) + { + if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack ) + { + break; + } + iFreeCallStack[i] = ( iReCallStack[i] ); + } + } + + // Try to remove old address from the storage server's + // leak array. If found. it's removed from the array because system frees + // old address directly in the RHeap in ReAlloc case. + if ( iLogOption == EATLogToTraceFast ) + { + ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack ); + } + else + { + iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ); + } + } + + // Log the memory allocation information + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast logging mode. + ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize ); + } + else + { + // Using storage server. + error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, + iReCallStack, + aSize ); + if ( KErrNone != error ) + { + switch ( error ) + { + case KErrNoMemory: + { + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - KErrNoMemory case" ); + // Check if eventhandler is active + if ( iEventHandler->IsActive() ) + { + // Cancel iEventHandler because not needed anymore + iEventHandler->Cancel(); + } + if ( iStorageServerOpen ) + { + // Close storage server + iStorageServerOpen = EFalse; + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::ReAlloc() - close iStorageServer" ); + iStorageServer.Close(); + } + break; + } + default: + { + LOGSTR2( "ATMH LogMemoryAllocated error %i", error ); + break; + } + } + } + } + } } // Release the mutex @@ -1060,15 +1249,6 @@ { // Remove the thread iThreadArray.Remove( i ); - if ( iLogOption == EATLogToTraceFast ) - { - // log thread removed - ATFastLogThreadEnded( RProcess().Id().operator TUint(), aThreadId ); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogThreadEnded( aThreadId ); - } LOGSTR1( "ATMH > thread removed" ); break; } @@ -1105,8 +1285,8 @@ // Retrieve and log the process initial information // ----------------------------------------------------------------------------- // -void RAnalyzeToolMainAllocator::LogProcessInformation( const TFileName& aFileName, const TPath& aFilePath, - TUint32 aLogOption, TUint32 aIsDebug, const TDesC8& aAtoolVersion, const TDesC8& aApiVersion ) +void RAnalyzeToolMainAllocator::LogProcessInformation( const TFileName aFileName, + TUint32 aLogOption, TUint32 aIsDebug ) { LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation()" ); @@ -1129,38 +1309,31 @@ // Append thread to array of the users of this allocator error = iThreadArray.Append( TThreadStack( RThread().Id(), params().iStackAddress + params().iStackSize) ); - __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) ); - - // Using fast logging mode. - if ( iLogOption == EATLogToTraceFast ) - { - LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogProcessStarted() #1" ); - // Log process information - ATFastLogProcessStarted( params().iProcessName, iProcessId, aIsDebug, aAtoolVersion, aApiVersion ); - // log thread added - ATFastLogThreadStarted( iProcessId, RThread().Id().operator TUint() ); - } - else if ( iLogOption == EATLogToFile ) - { - // Open a file server session and a file. - error = iLogFile.OpenFsAndFile( aFileName, aFilePath, params().iProcessName ); - // Return without logging, if an error occured - if ( error != KErrNone ) + // Log process information + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) + { + if ( iLogOption == EATLogToTraceFast ) { - // TODO + // Using fast logging mode. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogProcessStarted() #1" ); + ATFastLogProcessStarted( params().iProcessName, iProcessId, aIsDebug ); } - - //if everything is ok, add file version in the begining of file - iLogFile.ATFileLogVersion(); - //log process start - iLogFile.ATFileLogProcessStarted( params().iProcessName, iProcessId, aIsDebug, aAtoolVersion, aApiVersion ); - // log thread added - iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() ); + else + { + // Using storage server. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #1" ); + error = iStorageServer.LogProcessStarted( + aFileName, + params().iProcessName, + iProcessId, + aLogOption, + aIsDebug ); + } } - + LOGSTR2( "ATMH LogProcessStarted error %i", error ); // Iterate through process codesegments @@ -1176,27 +1349,31 @@ if ( KErrNone == error ) { // Don't log AnalyzeTool libraries - if ( 0 != codeinfo().iFullName.CompareC( KMemoryHook ) ) + if ( 0 != codeinfo().iFullName.CompareC( KMemoryHook ) && + 0 != codeinfo().iFullName.CompareC( KStorageServer ) ) { // Log the loaded codesegment(s) - - if ( iLogOption == EATLogToTraceFast ) + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) { - // Using fast logging mode. - LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogDllLoaded() #2" ); - ATFastLogDllLoaded( iProcessId, - codeinfo().iFullName, - codeinfo().iRunAddress, - codeinfo().iRunAddress + codeinfo().iSize ); + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast logging mode. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - ATFastLogDllLoaded() #2" ); + ATFastLogDllLoaded( iProcessId, + codeinfo().iFullName, + codeinfo().iRunAddress, + codeinfo().iRunAddress + codeinfo().iSize ); + } + else + { + // Using storage server. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #2" ); + error = iStorageServer.LogDllLoaded( + codeinfo().iFullName, + codeinfo().iRunAddress, + codeinfo().iRunAddress + codeinfo().iSize ); + } } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogDllLoaded( - codeinfo().iFullName, - codeinfo().iRunAddress, - codeinfo().iRunAddress + codeinfo().iSize ); - } - LOGSTR2( "ATMH LogDllLoaded error %i", error ); @@ -1226,24 +1403,29 @@ if ( KErrNone == error ) { // Log the loaded dynamic codesegment(s) - - if ( iLogOption == EATLogToTraceFast ) + if ( iStorageServerOpen || iLogOption == EATLogToTraceFast ) { - // Using fast logging mode. - LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - - ATFastLogDllLoaded()#3" ); - ATFastLogDllLoaded( iProcessId, - info().iLibraryName, - info().iRunAddress, - info().iRunAddress + info().iSize ); + if ( iLogOption == EATLogToTraceFast ) + { + // Using fast logging mode. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - - ATFastLogDllLoaded()#3" ); + ATFastLogDllLoaded( iProcessId, + info().iLibraryName, + info().iRunAddress, + info().iRunAddress + info().iSize ); + } + else + { + // Using storage server. + LOGSTR1( "ATMH RAnalyzeToolMainAllocator::LogProcessInformation() - iStorageServerOpen #3" ); + error = iStorageServer.LogDllLoaded( + info().iLibraryName, + info().iRunAddress, + info().iRunAddress + info().iSize ); + } } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogDllLoaded( - info().iLibraryName, - info().iRunAddress, - info().iRunAddress + info().iSize ); - } - + + LOGSTR2( "ATMH LogDllLoaded error %i", error ); if ( KErrNone == error ) @@ -1259,7 +1441,6 @@ } } - // ----------------------------------------------------------------------------- // RAnalyzeToolMainAllocator::FindCurrentThreadStack() // Find the current thread which is using the heap @@ -1323,59 +1504,6 @@ { iEventHandler->Start(); } - } - -// ----------------------------------------------------------------------------- -// RAnalyzeToolMainAllocator::LogDeviceInfo() -// Logs iversion of ATApp, ATApi, S60 version and ROM checksum -// at the startup of hooked application -// ----------------------------------------------------------------------------- -// -void RAnalyzeToolMainAllocator::LogDeviceInfo() - { - //get s60 version - TBuf bufS60; - if (SysUtil::GetSWVersion(bufS60) == KErrNone) - { - TInt index(0); - //replace newlines in string with '@' - //while ((index = bufS60.Find(KNewLine)) != KSysUtilVersionTextLength && index != KErrNotFound) - while ( (index = bufS60.Find(KNewLine16)) != KErrNotFound) - { - bufS60.Replace(index, 1, KSeparator); - } - while ( (index = bufS60.Find(KSpace16)) != KErrNotFound) - { - bufS60.Replace(index, 1, KSeparator); - } - } - - TBuf bufChecksum; - -#ifndef __WINSCW__ - TRomHeader* romHeader = (TRomHeader*) UserSvr::RomHeaderAddress(); - if (romHeader) - { - bufChecksum.Format(_L("%08x"), romHeader->iCheckSum); - } -#endif - - TBuf8 s60Version; - s60Version.Copy(bufS60); - - TBuf8 romChecksum; - romChecksum.Copy(bufChecksum); - - //only fastlog implementation - if (iLogOption == EATLogToTraceFast) - { - ATFastLogDeviceInfo(s60Version, romChecksum); - } - else if ( iLogOption == EATLogToFile ) - { - iLogFile.ATFileLogDeviceInfo( s60Version, romChecksum ); - } - } // End of File