using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Data; using System.Data.EntityClient; using System.Data.SqlClient; using System.Data.Common; using System.Data.Objects; using System.Reflection; using Microsoft.Win32; namespace Cosmos.Debug.Common { public class DebugInfo : IDisposable { // Please beware this field, it may cause issues if used incorrectly. public static DebugInfo CurrentInstance { get; private set; } public class Field_Map { public string TypeName { get; set; } public List FieldNames = new List(); } protected SqlConnection mConnection; protected string mDbName; // Dont use DbConnectionStringBuilder class, it doesnt work with LocalDB properly. protected string mDataSouce = @"(LocalDB)\v11.0"; //protected mDataSouce = @".\SQLEXPRESS"; protected string mConnStrBase; protected EntityConnection mEntConn; public void DeleteDB() { using (var xConn = new SqlConnection(mConnStrBase)) { xConn.Open(); bool xExists = false; using (var xCmd = xConn.CreateCommand()) { xCmd.CommandText = "select * from sys.databases where name = '" + mDbName + "'"; using (var xReader = xCmd.ExecuteReader()) { xExists = xReader.Read(); } } if (xExists) { // Necessary to because of SQL pooled connections etc, even if all our connections are closed. using (var xCmd = xConn.CreateCommand()) { xCmd.CommandText = "ALTER DATABASE " + mDbName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE"; xCmd.ExecuteNonQuery(); } // Yes this throws an exception if the database doesnt exist, so we have to // run it only if we know it exists. // This will detach and also delete the physica files. using (var xCmd = xConn.CreateCommand()) { xCmd.CommandText = "DROP DATABASE " + mDbName; xCmd.ExecuteNonQuery(); } } } } public DebugInfo(string aPathname, bool aCreate = false) { CurrentInstance = this; mDbName = Path.GetFileNameWithoutExtension(aPathname); mConnStrBase = @"Data Source=" + mDataSouce + ";Integrated Security=True;MultipleActiveResultSets=True;"; if (aCreate) { DeleteDB(); } // Initial Catalog is necessary for EDM string xConnStr = mConnStrBase + "Initial Catalog=" + mDbName + ";AttachDbFilename=" + aPathname + ";"; var xWorkspace = new System.Data.Metadata.Edm.MetadataWorkspace( new string[] { "res://*/" }, new Assembly[] { Assembly.GetExecutingAssembly() }); mEntConn = new EntityConnection(xWorkspace, new SqlConnection(xConnStr)); // Do not open mConnection before mEntities.CreateDatabase if (aCreate) { using (var xEntities = new Entities(mEntConn)) { // DatabaseExists checks if the DBName exists, not physical files. if (!xEntities.DatabaseExists()) { xEntities.CreateDatabase(); } } } mConnection = new SqlConnection(xConnStr); mConnection.Open(); } protected List local_MappingTypeNames = new List(); public void WriteFieldMappingToFile(IEnumerable aMapping) { var xMaps = aMapping.Where(delegate(Field_Map mp) { if (local_MappingTypeNames.Contains(mp.TypeName)) { return false; } else { local_MappingTypeNames.Add(mp.TypeName); return true; } }); // Is a real DB now, but we still store all in RAM. We don't need to. Need to change to query DB as needed instead. using (var xDB = DB()) { foreach (var xItem in xMaps) { foreach (var xFieldName in xItem.FieldNames) { var xRow = new FIELD_MAPPING(); xRow.TYPE_NAME = xItem.TypeName; xRow.FIELD_NAME = xFieldName; xDB.FIELD_MAPPING.AddObject(xRow); } } xDB.SaveChanges(); } } public Field_Map GetFieldMap(string aName) { var xMap = new Field_Map(); xMap.TypeName = aName; using (var xDB = DB()) { var xRows = from x in xDB.FIELD_MAPPING where x.TYPE_NAME == aName select x.FIELD_NAME; foreach (var xFieldName in xRows) { xMap.FieldNames.Add(xFieldName); } } return xMap; } public void ReadFieldMappingList(List aSymbols) { using (var xDB = DB()) { var xMap = new Field_Map(); foreach (var xRow in xDB.FIELD_MAPPING) { string xTypeName = xRow.TYPE_NAME; if (xTypeName != xMap.TypeName) { if (xMap.FieldNames.Count > 0) { aSymbols.Add(xMap); } xMap = new Field_Map(); xMap.TypeName = xTypeName; } xMap.FieldNames.Add(xRow.FIELD_NAME); } aSymbols.Add(xMap); } } protected List mLocalFieldInfoNames = new List(); public void WriteFieldInfoToFile(IEnumerable aFields) { using (var xDB = DB()) { foreach (var xItem in aFields) { if (!mLocalFieldInfoNames.Contains(xItem.NAME)) { mLocalFieldInfoNames.Add(xItem.NAME); xDB.FIELD_INFO.AddObject(xItem); } } xDB.SaveChanges(); } } public Entities DB() { return new Entities(mEntConn); } public void ReadLabels(out List> oLabels, out IDictionary oLabelAddressMappings) { oLabels = new List>(); oLabelAddressMappings = new Dictionary(); using (var xDB = DB()) { foreach (var xRow in xDB.Labels) { oLabels.Add(new KeyValuePair((uint)xRow.ADDRESS, xRow.LABELNAME)); oLabelAddressMappings.Add(xRow.LABELNAME, (uint)xRow.ADDRESS); } } } public void WriteSymbolsListToFile(IEnumerable aSymbols) { foreach (var x in aSymbols) { x.ID = Guid.NewGuid(); } BulkInsert("MLSYMBOLs", aSymbols.AsDataReader()); } // tuple format: MethodLabel, IsArgument, Index, Offset public void WriteAllLocalsArgumentsInfos(IEnumerable aInfos) { foreach (var x in aInfos) { x.ID = Guid.NewGuid(); } BulkInsert("LOCAL_ARGUMENT_INFO", aInfos.AsDataReader()); } // EF is slow on bulk operations. But we want to retain explicit bindings to the model to avoid unbound mistakes. // SqlBulk operations are on average 15x faster. So we use a hybrid approach by using the entities as containers // and EntityDataReader to bridge the gap to SqlBulk. public void BulkInsert(string aTableName, IDataReader aReader) { using (var xBulkCopy = new SqlBulkCopy(mConnection)) { xBulkCopy.DestinationTableName = aTableName; xBulkCopy.WriteToServer(aReader); } } public void WriteLabels(List