internal string?StoreValue_CorDebug(DbgEvaluationInfo evalInfo, ILDbgEngineStackFrame ilFrame, Func <CreateCorValueResult> createTargetValue, DmdType targetType, object?sourceValue) { debuggerThread.VerifyAccess(); if (RequiresNoFuncEvalToStoreValue(targetType, sourceValue)) { return(StoreSimpleValue_CorDebug(evalInfo, ilFrame, createTargetValue, targetType, sourceValue)); } evalInfo.CancellationToken.ThrowIfCancellationRequested(); var tmp = CheckFuncEval(evalInfo); if (tmp is not null) { return(tmp.Value.ErrorMessage ?? throw new InvalidOperationException()); } var dnThread = GetThread(evalInfo.Frame.Thread); var createdValues = new List <CorValue>(); CreateCorValueResult createResult = default; try { var appDomain = ilFrame.GetCorAppDomain(); var reflectionAppDomain = targetType.AppDomain; using (var dnEval = dnDebugger.CreateEval(evalInfo.CancellationToken, suspendOtherThreads: (evalInfo.Context.Options & DbgEvaluationContextOptions.RunAllThreads) == 0)) { dnEval.SetThread(dnThread); dnEval.SetTimeout(evalInfo.Context.FuncEvalTimeout); dnEval.EvalEvent += (s, e) => DnEval_EvalEvent(dnEval, evalInfo); var converter = new EvalArgumentConverter(this, dnEval, appDomain, reflectionAppDomain, createdValues); var evalRes = converter.Convert(sourceValue, targetType, out var newValueType); if (evalRes.ErrorMessage is not null) { return(evalRes.ErrorMessage); } var sourceCorValue = evalRes.CorValue !; var sourceType = new ReflectionTypeCreator(this, reflectionAppDomain).Create(sourceCorValue.ExactType); createResult = createTargetValue(); if (createResult.Value is null) { return(CordbgErrorHelper.GetErrorMessage(createResult.HResult)); } return(StoreValue_CorDebug(dnEval, createdValues, appDomain, dnThread, createResult.Value, targetType, sourceCorValue, sourceType)); } } catch (TimeoutException) { return(PredefinedEvaluationErrorMessages.FuncEvalTimedOut); } catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex)) { return(CordbgErrorHelper.InternalError); } finally { if (createResult.CanDispose) { dnDebugger.DisposeHandle(createResult.Value); } foreach (var v in createdValues) { dnDebugger.DisposeHandle(v); } } }
internal DbgDotNetValueResult FuncEvalCall_CorDebug(DbgEvaluationInfo evalInfo, CorAppDomain appDomain, DmdMethodBase method, DbgDotNetValue?obj, object?[] arguments, bool newObj) { debuggerThread.VerifyAccess(); evalInfo.CancellationToken.ThrowIfCancellationRequested(); var tmp = CheckFuncEval(evalInfo); if (tmp is not null) { return(tmp.Value); } Debug.Assert(!newObj || method.IsConstructor); Debug.Assert(method.SpecialMethodKind == DmdSpecialMethodKind.Metadata, "Methods not defined in metadata should be emulated by other code (i.e., the caller)"); if (method.SpecialMethodKind != DmdSpecialMethodKind.Metadata) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError)); } var reflectionAppDomain = method.AppDomain; var methodDbgModule = method.Module.GetDebuggerModule() ?? throw new InvalidOperationException(); if (!TryGetDnModule(methodDbgModule, out var methodModule)) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError)); } var func = methodModule.CorModule.GetFunctionFromToken((uint)method.MetadataToken) ?? throw new InvalidOperationException(); int hr; var dnThread = GetThread(evalInfo.Frame.Thread); var createdValues = new List <CorValue>(); try { using (var dnEval = dnDebugger.CreateEval(evalInfo.CancellationToken, suspendOtherThreads: (evalInfo.Context.Options & DbgEvaluationContextOptions.RunAllThreads) == 0)) { dnEval.SetThread(dnThread); dnEval.SetTimeout(evalInfo.Context.FuncEvalTimeout); dnEval.EvalEvent += (s, e) => DnEval_EvalEvent(dnEval, evalInfo); var converter = new EvalArgumentConverter(this, dnEval, appDomain, reflectionAppDomain, createdValues); var genTypeArgs = method.DeclaringType !.GetGenericArguments(); var methTypeArgs = method.GetGenericArguments(); var typeArgs = genTypeArgs.Count == 0 && methTypeArgs.Count == 0 ? Array.Empty <CorType>() : new CorType[genTypeArgs.Count + methTypeArgs.Count]; int w = 0; for (int i = 0; i < genTypeArgs.Count; i++) { typeArgs[w++] = GetType(appDomain, genTypeArgs[i]); } for (int i = 0; i < methTypeArgs.Count; i++) { typeArgs[w++] = GetType(appDomain, methTypeArgs[i]); } if (typeArgs.Length != w) { throw new InvalidOperationException(); } var paramTypes = GetAllMethodParameterTypes(method.GetMethodSignature()); if (paramTypes.Count != arguments.Length) { throw new InvalidOperationException(); } bool hiddenThisArg = !method.IsStatic && !newObj; int argsCount = arguments.Length + (hiddenThisArg ? 1 : 0); var args = argsCount == 0 ? Array.Empty <CorValue>() : new CorValue[argsCount]; w = 0; DmdType origType; var declType = method.DeclaringType; if (hiddenThisArg) { if (method is DmdMethodInfo m) { declType = m.GetBaseDefinition().DeclaringType !; } var val = converter.Convert(obj, declType, out origType); if (val.ErrorMessage is not null) { return(DbgDotNetValueResult.CreateError(val.ErrorMessage)); } args[w++] = BoxIfNeeded(dnEval, appDomain, createdValues, val.CorValue !, declType, origType); } for (int i = 0; i < arguments.Length; i++) { var paramType = paramTypes[i]; var val = converter.Convert(arguments[i], paramType, out origType); if (val.ErrorMessage is not null) { return(DbgDotNetValueResult.CreateError(val.ErrorMessage)); } var valType = origType ?? new ReflectionTypeCreator(this, method.AppDomain).Create(val.CorValue !.ExactType); args[w++] = BoxIfNeeded(dnEval, appDomain, createdValues, val.CorValue !, paramType, valType); } if (args.Length != w) { throw new InvalidOperationException(); } // Derefence/unbox the values here now that they can't get neutered for (int i = 0; i < args.Length; i++) { DmdType argType; if (!hiddenThisArg) { argType = paramTypes[i]; } else if (i == 0) { argType = declType; } else { argType = paramTypes[i - 1]; } CorValue?arg = args[i]; if (argType.IsValueType || argType.IsPointer || argType.IsFunctionPointer) { if (arg.IsReference) { if (arg.IsNull) { throw new InvalidOperationException(); } arg = arg.GetDereferencedValue(out hr); if (arg is null) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.GetErrorMessage(hr))); } } if (arg.IsBox) { arg = arg.GetBoxedValue(out hr); if (arg is null) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.GetErrorMessage(hr))); } } args[i] = arg; } } var res = newObj ? dnEval.CallConstructor(func, typeArgs, args, out hr) : dnEval.Call(func, typeArgs, args, out hr); if (res is null) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.GetErrorMessage(hr))); } if (res.Value.WasCustomNotification) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.FuncEvalRequiresAllThreadsToRun)); } if (res.Value.WasCancelled) { return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut)); } if (res.Value.WasException) { return(DbgDotNetValueResult.CreateException(CreateDotNetValue_CorDebug(res.Value.ResultOrException !, reflectionAppDomain, tryCreateStrongHandle: true))); } return(DbgDotNetValueResult.Create(CreateDotNetValue_CorDebug(res.Value.ResultOrException !, reflectionAppDomain, tryCreateStrongHandle: true))); } } catch (TimeoutException) { return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut)); } catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex)) { return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError)); } finally { foreach (var value in createdValues) { dnDebugger.DisposeHandle(value); } } }