Example #1
0
        /// <summary>
        /// We're done loading the initial threads.
        /// Notify the GUI that we're good to go.
        /// </summary>
        private void OnLoadThreadsDone(Dot42.DebuggerLib.Debugger debugger, DebugProcess debugProcess)
        {
            // Notify module
            //eventCallback.Send(program, new ModuleLoadEvent(program.MainModule, "Loading module", true));
            //eventCallback.Send(program, new SymbolSearchEvent(program.MainModule, "Symbols loaded", enum_MODULE_INFO_FLAGS.MIF_SYMBOLS_LOADED));

            var mainThread = debugProcess.ThreadManager.MainThread();

            if (mainThread != null)
            {
                // Threads loaded
                // Load complete
                //eventCallback.Send(mainThread, new LoadCompleteEvent());
                //eventCallback.Send(mainThread, new EntryPointEvent());

                // Resume now
                debugger.VirtualMachine.ResumeAsync();

                // Notify SD
                Action onDebugStarted = () => {
                    if (stateUpdate != null)
                    {
                        stateUpdate(LauncherStates.Attached, string.Empty);
                        stateUpdate = null;
                    }
                    DebugStarted.Fire(this);
                };
                Dot42Addin.InvokeAsyncAndForget(onDebugStarted);
            }
            else
            {
                DLog.Error(DContext.VSDebuggerLauncher, "No main thread found");
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public int GetHostName(enum_GETHOSTNAME_TYPE dwHostNameType, out string pbstrHostName)
        {
            //
            // Gets the name of the process hosting the program.
            //

            LoggingUtils.PrintFunction();

            try
            {
                if (dwHostNameType == enum_GETHOSTNAME_TYPE.GHN_FRIENDLY_NAME)
                {
                    LoggingUtils.RequireOk(DebugProcess.GetName(enum_GETNAME_TYPE.GN_MONIKERNAME, out pbstrHostName));
                }
                else if (dwHostNameType == enum_GETHOSTNAME_TYPE.GHN_FILE_NAME)
                {
                    LoggingUtils.RequireOk(DebugProcess.GetName(enum_GETNAME_TYPE.GN_FILENAME, out pbstrHostName));
                }
                else
                {
                    throw new NotImplementedException();
                }

                return(Constants.S_OK);
            }
            catch (Exception e)
            {
                LoggingUtils.HandleException(e);

                pbstrHostName = string.Empty;

                return(Constants.E_FAIL);
            }
        }
Example #3
0
            public override void OnCreateProcess(DebugProcess newProcess)
            {
                form.Log(
                    "Program " + newProcess.MainModule.ImageFile + " was launched\r\n" +
                    "\tPID #" + newProcess.Id);

                form.list_AvailableSources.Clear();

                if (newProcess.MainModule.ContainsSymbolData)
                {
                    foreach (var section in newProcess.MainModule.ModuleMetaInfo.CodeViewSection.Data.SubsectionDirectory.Sections)
                    {
                        if (section is sstSrcModule)
                        {
                            var srcModule = (sstSrcModule)section;

                            foreach (var f in srcModule.FileInfo)
                            {
                                form.list_AvailableSources.Items.Add(new ListViewItem(f.SourceFileName)
                                {
                                    Tag = f
                                });
                            }
                        }
                    }
                }
            }
Example #4
0
 public override void OnModuleUnloaded(DebugProcess mainProcess, DebugProcessModule module)
 {
     if (module != null)
     {
         form.Log(module.ImageFile + " unloaded (0x" + string.Format("{0,8:X}", module.ImageBase) + ")");
     }
     else
     {
         form.Log("Some module was unloaded");
     }
 }
Example #5
0
 /// <summary>
 /// Debug process has stopped.
 /// </summary>
 private void OnDebugProcessTerminated(object sender, EventArgs e)
 {
     DebugProcess = null;
     Dot42Addin.InvokeAsyncAndForget(() => {
         try {
             DebugStopped.Fire(this);
         } catch (Exception ex) {
             LoggingService.Warn("Error in DebugStopped", ex);
             // Ignore
         }
         ResetBreakpointBookmark();
     });
 }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public int GetHostPid(AD_PROCESS_ID [] pHostProcessId)
        {
            //
            // Gets the system process identifier for the process hosting the program.
            //

            LoggingUtils.PrintFunction();

            try
            {
                LoggingUtils.RequireOk(DebugProcess.GetPhysicalProcessId(pHostProcessId));

                return(Constants.S_OK);
            }
            catch (Exception e)
            {
                LoggingUtils.HandleException(e);

                return(Constants.E_FAIL);
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public int GetProgramName(out string pbstrProgramName)
        {
            //
            // Gets the name of the program.
            //

            LoggingUtils.PrintFunction();

            try
            {
                LoggingUtils.RequireOk(DebugProcess.GetName(enum_GETNAME_TYPE.GN_NAME, out pbstrProgramName));

                return(Constants.S_OK);
            }
            catch (Exception e)
            {
                LoggingUtils.HandleException(e);

                pbstrProgramName = string.Empty;

                return(Constants.E_FAIL);
            }
        }
Example #8
0
        /// <summary>
        /// Attach to the given debugger and start debugging.
        /// </summary>
        public void Attach(string apkPath, Dot42.DebuggerLib.Debugger debugger, Guid debuggerGuid)
        {
            // Cleanup static state
            Launcher.GetAndRemoveDebugger(debuggerGuid, out stateUpdate);

            // Notify SD
            Dot42Addin.InvokeAsyncAndForget(() => DebugStarting.Fire(this));

            // Load map file
            var mapFilePath = Path.ChangeExtension(apkPath, ".d42map");
            var mapFile     = File.Exists(mapFilePath) ? new MapFile(mapFilePath) : new MapFile();

            // Suspend and prepare the VM
            var suspend = debugger.VirtualMachine.SuspendAsync();
            var prepare = suspend.ContinueWith(t => {
                t.ForwardException();
                return(debugger.PrepareAsync());
            }).Unwrap();
            var debugProcess = new DebugProcess(debugger, mapFile);

            DebugProcess = debugProcess;
            var initializeBreakpoints = prepare.ContinueWith(t => {
                t.ForwardException();
                // Setup breakpoints
                Dot42Addin.Invoke(() => debugProcess.BreakpointManager.InitializeBreakpoints(DebuggerService.Breakpoints));
            });
            var loadThreads = initializeBreakpoints.ContinueWith(t => {
                t.ForwardException();
                return(debugProcess.ThreadManager.RefreshAsync());
            }).Unwrap();

            loadThreads.ContinueWith(t => {
                t.ForwardException();
                OnLoadThreadsDone(debugger, debugProcess);
            });
        }
Example #9
0
        static void Main(string[] args)
        {

            if(4 != IntPtr.Size)
            {
                string bitserr = "Expected to be 32-bit process, not " + (IntPtr.Size * 8).ToString() + "-bit";
                Console.WriteLine("Error:  {0}", bitserr);
                Console.Error.WriteLine("{0}", bitserr);
                Environment.Exit(110);
            }

            Console.WriteLine("MR.Debug");

            int iarg = 0;

            if (args.Length > iarg && "-xdebug" == args[iarg])
            {
                iarg++;
                IsXDebug = true;
                IsVerbose = true;
            }
            if (args.Length > iarg && "-verbose" == args[iarg])
            {
                iarg++;
                IsVerbose = true;
            }

            string ClientProcessName;
            if (args.Length > iarg)
            {
                ClientProcessName = args[iarg++];
            }
            else
            {
                throw new ArgumentException("Expected client process name");
            }
            string ClientProcessArgs = "";
            if (args.Length > iarg)
            {
                ClientProcessArgs = args[iarg++];
            }

            if (IsVerbose)
            {
                Console.WriteLine("  Debugging: \"{0}\" \"{1}\"", ClientProcessName, ClientProcessArgs);
            }

            DebugProcess dbgproc = new DebugProcess();

            dbgproc.idbg = CreateDebuggingInterfaceFromVersion(3 /* 2.0 */, ClientProcessVersion);
            if (null == dbgproc.idbg)
            {
                throw new NotSupportedException("null debugger");
            }
            dbgproc.idbg.Initialize();
            dbgproc.idbg.SetManagedHandler(new DebugCallback(dbgproc));

            if (IsVerbose)
            {
                Console.WriteLine("  Initialized debugging interface");
            }

            _SECURITY_ATTRIBUTES nosecattribs = new _SECURITY_ATTRIBUTES();
            nosecattribs.bInheritHandle = 0;
            nosecattribs.lpSecurityDescriptor = IntPtr.Zero;
            nosecattribs.nLength = (uint)Marshal.SizeOf(typeof(_SECURITY_ATTRIBUTES));

            //PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
            IntPtr ppi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PROCESS_INFORMATION)));

            STARTUPINFO si = new STARTUPINFO();
            si.cb = Marshal.SizeOf(typeof(STARTUPINFO));
            si.dwFlags = 0x00000100; // STARTF_USESTDHANDLES
            si.hStdInput = new IntPtr(0);
            si.hStdOutput = GetStdHandle(-11); // STD_OUTPUT_HANDLE
            si.hStdError = GetStdHandle(-12); // STD_ERROR_HANDLE
            IntPtr psi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(STARTUPINFO)));
            Marshal.StructureToPtr(si, psi, false);

            lock (dbgproc)
            {
                dbgproc.idbg.CreateProcess(ClientProcessName, "\"" + ClientProcessName + "\" " + ClientProcessArgs,
                    ref nosecattribs, ref nosecattribs, 1, 0,
                    IntPtr.Zero, Environment.CurrentDirectory, (uint)psi.ToInt32(), (uint)ppi.ToInt32(),
                    CorDebugCreateProcessFlags.DEBUG_NO_SPECIAL_OPTIONS, out dbgproc.idbgproc);
            }

            if (IsVerbose)
            {
                Console.WriteLine("  Started debug process");
            }

            System.Threading.Thread runthread = new System.Threading.Thread(
                new System.Threading.ThreadStart(
                delegate()
                {
                    try
                    {
                        dbgproc.Run();
                    }
                    catch (Exception e)
                    {
                        string runthderr = "Run thread error: " + e.Message;
                        Console.WriteLine("Error:  {0}", runthderr);
                        Console.Error.WriteLine("{0}", runthderr);
                        Environment.Exit(111);
                    }
                }));
            runthread.Start();

            while (!dbgproc.FinishedInitializing)
            {
                System.Threading.Thread.Sleep(100);
            }
            for (; ; )
            {
                string line = Console.ReadLine();
                if (null == line)
                {
                    line = "quit";
                }
                dbgproc.AddCommand(line);
                if (dbgproc.ProcessExit)
                {
                    break;
                }
            }

            dbgproc.idbg.Terminate();

#if DEBUG
            Console.WriteLine("DEBUG:  exit");
#endif

        }
Example #10
0
 public DebugCallback(DebugProcess dbgproc)
 {
     this.dbgproc = dbgproc;
 }
Example #11
0
 public DebugProgram(DebugProcess process)
 {
     _process = process;
 }
 public DebugProgram(DebugProcess process) {
     _process = process;
 }
Example #13
0
 public Breakpoint(DebugProcess proc, IntPtr breakpointAddress)
 {
     this.Owner   = proc;
     this.Address = breakpointAddress;
 }
Example #14
0
 public override void OnProcessExit(DebugProcess process, uint exitCode)
 {
     form.eventLogBox.AppendText("Process exited with code 0x" + string.Format("{0,X}", exitCode));
 }
Example #15
0
 public override void OnModuleLoaded(DebugProcess mainProcess, DebugProcessModule module)
 {
     form.Log(module.ImageFile + " loaded (0x" + string.Format("{0,8:X}", module.ImageBase) + ")");
 }