Exemple #1
0
		internal DnAppDomain(DnProcess ownerProcess, ICorDebugAppDomain appDomain, int incrementedId) {
			this.ownerProcess = ownerProcess;
			this.assemblies = new DebuggerCollection<ICorDebugAssembly, DnAssembly>(CreateAssembly);
			this.appDomain = new CorAppDomain(appDomain);
			this.incrementedId = incrementedId;
			NameChanged();
		}
Exemple #2
0
		internal DnAppDomain(DnProcess ownerProcess, ICorDebugAppDomain appDomain, int uniqueId, int uniqueIdProcess) {
			Process = ownerProcess;
			assemblies = new DebuggerCollection<ICorDebugAssembly, DnAssembly>(CreateAssembly);
			CorAppDomain = new CorAppDomain(appDomain);
			UniqueId = uniqueId;
			UniqueIdProcess = uniqueIdProcess;
			NameChanged();
		}
 CorType GetType(CorAppDomain appDomain, DmdType type) => CorDebugTypeCreator.GetType(this, appDomain, type);
Exemple #4
0
        // 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(DbgEvaluationContext context, DbgThread thread, CorAppDomain appDomain, DmdType elementType, int length, CancellationToken cancellationToken)
        {
            debuggerThread.VerifyAccess();
            cancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(context);

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

            var dnThread = GetThread(thread);

            try {
                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 corType = GetType(appDomain, elementType);
                    var res     = dnEval.CreateSZArray(corType, length, out int hr);
                    if (res == null)
                    {
                        return(new DbgDotNetValueResult(CordbgErrorHelper.GetErrorMessage(hr)));
                    }
                    Debug.Assert(!res.Value.WasException, "Shouldn't throw " + nameof(ArgumentOutOfRangeException));
                    if (res.Value.WasCustomNotification)
                    {
                        return(new DbgDotNetValueResult(CordbgErrorHelper.FuncEvalRequiresAllThreadsToRun));
                    }
                    if (res.Value.WasCancelled)
                    {
                        return(new DbgDotNetValueResult(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
                    }
                    return(new DbgDotNetValueResult(CreateDotNetValue_CorDebug(res.Value.ResultOrException, elementType.AppDomain, tryCreateStrongHandle: true), valueIsException: res.Value.WasException));
                }
            }
            catch (TimeoutException) {
                return(new DbgDotNetValueResult(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(new DbgDotNetValueResult(CordbgErrorHelper.InternalError));
            }
        }
Exemple #5
0
 public EntryPointBreakpointPauseState(CorAppDomain corAppDomain, CorThread corThread)
     : base(DebuggerPauseReason.EntryPointBreakpoint)
 {
     CorAppDomain = corAppDomain;
     CorThread    = corThread;
 }
Exemple #6
0
 public BreakPauseState(CorAppDomain corAppDomain, CorThread corThread)
     : base(DebuggerPauseReason.Break)
 {
     CorAppDomain = corAppDomain;
     CorThread    = corThread;
 }
Exemple #7
0
 internal void Unregister(CorAppDomain appDomain)
 {
     Debug.Assert(m_items.ContainsKey(appDomain));
     m_items.Remove(appDomain);
 }
Exemple #8
0
 public CorExceptionEventArgs(CorAppDomain appdomain, CorException exception, int unhandled)
     : base(appdomain, "Exception")
 {
     Exception = exception;
     Unhandled = unhandled;
 }
        string?StoreValue_CorDebug(DnEval dnEval, List <CorValue> createdValues, CorAppDomain appDomain, DnThread dnThread, CorValue targetValue, DmdType targetType, CorValue sourceValue, DmdType sourceType)
        {
            if (targetType.IsByRef)
            {
                return(CordbgErrorHelper.InternalError);
            }
            int hr;

            if (!targetType.IsValueType)
            {
                if (!targetValue.IsReference)
                {
                    return(CordbgErrorHelper.InternalError);
                }
                if (!sourceValue.IsReference)
                {
                    var boxedSourceValue = BoxIfNeeded(dnEval, appDomain, createdValues, sourceValue, targetType, sourceType);
                    if (!boxedSourceValue.IsReference)
                    {
                        return(CordbgErrorHelper.InternalError);
                    }
                    sourceValue = boxedSourceValue;
                }
                if (!sourceValue.IsNull && sourceType.IsValueType)
                {
                    var sourceDerefVal = sourceValue.GetDereferencedValue(out hr);
                    if (sourceDerefVal is null)
                    {
                        return(CordbgErrorHelper.GetErrorMessage(hr));
                    }
                    if (!sourceDerefVal.IsBox)
                    {
                        return(CordbgErrorHelper.InternalError);
                    }
                }
                hr = targetValue.SetReferenceAddress(sourceValue.ReferenceAddress);
                if (hr != 0)
                {
                    return(CordbgErrorHelper.GetErrorMessage(hr));
                }
                return(null);
            }
            else
            {
                if (!sourceType.IsValueType)
                {
                    return(CordbgErrorHelper.InternalError);
                }

                if (targetValue.IsReference)
                {
                    if (!(targetValue.GetDereferencedValue(out hr) is CorValue derefValue))
                    {
                        return(CordbgErrorHelper.GetErrorMessage(hr));
                    }
                    targetValue = derefValue;
                }
                if (targetValue.IsBox)
                {
                    return(CordbgErrorHelper.InternalError);
                }

                if (sourceValue.IsReference)
                {
                    if (!(sourceValue.GetDereferencedValue(out hr) is CorValue derefValue))
                    {
                        return(CordbgErrorHelper.GetErrorMessage(hr));
                    }
                    sourceValue = derefValue;
                }
                if (sourceValue.IsBox)
                {
                    if (!(sourceValue.GetBoxedValue(out hr) is CorValue unboxedValue))
                    {
                        return(CordbgErrorHelper.GetErrorMessage(hr));
                    }
                    sourceValue = unboxedValue;
                }

                if (!targetValue.IsGeneric || !sourceValue.IsGeneric)
                {
                    return(CordbgErrorHelper.InternalError);
                }
                if (targetValue.Size != sourceValue.Size)
                {
                    return(CordbgErrorHelper.InternalError);
                }
                hr = targetValue.WriteGenericValue(sourceValue.ReadGenericValue(), dnThread.Process.CorProcess);
                if (hr < 0)
                {
                    return(CordbgErrorHelper.GetErrorMessage(hr));
                }
                return(null);
            }
        }
        internal DbgDotNetValueResult Box_CorDebug(DbgEvaluationContext context, DbgThread thread, CorAppDomain appDomain, CorValue value, DmdType type, CancellationToken cancellationToken)
        {
            debuggerThread.VerifyAccess();
            cancellationToken.ThrowIfCancellationRequested();
            var tmp = CheckFuncEval(context);

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

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

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

                    boxedValue = BoxIfNeeded(dnEval, appDomain, createdValues, value, type.AppDomain.System_Object, type);
                    if (boxedValue == null)
                    {
                        return(new DbgDotNetValueResult(CordbgErrorHelper.GetErrorMessage(-1)));
                    }
                    return(new DbgDotNetValueResult(CreateDotNetValue_CorDebug(boxedValue, type.AppDomain, tryCreateStrongHandle: true), valueIsException: false));
                }
            }
            catch (TimeoutException) {
                return(new DbgDotNetValueResult(PredefinedEvaluationErrorMessages.FuncEvalTimedOut));
            }
            catch (Exception ex) when(ExceptionUtils.IsInternalDebuggerError(ex))
            {
                return(new DbgDotNetValueResult(CordbgErrorHelper.InternalError));
            }
            finally {
                foreach (var v in createdValues)
                {
                    if (boxedValue != v)
                    {
                        dnDebugger.DisposeHandle(v);
                    }
                }
            }
        }
Exemple #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CorBreakpointEventArgs"/> class.
 /// </summary>
 /// <param name="appdomain">The controller.</param>
 /// <param name="thread">The thread.</param>
 /// <param name="breakpoint">The breakpoint.</param>
 public CorBreakpointEventArgs(CorAppDomain appdomain, CorThread thread, CorBreakpoint breakpoint)
     : base(appdomain)
 {
     this.p_thread     = thread;
     this.p_breakpoint = breakpoint;
 }
Exemple #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CorBreakpointEventArgs"/> class.
 /// </summary>
 /// <param name="appdomain">The controller.</param>
 /// <param name="thread">The thread.</param>
 /// <param name="breakpoint">The breakpoint.</param>
 public CorExceptionEventArgs(CorAppDomain appdomain, CorThread thread, int unHandled)
     : base(appdomain)
 {
     this.thread    = thread;
     this.unHandled = unHandled;
 }
Exemple #13
0
 public UnhandledExceptionThrownStopReason(CorAppDomain appDomain, CorThread thread, CorFrame frame,
                                           int offset, CorDebugExceptionCallbackType eventType, int flags)
     : base(appDomain, thread, frame, offset, eventType, flags)
 {
 }
Exemple #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CorBreakpointEventArgs"/> class.
 /// </summary>
 /// <param name="appdomain">The controller.</param>
 /// <param name="thread">The thread.</param>
 /// <param name="breakpoint">The breakpoint.</param>
 public CorBreakpointEventArgs(CorAppDomain appdomain, CorThread thread, CorBreakpoint breakpoint)
     : base(appdomain, "Breakpoint")
 {
     Thread     = thread;
     Breakpoint = breakpoint;
 }
        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);
                }
            }
        }
        internal DbgDotNetValueResult FuncEvalCreateInstanceNoCtor_CorDebug(DbgEvaluationInfo evalInfo, CorAppDomain appDomain, DmdType typeToCreate)
        {
            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, typeToCreate);
                    var res     = dnEval.CreateDontCallConstructor(corType, out int 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 !, 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));
            }
        }
Exemple #17
0
 CorDebugTypeCreator(DbgEngineImpl engine, CorAppDomain appDomain)
 {
     this.engine      = engine;
     this.appDomain   = appDomain;
     recursionCounter = 0;
 }
Exemple #18
0
        public DnModule TryGetModule(CorAppDomain appDomain, CorClass cls)
        {
            if (appDomain == null || cls == null)
                return null;
            var clsMod = cls.Module;
            if (clsMod == null)
                return null;
            var ad = TryGetAppDomain(appDomain.RawObject);
            if (ad == null)
                return null;

            var asm = TryGetValidAssembly(appDomain.RawObject, clsMod.RawObject);
            if (asm == null)
                return null;
            return asm.TryGetModule(clsMod.RawObject);
        }
Exemple #19
0
 /// <summary>
 /// Locates MDbgAppDomain object from CorAppDomain object.
 /// </summary>
 /// <param name="appDomain">appDomain object from CorXXX layer.</param>
 /// <returns>MdbgAppDomain object</returns>
 public MDbgAppDomain Lookup(CorAppDomain appDomain)
 {
     return((MDbgAppDomain)m_items[appDomain]);
 }