How can I tell which process I am debugging (attached to multiple processes) in Visual Studio?
Asked Answered
T

3

11

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?

Tomchay answered 21/1, 2013 at 16:17 Comment(1)
H
10

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

Huxley answered 21/1, 2013 at 16:22 Comment(2)
Both solutions work, though this one is more succinct in retrieving the ID through the watch window so I'll mark this as the answer.Tomchay
Can also use ProcessName instead of ID to give something a bit more relevent (assuming the process name is clear)Romain
H
10

That's what the Processes window is for (Debug->Windows->Processes, or Ctrl+Alt+Z).

Hymettus answered 5/2, 2013 at 12:10 Comment(1)
The ID column in that window is the PID. If it's hex and you want it in decimal (ala Task Manager), switch to the Autos window, right-click and flip the Hexadecimal Display option, then switch back to the Processes window.Scifi
N
5

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

Nalda answered 21/1, 2013 at 16:22 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.