memspy/Engine/Source/MemSpyEngineUtils.cpp
changeset 0 a03f92240627
child 20 ca8a1b6995f6
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <memspy/engine/memspyengineutils.h>
       
    19 
       
    20 // System includes
       
    21 #include <e32svr.h>
       
    22 #include <e32capability.h>
       
    23 #ifdef __EPOC32__
       
    24 #include <e32rom.h>
       
    25 #endif
       
    26 
       
    27 // User includes
       
    28 #include <memspy/engine/memspyengineoutputsink.h>
       
    29 
       
    30 // Typedefs
       
    31 typedef TInt(*TSysUtilGetSWVersionFunction)(TDes&);
       
    32 
       
    33 // Literal constants
       
    34 _LIT( KMemSpyEngineDoubleColon, "::" );
       
    35 _LIT( KMemSpyKernelProcessName, "ekern" );
       
    36 _LIT( KMemSpyKernelProcessFullName, "ekern.exe[100041af]0001" );
       
    37 _LIT( KMemSpyKernelThreadName, "Supervisor" );
       
    38 _LIT( KMemSpyLogRootPath, "\\MemSpy\\" );
       
    39 _LIT( KMemSpyLogDefaultName, "Log" );
       
    40 _LIT( KMemSpyLogFileNameWithTimeStamp, "MemSpy - %4d%02d%02d - %02d.%02d.%02d.%06d - " );
       
    41 
       
    42 
       
    43 EXPORT_C void MemSpyEngineUtils::FormatTimeL( TDes& aBuf, const TInt64& aTimeVal, TBool aLocalTime )
       
    44     {
       
    45     const TTime time( aTimeVal );
       
    46     FormatTimeL( aBuf, time, aLocalTime );
       
    47     }
       
    48 
       
    49 
       
    50 EXPORT_C void MemSpyEngineUtils::FormatTimeL( TDes& aBuf, const TTime& aTime, TBool aLocalTime )
       
    51     {
       
    52     if  ( aLocalTime )
       
    53         {
       
    54         _LIT( KFormatSpecLocal, "%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B" );
       
    55         // 
       
    56         TTime time( aTime );
       
    57         time += User::UTCOffset();
       
    58         time.FormatL( aBuf, KFormatSpecLocal );
       
    59         }
       
    60     else
       
    61         {
       
    62         _LIT( KFormatSpecUTC, "%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B UTC" );
       
    63         aTime.FormatL( aBuf, KFormatSpecUTC );
       
    64         }
       
    65     }
       
    66 
       
    67 
       
    68 void MemSpyEngineUtils::FormatTimeSimple( TDes& aBuf, const TTime& aTime )
       
    69     {
       
    70     const TDateTime dt = aTime.DateTime();
       
    71     //
       
    72     _LIT( KTimeFormatSpec, "%04d%02d%02d %02d:%02d:%02d" );
       
    73     aBuf.Format( KTimeFormatSpec, dt.Year(), dt.Month()+1, dt.Day()+1, dt.Hour(), dt.Minute(), dt.Second() );
       
    74     }
       
    75 
       
    76 
       
    77 EXPORT_C void MemSpyEngineUtils::FormatHex( TDes& aBuf, TInt aValue )
       
    78     {
       
    79     _LIT(KMemSpyNumericHexFormat, "0x%08x");
       
    80     aBuf.Format( KMemSpyNumericHexFormat, aValue );
       
    81     }
       
    82 
       
    83 
       
    84 EXPORT_C TMemSpySizeText MemSpyEngineUtils::FormatSizeText( const TInt64& aValue, TInt aDecimalPlaces, TBool aExtraRounding )
       
    85     {
       
    86     _LIT(KFormatKilo, "%dK");
       
    87     _LIT(KFormatMega, "%SM");
       
    88     _LIT(KFormatGiga, "%SG");
       
    89 
       
    90 	TMemSpySizeText buf;
       
    91 	if  ( aValue < 1024000 )					// If < 1000K
       
    92 		{
       
    93 		TInt sizeInK = 0;
       
    94 
       
    95         if  ( aValue != 0 )
       
    96 			{
       
    97 			sizeInK = I64INT( (aValue + 512) >> 10 );
       
    98 			if  (sizeInK < 1)
       
    99                 {
       
   100 				sizeInK = 1;
       
   101                 }
       
   102 			if  (sizeInK > 999)
       
   103                 {
       
   104 				sizeInK = 999;
       
   105                 }
       
   106 			}
       
   107 
       
   108         buf.Format( KFormatKilo, sizeInK );
       
   109 		}
       
   110 	else
       
   111 		{
       
   112 		TReal sizeInM = I64INT( aValue );
       
   113 		sizeInM /= 1048576;
       
   114 		if  ( sizeInM < 1 )
       
   115             {
       
   116 			sizeInM = 1;
       
   117             }
       
   118 
       
   119         TPtrC pFormat( KFormatMega );
       
   120 		if  ( sizeInM >= 1000 )
       
   121 			{
       
   122 			sizeInM /= 1024;				// Size in G
       
   123 			if  (sizeInM < 1)
       
   124                 {
       
   125 				sizeInM = 1;
       
   126                 }
       
   127 			
       
   128             pFormat.Set( KFormatGiga );
       
   129 			}
       
   130 
       
   131         if  ( sizeInM > 999.9)
       
   132             {
       
   133             sizeInM = 999.9;
       
   134             }
       
   135 
       
   136         if  ( aExtraRounding )
       
   137             {
       
   138 			sizeInM += 0.499999;
       
   139             }
       
   140 
       
   141 		TBuf<16> size;
       
   142 		size.Num( sizeInM, TRealFormat( 14, aDecimalPlaces ) );	// Allow for "17179869184.0"G which is 2^64
       
   143         buf.Format( pFormat, &size );
       
   144 		}
       
   145 
       
   146     return buf;
       
   147     }
       
   148 
       
   149 
       
   150 EXPORT_C TMemSpySizeText MemSpyEngineUtils::FormatSizeText( TInt aValue )
       
   151     {
       
   152     const TInt64 value( aValue );
       
   153 	return FormatSizeText( value );
       
   154     }
       
   155 
       
   156 
       
   157 EXPORT_C TMemSpySizeText MemSpyEngineUtils::FormatSizeTextPrecise( TInt aValue )
       
   158     {
       
   159     /*
       
   160     0123456789
       
   161     ==========
       
   162     1234567890
       
   163 
       
   164     1,234,567,890
       
   165 
       
   166     len = 10;
       
   167     */
       
   168 
       
   169 	TMemSpySizeText buf;
       
   170     buf.Num( aValue );
       
   171     TInt index = buf.Length() - 3;
       
   172     while( index > 0 )
       
   173         {
       
   174         buf.Insert( index, _L(",") );
       
   175         index -= 3;
       
   176         }
       
   177 
       
   178     return buf;
       
   179     }
       
   180 
       
   181 
       
   182 EXPORT_C TBool MemSpyEngineUtils::IsRomAddress( TAny* aAddress )
       
   183     {
       
   184     TBool inRom = EFalse;
       
   185     //
       
   186 #ifdef __EPOC32__
       
   187     const TInt err = User::IsRomAddress( inRom, aAddress );
       
   188     if  ( err != KErrNone )
       
   189         {
       
   190         inRom = EFalse;
       
   191         }
       
   192 #else
       
   193     (void) aAddress;
       
   194     inRom = ETrue;
       
   195 #endif
       
   196     //
       
   197     return inRom;
       
   198     }
       
   199 
       
   200 
       
   201 EXPORT_C void MemSpyEngineUtils::GetCapabilityName( TDes& aBuf, TCapability aCapability )
       
   202     {
       
   203     const TPtrC8 pName( (const TUint8*) CapabilityNames[ aCapability ] );
       
   204     aBuf.Copy( pName );
       
   205     }
       
   206 
       
   207 
       
   208 EXPORT_C TBool MemSpyEngineUtils::StripText( TDes& aText, const TDesC& aStrip )
       
   209     {
       
   210     TBool stripped = EFalse;
       
   211     const TInt stripTextLength = aStrip.Length();
       
   212     //
       
   213     if  ( aText.Length() > stripTextLength )
       
   214         {
       
   215         const TPtrC leftText( aText.Left( stripTextLength ) );
       
   216         if  ( leftText.CompareF( aStrip ) == 0)
       
   217             {
       
   218             // Try to find the first double colon
       
   219             const TInt doubleColonPos = aText.Find( KMemSpyEngineDoubleColon );
       
   220             if  ( doubleColonPos >= stripTextLength )
       
   221                 {
       
   222                 aText.Delete( 0, doubleColonPos + 2 );
       
   223                 }
       
   224             else
       
   225                 {
       
   226                 aText.Delete( 0, stripTextLength );
       
   227                 }
       
   228 
       
   229             stripped = ETrue;
       
   230             }
       
   231         }
       
   232     //
       
   233     return stripped;
       
   234     }
       
   235 
       
   236 
       
   237 EXPORT_C void MemSpyEngineUtils::TextBeforeDoubleColon( TDes& aText )
       
   238     {
       
   239     const TInt doubleColonPos = aText.Find( KMemSpyEngineDoubleColon );
       
   240     if  ( doubleColonPos >= 0 )
       
   241         {
       
   242         aText.SetLength( doubleColonPos );
       
   243         }
       
   244     }
       
   245 
       
   246 
       
   247 EXPORT_C void MemSpyEngineUtils::TextAfterDoubleColon( TDes& aText )
       
   248     {
       
   249     const TInt doubleColonPos = aText.Find( KMemSpyEngineDoubleColon );
       
   250     if  ( doubleColonPos >= 0 )
       
   251         {
       
   252         aText.Delete( 0, doubleColonPos + 2 );
       
   253         }
       
   254     }
       
   255 
       
   256 
       
   257 TPtrC MemSpyEngineUtils::TextAfterLastDoubleColon( const TDesC& aText )
       
   258     {
       
   259     TPtrC ret( aText );
       
   260 
       
   261     // ABCD::123
       
   262     const TInt doubleColonPos = aText.LocateReverseF( ':' );
       
   263     if  ( doubleColonPos > 0 )
       
   264         {
       
   265         if ( aText[ doubleColonPos ] == ':' )
       
   266             {
       
   267             ret.Set( aText.Mid( doubleColonPos + 1 ) );
       
   268             }
       
   269         }
       
   270 
       
   271     return ret;
       
   272     }
       
   273 
       
   274 
       
   275 EXPORT_C TMemSpyPercentText MemSpyEngineUtils::FormatPercentage( TReal aOneHundredPercentValue, TReal aValue )
       
   276     {
       
   277     const TReal value = (TInt) (( aValue / aOneHundredPercentValue) * 100.0);
       
   278     
       
   279     _LIT(KPercentFormat, "%3.2f %%");
       
   280 
       
   281     TMemSpyPercentText val;
       
   282     val.Format( KPercentFormat, value );
       
   283     
       
   284     return val;
       
   285     }
       
   286 
       
   287 
       
   288 EXPORT_C HBufC* MemSpyEngineUtils::CleanupTextLC( const TDesC& aText )
       
   289     {
       
   290     _LIT( KMemSpyTabChar, "\t" );
       
   291     _LIT( KMemSpyTabReplacementChar, " " );
       
   292     _LIT( KMemSpyNewLineChar, "\n" );
       
   293     _LIT( KMemSpyNewLineReplacementChar, " " );
       
   294 
       
   295     TInt pos = KErrNotFound;
       
   296 
       
   297     // Create replacement
       
   298     const TInt originalLength = aText.Length();
       
   299     HBufC* text = HBufC::NewLC( originalLength );
       
   300     TPtr pText( text->Des() );
       
   301     pText.Copy( aText );
       
   302 
       
   303     // Replace tabs
       
   304     pos = pText.Find( KMemSpyTabChar );
       
   305     while( pos >= 0 )
       
   306         {
       
   307         pText.Replace( pos, KMemSpyTabChar().Length(), KMemSpyTabReplacementChar );
       
   308         pos = pText.Find( KMemSpyTabChar );
       
   309         }
       
   310 
       
   311     // Replace tabs
       
   312     pos = pText.Find( KMemSpyNewLineChar );
       
   313     while( pos >= 0 )
       
   314         {
       
   315         pText.Replace( pos, KMemSpyNewLineChar().Length(), KMemSpyNewLineReplacementChar );
       
   316         pos = pText.Find( KMemSpyNewLineChar );
       
   317         }
       
   318 
       
   319     __ASSERT_ALWAYS( pText.Length() == aText.Length(), User::Invariant() );
       
   320     return text;
       
   321     }
       
   322 
       
   323 
       
   324 EXPORT_C void MemSpyEngineUtils::GetRomInfoL( TDes& aPlatform, TDes& aChecksum )
       
   325     {
       
   326     aPlatform.Zero();
       
   327     aChecksum.Zero();
       
   328 
       
   329 #ifdef __EPOC32__
       
   330     // Get checksum 
       
   331     TRomHeader* romHeader = (TRomHeader*) UserSvr::RomHeaderAddress();
       
   332     if  ( romHeader )
       
   333         {
       
   334         aChecksum.Format( _L("0x%08x"), romHeader->iCheckSum );
       
   335         }
       
   336 #endif
       
   337 
       
   338     // Platform version
       
   339     _LIT( KS60VersionDllName, "SysUtil.dll" );
       
   340     RLibrary lib;
       
   341     if  ( lib.Load( KS60VersionDllName ) == KErrNone )
       
   342         {
       
   343         // Get exported version function
       
   344 #ifdef __EPOC32__
       
   345         const TInt KSysUtilOrdinal = 9;
       
   346 #else
       
   347         const TInt KSysUtilOrdinal = 6;
       
   348 #endif
       
   349         TLibraryFunction fn = lib.Lookup( KSysUtilOrdinal );
       
   350         if ( fn != NULL )
       
   351             {
       
   352             TSysUtilGetSWVersionFunction sysUtilGetSWVersion = (TSysUtilGetSWVersionFunction) fn;
       
   353             TInt err = (*sysUtilGetSWVersion)( aPlatform );
       
   354             err = err;
       
   355 #ifdef _DEBUG
       
   356             RDebug::Printf( "MemSpyEngineUtils::GetRomInfoL() - SysUtil::GetSWVersion() returned: %d", err );
       
   357 #endif
       
   358             }
       
   359 
       
   360         lib.Close();
       
   361         }
       
   362     }
       
   363 
       
   364 
       
   365 EXPORT_C void MemSpyEngineUtils::GetFolderL( RFs& aFsSession, TDes& aFolder, const CMemSpyEngineSinkMetaData& aMetaData, const TDriveNumber* aForceDrive )
       
   366     {
       
   367     const TChar KMemSpyDirectorySeparator = '\\';
       
   368 
       
   369     TDriveList drives;
       
   370     User::LeaveIfError( aFsSession.DriveList( drives ) );
       
   371     
       
   372     // We prefer to log to MMC if its available. If not, we'll log to C: instead. On
       
   373     // WINS we prefer to log to C: because its easier to find via Windows Explorer.
       
   374     TDriveNumber logDrive = EDriveC;
       
   375     if  ( aForceDrive )
       
   376         {
       
   377         logDrive = *aForceDrive;
       
   378         TRACE( RDebug::Printf( "MemSpyEngineUtils::GetFolderL() - FORCED TO DRIVE: %c:\\", *aForceDrive + 'A' ) );
       
   379         }
       
   380     else
       
   381         {
       
   382         logDrive = MemSpyEngineUtils::LocateSuitableDrive( aFsSession );
       
   383         }
       
   384 
       
   385     // Prepare the drive buffer
       
   386     HBufC* fileName = HBufC::NewLC( KMaxFileName * 2 );
       
   387     TPtr pFileName( fileName->Des() );
       
   388 
       
   389     // Prepare the drive name
       
   390     TDriveUnit driveUnit( logDrive );
       
   391     pFileName.Append( driveUnit.Name() );
       
   392     pFileName.Append( KMemSpyLogRootPath );
       
   393 
       
   394     // Add any custom folder information
       
   395     if  ( aMetaData.Folder().Length() > 0 )
       
   396         {
       
   397         pFileName.Append( aMetaData.Folder() );
       
   398         TRACE( RDebug::Print( _L("MemSpyEngineUtils::GetFolderL() - client folder: %S" ), &pFileName ) );
       
   399 
       
   400         TChar lastChar = pFileName[ pFileName.Length() - 1 ];
       
   401 
       
   402         // Take into account any "group" timestamp appendix
       
   403         if  ( aMetaData.FolderTimeStamp().Length() )
       
   404             {
       
   405             if  ( lastChar != KMemSpyDirectorySeparator )
       
   406                 {
       
   407                 // Doesn't end with a backslash, so we must
       
   408                 // add separator info before the timestamp
       
   409                 pFileName.Append( ' ' );
       
   410                 pFileName.Append( '-' );
       
   411                 pFileName.Append( ' ' );
       
   412                 }
       
   413 
       
   414             pFileName.Append( aMetaData.FolderTimeStamp() );
       
   415             TRACE( RDebug::Print( _L("MemSpyEngineUtils::GetFolderL() - timestamp folder: %S" ), &pFileName ) );
       
   416             }
       
   417 
       
   418         // Ensure post-fixed by '\\' character
       
   419         lastChar = ( pFileName[ pFileName.Length() - 1 ] );
       
   420         if  ( lastChar != KMemSpyDirectorySeparator )
       
   421             {
       
   422             pFileName.Append( KMemSpyDirectorySeparator );
       
   423             }
       
   424         }
       
   425 
       
   426     // Generate the timestamp file name
       
   427     if  ( aMetaData.UseFileTimeStamp() )
       
   428         {
       
   429         TTime now;
       
   430         now.HomeTime();
       
   431         const TDateTime dateTime( now.DateTime() );
       
   432         pFileName.AppendFormat( KMemSpyLogFileNameWithTimeStamp, dateTime.Year(), dateTime.Month() + 1, dateTime.Day() + 1,
       
   433                                                                  dateTime.Hour(), dateTime.Minute(), dateTime.Second(), dateTime.MicroSecond() );
       
   434         TRACE( RDebug::Print( _L("MemSpyEngineUtils::GetFolderL() - timestamp file: %S" ), &pFileName ) );
       
   435         }
       
   436         
       
   437     // Do we have some context information? If so, make sure its printable
       
   438     HBufC* cleanedContext = NULL;
       
   439     if  ( aMetaData.Context().Length() )
       
   440         {
       
   441         cleanedContext = CleanContextInfoLC( aMetaData.Context() );
       
   442         }
       
   443     else
       
   444         {
       
   445         // This must be the standard log then...
       
   446         cleanedContext = KMemSpyLogDefaultName().AllocLC();
       
   447         }
       
   448 
       
   449     TRACE( RDebug::Print( _L("MemSpyEngineUtils::GetFolderL() - cleaned context: %S" ), cleanedContext ) );
       
   450 
       
   451     // Build final part of file name
       
   452     pFileName.Append( *cleanedContext );
       
   453     CleanupStack::PopAndDestroy( cleanedContext );
       
   454 
       
   455     // and finally, add the extension
       
   456     if  ( aMetaData.Extension().Length() )
       
   457         {
       
   458         pFileName.Append( aMetaData.Extension() );
       
   459         }
       
   460     else
       
   461         {
       
   462         pFileName.Append( KMemSpyLogDefaultExtension );
       
   463         }
       
   464 
       
   465     TRACE( RDebug::Print( _L("MemSpyEngineUtils::GetFolderL() - END - fileName: %S"), fileName ) );
       
   466     if  ( pFileName.Length() > aFolder.MaxLength() )
       
   467         {
       
   468         User::Leave( KErrOverflow );
       
   469         }
       
   470 
       
   471     aFolder.Copy( pFileName );
       
   472     CleanupStack::PopAndDestroy( fileName );
       
   473     }
       
   474 
       
   475 
       
   476 TDriveNumber MemSpyEngineUtils::LocateSuitableDrive( RFs& aFsSession )
       
   477     {
       
   478 #ifndef __WINS__
       
   479     TDriveInfo driveInfo;
       
   480     TDriveList drives;
       
   481     if  ( aFsSession.DriveList( drives ) == KErrNone )
       
   482         {
       
   483         for( TInt i=EDriveY; i>=EDriveD; i-- )
       
   484             {
       
   485             const TDriveNumber drive = static_cast< TDriveNumber >( i );
       
   486             TRACE( RDebug::Print( _L("MemSpyEngineUtils::LocateSuitableDrive() - drive: %c:\\, available: %d"), drive + 'A', drives[ drive ] ) );
       
   487 
       
   488             // Is drive available from an OS perspective?
       
   489             if  ( drives[ drive ] != 0 )
       
   490                 {
       
   491                 // Check whether there is a disk present or not.
       
   492                 const TInt driveInfoErr = aFsSession.Drive( driveInfo, drive );
       
   493                 TRACE( RDebug::Print( _L("MemSpyEngineUtils::LocateSuitableDrive() - drive: %c:\\, driveInfoErr: %d"), drive + 'A', driveInfoErr ) );
       
   494                 if  ( driveInfoErr == KErrNone )
       
   495                     {
       
   496                     // Check if the drive is removable. We'll try to 
       
   497                     // save data here in preference to the system drive if at all
       
   498                     // possible...
       
   499                     const TBool removable = ( driveInfo.iDriveAtt & KDriveAttRemovable );
       
   500                     const TBool ram = ( driveInfo.iType == EMediaRam );
       
   501                     const TBool rom = ( driveInfo.iType == EMediaRom );
       
   502                     TRACE( RDebug::Printf( "MemSpyEngineUtils::LocateSuitableDrive() - drive: %c:\\, removable: %d, ram: %d, rom: %d", drive + 'A', removable, ram, rom ) );
       
   503                     //
       
   504                     if  ( removable && !( ram || rom ) )
       
   505                         {
       
   506                         // Check free space etc
       
   507                         TVolumeInfo volInfo;
       
   508                         const TInt volInfoErr = aFsSession.Volume( volInfo, drive );
       
   509                         TRACE( RDebug::Printf( "MemSpyEngineUtils::LocateSuitableDrive() - drive: %c:\\, volInfoErr: %d", drive + 'A', volInfoErr ) );
       
   510                         if  ( volInfoErr == KErrNone )
       
   511                             {
       
   512                             TRACE( RDebug::Printf( "MemSpyEngineUtils::LocateSuitableDrive() - END - using drive: %c:\\", drive + 'A', removable ) );
       
   513                             return drive;
       
   514                             }
       
   515                         }
       
   516                     }
       
   517                 }
       
   518             }
       
   519 
       
   520         }
       
   521 #else
       
   522 	(void) aFsSession;
       
   523 #endif
       
   524 
       
   525     // Don't use RFs::GetSystemDrive() as it isn't available on v9.1
       
   526     TRACE( RDebug::Printf( "MemSpyEngineUtils::LocateSuitableDrive() - END - fallback to EDriveC" ) );
       
   527     return EDriveC;
       
   528     }
       
   529 
       
   530 
       
   531 void MemSpyEngineUtils::FormatTimeNowL( TDes& aBuf, TBool aLocalTime )
       
   532     {
       
   533     TTime time;
       
   534     if  ( aLocalTime )
       
   535         {
       
   536         time.HomeTime();
       
   537         }
       
   538     else
       
   539         {
       
   540         time.UniversalTime();
       
   541         }
       
   542 
       
   543     FormatTimeL( aBuf, time, aLocalTime );
       
   544     }
       
   545 
       
   546 
       
   547 HBufC* MemSpyEngineUtils::DataStreamFolderNameWithTimeStampLC( const TDesC& aFolderName )
       
   548     {
       
   549     TMemSpyTimeText time;
       
   550     FormatTimeNowL( time, ETrue );
       
   551     //
       
   552     HBufC* folder = HBufC::NewLC( aFolderName.Length() + time.Length() + 10 );
       
   553     TPtr pFolder( folder->Des() );
       
   554     //
       
   555     pFolder.Append( aFolderName );
       
   556     pFolder.Append( time );
       
   557     //
       
   558     return folder;
       
   559     }
       
   560 
       
   561 
       
   562 void MemSpyEngineUtils::GetKernelHeapThreadName( TDes& aBuf, TBool aFullName )
       
   563     {
       
   564     if  ( !aFullName )
       
   565         {
       
   566         aBuf.Copy( KMemSpyKernelProcessName );
       
   567         }
       
   568     else
       
   569         {
       
   570         aBuf.Copy( KMemSpyKernelProcessFullName );
       
   571         }
       
   572 
       
   573     aBuf.Append( KMemSpyEngineDoubleColon );
       
   574     aBuf.Append( KMemSpyKernelThreadName );
       
   575     }
       
   576 
       
   577 
       
   578 void MemSpyEngineUtils::GetKernelHeapThreadAndProcessNames( TDes& aThreadName, TDes& aProcessName )
       
   579     {
       
   580     aThreadName.Copy( KMemSpyKernelThreadName );
       
   581     aProcessName.Copy( KMemSpyKernelProcessName );
       
   582     }
       
   583 
       
   584 
       
   585 TUint32 MemSpyEngineUtils::Hash( const TDesC& aText )
       
   586     {
       
   587     // DJB Hash Function
       
   588     TUint32 hash = 5381;
       
   589     //
       
   590     const TInt length = aText.Length();
       
   591     for( TInt i = 0; i < length; i++)
       
   592         {
       
   593         hash = ((hash << 5) + hash) + aText[ i ];
       
   594         }
       
   595     //
       
   596     return hash;
       
   597     }
       
   598 
       
   599 
       
   600 void MemSpyEngineUtils::Panic( TMemSpyEnginePanic aPanic )
       
   601     {
       
   602     _LIT( KMemSpyEnginePanicCategory, "MemSpyEngine" );
       
   603     User::Panic( KMemSpyEnginePanicCategory, aPanic );
       
   604     }
       
   605 
       
   606 
       
   607 TProcessId MemSpyEngineUtils::IdentifyFileServerProcessIdL()
       
   608     {
       
   609     TRACE( RDebug::Printf( "MemSpyEngineUtils::IdentifyFileServerProcessIdL() - START" ) );
       
   610 
       
   611     TProcessId ret = KNullProcessId;
       
   612 
       
   613     _LIT(KFindMask, "efile*");
       
   614     TFullName name( KFindMask );
       
   615     TFindProcess finder( name );
       
   616 
       
   617     if  ( finder.Next( name ) == KErrNone )
       
   618         {
       
   619         RProcess process;
       
   620         const TInt error = process.Open( name );
       
   621         TRACE( RDebug::Print( _L("MemSpyEngineUtils::IdentifyFileServerProcessIdL() - process open err: %d, name: %S"), error, &name ) );
       
   622         if  ( error == KErrNone )
       
   623             {
       
   624             ret = process.Id();
       
   625             }
       
   626 
       
   627         process.Close();
       
   628         }
       
   629 
       
   630     TRACE( RDebug::Printf( "MemSpyEngineUtils::IdentifyFileServerProcessIdL() - almost done - ret: %d", (TUint) ret ) );
       
   631 
       
   632     if  ( static_cast< TUint >( ret ) == KNullProcessId )
       
   633         {
       
   634         User::Leave( KErrNotFound );
       
   635         }
       
   636 
       
   637     TRACE( RDebug::Printf( "MemSpyEngineUtils::IdentifyFileServerProcessIdL() - END - ret: %d", (TUint) ret ) );
       
   638     return ret;
       
   639     }
       
   640 
       
   641 
       
   642 HBufC* MemSpyEngineUtils::CleanContextInfoLC( const TDesC& aContext )
       
   643     {
       
   644     TRACE( RDebug::Print( _L("MemSpyEngineUtils::CleanContextInfoLC() - START - %S"), &aContext ) );
       
   645     TFileName fileName;
       
   646 
       
   647     TBool seenDoubleColon = EFalse;
       
   648     const TInt length = aContext.Length();
       
   649     for( TInt i=0; i<length; i++ )
       
   650         {
       
   651         const TChar c( aContext[ i ] );
       
   652         const TBool haveNextChar = ( i+1 < length );
       
   653         //
       
   654         //TRACE( RDebug::Print( _L("MemSpyEngineUtils::CleanContextInfoLC() - c[%03d]: \'%c\', haveNextChar: %d, seenDoubleColon: %d"), i, (TUint32) c, haveNextChar, seenDoubleColon ) );
       
   655         //
       
   656         if  ( c == ':' && haveNextChar && aContext[ i + 1 ] == ':' )
       
   657             {
       
   658             // Skip double colon
       
   659             i++;
       
   660             fileName.Append( '-' );
       
   661             seenDoubleColon = ETrue;
       
   662             }
       
   663         else if ( c == '.' )
       
   664             {
       
   665             if  ( seenDoubleColon )
       
   666                 {
       
   667                 break;
       
   668                 }
       
   669             else
       
   670                 {
       
   671                 fileName.Append( '-' );
       
   672                 }
       
   673             }
       
   674         else
       
   675             {
       
   676             fileName.Append( c );
       
   677             }
       
   678         }
       
   679 
       
   680     TRACE( RDebug::Print( _L("MemSpyEngineUtils::CleanContextInfoLC() - END - %S"), &fileName ) );
       
   681     return fileName.AllocLC();
       
   682     }
       
   683 
       
   684