dbgsrv/coredumpserver/cdssupport/src/processdata.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Implemetation of class CProcessInfo
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @see CProcessInfo
       
    22 */
       
    23 
       
    24 #include <e32debug.h>
       
    25 #include <debuglogging.h>
       
    26 
       
    27 #include <processdata.h>
       
    28 
       
    29 /**
       
    30 Allocates and constructs a CProcessInfo object.
       
    31 @param aId Kernel process id
       
    32 @param aName Kernel process name
       
    33 @see CProcessInfo::CProcessInfo
       
    34 */
       
    35 EXPORT_C CProcessInfo* CProcessInfo::NewL( 
       
    36 										const TUint64	  aId,
       
    37 										const TDesC		& aName )
       
    38 	{
       
    39 
       
    40 	const TUint size = 
       
    41 		  sizeof( TUint32 )		// iId Low
       
    42 		+ sizeof( TUint32 )		// iId High
       
    43 		+ sizeof( TUint32 )		// When externalized, we send the name length, so must include this
       
    44 		+ 2						// When externalized, the << operator writes 2 bytes for the descriptor size
       
    45 		+ aName.Size()			// iName Size, in bytes.
       
    46 		+ sizeof( TUint32 ) 	// Observed
       
    47 		+ sizeof( TUint32 );	// iSize itself
       
    48 
       
    49 	if( size >= MaxSize() )
       
    50 		{
       
    51 		LOG_MSG3( "CProcessInfo::NewL() : Descriptorized object = 0x%X bytes would exceed the maximum of 0x%X\n", 
       
    52 			size, MaxSize() );
       
    53 
       
    54 		User::Leave( KErrTooBig );
       
    55 		}
       
    56 
       
    57 	CProcessInfo * data = new (ELeave) CProcessInfo( aId );
       
    58 
       
    59 	CleanupStack::PushL( data );
       
    60 	
       
    61 	data->ConstructL( aName );
       
    62 
       
    63 	CleanupStack::Pop(data);
       
    64 
       
    65 	return (data);
       
    66 
       
    67 	}
       
    68 
       
    69 
       
    70 /**
       
    71 Allocates and constructs a CProcessInfo object from a descriptor. 
       
    72 The descriptor contains an externalised version of a CProcessInfo object.
       
    73 This method is typically used to obtain a CProcessInfo object from a 
       
    74 descriptor returned by the core dump server.
       
    75 
       
    76 @param aStreamData Descriptor with externalised/streamed object
       
    77 @see InternalizeL
       
    78 @see ExternalizeL
       
    79 */
       
    80 EXPORT_C CProcessInfo* CProcessInfo::NewL( const TDesC8 & aStreamData )
       
    81 	{
       
    82 
       
    83 	CProcessInfo* self = new (ELeave) CProcessInfo();
       
    84 	
       
    85 	CleanupStack::PushL( self );
       
    86 	
       
    87 	// Open a read stream for the descriptor
       
    88 	RDesReadStream stream( aStreamData );
       
    89 
       
    90 	CleanupClosePushL( stream );
       
    91 
       
    92 	self->InternalizeL( stream );
       
    93 
       
    94 	CleanupStack::PopAndDestroy( &stream ); // finished with the stream
       
    95 
       
    96 	CleanupStack::Pop( self );
       
    97 
       
    98 	return ( self );
       
    99 
       
   100 	}
       
   101 
       
   102 
       
   103 /**
       
   104 Destructor. Deletes name if allocated.
       
   105 */
       
   106 EXPORT_C CProcessInfo::~CProcessInfo()
       
   107 	{
       
   108 
       
   109 	if( NULL != iName )
       
   110 		{
       
   111 		delete iName;
       
   112 		}
       
   113 	}
       
   114 
       
   115 
       
   116 /**
       
   117 First phase contructor. Sets the size to 0, name to NULL.
       
   118 @see CProcessInfo::NewL()
       
   119 */
       
   120 CProcessInfo::CProcessInfo(	const TUint64	  aId ) :
       
   121 	iId			 ( aId ),
       
   122     iObserved    ( EFalse ),
       
   123 	iSize        ( 0 )
       
   124 	{
       
   125 	iName = NULL;
       
   126 	}
       
   127 
       
   128 
       
   129 /**
       
   130 Second phase constructor initialises the name of the process.
       
   131 @param aName Process name
       
   132 @see NameL()
       
   133 */
       
   134 void CProcessInfo::ConstructL(  const TDesC & aName )
       
   135 	{
       
   136 	NameL( aName );
       
   137 	}
       
   138 
       
   139 
       
   140 /**
       
   141 Initialise this object with the contents of RReadStream aStream
       
   142 The descriptor contains an externalised version of an object.
       
   143 This method is typically used to obtain a CProcessInfo object from 
       
   144 the core dump server.
       
   145 Any modifications to this method should be synchronised with ExternalizeL().
       
   146 Also note that the methods used from RReadStream (>> or ReadUint32L) 
       
   147 can behave differently, especially for descriptors.
       
   148 @param aStream Stream with streamed object
       
   149 @see ExternalizeL
       
   150 @see RReadStream
       
   151 @pre Call Externalise to obtain the stream containing an externalised 
       
   152 version of this object.
       
   153 */
       
   154 EXPORT_C void CProcessInfo::InternalizeL( RReadStream & aStream )
       
   155 	{
       
   156 
       
   157 	TUint32 idLow = aStream.ReadUint32L(); 
       
   158 
       
   159 	TUint32 idHigh = aStream.ReadUint32L(); 
       
   160 	iId = MAKE_TUINT64( idHigh, idLow );
       
   161 
       
   162 	// Read the number of character elements in the name. 
       
   163 	TUint32 nameLength = aStream.ReadUint32L(); 
       
   164 
       
   165 	if( NULL != iName )
       
   166 		{
       
   167 		LOG_MSG( " iName != NULL\n" );
       
   168 		delete iName;
       
   169 		iName = NULL;
       
   170 		}
       
   171 
       
   172 	if ( nameLength > 0 )
       
   173 		{
       
   174 		iName  = HBufC::NewL( aStream, nameLength ); 
       
   175 		}
       
   176 	else
       
   177 		{
       
   178 		iName  = NULL;
       
   179 		}
       
   180 
       
   181 	iObserved = static_cast<TBool>(aStream.ReadUint32L());
       
   182 
       
   183 	iSize = aStream.ReadUint32L() ;
       
   184 	}
       
   185 
       
   186 
       
   187 
       
   188 /**
       
   189 Make a streamed representation of this object to RWriteStream aStream.
       
   190 
       
   191 This method is typically by the core dump server when contructing a list of 
       
   192 CProcessInfo for a client.
       
   193 Any modifications to this method should be synchronised with InternalizeL(). 
       
   194 Also note that the methods used from RWriteStream (>> or WriteUint32L) can behave differently,
       
   195 especially for descriptors.
       
   196 @param aStream Stream to stream object onto
       
   197 @param buf Buffer onto the same stream, used to obtain the correct size of the externalised object
       
   198 @see InternalizeL
       
   199 @see RReadStream
       
   200 @see RWriteStream
       
   201 @post The stream contains an externalised version of this object.
       
   202 */
       
   203 EXPORT_C void CProcessInfo::ExternalizeL( RWriteStream & aStream, CBufFlat* buf )
       
   204 	{
       
   205 
       
   206 	const TUint32 idLow = I64LOW( iId ); 
       
   207 	const TUint32 idHigh = I64HIGH( iId ); 
       
   208 
       
   209 	// Take the size of the buffer before we add anything to it.
       
   210 	TUint startBufSize = buf->Size();
       
   211 
       
   212 	aStream.WriteUint32L( idLow ); 
       
   213 	aStream.WriteUint32L( idHigh ); 
       
   214 
       
   215 	TUint nameLength = 0;
       
   216 
       
   217 	if ( ( NULL != iName ) && ( iName->Des().Length() > 0 ) )
       
   218 		{
       
   219 
       
   220 		nameLength = iName->Des().Length();
       
   221 
       
   222 		if( nameLength > 0 )
       
   223 			{
       
   224 			// Write the number of character elements in the name. 
       
   225 			aStream.WriteUint32L( nameLength ); 
       
   226 			aStream << iName->Des();
       
   227 			}
       
   228 		}
       
   229 
       
   230 	if( nameLength == 0 )
       
   231 		{
       
   232 		aStream.WriteUint32L( 0 ); 
       
   233 		}
       
   234 
       
   235 	aStream.WriteUint32L( static_cast<TUint32>(iObserved) );
       
   236 
       
   237 	iSize = buf->Size() - startBufSize + 4;
       
   238 
       
   239 	aStream.WriteUint32L( iSize );
       
   240 
       
   241 	}
       
   242 
       
   243 
       
   244 /**
       
   245 Obtain the kernel process id.
       
   246 */
       
   247 EXPORT_C const TUint64 & CProcessInfo::Id() const 
       
   248 	 { 
       
   249 	 return ( iId ); 
       
   250 	 }
       
   251 
       
   252 
       
   253 /**
       
   254 Set the name of the process by deleting, allocating and then copying the parameter.
       
   255 @param aName Name of the process to set to
       
   256 @see ConstructL()
       
   257 */
       
   258 EXPORT_C void CProcessInfo::NameL( const TDesC & aName )
       
   259 	{
       
   260 
       
   261 	if( aName.Length() > 0 )
       
   262 		{
       
   263 		TUint toCopy = aName.Length();
       
   264 		iName = HBufC::NewL( toCopy );
       
   265 		TPtr nameDes = iName->Des();
       
   266 
       
   267 		nameDes.Copy( aName.Ptr(), toCopy );
       
   268 		nameDes.SetLength( toCopy );
       
   269 		}
       
   270 	else
       
   271 		{
       
   272 		iName = NULL;
       
   273 		}
       
   274 	}
       
   275 
       
   276 
       
   277 /**
       
   278 Obtain the kernel process name.
       
   279 */
       
   280 EXPORT_C const TDesC & CProcessInfo::Name() const 
       
   281 	{ 
       
   282 	if(!iName)
       
   283 		{
       
   284 		return KNoThreadName;
       
   285 		}
       
   286 	
       
   287 	return ( *iName ); 
       
   288 	}
       
   289 
       
   290 /**
       
   291 Returns ETrue if the process is being observed for crashes by the Core Dump Server.
       
   292 */
       
   293 EXPORT_C TBool CProcessInfo::Observed() const 
       
   294 	 { 
       
   295 	 return ( iObserved ); 
       
   296 	 }
       
   297 
       
   298 /**
       
   299 Set whether this process is being observed for crashes by the Core Dump Server.
       
   300 */
       
   301 EXPORT_C void CProcessInfo::Observed( TBool aFlag ) 
       
   302 	 { 
       
   303 	 iObserved = aFlag; 
       
   304 	 }
       
   305 
       
   306 CProcessInfo::CProcessInfo()
       
   307 	{
       
   308 	}
       
   309 
       
   310 
       
   311  /* 
       
   312  Get the maximum size allowed for this object. This is needed as the object is passed  
       
   313  across the Client Server interface.
       
   314  */
       
   315  EXPORT_C TInt CProcessInfo::MaxSize()
       
   316 	{
       
   317 	
       
   318 	const TInt maxSize = 256;
       
   319 	return maxSize;
       
   320 	}
       
   321 
       
   322 
       
   323 /**
       
   324 Gets the size of the object when externalized. The sizeofs used to calculate this 
       
   325 must match the operators used in ExternalizeL and InternalizeL.
       
   326 Special attention must be paid to the name. If the object has not been 
       
   327 externalized yet then this method returns the maximum that it could take.
       
   328 The name descriptor is compressed when externalized, so it is not its Size().
       
   329 Furthermore the << operator adds two bytes to the stream when externalizing 
       
   330 a descriptor.
       
   331 */
       
   332  EXPORT_C TInt CProcessInfo::Size() const
       
   333 	{
       
   334 
       
   335 	if( iSize != 0 )
       
   336 		{
       
   337 		return iSize;
       
   338 		}
       
   339 
       
   340 	TUint extNameSize = 0;
       
   341 	if( iName )
       
   342 		{
       
   343 		extNameSize = 2					// When externalized, the << operator writes 2 bytes for the descriptor size
       
   344 					+ iName->Size();	// iName itself, in bytes.
       
   345 		}
       
   346 
       
   347 	const TUint size = 
       
   348 		  sizeof( TUint32 )		// iId Low
       
   349 		+ sizeof( TUint32 )		// iId High
       
   350 		+ sizeof( TUint32 )		// When externalized, we send the name length, so must include this
       
   351 		+ extNameSize
       
   352 		+ sizeof( TUint32 ) 	// iObserved
       
   353 		+ sizeof( TUint32 );	// iSize When externalized, we send the real externalized size of the buffer
       
   354 
       
   355 	return size;
       
   356 	}
       
   357 
       
   358