Exemplo n.º 1
0
        /// <summary>
        /// Update process info and thread stack traces
        /// </summary>
        /// <param name="withStack">Updates stack traces or just general info</param>
        /// <param name="allThreads">If updating stack trace should we update all of them</param>
        /// <param name="threadIds">If not updating all which ones do we update</param>
        /// <param name="depth">How many frames deep to display, 0 means show all</param>
        private void UpdateProcessInfo(bool withStack, bool allThreads, IList <int> threadIds, int depth)
        {
            processThreads.Clear();
            processCorThreads.Clear();
            generalThreadInfos.Clear();

            try
            {
                AttachAndStop();

                //build the general thread information hash
                foreach (ProcessThread gThread in generalProcessInfo.Threads)
                {
                    generalThreadInfos[gThread.Id] = gThread;
                }

                if (isAttached)
                {
                    //add the threads to an arraylist
                    foreach (CorThread cThread in debuggeeProcess.Threads)
                    {
                        try
                        {
                            ThreadInfo thread = new ThreadInfo(cThread, this);
                            if (withStack)         //update stacks also
                            {
                                if (allThreads || threadIds.Contains(cThread.Id))
                                {
                                    thread.UpdateStackTrace(depth);
                                }
                            }
                            processCorThreads.Add(cThread);
                            processThreads[cThread.Id] = thread;
                        }
                        catch (KeyNotFoundException)
                        {
                            // This can happen if the thread died before we could get to it
                        }
                    }
                }
            }
            catch (COMException)
            {
            }
            catch (Exception e)
            {
                // We need to explicitly catch the exception here (finally handler may not be invoked, when the exception terminates the whole process)
                Console.WriteLine("Caught exception: " + e);
                DetachAndResume();
                throw;
            }
            finally
            {
                DetachAndResume();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update process info and thread stack traces
        /// </summary>
        /// <param name="withStack">Updates stack traces or just general info</param>
        /// <param name="allThreads">If updating stack trace should we update all of them</param>
        /// <param name="threadIds">If not updating all which ones do we update</param>
        /// <param name="depth">How many frames deep to display, 0 means show all</param>
        private void UpdateProcessInfo(bool withStack, bool allThreads, IList <int> threadIds, int depth)
        {
            processThreads.Clear();
            processCorThreads.Clear();
            generalThreadInfos.Clear();

            try
            {
                AttachAndStop();

                //build the general thread information hash
                foreach (ProcessThread gThread in generalProcessInfo.Threads)
                {
                    generalThreadInfos[gThread.Id] = gThread;
                }

                if (isAttached)
                {
                    //add the threads to an arraylist
                    foreach (CorThread cThread in debuggeeProcess.Threads)
                    {
                        try
                        {
                            ThreadInfo thread = new ThreadInfo(cThread, this);
                            if (withStack)                            //update stacks also
                            {
                                if (allThreads || threadIds.Contains(cThread.Id))
                                {
                                    thread.UpdateStackTrace(depth);
                                }
                            }
                            processCorThreads.Add(cThread);
                            processThreads[cThread.Id] = thread;
                        }
                        catch (KeyNotFoundException ex)
                        {
                            // This can happen if the thread died before we could get to it
                            Debug.WriteLine(ex.ToString());
                        }
                    }
                }
            }
            catch (COMException ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                DetachAndResume();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns an array of formated strings for printing the stack trace
        /// *NOTE: changing the format of the stack trace will cause the unit test to fail since
        /// the unit test has hardcoded stack traces in it: be advised!
        /// </summary>
        public IList <string> GetDisplayStackTrace()
        {
            var threadSelectedIdList = new List <int>();

            List <string> stackTrace = new List <string>();

            try
            {
                UpdateAllStackTraces(0);
                foreach (ThreadInfo t in processThreads.Values)
                {
                    threadSelectedIdList.Add(t.ThreadId);
                }

                threadSelectedIdList.Sort();

                foreach (int threadId in threadSelectedIdList)
                {
                    try
                    {
                        ThreadInfo threadInfo = processThreads[threadId];
                        if (threadInfo.FrameStack.Count > 0)  // Ignore threads with no managed stack trace
                        {
                            stackTrace.Add(string.Format(CultureInfo.CurrentCulture.NumberFormat, "Thread: {0}", threadInfo.ThreadId, threadInfo.FrameStack));
                            foreach (FrameInfo frameInfo in threadInfo.FrameStack)
                            {
                                if (frameInfo.FunctionLineNumber > -1)
                                {
                                    stackTrace.Add(string.Format(CultureInfo.CurrentCulture.NumberFormat, "  {0} ({1}:{2})", frameInfo.FunctionFullName,
                                                                 frameInfo.FunctionFileName, frameInfo.FunctionLineNumber));
                                }
                                else
                                {
                                    stackTrace.Add(string.Format(CultureInfo.CurrentCulture.NumberFormat, "  {0} ({1})", frameInfo.FunctionFullName,
                                                                 frameInfo.FunctionFileName));
                                }
                            }
                            stackTrace.Add("\n");
                        }
                    }
                    catch (COMException ex) { Debug.WriteLine(ex.ToString()); }
                    catch (KeyNotFoundException ex)
                    {
                        Debug.WriteLine(ex.ToString());
                    }
                }
            }
            catch (COMException ex) { Debug.WriteLine(ex.ToString()); }

            return(stackTrace);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns an array of formated strings for printing the stack trace
        /// *NOTE: changing the format of the stack trace will cause the unit test to fail since
        /// the unit test has hardcoded stack traces in it: be advised!
        /// </summary>
        /// <param name="threadSelectedIdList">List of thread ids to get stack trace of, </param>
        /// <param name="depth">How many frames depe to display, 0 means show all</param>
        /// <returns></returns>
        public IList <string> GetDisplayStackTrace(IList <int> threadSelectedIdList, int depth)
        {
            ResourceManager stackStrings = new ResourceManager(typeof(Resources));

            if (threadSelectedIdList == null)
            {
                threadSelectedIdList = new List <int>();
            }

            List <string> stackTrace = new List <string>();

            try
            {
                if (threadSelectedIdList.Count == 0)
                {
                    UpdateAllStackTraces(depth);
                    foreach (ThreadInfo t in processThreads.Values)
                    {
                        threadSelectedIdList.Add(t.ThreadId);
                    }
                }
                else
                {
                    UpdateSelectedThreadStacks(threadSelectedIdList, depth);
                }

                stackTrace.Add(String.Format(CultureInfo.CurrentCulture.DateTimeFormat, "{1}{0:G}{1}", DateTime.Now, Environment.NewLine));
                if (depth == 1)
                {
                    stackTrace.Add(String.Format(CultureInfo.CurrentCulture, "{0} {1} {2}{3}", stackStrings.GetString("StackDepthIs"), depth, stackStrings.GetString("wordFrame"), Environment.NewLine));
                }
                else if (depth > 1)
                {
                    stackTrace.Add(String.Format(CultureInfo.CurrentCulture, "{0} {1} {2}{3}", stackStrings.GetString("StackDepthIs"), depth, stackStrings.GetString("wordFrames"), Environment.NewLine));
                }
                else
                {
                    stackTrace.Add(stackStrings.GetString("stackDepthAll") + Environment.NewLine);
                }
                foreach (int threadId in threadSelectedIdList)
                {
                    try
                    {
                        ThreadInfo threadInfo = processThreads[threadId];
                        stackTrace.Add(String.Format(CultureInfo.CurrentCulture.NumberFormat, "{3}{1} {2}: {0}{3}", threadInfo.ThreadId, stackStrings.GetString("wordThread"), stackStrings.GetString("wordID"), Environment.NewLine));
                        int i = 0;
                        foreach (FrameInfo frameInfo in threadInfo.FrameStack)
                        {
                            if (frameInfo.FunctionLineNumber > -1)
                            {
                                stackTrace.Add(String.Format(CultureInfo.CurrentCulture.NumberFormat, "\t{0}. {1} ({2}:{3}){4}", i, frameInfo.FunctionFullName, frameInfo.FunctionFileName, frameInfo.FunctionLineNumber, Environment.NewLine));
                            }
                            else
                            {
                                stackTrace.Add(String.Format(CultureInfo.CurrentCulture.NumberFormat, "\t{0}. {1} ({2}){3}", i, frameInfo.FunctionFullName, frameInfo.FunctionFileName, Environment.NewLine));
                            }
                            i++;
                        }
                    }
                    catch (COMException ex) { Debug.WriteLine(ex.ToString()); }
                    catch (KeyNotFoundException ex)
                    {
                        Debug.WriteLine(ex.ToString());
                    }
                }
            }
            catch (COMException ex) { Debug.WriteLine(ex.ToString()); }

            return(stackTrace);
        }