.NETGURU
Debbuger under .Net
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Aniskevich Sergey via .Net Guru (VIP)
Hello.
I am use debugging API under .Net and have one problem.
It is a function which is called for single stepping a debugging.

void CDebuggerServices::Step( const bool const wantToStepIn )
{
    ICorDebugThread *currentThread = NULL;
    ICorDebugStepper *threadStepper = NULL;
    ICorDebugFrame *activeFrame = NULL;
    ICorDebugILFrame *activeILFrame = NULL;
    ICorDebugFunction *activeFunction = NULL;
    ISymUnmanagedDocument * sourceDocument = NULL;
    ISymUnmanagedMethod *symbolicMethod = NULL;
    mdMethodDef methodToken;
    HRESULT hr;
    ULONG32 currentInstructionPointer;
    COR_DEBUG_STEP_RANGE *stepRanges = NULL;
    ULONG32 *instructionRanges = NULL;

    try
    {
        currentThread = GetCurrentThread( );

        hr = currentThread->GetActiveFrame( &activeFrame );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get active frame in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        hr = activeFrame->QueryInterface( __uuidof( ICorDebugILFrame ),
            reinterpret_cast< LPVOID * >( &activeILFrame )
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not query active frame for IL frame in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        
        CorDebugMappingResult debugMappingResult;

        hr = activeILFrame->GetIP( ¤tInstructionPointer,
            &debugMappingResult
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get instruction pointer from IL frame in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        hr = activeFrame->GetFunction( &activeFunction );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get function in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        hr = activeFunction->GetToken( &methodToken );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get function token in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        hr = m_symbolReader->GetMethod( methodToken,
            &symbolicMethod
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get symbolic method in CDebuggerServices::Step\n" ) );
            throw hr;
        }
        unsigned int documentCount;

        hr = m_symbolReader->GetDocuments( documentCount,
            &documentCount,
            &sourceDocument
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get source document in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        ULONG32 rangeSize;
        int currentSourceLine;

        currentSourceLine = GetSourceLineFromInstructionPointer( currentInstructionPointer );

        if( 0 == currentSourceLine )
        {
            TRACE( _T( "Could not match IP to source line in CDebuggerServices::Step\n" ) );
            throw E_FAIL;
        }

        hr = symbolicMethod->GetRanges( sourceDocument,
            currentSourceLine,
            0,
            0,
            &rangeSize,
            NULL
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get IL range size for source line in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        instructionRanges = new ULONG32[ rangeSize ];

        hr = symbolicMethod->GetRanges( sourceDocument,
            currentSourceLine,
            0,
            rangeSize,
            &rangeSize,
            instructionRanges
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not get IL ranges for source line in CDebuggerServices::Step\n" ) );
            throw hr;
        }

        stepRanges = new COR_DEBUG_STEP_RANGE[ rangeSize / 2 ];

        for( int currentRangeToSet = 0; (ULONG32)currentRangeToSet <= rangeSize ; currentRangeToSet += 2 )
        {
            stepRanges[ currentRangeToSet / 2 ].startOffset = instructionRanges[ currentRangeToSet ];
            stepRanges[ currentRangeToSet / 2 ].endOffset = instructionRanges[ currentRangeToSet + 1 ];
        }

        hr = currentThread->CreateStepper( &threadStepper );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not create thread stepper in CDebuggerServices::Step\n" ) );
            throw hr;
        }
        
        hr = threadStepper->SetRangeIL(TRUE);
        hr = threadStepper->StepRange( wantToStepIn,
            stepRanges,
            rangeSize
            );

        if( FAILED( hr ) )
        {
            TRACE( _T( "Could not step into function CDebuggerServices::Step\n" ) );
            throw hr;
        }

    }
    catch( const HRESULT const &hr )
    {
        TRACE( _T( "HRESULT 0x%08X received during COM operation in CDebuggerServices::Step\n" ),
            hr
            );
    }

    if( NULL != threadStepper )
    {
        threadStepper->Release( );
        threadStepper = NULL;
    }

    if( NULL != activeFrame )
    {
        activeFrame->Release( );
        activeFrame = NULL;
    }

    if( NULL != activeILFrame )
    {
        activeILFrame->Release( );
        activeILFrame = NULL;
    }

    if( NULL != activeFunction )
    {
        activeFunction->Release( );
        activeFunction = NULL;
    }

    if( NULL != sourceDocument )
    {
        sourceDocument->Release( );
        sourceDocument = NULL;
    }

    if( NULL != symbolicMethod )
    {
        symbolicMethod->Release( );
        symbolicMethod = NULL;
    }

    if( NULL != stepRanges )
    {
        delete [] stepRanges;
        stepRanges = NULL;
    }

    if( NULL != instructionRanges )
    {
        delete [] instructionRanges;
        instructionRanges = NULL;
    }
    ContinueFromCallback( currentThread );
}

If in a debugged code there is a cycle this function counts it for one instruction.

Can you help me?
--------------------------------
From: Aniskevich Sergey

-----------------------
Posted by a user from .Net Guru (http://www.dot-net-guru.com/)

<Id>VPUy2gOiV0SEPKS9Ne/3Dw==</Id>
Reply to this message...
 
    
Jonathan Keljo [MS] (VIP)
What exactly do you mean by "a cycle" in debugged code? A sample piece of
debugged code that shows the issue, along with the IL ranges in it, would
be very helpful in discussing the issue.

However glancing over the code, I suspect your issue is the use of
StepRange. The code builds up a list of all source ranges in the function.
and then passes it to StepRange. StepRange will complete only when
execution passes outside of the ranges it is given, so if the source ranges
completely cover the IL for the method--which appears to be the case
here--that means it won't complete until it leaves the method. So this
method looks like it would work fine for step-in, but step-over would
behave like a step-out.

If you're trying to step a single source line, you should use the range for
the current source line in StepRange. If you're trying to step the smallest
possible increment, you should just use Step.

Jonathan

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: Aniskevich Sergey via .Net Guru <Click here to reveal e-mail address>
| X-Newsreader: AspNNTP 1.50 (Dot Net Guruhew Reynolds Consulting)
| Subject: Debbuger under .Net
| Mime-Version: 1.0
| Content-Type: text/plain; charset="us-ascii"
| Content-Transfer-Encoding: 7bit
| Message-ID: <Click here to reveal e-mail address>
| Newsgroups: microsoft.public.dotnet.framework.clr
| Date: Sat, 28 Aug 2004 05:54:44 -0700
| NNTP-Posting-Host: 81-86-69-114.dsl.pipex.com 81.86.69.114
| Lines: 1
| Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP0
8.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.clr:11734
| X-Tomcat-NG: microsoft.public.dotnet.framework.clr
|
| Hello.
| I am use debugging API under .Net and have one problem.
| It is a function which is called for single stepping a debugging.
|
| void CDebuggerServices::Step( const bool const wantToStepIn )
| {
|     ICorDebugThread *currentThread = NULL;
|     ICorDebugStepper *threadStepper = NULL;
|     ICorDebugFrame *activeFrame = NULL;
|     ICorDebugILFrame *activeILFrame = NULL;
|     ICorDebugFunction *activeFunction = NULL;
|     ISymUnmanagedDocument * sourceDocument = NULL;
|     ISymUnmanagedMethod *symbolicMethod = NULL;
|     mdMethodDef methodToken;
|     HRESULT hr;
|     ULONG32 currentInstructionPointer;
|     COR_DEBUG_STEP_RANGE *stepRanges = NULL;
|     ULONG32 *instructionRanges = NULL;
|
|     try
|     {
|         currentThread = GetCurrentThread( );
|
|         hr = currentThread->GetActiveFrame( &activeFrame );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get active frame in CDebuggerServices::Step\n" )
);
|             throw hr;
|         }
|
|         hr = activeFrame->QueryInterface( __uuidof( ICorDebugILFrame ),
|             reinterpret_cast< LPVOID * >( &activeILFrame )
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not query active frame for IL frame in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         
|         CorDebugMappingResult debugMappingResult;
|
|         hr = activeILFrame->GetIP( ¤tInstructionPointer,
|             &debugMappingResult
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get instruction pointer from IL frame in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         hr = activeFrame->GetFunction( &activeFunction );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get function in CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         hr = activeFunction->GetToken( &methodToken );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get function token in CDebuggerServices::Step\n"
) );
|             throw hr;
|         }
|
|         hr = m_symbolReader->GetMethod( methodToken,
|             &symbolicMethod
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get symbolic method in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|         unsigned int documentCount;
|
|         hr = m_symbolReader->GetDocuments( documentCount,
|             &documentCount,
|             &sourceDocument
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get source document in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         ULONG32 rangeSize;
|         int currentSourceLine;
|
|         currentSourceLine = GetSourceLineFromInstructionPointer(
currentInstructionPointer );
|
|         if( 0 == currentSourceLine )
|         {
|             TRACE( _T( "Could not match IP to source line in
CDebuggerServices::Step\n" ) );
|             throw E_FAIL;
|         }
|
|         hr = symbolicMethod->GetRanges( sourceDocument,
|             currentSourceLine,
|             0,
|             0,
|             &rangeSize,
|             NULL
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get IL range size for source line in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         instructionRanges = new ULONG32[ rangeSize ];
|
|         hr = symbolicMethod->GetRanges( sourceDocument,
|             currentSourceLine,
|             0,
|             rangeSize,
|             &rangeSize,
|             instructionRanges
|             );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not get IL ranges for source line in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|
|         stepRanges = new COR_DEBUG_STEP_RANGE[ rangeSize / 2 ];
|
|         for( int currentRangeToSet = 0; (ULONG32)currentRangeToSet <= rangeSize
; currentRangeToSet += 2 )
|         {
|             stepRanges[ currentRangeToSet / 2 ].startOffset = instructionRanges[
currentRangeToSet ];
|             stepRanges[ currentRangeToSet / 2 ].endOffset = instructionRanges[
currentRangeToSet + 1 ];
|         }
|
|
|
|         hr = currentThread->CreateStepper( &threadStepper );
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not create thread stepper in
CDebuggerServices::Step\n" ) );
|             throw hr;
|         }
|         
|         hr = threadStepper->SetRangeIL(TRUE);
|         hr = threadStepper->StepRange( wantToStepIn,
|             stepRanges,
|             rangeSize
|             );
|
|
|         if( FAILED( hr ) )
|         {
|             TRACE( _T( "Could not step into function CDebuggerServices::Step\n" )
);
|             throw hr;
|         }
|
|     }
|     catch( const HRESULT const &hr )
|     {
|         TRACE( _T( "HRESULT 0x%08X received during COM operation in
CDebuggerServices::Step\n" ),
|             hr
|             );
|     }
|
|     if( NULL != threadStepper )
|     {
|         threadStepper->Release( );
|         threadStepper = NULL;
|     }
|
|     if( NULL != activeFrame )
|     {
|         activeFrame->Release( );
|         activeFrame = NULL;
|     }
|
|     if( NULL != activeILFrame )
|     {
|         activeILFrame->Release( );
|         activeILFrame = NULL;
|     }
|
|     if( NULL != activeFunction )
|     {
|         activeFunction->Release( );
|         activeFunction = NULL;
|     }
|
|     if( NULL != sourceDocument )
|     {
|         sourceDocument->Release( );
|         sourceDocument = NULL;
|     }
|
|     if( NULL != symbolicMethod )
|     {
|         symbolicMethod->Release( );
|         symbolicMethod = NULL;
|     }
|
|     if( NULL != stepRanges )
|     {
|         delete [] stepRanges;
|         stepRanges = NULL;
|     }
|
|     if( NULL != instructionRanges )
|     {
|         delete [] instructionRanges;
|         instructionRanges = NULL;
|     }
|     ContinueFromCallback( currentThread );
| }
|
|
| If in a debugged code there is a cycle this function counts it for one
instruction.
|
| Can you help me?
| --------------------------------
| From: Aniskevich Sergey
|
| -----------------------
| Posted by a user from .Net Guru (http://www.dot-net-guru.com/)
|
| <Id>VPUy2gOiV0SEPKS9Ne/3Dw==</Id>
|

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification