问题
I'm debugging a windows service which has two running instances, by attaching to both instances. I am doing this because I know only one instance will hit my breakpoint, but I'd like to know which instance that is, so that I don't have to attach to both in future.
Is there a way, when attached to multiple processes, that you can tell which one has hit a breakpoint? A trial-and-error solution would be to attach one at a time and see if the breakpoint is hit, or, stop one of the services (through services.msc) and see which process ID disappears - but neither solution seems scale-able to me. Is there a more elegant way?
Thanks!
回答1:
You could add a watch to the following statement:
System.Diagnostics.Process.GetCurrentProcess().Id
This gives you the PID (process id) you are attached to. You can look for pids in windows task manager (Menu View->Select Columns and tick PID).
Hope this helps
回答2:
You can try the "When Hit..." option available on a breakpoint (right click on the breakpoint, it's in the context menu that pops up). You can then print a message with the value of a variable along with lots of other information, such as:
$ADDRESS - Current Instruction
$CALLER - Previous Function Name
$CALLSTACK - Call Stack
$FUNCTION - Current Function Name
$PID - Process ID
$PNAME - Process Name
$TID - Thread ID
$TNAME - Thread Name
http://msdn.microsoft.com/en-us/library/232dxah7(v=vs.110).aspx
回答3:
That's what the Processes window is for (Debug->Windows->Processes, or Ctrl+Alt+Z).
来源:https://stackoverflow.com/questions/14442797/how-can-i-tell-which-process-i-am-debugging-attached-to-multiple-processes