Ejemplo n.º 1
0
// Window procedure for the per thread dispatch window.
static LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	if (nMsg==WM_POSTABLE_DISPATCH)
	{
		POSTABLETHREADINFO* pti=GetThreadInfo(GetCurrentThreadId(), false);
		ASSERT(pti!=NULL);
		pti->Dispatch();
	}
	else if (nMsg==WM_POSTABLE_SEND)
	{
		POSTABLEMESSAGE* p=(POSTABLEMESSAGE*)lParam;
		ASSERT(p->pTarget->GetThreadId()==GetCurrentThreadId());
		return p->pTarget->OnMessage(p->nMessage, p->wParam, p->lParam);
	}
	else if (nMsg==WM_POSTABLE_CLOSE)
	{
		CEnterCriticalSection ecs2(&g_Section);
		POSTABLETHREADINFO* pti=GetThreadInfo(GetCurrentThreadId(), false);
		if (pti->m_dwCount==0)
		{
			g_ThreadInfos.Remove(GetCurrentThreadId());
		}
	}

	return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
Ejemplo n.º 2
0
HWND CPostableObject::GetDispatchWindow()
{
	ASSERT(m_dwThreadId!=0);
	POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, false);
	ASSERT(pti!=NULL);
	return pti->m_hWndDispatcher;
}
Ejemplo n.º 3
0
LRESULT CPostableObject::Send(UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_dwThreadId!=0);
	if (GetCurrentThreadId()==m_dwThreadId)
	{
		// Same thread, just call it
		return OnMessage(nMessage, wParam, lParam);
	}
	else
	{
		// Different thread, need to switch to other thread to process the message

		// Get thread info
		POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, false);
		ASSERT(pti!=NULL);

		// Pack the message in a structure
		POSTABLEMESSAGE p;
		p.pTarget=this;
		p.nMessage=nMessage;
		p.wParam=wParam;
		p.lParam=lParam;
		p.hReplyEvent=NULL;

		// Send to the dispatcher window for handling
		return SendMessage(pti->m_hWndDispatcher, WM_POSTABLE_SEND, 0, (LPARAM)&p);
	}
}
Ejemplo n.º 4
0
void CPostableObject::DetachThread()
{
	if (!m_dwThreadId)
		return;

	// Get current thread info
	POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, false);
	ASSERT(pti!=NULL);

	{
		// Remove any messages for this object from the queue
		CEnterCriticalSection ecs(&pti->m_Section);
		if (m_iQueuedMessages)
		{
			for (int i=pti->m_MessageQueue.GetSize()-1; i>=0; i--)
			{
				// Message for this object?
				if (pti->m_MessageQueue[i]->pTarget==this)
				{
					// Mark as an obsolete message
					pti->m_MessageQueue[i]->pTarget=NULL;
				}
			}
		}

		pti->m_dwCount--;
		if (pti->m_dwCount==0)
		{
			PostMessage(pti->m_hWndDispatcher, WM_POSTABLE_CLOSE, 0, 0);
		}
	}

	m_iQueuedMessages=0;
	m_dwThreadId=0;
}
Ejemplo n.º 5
0
void
__end_ctrace__ (CTraceStruct *c, const char *name)
{
  if (file_to_write == 0)
    return;
  ThreadInfo *tinfo = GetThreadInfo ();
  tinfo->stack_end_--;
  if (tinfo->stack_end_ < ThreadInfo::max_stack)
    {
      if (c->start_time_ != invalid_time)
        {
          if (tinfo->stack_end_ == 0)
            {
              tinfo->UpdateCurrentTime ();
              c->min_end_time_ = tinfo->current_time_ + ticks;
            }
          // we should record this
          RecordThis (c, tinfo);
          if (tinfo->stack_end_ != 0)
            {
              // propagate the back's mini end time
              tinfo->stack_[tinfo->stack_end_ - 1]->min_end_time_
                  = c->min_end_time_ + ticks;
              tinfo->stack_[tinfo->stack_end_ - 1]->min_end_time_thread_
                  = c->min_end_time_thread_ + ticks;
              tinfo->current_time_ += ticks;
              tinfo->current_time_thread_ += ticks;
            }
        }
    }
}
Ejemplo n.º 6
0
LRESULT CPostableObject::PostAndWait(UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_dwThreadId!=0);

	// Get thread info
	POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, false);
	ASSERT(pti!=NULL);

	// Setup for response...
	CEvent hReplyEvent(true);
	LRESULT lResult=0;

	// Setup a new message
	POSTABLEMESSAGE* p=new POSTABLEMESSAGE;
	p->pTarget=this;
	p->nMessage=nMessage;
	p->wParam=wParam;
	p->lParam=lParam;
	p->hReplyEvent=hReplyEvent;
	p->pResult=&lResult;

	{
		CEnterCriticalSection ecs(&pti->m_Section);

		m_iQueuedMessages++;
		pti->m_MessageQueue.Add(p);

		// If no pending Windows message, post one...
		if (!pti->m_bPostPending)
		{
			// Unless we're currently dispatching messages... in which case we'll post at the end of that.
			if (!pti->m_bDispatching)
			{
				PostMessage(pti->m_hWndDispatcher, WM_POSTABLE_DISPATCH, 0, 0);
			}

			// Set flag saying post is pending
			pti->m_bPostPending=true;
		}
	}

	while (true)
	{
		// Dispatch messages...
		MSG msg;
		while (PeekMessage(&msg, 0, 0, NULL, PM_REMOVE))
		{
			if (!IsInputMessage(msg.message))
			{
				DispatchMessage(&msg);
			}
		}

		// Wait for response...
		if (MsgWaitForMultipleObjects(1, &hReplyEvent.m_hObject, FALSE, INFINITE, QS_ALLEVENTS)==WAIT_OBJECT_0)
			return lResult;
	}
}
Ejemplo n.º 7
0
BOOL
CmdStep(
    LPSTR             CmdBuf,
    HANDLE            hProcess,
    HANDLE            hThread,
    PEXCEPTION_RECORD ExceptionRecord
    )
{
    PPROCESS_INFO ThisProcess = GetProcessInfo( hProcess );
    if (!ThisProcess) {
        printf( "could not get process information\n" );
        return FALSE;
    }

    PTHREAD_INFO ThisThread = GetThreadInfo( hProcess, hThread );
    if (!ThisThread) {
        printf( "could not get thread information\n" );
        return FALSE;
    }

    SuspendAllThreads( ThisProcess, ThisThread );

    ULONG Address = GetNextOffset(
        ThisProcess->hProcess,
        (ULONG)ExceptionRecord->ExceptionAddress,
        TRUE
        );

    if (Address == (ULONG)-1) {
#ifdef _M_IX86
        CurrContext.EFlags |= 0x100;
        SetRegContext( ThisThread->hThread, &CurrContext );
        PBREAKPOINT_INFO bp = GetAvailBreakpoint( ThisProcess );
        if (bp) {
            bp->Flags   |= BPF_TRACE;
            bp->Address  = (ULONG)ExceptionRecord->ExceptionAddress;
            bp->Handler  = TraceBpHandler;
        }
#else
        printf( "could not trace the instruction\n" );
#endif
    } else {
        PBREAKPOINT_INFO bp = SetBreakpoint(
            ThisProcess,
            Address,
            0,
            NULL,
            TraceBpHandler
            );
        if (!bp) {
            printf( "could not trace the instruction\n" );
            return FALSE;
        }
    }

    return TRUE;
}
Ejemplo n.º 8
0
static void fillThreadInfo( HWND hwnd, ProcStats *info ) {

    HWND        lb;
    int         index;
    char        buf[100];
    DWORD       threadid;
    ThreadStats thdinfo;
#ifndef CHICAGO
    char        *str1;
#endif

    lb = GetDlgItem( hwnd, THREAD_LIST );
    index = (int)SendMessage( lb, LB_GETCURSEL, 0, 0L );
    if( index == LB_ERR ) {
        SetDlgItemText( hwnd, THREAD_TID, "" );
        SetDlgItemText( hwnd, THREAD_SUSPEND_CNT, "" );
        SetDlgItemText( hwnd, THREAD_PRIORITY, "" );
    } else {
        enableChoices( hwnd, TRUE );
        SendMessage( lb, LB_GETTEXT, index, (LPARAM)(LPSTR)buf );
        threadid = getThreadId( buf );
        sprintf( buf, "tid = %08lX", threadid );
        SetDlgItemText( hwnd, THREAD_TID, buf );

        if( GetThreadInfo( info->pid, threadid, &thdinfo ) ) {
#ifndef CHICAGO
            if( thdinfo.state == 5 ) {  /* the thread is in a wait state */
                str1 = SrchMsg( thdinfo.wait_reason, ThreadWaitMsgs, NULL );
                if( str1 == NULL ) {
                    str1 = AllocRCString( STR_WAIT_4_UNKNOWN );
                    RCsprintf( buf, STR_STATE, str1 );
                    FreeRCString( str1 );
                } else {
                    RCsprintf( buf, STR_STATE, str1 );
                }
            } else {
                str1 = SrchMsg( thdinfo.state, ThreadStateMsgs, NULL );
                if( str1 == NULL ) {
                    str1 = AllocRCString( STR_BRACED_UNKNOWN );
                    RCsprintf( buf, STR_STATE, str1 );
                    FreeRCString( str1 );
                } else {
                    RCsprintf( buf, STR_STATE, str1 );
                }
            }
            SetDlgItemText( hwnd, THREAD_SUSPEND_CNT, buf );
#endif

            /* get priority */
            RCsprintf( buf, STR_PRIORITY_X, thdinfo.cur_pri,
                        thdinfo.base_pri );
            SetDlgItemText( hwnd, THREAD_PRIORITY, buf );
        }
    }
}
Ejemplo n.º 9
0
void CPostableObject::AttachThread()
{
	// Save the current thread ID
	m_dwThreadId=GetCurrentThreadId();
	m_iQueuedMessages=0;

	CEnterCriticalSection ecs1(&g_Section);

	// Get info for this thread, creating it if need be
	POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, true);
	ASSERT(pti!=NULL);

	// Bump the count...
	CEnterCriticalSection ecs(&pti->m_Section);
	pti->m_dwCount++;
}
Ejemplo n.º 10
0
void
__start_ctrace__ (void *c, const char *name)
{
  if (file_to_write == 0)
    return;
  CTraceStruct *cs = new (c) CTraceStruct (name);
  ThreadInfo *tinfo = GetThreadInfo ();
  if (tinfo->stack_end_ == 0)
    {
      // always update the time in the first entry.
      // Or if it sleep too long, will make this entry looks
      // very time consuming.
      tinfo->UpdateCurrentTime ();
    }
  if (tinfo->stack_end_ < ThreadInfo::max_stack)
    {
      tinfo->stack_[tinfo->stack_end_] = cs;
    }
  tinfo->stack_end_++;
}
Ejemplo n.º 11
0
// Post a message to this object
void CPostableObject::Post(UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_dwThreadId!=0);

	// Get thread info
	POSTABLETHREADINFO* pti=GetThreadInfo(m_dwThreadId, false);
	ASSERT(pti!=NULL);

	// Setup a new message
	POSTABLEMESSAGE* p=new POSTABLEMESSAGE;
	p->pTarget=this;
	p->nMessage=nMessage;
	p->wParam=wParam;
	p->lParam=lParam;
	p->hReplyEvent=NULL;
	p->pResult=NULL;

	{
		CEnterCriticalSection ecs(&pti->m_Section);

		pti->m_MessageQueue.Add(p);
		m_iQueuedMessages++;

		// If no pending Windows message, post one...
		if (!pti->m_bPostPending)
		{
			// Unless we're currently dispatching messages... in which case we'll post at the end of that.
			if (!pti->m_bDispatching)
			{
				PostMessage(pti->m_hWndDispatcher, WM_POSTABLE_DISPATCH, 0, 0);
			}

			// Set flag saying post is pending
			pti->m_bPostPending=true;
		}
	}
}
Ejemplo n.º 12
0
TInt DKdaChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
	{
	__KTRACE_OPT(KDEBUGGER, Kern::Printf(">DKdaChannel::Request function=%d a1=0x%08X a2=0x%08X", aFunction, a1, a2));
	
	TInt r = KErrNone;
	switch (aFunction)
		{
	case RMinKda::ETrap:
		iCrashHandler->Trap((TRequestStatus*)a1, a2);
		break;
	case RMinKda::ECancelTrap:
		iCrashHandler->CancelTrap();
		break;
	case RMinKda::EKillCrashedThread:
		iCrashHandler->KillCrashedThread();
		break;
	case RMinKda::EGetThreadInfo:
		r = GetThreadInfo((TUint)a1, a2);
		break;
	case RMinKda::EReadMem:
		r = ReadMem((RMinKda::TReadMemParams*)a1);
		break;
	case RMinKda::EGetCodeSegs:
		r = GetCodeSegs((RMinKda::TCodeSnapshotParams*)a1);
		break;
	case RMinKda::EGetCodeSegInfo:
		r = GetCodeSegInfo((RMinKda::TCodeInfoParams*)a1);
		break;
	default:
		Kern::PanicCurrentThread(KClientPanic, EPanicUnsupportedRequest);
		break;
		}

	__KTRACE_OPT(KDEBUGGER, Kern::Printf("<DKdaChannel::Request r=%d", r));
	return r;
	}
Ejemplo n.º 13
0
HRESULT Kernel::ProcessThreadWaitList()
{
	LIST_ENTRY ThreadListHead;
	ULONG32 ListHeadAddress = (ULONG32)_KProcess + _pOffsets[OffSetId_KProcess_ThreadListHead];
	HRESULT hr = g_ExtData2->ReadVirtual(SIGN_EXTEND(ListHeadAddress), &ThreadListHead, sizeof(ThreadListHead), NULL);

	ULONG32 NextEntryAddress = (ULONG32)ThreadListHead.Flink;
	LIST_ENTRY NextEntry;

	while(NextEntryAddress != ListHeadAddress)
	{
		printf("\n");
		ULONG32 EThreadAddress = NextEntryAddress - _pOffsets[OffsetId_EThread_ThreadListEntry];

		hr = GetDispatcherInfo(EThreadAddress);
		hr = GetThreadInfo(EThreadAddress);
		hr = ProcessWaitList(EThreadAddress);
		hr = ProcessMutantList(EThreadAddress);
		hr = g_ExtData2->ReadVirtual(SIGN_EXTEND(NextEntryAddress), &NextEntry, sizeof(NextEntry), NULL);

		NextEntryAddress = (ULONG32)NextEntry.Flink;
	}
	return hr;
}
Ejemplo n.º 14
0
BOOL
CmdBreakPoint(
    LPSTR             CmdBuf,
    HANDLE            hProcess,
    HANDLE            hThread,
    PEXCEPTION_RECORD ExceptionRecord
    )
{
    CHAR BpCmd = tolower(CmdBuf[1]);
    ULONG Address = 0;
    PBREAKPOINT_INFO bp;
    PPROCESS_INFO ThisProcess;
    ULONG Flags;
    LPSTR p;
    LPSTR SymName;
    ULONG i;
    IMAGEHLP_MODULE mi;


    ThisProcess = GetProcessInfo( hProcess );
    if (!ThisProcess) {
        printf( "could not get process information\n" );
        return FALSE;
    }

    PTHREAD_INFO ThisThread = GetThreadInfo( hProcess, hThread );
    if (!ThisThread) {
        printf( "could not get thread information\n" );
        return FALSE;
    }

    SKIP_NONWHITE( CmdBuf );
    SKIP_WHITE( CmdBuf );
    p = CmdBuf;

    switch ( BpCmd ) {
        case 'p':
            Flags = 0;
            CmdBuf = GetAddress( CmdBuf, &Address );
            SymName = (LPSTR) MemAlloc( CmdBuf - p + 16 );
            if (!SymName) {
                printf( "could not allocate memory for bp command\n" );
                break;
            }
            strncpy( SymName, p, CmdBuf - p );
            if (!Address) {
                Flags = BPF_UNINSTANCIATED;
                printf( "breakpoint not instanciated\n" );
            }
            bp = SetBreakpoint(
                ThisProcess,
                Address,
                Flags,
                SymName,
                UserBpHandler
                );
            MemFree( SymName );
            if (!bp) {
                printf( "could not set breakpoint\n" );
            }
            ThisProcess->UserBpCount += 1;
            bp->Number = ThisProcess->UserBpCount;
            SKIP_WHITE( CmdBuf );
            if (CmdBuf[0]) {
                if (CmdBuf[0] == '/') {
                    switch (tolower(CmdBuf[1])) {
                        case 'c':
                            CmdBuf += 3;
                            if (CmdBuf[0] != '\"') {
                                printf( "invalid syntax\n" );
                                return FALSE;
                            }
                            CmdBuf += 1;
                            p = strchr( CmdBuf, '\"' );
                            if (!p) {
                                printf( "invalid syntax\n" );
                                return FALSE;
                            }
                            p[0] = 0;
                            bp->Command = _strdup( CmdBuf );
                            break;

                        default:
                            break;
                    }
                }
            }
            break;

        case 'l':
            for (i=0; i<MAX_BREAKPOINTS; i++) {
                if (ThisProcess->Breakpoints[i].Number) {
                    ULONG disp = 0;
                    if (ThisProcess->Breakpoints[i].Flags & BPF_WATCH) {
                        printf( "#%d %c%c\t          \tWatch\n",
                            ThisProcess->Breakpoints[i].Number,
                            ThisProcess->Breakpoints[i].Flags & BPF_UNINSTANCIATED ? 'U' : 'I',
                            ThisProcess->Breakpoints[i].Flags & BPF_DISABLED       ? 'D' : 'E'
                            );
                    } else if ((ThisProcess->Breakpoints[i].Address != 0) &&
                        (ThisProcess->Breakpoints[i].Address != 0xffffffff)) {
                        SymGetModuleInfo(
                            ThisProcess->hProcess,
                            ThisProcess->Breakpoints[i].Address,
                            &mi
                            );
                        if (SymGetSymFromAddr(
                            ThisProcess->hProcess,
                            ThisProcess->Breakpoints[i].Address,
                            &disp,
                            sym
                            )) {
                                printf( "#%d %c%c\t0x%08x\t%s!%s\n",
                                    ThisProcess->Breakpoints[i].Number,
                                    ThisProcess->Breakpoints[i].Flags & BPF_UNINSTANCIATED ? 'U' : 'I',
                                    ThisProcess->Breakpoints[i].Flags & BPF_DISABLED       ? 'D' : 'E',
                                    ThisProcess->Breakpoints[i].Address,
                                    mi.ModuleName,
                                    sym ? sym->Name : ""
                                    );
                        }
                    } else {
                        printf( "#%d %c%c\t          \t%s\n",
                            ThisProcess->Breakpoints[i].Number,
                            ThisProcess->Breakpoints[i].Flags & BPF_UNINSTANCIATED ? 'U' : 'I',
                            ThisProcess->Breakpoints[i].Flags & BPF_DISABLED       ? 'D' : 'E',
                            ThisProcess->Breakpoints[i].SymName
                            );
                    }
                }
            }
            break;

        case 'c':
            if (!CmdBuf[0]) {
                printf( "missing breakpoint number\n" );
                return FALSE;
            }
            if (CmdBuf[0] == '*') {
                for (i=0; i<MAX_BREAKPOINTS; i++) {
                    if (ThisProcess->Breakpoints[i].Number) {
                        ClearBreakpoint( ThisProcess, &ThisProcess->Breakpoints[i] );
                    }
                }
                return TRUE;
            }
            if (isdigit(CmdBuf[0])) {
                ULONG BpNum = atoi( CmdBuf );
                for (i=0; i<MAX_BREAKPOINTS; i++) {
                    if (ThisProcess->Breakpoints[i].Number == BpNum) {
                        ClearBreakpoint( ThisProcess, &ThisProcess->Breakpoints[i] );
                        return TRUE;
                    }
                }
            }
            printf( "invalid breakpoint number\n" );
            return FALSE;

        case 'd':
            break;

        case 'e':
            break;

        case 'a':
#if defined(_M_IX86)
            CmdBuf = GetAddress( CmdBuf, &Address );

            bp = GetAvailBreakpoint( ThisProcess );
            if (!bp) {
                printf( "could not set breakpoint\n" );
                return FALSE;
            }

            bp->Address = Address;
            bp->Handler = UserBpHandler;
            bp->Flags   = BPF_WATCH;

            ThisProcess->UserBpCount += 1;
            bp->Number = ThisProcess->UserBpCount;

            CurrContext.Dr0 = Address;
            CurrContext.Dr6 = 0x000d0002;
            SetRegContext( ThisThread->hThread, &CurrContext );
#else
            printf( "only available on x86\n" );
#endif
            break;

        default:
            break;
    }

    return TRUE;
}