首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于“代码已优化”异常,无法使用Mdbg进行函数求值

由于“代码已优化”异常,无法使用Mdbg进行函数求值
EN

Stack Overflow用户
提问于 2012-03-20 23:05:13
回答 1查看 390关注 0票数 4

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

为了做到这一点,我们正在执行函数求值。

不幸的是,对于CORDBG_E_ILLEGAL_IN_OPTIMIZED_CODE,我们所有执行函数求值的尝试都失败了,这似乎是由于用于函数求值的线程没有处于GC安全点。

这在这里有文档:http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx

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

关于这个主题的文档非常稀少,我们正在努力弄清楚是否有办法在GC安全点获得线程,这样我们就可以进行函数求值。我们会考虑任何允许我们确定性地进入进程并使用线程进行函数求值的东西。

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

示例代码如下:

代码语言:javascript
复制
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;
       }
   }

}

微软/ Mdbg团队,你能帮上忙吗?

最好的,迈克

EN

回答 1

Stack Overflow用户

发布于 2012-05-24 21:02:41

这与JIT优化有关吗?在我的程序中,我关闭了JIT优化(出于技术原因,我认为只能使用CreateProcess()而不能使用Attach())。

代码语言:javascript
复制
 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;
        }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9789348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档