Debugger: Refactor DBG file auto-load logic

This commit is contained in:
Sour 2019-01-01 14:08:07 -05:00
parent 299ab97cf1
commit d8008093b7
10 changed files with 160 additions and 129 deletions

View file

@ -421,10 +421,15 @@ namespace Mesen.GUI.Debugger.Controls
private void mnuSwitchView_Click(object sender, EventArgs e)
{
if(Viewer.SymbolProvider != null) {
this.OnSwitchView?.Invoke(Viewer);
this.SwitchView();
}
}
public void SwitchView()
{
this.OnSwitchView?.Invoke(Viewer);
}
public void ProcessMouseUp(Point location, MouseButtons button)
{
if(UpdateContextMenu(location)) {

View file

@ -20,7 +20,7 @@
_codeViewerActions.Dispose();
_codeViewerActions = null;
}
DebugWorkspaceManager.SymbolProviderChanged -= UpdateSymbolProvider;
base.Dispose(disposing);
}

View file

@ -38,9 +38,17 @@ namespace Mesen.GUI.Debugger
ctrlFindOccurrences.Viewer = this;
splitContainer.Panel2Collapsed = true;
this.SymbolProvider = DebugWorkspaceManager.SymbolProvider;
DebugWorkspaceManager.SymbolProviderChanged += UpdateSymbolProvider;
}
}
private void UpdateSymbolProvider(Ld65DbgImporter symbolProvider)
{
this.SymbolProvider = symbolProvider;
}
public void SetConfig(DebugViewInfo config, bool disableActions = false)
{
_config = config;

View file

@ -20,6 +20,7 @@
_codeViewerActions.Dispose();
_codeViewerActions = null;
}
DebugWorkspaceManager.SymbolProviderChanged -= UpdateSymbolProvider;
base.Dispose(disposing);
}

View file

@ -33,6 +33,17 @@ namespace Mesen.GUI.Debugger.Controls
base.OnLoad(e);
if(!IsDesignMode) {
_codeViewerActions = new CodeViewerActions(this, true);
this.SymbolProvider = DebugWorkspaceManager.SymbolProvider;
DebugWorkspaceManager.SymbolProviderChanged += UpdateSymbolProvider;
}
}
private void UpdateSymbolProvider(Ld65DbgImporter symbolProvider)
{
this.SymbolProvider = symbolProvider;
if(symbolProvider == null && this.Visible) {
_codeViewerActions.SwitchView();
}
}
@ -341,7 +352,7 @@ namespace Mesen.GUI.Debugger.Controls
public void ScrollToAddress(AddressTypeInfo addressInfo, bool scrollToTop = false)
{
if(addressInfo.Address >= 0 && addressInfo.Type == AddressType.PrgRom) {
LineInfo line = _symbolProvider.GetSourceCodeLineInfo(addressInfo.Address);
LineInfo line = _symbolProvider?.GetSourceCodeLineInfo(addressInfo.Address);
if(line != null) {
foreach(Ld65DbgImporter.FileInfo fileInfo in cboFile.Items) {
if(fileInfo.ID == line.FileID) {
@ -363,7 +374,7 @@ namespace Mesen.GUI.Debugger.Controls
AddressTypeInfo addressInfo = new AddressTypeInfo();
InteropEmu.DebugGetAbsoluteAddressAndType((uint)cpuAddress, addressInfo);
if(addressInfo.Address >= 0 && addressInfo.Type == AddressType.PrgRom) {
LineInfo line = _symbolProvider.GetSourceCodeLineInfo(addressInfo.Address);
LineInfo line = _symbolProvider?.GetSourceCodeLineInfo(addressInfo.Address);
return CurrentFile.ID == line?.FileID;
}
return false;
@ -426,7 +437,7 @@ namespace Mesen.GUI.Debugger.Controls
)
);
int prgAddress = _viewer._symbolProvider.GetPrgAddress(_viewer.CurrentFile.ID, lineIndex);
int prgAddress = _viewer._symbolProvider?.GetPrgAddress(_viewer.CurrentFile.ID, lineIndex) ?? -1;
if(prgAddress >= 0) {
AddressTypeInfo addressInfo = new AddressTypeInfo();

View file

@ -55,6 +55,8 @@ namespace Mesen.GUI.Debugger
public Dictionary<int, FileInfo> Files { get { return _files; } }
public DateTime DbgFileStamp { get; private set; }
public int GetPrgAddress(int fileID, int lineIndex)
{
int prgAddress;
@ -216,9 +218,14 @@ namespace Mesen.GUI.Debugger
{
Match match = _fileRegex.Match(row);
if(match.Success) {
string filename = Path.GetFullPath(Path.Combine(basePath, match.Groups[2].Value.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar))).Replace(basePath + Path.DirectorySeparatorChar, "");
string ext = Path.GetExtension(filename).ToLower();
bool isAsm = ext != ".c" && ext != ".h";
FileInfo file = new FileInfo() {
ID = Int32.Parse(match.Groups[1].Value),
Name = Path.GetFullPath(Path.Combine(basePath, match.Groups[2].Value.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar))).Replace(basePath + Path.DirectorySeparatorChar, "")
Name = filename,
IsAssembly = isAsm
};
_files.Add(file.ID, file);
@ -427,8 +434,7 @@ namespace Mesen.GUI.Debugger
continue;
}
string ext = Path.GetExtension(_files[line.FileID].Name).ToLower();
bool isAsm = ext != ".c" && ext != ".h";
bool isAsm = _files[line.FileID].IsAssembly;
string comment = "";
for(int i = line.LineNumber; i >= 0; i--) {
@ -515,6 +521,7 @@ namespace Mesen.GUI.Debugger
}
}
DbgFileStamp = File.GetLastWriteTime(path);
string[] fileRows = File.ReadAllLines(path);
string basePath = Path.GetDirectoryName(path);
@ -599,7 +606,7 @@ namespace Mesen.GUI.Debugger
labels.AddRange(_saveRamLabels.Values);
labelCount += _ramLabels.Count + _workRamLabels.Count + _saveRamLabels.Count;
}
LabelManager.SetLabels(labels);
LabelManager.SetLabels(labels, !silent);
if(!silent) {
if(_errorCount > 0) {
@ -635,6 +642,7 @@ namespace Mesen.GUI.Debugger
public int ID;
public string Name;
public string[] Data;
public bool IsAssembly;
public override string ToString()
{

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,9 +11,25 @@ namespace Mesen.GUI.Debugger
public class DebugWorkspaceManager
{
private static DebugWorkspace _workspace;
private static Ld65DbgImporter _symbolProvider;
private static string _romName;
private static object _lock = new object();
public delegate void SymbolProviderChangedHandler(Ld65DbgImporter provider);
public static event SymbolProviderChangedHandler SymbolProviderChanged;
public static Ld65DbgImporter SymbolProvider
{
get { return _symbolProvider; }
private set
{
if(_symbolProvider != value) {
_symbolProvider = value;
SymbolProviderChanged?.Invoke(value);
}
}
}
public static void SaveWorkspace()
{
if(_workspace != null) {
@ -32,6 +49,7 @@ namespace Mesen.GUI.Debugger
lock(_lock) {
_workspace = null;
_romName = null;
SymbolProvider = null;
}
}
@ -53,41 +71,11 @@ namespace Mesen.GUI.Debugger
}
}
public static void SetupWorkspace(bool saveCurrentWorkspace = true)
{
string romName = InteropEmu.GetRomInfo().GetRomName();
lock(_lock) {
if(_workspace != null && _romName == romName) {
if(saveCurrentWorkspace) {
SaveWorkspace();
}
//Setup labels
if(_workspace.Labels.Count == 0) {
LabelManager.ResetLabels();
if(!ConfigManager.Config.DebugInfo.DisableDefaultLabels) {
LabelManager.SetDefaultLabels(InteropEmu.GetRomInfo().MapperId);
}
} else {
LabelManager.ResetLabels();
LabelManager.SetLabels(_workspace.Labels, false);
}
//Load watch entries
WatchManager.WatchEntries = _workspace.WatchValues;
//Load breakpoints
BreakpointManager.SetBreakpoints(_workspace.Breakpoints);
} else {
Clear();
}
}
}
public static DebugWorkspace GetWorkspace()
{
string romName = InteropEmu.GetRomInfo().GetRomName();
if(_workspace == null || _romName != romName) {
SymbolProvider = null;
lock(_lock) {
if(_workspace == null || _romName != romName) {
if(_workspace != null) {
@ -95,7 +83,23 @@ namespace Mesen.GUI.Debugger
}
_romName = InteropEmu.GetRomInfo().GetRomName();
_workspace = DebugWorkspace.GetWorkspace();
SetupWorkspace(false);
//Setup labels
if(_workspace.Labels.Count == 0) {
LabelManager.ResetLabels();
if(!ConfigManager.Config.DebugInfo.DisableDefaultLabels) {
LabelManager.SetDefaultLabels(InteropEmu.GetRomInfo().MapperId);
}
} else {
LabelManager.ResetLabels();
LabelManager.SetLabels(_workspace.Labels, true);
}
//Load watch entries
WatchManager.WatchEntries = _workspace.WatchValues;
//Load breakpoints
BreakpointManager.SetBreakpoints(_workspace.Breakpoints);
}
}
}
@ -118,5 +122,76 @@ namespace Mesen.GUI.Debugger
}
InteropEmu.DebugSetFlags(_flags);
}
public static void ResetLabels()
{
LabelManager.ResetLabels();
if(!ConfigManager.Config.DebugInfo.DisableDefaultLabels) {
LabelManager.SetDefaultLabels(InteropEmu.GetRomInfo().MapperId);
}
SaveWorkspace();
GetWorkspace();
}
public static void AutoLoadDbgFiles(bool silent)
{
Ld65DbgImporter oldSymbolProvider = SymbolProvider;
if(ConfigManager.Config.DebugInfo.AutoLoadDbgFiles) {
RomInfo info = InteropEmu.GetRomInfo();
string dbgPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".dbg");
if(File.Exists(dbgPath)) {
DateTime lastDbgUpdate = File.GetLastWriteTime(dbgPath);
if(lastDbgUpdate != oldSymbolProvider?.DbgFileStamp) {
ImportDbgFile(dbgPath, silent);
} else {
//Currently loaded symbol provider is still valid
return;
}
} else {
string mlbPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".mlb");
if(File.Exists(mlbPath)) {
ImportMlbFile(mlbPath, silent);
} else {
string fnsPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".fns");
if(File.Exists(fnsPath)) {
ImportNesasmFnsFile(fnsPath, silent);
}
}
}
}
if(oldSymbolProvider == SymbolProvider) {
SymbolProvider = null;
}
}
public static void ImportNesasmFnsFile(string fnsPath, bool silent = false)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
NesasmFnsImporter.Import(fnsPath, silent);
}
public static void ImportMlbFile(string mlbPath, bool silent = false)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
MesenLabelFile.Import(mlbPath, silent);
}
public static void ImportDbgFile(string dbgPath, bool silent)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
Ld65DbgImporter dbgImporter = new Ld65DbgImporter();
dbgImporter.Import(dbgPath, silent);
DebugWorkspaceManager.SymbolProvider = dbgImporter;
}
}
}

View file

@ -70,7 +70,7 @@ namespace Mesen.GUI.Debugger
this.UpdateWorkspace();
this.AutoLoadCdlFiles();
this.AutoLoadDbgFiles(true);
DebugWorkspaceManager.AutoLoadDbgFiles(true);
if(!Program.IsMono) {
this.mnuSplitView.Checked = ConfigManager.Config.DebugInfo.SplitView;
@ -373,70 +373,6 @@ namespace Mesen.GUI.Debugger
}
}
private void AutoLoadDbgFiles(bool silent)
{
ctrlSourceViewer.SymbolProvider = null;
ctrlSourceViewerSplit.SymbolProvider = null;
ctrlDebuggerCode.SymbolProvider = null;
ctrlDebuggerCodeSplit.SymbolProvider = null;
if(ConfigManager.Config.DebugInfo.AutoLoadDbgFiles) {
RomInfo info = InteropEmu.GetRomInfo();
string dbgPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".dbg");
if(File.Exists(dbgPath)) {
ImportDbgFile(dbgPath, silent);
} else {
string mlbPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".mlb");
if(File.Exists(mlbPath)) {
ImportMlbFile(mlbPath, silent);
} else {
string fnsPath = Path.Combine(info.RomFile.Folder, info.GetRomName() + ".fns");
if(File.Exists(fnsPath)) {
ImportNesasmFnsFile(fnsPath, silent);
}
}
}
}
if(ctrlSourceViewer.SymbolProvider == null) {
ctrlSourceViewer.Visible = false;
ctrlSourceViewerSplit.Visible = false;
ctrlDebuggerCode.Visible = true;
ctrlDebuggerCodeSplit.Visible = true;
ctrlDebuggerCode.Focus();
}
}
private void ImportNesasmFnsFile(string fnsPath, bool silent = false)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
NesasmFnsImporter.Import(fnsPath, silent);
}
private void ImportMlbFile(string mlbPath, bool silent = false)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
MesenLabelFile.Import(mlbPath, silent);
}
private void ImportDbgFile(string dbgPath, bool silent)
{
if(ConfigManager.Config.DebugInfo.ImportConfig.ResetLabelsOnImport) {
ResetLabels();
}
Ld65DbgImporter dbgImporter = new Ld65DbgImporter();
dbgImporter.Import(dbgPath, silent);
ctrlDebuggerCode.SymbolProvider = dbgImporter;
ctrlDebuggerCodeSplit.SymbolProvider = dbgImporter;
ctrlSourceViewer.SymbolProvider = dbgImporter;
ctrlSourceViewerSplit.SymbolProvider = dbgImporter;
}
private void UpdateWorkspace()
{
DebugWorkspaceManager.SaveWorkspace();
@ -580,7 +516,7 @@ namespace Mesen.GUI.Debugger
this.BeginInvoke((MethodInvoker)(() => {
this.UpdateWorkspace();
this.AutoLoadCdlFiles();
this.AutoLoadDbgFiles(true);
DebugWorkspaceManager.AutoLoadDbgFiles(true);
UpdateDebugger(true, false);
if(!breakOnReset) {
@ -1352,19 +1288,11 @@ namespace Mesen.GUI.Debugger
}
}
private void ResetLabels()
{
LabelManager.ResetLabels();
if(!ConfigManager.Config.DebugInfo.DisableDefaultLabels) {
LabelManager.SetDefaultLabels(InteropEmu.GetRomInfo().MapperId);
}
UpdateWorkspace();
}
private void mnuResetLabels_Click(object sender, EventArgs e)
{
if(MessageBox.Show("This operation will reset labels to their default state." + Environment.NewLine + "Are you sure?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) {
ResetLabels();
DebugWorkspaceManager.ResetLabels();
UpdateWorkspace();
UpdateDebugger(false);
}
}
@ -1373,11 +1301,11 @@ namespace Mesen.GUI.Debugger
{
string ext = Path.GetExtension(path).ToLower();
if(ext == ".mlb") {
ImportMlbFile(path);
DebugWorkspaceManager.ImportMlbFile(path);
} else if(ext == ".fns") {
ImportNesasmFnsFile(path);
DebugWorkspaceManager.ImportNesasmFnsFile(path);
} else {
ImportDbgFile(path, false);
DebugWorkspaceManager.ImportDbgFile(path, false);
}
}
@ -1437,7 +1365,7 @@ namespace Mesen.GUI.Debugger
if(_debuggerInitialized) {
ConfigManager.Config.DebugInfo.AutoLoadDbgFiles = mnuAutoLoadDbgFiles.Checked;
ConfigManager.ApplyChanges();
AutoLoadDbgFiles(false);
DebugWorkspaceManager.AutoLoadDbgFiles(false);
}
}
@ -1802,7 +1730,7 @@ namespace Mesen.GUI.Debugger
private void mnuGoToAll_Click(object sender, EventArgs e)
{
using(frmGoToAll frm = new frmGoToAll(_lastCodeWindow.SymbolProvider)) {
using(frmGoToAll frm = new frmGoToAll()) {
if(frm.ShowDialog() == DialogResult.OK) {
frmGoToAll.GoToDestination dest = frm.Destination;

View file

@ -24,11 +24,11 @@ namespace Mesen.GUI.Debugger
public GoToDestination Destination { get; private set; }
public frmGoToAll(Ld65DbgImporter symbolProvider = null)
public frmGoToAll()
{
InitializeComponent();
_symbolProvider = symbolProvider;
_symbolProvider = DebugWorkspaceManager.SymbolProvider;
tlpResults.SuspendLayout();
for(int i = 0; i < MaxResultCount; i++) {

View file

@ -659,11 +659,6 @@ namespace Mesen.GUI.Forms
UpdateViewerSize();
ProcessPostLoadCommandSwitches();
}));
Task.Run(() => {
//If a workspace is already loaded for this game, make sure we setup the labels, watch, etc properly
DebugWorkspaceManager.SetupWorkspace();
});
break;
case InteropEmu.ConsoleNotificationType.GameReset: