【问题标题】:Unable to func-eval using Mdbg due to "code is optimized" exception由于“代码已优化”异常,无法使用 Mdbg 进行 func-eval
【发布时间】:2012-04-05 01:41:23
【问题描述】:

我们正在使用 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 安全点的线程,但它们似乎都将 UserState 标记为 USER_UNSAFE_POINT。

关于这个主题的文档非常稀少,我们正在努力找出是否有办法在 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 团队,你能帮忙吗?

最好, 迈克

【问题讨论】:

  • 您好,您找到解决方案了吗?

标签: debugging garbage-collection clr mdbg


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 2012-11-23
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多