memspy/Driver/Kernel/Source/SubChannels/MemSpyDriverLogChanHeapBase.cpp
changeset 48 516af714ebb4
parent 45 185201be11b0
child 55 f2950aff7424
equal deleted inserted replaced
45:185201be11b0 48:516af714ebb4
     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 "MemSpyDriverLogChanHeapBase.h"
       
    19 
       
    20 // System includes
       
    21 #include <u32hal.h>
       
    22 #include <e32rom.h>
       
    23 #include <memspy/driver/memspydriverobjectsshared.h>
       
    24 
       
    25 // Shared includes
       
    26 #include "MemSpyDriverOpCodes.h"
       
    27 #include "MemSpyDriverObjectsInternal.h"
       
    28 
       
    29 // User includes
       
    30 #include "MemSpyDriverHeap.h"
       
    31 #include "MemSpyDriverUtils.h"
       
    32 #include "MemSpyDriverDevice.h"
       
    33 #include "MemSpyDriverOSAdaption.h"
       
    34 
       
    35 // Constants
       
    36 const TInt KMemSpyDriverLogChanHeapBaseXferBufferSize = 1024 * 16;
       
    37 
       
    38 
       
    39 
       
    40 
       
    41 DMemSpyDriverLogChanHeapBase::DMemSpyDriverLogChanHeapBase( DMemSpyDriverDevice& aDevice, DThread& aThread )
       
    42 :	DMemSpyDriverLogChanBase( aDevice, aThread )
       
    43     {
       
    44 	TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::DMemSpyDriverLogChanHeapBase() - this: 0x%08x", this ));
       
    45     }
       
    46 
       
    47 
       
    48 DMemSpyDriverLogChanHeapBase::~DMemSpyDriverLogChanHeapBase()
       
    49 	{
       
    50 	}
       
    51 
       
    52 
       
    53 TInt DMemSpyDriverLogChanHeapBase::Construct()
       
    54 	{
       
    55 	TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::Construct() - START - this: 0x%08x", this ));
       
    56     
       
    57     const TInt ret = BaseConstruct( KMemSpyDriverLogChanHeapBaseXferBufferSize );
       
    58 
       
    59 	TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::Construct() - END - this: 0x%08x, err: %d", this, ret ));
       
    60     return ret;
       
    61 	}
       
    62 
       
    63 
       
    64 
       
    65 
       
    66 
       
    67 
       
    68 
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 TInt DMemSpyDriverLogChanHeapBase::Request( TInt aFunction, TAny* a1, TAny* a2 )
       
    74 	{
       
    75 	const TInt r = DMemSpyDriverLogChanBase::Request( aFunction, a1, a2 );
       
    76     return r;
       
    77 	}
       
    78 
       
    79 
       
    80 
       
    81 
       
    82 
       
    83 
       
    84 
       
    85 
       
    86 
       
    87 
       
    88 
       
    89 
       
    90 
       
    91 DMemSpyDriverLogChanHeapBase::TDrmMatchType DMemSpyDriverLogChanHeapBase::IsDrmThread( DThread& aThread )
       
    92     {
       
    93     TDrmMatchType ret = EMatchTypeNone;
       
    94     //
       
    95     const TUid procUid = aThread.iOwningProcess->iUids.iUid[ 2 ];
       
    96     TRACE( Kern::Printf( "DMemSpyDriverLogChanHeapBase::IsDrmThread() - START - aThread: %O, process uid: 0x%08x", &aThread, procUid.iUid ));
       
    97 
       
    98     // Some more rudimentary checks based upon process name and
       
    99     // known uids.
       
   100     TFullName fullName;
       
   101     aThread.FullName( fullName );
       
   102 
       
   103     // Exclude threads containing "DRM"
       
   104     _LIT( KDrmThreadMatchText, "*DRM*" );
       
   105     const TInt matchPos = fullName.MatchF( KDrmThreadMatchText );
       
   106     if ( matchPos >= 0 )
       
   107         {
       
   108         TRACE( Kern::Printf( "DMemSpyDriverLogChanHeapBase::IsDrmThread() - found \'DRM\' at pos: %d (%S)", matchPos, &fullName ));
       
   109         ret = EMatchTypeName;
       
   110         }
       
   111     else
       
   112         {
       
   113         // Some known DRM related process UIDs
       
   114         switch( procUid.iUid )
       
   115             {
       
   116         case 0x10005A22: // DRMEncryptor.exe
       
   117         case 0x01105901: // DRMEncryptor.exe
       
   118         case 0x101F85C7: // DRMRightsManager.exe
       
   119         case 0x10205CA8: // DcfRepSrv.exe
       
   120         case 0x101F51F2: // RightsServer.exe
       
   121         case 0x101F6DC5: // DRMHelperServer.exe
       
   122         case 0x10282F1B: // wmdrmserver.exe
       
   123             ret = EMatchTypeUid;
       
   124             break;
       
   125         default:
       
   126             ret = EMatchTypeNone;
       
   127             break;
       
   128             }
       
   129         }
       
   130 
       
   131     TRACE( Kern::Printf( "DMemSpyDriverLogChanHeapBase::IsDrmThread() - END - procUid: 0x%08x, matchType: %d", procUid.iUid, ret ));
       
   132     return ret;
       
   133     }
       
   134 
       
   135 
       
   136 
       
   137 
       
   138 
       
   139 
       
   140 
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 
       
   153 
       
   154 
       
   155 
       
   156 
       
   157 
       
   158 
       
   159 
       
   160 
       
   161 
       
   162 
       
   163 
       
   164 
       
   165 
       
   166 
       
   167 
       
   168 
       
   169 
       
   170 
       
   171 void DMemSpyDriverLogChanHeapBase::PrintHeapInfo( const TMemSpyHeapInfo& aInfo )
       
   172     {
       
   173     const TMemSpyHeapInfoRHeap& rHeapInfo = aInfo.AsRHeap();
       
   174     //const TMemSpyHeapObjectDataRHeap& rHeapObjectData = rHeapInfo.ObjectData();
       
   175     const TMemSpyHeapStatisticsRHeap& rHeapStats = rHeapInfo.Statistics();
       
   176     const TMemSpyHeapMetaDataRHeap& rHeapMetaData = rHeapInfo.MetaData();
       
   177 
       
   178     /*
       
   179 	TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   180     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator                                      -" ) );
       
   181     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   182     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iAccessCount:       %d", rHeapObjectData.iAccessCount ) );
       
   183     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iHandleCount:       %d", rHeapObjectData.iHandleCount ) );
       
   184     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iHandles:           0x%08x", rHeapObjectData.iHandles ) );
       
   185     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iFlags:             0x%08x", rHeapObjectData.iFlags ) );
       
   186     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iCellCount:         %d", rHeapObjectData.iCellCount ) );
       
   187     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RAllocator::iTotalAllocSize:    %d", rHeapObjectData.iTotalAllocSize ) );
       
   188     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - " ) );
       
   189 
       
   190     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   191     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap                                           -" ) );
       
   192     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   193     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iMinLength:              %d", rHeapObjectData.iMinLength ) );
       
   194     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iMaxLength:              %d", rHeapObjectData.iMaxLength ) );
       
   195     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iOffset:                 %d", rHeapObjectData.iOffset ) );
       
   196     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iGrowBy:                 %d", rHeapObjectData.iGrowBy ) );
       
   197     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iChunkHandle:            0x%08x", rHeapObjectData.iChunkHandle ) );
       
   198     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iBase:                   0x%08x", rHeapObjectData.iBase ) );
       
   199     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iTop:                    0x%08x", rHeapObjectData.iTop ) );
       
   200     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iAlign:                  %d", rHeapObjectData.iAlign ) );
       
   201     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iMinCell:                %d", rHeapObjectData.iAlign ) );
       
   202     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iPageSize:               %d", rHeapObjectData.iAlign ) );
       
   203     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFree.next:              0x%08x", rHeapObjectData.iFree.next ) );
       
   204     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFree.len:               %d", rHeapObjectData.iFree.len ) );
       
   205     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iNestingLevel:           %d", rHeapObjectData.iNestingLevel ) );
       
   206     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iAllocCount:             %d", rHeapObjectData.iAllocCount ) );
       
   207     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFailType:               %d", rHeapObjectData.iFailType ) );
       
   208     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFailRate:               %d", rHeapObjectData.iFailRate ) );
       
   209     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFailed:                 %d", rHeapObjectData.iFailed ) );
       
   210     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iFailAllocCount:         %d", rHeapObjectData.iFailAllocCount ) );
       
   211     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iRand:                   %d", rHeapObjectData.iRand ) );
       
   212     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - RHeap::iTestData:               0x%08x", rHeapObjectData.iTestData ) );
       
   213     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - " ) );
       
   214 	*/
       
   215     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   216     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - Stats (Free)                                    -" ) );
       
   217     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   218     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell count:                     %d", rHeapStats.StatsFree().TypeCount() ) );
       
   219     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell size:                      %d", rHeapStats.StatsFree().TypeSize() ) );
       
   220     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell largest:                   0x%08x", rHeapStats.StatsFree().LargestCellAddress() ) );
       
   221     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell largest size:              %d", rHeapStats.StatsFree().LargestCellSize() ) );
       
   222     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - slack:                          0x%08x", rHeapStats.StatsFree().SlackSpaceCellAddress() ) );
       
   223     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - slack size:                     %d", rHeapStats.StatsFree().SlackSpaceCellSize() ) );
       
   224     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - checksum:                       0x%08x", rHeapStats.StatsFree().Checksum() ) );
       
   225     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - " ) );
       
   226 
       
   227     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   228     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - Stats (Alloc)                                   -" ) );
       
   229     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   230     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell count:                     %d", rHeapStats.StatsAllocated().TypeCount() ) );
       
   231     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell size:                      %d", rHeapStats.StatsAllocated().TypeSize() ) );
       
   232     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell largest:                   0x%08x", rHeapStats.StatsAllocated().LargestCellAddress() ) );
       
   233     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell largest size:              %d", rHeapStats.StatsAllocated().LargestCellSize() ) );
       
   234     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - " ) );
       
   235 
       
   236     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   237     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - Misc. Info                                      -" ) );
       
   238     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() ---------------------------------------------------" ) );
       
   239     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - chunk size:                     %d", rHeapMetaData.ChunkSize() ) );
       
   240     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - chunk handle:                   0x%08x", rHeapMetaData.ChunkHandle() ) );
       
   241     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - chunk base address:             0x%08x", rHeapMetaData.ChunkBaseAddress() ) );
       
   242     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - debug allocator:                %d", rHeapMetaData.IsDebugAllocator() ) );
       
   243     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - shared heap:                    %d", rHeapMetaData.IsSharedHeap() ) );
       
   244     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - user thread:                    %d", rHeapMetaData.IsUserThread() ) );
       
   245     //TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell header size (free):        %d", rHeapMetaData.HeaderSizeFree() ) );
       
   246     //TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - cell header size (alloc):       %d", rHeapMetaData.HeaderSizeAllocated() ) );
       
   247     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - heap vTable:                    0x%08x", rHeapMetaData.VTable() ) );
       
   248     //TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - heap object size:               %d", rHeapMetaData.ClassSize() ) );
       
   249     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - heap size:                      %d", rHeapMetaData.iHeapSize ) );
       
   250     TRACE( Kern::Printf("DMemSpyDriverLogChanHeapBase::PrintHeapInfo() - allocator address:              0x%08x", rHeapMetaData.iAllocatorAddress ) );
       
   251     }
       
   252 
       
   253 TInt DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel(RMemSpyDriverRHeapBase& aHeap, TMemSpyHeapInfo* aHeapInfo, TDes8* aTransferBuffer )
       
   254     {
       
   255     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel() - START - aTransferBuffer: 0x%08x", aTransferBuffer ) );
       
   256 
       
   257     TInt r = KErrNone;
       
   258     NKern::ThreadEnterCS();
       
   259 
       
   260     // This object holds all of the info we will accumulate for the client.
       
   261     TMemSpyHeapInfo masterHeapInfo;
       
   262     masterHeapInfo.SetType(aHeap.GetTypeFromHelper());
       
   263     masterHeapInfo.SetTid( 2 );
       
   264     masterHeapInfo.SetPid( 1 );
       
   265 
       
   266     // This is the RHeap-specific object that contains all RHeap info
       
   267     TMemSpyHeapInfoRHeap& rHeapInfo = masterHeapInfo.AsRHeap();
       
   268 
       
   269 
       
   270     // When walking the kernel heap we must keep track of the free cells
       
   271     // without allocating any more memory (on the kernel heap...)
       
   272     //
       
   273     // Therefore, we start a stream immediately, which is actually already
       
   274     // pre-allocated.
       
   275     //
       
   276     // Start stream and pad with zero count, which we'll repopulate later on
       
   277     // once we know the final score.
       
   278     RMemSpyMemStreamWriter stream;
       
   279     TInt32* pCount = NULL;
       
   280 
       
   281     // We must walk the client's heap in order to build statistics
       
   282     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - calling heap walker constructor..."));
       
   283     RMemSpyDriverHeapWalker heapWalker(aHeap);
       
   284     if  ( aTransferBuffer )
       
   285         {
       
   286         // This will allow us to identify that we're writing directly to the stream
       
   287         stream = OpenXferStream();
       
   288         iStackStream = &stream;
       
   289 
       
   290         // Writer marker value which we'll update after the traversal completes
       
   291         pCount = stream.WriteInt32( 0 );
       
   292 
       
   293         // Receive cell info as we walk the heap...
       
   294         heapWalker.SetObserver( this );
       
   295         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - collecting free cells - iStackStream: 0x%08x, isOpen: %d, pCount: 0x%08x", iStackStream, stream.IsOpen(), pCount ));
       
   296         }
       
   297     else
       
   298         {
       
   299         iStackStream = NULL;
       
   300         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - not collecting free cells"));
       
   301         }
       
   302 
       
   303     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - locking system..." ));
       
   304     NKern::LockSystem();
       
   305     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - disabling interrupts..." ));
       
   306     const TInt irq = NKern::DisableAllInterrupts();
       
   307     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - starting traversal..." ));
       
   308 
       
   309 #if defined( TRACE_TYPE_KERNELHEAP )
       
   310     heapWalker.SetPrintDebug();
       
   311 #endif
       
   312     r = heapWalker.Traverse();
       
   313 
       
   314     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - restoring interrupts..." ));
       
   315     NKern::RestoreInterrupts( irq );
       
   316     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - finished traversal - err: %d, iStackStream: 0x%08x, pCount: 0x%08x, isOpen: %d", r, iStackStream, pCount, ( iStackStream == NULL ? 0 : iStackStream->IsOpen() ) ));
       
   317     NKern::UnlockSystem();
       
   318     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - unlocked system" ));
       
   319 
       
   320     // Write free cells if requested
       
   321     if  ( r == KErrNone && iStackStream && iStackStream->IsOpen() && pCount )
       
   322         {
       
   323         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - final free cell count: %d", iFreeCellCount ));
       
   324         *pCount = iFreeCellCount;
       
   325         r = stream.WriteAndClose( aTransferBuffer );
       
   326         iStackStream = NULL;
       
   327         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - stream commit result: %d", r ));
       
   328         }
       
   329 
       
   330     TMemSpyHeapStatisticsRHeap& rHeapStats = rHeapInfo.Statistics();
       
   331     heapWalker.CopyStatsTo( rHeapStats );
       
   332 
       
   333     // Get remaining meta data that isn't stored elsewhere
       
   334     TMemSpyHeapMetaDataRHeap& rHeapMetaData = rHeapInfo.MetaData();
       
   335 	TFullName chunkName;
       
   336 	aHeap.Chunk().FullName(chunkName);
       
   337     rHeapMetaData.SetChunkName(chunkName);
       
   338     rHeapMetaData.SetChunkSize( (TUint) aHeap.Chunk().Size() );
       
   339     rHeapMetaData.SetChunkHandle( &aHeap.Chunk() );
       
   340     rHeapMetaData.SetChunkBaseAddress( OSAdaption().DChunk().GetBase(aHeap.Chunk()) );
       
   341     rHeapMetaData.SetDebugAllocator(aHeap.Helper()->AllocatorIsUdeb());
       
   342     rHeapMetaData.SetUserThread( EFalse );
       
   343     rHeapMetaData.SetSharedHeap( ETrue );
       
   344 	rHeapMetaData.iHeapSize = aHeap.Helper()->CommittedSize();
       
   345 	rHeapMetaData.iAllocatorAddress = (TAny*)aHeap.Helper()->AllocatorAddress();
       
   346 	rHeapMetaData.iMinHeapSize = aHeap.Helper()->MinCommittedSize();
       
   347 	rHeapMetaData.iMaxHeapSize = aHeap.Helper()->MaxCommittedSize();
       
   348 
       
   349     PrintHeapInfo( masterHeapInfo );
       
   350 
       
   351     // Update info ready for writing back to the user-side
       
   352     if  ( r == KErrNone )
       
   353         {
       
   354         // Write results back to user-side
       
   355         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel - writing to user-side..."));
       
   356         r = Kern::ThreadRawWrite( &ClientThread(), aHeapInfo, &masterHeapInfo, sizeof( TMemSpyHeapInfo ) );
       
   357         }
       
   358 
       
   359     NKern::ThreadLeaveCS();
       
   360 
       
   361 	TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::GetHeapInfoKernel() - END - ret: %d", r) );
       
   362     return r;
       
   363     }
       
   364 
       
   365 
       
   366 
       
   367 
       
   368 
       
   369 
       
   370 
       
   371 
       
   372 
       
   373 
       
   374     
       
   375 
       
   376 
       
   377 
       
   378 
       
   379 
       
   380 
       
   381 TBool DMemSpyDriverLogChanHeapBase::HandleHeapCell(TMemSpyDriverCellType aCellType, TAny* aCellAddress, TInt aLength, TInt /*aNestingLevel*/, TInt /*aAllocNumber*/)
       
   382     {
       
   383     TInt error = KErrNone;
       
   384     //
       
   385     if  (aCellType & EMemSpyDriverFreeCellMask)
       
   386         {
       
   387         if  ( iStackStream )
       
   388             {
       
   389             if  ( !iStackStream->IsFull() )
       
   390                 {
       
   391                 ++iFreeCellCount;
       
   392                 TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::HandleHeapCell - writing free cell %d @ 0x%08x, space left: %u", iFreeCellCount, aCellAddress, iStackStream->Remaining() ));
       
   393                 //
       
   394                 iStackStream->WriteInt32( aCellType );
       
   395                 iStackStream->WriteUint32( reinterpret_cast<TUint32>( aCellAddress ) );
       
   396                 iStackStream->WriteInt32( aLength );
       
   397                 }
       
   398             else
       
   399                 {
       
   400                 Kern::Printf( "DMemSpyDriverLogChanHeapBase::HandleHeapCell - Kernel Free Cell stack stream IS FULL!" );
       
   401                 error = KErrAbort;
       
   402                 }
       
   403             }
       
   404        }
       
   405     //
       
   406     return ( error == KErrNone );
       
   407     }
       
   408 
       
   409 
       
   410 void DMemSpyDriverLogChanHeapBase::HandleHeapWalkInit()
       
   411 	{
       
   412 	iFreeCellCount = 0;
       
   413 	}
       
   414 
       
   415 TInt DMemSpyDriverLogChanHeapBase::OpenKernelHeap( RHeapK*& aHeap, DChunk*& aChunk, TDes8* aClientHeapChunkName )
       
   416     {
       
   417     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap() - START") );
       
   418 
       
   419     // This is what we're searching for...
       
   420     RHeapK* kernelHeap = NULL;
       
   421     DChunk* kernelHeapChunk = NULL;
       
   422 
       
   423     // Find the SvHeap chunk....
       
   424     _LIT( KKernelServerHeapChunkName, "SvHeap" );
       
   425  	NKern::ThreadEnterCS();
       
   426    
       
   427     DObjectCon* chunkContainer = Kern::Containers()[EChunk];
       
   428     chunkContainer->Wait();
       
   429     NKern::LockSystem();
       
   430     const TInt chunkCount = chunkContainer->Count();
       
   431 
       
   432     for(TInt i=0; i<chunkCount; i++)
       
   433         {
       
   434         DChunk* chunk = (DChunk*) (*chunkContainer)[ i ];
       
   435         //
       
   436         if  ( chunk->NameBuf() )
       
   437             {
       
   438             const TInt findResult = chunk->NameBuf()->Find( KKernelServerHeapChunkName );
       
   439     	    TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap - checking chunk: %O against %S => %d", chunk, &KKernelServerHeapChunkName, findResult ) );
       
   440             if  ( findResult != KErrNotFound )
       
   441                 {
       
   442                 // Found it.
       
   443                 kernelHeapChunk = chunk;
       
   444                 TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - found chunk with base: 0x%08x", chunk->iBase ) );
       
   445                 break;
       
   446                 }
       
   447             }
       
   448         }
       
   449 
       
   450     NKern::UnlockSystem();
       
   451     chunkContainer->Signal();
       
   452 
       
   453     TInt r = KErrNotFound;
       
   454     if  ( kernelHeapChunk != NULL )
       
   455         {
       
   456 #ifndef __WINS__
       
   457         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - kernelHeapChunk: 0x%08x", kernelHeapChunk ) );
       
   458 
       
   459         const TRomHeader& romHdr = Epoc::RomHeader();
       
   460 	    const TRomEntry* primaryEntry = (const TRomEntry*) Kern::SuperPage().iPrimaryEntry;
       
   461         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - primaryEntry: 0x%08x, primaryEntry->iAddressLin: 0x%08x", primaryEntry, primaryEntry->iAddressLin ) );
       
   462 	    const TRomImageHeader* primaryImageHeader = (const TRomImageHeader*) primaryEntry->iAddressLin;
       
   463         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - primaryEntry: 0x%08x", primaryImageHeader ) );
       
   464 
       
   465         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iCodeSize:            0x%08x", primaryImageHeader->iCodeSize ) );
       
   466         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iTextSize:            0x%08x", primaryImageHeader->iTextSize ) );
       
   467         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iDataSize:            0x%08x", primaryImageHeader->iDataSize ) );
       
   468         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iBssSize:             0x%08x", primaryImageHeader->iBssSize ) );
       
   469         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iHeapSizeMin:         0x%08x", primaryImageHeader->iHeapSizeMin ) );
       
   470         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iHeapSizeMax:         0x%08x", primaryImageHeader->iHeapSizeMax ) );
       
   471         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - TRomImageHeader::iStackSize:           0x%08x", primaryImageHeader->iStackSize ) );
       
   472 
       
   473         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - romHdr.iKernDataAddress: 0x%08x", romHdr.iKernDataAddress ) );
       
   474         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - Kern::RoundToPageSize( romHdr.iTotalSvDataSize ): 0x%08x", Kern::RoundToPageSize( romHdr.iTotalSvDataSize ) ) );
       
   475         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - Kern::RoundToPageSize( kernelProcessCreateInfo.iStackSize ): 0x%08x", Kern::RoundToPageSize( primaryImageHeader->iStackSize ) ) );
       
   476 
       
   477         TAny* stack = (TAny*)( romHdr.iKernDataAddress + Kern::RoundToPageSize( romHdr.iTotalSvDataSize ));
       
   478         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - aStack: 0x%08x", stack ) );
       
   479         
       
   480         // NB: This is supposed to be Kern::RoundToPageSize( kernelProcessCreateInfo.iStackSize ) but that
       
   481         // sometimes returns very dodgy values on ARMv5 Multiple Memory Model when using MemSpy's driver
       
   482         // installed via a SIS file. No idea why. Cache problem? 
       
   483         TAny* heap = (TAny*)(TLinAddr( stack ) + Kern::RoundToPageSize( primaryImageHeader->iStackSize ));
       
   484         TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - aHeap: 0x%08x", heap ) );
       
   485 
       
   486         kernelHeap = (RHeapK*) heap;
       
   487 #else
       
   488         kernelHeap = (RHeapK*) kernelHeapChunk->Base();
       
   489 #endif
       
   490         // Finalise construction of heap 
       
   491         if  ( kernelHeap != NULL )
       
   492             {
       
   493             TRACE_KH( Kern::Printf( "DMemSpyDriverLogChanHeapBase::OpenKernelHeap - kernelHeapChunk->Base(): 0x%08x", kernelHeapChunk->Base() ) );
       
   494             aHeap = kernelHeap;
       
   495             aChunk = kernelHeapChunk;
       
   496 
       
   497             // Get the chunk name (if the caller asked for it)
       
   498             if  ( aClientHeapChunkName )
       
   499                 {
       
   500                 kernelHeapChunk->FullName( *aClientHeapChunkName );
       
   501                 }
       
   502 
       
   503             // Opened okay
       
   504             r = KErrNone;
       
   505             }
       
   506         else
       
   507             {
       
   508             TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap - kernel heap was NULL..."));
       
   509             }
       
   510         }
       
   511     else
       
   512         {
       
   513         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap - couldnt find kernel chunk..."));
       
   514         r = KErrNotFound;
       
   515         }
       
   516 
       
   517  	NKern::ThreadLeaveCS();
       
   518 
       
   519 	TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap() - END - ret: %d", r ) );
       
   520     return r;
       
   521     }
       
   522 
       
   523 TInt DMemSpyDriverLogChanHeapBase::OpenKernelHeap( RMemSpyDriverRHeapKernelFromCopy& aHeap, TDes8* aClientHeapChunkName )
       
   524     {
       
   525     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - START") );
       
   526 
       
   527     RHeapK* heap = NULL;
       
   528     DChunk* chunk = NULL;
       
   529     TInt r = OpenKernelHeap( heap, chunk, aClientHeapChunkName );
       
   530 	
       
   531     TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - open err: %d", r ) );
       
   532     if  ( r == KErrNone )
       
   533         {
       
   534 #ifdef __SYMBIAN_KERNEL_HYBRID_HEAP__
       
   535 		// RAllocator::Size() not exported on hybrid heap
       
   536 		const TInt heapSize = heap->DebugFunction(RAllocator::EGetSize);
       
   537 #else
       
   538         const TInt heapSize = heap->Size();
       
   539 #endif
       
   540         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - heapSize: %d, heap: 0x%08x, chunkBase: 0x%08x", heapSize, heap, chunk->Base() ) );
       
   541 
       
   542         // Make a new chunk that we can copy the kernel heap into. We cannot lock the system the entire time
       
   543         // we need to do this, therefore there is no guarantee that the chunk will be large enough to hold the
       
   544         // (current) heap data at the time we need to make the copy. We oversize the chunk by 1mb in the "hope"
       
   545         // that it will be enough... :(
       
   546         TChunkCreateInfo info;
       
   547         info.iType         = TChunkCreateInfo::ESharedKernelSingle;
       
   548         info.iMaxSize      = heapSize + ( 1024 * 1024 );
       
   549         info.iOwnsMemory   = ETrue; // Use memory from system's free pool
       
   550         info.iDestroyedDfc = NULL;
       
   551         #ifdef __EPOC32__
       
   552         info.iMapAttr      = (TInt)EMapAttrFullyBlocking; // Full caching
       
   553         #endif
       
   554 
       
   555         // Holds a copy of the client's heap chunk
       
   556         DChunk* heapCopyChunk;
       
   557         TLinAddr heapCopyChunkAddress;
       
   558         TUint32 heapCopyChunkMappingAttributes;
       
   559         r = Kern::ChunkCreate( info, heapCopyChunk, heapCopyChunkAddress, heapCopyChunkMappingAttributes );
       
   560         TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - creating chunk returned: %d", r));
       
   561 
       
   562         if  ( r == KErrNone )
       
   563             {
       
   564             TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - copy chunk base: 0x%08x, heapCopyChunkAddress: 0x%08x", heapCopyChunk->iBase, heapCopyChunkAddress));
       
   565 
       
   566             // Commit memory for entire buffer
       
   567             TUint32 physicalAddress = 0;
       
   568             r = Kern::ChunkCommitContiguous( heapCopyChunk, 0, heapSize, physicalAddress );
       
   569             TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - commiting chunk returned: %d", r));
       
   570 
       
   571             if  ( r != KErrNone)
       
   572                 {
       
   573                 // On error, throw away the chunk we have created
       
   574                 Kern::ChunkClose( heapCopyChunk );
       
   575                 heapCopyChunk = NULL;
       
   576                 }
       
   577             else
       
   578                 {
       
   579                 TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - heapCopyChunk->iSize: 0x%08x, heapCopyChunk->iBase: 0x%08x, heapCopyChunkAddress: 0x%08x, physicalAddress: 0x%08x", heapCopyChunk->iSize, heapCopyChunk->iBase, heapCopyChunkAddress, physicalAddress));
       
   580 
       
   581                 NKern::LockSystem();
       
   582                 const TUint32 copyLength = heapSize; // TODO Min( heap->Size(), heapSize );
       
   583 
       
   584                 TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - trying to copy %d (vs orig estimate of %d) bytes from kernel allocator address: 0x%08x", copyLength, heapSize, heap));
       
   585                 memcpy( (TUint8*) heapCopyChunkAddress, heap, copyLength );
       
   586 
       
   587                 NKern::UnlockSystem();
       
   588 
       
   589                 TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - copied kernel heap data" ));
       
   590 
       
   591                 // Transfer ownership of the copy heap chunk to the heap object. This also calculates the delta
       
   592                 // beween the heap addresses in the client's address space and the kernel address space.
       
   593                 TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - associate chunk and transfer ownership..." ));
       
   594                 aHeap.SetKernelHeap( *heap );
       
   595                 aHeap.AssociateWithKernelChunk( heapCopyChunk, heapCopyChunkAddress, heapCopyChunkMappingAttributes );
       
   596                 }
       
   597             }
       
   598         else
       
   599             {
       
   600 	        TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - copy chunk create error: %d", r ) );
       
   601             }
       
   602         }
       
   603 
       
   604 	TRACE_KH( Kern::Printf("DMemSpyDriverLogChanHeapBase::OpenKernelHeap(CP) - END - ret: %d", r ) );
       
   605     return r;
       
   606     }
       
   607 
       
   608 
       
   609