Example #1
0
 void SetActiveThread(CorThread t)
 {
     activeThread = t;
     stepper      = activeThread.CreateStepper();
     stepper.SetUnmappedStopMask(CorDebugUnmappedStop.STOP_NONE);
     stepper.SetJMC(true);
 }
        internal static IEnumerable <CorFrame> GetFrames(CorThread thread)
        {
            var corFrames = new List <CorFrame> ();

            try {
                foreach (CorChain chain in thread.Chains)
                {
                    if (!chain.IsManaged)
                    {
                        continue;
                    }
                    try {
                        var chainFrames = chain.Frames;

                        foreach (CorFrame frame in chainFrames)
                        {
                            corFrames.Add(frame);
                        }
                    }
                    catch (COMException e) {
                        DebuggerLoggingService.LogMessage("Failed to enumerate frames of chain: {0}", e.Message);
                    }
                }
            }
            catch (COMException e) {
                DebuggerLoggingService.LogMessage("Failed to enumerate chains of thread: {0}", e.Message);
            }
            return(corFrames);
        }
Example #3
0
        internal ICorDebugFrame GetFrameAt(FrameID frameID)
        {
            process.AssertPaused();

            ICorDebugChainEnum corChainEnum = CorThread.EnumerateChains();

            if (frameID.ChainIndex >= corChainEnum.Count)
            {
                throw new ArgumentException("Chain index too big", "chainIndex");
            }
            corChainEnum.Skip(corChainEnum.Count - frameID.ChainIndex - 1);

            ICorDebugChain corChain = corChainEnum.Next();

            if (corChain.IsManaged == 0)
            {
                throw new ArgumentException("Chain is not managed", "chainIndex");
            }

            ICorDebugFrameEnum corFrameEnum = corChain.EnumerateFrames();

            if (frameID.FrameIndex >= corFrameEnum.Count)
            {
                throw new ArgumentException("Frame index too big", "frameIndex");
            }
            corFrameEnum.Skip(corFrameEnum.Count - frameID.FrameIndex - 1);

            return(corFrameEnum.Next());
        }
Example #4
0
        string EvaluateTrace(CorThread thread, string exp)
        {
            StringBuilder sb   = new StringBuilder();
            int           last = 0;
            int           i    = exp.IndexOf('{');

            while (i != -1)
            {
                if (i < exp.Length - 1 && exp [i + 1] == '{')
                {
                    sb.Append(exp.Substring(last, i - last + 1));
                    last = i + 2;
                    i    = exp.IndexOf('{', i + 2);
                    continue;
                }
                int j = exp.IndexOf('}', i + 1);
                if (j == -1)
                {
                    break;
                }
                string se = exp.Substring(i + 1, j - i - 1);
                se = EvaluateExpression(thread, se);
                sb.Append(exp.Substring(last, i - last));
                sb.Append(se);
                last = j + 1;
                i    = exp.IndexOf('{', last);
            }
            sb.Append(exp.Substring(last, exp.Length - last));
            return(sb.ToString());
        }
Example #5
0
        ThreadInfo GetThread(CorThread thread)
        {
            ThreadInfo info;

            lock (threads) {
                if (!threads.TryGetValue(thread.Id, out info))
                {
                    string loc = string.Empty;
                    try {
                        if (thread.ActiveFrame != null)
                        {
                            StackFrame frame = CorBacktrace.CreateFrame(this, thread.ActiveFrame);
                            loc = frame.ToString();
                        }
                        else
                        {
                            loc = "<Unknown>";
                        }
                    }
                    catch {
                        loc = "<Unknown>";
                    }

                    info = new ThreadInfo(thread.Process.Id, thread.Id, GetThreadName(thread), loc);
                    threads[thread.Id] = info;
                }
                return(info);
            }
        }
Example #6
0
 public NativeCodeBreakpointPauseState(DnNativeCodeBreakpoint bp, CorAppDomain corAppDomain, CorThread corThread)
     : base(DebuggerPauseReason.NativeCodeBreakpoint)
 {
     Breakpoint   = bp;
     CorAppDomain = corAppDomain;
     CorThread    = corThread;
 }
Example #7
0
 public ExceptionUnwindStopReason(CorAppDomain appDomain, CorThread thread,
                                  CorDebugExceptionUnwindCallbackType eventType, int flags)
 {
     m_appDomain = appDomain;
     m_thread    = thread;
     m_eventtype = eventType;
     m_flags     = flags;
 }
Example #8
0
 DbgModule TryGetModule(CorFrame frame, CorThread thread)
 {
     if (frame == null)
     {
         frame = thread?.ActiveFrame ?? thread?.AllFrames.FirstOrDefault();
     }
     return(TryGetModule(frame?.Function?.Module));
 }
Example #9
0
 public CorBacktrace(CorThread thread, CorDebuggerSession session) : base(session.ObjectAdapter)
 {
     this.session  = session;
     this.thread   = thread;
     threadId      = thread.Id;
     frames        = new List <CorFrame> (GetFrames(thread));
     evalTimestamp = CorDebuggerSession.EvaluationTimestamp;
 }
Example #10
0
 internal void Register(CorThread t)
 {
     // Prevent double-registration. This may happen if we pick up a CorThread
     // via enumeration before the CreateThread callback.
     if (!m_items.Contains(t.Id))
     {
         m_items.Add(t.Id, new MDbgThread(this, t, m_freeThreadNumber++));
     }
 }
Example #11
0
 internal void UnRegister(CorThread t)
 {
     m_items.Remove(t.Id);
     if (m_active != null &&
         m_active.CorThread.Id == t.Id)
     {
         m_active = null;
     }
 }
Example #12
0
 void CheckTimestamp( )
 {
     if (evalTimestamp != CorDebuggerSession.EvaluationTimestamp)
     {
         thread  = null;
         frame   = null;
         corEval = null;
     }
 }
Example #13
0
 public ExceptionThrownStopReason(CorAppDomain appDomain, CorThread thread, CorFrame frame,
                                  int offset, CorDebugExceptionCallbackType eventType, int flags)
 {
     m_appDomain = appDomain;
     m_thread    = thread;
     m_frame     = frame;
     m_offset    = offset;
     m_eventtype = eventType;
     m_flags     = flags;
 }
Example #14
0
        internal DbgThread TryGetThread(CorThread thread)
        {
            if (thread == null)
            {
                return(null);
            }
            var dnThread = dnDebugger.Processes.FirstOrDefault()?.Threads.FirstOrDefault(a => a.CorThread == thread);

            return(TryGetThread(dnThread));
        }
Example #15
0
        static void DisplayCallstack(CorThread thread)
        {
            Console.WriteLine("Display callstack:");
            List <CorFrame> frameList = thread.GetFrameList();

            foreach (var frame in frameList)
            {
                var source = frame.GetSourcePosition();
                DisplayCurrentSourceCode(source);
            }
        }
Example #16
0
 /// <summary>
 /// Create a new instance of the FunctionRemapCompleteStopReason class.
 /// </summary>
 /// <param name="appDomain">The appDomain where remapping is occuring.</param>
 /// <param name="thread">The thread on which the remapping is occuring.</param>
 /// <param name="managedFunction">The version of function the debugger remapped to.</param>
 public FunctionRemapCompleteStopReason(CorAppDomain appDomain,
                                        CorThread thread,
                                        CorFunction managedFunction)
 {
     Debug.Assert(appDomain != null);
     Debug.Assert(thread != null);
     Debug.Assert(managedFunction != null);
     m_appDomain = appDomain;
     m_thread    = thread;
     m_function  = managedFunction;
 }
Example #17
0
 internal void SetActiveThread(CorThread thread)
 {
     if (thread == null)
     {
         m_active = null;
     }
     else
     {
         m_active = GetThreadFromThreadId(thread.Id);
     }
     InvalidateAllStacks();
 }
Example #18
0
 protected override void OnSetActiveThread(long processId, long threadId)
 {
     activeThread = null;
     stepper      = null;
     foreach (CorThread t in process.Threads)
     {
         if (t.Id == threadId)
         {
             SetActiveThread(t);
             break;
         }
     }
 }
Example #19
0
 bool OnBreakpointHit(CorThread thread)
 {
     if (this.thread == null || engine.TryGetThread(thread) == this.thread)
     {
         var currentThread = engine.TryGetThread(thread) ?? throw new InvalidOperationException();
         var e             = new DbgDotNetStepperBreakpointEventArgs(currentThread);
         Hit?.Invoke(this, e);
         return(e.Pause);
     }
     else
     {
         return(false);
     }
 }
Example #20
0
 internal static IEnumerable <CorFrame> GetFrames(CorThread thread)
 {
     foreach (CorChain chain in thread.Chains)
     {
         if (!chain.IsManaged)
         {
             continue;
         }
         foreach (CorFrame frame in chain.Frames)
         {
             yield return(frame);
         }
     }
 }
Example #21
0
 internal void SetActiveThread(CorThread thread)
 {
     if (thread == null)
     {
         m_active = null;
     }
     else
     {
         m_active = GetThreadFromThreadId(thread.Id);
     }
     lock (this.m_process)
     {
         m_frameFactory.InvalidateStackWalkers();                 // @TODO can this line be removed???
     }
 }
Example #22
0
 public RemapOpportunityReachedStopReason(CorAppDomain appDomain,
                                          CorThread thread,
                                          CorFunction oldFunction,
                                          CorFunction newFunction,
                                          int oldILOffset)
 {
     Debug.Assert(appDomain != null);
     Debug.Assert(thread != null);
     Debug.Assert(oldFunction != null);
     Debug.Assert(newFunction != null);
     m_appDomain   = appDomain;
     m_thread      = thread;
     m_oldFunction = oldFunction;
     m_newFunction = newFunction;
     m_oldILOffset = oldILOffset;
 }
Example #23
0
        public override void Dispose( )
        {
            TerminateDebugger();

            base.Dispose();

            // There is no explicit way of disposing the metadata objects, so we have
            // to rely on the GC to do it.

            modules      = null;
            documents    = null;
            threads      = null;
            processes    = null;
            activeThread = null;
            GC.Collect();
        }
Example #24
0
        public DnEval CreateEval(CorThread thread)
        {
            Debug.WriteLineIf(ProcessState != DebuggerProcessState.Paused, dnSpy_Debugger_Resources.Error_CantEvalUnlessDebuggerStopped);
            if (ProcessState != DebuggerProcessState.Paused)
            {
                throw new EvalException(-1, dnSpy_Debugger_Resources.Error_CantEvalUnlessDebuggerStopped);
            }
            if (unhandledException)
            {
                throw new EvalException(-1, dnSpy_Debugger_Resources.Error_CantEvalWhenUnhandledExceptionHasOccurred);
            }
            var eval = Debugger.CreateEval();

            eval.EvalEvent += (s, e) => DnEval_EvalEvent(s, e, eval);
            eval.SetThread(thread);
            return(eval);
        }
Example #25
0
        /// <summary>
        /// Gets the MDbgThread with the given ThreadId.
        /// </summary>
        /// <param name="threadId">The ThreadId to look up.</param>
        /// <returns>The MDbgThread.</returns>
        public MDbgThread GetThreadFromThreadId(int threadId)
        {
            // This sometimes fails because we're looking for a thread don't recognize.
            // Need to offer lazy create semantics here.
            MDbgThread te = (MDbgThread)m_items[threadId];

            if (te == null)
            {
                CorThread t = m_process.CorProcess.GetThread(threadId);
                if (t != null)
                {
                    Register(t);
                    te = (MDbgThread)m_items[threadId];
                }
            }
            return(te);
        }
Example #26
0
        protected override void OnStop( )
        {
            process.Stop(0);
            OnStopped();
            CorThread currentThread = null;

            foreach (CorThread t in process.Threads)
            {
                currentThread = t;
                break;
            }
            TargetEventArgs args = new TargetEventArgs(TargetEventType.TargetStopped);

            args.Process   = GetProcess(process);
            args.Thread    = GetThread(currentThread);
            args.Backtrace = new Backtrace(new CorBacktrace(currentThread, this));
            OnTargetEvent(args);
        }
Example #27
0
 string EvaluateExpression(CorThread thread, string exp)
 {
     try {
         if (thread.ActiveFrame == null)
         {
             return(string.Empty);
         }
         EvaluationOptions ops = Options.EvaluationOptions;
         ops.AllowTargetInvoke = true;
         CorEvaluationContext ctx = new CorEvaluationContext(this, new CorBacktrace(thread, this), 0, ops);
         ctx.Thread = thread;
         ValueReference val = ctx.Evaluator.Evaluate(ctx, exp);
         return(val.CreateObjectValue(false).Value);
     } catch (Exception ex) {
         OnDebuggerOutput(true, ex.ToString());
         return(string.Empty);
     }
 }
Example #28
0
        string GetThreadName(CorThread thread)
        {
            return(string.Empty);

/*			CorValue to = thread.ThreadVariable;
 *                      if (to == null)
 *                              return string.Empty;
 *
 *                      CorEvaluationContext ctx = new CorEvaluationContext (this);
 *                      ctx.Thread = thread;
 *                      ctx.Frame = thread.ActiveFrame;
 *
 *                      LiteralValueReference val = new LiteralValueReference (ctx, "", new CorValRef (to));
 *                      ValueReference prop = val.GetChild ("Name");
 *                      if (prop != null)
 *                              return prop.ObjectValue as string;
 *                      else
 *                              return string.Empty;*/
        }
Example #29
0
        //constructors
        /// <summary>
        /// Initialze the StackTrace Class and create all the FrameInfo objects
        /// </summary>
        /// <param name="proc">The thread to get the stack trace of</param>
        internal ThreadInfo(CorThread thread, ProcessInfo procInfo)
        {
            if (thread == null)
            {
                throw new ArgumentNullException("thread");
            }
            if (procInfo == null)
            {
                throw new ArgumentNullException("procInfo");
            }

            this.thread    = thread;
            metaImportHash = new Dictionary <string, CorMetadataImport>();
            frameStack     = new List <FrameInfo>();
            threadId       = thread.Id;
            processInfo    = procInfo;
            //get the general thread information object
            generalThreadInfo = procInfo.GeneralThreads[threadId];
        }
Example #30
0
 public bool InterceptCurrentException()
 {
     if (!CorThread.Is <ICorDebugThread2>())
     {
         return(false);                                               // Is the debuggee .NET 2.0?
     }
     if (LastFunction == null)
     {
         return(false);                                  // Is frame available?  It is not at StackOverflow
     }
     try {
         CorThread.CastTo <ICorDebugThread2>().InterceptCurrentException(LastFunction.CorILFrame.CastTo <ICorDebugFrame>());
         return(true);
     } catch (COMException e) {
         // 0x80131C02: Cannot intercept this exception
         if ((uint)e.ErrorCode == 0x80131C02)
         {
             return(false);
         }
         throw;
     }
 }
Example #31
0
 /// <summary>
 /// Creates an eval. Don't call this if <see cref="EvalDisabled"/> is true
 /// </summary>
 /// <param name="thread">Thread to use</param>
 /// <returns></returns>
 public DnEval CreateEval(CorThread thread)
 {
     Debug.WriteLineIf(ProcessState != DebuggerProcessState.Stopped, "Can't evaluate unless debugger is stopped");
     if (ProcessState != DebuggerProcessState.Stopped)
         throw new EvalException(-1, "Can't evaluate unless debugger is stopped");
     if (UnhandledException_counter != 0)
         throw new EvalException(-1, "Can't evaluate when an unhandled exception has occurred");
     var eval = Debugger.CreateEval();
     eval.EvalEvent += (s, e) => DnEval_EvalEvent(s, e, eval);
     eval.SetThread(thread);
     return eval;
 }
Example #32
0
		public DnEval CreateEval(CorThread thread) {
			Debug.WriteLineIf(ProcessState != DebuggerProcessState.Stopped, dnSpy_Debugger_Resources.Error_CantEvalUnlessDebuggerStopped);
			if (ProcessState != DebuggerProcessState.Stopped)
				throw new EvalException(-1, dnSpy_Debugger_Resources.Error_CantEvalUnlessDebuggerStopped);
			if (unhandledException)
				throw new EvalException(-1, dnSpy_Debugger_Resources.Error_CantEvalWhenUnhandledExceptionHasOccurred);
			var eval = Debugger.CreateEval();
			eval.EvalEvent += (s, e) => DnEval_EvalEvent(s, e, eval);
			eval.SetThread(thread);
			return eval;
		}
Example #33
0
            static List<ThreadInfo> GetThreadInfos(CorThread thread)
            {
                var process = thread.Process;
                var list = new List<ThreadInfo>();
                if (process == null) {
                    list.Add(new ThreadInfo(thread));
                    return list;
                }

                foreach (var t in process.Threads)
                    list.Add(new ThreadInfo(t));

                return list;
            }
Example #34
0
 public ThreadInfos(CorThread thread, bool suspendOtherThreads)
 {
     this.thread = thread;
     this.list = GetThreadInfos(thread);
     this.suspendOtherThreads = suspendOtherThreads;
 }
Example #35
0
 public ThreadInfo(CorThread thread)
 {
     this.Thread = thread;
     this.State = thread.State;
 }
Example #36
0
        public void SetThread(CorThread thread)
        {
            if (thread == null)
                throw new InvalidOperationException();

            ICorDebugEval ce;
            int hr = thread.RawObject.CreateEval(out ce);
            if (hr < 0 || ce == null)
                throw new EvalException(hr, string.Format("Could not create an evaluator, HR=0x{0:X8}", hr));
            this.thread = thread;
            this.eval = new CorEval(ce);
        }
Example #37
0
		internal DnThread(DnProcess ownerProcess, ICorDebugThread thread, int uniqueId, int uniqueIdProcess) {
			this.ownerProcess = ownerProcess;
			this.thread = new CorThread(thread);
			this.uniqueId = uniqueId;
			this.uniqueIdProcess = uniqueIdProcess;
		}
Example #38
0
		internal DnThread(DnProcess ownerProcess, ICorDebugThread thread, int uniqueId, int uniqueIdProcess) {
			Process = ownerProcess;
			CorThread = new CorThread(thread);
			UniqueId = uniqueId;
			UniqueIdProcess = uniqueIdProcess;
		}
Example #39
0
			public ThreadInfo(CorThread thread) {
				Thread = thread;
				State = thread.State;
			}
Example #40
0
		/// <summary>
		/// Creates an eval. Don't call this if <see cref="EvalDisabled"/> is true
		/// </summary>
		/// <param name="thread">Thread to use</param>
		/// <returns></returns>
		public DnEval CreateEval(CorThread thread) {
			Debug.Assert(ProcessState == DebuggerProcessState.Stopped);
			if (ProcessState != DebuggerProcessState.Stopped)
				throw new EvalException(-1, "Can't evaluate unless debugger is stopped");
			var eval = Debugger.CreateEval();
			eval.EvalEvent += (s, e) => DnEval_EvalEvent(s, e, eval);
			eval.SetThread(thread);
			return eval;
		}
Example #41
0
		internal DnThread(DnProcess ownerProcess, ICorDebugThread thread, int incrementedId) {
			this.ownerProcess = ownerProcess;
			this.thread = new CorThread(thread);
			this.incrementedId = incrementedId;
		}