Note that there are some explanatory texts on larger screens.

plurals
  1. POForcing WaitForDebugEvent to execute a certain thread
    primarykey
    data
    text
    <p>I am writing a windows debugger, so I have the usual ContinueDebugEvent/WaitForDebugEvent loop:</p> <pre><code>while (true) { ContinueDebugEvent(hTargetProcessId, hTargetThreadId, lastDebugResult); WaitForDebugEvent(&amp;evt, INFINITE); ... hTargetThreadId = evt.dwThreadId; } </code></pre> <p>This works fine, but I'm in the process of adding a feature which requires the ability to execute a specific thread mid-debugging. To do this, I suspend all threads I'm not interested in, like this (I've omitted a load of code for brevity - hope this is clear enough!):</p> <pre><code>while (true) { if (onlyACertainThread) { for (int n=0; n&lt;threadcount; n++) if(threads[n] != certainThread) SuspendThread(threads[n]); } ContinueDebugEvent(hTargetProcessId, hTargetThreadId, lastDebugResult); WaitForDebugEvent(&amp;evt, INFINITE); if (onlyACertainThread) { for (int n=0; n&lt;threadcount; n++) if(threads[n] != certainThread) ResumeThread(threads[n]); } ... (code to add to 'threads', update threadcount, decide if we want to restrict to a certain thread, etc) ... hTargetThreadId = evt.dwThreadId; } </code></pre> <p>However, I often see debug events from the wrong thread. For example:</p> <ul> <li>Thread A is running</li> <li>Decide we want to observe thread B; suspend thread A, call continue/waitfordebugevent(.. A.pid..)</li> <li>Get debug notifications (breakpoints, exceptions) from thread A.</li> </ul> <p>What I want is for the final step to be 'get notifications from B'. I've tried calling WaitForDebugEvent(..B.pid..) which doesn't work.</p> <p>Is this expected behaviour? </p> <p>My suspicion is that thread suspend/resume counters are only observed by the kernel when at thread is being scheduled; ie, at the end of a timeslice. This would cause thread A to continue running until its current timeslice is up. Can anyone confirm/deny this idea? If so.. how would I work around it? My best idea at the moment is to 'inject' an artificial call to Sleep(0) after I suspend a thread, but I really don't want to do that unless I have to!</p> <p>If it's any difference, the machine I run on is a single-threaded XP box.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload