3

Suppose you're debugging an application with 2 threads, Thread A and B, and you have a breakpoint set somewhere that is reachable by A. You select A as the active thread and run until it hits that breakpoint and execution stops for all threads.

At this point, if you were to hit "Step Over" (F10), do A and B both progress by one line of code 1? Or do all threads single-step forward?

What if instead, you hit "Step Into" (F11) or "Step Out" (Shift + F11) or "Continue"? What do the inactive thread(s) do?

I ask because, sometimes, it seems like while I single-step through code in the active thread, the other (left unfrozen) threads execute one or multiple steps (or possibly however many instructions they can execute in the time it takes the active thread to run until it reaches the next LoC), which is undesirable. But this might be the intended behavior, which is why VS allows you to Freeze/Thaw threads - so that you can manage the lock-stepping yourself to have granularity in thread execution order.

1: I don't think Step Over always single steps through the next line of code. Sometimes it steps through the next assembly instruction.

6
  • Threads operate independently. Every "step" operation on the current thread only applies to the current thread. Commented Feb 11 at 20:27
  • @DrewDormann I understand and observe that. I'm asking about the things I can't observe - what are the other unfrozen threads doing? Commented Feb 11 at 20:31
  • 1
    You have to add breakpoints, each thread runs independently. If the two threads share the same code and you put a breakpoint to a common code, both threads will break and stepping will be messed up. Put a flag to distinguish the thread you want to break. Commented Feb 11 at 20:35
  • 1
    As you already observed, the other threads run as long (far?) as they get until the active thread stops due to the "step over" or "step into" instruction. The threads do not progress in lock-step. Commented Feb 11 at 20:54
  • 1
    @MinhTran The "program pointer" icon that points at the line where the program stopped has an additional sub-symbol that indicates when the program stopped in a thread that is different from the one in which the execution was continued. Commented Feb 12 at 6:10

2 Answers 2

3

When you hit a breakpoint in VS, all threads for the application you are debugging are paused at whatever point they happen to be at. When you click "step over/into" in the active thread, all other threads will be unpaused and will execute as many instructions as possible, until the active thread reaches the next breakable line.

This could be one instruction for one thread, and multiple for another; this really depends on how long it takes to step over the instruction in the thread you were debugging.

You cannot "lock step" each thread to each other, short of putting breakpoints on the next line of code for all the active threads.

You can, in VS, select the threads from a dropdown to see what each is currently paused on.

Sign up to request clarification or add additional context in comments.

1 Comment

To be clear this also means that if you then single step to the next function in the current call sequence all other threads will briefly run again too (and then will be paused again) when the next line is hit (so you can even hit race conditions that will be very rare when running normally since your timing will definitely be different).
1

The above answer is fine, I just want to add a simple detail. Windows indeed allows stepping only the currently in-break thread by first calling SuspendThread to all other threads before resuming the current thread.

See description in ResumeThread.

Note that while reporting debug events, all threads within the reporting process are frozen. Debuggers are expected to use the SuspendThread and ResumeThread functions to limit the set of threads that can execute within a process. By suspending all threads in a process except for the one reporting a debug event, it is possible to "single step" a single thread. The other threads are not released by a continue operation if they are suspended

It is just that VS debugger doesnt support that but perhaps you could emulate it with a function that can be called from the immediate panel: call a custom function there that would suspend every other thread and then step.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.