Ejemplo n.º 1
0
        private void FrameList_DoubleClick(object sender, EventArgs e)
        {
            Frame selectedFrame = FrameList.SelectedItems.Count == 0 ? null : FrameList.SelectedItems[0].Tag as Frame;

            if (selectedFrame == null)
            {
                return;
            }

            PdbLineNumber line = PdbSession.FindLinesByVirtualAddress(selectedFrame.Eip, 1).FirstOrDefault();

            if (line == null)
            {
                return;
            }

            PdbSourceFile sourceFile = line.SourceFile;

            if (sourceFile == null)
            {
                return;
            }

            EnvDTE.Window        window    = VisualStudio.Instance.ItemOperations.OpenFile(sourceFile.FileName);
            EnvDTE.TextSelection selection = window.Document.Selection as EnvDTE.TextSelection;
            if (selection == null)
            {
                return;
            }

            selection.GotoLine((int)line.LineNumber);
        }
Ejemplo n.º 2
0
        private void StepGdbButton_Click(object sender, EventArgs e)
        {
            if (Gdb.Running)
            {
                return;
            }

            Gdb.Step();

            OnUpdateControls();
            OnUpdate();

            PdbLineNumber line = PdbSession.FindLinesByVirtualAddress(Gdb.Registers.Eip, 1).FirstOrDefault();

            if (line == null)
            {
                return;
            }

            PdbSourceFile sourceFile = line.SourceFile;

            if (sourceFile == null)
            {
                return;
            }

            EnvDTE.Window        window    = VisualStudio.Instance.ItemOperations.OpenFile(sourceFile.FileName);
            EnvDTE.TextSelection selection = window.Document.Selection as EnvDTE.TextSelection;
            if (selection == null)
            {
                return;
            }

            selection.GotoLine((int)line.LineNumber);
        }
Ejemplo n.º 3
0
        public static void Generate()
        {
            string pdbPath      = Path.Combine(Program.Root, @"System\Kernel\Kernel.pdb");
            string wrappersPath = Path.Combine(Program.Root, @"System\[Tools]\Debugger\Wrappers\Wrappers.cs");

            PdbFile    pdbFile    = new PdbFile(pdbPath);
            PdbSession pdbSession = pdbFile.OpenSession(0x100000);

            using (StreamWriter output = File.CreateText(wrappersPath))
            {
                output.WriteLine("using System;");
                output.WriteLine("using System.Collections.Generic;");
                output.WriteLine("using System.IO;");
                output.WriteLine("using System.Linq;");
                output.WriteLine();

                output.WriteLine("namespace Debugger.Wrappers");
                output.WriteLine("{");

                /*foreach (PdbSymbol customType in pdbSession.Global.FindChildren(PdbSymbolTag.CustomType))
                 * {
                 *  output.WriteLine("    class _{0} : Object", customType.VirtualAddress);
                 *  output.WriteLine("    {");
                 *  output.WriteLine("    }");
                 * }*/

                output.Write("}");
            }
        }
Ejemplo n.º 4
0
        private void StepLineButton_Click(object sender, EventArgs e)
        {
            if (Gdb.Running)
            {
                return;
            }

            PdbLineNumber origin, line;

            origin = PdbSession.FindLinesByVirtualAddress(Gdb.Registers.Eip, 1).FirstOrDefault();

            while (true)
            {
                Gdb.Step();

                line = PdbSession.FindLinesByVirtualAddress(Gdb.Registers.Eip, 1).FirstOrDefault();
                if (line == null)
                {
                    continue;
                }

                if (origin == null && line != null)
                {
                    break;
                }
                if (line.SourceFileId != origin.SourceFileId)
                {
                    break;
                }
                if (line.LineNumber != origin.LineNumber || line.ColumnNumber != origin.ColumnNumber)
                {
                    break;
                }
            }

            OnUpdateControls();
            OnUpdate();

            PdbSourceFile sourceFile = line.SourceFile;

            if (sourceFile == null)
            {
                return;
            }

            EnvDTE.Window        window    = VisualStudio.Instance.ItemOperations.OpenFile(sourceFile.FileName);
            EnvDTE.TextSelection selection = window.Document.Selection as EnvDTE.TextSelection;
            if (selection == null)
            {
                return;
            }

            selection.GotoLine((int)line.LineNumber);
        }
Ejemplo n.º 5
0
        private void OnUpdateTasks()
        {
            // Get tasks
            List <Task> tasks = new List <Task>();

            Task firstTask = FirstTask.Object;

            while (firstTask != null)
            {
                tasks.Add(firstTask);
                firstTask = firstTask.Next;
            }

            // Refresh tasks panel
            Task         selectedTask = TaskList.SelectedItems.Count == 0 ? null : TaskList.SelectedItems[0].Tag as Task;
            ListViewItem selectedItem = null;

            TaskList.SuspendLayout();
            TaskList.Items.Clear();

            foreach (Task task in tasks)
            {
                uint eip = task.Eip;
                uint esp = task.Esp;

                uint         taskId   = task.Id;
                ListViewItem taskItem = new ListViewItem(taskId.ToString());

                taskItem.Tag = task;
                taskItem.SubItems.Add("0x" + eip.ToString("X8"));
                taskItem.SubItems.Add("0x" + esp.ToString("X8"));

                if (selectedTask != null && selectedTask.Id == taskId)
                {
                    selectedItem = taskItem;
                }

                PdbSymbol function = PdbSession.GetSymbolAtVirtualAddress(PdbSymbolTag.Function, eip);
                if (function != null)
                {
                    taskItem.SubItems.Add(function.Name);
                }

                TaskList.Items.Add(taskItem);
            }

            if (selectedItem != null)
            {
                TaskList.SelectedIndices.Add(TaskList.Items.IndexOf(selectedItem));
            }

            TaskList.ResumeLayout(true);
        }
Ejemplo n.º 6
0
        private void OnUpdateCallstack()
        {
            string       selectedFrame = FrameList.SelectedItems.Count == 0 ? null : FrameList.SelectedItems[0].Text;
            ListViewItem selectedItem  = null;

            FrameList.Items.Clear();

            // Get selected task
            Task selectedTask = TaskList.SelectedItems.Count == 0 ? null : TaskList.SelectedItems[0].Tag as Task;

            CallstackLabel.Text = "Callstack" + (selectedTask == null ? "" : (" - Task #" + selectedTask.Id));

            uint eip = selectedTask == null ? Gdb.Registers.Eip : selectedTask.Eip;
            uint ebp = selectedTask == null ? Gdb.Registers.Ebp : selectedTask.Ebp;

            // Add current method
            {
                PdbSymbol function = PdbSession.GetSymbolAtVirtualAddress(PdbSymbolTag.Function, eip);
                if (function == null)
                {
                    return;
                }

                Frame frame   = new Frame(eip, ebp, function);
                ulong pointer = function.VirtualAddress;

                ListViewItem frameItem = new ListViewItem("0x" + pointer.ToString("X8"));

                frameItem.Tag = frame;
                frameItem.SubItems.Add(function.Name);
                frameItem.SubItems.Add("0x" + (eip - pointer).ToString("X"));

                FrameList.Items.Add(frameItem);

                if (selectedFrame != null && selectedFrame == frameItem.Text)
                {
                    selectedItem = frameItem;
                }
            }

            uint stackPointer = ebp;

            /*// Search first frame
             * while (stackPointer < esp + 0x100)
             * {
             *  // We found a stack pointer
             *  uint stackValue = Gdb.Memory.ReadUInt32(stackPointer);
             *  if ((stackValue & 0xFFFF0000) == (stackPointer & 0xFFFF0000))
             *  {
             *      // And this stack pointer leads to another one
             *      uint stackValueValue = Gdb.Memory.ReadUInt32(stackValue);
             *      if ((stackValueValue & 0xFFFF0000) == (stackPointer & 0xFFFF0000))
             *      {
             *          // Then stop, this should be the one :)
             *          break;
             *      }
             *  }
             *
             *  stackPointer += 4;
             * }*/

            // Decode frames
            while (true)
            {
                uint ebp2 = Gdb.Memory.ReadUInt32(stackPointer);
                uint ret  = Gdb.Memory.ReadUInt32(stackPointer + 4);

                if (ret == 0)
                {
                    break;
                }

                PdbSymbol function = PdbSession.GetSymbolAtVirtualAddress(PdbSymbolTag.Function, ret);
                if (function == null)
                {
                    break;
                }

                Frame frame   = new Frame(ret, ebp2, function);
                ulong pointer = function.VirtualAddress;

                ListViewItem frameItem = new ListViewItem("0x" + pointer.ToString("X8"));

                frameItem.Tag = frame;
                frameItem.SubItems.Add(function.Name);
                frameItem.SubItems.Add("0x" + (ret - pointer).ToString("X"));

                FrameList.Items.Add(frameItem);

                if (selectedFrame != null && selectedFrame == frameItem.Text)
                {
                    selectedItem = frameItem;
                }

                stackPointer = ebp2;
            }

            if (selectedItem != null)
            {
                FrameList.SelectedIndices.Add(FrameList.Items.IndexOf(selectedItem));
            }
        }
Ejemplo n.º 7
0
        private void StepOverButton_Click(object sender, EventArgs e)
        {
            if (Gdb.Running)
            {
                return;
            }

            List <PdbLineNumber> lines = PdbSession.FindLinesByVirtualAddress(Gdb.Registers.Eip, 100).ToList();

            if (lines.Count < 1)
            {
                StepLineButton_Click(sender, e);
                return;
            }

            PdbLineNumber currentLine = lines.First();
            PdbLineNumber nextLine    = currentLine == null ? null : lines.FirstOrDefault(l => l.SourceFileId == currentLine.SourceFileId && l.VirtualAddress > currentLine.VirtualAddress);

            if (nextLine == null)
            {
                StepLineButton_Click(sender, e);
                return;
            }

            PdbLineNumber line = null;

            while (true)
            {
                Gdb.Step();

                line = PdbSession.FindLinesByVirtualAddress(Gdb.Registers.Eip, 1).FirstOrDefault();
                if (line == null)
                {
                    continue;
                }

                if (line.SourceFileId == nextLine.SourceFileId && line.LineNumber == nextLine.LineNumber && line.ColumnNumber == nextLine.ColumnNumber)
                {
                    break;
                }
            }

            OnUpdateControls();
            OnUpdate();

            PdbSourceFile sourceFile = line.SourceFile;

            if (sourceFile == null)
            {
                return;
            }

            EnvDTE.Window        window    = VisualStudio.Instance.ItemOperations.OpenFile(sourceFile.FileName);
            EnvDTE.TextSelection selection = window.Document.Selection as EnvDTE.TextSelection;
            if (selection == null)
            {
                return;
            }

            selection.GotoLine((int)line.LineNumber);
        }
Ejemplo n.º 8
0
        private void OnUpdateVariables()
        {
            VariableList.Items.Clear();

            Task  selectedTask  = TaskList.SelectedItems.Count == 0 ? null : TaskList.SelectedItems[0].Tag as Task;
            Frame selectedFrame = FrameList.SelectedItems.Count == 0 ? null : FrameList.SelectedItems[0].Tag as Frame;

            VariablesLabel.Text = "Variables";
            if (selectedTask != null)
            {
                VariablesLabel.Text += " - Task #" + selectedTask.Id;
            }
            if (selectedFrame != null)
            {
                VariablesLabel.Text += " - " + selectedFrame.Function.Name;
            }

            uint eip = selectedFrame != null ? selectedFrame.Eip : selectedTask != null ? selectedTask.Eip : Gdb.Registers.Eip;
            uint ebp = selectedFrame != null ? selectedFrame.Ebp : selectedTask != null ? selectedTask.Ebp : Gdb.Registers.Ebp;

            PdbSymbol function = PdbSession.GetSymbolAtVirtualAddress(PdbSymbolTag.Function, eip);

            if (function == null)
            {
                return;
            }

            foreach (PdbSymbol variable in function.FindChildren(PdbSymbolTag.Data))
            {
                ListViewItem variableItem = new ListViewItem(variable.Name);

                PdbSymbol variableType = variable.Type;
                if (variableType == null)
                {
                    continue;
                }

                int   offset = variable.Offset;
                ulong size   = variableType.Length;

                byte[] buffer = new byte[size];
                Gdb.Memory.Read((ulong)(ebp + offset), buffer, 0, (int)size);

                Type   type     = GetTypeFromSymbol(variableType);
                string typeName = GetTypeNameFromSymbol(variableType);
                string value    = "";

                if (type == null)
                {
                    value = "";
                }
                else if (type == typeof(string))
                {
                    value = "{ String }";
                }
                else if (type == typeof(bool))
                {
                    value = buffer[0] != 0 ? "true" : "false";
                }
                else if (type == typeof(sbyte))
                {
                    value = ((sbyte)buffer[0]).ToString();
                }
                else if (type == typeof(byte))
                {
                    value = buffer[0].ToString();
                }
                else if (type == typeof(short))
                {
                    value = BitConverter.ToInt16(buffer, 0).ToString();
                }
                else if (type == typeof(ushort))
                {
                    value = BitConverter.ToUInt16(buffer, 0).ToString();
                }
                else if (type == typeof(int))
                {
                    value = BitConverter.ToInt32(buffer, 0).ToString();
                }
                else if (type == typeof(uint))
                {
                    value = BitConverter.ToUInt32(buffer, 0).ToString();
                }
                else if (type == typeof(long))
                {
                    value = BitConverter.ToInt64(buffer, 0).ToString();
                }
                else if (type == typeof(ulong))
                {
                    value = BitConverter.ToUInt64(buffer, 0).ToString();
                }
                else if (typeName == "char*")
                {
                    value = "\"" + ReadCString(BitConverter.ToUInt32(buffer, 0)) + "\"";
                }
                else if (typeName == "String*")
                {
                    value = "\"" + ReadString(BitConverter.ToUInt32(buffer, 0)) + "\"";
                }
                else if (type == typeof(IntPtr))
                {
                    value = "0x" + BitConverter.ToUInt32(buffer, 0).ToString("x8");
                }

                variableItem.SubItems.Add(value);
                variableItem.SubItems.Add(typeName);
                variableItem.SubItems.Add(string.Join(" ", buffer.Select(b => b.ToString("X2"))));

                VariableList.Items.Add(variableItem);
            }
        }