Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Math.Abs() calls into native method System.AppDomain.GetId()?

My conditional breakpoint sometimes works fine, and sometimes fails, with the following error:

The condition for a breakpoint failed to execute. The condition was 'Math.Abs(-4.36767421599683 -x) < 1e-5'. The error returned was 'Evaluation of method System.Math.Abs() calls into native method System.AppDomain.GetId(). Evaluation of native methods in this context is not supported.'.

  1. How does it only work some of the time? Is there runtime trickery happening where the conditional breakpoint code isn't the same each time I run it?
  2. If there was, why on earth would any version of the code for Abs() call AppDomain.GetId() - it's purely arithmetic surely?
  3. The .NET reference source shows a method extern public static double Abs(double). Doesn't that mean it's always native anyway? How could it ever work?
like image 690
Oli Avatar asked Dec 15 '16 17:12

Oli


1 Answers

First, you need to know that the debugger evaluate can't call native method. (well its depend if it real func eval or not, but even when its not real func eval we will see that its not completely true but wait...)

How does it only work some of the time? Is there runtime trickery happening where the conditional breakpoint code isn't the same each time I run it?

Yes it's strange but it could be. The debugger evaluator actually able to get the app domain id, but sometimes some error occurred and then you will get that exception.

If there was, why on earth would any version of the code for Abs() call AppDomain.GetId() - it's purely arithmetic surely?

I pretty sure that this call not relating just to Math.Abs.

The .NET reference source shows a method extern public static double Abs(double). Doesn't that mean it's always native anyway? How could it ever work?

Because for some of the native methods, the debugger evaluator has hooks that handle it in a special way. Math.Abs is one of them.


I tried to reproduce it in my Visual Studio but without success. (VS 2017)

Try to evaluate the same expression in Watch Window (with suffix of ,nse) and tell me if you still get the exception.

Just to show you an example of trying evaluate a method that call to native code: (read the error message)

enter image description here

One more thing, try to evaluate it in other version of Visual Studio.

like image 198
Dudi Keleti Avatar answered Oct 21 '22 18:10

Dudi Keleti