memana/analyzetoolclient/dynamicmemoryhook/src/analyzetoolfastlog.cpp
changeset 0 f0f2b8682603
equal deleted inserted replaced
-1:000000000000 0:f0f2b8682603
       
     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 
       
    19 #include <e32debug.h> // RDebug
       
    20 #include <analyzetool/analyzetooltraceconstants.h>
       
    21 #include "analyzetoolfastlog.h"
       
    22 #include "atlog.h"
       
    23 
       
    24 // Local time function.
       
    25 TInt64 CurrentTime()
       
    26     {
       
    27     LOGSTR1( "ATFL CurrentTime()" );
       
    28     TTime time;
       
    29     time.UniversalTime();
       
    30     return time.Int64() - KMicroSecondsAt1970;
       
    31     }
       
    32 
       
    33 TInt ATFastLogProcessStarted( const TDesC8& aProcessName,
       
    34                                  TUint aProcessId,
       
    35                                  TUint32 aIsDebug )
       
    36     {
       
    37     LOGSTR1( "ATFL ATFastLogProcessStarted()" );
       
    38     // Convert process name to 16-bit descriptor.
       
    39     TBuf<KMaxProcessName> processName;
       
    40     processName.Copy( aProcessName );
       
    41     // Buffer to trace.
       
    42     TBuf<KProcessStartBufLength> buffer;
       
    43     // Format process name and id.
       
    44     buffer.Format( KProcessStart16, &processName, aProcessId );
       
    45     // Timestamp.
       
    46     buffer.AppendNum( CurrentTime(), EHex ) ;
       
    47     // Append udeb/urel information to the process start.
       
    48     buffer.Append( KSpaceXti );  
       
    49     buffer.AppendNum( aIsDebug, EHex );
       
    50     // Append version number.
       
    51     buffer.Append( KSpaceXti );  
       
    52     buffer.AppendNum( KATTraceVersion, EHex );
       
    53     // Log to XTI.
       
    54     RDebug::Print( KXtiMessage, aProcessId ,&buffer );
       
    55     return KErrNone;
       
    56     }
       
    57 
       
    58 TInt ATFastLogProcessEnded( TUint aProcessId, 
       
    59                             TUint aHandleLeakCount )
       
    60     {
       
    61     LOGSTR1( "ATFL ATFastLogProcessEnded()" );
       
    62     // Handle leaks.
       
    63     if ( aHandleLeakCount > 0 )
       
    64         {
       
    65         // Buffer to trace.
       
    66         TBuf<KHandleLeakBufLength> buffer2;
       
    67         buffer2.Format( KHandleLeak16, &KUnknownModule16, aHandleLeakCount );
       
    68         // Trace it.
       
    69         RDebug::Print( KXtiMessage, aProcessId, &buffer2 );
       
    70         }
       
    71     // Process end trace.
       
    72     TBuf<KProcessEndBufLength> buffer;
       
    73     buffer.Format( KProcessEnd16, aProcessId );
       
    74     buffer.AppendNum( CurrentTime(), EHex);
       
    75     buffer.Append( KNewLineXti );
       
    76     RDebug::Print( KXtiMessage, aProcessId, &buffer );
       
    77     return KErrNone;
       
    78     }
       
    79 
       
    80 TInt ATFastLogDllLoaded( TUint aProcessId, 
       
    81                                         const TDesC8& aDllName,
       
    82                                         TUint32 aStartAddress,
       
    83                                         TUint32 aEndAddress )
       
    84     {
       
    85     LOGSTR1( "ATFL ATFastLogDllLoaded()" );
       
    86     // Timestamp.
       
    87     TInt64 time = CurrentTime();
       
    88     // Convert dll name to 16-bit descriptor.
       
    89     TBuf<KMaxLibraryName> dll;
       
    90     dll.Copy( aDllName );
       
    91     // Buffer to trace.
       
    92     TBuf<KDllLoadBufLength> buffer;
       
    93     buffer.Format( KDllLoad16, &dll, time, aStartAddress, aEndAddress );
       
    94     RDebug::Print( KXtiMessage, aProcessId, &buffer );
       
    95     return KErrNone;
       
    96     }
       
    97 
       
    98 TInt ATFastLogDllUnloaded( TUint aProcessId, const TDesC8& aDllName, TUint32 aStartAddress,
       
    99                                        TUint32 aEndAddress )
       
   100     {
       
   101     LOGSTR1( "ATFL ATFastLogDllUnloaded()" );
       
   102     // Timestamp.
       
   103     TInt64 time = CurrentTime();
       
   104     // Convert dll name to 16-bit descriptor.
       
   105     TBuf<KMaxLibraryName> dll;
       
   106     dll.Copy( aDllName );
       
   107     // Buffer to trace.
       
   108     TBuf<KDllLoadBufLength> buffer;
       
   109     buffer.Format( KDllUnload16, &dll, time, aStartAddress, aEndAddress );
       
   110     RDebug::Print( KXtiMessage, aProcessId, &buffer );   
       
   111     return KErrNone;
       
   112     }
       
   113 
       
   114 TInt ATFastLogMemoryAllocated( TUint aProcessId, TUint32 aMemAddress,
       
   115                                   TFixedArray<TUint32, KATMaxCallstackLength>& aCallstack,
       
   116                                   TInt aSize )
       
   117     {
       
   118     LOGSTR1( "ATFL ATFastLogMemoryAllocated()" );
       
   119     // ALLOCH <Memory address> <Time stamp> <Allocation size> <Call stack address count> 
       
   120     // <Call stack address> <Call stack address> ...
       
   121     
       
   122     // Timestamp.
       
   123     TInt64 time = CurrentTime();
       
   124     
       
   125     // Trace buffer and pointer to it.
       
   126     TBufC<KMemAllocBufLength> buffer;
       
   127     TPtr ptr( buffer.Des() );
       
   128     // Append the tag implying a memory allocation line in the data file
       
   129     ptr.Append( KMemoryAllocHeader );
       
   130     
       
   131     // Append the start address of this allocation in the 32-bit (max 8 characters)
       
   132     // hexadecimal text format.
       
   133     ptr.AppendNum( aMemAddress, EHex );
       
   134     
       
   135     // Append the current time in the 64-bit (max 16 characters) hexadecimal text
       
   136     // format
       
   137     ptr.Append( KSpaceXti );
       
   138     ptr.AppendNum( time, EHex );
       
   139     
       
   140     // Append the size of the allocation in the 32-bit (max 8 characters) hexadecimal
       
   141     // text format.
       
   142     ptr.Append( KSpaceXti );
       
   143     ptr.AppendNum( aSize, EHex );
       
   144     
       
   145     // Search call stack for address count.
       
   146     TInt addrCount(0);
       
   147     for ( TInt j = 0; j < aCallstack.Count() ; j++ )
       
   148         {
       
   149         if ( aCallstack.At(j) == 0 )
       
   150             break;
       
   151         addrCount++;
       
   152         }
       
   153     // Current position in call stack.
       
   154     TInt addrPos( 0 );
       
   155     
       
   156     // Append address count.
       
   157     ptr.Append( KSpaceXti );
       
   158     ptr.AppendNum( addrCount, EHex );
       
   159             
       
   160     // Calculate last item length
       
   161     TInt lastItemLength( KXtiMessage().Length() + KHexa32Length + 
       
   162             KSpaceLength + KNewlineLength );
       
   163     
       
   164     TUint packetNumber( 1 );
       
   165     
       
   166     // Go through all call stack's memory addresses associated with
       
   167     // this memory allocation 
       
   168     for ( TInt j = 0; j < addrCount; j++ )
       
   169         {
       
   170         // ALLOCF <Memory address> <Time stamp> <Packet number> 
       
   171         // <Call stack address> <Call stack address> ...
       
   172         if ( ptr.Length() <= 0 )
       
   173             {               
       
   174             // Create alloc fragment message header
       
   175             ptr.Append( KMemoryAllocFragment );
       
   176             ptr.AppendNum( aMemAddress, EHex );
       
   177             ptr.Append( KSpaceXti );
       
   178             ptr.AppendNum( time, EHex );
       
   179             ptr.Append( KSpaceXti );        
       
   180             ptr.AppendNum( packetNumber, EHex );
       
   181             // Increase packet number
       
   182             packetNumber++;
       
   183             }
       
   184       
       
   185         // Append call stack address.
       
   186         ptr.AppendFormat( KHexaNumberXti, aCallstack.At( addrPos ) );
       
   187         
       
   188         // Move the call stack position.
       
   189         addrPos++;
       
   190         
       
   191         // Check if buffer max length exceed
       
   192         if ( lastItemLength + ptr.Length() >= KMemAllocBufLength )
       
   193             {
       
   194             ptr.Append( KNewLineXti );
       
   195             // Log through XTI channel 
       
   196             RDebug::Print( KXtiMessage, aProcessId, &buffer );
       
   197             // Empty XTI buffer
       
   198             ptr.Delete( 0, ptr.MaxLength() );
       
   199             }
       
   200         }
       
   201     // Send last message if exists.
       
   202     if ( ptr.Length() > 0 )
       
   203         {
       
   204         ptr.Append( KNewLineXti );
       
   205         RDebug::Print( KXtiMessage, aProcessId, &buffer);
       
   206         }
       
   207     return KErrNone;
       
   208     }
       
   209 
       
   210 
       
   211 TInt ATFastLogMemoryFreed( TUint aProcessId, TUint32 aMemAddress, 
       
   212                               TFixedArray<TUint32, KATMaxCallstackLength>& aFreeCallstack )
       
   213     {
       
   214     LOGSTR1( "ATFL ATFastLogMemoryFreed()" );
       
   215     // FREEH <Memory address> <Time tamp> <Call stack address count> <Call stack address>
       
   216     // <Call stack address> ...
       
   217     
       
   218     // Timestamp.
       
   219     TInt64 time = CurrentTime();
       
   220     
       
   221     // Trace buffer and pointer to it.
       
   222     TBufC<KMemFreedBufLength> buffer;
       
   223     TPtr ptr( buffer.Des() );
       
   224  
       
   225     // Append the tag implying a memory allocation line in the data file
       
   226     ptr.Append( KMemoryFreedHeader );
       
   227     
       
   228     // Append the start address of this allocation in the 32-bit (max 8 characters)
       
   229     // hexadecimal text format.
       
   230     ptr.AppendNum( aMemAddress, EHex );
       
   231     
       
   232     // Append timestamp;
       
   233     ptr.Append( KSpaceXti );
       
   234     ptr.AppendNum( time, EHex);
       
   235     
       
   236     // Search call stack for address count.
       
   237     TInt addrCount(0);
       
   238     for ( TInt j = 0; j < aFreeCallstack.Count() ; j++ )
       
   239         {
       
   240         if ( aFreeCallstack.At(j) == 0 )
       
   241             break;
       
   242         addrCount++;
       
   243         }
       
   244     // Current position in call stack.
       
   245     TInt addrPos( 0 );
       
   246     
       
   247     // Append address count.
       
   248     ptr.Append( KSpaceXti );
       
   249     ptr.AppendNum( addrCount, EHex );
       
   250             
       
   251     // Calculate last item length
       
   252     TInt lastItemLength( KXtiMessage().Length() + KHexa32Length + 
       
   253             KSpaceLength + KNewlineLength );
       
   254     
       
   255     TUint packetNumber( 1 );
       
   256     
       
   257     // Go through all call stack's memory addresses associated with
       
   258     // this memory allocation 
       
   259     for ( TInt j = 0; j < addrCount; j++ )
       
   260         {
       
   261         // ALLOCF <Memory address> <Time stamp> <Packet number> 
       
   262         // <Call stack address> <Call stack address> ...
       
   263         if ( ptr.Length() <= 0 )
       
   264             {               
       
   265             // Create alloc fragment message header
       
   266             ptr.Append( KMemoryFreedFragment );
       
   267             ptr.AppendNum( aMemAddress, EHex );
       
   268             ptr.Append( KSpaceXti );
       
   269             ptr.AppendNum( packetNumber, EHex );
       
   270             // Increase packet number
       
   271             packetNumber++;
       
   272             }
       
   273       
       
   274         // Append call stack address.
       
   275         ptr.AppendFormat( KHexaNumberXti, aFreeCallstack.At( addrPos ) );
       
   276         
       
   277         // Move the call stack position.
       
   278         addrPos++;
       
   279         
       
   280         // Check if buffer max length exceed
       
   281         if ( lastItemLength + ptr.Length() >= KMemFreedBufLength )
       
   282             {
       
   283             ptr.Append( KNewLineXti );
       
   284             // Log through XTI channel 
       
   285             RDebug::Print( KXtiMessage, aProcessId, &buffer );
       
   286             // Empty XTI buffer
       
   287             ptr.Delete( 0, ptr.MaxLength() );
       
   288             }
       
   289         }
       
   290     // Send last message if exists.
       
   291     if ( ptr.Length() > 0 )
       
   292         {
       
   293         ptr.Append( KNewLineXti );
       
   294         RDebug::Print( KXtiMessage, aProcessId, &buffer);
       
   295         }
       
   296     return KErrNone;   
       
   297     }
       
   298