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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alec
  • 946
  • 1
  • 11
  • 22

3 Answers3

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

Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
  • 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. – Alec Jan 21 '13 at 16:26
  • Can also use ProcessName instead of ID to give something a bit more relevent (assuming the process name is clear) – Dave May 02 '19 at 10:01
10

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

Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • 2
    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. – Spike0xff Jun 17 '14 at 19:41
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

Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74