using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Windows.Forms; using System.Text.RegularExpressions; using UAF.Remoting; namespace MemoryViewer { public partial class TopAllocationViewer : Form { protected Hashtable allocationTable = null; protected enum CaptureMode { topAllocation, frameAllocation }; protected CaptureMode captureMode = CaptureMode.topAllocation; public TopAllocationViewer() { InitializeComponent(); comboBoxFilterAlloc.Items.Add("ALL"); foreach (MemoryFile.XALLOC_ALLOCATOR_IDS filter in Enum.GetValues(typeof(MemoryFile.XALLOC_ALLOCATOR_IDS))) { comboBoxFilterAlloc.Items.Add(filter.ToString()); } comboBoxFilterAlloc.SelectedIndex = 0; } private void sendCommand(string _command,params object[] args) { //call back callbackAction cbAction = (callbackAction)Tag; if (cbAction != null) cbAction(GetType().ToString(), _command, args); } private void btnCapture_Click(object sender, EventArgs e) { UInt32 filter = 0xFFFFFFFF; if (comboBoxFilterAlloc.Text!="ALL") { filter = (UInt32)MemoryUtils.findAllocatorId(comboBoxFilterAlloc.Text); } UInt32 count = (UInt32) int.Parse(textBoxcount.Text); if (count == 0 || count > 1000) count = 100; string command = ""; if (captureMode == CaptureMode.frameAllocation) command = "capture_frameallocation"; else command = "capture_topallocation"; sendCommand(command, filter,count); } public void dumpAddress(IBlob _currentBlob) { UInt32 size = 0; UInt32 address = 0; _currentBlob.Extract(out size); _currentBlob.Extract(out address); Byte[] bytes = _currentBlob.ExtractBytes(); bytes = bytes.Select((b) => b == 0 ? (byte)'.' : b).ToArray(); string textDump = System.Text.ASCIIEncoding.ASCII.GetString(bytes); DumpMemory dumpMemory = new DumpMemory(); dumpMemory.fill(ref textDump); dumpMemory.Show(); } public class AllocationInfo { public UInt32 crc; public void addLine(string line) { lines.Add(line); } public void fillText(ref string text) { foreach (string line in lines) { text += line; text += "\r\n"; } } protected List lines = new List(); }; public void dumpAllocation(IBlob _currentBlob) { string filename = ""; _currentBlob.Extract(out filename); List lines = new List(); MemoryUtils.readFromFile(ref lines,filename,true); dataGridViewAllocation.Rows.Clear(); AllocationInfo currentAllocation = null; UInt32 count = 0; allocationTable = new Hashtable(); foreach (string line in lines) { if (line.Contains("ALLOC:")) { currentAllocation = new AllocationInfo(); //scan the string string[] lineInfo = Regex.Split(line," "); UInt32 size = (UInt32)int.Parse(lineInfo[1]); UInt32 address = (UInt32)int.Parse(lineInfo[2]); MemoryFile.XALLOC_ALLOCATOR_IDS eAlloc = (MemoryFile.XALLOC_ALLOCATOR_IDS)int.Parse(lineInfo[3]); UInt32 crc = (UInt32)int.Parse(lineInfo[4]); string addressX8 = "0x" + address.ToString("X8"); dataGridViewAllocation.Rows.Add(eAlloc.ToString(), addressX8, size, crc); currentAllocation.crc = crc; allocationTable[crc] = currentAllocation; count++; } else { currentAllocation.addLine(line); } } labelAllocationCount.Text = count.ToString(); } private void dataGridViewAllocation_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != -1) { if (dataGridViewAllocation.Columns[e.ColumnIndex].Name == "DumpColumn") { if ( dataGridViewAllocation.CurrentRow.Cells["CRCColumn"].Value!=null) { string crc = dataGridViewAllocation.CurrentRow.Cells["CRCColumn"].Value.ToString(); AllocationInfo allocationInfo = (AllocationInfo)allocationTable[UInt32.Parse(crc)]; if (allocationInfo == null) return; CallstackViewer callstackViewer = new CallstackViewer(); string tmp = ""; allocationInfo.fillText(ref tmp); callstackViewer.callStackText.SelectedText = tmp; callstackViewer.Visible = true; } } } } } }