调试 – 由于“代码优化”异常,无法使用Mdbg执行func-eval

我们正在使用MdbgCore.dll来评估线程callstack上的参数的属性.

为此,我们正在执行一个功能.

不幸的是,我们所有尝试执行func-eval的尝试都失败了CORDBG_E_ILLEGAL_IN_OPTIMIZED_CODE,这似乎是由于用于func-eval的线程不在GC安全点.

这在此处记录:http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx.

我们尝试扫描进程中的所有线程以查找处于GC安全点的线程,但它们似乎都具有标记为USER_UNSAFE_POINT的UserState.

关于这个主题的文档非常稀缺,我们正在努力弄清楚是否有办法在GC安全点获取一个线程,以便我们可以执行功能.我们会考虑任何允许我们确定性地使用线程来执行函数的过程.

免责声明:我们正在尝试评估驻留在优化程序集中的类的方法,因此不确定这是否也可能导致问题.

示例代码如下:

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团队,您能帮忙吗?

最好,
麦克风

最佳答案 这与JIT优化有关吗?

在我的程序中,我关闭了JIT优化(出于技术原因,我认为你只能使用CreateProcess()而不使用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;
        }
点赞