Example #1
0
int ThreadGetPriority(void* Handle) 
{ 
	RThread Thread;
	Thread.SetHandle((TInt)Handle);	
    TThreadPriority v = Thread.Priority();
    if (v<=EPriorityMuchLess)
        return MULTITHREAD_PRIORITY_IDLE;
    if (v<=EPriorityLess)
        return MULTITHREAD_PRIORITY_LOW;
    if (v<=EPriorityNormal)
        return MULTITHREAD_PRIORITY_NORMAL;
    if (v<=EPriorityMore)
        return MULTITHREAD_PRIORITY_IO;
    if (v<=EPriorityMuchMore)
        return MULTITHREAD_PRIORITY_USERINPUT;
    return MULTITHREAD_PRIORITY_HIGH;
}    
/** Obtain the thread list. If aProcessId is negative, we obtain the entire system
thread list. If aProcessId is positive we get the thread list for that process */
void CServerCrashDataSource::GetThreadListL( const TUint64 aProcessId, 
											 RThreadPointerList & aThreadList,
											 TUint & aTotalThreadListDescSize )
	{
	LOG_MSG2( "->CServerCrashDataSource::GetThreadListL(aProcessId=%Lu)\n", aProcessId); 

	// Delete any objects in the array, since we will replace them. 
	aThreadList.ResetAndDestroy();
		
	aTotalThreadListDescSize = 0;

	if( iThreadListBuffer.Size() != iLastThreadListSize )
		{
		LOG_MSG2("CServerCrashDataSource::GetThreadListL -> iThreadListBuffer.ReAlloc(%d)\n", iLastThreadListSize );
		iThreadListBuffer.ReAllocL( iLastThreadListSize );
		}

	LOG_MSG( "CServerCrashDataSource::GetThreadListL -> DoGetListL()\n" );
	DoGetListL( EThreads, (TUint)-1, aProcessId, iThreadListBuffer, iLastThreadListSize );

	iLastThreadListSize = iThreadListBuffer.Size();
	

	CThreadInfo		 *  threadInfoPtr;
	RThread				thread;
	TThreadStackInfo	stackInfo;
	TThreadListEntry *	entry;
	TUint				usrStackSize;
	TLinAddr			usrStackAddr;
	TLinAddr			svcStackPtr;
	TLinAddr			svcStackBase;
	TUint				svcStackSize;

	TUint				priority;

	TUint8* ptr = (TUint8*)( iThreadListBuffer.Ptr() );
	const TUint8* ptrEnd = ptr + iLastThreadListSize;
	//LOG_MSG3( "  Data start,end=(0x%x, 0x%x)\n", ptr, ptrEnd );

	while( ptr < ptrEnd ) 
		{

		entry = (TThreadListEntry*)ptr;

		if( !entry )
			{
			LOG_MSG( "  ERROR !* : TThreadListEntry is NULL\n" );
			User::Leave(KErrBadHandle);
			}

		if( entry->iNameLength == 0 )
			{
			LOG_MSG4( "  Skipping Thread 0x%X%X : entry->iNameLength=%d", 
				I64HIGH(entry->iThreadId), I64LOW(entry->iThreadId), entry->iNameLength );
			ptr += Align4( entry->GetSize() );
			continue;
			}

		//LOG_MSG3( "  entry &=0x%X, size=%d\n", &(entry->iThreadId), entry->GetSize() );
		//LOG_MSG3( "  entry->iThreadId= 0x%X%X\n", I64HIGH(entry->iThreadId), I64LOW(entry->iThreadId) );
		//LOG_MSG3( "  found tid=%d, pid=%d\n", I64LOW(entry->iThreadId), I64LOW(entry->iProcessId) );
			
		if( entry->iSupervisorStackPtrValid )
			{
			svcStackPtr = entry->iSupervisorStackPtr;
			}
		else
			{
			svcStackPtr = 0;
			}

		if( entry->iSupervisorStackBaseValid )
			{
			svcStackBase = entry->iSupervisorStackBase;
			}
		else
			{
			svcStackBase = 0;
			}
		
		if( entry->iSupervisorStackSizeValid )
			{
			svcStackSize = entry->iSupervisorStackSize;
			}
		else
			{
			svcStackSize = 0;
			}		



		if( KErrNone == thread.Open( entry->iThreadId ) )
			{
			priority = (TUint)(thread.Priority());

			if( KErrNone == thread.StackInfo( stackInfo ) )
				{
				usrStackAddr = stackInfo.iLimit;
				usrStackSize = stackInfo.iBase - stackInfo.iLimit;
				}
			else
				{
				usrStackSize = 0;
				usrStackAddr = 0;
				}

			thread.Close();
			}
		else
			{
			usrStackSize = 0;
			usrStackAddr = 0;
			priority = 0;
			}

		/*
		LOG_MSG3( "  entry->iNameLength=%d, &(entry->iName[0])=0x%X\n", 
				entry->iNameLength, &(entry->iName[0]) );
		*/

		TPtrC entryName( &(entry->iName[0]), entry->iNameLength );
		//LOG_MSG2( "  -> threadInfoPtr = CThreadInfo::NewL( name.Size()=%d)\n", entryName.Size() );

		threadInfoPtr = CThreadInfo::NewL( 
										entry->iThreadId, 
										entryName,
										entry->iProcessId,
										(TUint)priority,
										svcStackPtr,
										svcStackBase,
										svcStackSize,
										usrStackAddr,
										usrStackSize );

		/*
		LOG_MSG3( "  threadInfoPtr->iSvcStackAddr=0x%X, threadInfoPtr->iSvcStackSize=0x%X\n", 
			threadInfoPtr->SvcStackAddr(), threadInfoPtr->SvcStackSize() );
		*/

		TInt err = aThreadList.Append( threadInfoPtr );
		if( err != KErrNone )
			{
			// We use this id so as not to use Push(), AppendL(), Pop()
			delete threadInfoPtr;
			User::Leave( err );
			}

		aTotalThreadListDescSize += threadInfoPtr->Size();

		/*
		LOG_MSG3( "  aTotalThreadListDescSize = %d after adding %d\n", 
				aTotalThreadListDescSize, threadInfoPtr->Size() );

		RBuf rPrintBuf;
		rPrintBuf.Create( threadInfoPtr->Name()); 
		RDebug::Printf( "  <- rPrintBuf.Create(), rPrintBuf.Length()=%d\n", rPrintBuf.Length() );
		char* cl = (char*) rPrintBuf.Collapse().PtrZ();
		RDebug::Printf("  name=%s\n", cl );
		rPrintBuf.Close();
		*/

		ptr += Align4( entry->GetSize() );
		//LOG_MSG2( "  ptr += Align4(entry->GetSize()) = 0x%X\n", ptr );

		} // while

	}