Esempio n. 1
0
        private void FireTargetEvent(TargetEventType type, ResultData curFrame, BreakEvent breakEvent = null)
        {
            UpdateHitCountData();

            TargetEventArgs args = new TargetEventArgs(type);

            if (type != TargetEventType.TargetExited)
            {
                GdbCommandResult res = RunCommand("-stack-info-depth");
                int fcount           = int.Parse(res.GetValue("depth"));

                GdbBacktrace bt = new GdbBacktrace(this, activeThread, fcount, curFrame);
                args.Backtrace  = new Backtrace(bt);
                args.Thread     = GetThread(activeThread);
                args.BreakEvent = breakEvent;
            }

            if (_suppressEvents && type == TargetEventType.TargetStopped)
            {
                args.IsStopEvent = true;
                TargetStoppedWhenSuppressed?.Invoke(this, args);
            }
            else
            {
                OnTargetEvent(args);
            }
        }
Esempio n. 2
0
        public GdbCommandResult RunCommand(string command, int timeout = 30000, params string[] args)
        {
            lock (gdbLock)
            {
                lock (syncLock)
                {
                    lastResult = null;

                    lock (eventLock)
                    {
                        running = true;
                    }

                    if (logGdb)
                    {
                        _console.WriteLine("gdb<: " + command + " " + string.Join(" ", args));
                    }

                    sin.WriteLine(command + " " + string.Join(" ", args));

                    if (!Monitor.Wait(syncLock, timeout))
                    {
                        lastResult        = new GdbCommandResult("");
                        lastResult.Status = CommandStatus.Timeout;
                        throw new TimeoutException();
                    }

                    return(lastResult);
                }
            }
        }
        public override AssemblyLine[] GetLines(long startAddr, long endAddr)
        {
            GdbCommandResult data = null;

            data = session.RunCommand("-data-disassemble", "-s", startAddr.ToString(), "-e", endAddr.ToString(), "--", "0");

            if (data.Status == CommandStatus.Done)
            {
                ResultData ins = data.GetObject("asm_insns");

                AssemblyLine[] alines = new AssemblyLine[ins.Count];

                for (int n = 0; n < ins.Count; n++)
                {
                    ResultData   aline = ins.GetObject(n);
                    long         addr  = long.Parse(aline.GetValue("address").Substring(2), NumberStyles.HexNumber);
                    AssemblyLine line  = new AssemblyLine(addr, aline.GetValue("inst"));
                    alines[n] = line;
                }

                return(alines);
            }
            else
            {
                long           range    = endAddr - startAddr;
                AssemblyLine[] badlines = new AssemblyLine[range];
                for (int n = 0; n < range; n++)
                {
                    badlines[n] = new AssemblyLine(startAddr + n, "Unable to read data.");
                }

                return(badlines);
            }
        }
Esempio n. 4
0
        private bool CheckBreakpoint(int handle)
        {
            BreakEventInfo binfo;

            if (!breakpoints.TryGetValue(handle, out binfo))
            {
                return(true);
            }

            Breakpoint bp = (Breakpoint)binfo.BreakEvent;

            if (!string.IsNullOrEmpty(bp.ConditionExpression) && bp.BreakIfConditionChanges)
            {
                // Update the condition expression
                GdbCommandResult res = RunCommand("-data-evaluate-expression", Escape(bp.ConditionExpression));
                string           val = res.GetValue("value");
                RunCommand("-break-condition", handle.ToString(), "(" + bp.ConditionExpression + ") != " + val);
            }

            if (!string.IsNullOrEmpty(bp.TraceExpression) && bp.HitAction == HitAction.PrintExpression)
            {
                GdbCommandResult res = RunCommand("-data-evaluate-expression", Escape(bp.TraceExpression));
                string           val = res.GetValue("value");
                NotifyBreakEventUpdate(binfo, 0, val);
                return(false);
            }
            return(true);
        }
Esempio n. 5
0
        private ObjectValue CreateVarObject(string exp)
        {
            try
            {
                session.SelectThread(threadId);
                exp = exp.Replace("\"", "\\\"");
                GdbCommandResult res = session.RunCommand("-var-create", "-", "*", "\"" + exp + "\"");

                if (res.Status == CommandStatus.Done)
                {
                    string vname  = res.GetValue("name");
                    var    result = CreateObjectValue(exp, res);

                    session.RegisterTempVariableObject(vname, result);

                    return(result);
                }
                else
                {
                    return(ObjectValue.CreateUnknown(exp));
                }
            }
            catch
            {
                return(ObjectValue.CreateUnknown(exp));
            }
        }
Esempio n. 6
0
        protected override Backtrace OnGetThreadBacktrace(long processId, long threadId)
        {
            ResultData       data = SelectThread(threadId);
            GdbCommandResult res  = RunCommand("-stack-info-depth");
            int          fcount   = int.Parse(res.GetValue("depth"));
            GdbBacktrace bt       = new GdbBacktrace(this, threadId, fcount, data != null ? data.GetObject("frame") : null);

            return(new Backtrace(bt));
        }
Esempio n. 7
0
        private BreakEventInfo OnInsertWatchPoint(BreakEvent be)
        {
            var bi = new BreakEventInfo();
            var wp = be as WatchPoint;

            lock (gdbLock)
            {
                bool dres = InternalStop();
                try
                {
                    string           errorMsg = string.Empty;
                    GdbCommandResult res      = null;

                    try
                    {
                        if (wp.TriggerOnRead && wp.TriggerOnWrite)
                        {
                            res = RunCommand("-break-watch", wp.Expression, "-a");
                        }
                        else if (wp.TriggerOnRead && !wp.TriggerOnWrite)
                        {
                            res = RunCommand("-break-watch", wp.Expression, "-r");
                        }
                        else
                        {
                            res = RunCommand("-break-watch", wp.Expression);
                        }
                    }
                    catch (Exception ex)
                    {
                        errorMsg = ex.Message;
                    }

                    if (res == null || res.Status != CommandStatus.Done)
                    {
                        bi.SetStatus(BreakEventStatus.Invalid, errorMsg);
                        return(bi);
                    }

                    int bh = res.GetObject("wpt").GetInt("number");

                    if (!be.Enabled)
                    {
                        RunCommand("-break-disable", bh.ToString());
                    }

                    breakpoints[bh] = bi;
                    bi.Handle       = bh;
                    bi.SetStatus(BreakEventStatus.Bound, null);
                    return(bi);
                }
                finally
                {
                    InternalResume(dres);
                }
            }
        }
Esempio n. 8
0
        protected override void OnFinish()
        {
            SelectThread(activeThread);
            GdbCommandResult res = RunCommand("-stack-info-depth", "2");

            if (res.GetValue("depth") == "1")
            {
                RunCommand("-exec-continue");
            }
            else
            {
                RunCommand("-stack-select-frame", "0");
                RunCommand("-exec-finish");
            }
        }
Esempio n. 9
0
        protected override AssemblyLine[] OnDisassembleFile(string file)
        {
            List <AssemblyLine> lines = new List <AssemblyLine>();
            int cline = 1;

            do
            {
                GdbCommandResult data = null;
                try
                {
                    data = RunCommand("-data-disassemble", "-f", file, "-l", cline.ToString(), "--", "1");
                }
                catch
                {
                    break;
                }

                int newLine = cline;

                if (data.Status == CommandStatus.Done)
                {
                    ResultData asm_insns = data.GetObject("asm_insns");

                    for (int n = 0; n < asm_insns.Count; n++)
                    {
                        ResultData src_and_asm_line = asm_insns.GetObject(n).GetObject("src_and_asm_line");
                        newLine = src_and_asm_line.GetInt("line");
                        ResultData line_asm_insn = src_and_asm_line.GetObject("line_asm_insn");
                        for (int i = 0; i < line_asm_insn.Count; i++)
                        {
                            ResultData asm  = line_asm_insn.GetObject(i);
                            long       addr = long.Parse(asm.GetValue("address").Substring(2), NumberStyles.HexNumber);
                            string     code = asm.GetValue("inst");
                            lines.Add(new AssemblyLine(addr, code, newLine));
                        }
                    }
                }

                if (newLine <= cline)
                {
                    break;
                }

                cline = newLine + 1;
            }while (true);

            return(lines.ToArray());
        }
Esempio n. 10
0
        public ObjectValue[] GetChildren(ObjectPath path, int index, int count, EvaluationOptions options)
        {
            List <ObjectValue> children = new List <ObjectValue>();

            session.SelectThread(threadId);
            GdbCommandResult res   = session.RunCommand("-var-list-children", "2", path.Join("."));
            ResultData       cdata = res.GetObject("children");

            // The response may not contain the "children" list at all.
            if (cdata == null)
            {
                return(children.ToArray());
            }

            if (index == -1)
            {
                index = 0;
                count = cdata.Count;
            }

            for (int n = index; n < cdata.Count && n < index + count; n++)
            {
                ResultData data  = cdata.GetObject(n);
                ResultData child = data.GetObject("child");

                string name = child.GetValue("exp");
                if (name.Length > 0 && char.IsNumber(name[0]))
                {
                    name = "[" + name + "]";
                }

                // C++ structures may contain typeless children named
                // "public", "private" and "protected".
                if (child.GetValue("type") == null)
                {
                    ObjectPath    childPath   = new ObjectPath(child.GetValue("name").Split('.'));
                    ObjectValue[] subchildren = GetChildren(childPath, -1, -1, options);
                    children.AddRange(subchildren);
                }
                else
                {
                    ObjectValue val = CreateObjectValue(name, child);
                    children.Add(val);
                }
            }
            return(children.ToArray());
        }
Esempio n. 11
0
 private void UpdateHitCountData()
 {
     foreach (BreakEventInfo bp in breakpointsWithHitCount)
     {
         GdbCommandResult res = RunCommand("-break-info", bp.Handle.ToString());
         string           val = res.GetObject("BreakpointTable").GetObject("body").GetObject(0).GetObject("bkpt").GetValue("ignore");
         if (val != null)
         {
             NotifyBreakEventUpdate(bp, int.Parse(val), null);
         }
         else
         {
             NotifyBreakEventUpdate(bp, 0, null);
         }
     }
     breakpointsWithHitCount.Clear();
 }
Esempio n. 12
0
        public ObjectValue[] GetParameters(int frameIndex, EvaluationOptions options)
        {
            List <ObjectValue> values = new List <ObjectValue>();

            SelectFrame(frameIndex);
            GdbCommandResult res = session.RunCommand("-stack-list-arguments", "0", frameIndex.ToString(), frameIndex.ToString());

            if (res.Status == CommandStatus.Done)
            {
                foreach (ResultData data in res.GetObject("stack-args").GetObject(0).GetObject("frame").GetObject("args"))
                {
                    values.Add(CreateVarObject(data.GetValue("name")));
                }
            }

            return(values.ToArray());
        }
Esempio n. 13
0
        public ObjectValue[] GetLocalVariables(int frameIndex, EvaluationOptions options)
        {
            List <ObjectValue> values = new List <ObjectValue>();

            SelectFrame(frameIndex);

            GdbCommandResult res = session.RunCommand("-stack-list-locals", "0");

            if (res.Status == CommandStatus.Done)
            {
                foreach (ResultData data in res.GetObject("locals"))
                {
                    values.Add(CreateVarObject(data.GetValue("name")));
                }
            }

            return(values.ToArray());
        }
Esempio n. 14
0
        public StackFrame[] GetStackFrames(int firstIndex, int lastIndex)
        {
            List <StackFrame> frames = new List <StackFrame>();

            if (firstIndex == 0 && firstFrame != null)
            {
                frames.Add(firstFrame);
                firstIndex++;
            }

            if (lastIndex >= fcount)
            {
                lastIndex = fcount - 1;
            }

            if (firstIndex > lastIndex)
            {
                return(frames.ToArray());
            }

            session.SelectThread(threadId);

            GdbCommandResult res = session.RunCommand("-stack-list-frames", firstIndex.ToString(), lastIndex.ToString());

            if (res.Status == CommandStatus.Done)
            {
                ResultData stack = res.GetObject("stack");
                for (int n = 0; n < stack.Count; n++)
                {
                    ResultData frd = stack.GetObject(n);
                    frames.Add(CreateFrame(frd.GetObject("frame")));
                }
            }

            return(frames.ToArray());
        }
Esempio n. 15
0
        private BreakEventInfo OnInsertBreakPoint(BreakEvent be)
        {
            var bi = new BreakEventInfo();
            var bp = be as Breakpoint;

            lock (gdbLock)
            {
                bool dres = InternalStop();
                try
                {
                    string extraCmd = string.Empty;
                    if (bp.HitCount > 0)
                    {
                        extraCmd += "-i " + bp.HitCount;
                        breakpointsWithHitCount.Add(bi);
                    }
                    if (!string.IsNullOrEmpty(bp.ConditionExpression))
                    {
                        if (!bp.BreakIfConditionChanges)
                        {
                            extraCmd += " -c " + bp.ConditionExpression;
                        }
                    }

                    GdbCommandResult res      = null;
                    string           errorMsg = null;

                    if (bp is FunctionBreakpoint)
                    {
                        try
                        {
                            res = RunCommand("-break-insert", extraCmd.Trim(), ((FunctionBreakpoint)bp).FunctionName);
                        }
                        catch (Exception ex)
                        {
                            errorMsg = ex.Message;
                        }
                    }
                    else
                    {
                        // Breakpoint locations must be double-quoted if files contain spaces.
                        // For example: -break-insert "\"C:/Documents and Settings/foo.c\":17"
                        RunCommand("-environment-directory", Escape(Path.GetDirectoryName(bp.FileName).ToAvalonPath()));

                        try
                        {
                            res = RunCommand("-break-insert", extraCmd.Trim(), Escape(Escape(bp.FileName.ToAvalonPath()) + ":" + bp.Line));
                        }
                        catch (Exception ex)
                        {
                            errorMsg = ex.Message;
                        }

                        if (res == null)
                        {
                            try
                            {
                                res = RunCommand("-break-insert", extraCmd.Trim(), Escape(Escape(Path.GetFileName(bp.FileName)) + ":" + bp.Line));
                            }
                            catch
                            {
                                // Ignore
                            }
                        }
                    }

                    if (res == null || res.Status != CommandStatus.Done)
                    {
                        bi.SetStatus(BreakEventStatus.Invalid, errorMsg);
                        return(bi);
                    }
                    int bh = res.GetObject("bkpt").GetInt("number");
                    if (!be.Enabled)
                    {
                        RunCommand("-break-disable", bh.ToString());
                    }
                    breakpoints[bh] = bi;
                    bi.Handle       = bh;
                    bi.SetStatus(BreakEventStatus.Bound, null);
                    return(bi);
                }
                finally
                {
                    InternalResume(dres);
                }
            }
        }
Esempio n. 16
0
        private void ProcessOutput(string line)
        {
            if (logGdb)
            {
                _console.WriteLine("dbg>: '" + line + "'");
            }
            switch (line[0])
            {
            case '^':
                lock (syncLock)
                {
                    lastResult = new GdbCommandResult(line);

                    lock (eventLock)
                    {
                        running = lastResult.Status == CommandStatus.Running;
                    }

                    Monitor.PulseAll(syncLock);
                }
                break;

            case '~':
            case '&':
                if (line.Length > 1 && line[1] == '"')
                {
                    line = line.Substring(2, line.Length - 5);
                }
                ThreadPool.QueueUserWorkItem(delegate
                {
                    OnTargetOutput(false, line + "\n");
                });
                break;

            case '*':
                GdbEvent ev = null;
                lock (eventLock)
                {
                    if (!line.StartsWith("*running"))
                    {
                        running = false;
                        ev      = new GdbEvent(line);
                        string ti = ev.GetValue("thread-id");
                        if (ti != null && ti != "all")
                        {
                            currentThread = activeThread = int.Parse(ti);
                        }
                        Monitor.PulseAll(eventLock);
                        if (internalStop)
                        {
                            internalStop = false;
                            return;
                        }
                    }
                }

                if (ev != null)
                {
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        try
                        {
                            HandleEvent(ev);
                        }
                        catch (Exception ex)
                        {
                            _console.WriteLine(ex.ToString());
                        }
                    });
                }
                break;
            }
        }
Esempio n. 17
0
        public CompletionData GetExpressionCompletionData(int frameIndex, string exp)
        {
            SelectFrame(frameIndex);

            bool pointer = exp.EndsWith("->");
            int  i;

            if (pointer || exp.EndsWith("."))
            {
                exp = exp.Substring(0, exp.Length - (pointer ? 2 : 1));
                i   = 0;
                while (i < exp.Length)
                {
                    ObjectValue val = CreateVarObject(exp);
                    if (!val.IsUnknown && !val.IsError)
                    {
                        CompletionData data = new CompletionData();
                        foreach (ObjectValue cv in val.GetAllChildren())
                        {
                            data.Items.Add(new CompletionItem(cv.Name, cv.Flags));
                        }
                        data.ExpressionLength = 0;
                        return(data);
                    }
                    i++;
                }
                return(null);
            }

            i = exp.Length - 1;
            bool lastWastLetter = false;

            while (i >= 0)
            {
                char c = exp[i--];
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    break;
                }
                lastWastLetter = !char.IsDigit(c);
            }

            if (lastWastLetter)
            {
                string partialWord = exp.Substring(i + 1);

                CompletionData cdata = new CompletionData();
                cdata.ExpressionLength = partialWord.Length;

                // Local variables
                GdbCommandResult res = session.RunCommand("-stack-list-locals", "0");
                foreach (ResultData data in res.GetObject("locals"))
                {
                    string name = data.GetValue("name");
                    if (name.StartsWith(partialWord))
                    {
                        cdata.Items.Add(new CompletionItem(name, ObjectValueFlags.Variable));
                    }
                }

                // Parameters
                res = session.RunCommand("-stack-list-arguments", "0", frameIndex.ToString(), frameIndex.ToString());
                foreach (ResultData data in res.GetObject("stack-args").GetObject(0).GetObject("frame").GetObject("args"))
                {
                    string name = data.GetValue("name");
                    if (name.StartsWith(partialWord))
                    {
                        cdata.Items.Add(new CompletionItem(name, ObjectValueFlags.Parameter));
                    }
                }

                if (cdata.Items.Count > 0)
                {
                    return(cdata);
                }
            }
            return(null);
        }