private void UpdateResults() { string searchString = txtSearch.Text.Trim(); List <string> searchStrings = new List <string>(); searchStrings.Add(searchString.ToLower()); searchStrings.AddRange(searchString.ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); for (int i = 0; i < searchString.Length; i++) { char ch = searchString[i]; if (ch >= 'A' && ch <= 'Z') { searchString = searchString.Remove(i, 1).Insert(i, " " + (char)(ch + 'a' - 'A')); } } searchStrings.AddRange(searchString.ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); searchStrings = searchStrings.Distinct().ToList(); _resultCount = 0; HashSet <int> entryPoints = new HashSet <int>(InteropEmu.DebugGetFunctionEntryPoints()); byte[] cdlData = InteropEmu.DebugGetPrgCdlData(); List <SearchResultInfo> searchResults = new List <SearchResultInfo>(); if (!string.IsNullOrWhiteSpace(searchString)) { if (_symbolProvider != null) { if (_showFilesAndConstants) { foreach (Ld65DbgImporter.FileInfo file in _symbolProvider.Files.Values) { if (Contains(file.Name, searchStrings)) { searchResults.Add(new SearchResultInfo() { Caption = Path.GetFileName(file.Name), AbsoluteAddress = -1, MemoryType = AddressType.InternalRam, SearchResultType = SearchResultType.File, Filename = file.Name, FileLineNumber = 0, RelativeAddress = -1, CodeLabel = null }); } } } foreach (Ld65DbgImporter.SymbolInfo symbol in _symbolProvider.GetSymbols()) { if (Contains(symbol.Name, searchStrings)) { Ld65DbgImporter.ReferenceInfo def = _symbolProvider.GetSymbolDefinition(symbol); AddressTypeInfo addressInfo = _symbolProvider.GetSymbolAddressInfo(symbol); int value = 0; int relAddress = -1; bool isConstant = addressInfo == null; if (!_showFilesAndConstants && isConstant) { continue; } if (addressInfo != null) { value = InteropEmu.DebugGetMemoryValue(addressInfo.Type.ToMemoryType(), (uint)addressInfo.Address); relAddress = InteropEmu.DebugGetRelativeAddress((uint)addressInfo.Address, addressInfo.Type); } else { //For constants, the address field contains the constant's value value = symbol.Address ?? 0; } SearchResultType resultType = SearchResultType.Data; if (addressInfo?.Type == AddressType.PrgRom && entryPoints.Contains(addressInfo.Address)) { resultType = SearchResultType.Function; } else if (addressInfo?.Type == AddressType.PrgRom && addressInfo.Address < cdlData.Length && (cdlData[addressInfo.Address] & (byte)CdlPrgFlags.JumpTarget) != 0) { resultType = SearchResultType.JumpTarget; } else if (isConstant) { resultType = SearchResultType.Constant; } searchResults.Add(new SearchResultInfo() { Caption = symbol.Name, AbsoluteAddress = addressInfo?.Address ?? -1, MemoryType = addressInfo?.Type ?? AddressType.InternalRam, SearchResultType = resultType, Value = value, Filename = def?.FileName ?? "", FileLineNumber = def?.LineNumber ?? 0, RelativeAddress = relAddress, CodeLabel = LabelManager.GetLabel(symbol.Name) }); } } } else { foreach (CodeLabel label in LabelManager.GetLabels()) { if (Contains(label.Label, searchStrings)) { SearchResultType resultType = SearchResultType.Data; if (label.AddressType == AddressType.PrgRom && entryPoints.Contains((int)label.Address)) { resultType = SearchResultType.Function; } else if (label.AddressType == AddressType.PrgRom && label.Address < cdlData.Length && (cdlData[label.Address] & (byte)CdlPrgFlags.JumpTarget) != 0) { resultType = SearchResultType.JumpTarget; } int relativeAddress = label.GetRelativeAddress(); searchResults.Add(new SearchResultInfo() { Caption = label.Label, AbsoluteAddress = (int)label.Address, Value = label.GetValue(), MemoryType = label.AddressType, SearchResultType = resultType, Filename = "", Disabled = !_allowOutOfScope && relativeAddress < 0, RelativeAddress = relativeAddress, CodeLabel = label }); } } } } searchResults.Sort((SearchResultInfo a, SearchResultInfo b) => { int comparison = a.Disabled.CompareTo(b.Disabled); if (comparison == 0) { bool aStartsWithSearch = a.Caption.StartsWith(searchString, StringComparison.InvariantCultureIgnoreCase); bool bStartsWithSearch = b.Caption.StartsWith(searchString, StringComparison.InvariantCultureIgnoreCase); comparison = bStartsWithSearch.CompareTo(aStartsWithSearch); if (comparison == 0) { comparison = a.Caption.CompareTo(b.Caption); } } return(comparison); }); _resultCount = Math.Min(searchResults.Count, MaxResultCount); SelectedResult = 0; if (searchResults.Count == 0) { searchResults.Add(new SearchResultInfo() { Caption = "No results found.", AbsoluteAddress = -1 }); pnlResults.BackColor = SystemColors.ControlLight; } else { pnlResults.BackColor = SystemColors.ControlDarkDark; } if (Program.IsMono) { pnlResults.Visible = false; } else { //Suspend layout causes a crash on Mono tlpResults.SuspendLayout(); } for (int i = 0; i < _resultCount; i++) { _results[i].Initialize(searchResults[i]); _results[i].Tag = searchResults[i]; _results[i].Visible = true; } for (int i = searchResults.Count; i < MaxResultCount; i++) { _results[i].Visible = false; } pnlResults.VerticalScroll.Value = 0; tlpResults.Height = (_results[0].Height + 1) * _resultCount; pnlResults.ResumeLayout(); if (Program.IsMono) { pnlResults.Visible = true; tlpResults.Width = pnlResults.ClientSize.Width - 17; } else { tlpResults.ResumeLayout(); tlpResults.Width = pnlResults.ClientSize.Width - 1; } }
public void UpdateFunctionList(bool reset) { if (reset) { _listItems.Clear(); _functions.Clear(); } Font italicFont = null; Font regularFont = null; Int32[] entryPoints = InteropEmu.DebugGetFunctionEntryPoints(); for (int i = 0; i < entryPoints.Length && entryPoints[i] >= 0; i++) { Int32 entryPoint = entryPoints[i]; ListViewItem item; if (!_functions.TryGetValue(entryPoint, out item)) { CodeLabel label = LabelManager.GetLabel((UInt32)entryPoint, AddressType.PrgRom); item = new ListViewItem(label?.Label); item.Tag = label; item.SubItems.Add("[n/a]"); item.SubItems[1].Tag = -1; item.ForeColor = Color.Gray; if (italicFont == null) { italicFont = new Font(item.Font, FontStyle.Italic); } item.Font = italicFont; item.SubItems.Add("$" + entryPoint.ToString("X4")); item.SubItems[2].Tag = entryPoint; _listItems.Add(item); _functions[entryPoint] = item; } Int32 relativeAddress = InteropEmu.DebugGetRelativeAddress((UInt32)entryPoint, AddressType.PrgRom); if (relativeAddress != (Int32)item.SubItems[1].Tag) { if (relativeAddress >= 0) { item.SubItems[1].Text = "$" + relativeAddress.ToString("X4"); item.ForeColor = Color.Black; if (regularFont == null) { regularFont = new Font(item.Font, FontStyle.Regular); } item.Font = regularFont; } else { item.SubItems[1].Text = "[n/a]"; item.ForeColor = Color.Gray; if (italicFont == null) { italicFont = new Font(item.Font, FontStyle.Italic); } item.Font = italicFont; } item.SubItems[1].Tag = relativeAddress; } } lstFunctions.BeginUpdate(); _listItems.Sort(CompareFunctions); lstFunctions.VirtualMode = true; lstFunctions.VirtualListSize = _listItems.Count; lstFunctions.EndUpdate(); }