memana/analyzetoolclient/dynamicmemoryhook/src/customuser.cpp
changeset 2 6a82cd05fb1e
parent 1 3ff3fecb12fe
equal deleted inserted replaced
1:3ff3fecb12fe 2:6a82cd05fb1e
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Definitions for the class CustomUser.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <utf.h>
       
    20 #include "customuser.h"
       
    21 #include "analyzetoolmainallocator.h"
       
    22 #include "analyzetoolallocator.h"
       
    23 #include "atlog.h"
       
    24 #include "analyzetoolmemoryallocator.h"
       
    25 #include "analyzetoolpanics.pan"
       
    26 #include "atstorageservercommon.h"
       
    27 #include "atdriveinfo.h"
       
    28 
       
    29 // CONSTANTS
       
    30 // When needed, update the version number directly inside _LIT macro.
       
    31 // Constant for the atool API(staticlib) version.
       
    32 _LIT( KAtoolApiVersion, "1.7.4" );
       
    33 
       
    34 // Version number buffer length
       
    35 const TInt KAtoolVersionNumberLength = 10;
       
    36 
       
    37 // Wrong version error code
       
    38 const TInt KAtoolVersionError = -1999;
       
    39 
       
    40 // Version number separator
       
    41 _LIT( KVersionSeparator, ";" );
       
    42 
       
    43 // Incorrect version error strings 
       
    44 _LIT8( KDataFileVersion, "DATA_FILE_VERSION 10\r\n" );
       
    45 _LIT( KIncorrectText, "ERROR_OCCURED INCORRECT_ATOOL_VERSION [API v.%S][ATOOL v.%S]" );
       
    46 _LIT( KIncorrectTextXti, "PCSS " );
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CustomUser::Panic()
       
    50 // Overloaded User::Panic() function
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 EXPORT_C void CustomUser::Panic( const TDesC& aCategory, TInt aReason )
       
    54     {
       
    55     LOGSTR3( "ATMH CustomUser::Panic() %S %i", &aCategory, aReason );
       
    56     
       
    57     // Uninstall thread's RAllocator
       
    58     ( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall();
       
    59        
       
    60     // Call the "real" User::Panic()
       
    61     User::Panic( aCategory, aReason );
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CustomUser::Exit()
       
    66 // Overloaded User::Exit() function
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C void CustomUser::Exit( TInt aReason )
       
    70     {
       
    71     LOGSTR3( "ATMH CustomUser::Exit() %i %i", aReason, RThread().Id().Id() );
       
    72     
       
    73     if ( aReason != KAtoolVersionError )
       
    74     	{
       
    75     	// Uninstall thread's RAllocator
       
    76     	( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall();
       
    77     	LOGSTR1( "ATMH CustomUser::Exit() - about to User::Exit" );
       
    78     	}
       
    79     
       
    80     // Call the "real" User::Exit()
       
    81     User::Exit( aReason );
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CustomUser::SetCritical()
       
    86 // Overloaded User::SetCritical() function which returns
       
    87 // KErrNone, if successful; KErrArgument, if EAllThreadsCritical is 
       
    88 // passed - this is a state associated with a process, and you use 
       
    89 // User::SetProcessCritical() to set it.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 EXPORT_C TInt CustomUser::SetCritical( User::TCritical aCritical )
       
    93     {
       
    94     LOGSTR1( "ATMH CustomUser::SetCritical()" );
       
    95     // Check the given User::TCritical type
       
    96     if ( aCritical == User::EAllThreadsCritical )
       
    97         {
       
    98         return KErrArgument;
       
    99         }
       
   100     else
       
   101         {
       
   102         return KErrNone;
       
   103         }
       
   104     }
       
   105   
       
   106 // -----------------------------------------------------------------------------
       
   107 // CustomUser::SetProcessCritical()
       
   108 // Overloaded User::SetProcessCritical() function
       
   109 // KErrNone, if successful; KErrArgument, if either EProcessCritical or 
       
   110 // EProcessPermanent is passed - these are states associated with a 
       
   111 // thread, and you use User::SetCritical() to set them.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C TInt CustomUser::SetProcessCritical( User::TCritical aCritical )
       
   115     {
       
   116     LOGSTR1( "ATMH CustomUser::SetProcessCritical()" );
       
   117      // Check the given User::TCritical type 
       
   118     if ( aCritical == User::EProcessCritical || 
       
   119          User::EProcessPermanent == aCritical )
       
   120         {
       
   121         return KErrArgument;
       
   122         }
       
   123     else
       
   124         {
       
   125         return KErrNone;
       
   126         }
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CustomUser::SetupThreadHeap()
       
   131 // Overloaded UserHeap::SetupThreadHeap function
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C TInt CustomUser::SetupThreadHeap( TBool aNotFirst, 
       
   135     SStdEpocThreadCreateInfo& aInfo, const TFileName aFileName,
       
   136     TUint32 aLogOption, TUint32 aIsDebug, const TFileName aVersion,
       
   137     TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize )
       
   138     {
       
   139     LOGSTR1( "ATMH CustomUser::SetupThreadHeap()" );
       
   140     LOGSTR2( "ATMH > Thread id(%d)", RThread().Id().operator TUint() );
       
   141     
       
   142     TInt ret( KErrNone );    
       
   143     // Check version number
       
   144     TBuf<KAtoolVersionNumberLength> atoolVer;
       
   145     if ( CheckVersion( aVersion, atoolVer ) != KErrNone )
       
   146     	{
       
   147     	LOGSTR1( "ATMH > Wrong API version > Inform user and Exit." );
       
   148     	ReportIncorrectVersion( aLogOption, aFileName, atoolVer );
       
   149     	return KAtoolVersionError;
       
   150     	}
       
   151     
       
   152     // Check is this shared heap
       
   153     if ( aInfo.iAllocator == NULL )
       
   154         {
       
   155         LOGSTR1( "ATMH creating a new heap" );
       
   156         // RAllocator is NULL so heap is not shared, creating a new heap
       
   157         ret = UserHeap::SetupThreadHeap( aNotFirst, aInfo );
       
   158         __ASSERT_ALWAYS( KErrNone == ret, AssertPanic( EFailedToCreateHeap ) );
       
   159         
       
   160 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_3 )
       
   161     #ifndef __WINS__
       
   162         // Set dummy Tls value
       
   163         TAny* dummyPtr( NULL );
       
   164         TInt setErr( UserSvr::DllSetTls( KDummyHandle, dummyPtr ) );
       
   165         LOGSTR2( "ATMH > Set Tls err(%i)", setErr );
       
   166     #endif
       
   167 #endif
       
   168         // Install the RAllocator
       
   169         aInfo.iAllocator = &InstallAllocator( aNotFirst, aFileName, aLogOption, aIsDebug,
       
   170                 aAllocCallStackSize, aFreeCallStackSize );
       
   171         }
       
   172     else
       
   173         {
       
   174         LOGSTR1( "ATMH sharing the heap" );
       
   175         // The heap is shared. Acquire pointer to the original heap
       
   176         RAnalyzeToolMemoryAllocator* allocator = 
       
   177 			(RAnalyzeToolMemoryAllocator*) aInfo.iAllocator;
       
   178         // Share the heap
       
   179         allocator->ShareHeap();
       
   180         // Switch thread heap 
       
   181         User::SwitchAllocator( allocator );
       
   182         }
       
   183     return ret;
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CustomUser::InstallAllocator
       
   188 // Installs the RAllocator
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 //lint -e{429} suppress "Custodial pointer 'allocator' has not been freed or returned"
       
   192 EXPORT_C RAllocator& CustomUser::InstallAllocator( TBool aNotFirst, 
       
   193 	const TFileName aFileName, TUint32 aLogOption, TUint32 aIsDebug,
       
   194 	TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize )
       
   195     {
       
   196     LOGSTR1( "ATMH CustomUser::InstallAllocator()" );
       
   197     
       
   198     // Open handle to the device driver
       
   199     RAnalyzeTool analyzetool;
       
   200     TInt error = analyzetool.Open();
       
   201     
       
   202     // Check if the device driver has already loaded
       
   203     if ( KErrNone == error )
       
   204         {
       
   205         LOGSTR1( "ATMH CustomUser::InstallAllocator() - analyzetool.Open() returned KErrNone" );
       
   206         // The device driver has already loaded
       
   207         // Get pointer to the main thread allocator
       
   208         TMainThreadParamsBuf params;
       
   209         params().iProcessId = RProcess().Id().operator TUint();
       
   210         error = analyzetool.MainThreadAlloctor( params );
       
   211 
       
   212         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantOpenHandle ) );
       
   213         
       
   214         // Close handle to the device driver
       
   215         analyzetool.Close();
       
   216         
       
   217         // Is this the first thread of the program
       
   218         if ( params().iAlone )
       
   219             {
       
   220             LOGSTR1( "ATMH CustomUser::InstallAllocator() - first thread of the program" );
       
   221             // Only one thread in the program. Must be main thread
       
   222             RAnalyzeToolMainAllocator* allocator = 
       
   223 				new RAnalyzeToolMainAllocator( aNotFirst, aFileName, aLogOption,
       
   224 				                               aIsDebug, aAllocCallStackSize, aFreeCallStackSize );
       
   225             
       
   226             __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) );
       
   227             
       
   228             // Change threads allocator
       
   229             User::SwitchAllocator( allocator );
       
   230             
       
   231             // Return reference to the RAllocator
       
   232             return *allocator;
       
   233             }
       
   234         // This is not the first thread. A new thread with a new heap created
       
   235         else
       
   236             {
       
   237             LOGSTR1( "ATMH CustomUser::InstallAllocator() - create a new allocator for the new thread" );
       
   238             // Create new RAllocator with handles from the main thread
       
   239             RAnalyzeToolAllocator* allocator = new RAnalyzeToolAllocator( 
       
   240                     aNotFirst,
       
   241                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->StorageServer(), 
       
   242                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->Codeblocks(), 
       
   243                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->Mutex(), 
       
   244                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->ProcessId(), 
       
   245                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->AnalyzeTool(),
       
   246                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->StorageServerOpen(),
       
   247                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->LogOption(),
       
   248                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->AllocMaxCallStack(),
       
   249                     ((RAnalyzeToolMainAllocator*)params().iAllocator)->FreeMaxCallStack() );
       
   250 
       
   251             __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) );
       
   252             
       
   253             // Change threads allocator
       
   254             User::SwitchAllocator( allocator );
       
   255             
       
   256             // Return reference to the RAllocator
       
   257             return *allocator;
       
   258             }
       
   259         }
       
   260     // The device driver does not exists so this must be the first thread
       
   261     else
       
   262         {
       
   263         LOGSTR1( "ATMH CustomUser::InstallAllocator() - analyzetool.Open() returned error, creating DD" );
       
   264         RAnalyzeToolMainAllocator* allocator = 
       
   265 			new RAnalyzeToolMainAllocator( aNotFirst, aFileName, aLogOption, aIsDebug,
       
   266 			        aAllocCallStackSize, aFreeCallStackSize );
       
   267         
       
   268         __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) );
       
   269         
       
   270         // Change threads allocator
       
   271         User::SwitchAllocator( allocator );
       
   272 
       
   273         // Return reference to the RAllocator
       
   274         return *allocator;
       
   275         }
       
   276     } 
       
   277     
       
   278 // -----------------------------------------------------------------------------
       
   279 // AnalyzeToolInterface::StartSubTest
       
   280 // Function for starting a subtest with a given name
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 EXPORT_C void AnalyzeToolInterface::StartSubTest( const TDesC8& aSubtestId )
       
   284     {
       
   285     LOGSTR1( "ATMH AnalyzeToolInterface::StartSubTest()" );
       
   286     // Open handle to the device driver
       
   287     RAnalyzeTool analyzetool;
       
   288     TInt error = analyzetool.Open();
       
   289     if ( KErrNone == error )
       
   290         {
       
   291         LOGSTR1( "ATMH AnalyzeToolInterface::StartSubTest() - DD opened" );
       
   292         // The device driver has already loaded
       
   293         // Get pointer to the main thread allocator
       
   294         TMainThreadParamsBuf params;
       
   295         params().iProcessId = RProcess().Id().operator TUint();
       
   296         error = analyzetool.MainThreadAlloctor( params );
       
   297 
       
   298         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantOpenHandle ) );
       
   299         
       
   300         // Close handle to the device driver
       
   301         analyzetool.Close();
       
   302 
       
   303         if ( ( ( RAnalyzeToolMainAllocator* )params().iAllocator )->StorageServerOpen() )
       
   304             {
       
   305                 LOGSTR1( "ATMH AnalyzeToolInterface::StartSubTest() - SS open -> try to start subtest" );
       
   306             // Start subtest
       
   307             ( ( RAnalyzeToolMainAllocator* )params().iAllocator )->
       
   308                         StorageServer().StartSubTest( aSubtestId );
       
   309             }
       
   310         }
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // AnalyzeToolInterface::StopSubTest
       
   315 // Function for stopping a subtest with a given name
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 EXPORT_C void AnalyzeToolInterface::StopSubTest( const TDesC8& aSubtestId )
       
   319     {
       
   320     LOGSTR1( "ATMH AnalyzeToolInterface::StopSubTest()" );
       
   321     // Open handle to the device driver
       
   322     RAnalyzeTool analyzetool;
       
   323     TInt error = analyzetool.Open();
       
   324     if ( KErrNone == error )
       
   325         {
       
   326         LOGSTR1( "ATMH AnalyzeToolInterface::StopSubTest() - DD opened" );
       
   327         // The device driver has already loaded
       
   328         // Get pointer to the main thread allocator
       
   329         TMainThreadParamsBuf params;
       
   330         params().iProcessId = RProcess().Id().operator TUint();
       
   331         error = analyzetool.MainThreadAlloctor( params );
       
   332 
       
   333         __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantOpenHandle ) );
       
   334         
       
   335         // Close handle to the device driver
       
   336         analyzetool.Close();
       
   337 
       
   338         if ( ( ( RAnalyzeToolMainAllocator* )params().iAllocator )->StorageServerOpen() )
       
   339             {
       
   340                 LOGSTR1( "ATMH AnalyzeToolInterface::StopSubTest() - SS open -> try to stop subtest" );
       
   341             // Start subtest
       
   342             ( ( RAnalyzeToolMainAllocator* )params().iAllocator )->
       
   343                         StorageServer().StopSubTest( aSubtestId );
       
   344             }
       
   345         }
       
   346     }
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // CustomUser::__DbgMarkEnd
       
   350 // Marks the end of heap cell checking at the current nested level 
       
   351 // for the current thread's default heap, or the kernel heap.
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 EXPORT_C TUint32 CustomUser::__DbgMarkEnd( TBool /*aKernel*/, TInt /*aCount*/ )
       
   355     {
       
   356     LOGSTR1( "ATMH CustomUser::__DbgMarkEnd()");
       
   357     // Return just zero to override system's memory leak check
       
   358     return 0;
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CustomUser::CheckVersion
       
   363 // Check atool version
       
   364 // -----------------------------------------------------------------------------
       
   365 //
       
   366 TInt CustomUser::CheckVersion( const TFileName aVersion, TDes& aToolVersion )
       
   367     { 
       
   368     LOGSTR2( "ATMH CustomUser::CheckVersion(), aVersion( %S )", &aVersion );
       
   369     
       
   370     TFileName version;
       
   371     version.Copy( aVersion );
       
   372     TBuf<KAtoolVersionNumberLength> apiVer;
       
   373     	
       
   374     // Find separator place
       
   375     TInt findplace( version.Find( KVersionSeparator() ) );
       
   376     // Parse API version first [x.x.x;x.x.x]
       
   377     if ( findplace >= 0 && findplace <= apiVer.MaxLength() )
       
   378 		{
       
   379 		apiVer.Copy( version.Mid( 0, findplace ) ); 
       
   380 		version.Delete( 0, findplace + KVersionSeparator().Length() );
       
   381 		}
       
   382  
       
   383     if ( version.Length() <= aToolVersion.MaxLength() )
       
   384     	{
       
   385     	aToolVersion.Copy( version );
       
   386     	if ( aToolVersion.Compare( KAtoolApiVersion ) == KErrNone &&
       
   387     		 apiVer.Length() == 0 )
       
   388     		{
       
   389     		// Support 1.5.0 version (Version info: [1.5.0])
       
   390     		apiVer.Copy( version );
       
   391     		}
       
   392     	}
       
   393     
       
   394     LOGSTR3( "ATMH > API version( %S ), ATOOL version( %S )", 
       
   395     		&apiVer, &aToolVersion );
       
   396         
       
   397     // Check version numbers 
       
   398     if ( apiVer.Compare( KAtoolApiVersion ) == KErrNone )
       
   399     	{
       
   400     	return KErrNone;
       
   401     	}
       
   402     return KErrCancel;    
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CustomUser::ReportIncorrectVersion
       
   407 // Function for showing incorrect version information
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 void CustomUser::ReportIncorrectVersion( const TUint32 aLogOption,
       
   411 	const TFileName aFileName, const TDes& aToolVersion )
       
   412 	{
       
   413 	LOGSTR2( "ATMH CustomUser::ReportIncorrectVersion(), aFileName( %S )", 
       
   414 			&aFileName );
       
   415 	
       
   416 	switch ( aLogOption )
       
   417 		{
       
   418 		case EATLogToFile:
       
   419 			{
       
   420 			LOGSTR1( "ATMH ReportIncorrectVersion > EATLogToFile" );			
       
   421 			
       
   422 			// A handle to a file server session.
       
   423 			RFs fs;
       
   424 			// Creates and opens a file, 
       
   425 			// and performs all operations on a single open file.
       
   426 			RFile file;	
       
   427 			// Create full path buffer
       
   428 			TBuf<KMaxFileName> logFileBuf;
       
   429 			// Connects a client to the file server.
       
   430 			TInt err( fs.Connect() );
       
   431 			
       
   432 			if ( !err )
       
   433 				{				
       
   434                 err = TATDriveInfo::CreatePath( logFileBuf, aFileName, fs );
       
   435 			    
       
   436 				// Replace file if exists
       
   437 				if ( err && err != KErrAlreadyExists )
       
   438 					{
       
   439 					LOGSTR2( "ATMH > TATDriveInfo::CreatePath() err( %i )", err );
       
   440 					return;
       
   441 					}
       
   442 				
       
   443 				// Replace file if exists (drive C)
       
   444 				err = file.Replace( fs, logFileBuf, EFileWrite );
       
   445 										
       
   446 				// Write to file
       
   447 				if ( !err )
       
   448 					{
       
   449 					err = file.Write( KDataFileVersion );
       
   450 					// Error msg buffer
       
   451 					TBuf8<KMaxFileName> msg;				     
       
   452 					// Write the error code to the buffer  
       
   453 					logFileBuf.Format( KIncorrectText, &KAtoolApiVersion, &aToolVersion );	
       
   454 					CnvUtfConverter::ConvertFromUnicodeToUtf8( msg, logFileBuf );
       
   455 					err = file.Write( msg );
       
   456 					}
       
   457 				// Closes the file.
       
   458 				file.Close();
       
   459 				}
       
   460 			
       
   461 			LOGSTR2( "ATMH > File err( %i )", err );			
       
   462 			// Closes the handle.
       
   463 			fs.Close();
       
   464 			}
       
   465 			break;
       
   466 			
       
   467 		case EATUseDefault:
       
   468 		case EATLogToXti:
       
   469 			{
       
   470 			LOGSTR1( "ATMH > ReportIncorrectVersion > EATLogToXti" );
       
   471 			// Error msg buffer
       
   472 			TBuf<KMaxFileName> msg;	
       
   473 			msg.Copy( KIncorrectTextXti );
       
   474 			msg.Append( KIncorrectText );
       
   475 			TBuf<KMaxFileName> xtiMsg;	
       
   476 			// Write the error code to the buffer  
       
   477 			xtiMsg.Format( msg, &KAtoolApiVersion, &aToolVersion );
       
   478 			RDebug::Print( xtiMsg );
       
   479 			}
       
   480 			break;
       
   481 		
       
   482 		default:
       
   483 			{
       
   484 			LOGSTR1( "ATMH > ReportIncorrectVersion > default" );
       
   485 			}
       
   486 			break;
       
   487 		}	
       
   488 	}
       
   489 
       
   490 // End of File