4

We are using MdbgCore.dll to evaluate a property off a parameter on a thread callstack.

To do this, we are performing a func-eval.

Unfortunately, all our attempts to perform the func-eval are failing with CORDBG_E_ILLEGAL_IN_OPTIMIZED_CODE, which seems to be due to the thread being used for the func-eval not being in a GC-safe point.

This is documented here: http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx.

We tried scanning all threads in the process to find a thread that is in a GC-safe point, but they all appear to have UserState marked with USER_UNSAFE_POINT.

There is very scarce documentation on the subject, and we are pulling our hair out trying to figure out if there is a way to get a thread in a GC-safe point so we can do the func-eval. We would consider anything that allows us to deterministically break into the process with a thread to do the func-eval with.

Disclaimer: we are trying to evaluate a method on a class that resides in an optimized assembly, so not sure if this is maybe also causing an issue.

The sample code follows:

if (argument.TypeName.EndsWith(
      "WorkerRequest", StringComparison.OrdinalIgnoreCase)
       && !argument.IsNull)
{
   try
   {
       // Invoke the "GetUriPath()" function to obtain the URI
       string functionName = "System.Web.HttpWorkerRequest.GetUriPath";

       MDbgFunction func = debugger.Processes.Active.ResolveFunctionNameFromScope(
           functionName, 
           thread.CorThread.AppDomain
       );
       if (null == func)
       {
           throw new InvalidOperationException(
               String.Format("Could not resolve {0}", functionName));
       }

       // Setup the eval
       CorEval eval = threadForFuncEvals.CorThread.CreateEval();

       // Setup the function parameters
       List<CorValue> values = new List<CorValue>();

       // Add the worker request "this" pointer
       values.Add(
           argument.CorValue
           );

       // resume the thread being used to do the func-eval
       threadForFuncEvals.CorThread.DebugState = CorDebugThreadState.THREAD_RUN;

       // Queue the function for execution

       // EXCEPTION THROWN BELOW
       // EXCEPTION THROWN BELOW
       // EXCEPTION THROWN BELOW

       eval.CallFunction(func.CorFunction, values.ToArray());   



       // BUGBUG: Should we pause all other threads to prevent them from moving?

       // Continue the process to execute the function
       if (!proc.Go().WaitOne(settings.BreakTimeout))
       {
           throw new InvalidOperationException("Timeout while evaluating function");
       }

       // get the returned string
       var result = eval.Result;
       if (result != null)
       {
           MDbgValue mv = new MDbgValue(proc, result);

           string returnedValue = mv.GetStringValue(false);

           threadInfo.Url = returnedValue;
       }
   }
   catch (Exception e)
   {
       // BUGBUG: Ignoring exception
   }
   finally
   {
       // suspend the thread again
       if (threadForFuncEvals != null)
       {
           threadForFuncEvals.CorThread.DebugState =
                        CorDebugThreadState.THREAD_SUSPEND;
       }
   }

}

Microsoft / Mdbg team, can you help?

Best, Mike

ionwarp
  • 215
  • 1
  • 10

1 Answers1

2

Is this something to do with JIT optimization? In my program, I turn JIT optimization off (for technical reasons, I think you can only do this with CreateProcess() and not using Attach()).

 proc = m_Debugger.CreateProcess(ProcessName, ProcessArgs, DebugModeFlag.Default, DebugEngineUtils.GetAssemblyRuntimeVersion(ProcessName,DefaultNetVersion));
 if (proc!=null) proc.CorProcess.OnCreateProcess += new Microsoft.Samples.Debugging.CorDebug.CorProcessEventHandler(CorProcess_OnCreateProcess);
 if (proc!=null) proc.CorProcess.OnModuleLoad += new Microsoft.Samples.Debugging.CorDebug.CorModuleEventHandler(CorProcess_OnModuleLoad);
 void CorProcess_OnModuleLoad(object sender, Microsoft.Samples.Debugging.CorDebug.CorModuleEventArgs e)
        {
            e.Module.JITCompilerFlags = Microsoft.Samples.Debugging.CorDebug.CorDebugJITCompilerFlags.CORDEBUG_JIT_DISABLE_OPTIMIZATION;
        }

 void CorProcess_OnCreateProcess(object sender, Microsoft.Samples.Debugging.CorDebug.CorProcessEventArgs e)
        {
            //try to disable optimization
            ((Microsoft.Samples.Debugging.CorDebug.CorProcess)sender).DesiredNGENCompilerFlags = Microsoft.Samples.Debugging.CorDebug.CorDebugJITCompilerFlags.CORDEBUG_JIT_DISABLE_OPTIMIZATION;
        }
Brian Donahue
  • 216
  • 1
  • 3