From 17a6d572eb475c43b1496bf2631271ac970927d1 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Mon, 7 Nov 2022 20:50:30 +0200 Subject: [PATCH 01/16] Fix Heap and Other bugs --- Setup/Cosmos.iss | 2 +- .../CosmosRuntimeType.cs | 1 - source/Cosmos.Core/Memory/HeapSmall.cs | 7 ++++--- 3 files changed, 5 insertions(+), 5 deletions(-) rename source/{Cosmos.Core_Plugs => Cosmos.Core}/CosmosRuntimeType.cs (99%) diff --git a/Setup/Cosmos.iss b/Setup/Cosmos.iss index 9abbde489d..779616816a 100644 --- a/Setup/Cosmos.iss +++ b/Setup/Cosmos.iss @@ -16,7 +16,7 @@ #endif #ifndef RealPath - #define RealPath {userappdata} + #define RealPath "{userappdata}" #endif #if BuildConfiguration == "DevKit" diff --git a/source/Cosmos.Core_Plugs/CosmosRuntimeType.cs b/source/Cosmos.Core/CosmosRuntimeType.cs similarity index 99% rename from source/Cosmos.Core_Plugs/CosmosRuntimeType.cs rename to source/Cosmos.Core/CosmosRuntimeType.cs index 175d5c6fbd..c2386ea360 100644 --- a/source/Cosmos.Core_Plugs/CosmosRuntimeType.cs +++ b/source/Cosmos.Core/CosmosRuntimeType.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Reflection; -using Cosmos.IL2CPU; namespace Cosmos.Core { diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index 136978c948..66f5347b00 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -396,10 +396,11 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) /// Byte pointer to the start of the block. public static byte* Alloc(ushort aSize) { - var pageBlock = GetFirstWithSpace(aSize); + var smtblock = GetFirstBlock(SMT, aSize); + var pageBlock = GetFirstWithSpace(aSize, smtblock); if (pageBlock == null) // This happens when the page is full and we need to allocate a new page for this size { - CreatePage(GetLastPage(), GetRoundedSize(aSize)); + CreatePage(GetLastPage(), smtblock->Size); pageBlock = GetFirstWithSpace(aSize); if (pageBlock == null) { @@ -410,7 +411,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) //now find position in the block var page = (ushort*)pageBlock->PagePtr; - var elementSize = GetRoundedSize(aSize) + PrefixItemBytes; + var elementSize = smtblock->Size + PrefixItemBytes; var positions = RAT.PageSize / elementSize; for (int i = 0; i < positions; i++) { From b7ecf8da02dec44cad53a640216ae46d03444e10 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Thu, 24 Nov 2022 11:13:01 +0200 Subject: [PATCH 02/16] Fix Native Memory Blocking compiler --- .../System/Runtime/InteropServices/NativeMemoryImpl.cs | 4 ++-- source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Process.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Cosmos.Core_Plugs/System/Runtime/InteropServices/NativeMemoryImpl.cs b/source/Cosmos.Core_Plugs/System/Runtime/InteropServices/NativeMemoryImpl.cs index 2ea6eeb419..a66b6c72b6 100644 --- a/source/Cosmos.Core_Plugs/System/Runtime/InteropServices/NativeMemoryImpl.cs +++ b/source/Cosmos.Core_Plugs/System/Runtime/InteropServices/NativeMemoryImpl.cs @@ -2,10 +2,10 @@ using Cosmos.Core.Memory; using Cosmos.Core; -namespace System.Runtime.InteropServices +namespace Cosmos.Core_Plugs.System.Runtime.InteropServices { [Plug("System.Runtime.InteropServices.NativeMemory, System.Private.CoreLib")] - public static unsafe class NativeMemory + public static unsafe class NativeMemoryImpl { public static void* Realloc(void* ptr, nuint byteCount) { diff --git a/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Process.cs b/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Process.cs index 4a263172b5..7693820d74 100644 --- a/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Process.cs +++ b/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Process.cs @@ -676,7 +676,7 @@ void DbgCmdSimpleNumber(uint nr) void DbgCmdKernelPanic(uint nr) { - AD7Util.ShowError("Kernel panic: 0x" + nr.ToString()); + AD7Util.ShowError("Kernel panic: 0x" + nr.ToString("X").ToUpper()); } void DbgCmdSimpleLongNumber(ulong nr) From 274b0522e882625d1d6b703ef87f1c584a3cb9ef Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Thu, 24 Nov 2022 11:15:10 +0200 Subject: [PATCH 03/16] Fix Heap Crashes --- source/Cosmos.Core/Memory/HeapSmall.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index 66f5347b00..3ebd257042 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -450,9 +450,10 @@ public static void Free(void* aPtr) if (size == 0) { // double free, this object has already been freed - Debugger.DoBochsBreak(); - Debugger.DoSendNumber((uint)heapObject); - Debugger.SendKernelPanic(0x99); + //Debugger.DoBochsBreak(); + //Debugger.DoSendNumber((uint)heapObject); + //Debugger.SendKernelPanic(0x99); + return; } var allocated = (uint*)aPtr; @@ -484,7 +485,7 @@ public static void Free(void* aPtr) { blockPtr = blockPtr->NextBlock; } - if(blockPtr->PagePtr == allocatedOnPage) + if (blockPtr != null && blockPtr->PagePtr == allocatedOnPage) { break; } From d03329293bef12e8631a261b8884aae64f1a2ddc Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 27 Nov 2022 14:14:25 +0200 Subject: [PATCH 04/16] Heap Large Free check --- source/Cosmos.Core/Memory/HeapLarge.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/Cosmos.Core/Memory/HeapLarge.cs b/source/Cosmos.Core/Memory/HeapLarge.cs index 7b9815d59f..ee66a922ec 100644 --- a/source/Cosmos.Core/Memory/HeapLarge.cs +++ b/source/Cosmos.Core/Memory/HeapLarge.cs @@ -52,7 +52,13 @@ public static void Init() /// Thrown if page type is not found. public static void Free(void* aPtr) { + var heapObject = (uint*)aPtr; var xPageIdx = RAT.GetFirstRATIndex(aPtr); + if (heapObject[-4] == 0 && xPageIdx == 0) + { + // The object is not allocated + return; + } RAT.Free(xPageIdx); } } From 5ac63ed50ce74c5ccb9fac9c1c889898e9723a16 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 27 Nov 2022 21:40:40 +0200 Subject: [PATCH 05/16] Fix Heap Bug --- source/Cosmos.Core/Memory/HeapSmall.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index 3ebd257042..19706ac905 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -389,6 +389,28 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) smtBlock->PagePtr = xPtr; } + + + /// + /// Get the first block for this size, which has space left to allocate to And + /// + /// The size + /// the Output block + /// The parent of the block + private static RootSMTBlock* GetFirstWithSpaceAndParent(uint aSize, out SMTBlock* block) + { + var page = SMT; + RootSMTBlock* rootblock = null; + do + { + rootblock = GetFirstBlock(page, aSize); + block = GetFirstWithSpace(aSize, rootblock); + + page = page->Next; + } while (rootblock == null && page != null); + return rootblock; + } + /// /// Alloc memory block, of a given size. /// @@ -396,8 +418,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) /// Byte pointer to the start of the block. public static byte* Alloc(ushort aSize) { - var smtblock = GetFirstBlock(SMT, aSize); - var pageBlock = GetFirstWithSpace(aSize, smtblock); + var smtblock = GetFirstWithSpaceAndParent(aSize, out SMTBlock* pageBlock); if (pageBlock == null) // This happens when the page is full and we need to allocate a new page for this size { CreatePage(GetLastPage(), smtblock->Size); From 212e2e17537c0502be3fe5e4124852982883a64a Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Mon, 28 Nov 2022 09:38:30 +0200 Subject: [PATCH 06/16] Documenting missing --- source/Cosmos.Core/Memory/HeapSmall.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index 19706ac905..acb812eae2 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -392,7 +392,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) /// - /// Get the first block for this size, which has space left to allocate to And + /// Get the first block for this size, which has space left to allocate to And get the root block /// /// The size /// the Output block From e8a630ea29a4acf84fadcb56e7b69c1fd509d083 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Tue, 29 Nov 2022 16:54:24 +0200 Subject: [PATCH 07/16] Made The debugger more friendly by adding assert and option to ignore it by user choose --- source/Cosmos.Core/Memory/HeapLarge.cs | 3 +- source/Cosmos.Core/Memory/HeapSmall.cs | 19 +- source/Cosmos.Core/Memory/RAT.cs | 2 +- source/Cosmos.Core/VTablesImpl.cs | 31 ++- source/Cosmos.Debug.Hosts/VMware.cs | 2 +- .../DebuggerAsm.cs | 8 + source/Cosmos.Debug.Kernel/Debugger.cs | 198 +++++++++++++++--- 7 files changed, 197 insertions(+), 66 deletions(-) diff --git a/source/Cosmos.Core/Memory/HeapLarge.cs b/source/Cosmos.Core/Memory/HeapLarge.cs index ee66a922ec..32c9c06505 100644 --- a/source/Cosmos.Core/Memory/HeapLarge.cs +++ b/source/Cosmos.Core/Memory/HeapLarge.cs @@ -35,8 +35,7 @@ public static void Init() var xPtr = (uint*)RAT.AllocPages(aType, xPages); if (xPtr == null) { - Debugger.SendKernelPanic(0x67); // out of pages - while (true) { } + Debugger.DoFail(0x67); // out of pages } xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size xPtr[1] = aSize; // Actual data size diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index acb812eae2..91c9f9d034 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -230,8 +230,7 @@ private static void AddRootSMTBlock(SMTPage* aPage, uint aSize) // we cant later add a block with a size smaller than an earlier block. That would break the algorithm Debugger.DoSendNumber(aSize); Debugger.DoSendNumber(ptr->Size); - Debugger.SendKernelPanic(0x83); - while (true) { } + Debugger.DoFail(0x83); } if (ptr->Size == 0) @@ -367,8 +366,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) smtBlock = NextFreeBlock(); if (smtBlock == null) { - Debugger.SendKernelPanic(0x93); - while (true) { }; + Debugger.DoFail(0x93); } } @@ -426,7 +424,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) if (pageBlock == null) { //this means that we cant allocate another page - Debugger.SendKernelPanic(0x121); + Debugger.DoAssert(0x121); } } @@ -456,8 +454,8 @@ static void CreatePage(SMTPage* aPage, uint aItemSize) // if we get here, RAM is corrupted, since we know we had a space but it turns out we didnt Debugger.DoSendNumber((uint)pageBlock); Debugger.DoSendNumber(aSize); - Debugger.SendKernelPanic(0x122); - while (true) { } + Debugger.DoFail(0x122); + return null; } /// @@ -472,8 +470,8 @@ public static void Free(void* aPtr) { // double free, this object has already been freed //Debugger.DoBochsBreak(); - //Debugger.DoSendNumber((uint)heapObject); - //Debugger.SendKernelPanic(0x99); + Debugger.DoSendNumber((uint)heapObject); + Debugger.DoAssert(true, 0x99, true); return; } @@ -518,8 +516,7 @@ public static void Free(void* aPtr) // this shouldnt happen Debugger.DoSendNumber((uint)aPtr); Debugger.DoSendNumber((uint)SMT); - Debugger.SendKernelPanic(0x98); - while (true) { } + Debugger.DoFail(0x98); } blockPtr->SpacesLeft++; } diff --git a/source/Cosmos.Core/Memory/RAT.cs b/source/Cosmos.Core/Memory/RAT.cs index fcb81b1a71..f5ebfb2adf 100644 --- a/source/Cosmos.Core/Memory/RAT.cs +++ b/source/Cosmos.Core/Memory/RAT.cs @@ -124,7 +124,7 @@ public static void Init(byte* aStartPtr, uint aSize) if (aSize % PageSize != 0) { Debugger.DoSendNumber((aSize % PageSize)); - Debugger.SendKernelPanic(11); + Debugger.DoAssert(11); throw new Exception("RAM size must be page aligned."); } diff --git a/source/Cosmos.Core/VTablesImpl.cs b/source/Cosmos.Core/VTablesImpl.cs index 9d38a10dbb..35457f5d29 100644 --- a/source/Cosmos.Core/VTablesImpl.cs +++ b/source/Cosmos.Core/VTablesImpl.cs @@ -156,7 +156,7 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) EnableDebug = true; DebugHex("Type", aType); DebugHex("MethodId", aMethodId); - Debugger.SendKernelPanic(KernelPanics.VMT_TypeIdInvalid); + Debugger.DoFail(KernelPanics.VMT_TypeIdInvalid); while (true) ; } var xCurrentType = aType; @@ -170,14 +170,14 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) { EnableDebug = true; DebugHex("MethodIndexes is null for type", aType); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodIndexesNull); + Debugger.DoFail(KernelPanics.VMT_MethodIndexesNull); while (true) ; } if (xCurrentTypeInfo.MethodAddresses == null) { EnableDebug = true; DebugHex("MethodAddresses is null for type", aType); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodAddressesNull); + Debugger.DoFail(KernelPanics.VMT_MethodAddressesNull); while (true) ; } @@ -196,9 +196,8 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) DebugHex("MethodCount", xCurrentTypeInfo.MethodCount); DebugHex("MethodAddresses.Length", (uint)xCurrentTypeInfo.MethodAddresses.Length); Debug("Method found, but address is invalid!"); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodFoundButAddressInvalid); - while (true) - ; + Debugger.DoFail(KernelPanics.VMT_MethodFoundButAddressInvalid); + } Debug("Found."); return xResult; @@ -218,8 +217,7 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) DebugHex("MethodId", aMethodId); Debug("Not FOUND!"); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound); - while (true) ; + Debugger.DoFail(KernelPanics.VMT_MethodNotFound); throw new Exception("Cannot find virtual method!"); } @@ -252,8 +250,8 @@ public static uint GetDeclaringTypeOfMethodForType(uint aType, uint aMethodId) DebugHex("MethodId", aMethodId); Debug("Not FOUND Declaring TYPE!"); Debugger.DoBochsBreak(); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound); - while (true) ; + Debugger.DoFail(KernelPanics.VMT_MethodNotFound); + return 0; } public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceMethodId) @@ -263,8 +261,7 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM EnableDebug = true; DebugHex("Type", aType); DebugHex("InterfaceMethodId", aInterfaceMethodId); - Debugger.SendKernelPanic(KernelPanics.VMT_TypeIdInvalid); - while (true) ; + Debugger.DoFail((int)KernelPanics.VMT_TypeIdInvalid); } var xTypeInfo = mTypes[aType]; @@ -273,16 +270,14 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM { EnableDebug = true; DebugHex("InterfaceMethodIndexes is null for type", aType); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodIndexesNull); - while (true) ; + Debugger.DoFail((int)KernelPanics.VMT_MethodIndexesNull); } if (xTypeInfo.TargetMethodIndexes == null) { EnableDebug = true; DebugHex("TargetMethodIndexes is null for type", aType); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodAddressesNull); - while (true) ; + Debugger.DoFail((int)KernelPanics.VMT_MethodAddressesNull); } for (int i = 0; i < xTypeInfo.InterfaceMethodIndexes.Length; i++) @@ -299,8 +294,8 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM DebugHex("InterfaceMethodId", aInterfaceMethodId); Debug("Not FOUND!"); - Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound); - while (true) ; + Debugger.DoFail((int)KernelPanics.VMT_MethodNotFound); + return 0; } /// diff --git a/source/Cosmos.Debug.Hosts/VMware.cs b/source/Cosmos.Debug.Hosts/VMware.cs index 76c3027665..195f01ea9b 100644 --- a/source/Cosmos.Debug.Hosts/VMware.cs +++ b/source/Cosmos.Debug.Hosts/VMware.cs @@ -117,7 +117,7 @@ public override void Stop() { mProcess.Kill(); //kil vmware-vmx.exe - foreach (var process in Process.GetProcessesByName("vmware-vmx.exe")) + foreach (var process in Process.GetProcessesByName("vmware-vmx")) { process.Kill(); } diff --git a/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs b/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs index 95fadc540d..6e802074ef 100644 --- a/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs +++ b/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs @@ -60,6 +60,14 @@ public static void SendKernelPanic(uint id) new LiteralAssemblerCode("%endif"); } + [Inline] + public static void SendCoreDump() + { + new LiteralAssemblerCode("%ifdef DEBUGSTUB"); + new LiteralAssemblerCode("Call DebugStub_SendCoreDump"); + new LiteralAssemblerCode("%endif"); + } + [PlugMethod(Assembler = typeof(DoRealHalt))] public static void DoRealHalt() { } diff --git a/source/Cosmos.Debug.Kernel/Debugger.cs b/source/Cosmos.Debug.Kernel/Debugger.cs index 3f135e48a2..cc03c25e76 100644 --- a/source/Cosmos.Debug.Kernel/Debugger.cs +++ b/source/Cosmos.Debug.Kernel/Debugger.cs @@ -21,6 +21,8 @@ public static Debugger CreateDebugger(string aRing = "", string aSection = "") public class Debugger { + public static bool IgnoreAssert = false; + public Debugger(string aRing, string aSection) { Ring = aRing; @@ -31,11 +33,13 @@ public Debugger(string aRing, string aSection) public string Section { get; } + #region Break public void Break() { } public static void DoBochsBreak() { } internal static void DoRealHalt() { } + #endregion private static unsafe void ActualSend(int aLength, char* aText) { } @@ -53,22 +57,22 @@ public static void DoSendNumber(float aNumber) { } public static void DoSendNumber(double aNumber) { } - internal static void DoSendCoreDump() { } - public void SendNumber(uint aNumber) => DoSendNumber(aNumber); - + public void SendNumber(int aNumber) => DoSendNumber(aNumber); - + public void SendNumber(ulong aNumber) => DoSendNumber(aNumber); - + public void SendNumber(long aNumber) => DoSendNumber(aNumber); public void SendNumber(float aNumber) => DoSendNumber(aNumber); - + public void SendNumber(double aNumber) => DoSendNumber(aNumber); - - public unsafe void SendChannelCommand(byte aChannel, byte aCommand, byte[] aData) { - fixed (byte* xPtr = &aData[0]) { + + public unsafe void SendChannelCommand(byte aChannel, byte aCommand, byte[] aData) + { + fixed (byte* xPtr = &aData[0]) + { SendChannelCommand(aChannel, aCommand, aData.Length, xPtr); } } @@ -77,28 +81,138 @@ public static unsafe void SendChannelCommand(byte aChannel, byte aCommand, int a public static void SendChannelCommand(byte aChannel, byte aCommand) { } + #region Assert + private static void SendKernelPanic(uint id) { } + + public static void SendCoreDump() { } + + #region Assert Function + + public static void DoAssert(int code) + { + DoAssert(true, code); + } + + public static void DoAssert(bool condition) + { + DoAssert(condition, -1); + } + + public static void DoAssert(bool condition, int code) + { + DoAssert(condition, code, false); + } + public static void DoAssert(bool condition, int code, string message) + { + DoAssert(condition, code, false, message); + } + public static void DoAssert(bool condition, bool DoBreak) + { + DoAssert(condition, -1, DoBreak, null); + } + public static void DoAssert(bool condition, bool DoBreak, string message) + { + DoAssert(condition, -1, DoBreak, message); + } + + public static void DoAssert(bool condition, string message) + { + DoAssert(condition, -1, false, message); + } + + public static void DoAssert(bool condition, int code, bool DoBreak) + { + DoAssert(condition, code, DoBreak, null); + } + + public static void DoAssert(bool condition, int code, bool DoBreak, string message) + { + if (condition && !IgnoreAssert) + { + if (message != null) { DoSend(message); } + if (DoBreak) + { + DoBochsBreak(); + } + if (code > -1) + { + SendKernelPanic((uint)code); + } + else { SendCoreDump(); } + } + } + + [Conditional("COSMOSDEBUG")] + public virtual void Assert(bool condition) => DoAssert(condition); + + [Conditional("COSMOSDEBUG")] + public virtual void Assert(bool condition, int code) => DoAssert(condition, code); + + [Conditional("COSMOSDEBUG")] + public virtual void Assert(bool condition, int code, string message) => DoAssert(condition, code, message); + #endregion + + #region Fail Function + + public static void DoFail(uint code) + { + DoFail((int)code); + } + + public static void DoFail(int code) + { + DoFail(code, null); + } + + public static void DoFail(string message) + { + DoFail(-1, message); + } + + public static void DoFail(int code, string message) + { + if (message != null) { DoSend(message); } + if (code > -1) + { + SendKernelPanic((uint)code); + } + else { SendCoreDump(); } // behave like assert function + // halt + while (true) { } + } + [Conditional("COSMOSDEBUG")] + public virtual void Fail(string message) => DoFail(message); + [Conditional("COSMOSDEBUG")] + public virtual void Fail(int code) => DoFail(code); + [Conditional("COSMOSDEBUG")] + public virtual void Fail(int code, string message) => DoFail(code, message); + #endregion + + #endregion + + #region Trace + internal static void DoSend(string aText) { } - public static void SendKernelPanic(uint id) { } public void Send(string aText) => DoSend(aText); [Conditional("COSMOSDEBUG")] public virtual void SendInternal(string aText) => DoSend(aText); - + [Conditional("COSMOSDEBUG")] public virtual void SendInternal(uint aNumber) => DoSendNumber(aNumber); - + [Conditional("COSMOSDEBUG")] public virtual void SendInternal(int aNumber) => DoSendNumber(aNumber); [Conditional("COSMOSDEBUG")] public virtual void SendInternal(ulong aNumber) => DoSendNumber(aNumber); - + [Conditional("COSMOSDEBUG")] public virtual void SendInternal(long aNumber) => DoSendNumber(aNumber); [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(float aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(float aNumber) => DoSendNumber(aNumber); [Conditional("COSMOSDEBUG")] public virtual void SendInternal(double aNumber) => DoSendNumber(aNumber); @@ -120,21 +234,25 @@ public static void SendKernelPanic(uint id) { } public unsafe void SendMessageBox(int aLength, char* aText) { } // Plugged - public unsafe void SendMessageBox(string aText) { + public unsafe void SendMessageBox(string aText) + { // TODO: Need to fix this so it can send empty strings. // Sending empty strings locks it up right now - if (aText.Length == 0) { + if (aText.Length == 0) + { return; } var xChars = aText.ToCharArray(); - fixed (char* xPtr = &xChars[0]) { + fixed (char* xPtr = &xChars[0]) + { SendMessageBox(xChars.Length, xPtr); } } // TODO: Kudzu replacement methods for Cosmos.HAL.DebugUtil - public unsafe void SendMessage(string aModule, string aData) { + public unsafe void SendMessage(string aModule, string aData) + { //string xSingleString; //xSingleString = "Message Module: \"" + aModule + "\""; //xSingleString += " Data: \"" + aData + "\""; @@ -145,34 +263,42 @@ public unsafe void SendMessage(string aModule, string aData) { DoSend("Data:"); DoSend(aData); } + #endregion - public unsafe void SendError(string aModule, string aData) { + #region Not Implement + public unsafe void SendError(string aModule, string aData) + { //string xSingleString; //xSingleString = "Error Module: \"" + aModule + "\""; //xSingleString += " Data: \"" + aData + "\""; //Send(xSingleString); } - public unsafe void SendNumber(string aModule, string aDescription, uint aNumber, byte aBits) { + public unsafe void SendNumber(string aModule, string aDescription, uint aNumber, byte aBits) + { //string xSingleString; //xSingleString = "Number Module: \"" + aModule + "\""; //xSingleString += " Description: \"" + aDescription + "\""; //xSingleString += " Number: \"" + CreateNumber(aNumber, aBits) + "\""; } - public unsafe void WriteNumber(uint aNumber, byte aBits) { + public unsafe void WriteNumber(uint aNumber, byte aBits) + { WriteNumber(aNumber, aBits, true); } - public unsafe void WriteNumber(uint aNumber, byte aBits, bool aWritePrefix) { + public unsafe void WriteNumber(uint aNumber, byte aBits, bool aWritePrefix) + { Send(CreateNumber(aNumber, aBits, aWritePrefix)); } - public unsafe string CreateNumber(uint aNumber, byte aBits) { + public unsafe string CreateNumber(uint aNumber, byte aBits) + { return CreateNumber(aNumber, aBits, true); } - public unsafe string CreateNumber(uint aNumber, byte aBits, bool aWritePrefix) { + public unsafe string CreateNumber(uint aNumber, byte aBits, bool aWritePrefix) + { return "Cosmos.Debug.Debugger.CreateNumber(aNumber, aBits, aWritePrefix) not implemented"; //string xNumberString = null; //uint xValue = aNumber; @@ -244,11 +370,13 @@ public unsafe string CreateNumber(uint aNumber, byte aBits, bool aWritePrefix) { //return xNumberString; } - public unsafe void WriteBinary(string aModule, string aMessage, byte[] aValue) { + public unsafe void WriteBinary(string aModule, string aMessage, byte[] aValue) + { WriteBinary(aModule, aMessage, aValue, 0, aValue.Length); } - public unsafe void WriteBinary(string aModule, string aMessage, byte[] aValue, int aIndex, int aLength) { + public unsafe void WriteBinary(string aModule, string aMessage, byte[] aValue, int aIndex, int aLength) + { //string xSingleString; //xSingleString = "Binary Module = \"" + aModule + "\""; //xSingleString += " Message = " + aMessage + "\""; @@ -261,7 +389,8 @@ public unsafe void WriteBinary(string aModule, string aMessage, byte[] aValue, i //Send(xSingleString); } - public unsafe void WriteBinary(string aModule, string aMessage, byte* aValue, int aIndex, int aLength) { + public unsafe void WriteBinary(string aModule, string aMessage, byte* aValue, int aIndex, int aLength) + { //string xSingleString; //xSingleString = "Binary Module = \"" + aModule + "\""; //xSingleString += " Message = " + aMessage + "\""; @@ -274,11 +403,13 @@ public unsafe void WriteBinary(string aModule, string aMessage, byte* aValue, in //Send(xSingleString); } - public unsafe void ViewMemory() { + public unsafe void ViewMemory() + { ViewMemory(0); } - public unsafe void ViewMemory(int addr) { + public unsafe void ViewMemory(int addr) + { //while (true) { // Console.Clear(); // Console.WriteLine(); @@ -316,16 +447,17 @@ public unsafe void ViewMemory(int addr) { // addr = FromHex(s); //} } + #endregion - public void SendCoreDump() => DoSendCoreDump(); - - private int FromHex(string p) { + private int FromHex(string p) + { p = p.ToLower(); string hex = "0123456789abcdef"; int ret = 0; - for (int i = 0; i < p.Length; i++) { + for (int i = 0; i < p.Length; i++) + { ret = ret * 16 + hex.IndexOf(p[i]); } return ret; From 78048cd263196132a9ab924de1fe9ea3aed1b0a1 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Tue, 29 Nov 2022 21:34:49 +0200 Subject: [PATCH 08/16] Fix syntax --- source/Cosmos.Core/VTablesImpl.cs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/source/Cosmos.Core/VTablesImpl.cs b/source/Cosmos.Core/VTablesImpl.cs index 35457f5d29..08c9e58bcc 100644 --- a/source/Cosmos.Core/VTablesImpl.cs +++ b/source/Cosmos.Core/VTablesImpl.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Cosmos.Debug.Kernel; namespace Cosmos.Core @@ -157,7 +153,6 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) DebugHex("Type", aType); DebugHex("MethodId", aMethodId); Debugger.DoFail(KernelPanics.VMT_TypeIdInvalid); - while (true) ; } var xCurrentType = aType; do @@ -171,14 +166,12 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) EnableDebug = true; DebugHex("MethodIndexes is null for type", aType); Debugger.DoFail(KernelPanics.VMT_MethodIndexesNull); - while (true) ; } if (xCurrentTypeInfo.MethodAddresses == null) { EnableDebug = true; DebugHex("MethodAddresses is null for type", aType); Debugger.DoFail(KernelPanics.VMT_MethodAddressesNull); - while (true) ; } for (int i = 0; i < xCurrentTypeInfo.MethodIndexes.Length; i++) @@ -197,7 +190,7 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId) DebugHex("MethodAddresses.Length", (uint)xCurrentTypeInfo.MethodAddresses.Length); Debug("Method found, but address is invalid!"); Debugger.DoFail(KernelPanics.VMT_MethodFoundButAddressInvalid); - + } Debug("Found."); return xResult; @@ -261,7 +254,7 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM EnableDebug = true; DebugHex("Type", aType); DebugHex("InterfaceMethodId", aInterfaceMethodId); - Debugger.DoFail((int)KernelPanics.VMT_TypeIdInvalid); + Debugger.DoFail(KernelPanics.VMT_TypeIdInvalid); } var xTypeInfo = mTypes[aType]; @@ -270,14 +263,14 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM { EnableDebug = true; DebugHex("InterfaceMethodIndexes is null for type", aType); - Debugger.DoFail((int)KernelPanics.VMT_MethodIndexesNull); + Debugger.DoFail(KernelPanics.VMT_MethodIndexesNull); } if (xTypeInfo.TargetMethodIndexes == null) { EnableDebug = true; DebugHex("TargetMethodIndexes is null for type", aType); - Debugger.DoFail((int)KernelPanics.VMT_MethodAddressesNull); + Debugger.DoFail(KernelPanics.VMT_MethodAddressesNull); } for (int i = 0; i < xTypeInfo.InterfaceMethodIndexes.Length; i++) @@ -294,7 +287,7 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM DebugHex("InterfaceMethodId", aInterfaceMethodId); Debug("Not FOUND!"); - Debugger.DoFail((int)KernelPanics.VMT_MethodNotFound); + Debugger.DoFail(KernelPanics.VMT_MethodNotFound); return 0; } From 4e68f2ba809fa2ba3233a6e952ad1beafdcdf632 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Wed, 21 Dec 2022 17:45:28 +0200 Subject: [PATCH 09/16] VMware host upgrade --- source/Cosmos.Debug.Hosts/VMware.cs | 143 ++++++++++++++++++++-------- 1 file changed, 104 insertions(+), 39 deletions(-) diff --git a/source/Cosmos.Debug.Hosts/VMware.cs b/source/Cosmos.Debug.Hosts/VMware.cs index 195f01ea9b..f42c1eb547 100644 --- a/source/Cosmos.Debug.Hosts/VMware.cs +++ b/source/Cosmos.Debug.Hosts/VMware.cs @@ -7,38 +7,48 @@ //using Vestris.VMWareLib; using Cosmos.Build.Common; +using System.Linq; -namespace Cosmos.Debug.Hosts { - public class VMware : Host { +namespace Cosmos.Debug.Hosts +{ + public class VMware : Host + { protected VMwareEdition mEdition; protected string mDir; protected string mVmxPath; protected Process mProcess; + protected Process[] mProcessesBefore, mProcessesAfter, mProcesses; protected string mWorkstationPath; protected string mPlayerPath; protected string mHarddisk; - public VMware(Dictionary aParams, bool aUseGDB,string harddisk = "Filesystem.vmdk") : base(aParams, aUseGDB) { + public VMware(Dictionary aParams, bool aUseGDB, string harddisk = "Filesystem.vmdk") : base(aParams, aUseGDB) + { mHarddisk = harddisk; mDir = Path.Combine(CosmosPaths.Build, @"VMWare\Workstation\"); mVmxPath = Path.Combine(mDir, @"Debug.vmx"); mWorkstationPath = GetPathname("VMware Workstation", "vmware.exe"); mPlayerPath = GetPathname("VMware Player", "vmplayer.exe"); - if (mWorkstationPath == null && mPlayerPath == null) { + if (mWorkstationPath == null && mPlayerPath == null) + { throw new Exception("VMware not found."); } string xFlavor = aParams[BuildPropertyNames.VMwareEditionString].ToUpper(); mEdition = VMwareEdition.Player; - if (xFlavor == "WORKSTATION") { + if (xFlavor == "WORKSTATION") + { mEdition = VMwareEdition.Workstation; } // Try alternate if selected one is not installed - if (mEdition == VMwareEdition.Player && mPlayerPath == null && mWorkstationPath != null) { + if (mEdition == VMwareEdition.Player && mPlayerPath == null && mWorkstationPath != null) + { mEdition = VMwareEdition.Workstation; - } else if (mEdition == VMwareEdition.Workstation && mWorkstationPath == null) { + } + else if (mEdition == VMwareEdition.Workstation && mWorkstationPath == null) + { mEdition = VMwareEdition.Player; } } @@ -51,11 +61,15 @@ public VMware(Dictionary aParams, bool aUseGDB,string harddisk = // } //} - protected string GetPathname(string aKey, string aEXE) { - using (var xRegKey = Registry.LocalMachine.OpenSubKey(@"Software\WOW6432Node\VMware, Inc.\" + aKey, false)) { - if (xRegKey != null) { + protected string GetPathname(string aKey, string aEXE) + { + using (var xRegKey = Registry.LocalMachine.OpenSubKey(@"Software\WOW6432Node\VMware, Inc.\" + aKey, false)) + { + if (xRegKey != null) + { string xResult = Path.Combine(((string)xRegKey.GetValue("InstallPath")), aEXE); - if (File.Exists(xResult)) { + if (File.Exists(xResult)) + { return xResult; } } @@ -63,24 +77,31 @@ protected string GetPathname(string aKey, string aEXE) { } } - public override void Start() { + public override void Start() + { Cleanup(); CreateDebugVmx(); // Target exe or file mProcess = new Process(); var xPSI = mProcess.StartInfo; - if (mEdition == VMwareEdition.Player) { + if (mEdition == VMwareEdition.Player) + { xPSI.FileName = mPlayerPath; - } else { + } + else + { xPSI.FileName = mWorkstationPath; } var xArgSB = new StringBuilder(); string xVmxPath = "\"" + mVmxPath + "\""; - if (mEdition == VMwareEdition.Player) { + if (mEdition == VMwareEdition.Player) + { xPSI.Arguments = xVmxPath; - } else { + } + else + { // -x: Auto power on VM. Must be small x, big X means something else. // -q: Close VMWare when VM is powered off. // Options must come beore the vmx, and cannot use shellexecute @@ -89,7 +110,27 @@ public override void Start() { xPSI.UseShellExecute = false; //must be true to allow elevate the process, sometimes needed if vmware only runs with admin rights mProcess.EnableRaisingEvents = true; mProcess.Exited += ExitCallback; + + mProcessesBefore = Process.GetProcessesByName("vmware-vmx"); mProcess.Start(); + + Stopwatch watch = Stopwatch.StartNew(); + + // Wait for the process to spawn + mProcessesAfter = Process.GetProcessesByName("vmware-vmx"); + while (mProcessesAfter.Length == mProcessesBefore.Length) + { + if (watch.Elapsed.Seconds == 15) + { + watch.Stop(); + throw new TimeoutException("VMware Workstation took too long to launch!"); + } + + mProcessesAfter = Process.GetProcessesByName("vmware-vmx"); + } + + // Get the new processes + mProcesses = mProcessesBefore.Concat(mProcessesAfter).Distinct().ToArray(); } private void ExitCallback(object sender, EventArgs e) @@ -106,18 +147,19 @@ private void ExitCallback(object sender, EventArgs e) } } - public override void Stop() { + public override void Stop() + { if (null != mProcess) { try { //TODO: Close VMWare properly - //Force Kill VMWare + // Kill the VMware GUI mProcess.Kill(); - //kil vmware-vmx.exe - foreach (var process in Process.GetProcessesByName("vmware-vmx")) + // Kill the actual VM instance + foreach (var process in mProcesses) { process.Kill(); } @@ -129,15 +171,19 @@ public override void Stop() { Cleanup(); } - protected void DeleteFiles(string aPath, string aPattern) { + protected void DeleteFiles(string aPath, string aPattern) + { var xFiles = Directory.GetFiles(aPath, aPattern); - foreach (var xFile in xFiles) { + foreach (var xFile in xFiles) + { File.Delete(xFile); } } - protected void Cleanup() { - try { + protected void Cleanup() + { + try + { string xPath = Path.GetDirectoryName(mVmxPath); // Delete old Debug.vmx and other files that might be left over from previous run. // Especially important with newer versions of VMWare player which defaults to suspend @@ -155,50 +201,66 @@ protected void Cleanup() { File.Delete(Path.Combine(xPath, "vmware-0.log")); File.Delete(Path.Combine(xPath, "vmware-1.log")); File.Delete(Path.Combine(xPath, "vmware-2.log")); - } catch (Exception) { + } + catch (Exception) + { // Ignore errors, users can stop VS while VMware is still running and files will be locked. } } - protected void CreateDebugVmx() { + protected void CreateDebugVmx() + { // VMWare doesn't like to boot a read only VMX. // We also need to make changes based on project / debug settings. // Finally we do not want to create VCS checkins based on local user changes. // Because of this we use Cosmos.vmx as a template and output a Debug.vmx on every run. - using (var xSrc = new StreamReader(File.Open(Path.Combine(mDir, "Cosmos.vmx"), FileMode.OpenOrCreate))) { - try { + using (var xSrc = new StreamReader(File.Open(Path.Combine(mDir, "Cosmos.vmx"), FileMode.OpenOrCreate))) + { + try + { // Write out Debug.vmx - using (var xDest = new StreamWriter(File.Open(mVmxPath, FileMode.Create))) { + using (var xDest = new StreamWriter(File.Open(mVmxPath, FileMode.Create))) + { string xLine; - while ((xLine = xSrc.ReadLine()) != null) { + while ((xLine = xSrc.ReadLine()) != null) + { var xParts = xLine.Split('='); - if (xParts.Length == 2) { + if (xParts.Length == 2) + { string xName = xParts[0].Trim(); string xValue = xParts[1].Trim(); - if ((xName == "uuid.location") || (xName == "uuid.bios")) { + if ((xName == "uuid.location") || (xName == "uuid.bios")) + { // We delete uuid entries so VMWare doesnt ask the user "Did you move or copy" the file xValue = null; - } else if (xName == "ide1:0.fileName") + } + else if (xName == "ide1:0.fileName") { // Set the ISO file for booting xValue = "\"" + mParams["ISOFile"] + "\""; - } else if (xName == "ide0:0.fileName") { + } + else if (xName == "ide0:0.fileName") + { xValue = "\"" + mHarddisk + "\""; - } else if (xName == "nvram") { + } + else if (xName == "nvram") + { // Point it to an initially non-existent nvram. // This has the effect of disabling PXE so the boot is faster. xValue = "\"Debug.nvram\""; } - if (xValue != null) { + if (xValue != null) + { xDest.WriteLine(xName + " = " + xValue); } } } - if (mUseGDB) { + if (mUseGDB) + { xDest.WriteLine(); xDest.WriteLine("debugStub.listen.guest32 = \"TRUE\""); xDest.WriteLine("debugStub.hideBreakpoints = \"TRUE\""); @@ -206,8 +268,11 @@ protected void CreateDebugVmx() { xDest.WriteLine("debugStub.listen.guest32.remote = \"TRUE\""); } } - } catch (IOException ex) { - if (ex.Message.Contains(Path.GetFileName(mDir))) { + } + catch (IOException ex) + { + if (ex.Message.Contains(Path.GetFileName(mDir))) + { throw new Exception("The VMware image " + mDir + " is still in use. Please exit current Vmware session with Cosmos and try again.", ex); } throw; From 361bfdb1295e9421f3685ff40284c0a1fde34a4a Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Wed, 21 Dec 2022 18:14:53 +0200 Subject: [PATCH 10/16] Restore Changes --- source/Cosmos.Core/CosmosRuntimeType.cs | 2 ++ source/Cosmos.Core/Memory/HeapLarge.cs | 1 + source/Cosmos.Core/Memory/HeapSmall.cs | 1 + .../DebuggerAsm.cs | 1 + source/Cosmos.Debug.Kernel/Debugger.cs | 1 - source/Cosmos.System2/FileSystem/Disk.cs | 31 +++++++++++++++++++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/source/Cosmos.Core/CosmosRuntimeType.cs b/source/Cosmos.Core/CosmosRuntimeType.cs index c2386ea360..a12633ea50 100644 --- a/source/Cosmos.Core/CosmosRuntimeType.cs +++ b/source/Cosmos.Core/CosmosRuntimeType.cs @@ -12,6 +12,8 @@ public CosmosRuntimeType(uint aTypeId) : this() { mTypeId = aTypeId; + Name = VTablesImpl.GetName(aTypeId); + AssemblyQualifiedName = VTablesImpl.GetAssemblyQualifiedName(aTypeId); } protected CosmosRuntimeType() diff --git a/source/Cosmos.Core/Memory/HeapLarge.cs b/source/Cosmos.Core/Memory/HeapLarge.cs index ddaf06c9a3..f69296b21b 100644 --- a/source/Cosmos.Core/Memory/HeapLarge.cs +++ b/source/Cosmos.Core/Memory/HeapLarge.cs @@ -36,6 +36,7 @@ public static void Init() if (xPtr == null) { Debugger.DoFail(0x67); // out of pages + } xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size xPtr[1] = aSize; // Actual data size diff --git a/source/Cosmos.Core/Memory/HeapSmall.cs b/source/Cosmos.Core/Memory/HeapSmall.cs index c48a4ad373..ed4f1b8cb9 100644 --- a/source/Cosmos.Core/Memory/HeapSmall.cs +++ b/source/Cosmos.Core/Memory/HeapSmall.cs @@ -579,4 +579,5 @@ private static int GetAllocatedObjectCount(SMTPage* aPage, uint aSize) #endregion } + } diff --git a/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs b/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs index 6e802074ef..fa5039d526 100644 --- a/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs +++ b/source/Cosmos.Debug.Kernel.Plugs.Asm/DebuggerAsm.cs @@ -60,6 +60,7 @@ public static void SendKernelPanic(uint id) new LiteralAssemblerCode("%endif"); } + [Inline] public static void SendCoreDump() { diff --git a/source/Cosmos.Debug.Kernel/Debugger.cs b/source/Cosmos.Debug.Kernel/Debugger.cs index cc03c25e76..d228ee44e1 100644 --- a/source/Cosmos.Debug.Kernel/Debugger.cs +++ b/source/Cosmos.Debug.Kernel/Debugger.cs @@ -22,7 +22,6 @@ public static Debugger CreateDebugger(string aRing = "", string aSection = "") public class Debugger { public static bool IgnoreAssert = false; - public Debugger(string aRing, string aSection) { Ring = aRing; diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs index 6bc5f8324a..a78b5b69a3 100644 --- a/source/Cosmos.System2/FileSystem/Disk.cs +++ b/source/Cosmos.System2/FileSystem/Disk.cs @@ -58,6 +58,23 @@ public List Partitions i++; } } + // Partitions is lost + if (converted.Count == 0) + { + int i = 0; + foreach (var item in FindLostPartitions()) + { + var part = new ManagedPartition(item); + if (MountedPartitions[i] != null) + { + var data = MountedPartitions[i]; + part.RootPath = data.RootPath; + part.MountedFS = data; + } + converted.Add(part); + i++; + } + } return converted; } @@ -265,6 +282,20 @@ public void FormatPartition(int index, string format, bool quick = true) } } + private List FindLostPartitions() + { + List list = new(); + foreach (var p in Partition.Partitions) + { + if (p.Host == Host) + { + // start mount + list.Add(p); + } + } + return list; + } + private FileSystem[] MountedPartitions = new FileSystem[4]; /// /// Mounts a partition From ff91d469f2a2af8022cc92ae15cb27b8d0f11ace Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Wed, 4 Jan 2023 20:08:31 +0200 Subject: [PATCH 11/16] Fix File Copy Function --- .../System/IO/CosmosFileSystem.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs index 84d62e490c..2976a559e6 100644 --- a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs +++ b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs @@ -93,19 +93,21 @@ public static void CopyFile(string sourceFullPath, string destFullPath, bool ove { destFullPath = Path.Combine(destFullPath, Path.GetFileName(sourceFullPath)); } - - // Copy the contents of the file from the source to the destination, creating the destination in the process - using (var src = new FileStream(sourceFullPath, FileMode.Open)) - using (var dst = new FileStream(destFullPath, overwrite ? FileMode.Create : FileMode.CreateNew)) + if (!File.Exists(destFullPath) || overwrite) { - int xSize = (int)src.Length; - Global.mFileSystemDebugger.SendInternal($"size of {sourceFullPath} is {xSize} bytes"); - byte[] content = new byte[xSize]; - Global.mFileSystemDebugger.SendInternal($"content byte buffer allocated"); - src.Read(content, 0, xSize); - Global.mFileSystemDebugger.SendInternal($"content byte buffer read"); - dst.Write(content, 0, xSize); - Global.mFileSystemDebugger.SendInternal($"content byte buffer written"); + // Copy the contents of the file from the source to the destination, creating the destination in the process + using (var src = new FileStream(sourceFullPath, FileMode.Open)) + using (var dst = new FileStream(destFullPath, overwrite ? FileMode.Create : FileMode.CreateNew)) + { + int xSize = (int)src.Length; + Global.mFileSystemDebugger.SendInternal($"size of {sourceFullPath} is {xSize} bytes"); + byte[] content = new byte[xSize]; + Global.mFileSystemDebugger.SendInternal($"content byte buffer allocated"); + src.Read(content, 0, xSize); + Global.mFileSystemDebugger.SendInternal($"content byte buffer read"); + dst.Write(content, 0, xSize); + Global.mFileSystemDebugger.SendInternal($"content byte buffer written"); + } } } } From d933d21263122bb9cbf879af5dddc28b1e77e85b Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Wed, 4 Jan 2023 21:29:38 +0200 Subject: [PATCH 12/16] Fix --- source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs index 2976a559e6..76661eaf6f 100644 --- a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs +++ b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs @@ -79,8 +79,11 @@ public static void MoveDirectory(string sourceFullPath, string destFullPath) public static void DeleteFile(string fullPath) { - Global.mFileSystemDebugger.SendInternal($"DeleteFile : fullPath = {fullPath}"); - VFSManager.DeleteFile(fullPath); + if (File.Exists(fullPath)) + { + Global.mFileSystemDebugger.SendInternal($"DeleteFile : fullPath = {fullPath}"); + VFSManager.DeleteFile(fullPath); + } } public static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite) From bd17e982de3b5890b91a4e21df65d5b437d1268f Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 28 May 2023 15:11:22 +0300 Subject: [PATCH 13/16] . --- source/Cosmos.Core_Plugs/System/ArrayImpl.cs | 10 +++---- .../CosmosDebugEnginePackage.cs | 30 +++++++++---------- .../Utilities/Extensions.cs | 25 ++++++++++++---- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/source/Cosmos.Core_Plugs/System/ArrayImpl.cs b/source/Cosmos.Core_Plugs/System/ArrayImpl.cs index 846bba9d31..5c3f567b4c 100644 --- a/source/Cosmos.Core_Plugs/System/ArrayImpl.cs +++ b/source/Cosmos.Core_Plugs/System/ArrayImpl.cs @@ -119,7 +119,7 @@ public static unsafe object GetValue(Array aThis, params int[] aIndices) } [PlugMethod(Signature = "System_Void__System_Array_SetValue_System_Object__System_Int32_")] - public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, uint aValue, int aIndex) + public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, [ObjectPointerAccess] uint* aValue, int aIndex) { aThis = (uint*) aThis[0]; aThis += 3; @@ -129,16 +129,16 @@ public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, uint aValu switch (xElementSize) { case 1: - *(byte*) aThis = (byte) aValue; + *(byte*) aThis = (byte) *aValue; return; case 2: - *(ushort*) aThis = (ushort) aValue; + *(ushort*) aThis = (ushort) *aValue; return; case 3: - *(uint*) aThis = (uint) aValue; + *(uint*) aThis = (uint) *aValue; return; case 4: - *(uint*) aThis = (uint) aValue; + *(uint*) aThis = (uint) *aValue; return; } throw new NotSupportedException("SetValue not supported in this situation!"); diff --git a/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs b/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs index 9bcccc28bd..0373037e88 100644 --- a/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs +++ b/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs @@ -8,23 +8,23 @@ using Microsoft.VisualStudio.Shell; using Task = System.Threading.Tasks.Task; -[assembly: ProvideBindingRedirection( - AssemblyName = "SQLitePCLRaw.batteries_v2", - NewVersion = "2.0.6.1341", - OldVersionLowerBound = "1.0.0.0", - OldVersionUpperBound = "2.0.6.1341")] +//[assembly: ProvideBindingRedirection( +// AssemblyName = "SQLitePCLRaw.batteries_v2", +// NewVersion = "2.0.6.1341", +// OldVersionLowerBound = "1.0.0.0", +// OldVersionUpperBound = "2.0.6.1341")] -[assembly: ProvideBindingRedirection( - AssemblyName = "SQLitePCLRaw.core", - NewVersion = "2.0.6.1341", - OldVersionLowerBound = "1.0.0.0", - OldVersionUpperBound = "2.0.6.1341")] +//[assembly: ProvideBindingRedirection( +// AssemblyName = "SQLitePCLRaw.core", +// NewVersion = "2.0.6.1341", +// OldVersionLowerBound = "1.0.0.0", +// OldVersionUpperBound = "2.0.6.1341")] -[assembly: ProvideBindingRedirection( - AssemblyName = "SQLitePCLRaw.provider.e_sqlite3", - NewVersion = "2.0.6.1341", - OldVersionLowerBound = "1.0.0.0", - OldVersionUpperBound = "2.0.6.1341")] +//[assembly: ProvideBindingRedirection( +// AssemblyName = "SQLitePCLRaw.provider.e_sqlite3", +// NewVersion = "2.0.6.1341", +// OldVersionLowerBound = "1.0.0.0", +// OldVersionUpperBound = "2.0.6.1341")] namespace Cosmos.VS.DebugEngine { diff --git a/source/Cosmos.VS.DebugEngine/Utilities/Extensions.cs b/source/Cosmos.VS.DebugEngine/Utilities/Extensions.cs index 0da698e20a..85c6b71284 100644 --- a/source/Cosmos.VS.DebugEngine/Utilities/Extensions.cs +++ b/source/Cosmos.VS.DebugEngine/Utilities/Extensions.cs @@ -71,12 +71,8 @@ public static string GetFullName(this MethodBase aMethod) return String.Intern(xBuilder.ToString()); } - public static string GetFullName(this Type aType) + public static string GetFullName(this Type aType,bool checkParam=false) { - if (aType.IsGenericParameter) - { - return aType.FullName; - } var xSB = new StringBuilder(); if (aType.IsArray) { @@ -103,7 +99,24 @@ public static string GetFullName(this Type aType) { xSB.Append(aType.FullName); } - if (aType.IsGenericType) + if (aType.IsGenericParameter) + { + if (checkParam) + { + xSB.Append(aType.DeclaringType.FullName); + xSB.Append(aType.DeclaringMethod?.Name); + var paramss = aType.DeclaringMethod?.GetParameters(); + if (paramss != null) + { + foreach (var item in paramss) + { + xSB.Append(GetFullName(item.ParameterType, false)); + } + } + } + xSB.Append(aType.Name); + } + if (aType.IsGenericType && !aType.IsGenericTypeDefinition) { xSB.Append("<"); var xArgs = aType.GetGenericArguments(); From 6208e17e8a1a1d76fdd96d538a039a61d0073c9a Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 28 May 2023 15:16:15 +0300 Subject: [PATCH 14/16] . --- .../CosmosDebugEnginePackage.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs b/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs index 0373037e88..9bcccc28bd 100644 --- a/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs +++ b/source/Cosmos.VS.DebugEngine/CosmosDebugEnginePackage.cs @@ -8,23 +8,23 @@ using Microsoft.VisualStudio.Shell; using Task = System.Threading.Tasks.Task; -//[assembly: ProvideBindingRedirection( -// AssemblyName = "SQLitePCLRaw.batteries_v2", -// NewVersion = "2.0.6.1341", -// OldVersionLowerBound = "1.0.0.0", -// OldVersionUpperBound = "2.0.6.1341")] +[assembly: ProvideBindingRedirection( + AssemblyName = "SQLitePCLRaw.batteries_v2", + NewVersion = "2.0.6.1341", + OldVersionLowerBound = "1.0.0.0", + OldVersionUpperBound = "2.0.6.1341")] -//[assembly: ProvideBindingRedirection( -// AssemblyName = "SQLitePCLRaw.core", -// NewVersion = "2.0.6.1341", -// OldVersionLowerBound = "1.0.0.0", -// OldVersionUpperBound = "2.0.6.1341")] +[assembly: ProvideBindingRedirection( + AssemblyName = "SQLitePCLRaw.core", + NewVersion = "2.0.6.1341", + OldVersionLowerBound = "1.0.0.0", + OldVersionUpperBound = "2.0.6.1341")] -//[assembly: ProvideBindingRedirection( -// AssemblyName = "SQLitePCLRaw.provider.e_sqlite3", -// NewVersion = "2.0.6.1341", -// OldVersionLowerBound = "1.0.0.0", -// OldVersionUpperBound = "2.0.6.1341")] +[assembly: ProvideBindingRedirection( + AssemblyName = "SQLitePCLRaw.provider.e_sqlite3", + NewVersion = "2.0.6.1341", + OldVersionLowerBound = "1.0.0.0", + OldVersionUpperBound = "2.0.6.1341")] namespace Cosmos.VS.DebugEngine { From fcd940be1f7cd26a1bbf3a1acee0ccfbdfe43ab4 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 28 May 2023 17:28:22 +0300 Subject: [PATCH 15/16] . --- source/Cosmos.Debug.Kernel/Debugger.cs | 39 ++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/source/Cosmos.Debug.Kernel/Debugger.cs b/source/Cosmos.Debug.Kernel/Debugger.cs index 5959bf6b22..9dda28dc20 100644 --- a/source/Cosmos.Debug.Kernel/Debugger.cs +++ b/source/Cosmos.Debug.Kernel/Debugger.cs @@ -264,26 +264,53 @@ public static void DoFail(int code, string message) public void Send(string[] aStringArray) => DoSend(aStringArray); + /// + /// Sends the given message to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(string aText) => DoSend(aText); + public virtual void SendInternal(string text) => DoSend(text); + /// + /// Sends the given strings to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(uint aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(string[] stringArray) => DoSend(stringArray); + /// + /// Sends the given 32-bit unsigned integer to all connected debugging hosts. + /// + [Conditional("COSMOSDEBUG")] + public virtual void SendInternal(uint number) => DoSendNumber(number); + + /// + /// Sends the given 32-bit signed integer to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(int aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(int number) => DoSendNumber(number); + /// + /// Sends the given 64-bit unsigned integer to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(ulong aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(ulong number) => DoSendNumber(number); + /// + /// Sends the given 64-bit signed integer to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(long aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(long number) => DoSendNumber(number); + /// + /// Sends the given 32-bit floating-point number to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] public virtual void SendInternal(float number) => DoSendNumber(number); + /// + /// Sends the given 64-bit floating-point number to all connected debugging hosts. + /// [Conditional("COSMOSDEBUG")] - public virtual void SendInternal(double aNumber) => DoSendNumber(aNumber); + public virtual void SendInternal(double number) => DoSendNumber(number); //public void OldSend(string aText) { // // TODO: Need to fix this so it can send empty strings. From e36fff97b311d59b90bd7ad2d222df4ab8b2c4f0 Mon Sep 17 00:00:00 2001 From: TheFocusMan Date: Sun, 28 May 2023 17:45:49 +0300 Subject: [PATCH 16/16] . --- source/Cosmos.System2/FileSystem/Disk.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs index 961a97cb0b..1d979b5917 100644 --- a/source/Cosmos.System2/FileSystem/Disk.cs +++ b/source/Cosmos.System2/FileSystem/Disk.cs @@ -66,9 +66,9 @@ public List Partitions foreach (var item in FindLostPartitions()) { var part = new ManagedPartition(item); - if (MountedPartitions[i] != null) + if (mountedPartitions[i] != null) { - var data = MountedPartitions[i]; + var data = mountedPartitions[i]; part.RootPath = data.RootPath; part.MountedFS = data; }