24

In Visual Studio 2017 I would like to use the Diagnostic Tools window to analyze my unit tests (x64), written in C#. However it always displays the message

Multiple processes have been debugged. This window only shows data for testhost.exe (PID: xxxxx).

VS Diagnostic Tools Window Screenshot

testhost.exe doesn't help me much, I would need QTAgent.exe. I can see in the Processes window that the debugger is attached to both processes, QTAgent.exe and testhost.exe. But I can't find a way how to switch to QTAgent.exe in the Diagnostic Tools window. How can you do that?

Detaching from testhost.exe does not help.

Tobias
  • 2,089
  • 2
  • 26
  • 51
  • `testhost.exe` is the test runner. `qtagent.exe` is the *load tester*. Why are *they* being debugged in the first place? What they do doesn't matter, they aren't your application or web site. Were you looking for the Performance profiler perhaps? – Panagiotis Kanavos Apr 17 '18 at 10:19
  • 1
    I don't want to debug my application, but my unit test. And I want to analyze the memory consumption of my unit test, which is why I want to use the Diagnostic Tools window. When I run my unit test and put a break point in the code and the debugger stops at that, it's always the `QTAgent.exe` process which is active. – Tobias Apr 17 '18 at 14:59

8 Answers8

5

Only one process is active in the debugger at any given time. You can set the active or current process in the Debug Location toolbar or in the Processes window. To switch between processes, both processes must be in break mode.

On the Debug Location toolbar, choose Process to view the Process list box. Select the process that you want to designate as current process. enter image description here

If the Debug Location toolbar is not visible, choose Tools, Customize. On the Toolbars tab, choose Debug Location.

Open the Processes window (shortcut Ctrl+Alt+Z), find the process that you want to set as the current process, and double-click it.

enter image description here

The current process is marked by a yellow arrow.

Switching to a project sets it the current process for debugging purposes. Any debugger window that you view will show the state for the current process, and all stepping commands affect only the current process.

Anita George
  • 1,145
  • 6
  • 18
  • 4
    I think you misunderstood my question. My question was how to switch the process in the **Diagnostic Tools window**. Switching the active process in the debugger, like you described, does NOT change the process which is shown in the Diagnostic Tools window. At least not in my VS. Does it work for you? – Tobias Apr 16 '18 at 09:51
  • @Tobias this answer is correct. You don't switch anything in the Diagnostics Window - it's just a window that displays diagnostic data from the session being debugged. You switch the process in the *debugger*. BTW why are you debugging the *test runner*? – Panagiotis Kanavos Apr 17 '18 at 10:18
  • @PanagiotisKanavos See also my comment under the question above. So if the answer is correct, please tell me how I can get the Diagnostic Tools window to show data for QTAgent.exe. Maybe I don't understand it and you could give me step-by-step instructions? The problem is that when I debug a unit test, the `QTAgent.exe` is the active process in the debugger but `testhost.exe` is the one being shown in Diagnostic Tools. And I'm not able to change the latter one. – Tobias Apr 17 '18 at 15:02
2

Not a 100% solution, but usable if you are only interested in CPU usage profiling:

  1. Disable Diagnostic Tools while debugging (Options -> Debugging or simply search for "diagn")
  2. Set a breakpoint in the first line of code of your unit test.
  3. Open "Performance Profiler" (ALT+F2) and attach to "QTAgent32.exe"
  4. Continue debugging (F5)
  5. Inspect where CPU time is going:

enter image description here

Jack Miller
  • 6,843
  • 3
  • 48
  • 66
  • Thanks for the tip. I tested this and it works, however when I posted the question I was interested in memory usage and not CPU usage. And unfortunately when using the Performance Profiler to attach to a running process, it only lets you profile CPU & GPU usage, but not memory. – Tobias Oct 11 '19 at 12:27
1

This thread is a little old but I found a workaround.

I needed to see Diagnostics for the iisexpress.exe process when debugging an Azure WebRole but instead the Diagnostics Tool was showing data for the WaIISHost.exe process.

This is what I did:

1- Opened the Processes window (Ctrl+Alt+Z) while debugging my WebRole on Visual Studio.

2- On the Processes window I right clicked on iisexpress.exe and selected "Detach process".

3- Opened a second Visual Studio and opened the same solution I was running on the first one.

4- Went to menu "Debug/Attach to Process".

5- Selected iisexpress.exe from the running processes and then I was finally seeing data for iisexpress.exe

Note: If you don't detach from the target process on your first Visual Studio then your second visual studio won't let you attach to it because it will complain there is already a debugging session on the same process.

Note 2: The detached process could be left orphan, meaning that when you stop Visual Studio debugger it might not kill the detached process and you might need to manually kill it using process explorer or something.

  • Thanks for your answer and welcome to StackOverflow! Although this thread is pretty old, there is still no solution. Unfortunately your answer doesn't help me, my problem is different. I cannot detach from QTAgent.exe, which is the process I need, because then the unit test would run to completion before I would be able to attach a 2nd VS to that process. Unit tests are very short-lived, while your iisexpress.exe remains running all the time. – Tobias Oct 08 '18 at 08:26
  • Clarifying Tobias's comment: We *can* detach "QTAgent.exe" but the instance we do, it will continue executing. So if the test is done by the time we attach the second debugger. If, however, the test was really long, this solution would work. – Jack Miller Oct 10 '19 at 09:20
1

Switching the current process did not help me.

It has been my experience that the Diagnostic Tools window attaches to the first process that is being debugged. I was able to change the order that the projects are started in my solution in the Solution Properties window where you select the Multiple startup projects (Common Properties>>Startup Project). Then, I used the up and down arrows on the right to move the project I wanted the Diagnostic Tools window to attach to to the top of this list making it start first.

MrSethT
  • 43
  • 6
1

Partial solution (for just one project):

  1. In solution properties > startup projects
  2. Under multiple startup projects
  3. Re-order the project you want diagnostics for as the first project (using up and down arrows)
hallz
  • 236
  • 4
  • 6
0

Finally, two years after my original question, this issue seems to be resolved with Visual Studio 2019 16.5.

Now QTAgent.exe does not appear anymore in the Processes window and seems to be not used anymore. Now there is only the testhost.exe process which is now executing the unit tests. Thus the Diagnostic Tools window now correctly shows the memory & CPU usage of the unit test, which is what I tried to achieve in my original issue.

To be precise, the original question in my post's title is not solved, but at least in my case I don't need to switch the process anymore, as the Diagnostic Tools window now works out of the box for unit tests.

Tobias
  • 2,089
  • 2
  • 26
  • 51
0

I found a workaround for this problem:
Ad a delay with System.Threading.Thread.Sleep(15000); at the beginning of the test. Set a breakpoint before the sleep and run the test in debug mode. When the breakpoint is reached detach the debugger and attach to QTAgent.exe in the given break by Thread.Sleep. In my case I got an exception and had to do the following also:
Disabling the Tools->Options setting "Debugging->General-> Enable property evaluation and other implicit function calls"

(Worked with VS 16.5.4)

user1127860
  • 150
  • 2
  • 14
0

I have just run into this issue on VS2019 16.6.1, and have a workaround that was effective in my case. If I try to debug the tests via anything in the Test Explorer or by the DebugTests dropdown option then the Diagnostic Tools window would attach to testhost.exe rather than the test executable, and this window ignores the process selection bar.

My solution was to instead select the project that produces the test executable and select Debug->StartNewInstance from the dropdown menu under the Solution Explorer. Starting the process this way shows only my test executable in the Processes tab of the debugging window, and the Diagnostic Tools window displays no warning.

The difference here seems to be that starting the debugger via the Test Explorer means Visual Studio is aware of your tests via the testhost, but debugging the project directly is just like attaching to any normal executable. The downside here is that if your test suite is large and you have a specific test you want to profile, then you may be waiting for a long time to reach the relevant portion.

Reported as an issue to Microsoft, and it appears to be an area of active development for the Test Tools team.

caldfir
  • 354
  • 3
  • 7