does this is a weird behavior of DebuggerNonUserCodeAttribute?

44 Views Asked by At

I'm trying to make the Visual Studio Debugger to step into a specific inner function when hitting F11 (Step Into) on an outer function call,

I have the following call snippets:

internal class Program
{
    static void Main()
    {
        NonUserCodeFunc();
    }

    [DebuggerNonUserCode]
    public static void NonUserCodeFunc()
    {
        LogStart();
        SomeFunc();
    }

    private static void SomeFunc()
    {
        Console.WriteLine(MethodBase.GetCurrentMethod().Name);
    }

    public static bool DoLog { get; set; } = true;

    [DebuggerNonUserCode()]
    private static void LogStart()
    {
        if (DoLog)
        {
            Console.WriteLine("Start");
        }
    }
}

my purpose is to make the debugger to step into the 'SomeFunc' when the user hit F11 on the calling to 'NonUserCodeFunc' in the main.

I can achieve this only when the condition line in the 'LogStart' function is comment out. Why so?

1

There are 1 best solutions below

0
Dou Xu-MSFT On

I can achieve this only when the condition line in the 'LogStart' function is comment out

Base on my test result, it is a weird behavior of DebuggerNonUserCodeAttribute. It looks like you've got some debugging settings wrong in Visual Studio.

When you using DebuggerNonUserCode, this attribute marks code as External Code and you will not hit breakpoints or step into code marked with this attribute.

Test result:

enter image description here

You can toggle off the Just My Code” (JMC) setting to ignore the attribute.

Please check if you enable Just My Code and make sure disable this feature. under Tools > Options (or Debug > Options) > Debugging > General, deselect Enable Just My Code.

https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code?view=vs-2022#BKMK_Enable_or_disable_Just_My_Code

Besides please check whether you have enabled Step over properties and operators (Managed only). On the Debugging > General page, clear the Step over properties and operators (Managed only) checkbox.

To make a summary, if you want to make the debugger to step into the 'SomeFunc' when the user hit F11 on the calling to 'NonUserCodeFunc' in the main, please make sure that you have cleared the Step over properties and operators on debugging settings.

Expected test result: enter image description here

Hope it can help you.