Пример #1
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                return;
            }

            if (args.Length == 2)
            {
                UnpackDummyApp();
            }

            string  typeName       = args[0];
            string  moduleFullName = args[1];
            string  moduleFileName = Path.GetFileNameWithoutExtension(moduleFullName);
            int     pid            = 0;
            Process child          = null;
            string  proxyExePath   = "DummyApp.exe";

            if (args.Length >= 3)
            {
                if (Int32.TryParse(args[2], out pid) == false)
                {
                    proxyExePath = args[2];
                }
            }

            string rootPathToSave = Path.Combine(Environment.CurrentDirectory, "sym");

            using (UserDebugger debugger = new UserDebugger())
            {
                debugger.SetOutputText(false);
                debugger.FlushCallbacks();

                if (pid == 0)
                {
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName         = proxyExePath;
                    psi.UseShellExecute  = false;
                    psi.CreateNoWindow   = true;
                    psi.LoadUserProfile  = false;
                    psi.WorkingDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location);

                    child = Process.Start(psi);
                    pid   = child.Id;
                }

                bool attached = false;

                try
                {
                    debugger.ModuleLoaded += (ModuleInfo modInfo) =>
                    {
                        if (modInfo.ModuleName.IndexOf(moduleFileName, StringComparison.OrdinalIgnoreCase) == -1)
                        {
                            return;
                        }

                        byte[] buffer;
                        int    readBytes = debugger.ReadMemory(modInfo.BaseOffset, modInfo.ModuleSize, out buffer);
                        if (readBytes != modInfo.ModuleSize)
                        {
                            return;
                        }

                        PEImage.DownloadPdb(modInfo.ModuleName, buffer, new IntPtr((long)modInfo.BaseOffset), (int)modInfo.ModuleSize, rootPathToSave);

                        RunDTSetCommand(debugger, rootPathToSave, moduleFullName, typeName);

                        debugger.SetOutputText(false);
                        debugger.Execute(DEBUG_OUTCTL.IGNORE, (child == null) ? "q" : "qd", DEBUG_EXECUTE.NOT_LOGGED);
                    };

                    if (debugger.AttachTo(pid) == false)
                    {
                        Console.WriteLine("Failed to attach");
                        return;
                    }

                    attached = true;
                    debugger.WaitForEvent(DEBUG_WAIT.DEFAULT, 1000 * 5);
                }
                finally
                {
                    if (attached == true)
                    {
                        debugger.Detach();
                    }

                    try
                    {
                        if (child != null)
                        {
                            child.Kill();
                        }
                    }
                    catch { }
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            using (UserDebugger debugger = new UserDebugger())
            {
                Console.CancelKeyPress += (s, e) =>
                {
                    e.Cancel = true;
                    debugger.SetInterrupt();
                };

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName        = "DummyApp.exe";
                psi.UseShellExecute = true;

                Process child = Process.Start(psi);

                try
                {
                    if (debugger.AttachTo(child.Id) == false)
                    {
                        Console.WriteLine("Failed to attach");
                        return;
                    }

                    int hr = debugger.WaitForEvent();

                    while (true)
                    {
                        hr = debugger.GetExecutionStatus(out DEBUG_STATUS status);
                        if (hr != (int)HResult.S_OK)
                        {
                            break;
                        }

                        if (status == DEBUG_STATUS.NO_DEBUGGEE)
                        {
                            Console.WriteLine("No Target");
                            break;
                        }

                        if (status == DEBUG_STATUS.GO || status == DEBUG_STATUS.STEP_BRANCH ||
                            status == DEBUG_STATUS.STEP_INTO || status == DEBUG_STATUS.STEP_OVER)
                        {
                            hr = debugger.WaitForEvent();
                            continue;
                        }

                        if (debugger.StateChanged)
                        {
                            Console.WriteLine();
                            debugger.StateChanged = false;
                            if (debugger.BreakpointHit)
                            {
                                debugger.OutputCurrentState(DEBUG_OUTCTL.THIS_CLIENT, DEBUG_CURRENT.DEFAULT);
                                debugger.BreakpointHit = false;
                            }
                        }

                        debugger.OutputPromptWide(DEBUG_OUTCTL.THIS_CLIENT, null);
                        Console.Write(" ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        string command = Console.ReadLine();
                        debugger.ExecuteWide(command);
                    }
                }
                finally
                {
                    try
                    {
                        debugger.Detach();
                    } catch { }

                    try
                    {
                        child.Kill();
                    }
                    catch { }
                }
            }
        }