Beispiel #1
0
        /// <summary>
        /// Function tries to bind a breakpoint to the specified module.
        /// </summary>
        /// <param name="managedModule">A module the breakpoint should be bound to.</param>
        /// <returns>true if breakpoint was successfully bound or false if it failed or was already bound.</returns>
        /// <remarks>
        ///     This function is called by breakpoint manager for every brekapoint whenever a new module
        ///     gets loaded into the debugged process or whenever a dynamic module loads a new class
        ///     or new symbols. This adds any missing bindings, but will not duplicate any that already exist.
        /// </remarks>
        public sealed override bool BindToModule(MDbgModule managedModule)
        {
            List <MDbgFunction> funcs;
            int ILoffset;

            // If we already bound a breakpoint in this module then nothing to do
            if (m_breakpoints != null && m_breakpoints.ContainsKey(managedModule))
            {
                return(false);
            }

            // If we can't resolve the location in this module then there is nothing to do
            if (!m_location.ResolveLocation(this, managedModule, out funcs, out ILoffset))
            {
                return(false);
            }

            // Use the resolved information to get a raw CorBreakpoint object.
            CorFunctionBreakpoint breakpoint = null;

            try
            {
                if (ILoffset == 0)
                {
                    foreach (var func in funcs)
                    {
                        breakpoint = func.CorFunction.CreateBreakpoint();
                        SetupBreakpoint(breakpoint, managedModule);
                    }
                }
                else
                {
                    foreach (var func in funcs)
                    {
                        // we need to set a breakpoint on code rather than directly on function
                        CorCode code = func.CorFunction.ILCode;

                        if (code == null)
                        {
                            throw new MDbgException(String.Format(CultureInfo.InvariantCulture, "IL Code for function {0} is null", new Object[] { func.FullName }));
                        }
                        breakpoint = code.CreateBreakpoint(ILoffset);
                        SetupBreakpoint(breakpoint, managedModule);
                    }
                }
            }
            catch (NotImplementedException)
            {
                return(false);
            }
            catch (COMException)
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        static void ProcessCommand(CorProcess process)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Console.Write("> ");
                    String command = Console.ReadLine();

                    if (command.StartsWith("set-break", StringComparison.Ordinal))
                    {
                        // setting breakpoint
                        command = command.Remove(0, "set-break".Length).Trim();

                        // try module!type.method location (simple regex used)
                        Match match = methodBreakpointRegex.Match(command);
                        if (match.Groups["method"].Length > 0)
                        {
                            Console.Write("Setting method breakpoint... ");

                            CorFunction func = process.ResolveFunctionName(match.Groups["module"].Value, match.Groups["class"].Value,
                                                                           match.Groups["method"].Value);
                            func.CreateBreakpoint().Activate(true);

                            Console.WriteLine("done.");
                            continue;
                        }
                        // try file code:line location
                        match = codeBreakpointRegex.Match(command);
                        if (match.Groups["filepath"].Length > 0)
                        {
                            Console.Write("Setting code breakpoint...");

                            int offset;
                            CorCode code = process.ResolveCodeLocation(match.Groups["filepath"].Value,
                                                                       Int32.Parse(match.Groups["linenum"].Value),
                                                                       out offset);
                            code.CreateBreakpoint(offset).Activate(true);

                            Console.WriteLine("done.");
                            continue;
                        }
                    }
                    else if (command.StartsWith("go", StringComparison.Ordinal))
                    {
                        process.Continue(false);
                        ProcessCommand(process);
                        break;
                    }
                }
            });
        }
Beispiel #3
0
        /// <summary>
        /// Function tries to bind a breakpoint to the specified module.
        /// </summary>
        /// <param name="managedModule">A module the breakpoint should be bound to.</param>
        /// <returns>true if breakpoint was successfully bound or false if it failed or was already bound.</returns>
        /// <remarks>
        ///     This function is called by breakpoint manager for every brekapoint whenever a new module
        ///     gets loaded into the debugged process or whenever a dynamic module loads a new class
        ///     or new symbols. This adds any missing bindings, but will not duplicate any that already exist.
        /// </remarks>
        public sealed override bool BindToModule(MDbgModule managedModule)
        {
            MDbgFunction func;
            int          ILoffset;

            // If we already bound a breakpoint in this module then nothing to do
            if (m_breakpoints != null && m_breakpoints.ContainsKey(managedModule))
            {
                return(false);
            }

            // If we can't resolve the location in this module then there is nothing to do
            if (!m_location.ResolveLocation(this, managedModule, out func, out ILoffset))
            {
                return(false);
            }

            // Use the resolved information to get a raw CorBreakpoint object.
            CorFunctionBreakpoint breakpoint = null;

            try
            {
                if (ILoffset == 0)
                {
                    breakpoint = func.CorFunction.CreateBreakpoint();
                }
                else
                {
                    // we need to set a breakpoint on code rather than directly on function
                    CorCode code = func.CorFunction.ILCode;

                    if (code == null)
                    {
                        throw new MDbgException(String.Format(CultureInfo.InvariantCulture, "IL Code for function {0} is null", new Object[] { func.FullName }));
                    }
                    breakpoint = code.CreateBreakpoint(ILoffset);
                }
            }
            catch (NotImplementedException)
            {
                return(false);
            }
            catch (COMException)
            {
                return(false);
            }

            // Add the new CorBreakpoint object to our internal list and register a handler for it.
            Debug.Assert(breakpoint != null);
            breakpoint.Activate(true);
            if (m_breakpoints == null)
            {
                m_breakpoints = new Dictionary <MDbgModule, CorFunctionBreakpoint>();
            }
            m_breakpoints.Add(managedModule, breakpoint);

            MDbgProcess p = managedModule.Process;
            CustomBreakpointEventHandler handler = new CustomBreakpointEventHandler(this.InternalOnHitHandler);

            p.RegisterCustomBreakpoint(breakpoint, handler);

            return(true);
        }
        /// <summary>
        /// Function tries to bind a breakpoint to the specified module.
        /// </summary>
        /// <param name="managedModule">A module the breakpoint should be bound to.</param>
        /// <returns>true if breakpoint was successfully bound or false if it failed or was already bound.</returns>
        /// <remarks>
        ///     This function is called by breakpoint manager for every brekapoint whenever a new module
        ///     gets loaded into the debugged process or whenever a dynamic module loads a new class
        ///     or new symbols. This adds any missing bindings, but will not duplicate any that already exist.
        /// </remarks>
        public sealed override bool BindToModule(MDbgModule managedModule)
        {
            MDbgFunction func;
            int          ILoffset;

            // Note that in some cases (eg. source/line breakpoints) we may actually
            // want to bind to multiple locations in this module instead of just one.
            if (!m_location.ResolveLocation(this, managedModule, out func, out ILoffset))
            {
                return(false);
            }

            if (m_breakpoints != null)
            {
                // Assume all breakpoints are CorFunctionBreakpoints.
                // If this ever becomes invalid, we'll need a new check here to avoid
                // duplicating that type of breakpoint.
                foreach (CorFunctionBreakpoint cb in m_breakpoints)
                {
                    // If we find a CorBreakpoint that already matches this location
                    // don't add a new one, or the debugger will stop twice when it's hit.
                    // Note that CorFunction instances are 1:1 with a specific function in a
                    // specific module and AppDomain (but represents all generic instantiations).
                    if (cb.Function == func.CorFunction && cb.Offset == ILoffset)
                    {
                        return(false);
                    }
                }
            }

            // Use the resolved information to get a raw CorBreakpoint object.
            CorBreakpoint breakpoint = null;

            try
            {
                if (ILoffset == 0)
                {
                    breakpoint = func.CorFunction.CreateBreakpoint();
                }
                else
                {
                    // we need to set a breakpoint on code rather than directly on function
                    CorCode code = func.CorFunction.ILCode;

                    if (code == null)
                    {
                        throw new MDbgException(String.Format(CultureInfo.InvariantCulture, "IL Code for function {0} is null", new Object[] { func.FullName }));
                    }
                    breakpoint = code.CreateBreakpoint(ILoffset);
                }
            }
            catch (COMException)
            {
                return(false);
            }

            // Add the new CorBreakpoint object to our internal list and register a handler for it.
            Debug.Assert(breakpoint != null);
            breakpoint.Activate(true);
            if (m_breakpoints == null)
            {
                m_breakpoints = new ArrayList();
            }
            m_breakpoints.Add(breakpoint);

            MDbgProcess p = managedModule.Process;
            CustomBreakpointEventHandler handler = new CustomBreakpointEventHandler(this.InternalOnHitHandler);

            p.RegisterCustomBreakpoint(breakpoint, handler);

            return(true);
        }