memspy/MemSpyClient/src/memspysession.cpp
changeset 17 67c6ff54ec25
child 22 a009639409f5
equal deleted inserted replaced
15:ccab7f1f8266 17:67c6ff54ec25
       
     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 "memspysession.h"
       
    20 #include <memspy/api/memspyapiprocess.h>
       
    21 #include <memspy/api/memspyapikernelobject.h>
       
    22 #include <memspy/api/memspyapikernelobjectitem.h>
       
    23 //KernelObjects
       
    24 #include <memspy/driver/memspydriverenumerationsshared.h>
       
    25 // IMPLEMENTATION
       
    26 
       
    27 EXPORT_C RMemSpySession::RMemSpySession()
       
    28     {
       
    29     }
       
    30 
       
    31 
       
    32 EXPORT_C TInt RMemSpySession::Connect()
       
    33     {
       
    34 	TInt error(KErrNone);
       
    35 	
       
    36 	for (TInt i=0; i<2; i++) // Two retries max
       
    37 		{
       
    38 		TInt error = CreateSession(KMemSpyServer2, TVersion(KMemSpyVersion, 0, 0));
       
    39 		
       
    40 		if (error != KErrNotFound && error != KErrServerTerminated)
       
    41 			return error;
       
    42 		
       
    43 		error = StartServer();
       
    44 		
       
    45 		if (error != KErrNone && error != KErrAlreadyExists)
       
    46 			return error;
       
    47 		}
       
    48 	
       
    49 	return error;	
       
    50     }
       
    51 
       
    52 TInt RMemSpySession::StartServer()
       
    53 	{
       
    54 	RProcess server;
       
    55 	_LIT(KCommand, "start");
       
    56 	const TUid KServerUid3 = {0xE5883BC2};
       
    57 	TInt error = server.Create(KMemSpyServer2, KCommand);//, KServerUid3);
       
    58 	
       
    59 	if (error != KErrNone)
       
    60 		return error;
       
    61 	
       
    62 	TRequestStatus startStatus, stopStatus;
       
    63 	server.Logon(stopStatus);
       
    64 	if (stopStatus != KRequestPending)
       
    65 		{
       
    66 		User::WaitForRequest(stopStatus);
       
    67 		server.Kill(0);
       
    68 		server.Close();
       
    69 		return stopStatus.Int();
       
    70 		}
       
    71 	
       
    72 	server.Rendezvous(startStatus);
       
    73 	server.Resume();
       
    74 	User::WaitForRequest(startStatus, stopStatus);
       
    75 	if (startStatus == KRequestPending)
       
    76 		{
       
    77 		// not started yet, i.e. stopStatus was signaled
       
    78 		server.Close();
       
    79 		return stopStatus.Int();
       
    80 		}
       
    81 	
       
    82 	// Rendezvous was called - the server is ready
       
    83 	
       
    84 	// We first need to cancel Logon
       
    85 	server.LogonCancel(stopStatus);
       
    86 	// We don't need this anymore
       
    87 	server.Close();
       
    88 	// Wait for LogonCancel to complete
       
    89 	User::WaitForRequest(stopStatus);
       
    90 
       
    91 	// Everything went OK
       
    92 	return KErrNone;
       
    93 	}
       
    94 
       
    95 //inline void RMemSpySession::Close()
       
    96 //    {
       
    97 //    RSessionBase::Close();
       
    98 //    }
       
    99 //
       
   100 //inline TMemSpySinkType RMemSpySession::GetSinkType()
       
   101 //	{
       
   102 //	TPckgBuf<TMemSpySinkType> OutBuf;
       
   103 //	TIpcArgs args( &OutBuf );
       
   104 //	SendReceive( EGetSinkType, args );
       
   105 //	return OutBuf();
       
   106 //	}
       
   107 //
       
   108 //inline void RMemSpySession::OutputKernelObjects()
       
   109 //	{
       
   110 //	SendReceive( EOutputKernelObjects );
       
   111 //	}
       
   112 //
       
   113 //inline void RMemSpySession::OutputToDebug()
       
   114 //	{
       
   115 //	SendReceive( EOutputToDebug );
       
   116 //	}
       
   117 //
       
   118 //inline void RMemSpySession::OutputToFile()
       
   119 //	{
       
   120 //	SendReceive( EOutputToFile );
       
   121 //	}
       
   122 //
       
   123 //inline void RMemSpySession::SetServerTimeOutStatus( TUint32 aValue, TBool aEnabled )
       
   124 //	{
       
   125 //	TPckgBuf<TUint32> In1(aValue);
       
   126 //	TPckgBuf<TBool> In2(aEnabled);
       
   127 //	TIpcArgs args( &In1, &In2 );
       
   128 //	SendReceive( ESetServerTimeOutStatus, args );	
       
   129 //	}
       
   130 
       
   131 //inline void RMemSpySession::OutputProcessInfo( TMemSpyProcess aProcess )
       
   132 //	{
       
   133 //	TProcessId iId = aProcess.iId;
       
   134 //	TPckgBuf<TProcessId> In( iId );
       
   135 //	TIpcArgs args( &In );
       
   136 //	SendReceive( EOutputProcessInfo, args );
       
   137 //	}
       
   138 
       
   139 ////Processes operations
       
   140 //inline TInt RMemSpySession::ProcessesCount()
       
   141 //	{
       
   142 //	TPckgBuf<TInt> Out;
       
   143 //	TIpcArgs args( &Out );
       
   144 //	SendReceive( EProcessesCount, args );
       
   145 //	return Out();
       
   146 //	}
       
   147 //
       
   148 //inline TMemSpyProcess RMemSpySession::GetProcessByIndex( TInt aIndex )
       
   149 //	{
       
   150 //	TPckgBuf<TInt> In( aIndex );
       
   151 //	TPckgBuf<TMemSpyProcess> Out;
       
   152 //	TIpcArgs args( &In, &Out );
       
   153 //	SendReceive( EProcessByIndex, args );
       
   154 //	return Out();
       
   155 //	}
       
   156 //
       
   157 //inline TInt RMemSpySession::ProcessIndexById( TProcessId aId )
       
   158 //	{
       
   159 //	TPckgBuf<TProcessId> In( aId );
       
   160 //	TPckgBuf<TInt> Out;
       
   161 //	TIpcArgs args( &In, &Out );
       
   162 //	SendReceive( EProcessIndexById, args );
       
   163 //	return Out();
       
   164 //	}
       
   165 //
       
   166 //inline TBool RMemSpySession::ProcessIsDead( TMemSpyProcess aProcess )
       
   167 //	{
       
   168 //	TProcessId iId = aProcess.iId;
       
   169 //	TPckgBuf<TProcessId> In( iId );
       
   170 //	TPckgBuf<TBool> Out;
       
   171 //	TIpcArgs args( &In, &Out );
       
   172 //	SendReceive( EProcessIsDead, args );
       
   173 //	return Out();
       
   174 //	}
       
   175 
       
   176 EXPORT_C void RMemSpySession::GetProcessesL(RArray<CMemSpyApiProcess*> &aProcesses, TSortType aSortType)
       
   177 	{
       
   178 	TPckgBuf<TInt> count;
       
   179 	User::LeaveIfError(SendReceive(EGetProcessCount, TIpcArgs(&count)));
       
   180 	
       
   181 	TInt requestedCount = count();
       
   182 	HBufC8* buffer = HBufC8::NewLC(requestedCount * sizeof(TMemSpyProcessData));
       
   183 	TPtr8 bufferPtr(buffer->Des());
       
   184 	
       
   185 	User::LeaveIfError(SendReceive(EGetProcesses, TIpcArgs(&count, &bufferPtr)));
       
   186 	aProcesses.Reset();
       
   187 	
       
   188 	TInt receivedCount = Min(count(), requestedCount);
       
   189 	for(TInt i=0, offset = 0; i<requestedCount; i++, offset+=sizeof(TMemSpyProcessData))
       
   190 		{
       
   191 		TPckgBuf<TMemSpyProcessData> data;
       
   192 		data.Copy(bufferPtr.Ptr()+offset, sizeof(TMemSpyProcessData));
       
   193 		aProcesses.AppendL(CMemSpyApiProcess::NewLC(data()));
       
   194 		}
       
   195 	CleanupStack::Pop(aProcesses.Count());
       
   196 	CleanupStack::PopAndDestroy(buffer);
       
   197 	}
       
   198 
       
   199 EXPORT_C TInt RMemSpySession::ProcessSystemPermanentOrCritical( TProcessId aId, TBool aValue )
       
   200 	{
       
   201 	TPckgBuf<TProcessId> arg1( aId );
       
   202 	TPckgBuf<TBool> arg2( aValue );
       
   203 	TIpcArgs args( &arg1, &arg2 );
       
   204 		
       
   205 	TInt error = SendReceive( EProcessSystemPermanentOrCritical, args );
       
   206 	
       
   207 	return error;
       
   208 	}
       
   209 
       
   210 EXPORT_C TInt RMemSpySession::EndProcess( TProcessId aId, TEndType aType )
       
   211 	{
       
   212 	TPckgBuf<TProcessId> arg1( aId );
       
   213 	TPckgBuf<TEndType> arg2( aType );
       
   214 	TIpcArgs args( &arg1, &arg2 );
       
   215 	
       
   216 	TInt error = SendReceive( EEndProcess, args );
       
   217 	
       
   218 	return error;
       
   219 	}
       
   220 
       
   221 EXPORT_C TInt RMemSpySession::SwitchToProcess( TProcessId aId, TBool aBrought )
       
   222 	{
       
   223 	TPckgBuf<TProcessId> arg1( aId );
       
   224 	TPckgBuf<TBool> arg2( aBrought );
       
   225 	TIpcArgs args( &arg1, &arg2 );
       
   226 	
       
   227 	TInt error = SendReceive( ESwitchToProcess, args );
       
   228 	
       
   229 	return error;	
       
   230 	}
       
   231 
       
   232 EXPORT_C void RMemSpySession::GetThreadsL(TProcessId aProcessId, RArray<CMemSpyApiThread*> &aThreads, TSortType aSortType)
       
   233 	{
       
   234 	TPckgBuf<TInt> count;
       
   235 	TPckgBuf<TProcessId> pid(aProcessId);
       
   236 	User::LeaveIfError(SendReceive(EGetThreadCount, TIpcArgs(&count, &pid)));
       
   237 	
       
   238 	TInt requestedCount = count();
       
   239 	HBufC8* buffer = HBufC8::NewLC(requestedCount * sizeof(TMemSpyThreadData));
       
   240 	TPtr8 bufferPtr(buffer->Des());
       
   241 	
       
   242 	User::LeaveIfError(SendReceive(EGetThreads, TIpcArgs(&count, &bufferPtr, &pid)));
       
   243 	aThreads.Reset();
       
   244 	
       
   245 	TInt receivedCount = Min(count(), requestedCount);
       
   246 	for(TInt i=0, offset = 0; i<requestedCount; i++, offset+=sizeof(TMemSpyThreadData))
       
   247 		{
       
   248 		TPckgBuf<TMemSpyThreadData> data;
       
   249 		data.Copy(bufferPtr.Ptr()+offset, sizeof(TMemSpyThreadData));
       
   250 		aThreads.AppendL(CMemSpyApiThread::NewLC(data()));
       
   251 		}
       
   252 	CleanupStack::Pop(aThreads.Count());
       
   253 	CleanupStack::PopAndDestroy(buffer);
       
   254 	}
       
   255 
       
   256 EXPORT_C void RMemSpySession::SetThreadPriorityL(TThreadId aId, TInt aPriority)
       
   257 	{
       
   258 	TPckgBuf<TThreadId> arg1( aId );
       
   259 	TPckgBuf<TInt> arg2( aPriority );
       
   260 	
       
   261 	User::LeaveIfError(SendReceive( ESetThreadPriority, TIpcArgs(&arg1, &arg2)));
       
   262 	}
       
   263 
       
   264 EXPORT_C TInt RMemSpySession::ThreadSystemPermanentOrCritical( TThreadId aId, TBool aValue )
       
   265 	{
       
   266 	TPckgBuf<TThreadId> arg1( aId );
       
   267 	TPckgBuf<TBool> arg2( aValue );
       
   268 	TIpcArgs args( &arg1, &arg2 );
       
   269 	
       
   270 	TInt error = SendReceive( EThreadSystemPermanentOrCritical, args );
       
   271 	
       
   272 	return error;
       
   273 	}
       
   274 
       
   275 EXPORT_C TInt RMemSpySession::EndThread( TThreadId aId, TEndType aType )
       
   276 	{
       
   277 	TPckgBuf<TThreadId> arg1( aId );
       
   278 	TPckgBuf<TEndType> arg2( aType );
       
   279 	TIpcArgs args( &arg1, &arg2 );
       
   280 	
       
   281 	TInt error = SendReceive( EEndThread, args );
       
   282 	
       
   283 	return error;
       
   284 	}
       
   285 
       
   286 EXPORT_C TInt RMemSpySession::SwitchToThread( TThreadId aId, TBool aBrought )
       
   287 	{
       
   288 	
       
   289 	}
       
   290 /*
       
   291 EXPORT_C TInt RMemSpySession::TerminateThread( TThreadId aId )
       
   292 	{
       
   293 	TPckgBuf<TThreadId> arg( aId );
       
   294 	TIpcArgs args( &arg );
       
   295 	
       
   296 	TInt error = SendReceive( ETerminateThread, args );
       
   297 	
       
   298 	return error;
       
   299 	}
       
   300 */
       
   301 
       
   302 //inline void RMemSpySession::SortProcessesBy( TSortType aType )
       
   303 //	{
       
   304 //	TPckgBuf<TSortType> In( aType );
       
   305 //	TIpcArgs args( &In );
       
   306 //	SendReceive( ESortProcessesBy, args );
       
   307 //	}
       
   308 
       
   309 //inline void RMemSpySession::OpenCloseCurrentProcess( TProcessId aId, TBool  aOpen )
       
   310 //	{
       
   311 //	TPckgBuf<TProcessId> In1( aId );
       
   312 //	TPckgBuf<TBool> In2( aOpen );
       
   313 //	TIpcArgs args( &In1, &In2 );
       
   314 //	SendReceive( EOpenCloseCurrentProcess, args );
       
   315 //	}
       
   316 
       
   317 //Kernel Objects specific operations
       
   318 EXPORT_C TInt RMemSpySession::GetKernelObjects( RArray<CMemSpyApiKernelObject*> &aKernelObjects )
       
   319 	{		
       
   320 	TPckgBuf<TInt> count;
       
   321 	TInt error = SendReceive( EGetKernelObjectTypeCount, TIpcArgs(&count) );
       
   322 	
       
   323 	if( error == KErrNone )
       
   324 		{			
       
   325 		TInt requestedCount = count();
       
   326 		HBufC8* buffer = HBufC8::NewLC(requestedCount * sizeof(TMemSpyKernelObjectData));
       
   327 		TPtr8 bufferPtr(buffer->Des());
       
   328 			
       
   329 		TPckgBuf<TInt> count(requestedCount);
       
   330 		TIpcArgs args( &count, &bufferPtr );
       
   331 		TInt error = SendReceive( EGetKernelObjects, args );
       
   332 			
       
   333 		for(TInt i=0, offset = 0; i<requestedCount; i++, offset+=sizeof(TMemSpyKernelObjectData))
       
   334 			{
       
   335 			TPckgBuf<TMemSpyKernelObjectData> data;
       
   336 			data.Copy(bufferPtr.Ptr()+offset, sizeof(TMemSpyKernelObjectData));
       
   337 			aKernelObjects.AppendL(CMemSpyApiKernelObject::NewL(data()));
       
   338 			}
       
   339 				
       
   340 		CleanupStack::PopAndDestroy(buffer);						
       
   341 		}	
       
   342 	return KErrNone;		
       
   343 	}
       
   344 
       
   345 EXPORT_C TInt RMemSpySession::GetKernelObjectItems( RArray<CMemSpyApiKernelObjectItem*> &aKernelObjectItems, TMemSpyDriverContainerType aForContainer )
       
   346 	{
       
   347 	TPckgBuf<TInt> count;
       
   348 	TPckgBuf<TMemSpyDriverContainerType> type(aForContainer);
       
   349 	TInt error = SendReceive( EGetKernelObjectItemsCount, TIpcArgs(&count, &type) );
       
   350 		
       
   351 	if (error == KErrNone)
       
   352 		{
       
   353 		TInt requestedCount = count();
       
   354 		HBufC8* buffer = HBufC8::NewLC(requestedCount * sizeof(TMemSpyDriverHandleInfoGeneric));
       
   355 		TPtr8 bufferPtr(buffer->Des());
       
   356 		
       
   357 		TPckgBuf<TInt> count(requestedCount);
       
   358 		TIpcArgs args( &count, &type, &bufferPtr );
       
   359 		TInt error = SendReceive( EGetKernelObjectItems, args );
       
   360 		
       
   361 		for(TInt i=0, offset = 0; i<requestedCount; i++, offset+=sizeof(TMemSpyDriverHandleInfoGeneric))
       
   362 			{
       
   363 			TPckgBuf<TMemSpyDriverHandleInfoGeneric> data;
       
   364 			data.Copy(bufferPtr.Ptr()+offset, sizeof(TMemSpyDriverHandleInfoGeneric));
       
   365 			aKernelObjectItems.AppendL( CMemSpyApiKernelObjectItem::NewL( data() ) );
       
   366 			}
       
   367 						
       
   368 		CleanupStack::PopAndDestroy(buffer);				
       
   369 		}
       
   370 	return KErrNone;
       
   371 	}
       
   372 
       
   373 //Heap specific operations
       
   374 EXPORT_C CMemSpyApiHeap* RMemSpySession::GetHeap()
       
   375 	{
       
   376 	CMemSpyApiHeap* aHeap;
       
   377 	TInt error = KErrNone;
       
   378 	
       
   379 	HBufC8* buffer = HBufC8::NewLC( sizeof(TMemSpyHeapData) );
       
   380 	TPtr8 bufferPtr(buffer->Des());
       
   381 	TIpcArgs args( &bufferPtr );
       
   382 	
       
   383 	error = SendReceive( EGetHeap, args );
       
   384 	
       
   385 	if( error == KErrNone )
       
   386 		{
       
   387 		TPckgBuf<TMemSpyHeapData> data;
       
   388 		data.Copy(bufferPtr.Ptr(), sizeof(TMemSpyHeapData));		
       
   389 		aHeap = CMemSpyApiHeap::NewL( data() );
       
   390 		}
       
   391 	CleanupStack::PopAndDestroy(buffer);
       
   392 		
       
   393 	return aHeap;
       
   394 	}