1

When debugging multithread app, Is it possible to glue the debugger to a particular thread? because when I do step by step (F7) debugging often I jump in another thread and that is a little awful for debugging :(

5
  • I think when stepping then only the thread being debugged will run. I don't experience what you describe. What OS are you targeting? Remember that there are many different targets these days. Commented Mar 5, 2022 at 13:32
  • Thanks @DavidHeffernan I m under windows, targeting win32. it's just that often the step by step jump between thread (if their is a breakpoint that is fired by another thread than the current I m debugging off course)
    – zeus
    Commented Mar 5, 2022 at 13:36
  • Does this happen in an application using Indy components? Commented Mar 5, 2022 at 14:49
  • 1
    Threads come and go so it is relatively hard to set a break point on a specific thread (unless in a very specific scenario you want to debug). If possible, use a logging system to generate extensive log entries so that you can learn more about what happens at runtime. CodeSite (or its alternatives) is commonly used, raize.com/codesite
    – Lex Li
    Commented Mar 5, 2022 at 15:41
  • This happens to me all the time and it's annoying. It would be really great to have such a feature.
    – dummzeuch
    Commented Mar 6, 2022 at 10:50

1 Answer 1

6

If I interpret your question as:

"When debugging is it possible to only allow execution of the thread I am debugging"

Then the answer is yes.

  • Set your breakpoint.
  • When the debugger hits your breakpoint, go to the threads view. The current thread will be marked with a 'running' icon (it has a green 'play' arrowhead on it in recent versions of RAD Studio).
  • Right click on that thread and choose Freeze all other threads.

The debugger will now allow you to single step through the current thread without allowing the other threads to be scheduled.

When you have finished you can right click on any of the threads and choose Thaw all threads

Of course this will affect the behaviour of the system as the other threads will not be running.

If I interpret your question as:

"Is it possible to ensure that a breakpoint only fires for a particular thread"

Then the answer is yes.

  • Set your breakpoint.
  • Right click on the breakpoint and choose "Breakpoint Properties".
  • Select a thread from the dropdown list and only this thread will break into the debugger at the breakpoint.

If I interpret your question as:

"Is it possible to have all threads running, but only single step through a single thread in the debugger (while other threads run)"

Then practically I think the answer is no. You could set a break point on each successive line and set the properties for the breakpoint to the current thread, but that's not really practical for most single-stepping.

Remember that when debugging any enabled breakpoint (hit by any thread) will be evaluated and will cause a break into the debugger unless the properties of the breakpoint prevent that. When single stepping the debugger is effectively inserting a breakpoint at the next line, so any thread hitting that line will cause a break to the debugger.

If all of your threads are executing different code this will not be a problem, but if they are executing common code (including library routines and object methods) then any thread hitting that next step will break.

Another thing that may help you with single-stepping a multi-threaded application when you have a number of breakpoints set is to disable them in the breakpoints window (normally a tab in the bottom part of the debugger view) when single stepping so that hitting one of them in another thread does not cause a break to the debugger.

2
  • "When single stepping the debugger is effectively inserting a breakpoint at the next line" - no: with my 20 threads the next line of code being highlighted for stepping through is unpredictable, because any of them can be next (at any position of the program/code). There's still missing a debugger feature like "step selected thread(s)", because I don't want the program to break (pause) for unselected thread's actions.
    – AmigoJack
    Commented Mar 7, 2022 at 1:05
  • Hi @AmigoJack, I use multiple threads routinely, and I agree that any thread can break into the debugger, but as I recall using trace into | step over doesn't get to break in another thread unless that thread hits a breakpoint. "I don't want the program to break (pause) for unselected thread's actions." The debugger attaches to the process, not to an individual thread. When in the debugger all threads are suspended until execution begins again. You can 'Freeze all other threads' as I pointed out in my answer. Commented Mar 7, 2022 at 8:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.