Debugging 由于“无法使用Mdbg进行功能评估”;“代码已优化”;例外

Debugging 由于“无法使用Mdbg进行功能评估”;“代码已优化”;例外,debugging,garbage-collection,clr,mdbg,Debugging,Garbage Collection,Clr,Mdbg,我们使用MdbgCore.dll根据线程调用堆栈上的参数计算属性 为此,我们正在执行函数评估 不幸的是,我们执行函数求值的所有尝试都失败了,因为优化代码中的CORDBG_E_非法,这似乎是由于用于函数求值的线程不在GC安全点 此处记录了这一点: 我们尝试扫描进程中的所有线程,以找到位于GC安全点的线程,但它们似乎都有UserState标记为USER_UNSAFE_point 关于这个主题的文档非常稀少,我们正在努力弄清楚是否有办法在GC安全点中获取线程,以便进行函数评估。我们会考虑任何允许我们用

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

为此,我们正在执行函数评估

不幸的是,我们执行函数求值的所有尝试都失败了,因为优化代码中的CORDBG_E_非法,这似乎是由于用于函数求值的线程不在GC安全点

此处记录了这一点:

我们尝试扫描进程中的所有线程,以找到位于GC安全点的线程,但它们似乎都有UserState标记为USER_UNSAFE_point

关于这个主题的文档非常稀少,我们正在努力弄清楚是否有办法在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;
       }
   }
if(argument.TypeName.EndsWith)(
“WorkerRequest”,StringComparison.OrdinalIgnoreCase)
&&!argument.IsNull)
{
尝试
{
//调用“GetUriPath()”函数以获取URI
字符串functionName=“System.Web.HttpWorkerRequest.GetUriPath”;
MDbgFunction func=debugger.processs.Active.ResolveFunctionNameFromScope(
函数名,
thread.CorThread.AppDomain
);
if(null==func)
{
抛出新的InvalidOperationException(
Format(“无法解析{0}”,functionName));
}
//设置评估
CorEval eval=threadForFuncEvals.CorThread.CreateEval();
//设置功能参数
列表值=新列表();
//添加工作请求“this”指针
值。添加(
参数.CorValue
);
//恢复用于执行函数评估的线程
threadForFuncEvals.CorThread.DebugState=CorDebugThreadState.THREAD\u RUN;
//将函数排队执行
//下面抛出异常
//下面抛出异常
//下面抛出异常
eval.CallFunction(func.CorFunction,values.ToArray());
//我们应该暂停所有其他线程以防止它们移动吗?
//继续该过程以执行该函数
如果(!proc.Go().WaitOne(settings.BreakTimeout))
{
抛出新的InvalidOperationException(“评估函数时超时”);
}
//获取返回的字符串
var结果=评估结果;
如果(结果!=null)
{
MDbgValue mv=新的MDbgValue(过程、结果);
字符串returnedValue=mv.GetStringValue(false);
Url=returnedValue;
}
}
捕获(例外e)
{
//错误:忽略异常
}
最后
{
//再次挂起线程
if(threadForFuncEvals!=null)
{
threadForFuncEvals.CorThread.DebugState=
CorDebugThreadState.THREAD\u挂起;
}
}
}

Microsoft/Mdbg团队,您能帮忙吗

最好的,
Mike这与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;
        }