JD2022-TU1/main/tools/legacy/MemoryViewer/MemoryEvent.cs

286 lines
8.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using UAF.Remoting;
namespace MemoryViewer
{
public partial class MemoryEvent : Form
{
private Hashtable nodesTable = new Hashtable();
public MemoryEvent()
{
InitializeComponent();
checkboxlistEvent.Items.Clear();
}
class NodeInfo
{
public UInt32 parentTag;
public UInt32 eventTag;
}
public void fill(IBlob blob)
{
checkboxlistEvent.Items.Clear();
UInt32 countEvent = 0;
blob.Extract(out countEvent);
string nameEvent = "";
UInt32 parentTag = 0;
UInt32 eventTag = 0;
nodesTable.Clear();
treeViewEvent.Nodes.Clear();
for (UInt32 index = 0; index < countEvent; ++index)
{
blob.Extract(out nameEvent);
blob.Extract(out parentTag);
blob.Extract(out eventTag);
TreeNode node = new TreeNode();
node.Text = nameEvent;
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.parentTag = parentTag;
nodeInfo.eventTag = eventTag;
node.Tag = nodeInfo;
nodesTable[eventTag] = node;
if (nodesTable[parentTag] != null)
{
TreeNode nodeParent = (TreeNode)nodesTable[parentTag];
nodeParent.Nodes.Add(node);
}
else
{
treeViewEvent.Nodes.Add(node);
}
}
treeViewEvent.ExpandAll();
}
public class callStackAllocation
{
public List<string> stack = new List<string>();
public int allocationSize = 0;
public void dump(ref List<string>listLine)
{
foreach (string s in stack)
{
listLine.Add(s);
}
listLine.Add("");
}
}
public void fillResult(IBlob blob)
{
String EventName = "";
blob.Extract(out EventName);
UInt32 countAllocation = 0;
blob.Extract(out countAllocation);
string FilenameDump = "";
blob.Extract(out FilenameDump);
float timeComputation = 0.0f;
blob.Extract(out timeComputation);
timeComputation *= 1000.0f;
labelAllocation.Text = "Allocation by frame: " + countAllocation.ToString();
labelTimeComputation.Text = "Time computation: " + timeComputation.ToString() + " ms";
//open the file and analyze
if (File.Exists(FilenameDump))
{
List<string> dst = new List<string>();
MemoryUtils.readFromFile(ref dst,FilenameDump,true);
bool lineInfo = true;
List<callStackAllocation> callstackList = new List<callStackAllocation>();
callStackAllocation callstack = null;
foreach (string line in dst)
{
if (lineInfo)
{
callstack = new callStackAllocation();
callstack.allocationSize = int.Parse(line);
callstackList.Add(callstack);
lineInfo = false;
}
else
{
if (line == "")
{
lineInfo = true;//next line is an info
}
else //it s call stack
{
callstack.stack.Add(line);
}
}
}
List<string> listLine = new List<string>();
callstackList.Sort(delegate(callStackAllocation p1, callStackAllocation p2) { return p2.allocationSize.CompareTo(p1.allocationSize); });
foreach (callStackAllocation callstackAllocation in callstackList)
{
listLine.Add("Allocation size: " + callstackAllocation.allocationSize);
callstackAllocation.dump(ref listLine);
}
listBoxCallstackAllocation.Items.Clear();
foreach (string s in listLine)
listBoxCallstackAllocation.Items.Add(s);
}
/*
for (UInt32 index = 0; index < countAllocation; ++index)
{
blob.extract(ref _size);
//callstack
blob.extract(ref callstackSize);
for (UInt32 cb = 0; cb < callstackSize; ++cb)
{
blob.extract(ref filename);
blob.extract(ref symbolname);
blob.extract(ref line);
tmp = filename + "(" + line.ToString() + "):" + symbolname + "\r\n";
globaltext += tmp;1
}
globaltext += "\r\n";
}*/
}
private void sendCommand(string _command, params object[] args)
{
//call back
callbackAction cbAction = (callbackAction)Tag;
if (cbAction != null)
cbAction(GetType().ToString(), _command, args);
}
private void MemoryEvent_Load(object sender, EventArgs e)
{
sendCommand("dump_memoryevents", null);
}
private void checkboxlistEvent_SelectedIndexChanged(object sender, EventArgs e)
{
//look for the appropriate event
int index = checkboxlistEvent.SelectedIndex;
if (index == -1)
return;
if (checkboxlistEvent.GetItemCheckState(index) == CheckState.Checked)
{
sendCommand("dump_specificevent", checkboxlistEvent.Items[index].ToString());
}
//checkboxlistEvent.SetItemCheckState(checkboxlistEvent.SelectedIndex, CheckState.Unchecked);
}
private void textBoxCallstackView_TextChanged(object sender, EventArgs e)
{
//get the first selected line and open visual
//if (textBoxCallstackView.s
}
private void listBoxCallstackAllocation_SelectedIndexChanged(object sender, EventArgs e)
{
//get the first selected line and open visual
if (listBoxCallstackAllocation.SelectedIndex == -1)
return;
string line = (string) listBoxCallstackAllocation.Items[listBoxCallstackAllocation.SelectedIndex];
//get filename
int index = line.IndexOf('(');
if (index !=-1)
{
string filename = line.Substring(0, index);
int indexEnd = line.IndexOf(')');
if (indexEnd != -1)
{
string linenumber = line.Substring(index + 1, indexEnd - index-1);
MemoryUtils.openFileAtLine(filename, linenumber);
}
}
}
private void buildEventsHierarchy(ref TreeNode currentNode, ref List<UInt32> tagList)
{
while (currentNode!=null)
{
NodeInfo nodeInfo = (NodeInfo)currentNode.Tag;
UInt32 parentTag = nodeInfo.parentTag;
if (nodesTable[parentTag] != null)
{
currentNode = (TreeNode)nodesTable[parentTag];
buildEventsHierarchy(ref currentNode, ref tagList);
}
else
{
currentNode = null;
}
tagList.Add(nodeInfo.eventTag);
}
}
private void treeViewEvent_MouseDoubleClick(object sender, MouseEventArgs e)
{
//check the selected node
if (treeViewEvent.SelectedNode != null)
{
//send the request for this node
//build the hierarchy
TreeNode treeNode = treeViewEvent.SelectedNode;
List<UInt32> tagList = new List<UInt32>();
buildEventsHierarchy(ref treeNode, ref tagList);
if (tagList.Count>0)
{
UInt32[] array = new UInt32[tagList.Count];
UInt32 index = 0;
foreach (UInt32 tag in tagList)
array[index++] = tag;
sendCommand("dump_specificevent", array);
}
}
}
}
}