Condividi tramite


Registrare il programma

Dopo che il motore di debug ha acquisito una porta, rappresentata da un'interfaccia IDebugPort2 , il passaggio successivo per consentire il debug del programma consiste nel registrarlo con la porta. Dopo la registrazione, il programma è disponibile per il debug con uno dei mezzi seguenti:

  • Processo di collegamento, che consente al debugger di ottenere il controllo completo del debug di un'applicazione in esecuzione.

  • Debugging JIT (Just-In-Time), che consente il debugging retrospettivo di un programma che viene eseguito indipendentemente da un debugger. Quando l'architettura di runtime rileva un errore, il debugger riceve una notifica prima che il sistema operativo o l'ambiente di runtime rilasci la memoria e le risorse del programma di errore.

Procedura di registrazione

Per registrare il programma

  1. Chiamare il metodo AddProgramNode implementato dalla porta.

    IDebugPortNotify2::AddProgramNode richiede un puntatore a un'interfaccia IDebugProgramNode2 .

    In genere, quando il sistema operativo o l'ambiente di runtime carica un programma, crea il nodo del programma. Se viene richiesto al motore di debug (DE) di caricare il programma, de crea e registra il nodo del programma.

    L'esempio seguente mostra il motore di debug che avvia il programma e lo registra con una porta.

    Annotazioni

    Questo esempio di codice non è l'unico modo per avviare e riprendere un processo; questo codice è principalmente un esempio di registrazione di un programma con una porta.

    // This is an IDebugEngineLaunch2 method.
    HRESULT CDebugEngine::LaunchSuspended(/* omitted parameters */,
                                          IDebugPort2 *pPort,
                                          /* omitted parameters */,
                                          IDebugProcess2**ppDebugProcess)
    {
        // do stuff here to set up for a launch (such as handling the other parameters)
        ...
    
        // Now get the IPortNotify2 interface so we can register a program node
        // in CDebugEngine::ResumeProcess.
        CComPtr<IDebugDefaultPort2> spDefaultPort;
        HRESULT hr = pPort->QueryInterface(&spDefaultPort);
        if (SUCCEEDED(hr))
        {
            CComPtr<IDebugPortNotify2> spPortNotify;
            hr = spDefaultPort->GetPortNotify(&spPortNotify);
            if (SUCCEEDED(hr))
            {
                // Remember the port notify so we can use it in ResumeProcess.
                m_spPortNotify = spPortNotify;
    
                // Now launch the process in a suspended state and return the
                // IDebugProcess2 interface
                CComPtr<IDebugPortEx2> spPortEx;
                hr = pPort->QueryInterface(&spPortEx);
                if (SUCCEEDED(hr))
                {
                    // pass on the parameters we were given (omitted here)
                    hr = spPortEx->LaunchSuspended(/* omitted parameters */,ppDebugProcess)
                }
            }
        }
        return(hr);
    }
    
    HRESULT CDebugEngine::ResumeProcess(IDebugProcess2 *pDebugProcess)
    {
        // Make a program node for this process
        HRESULT hr;
        CComPtr<IDebugProgramNode2> spProgramNode;
        hr = this->GetProgramNodeForProcess(pProcess, &spProgramNode);
        if (SUCCEEDED(hr))
        {
            hr = m_spPortNotify->AddProgramNode(spProgramNode);
            if (SUCCEEDED(hr))
            {
                // resume execution of the process using the port given to us earlier.
               // (Querying for the IDebugPortEx2 interface is valid here since
               // that's how we got the IDebugPortNotify2 interface in the first place.)
                CComPtr<IDebugPortEx2> spPortEx;
                hr = m_spPortNotify->QueryInterface(&spPortEx);
                if (SUCCEEDED(hr))
                {
                    hr  = spPortEx->ResumeProcess(pDebugProcess);
                }
            }
        }
        return(hr);
    }