From a25838f5460d6fcef499be1be15c6fdaa62e2874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Mon, 11 Jul 2005 14:42:48 +0000 Subject: [PATCH] Removed length limitation on unmanaged strings git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@172 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Debugger/NDebugger.cs | 15 --- .../Project/Src/Modules/Module.cs | 19 ++-- .../Project/Src/Threads/Function.cs | 65 ++++++++++--- .../Project/Src/Variables/BuiltInVariable.cs | 25 ++++- .../Project/Src/Variables/ObjectVariable.cs | 92 ++++++++++++++----- 5 files changed, 154 insertions(+), 62 deletions(-) diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs index 90418aa36e..62ee1455ff 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs @@ -26,20 +26,6 @@ namespace DebuggerLibrary } } - - // Some variables that are used to get strings - // They are used all over the library and - // they are here so I don't have to decare them every time I need them - static internal uint unused; - static internal IntPtr unusedPtr = IntPtr.Zero; - internal const int pStringLen = 65536; - static internal IntPtr pString = Marshal.AllocHGlobal(pStringLen*2); - static internal string pStringAsUnicode { - get { - return Marshal.PtrToStringUni(pString); - } - } - static ICorDebug corDebug; static ManagedCallback managedCallback; static ManagedCallbackProxy managedCallbackProxy; @@ -94,7 +80,6 @@ namespace DebuggerLibrary ~NDebugger() //TODO { //corDebug.Terminate(); - Marshal.FreeHGlobal(pString); } static internal void InitDebugger() diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs index c4c86df04e..548598179b 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs @@ -108,12 +108,19 @@ namespace DebuggerLibrary pModule.GetMetaDataInterface(ref metaDataInterfaceGuid, out pMetaDataInterface); metaDataInterface = (IMetaDataImport) pMetaDataInterface; - - pModule.GetName(NDebugger.pStringLen, - out NDebugger.unused, // real string lenght - NDebugger.pString); - fullPath = NDebugger.pStringAsUnicode; - + + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; + pModule.GetName(pStringLenght, + out pStringLenght, // real string lenght + pString); + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + pModule.GetName(pStringLenght, + out pStringLenght, // real string lenght + pString); + fullPath = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); SymBinder symBinder = new SymBinder(); diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs index a8dcee980d..1b1371f08b 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs @@ -51,7 +51,9 @@ namespace DebuggerLibrary } unsafe void Init() - { + { + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; uint codeRVA; uint implFlags; IntPtr pSigBlob; @@ -59,15 +61,31 @@ namespace DebuggerLibrary module.MetaDataInterface.GetMethodProps( (uint)token.GetToken(), out parentClassToken, - NDebugger.pString, - NDebugger.pStringLen, - out NDebugger.unused, // real string lenght + pString, + pStringLenght, + out pStringLenght, // real string lenght + out attributes, + new IntPtr(&pSigBlob), + out sigBlobSize, + out codeRVA, + out implFlags); + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + + module.MetaDataInterface.GetMethodProps( + (uint)token.GetToken(), + out parentClassToken, + pString, + pStringLenght, + out pStringLenght, // real string lenght out attributes, new IntPtr(&pSigBlob), out sigBlobSize, out codeRVA, out implFlags); - name = NDebugger.pStringAsUnicode; + + name = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); SignatureStream sig = new SignatureStream(pSigBlob, sigBlobSize); @@ -303,23 +321,40 @@ namespace DebuggerLibrary if (paramsFetched == 0) break; ICorDebugValue arg; - corILFrame.GetArgument(i, out arg); - + corILFrame.GetArgument(i, out arg); + + uint unused; + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; uint argPos, attr, type; - Module.MetaDataInterface.GetParamProps( - paramToken, - out NDebugger.unused, + Module.MetaDataInterface.GetParamProps(paramToken, + out unused, out argPos, - NDebugger.pString, - NDebugger.pStringLen, - out NDebugger.unused, // real string lenght + pString, + pStringLenght, + out pStringLenght, // real string lenght out attr, out type, IntPtr.Zero, - out NDebugger.unused); + out unused); + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + Module.MetaDataInterface.GetParamProps(paramToken, + out unused, + out argPos, + pString, + pStringLenght, + out pStringLenght, // real string lenght + out attr, + out type, + IntPtr.Zero, + out unused); + + string name = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); - collection.Add(VariableFactory.CreateVariable(arg, NDebugger.pStringAsUnicode)); + collection.Add(VariableFactory.CreateVariable(arg, name)); } // local variables diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/BuiltInVariable.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/BuiltInVariable.cs index ac270a353d..0dc8fd7b18 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/BuiltInVariable.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/BuiltInVariable.cs @@ -17,10 +17,27 @@ namespace DebuggerLibrary { if (CorType == CorElementType.STRING) { - ((ICorDebugStringValue)corValue).GetString(NDebugger.pStringLen, - out NDebugger.unused, - NDebugger.pString); - return NDebugger.pStringAsUnicode; + uint pStringLenght = 1; // Terminating character NOT included in pStringLenght + IntPtr pString = Marshal.AllocHGlobal(2); + + // For some reason this function does not accept IntPtr.Zero + ((ICorDebugStringValue)corValue).GetString(pStringLenght, + out pStringLenght, + pString); + // Re-allocate string buffer + Marshal.FreeHGlobal(pString); + // Termination null is not included in pStringLenght + pStringLenght++; + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + + ((ICorDebugStringValue)corValue).GetString(pStringLenght, + out pStringLenght, + pString); + + string text = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); + + return text; } object retValue; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/ObjectVariable.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/ObjectVariable.cs index 27ca0a2149..163b83afe2 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/ObjectVariable.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/ObjectVariable.cs @@ -18,10 +18,11 @@ namespace DebuggerLibrary ICorDebugClass corClass; ICorDebugModule corModule; IMetaDataImport metaData; - uint classToken; - uint superCallsToken; ICorDebugModule corModuleSuperclass; ObjectVariable baseClass; + uint classToken; + uint superCallsToken; + uint typeDefFlags; string type; public override object Value { @@ -55,13 +56,27 @@ namespace DebuggerLibrary corClass.GetModule(out corModule); metaData = new Module(corModule).MetaDataInterface; //TODO + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; + // Get length of string 'type' metaData.GetTypeDefProps(classToken, - NDebugger.pString, - NDebugger.pStringLen, - out NDebugger.unused, // real string lenght - out NDebugger.unused, + pString, + pStringLenght, + out pStringLenght, + out typeDefFlags, out superCallsToken); - type = NDebugger.pStringAsUnicode; + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + // Get properties + metaData.GetTypeDefProps(classToken, + pString, + pStringLenght, + out pStringLenght, + out typeDefFlags, + out superCallsToken); + type = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); + corModuleSuperclass = corModule; } @@ -82,19 +97,39 @@ namespace DebuggerLibrary break; } + uint unused; + IntPtr unusedPtr = IntPtr.Zero; + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; uint attrib; metaData.GetFieldProps(fieldToken, - out NDebugger.unused, - NDebugger.pString, - NDebugger.pStringLen, - out NDebugger.unused, // real string lenght + out unused, + pString, + pStringLenght, + out pStringLenght, // real string lenght out attrib, - IntPtr.Zero, - out NDebugger.unused, - out NDebugger.unused, - out NDebugger.unusedPtr, - out NDebugger.unused); - string name = NDebugger.pStringAsUnicode; + IntPtr.Zero, + out unused, + out unused, + out unusedPtr, + out unused); + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + + metaData.GetFieldProps(fieldToken, + out unused, + pString, + pStringLenght, + out pStringLenght, // real string lenght + out attrib, + IntPtr.Zero, + out unused, + out unused, + out unusedPtr, + out unused); + + string name = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); ICorDebugValue innerValue; if ((attrib & (uint)ClassFieldAttribute.fdStatic)!=0) @@ -137,12 +172,25 @@ namespace DebuggerLibrary // If referencing to external assembly if ((superCallsToken & 0x01000000) != 0) { + uint unused; + uint pStringLenght = 0; // Terminating character included in pStringLenght + IntPtr pString = IntPtr.Zero; metaData.GetTypeRefProps(superCallsToken, - out NDebugger.unused, - NDebugger.pString, - NDebugger.pStringLen, - out NDebugger.unused); // real string lenght - fullTypeName = NDebugger.pStringAsUnicode; + out unused, + pString, + pStringLenght, + out pStringLenght); // real string lenght + // Allocate string buffer + pString = Marshal.AllocHGlobal((int)pStringLenght * 2); + + metaData.GetTypeRefProps(superCallsToken, + out unused, + pString, + pStringLenght, + out pStringLenght); // real string lenght + + fullTypeName = Marshal.PtrToStringUni(pString); + Marshal.FreeHGlobal(pString); superCallsToken = 0; foreach (Module m in NDebugger.Instance.Modules)