This project is read-only.

Does OpenCppCoverage work with managed C++ code?

Sep 8, 2016 at 9:34 PM
Does this work with managed C++? Is it able to either report on managed C++ coverage or ignore it?

I have tried it on a few native applications and it works great. However, I also have a native Windows app “TestRunner” that runs a series of C++ unit tests. Some of the tests call into managed C++ code.

The TestRunner app runs fine when executed without OpenCppCoverage.

First attempt at using OpenCppCoverage

But when I run under OpenCppCoverage and hit the test that will call into managed code, an exception is thrown that is caught by the TestRunner app itself. If I run the tests a second time without quitting the Windows TestRunner app, same thing happens.

OpenCppCoverage finishes and generates a valid coverage report. The report displays the executable lines in the managed C++ source files correctly, but they are all red, indicating that they were apparently not executed. The LastCoverageResults.log file doesn’t show any problems.

The "--continue_after_cpp_exception" flag has no apparent effect on this behavior.

Another attempt

If I put in “--excluded_modules” for the managed DLL, when I hit the test that will call into managed code, the target application exits, and apparently OpenCppCoverage.exe quits as well, since it doesn’t generate any coverage report. The last line output by OpenCppCoverage is:

Error: FileDebugInformation.cpp:78 Error when calling SymFromAddr

In Visual Studio debugger

If I run the application under the Visual Studio debugger (not using OpenCppCoverage at all), with all Exception catching turned on, the debugger stops at a number of exceptions, and I need to press "Continue". But still the code succeeds; none of the exceptions are uncaught, presumably because they were caught and handled within .NET, and are expected errors. (The initial error is a Microsoft C++ exception EEFileLoadException, followed by a few other Microsoft C++ exceptions and .NET exceptions.) If I run the tests a second time without quitting the Windows TestRunner app, there are no exceptions caught at all.

Analysis

So it seems like OpenCppCoverage is attached to the code like a debugger, but rather than ignoring .NET exceptions and letting .NET itself handle them, it is allowing them to propagate as uncaught exceptions out of the managed code back to the native code.

Question: is this kind of scenario supposed to work?

Thanks!
Sep 12, 2016 at 12:54 PM
Hello,

Unfortunately, managed code is not supported. It seems only the pdb format is supported.

I have just run vstest.console.exe (unmanaged application to run MSTest) on a managed tests.
  • If I exclude the managed dll, the test run fine but of course there is no coverage.
  • If I do not excluded the managed dll, I get an InvalidProgramException, which is normal because OpenCppCoverage modifies MSIL code as if it is x86 assembly.
"but rather than ignoring .NET exceptions and letting .NET itself handle them, it is allowing them to propagate as uncaught exceptions out of the managed code back to the native code."

I suspect exceptions are not ignored but caught inside the code. Your debugger probably breaks before the exception is thrown. OpenCppCoverage does not change whether an exception is caught or not. In my test, another kind of exception was thrown (InvalidProgramException) that made the program stop.

I hope this will help,

OpenCppCoverage
Marked as answer by OpenCppCoverage on 11/27/2016 at 7:07 AM