2017-03-02 20:33:25 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2018-06-07 22:38:45 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-03-02 20:33:25 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Debugger
|
|
|
|
|
{
|
|
|
|
|
class WatchManager
|
|
|
|
|
{
|
|
|
|
|
public static event EventHandler WatchChanged;
|
|
|
|
|
private static List<string> _watchEntries = new List<string>();
|
|
|
|
|
private static List<WatchValueInfo> _previousValues = new List<WatchValueInfo>();
|
2018-06-09 15:43:14 -04:00
|
|
|
|
private static Regex _arrayWatchRegex = new Regex(@"\[((\$[0-9A-Fa-f]+)|(\d+)|([@_a-zA-Z0-9]+))\s*,\s*(\d+)\]", RegexOptions.Compiled);
|
2017-03-02 20:33:25 -05:00
|
|
|
|
|
|
|
|
|
public static List<string> WatchEntries
|
|
|
|
|
{
|
|
|
|
|
get { return _watchEntries; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-08-14 23:44:01 -04:00
|
|
|
|
_watchEntries = new List<string>(value);
|
2017-03-02 20:33:25 -05:00
|
|
|
|
WatchChanged?.Invoke(null, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<WatchValueInfo> GetWatchContent(bool useHex)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<WatchValueInfo>();
|
|
|
|
|
for(int i = 0; i < _watchEntries.Count; i++) {
|
2018-12-10 19:30:09 -05:00
|
|
|
|
string expression = _watchEntries[i].Trim();
|
2017-03-02 20:33:25 -05:00
|
|
|
|
string newValue = "";
|
|
|
|
|
EvalResultType resultType;
|
2018-06-07 22:38:45 -04:00
|
|
|
|
|
2017-03-02 20:33:25 -05:00
|
|
|
|
bool forceHasChanged = false;
|
2018-06-07 22:38:45 -04:00
|
|
|
|
Match match = _arrayWatchRegex.Match(expression);
|
|
|
|
|
if(match.Success) {
|
|
|
|
|
//Watch expression matches the array display syntax (e.g: [$300,10] = display 10 bytes starting from $300)
|
|
|
|
|
newValue = ProcessArrayDisplaySyntax(useHex, ref forceHasChanged, match);
|
|
|
|
|
} else {
|
2018-08-02 20:44:48 -04:00
|
|
|
|
Int32 result = InteropEmu.DebugEvaluateExpression(expression, out resultType, true);
|
2018-06-07 22:38:45 -04:00
|
|
|
|
switch(resultType) {
|
2018-12-10 19:30:09 -05:00
|
|
|
|
case EvalResultType.Numeric:
|
|
|
|
|
//When using {$00} syntax to show the value of a word, always display 4 hex characters.
|
|
|
|
|
bool displayAsWord = expression.StartsWith("{") && expression.EndsWith("}");
|
|
|
|
|
newValue = useHex ? ("$" + result.ToString(displayAsWord ? "X4" : "X2")) : result.ToString();
|
|
|
|
|
break;
|
|
|
|
|
|
2018-06-07 22:38:45 -04:00
|
|
|
|
case EvalResultType.Boolean: newValue = result == 0 ? "false" : "true"; break;
|
|
|
|
|
case EvalResultType.Invalid: newValue = "<invalid expression>"; forceHasChanged = true; break;
|
|
|
|
|
case EvalResultType.DivideBy0: newValue = "<division by zero>"; forceHasChanged = true; break;
|
|
|
|
|
}
|
2017-03-02 20:33:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.Add(new WatchValueInfo() { Expression = expression, Value = newValue, HasChanged = forceHasChanged || (i < _previousValues.Count ? (_previousValues[i].Value != newValue) : false) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_previousValues = list;
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 22:38:45 -04:00
|
|
|
|
private static string ProcessArrayDisplaySyntax(bool useHex, ref bool forceHasChanged, Match match)
|
|
|
|
|
{
|
|
|
|
|
string newValue;
|
|
|
|
|
int address;
|
|
|
|
|
if(match.Groups[2].Value.Length > 0) {
|
|
|
|
|
address = int.Parse(match.Groups[2].Value.Substring(1), System.Globalization.NumberStyles.HexNumber);
|
|
|
|
|
} else if(match.Groups[3].Value.Length > 0) {
|
|
|
|
|
address = int.Parse(match.Groups[3].Value);
|
|
|
|
|
} else {
|
|
|
|
|
CodeLabel label = LabelManager.GetLabel(match.Groups[4].Value);
|
2018-06-09 15:43:14 -04:00
|
|
|
|
if(label == null) {
|
|
|
|
|
forceHasChanged = true;
|
|
|
|
|
return "<invalid label>";
|
|
|
|
|
}
|
2018-06-07 22:38:45 -04:00
|
|
|
|
address = label.GetRelativeAddress();
|
|
|
|
|
}
|
|
|
|
|
int elemCount = int.Parse(match.Groups[5].Value);
|
|
|
|
|
|
|
|
|
|
if(address >= 0) {
|
|
|
|
|
List<string> values = new List<string>(elemCount);
|
|
|
|
|
for(int j = address, end = address + elemCount; j < end; j++) {
|
|
|
|
|
int memValue = InteropEmu.DebugGetMemoryValue(DebugMemoryType.CpuMemory, (uint)j);
|
|
|
|
|
values.Add(useHex ? memValue.ToString("X2") : memValue.ToString());
|
|
|
|
|
}
|
|
|
|
|
newValue = string.Join(" ", values);
|
|
|
|
|
} else {
|
|
|
|
|
newValue = "<label out of scope>";
|
|
|
|
|
forceHasChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 20:33:25 -05:00
|
|
|
|
public static void AddWatch(params string[] expressions)
|
|
|
|
|
{
|
|
|
|
|
foreach(string expression in expressions) {
|
|
|
|
|
_watchEntries.Add(expression);
|
|
|
|
|
}
|
|
|
|
|
WatchChanged?.Invoke(null, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void UpdateWatch(int index, string expression)
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(expression)) {
|
|
|
|
|
RemoveWatch(index);
|
|
|
|
|
} else {
|
|
|
|
|
if(index >= _watchEntries.Count) {
|
|
|
|
|
_watchEntries.Add(expression);
|
|
|
|
|
} else {
|
|
|
|
|
_watchEntries[index] = expression;
|
|
|
|
|
}
|
|
|
|
|
WatchChanged?.Invoke(null, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveWatch(params int[] indexes)
|
|
|
|
|
{
|
|
|
|
|
HashSet<int> set = new HashSet<int>(indexes);
|
|
|
|
|
_watchEntries = _watchEntries.Where((el, index) => !set.Contains(index)).ToList();
|
|
|
|
|
_previousValues = _previousValues.Where((el, index) => !set.Contains(index)).ToList();
|
|
|
|
|
WatchChanged?.Invoke(null, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class WatchValueInfo
|
|
|
|
|
{
|
|
|
|
|
public string Expression { get; set; }
|
|
|
|
|
public string Value { get; set; }
|
|
|
|
|
public bool HasChanged { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|