debugging - 由于 "code is optimized"异常,无法使用 Mdbg 进行 func-eval

标签 debugging garbage-collection clr mdbg

我们使用 MdbgCore.dll 来评估线程调用堆栈上参数的属性。

为此,我们正在执行 func-eval。

不幸的是,我们执行 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 安全点获取线程,以便我们可以执行 func-eval。我们会考虑任何允许我们使用线程确定性地闯入进程来执行 func-eval 的任何事情。

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

示例代码如下:

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;
        }

关于debugging - 由于 "code is optimized"异常,无法使用 Mdbg 进行 func-eval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789348/

相关文章:

c - C程序的调试(Redis服务器)

Eclipse 在运行 tomcat 时需要很长时间才能显示断点

flash - 使用 Flash + AS3 发布/调试配置

java - 垃圾回收会改变Java中的对象地址吗?

visual-studio-2010 - Visual Studio 2010 : Embed Interop Types

c# - 任何人都知道 IL/CLR 如何准确生成局部函数 C#7

eclipse - 在 Eclipse 中,有没有办法禁用断点,直到首先命中另一个断点?

javascript - 如何调试 Chrome Javascript 中 GC 事件之间看似长时间的停顿

c# - 幕后 Actor 是什么

c++ - 是什么让一种语言不愿意/不能包含 RAII?