Beispiel #1
0
        internal string StoreValue_CorDebug(DbgEvaluationContext context, DbgThread thread, ILDbgEngineStackFrame ilFrame, Func <CreateCorValueResult> createTargetValue, DmdType targetType, object sourceValue, CancellationToken cancellationToken)
        {
            debuggerThread.VerifyAccess();
            cancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(context);

            if (tmp != null)
            {
                return(tmp.Value.ErrorMessage ?? throw new InvalidOperationException());
            }

            var dnThread      = GetThread(thread);
            var createdValues = new List <CorValue>();
            CreateCorValueResult createResult = default;

            try {
                var appDomain           = ilFrame.GetCorAppDomain();
                var reflectionAppDomain = thread.AppDomain.GetReflectionAppDomain() ?? throw new InvalidOperationException();
                using (var dnEval = dnDebugger.CreateEval(cancellationToken, suspendOtherThreads: (context.Options & DbgEvaluationContextOptions.RunAllThreads) == 0)) {
                    dnEval.SetThread(dnThread);
                    dnEval.SetTimeout(context.FuncEvalTimeout);
                    dnEval.EvalEvent += (s, e) => DnEval_EvalEvent(dnEval, context);

                    var converter = new EvalArgumentConverter(this, dnEval, appDomain, reflectionAppDomain, createdValues);

                    var evalRes = converter.Convert(sourceValue, targetType, out var newValueType);
                    if (evalRes.ErrorMessage != null)
                    {
                        return(evalRes.ErrorMessage);
                    }

                    var sourceCorValue = evalRes.CorValue;
                    var sourceType     = new ReflectionTypeCreator(this, reflectionAppDomain).Create(sourceCorValue.ExactType);

                    createResult = createTargetValue();
                    if (createResult.Value == null)
                    {
                        return(CordbgErrorHelper.GetErrorMessage(createResult.HResult));
                    }
                    return(StoreValue_CorDegbug(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);
                }
            }
        }
        string?StoreSimpleValue_CorDebug(DbgEvaluationInfo evalInfo, ILDbgEngineStackFrame ilFrame, Func <CreateCorValueResult> createTargetValue, DmdType targetType, object?sourceValue)
        {
            Debug.Assert(RequiresNoFuncEvalToStoreValue(targetType, sourceValue));
            evalInfo.CancellationToken.ThrowIfCancellationRequested();
            CreateCorValueResult createResult = default;

            try {
                var dnThread = GetThread(evalInfo.Frame.Thread);
                createResult = createTargetValue();
                if (createResult.Value is null)
                {
                    return(CordbgErrorHelper.GetErrorMessage(createResult.HResult));
                }
                return(StoreSimpleValue_CorDebug(dnThread, createResult.Value, targetType, sourceValue));
            }
            catch (TimeoutException) {
                return(PredefinedEvaluationErrorMessages.FuncEvalTimedOut);
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(CordbgErrorHelper.InternalError);
            }
            finally {
                if (createResult.CanDispose)
                {
                    dnDebugger.DisposeHandle(createResult.Value);
                }
            }
        }
        internal DbgDotNetValueResult CreateValue_CorDebug(DbgEvaluationInfo evalInfo, ILDbgEngineStackFrame ilFrame, object?value)
        {
            debuggerThread.VerifyAccess();
            evalInfo.CancellationToken.ThrowIfCancellationRequested();
            if (value is DbgDotNetValueImpl)
            {
                return(DbgDotNetValueResult.Create((DbgDotNetValueImpl)value));
            }
            var tmp = CheckFuncEval(evalInfo);

            if (!(tmp is null))
            {
                return(tmp.Value);
            }

            var      dnThread        = GetThread(evalInfo.Frame.Thread);
            var      createdValues   = new List <CorValue>();
            CorValue?createdCorValue = null;

            try {
                var appDomain           = ilFrame.GetCorAppDomain();
                var reflectionAppDomain = ilFrame.GetReflectionModule().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(value, reflectionAppDomain.System_Object, out var newValueType);
                    if (!(evalRes.ErrorMessage is null))
                    {
                        return(DbgDotNetValueResult.CreateError(evalRes.ErrorMessage));
                    }

                    var resultValue = CreateDotNetValue_CorDebug(evalRes.CorValue !, reflectionAppDomain, tryCreateStrongHandle: true);
                    createdCorValue = evalRes.CorValue;
                    return(DbgDotNetValueResult.Create(resultValue));
                }
            }
            catch (TimeoutException) {
                return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError));
            }
            finally {
                foreach (var v in createdValues)
                {
                    if (!v.Equals(createdCorValue))
                    {
                        dnDebugger.DisposeHandle(v);
                    }
                }
            }
        }
Beispiel #4
0
        internal DbgDotNetCreateValueResult CreateValue_CorDebug(DbgEvaluationContext context, DbgThread thread, ILDbgEngineStackFrame ilFrame, object value, CancellationToken cancellationToken)
        {
            debuggerThread.VerifyAccess();
            cancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(context);

            if (tmp != null)
            {
                return(new DbgDotNetCreateValueResult(tmp.Value.ErrorMessage ?? throw new InvalidOperationException()));
            }

            var      dnThread        = GetThread(thread);
            var      createdValues   = new List <CorValue>();
            CorValue createdCorValue = null;

            try {
                var appDomain           = ilFrame.GetCorAppDomain();
                var reflectionAppDomain = thread.AppDomain.GetReflectionAppDomain() ?? throw new InvalidOperationException();
                using (var dnEval = dnDebugger.CreateEval(cancellationToken, suspendOtherThreads: (context.Options & DbgEvaluationContextOptions.RunAllThreads) == 0)) {
                    dnEval.SetThread(dnThread);
                    dnEval.SetTimeout(context.FuncEvalTimeout);
                    dnEval.EvalEvent += (s, e) => DnEval_EvalEvent(dnEval, context);

                    var converter = new EvalArgumentConverter(this, dnEval, appDomain, reflectionAppDomain, createdValues);
                    var evalRes   = converter.Convert(value, reflectionAppDomain.System_Object, out var newValueType);
                    if (evalRes.ErrorMessage != null)
                    {
                        return(new DbgDotNetCreateValueResult(evalRes.ErrorMessage));
                    }

                    var resultValue = CreateDotNetValue_CorDebug(evalRes.CorValue, reflectionAppDomain, tryCreateStrongHandle: true);
                    createdCorValue = evalRes.CorValue;
                    return(new DbgDotNetCreateValueResult(resultValue));
                }
            }
            catch (TimeoutException) {
                return(new DbgDotNetCreateValueResult(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(new DbgDotNetCreateValueResult(CordbgErrorHelper.InternalError));
            }
            finally {
                foreach (var v in createdValues)
                {
                    if (createdCorValue != v)
                    {
                        dnDebugger.DisposeHandle(v);
                    }
                }
            }
        }
        // This method calls ICorDebugEval2.NewParameterizedArray() which doesn't support creating SZ arrays
        // with any element type. See the caller of this method (CreateSZArrayCore) for more info.
        internal DbgDotNetValueResult CreateSZArray_CorDebug(DbgEvaluationInfo evalInfo, CorAppDomain appDomain, DmdType elementType, int length)
        {
            debuggerThread.VerifyAccess();
            evalInfo.CancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(evalInfo);

            if (!(tmp is null))
            {
                return(tmp.Value);
            }

            var dnThread = GetThread(evalInfo.Frame.Thread);

            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 corType = GetType(appDomain, elementType);
                    var res     = dnEval.CreateSZArray(corType, length, out int hr);
                    if (res is null)
                    {
                        return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.GetErrorMessage(hr)));
                    }
                    Debug.Assert(!res.Value.WasException, "Shouldn't throw " + nameof(ArgumentOutOfRangeException));
                    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 !, elementType.AppDomain, tryCreateStrongHandle: true)));
                    }
                    return(DbgDotNetValueResult.Create(CreateDotNetValue_CorDebug(res.Value.ResultOrException !, elementType.AppDomain, tryCreateStrongHandle: true)));
                }
            }
            catch (TimeoutException) {
                return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError));
            }
        }
Beispiel #6
0
        internal DbgDotNetValueResult FuncEvalCreateInstanceNoCtor_CorDebug(DbgEvaluationInfo evalInfo, CorAppDomain appDomain, DmdType typeToCreate)
        {
            debuggerThread.VerifyAccess();
            evalInfo.CancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(evalInfo);

            if (tmp != null)
            {
                return(tmp.Value);
            }

            var dnThread = GetThread(evalInfo.Frame.Thread);

            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 corType = GetType(appDomain, typeToCreate);
                    var res     = dnEval.CreateDontCallConstructor(corType, out int hr);
                    if (res == 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, typeToCreate.AppDomain, tryCreateStrongHandle: true)));
                    }
                    return(DbgDotNetValueResult.Create(CreateDotNetValue_CorDebug(res.Value.ResultOrException, typeToCreate.AppDomain, tryCreateStrongHandle: true)));
                }
            }
            catch (TimeoutException) {
                return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError));
            }
        }
        internal DbgDotNetValueResult Box_CorDebug(DbgEvaluationInfo evalInfo, CorAppDomain appDomain, CorValue value, DmdType type)
        {
            debuggerThread.VerifyAccess();
            evalInfo.CancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(evalInfo);

            if (!(tmp is null))
            {
                return(tmp.Value);
            }

            var      dnThread      = GetThread(evalInfo.Frame.Thread);
            var      createdValues = new List <CorValue>();
            CorValue?boxedValue    = null;

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

                    boxedValue = BoxIfNeeded(dnEval, appDomain, createdValues, value, type.AppDomain.System_Object, type);
                    if (boxedValue is null)
                    {
                        return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.GetErrorMessage(-1)));
                    }
                    return(DbgDotNetValueResult.Create(CreateDotNetValue_CorDebug(boxedValue, type.AppDomain, tryCreateStrongHandle: true)));
                }
            }
            catch (TimeoutException) {
                return(DbgDotNetValueResult.CreateError(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(DbgDotNetValueResult.CreateError(CordbgErrorHelper.InternalError));
            }
            finally {
                foreach (var v in createdValues)
                {
                    if (!v.Equals(boxedValue))
                    {
                        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 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 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 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);
                }
            }
        }