Skip to content

Commit

Permalink
Change return value type from HResult to int to avoid crash on Linux x86
Browse files Browse the repository at this point in the history
  • Loading branch information
o-lek committed Jun 16, 2022
1 parent a513fed commit 4bacd46
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 100 deletions.
92 changes: 46 additions & 46 deletions src/SOS/SOS.Extensions/DebuggerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ internal DebuggerServices(IntPtr punk, HostType hostType)
_hostType = hostType;
}

public HResult GetOperatingSystem(out DebuggerServices.OperatingSystem operatingSystem)
public int GetOperatingSystem(out DebuggerServices.OperatingSystem operatingSystem)
{
return VTable.GetOperatingSystem(Self, out operatingSystem);
}

public HResult GetDebuggeeType(out DEBUG_CLASS debugClass, out DEBUG_CLASS_QUALIFIER qualifier)
public int GetDebuggeeType(out DEBUG_CLASS debugClass, out DEBUG_CLASS_QUALIFIER qualifier)
{
return VTable.GetDebuggeeType(Self, out debugClass, out qualifier);
}

public HResult GetExecutingProcessorType(out IMAGE_FILE_MACHINE type)
public int GetExecutingProcessorType(out IMAGE_FILE_MACHINE type)
{
return VTable.GetExecutingProcessorType(Self, out type);
}

public HResult AddCommand(string command, string help, IEnumerable<string> aliases)
public int AddCommand(string command, string help, IEnumerable<string> aliases)
{
if (string.IsNullOrEmpty(command) || string.IsNullOrEmpty(help) || aliases == null) throw new ArgumentNullException();

Expand Down Expand Up @@ -88,28 +88,28 @@ public void OutputString(DEBUG_OUTPUT mask, string message)
}
}

public HResult ReadVirtual(ulong offset, Span<byte> buffer, out int bytesRead)
public int ReadVirtual(ulong offset, Span<byte> buffer, out int bytesRead)
{
fixed (byte* bufferPtr = buffer)
{
return VTable.ReadVirtual(Self, offset, bufferPtr, (uint)buffer.Length, out bytesRead);
}
}

public HResult WriteVirtual(ulong offset, Span<byte> buffer, out int bytesWritten)
public int WriteVirtual(ulong offset, Span<byte> buffer, out int bytesWritten)
{
fixed (byte* bufferPtr = buffer)
{
return VTable.WriteVirtual(Self, offset, bufferPtr, (uint)buffer.Length, out bytesWritten);
}
}

public HResult GetNumberModules(out uint loaded, out uint unloaded)
public int GetNumberModules(out uint loaded, out uint unloaded)
{
return VTable.GetNumberModules(Self, out loaded, out unloaded);
}

public HResult GetModuleName(int index, out string imageName)
public int GetModuleName(int index, out string imageName)
{
imageName = null;

Expand Down Expand Up @@ -148,14 +148,14 @@ public HResult GetModuleName(int index, out string imageName)
}
}

public HResult GetModuleInfo(int index, out ulong moduleBase, out ulong moduleSize, out uint timestamp, out uint checksum)
public int GetModuleInfo(int index, out ulong moduleBase, out ulong moduleSize, out uint timestamp, out uint checksum)
{
return VTable.GetModuleInfo(Self, (uint)index, out moduleBase, out moduleSize, out timestamp, out checksum);
}

private static readonly byte[] s_getVersionInfo = Encoding.ASCII.GetBytes("\\\0");

public HResult GetModuleVersionInformation(int index, out VS_FIXEDFILEINFO fileInfo)
public int GetModuleVersionInformation(int index, out VS_FIXEDFILEINFO fileInfo)
{
int versionBufferSize = Marshal.SizeOf(typeof(VS_FIXEDFILEINFO));
byte[] versionBuffer = new byte[versionBufferSize];
Expand All @@ -175,7 +175,7 @@ public HResult GetModuleVersionInformation(int index, out VS_FIXEDFILEINFO fileI

private static readonly byte[] s_getVersionString = Encoding.ASCII.GetBytes("\\StringFileInfo\\040904B0\\FileVersion\0");

public HResult GetModuleVersionString(int index, out string version)
public int GetModuleVersionString(int index, out string version)
{
byte[] versionBuffer = new byte[1024];
version = default;
Expand All @@ -192,12 +192,12 @@ public HResult GetModuleVersionString(int index, out string version)
}
}

public HResult GetNumberThreads(out uint number)
public int GetNumberThreads(out uint number)
{
return VTable.GetNumberThreads(Self, out number);
}

public HResult GetThreadIdsByIndex(uint start, uint count, uint[] ids, uint[] sysIds)
public int GetThreadIdsByIndex(uint start, uint count, uint[] ids, uint[] sysIds)
{
if (ids != null && (start >= ids.Length || start + count > ids.Length)) throw new ArgumentOutOfRangeException(nameof(ids));
if (sysIds != null && (start >= sysIds.Length || start + count > sysIds.Length)) throw new ArgumentOutOfRangeException(nameof(sysIds));
Expand All @@ -211,45 +211,45 @@ public HResult GetThreadIdsByIndex(uint start, uint count, uint[] ids, uint[] sy
}
}

public HResult GetThreadContext(uint threadId, uint contextFlags, uint contextSize, byte[] context)
public int GetThreadContext(uint threadId, uint contextFlags, uint contextSize, byte[] context)
{
fixed (byte* contextPtr = context)
{
return VTable.GetThreadContextBySystemId(Self, threadId, contextFlags, contextSize, contextPtr);
}
}

public HResult GetCurrentProcessId(out uint processId)
public int GetCurrentProcessId(out uint processId)
{
return VTable.GetCurrentProcessSystemId(Self, out processId);
}

public HResult GetCurrentThreadId(out uint threadId)
public int GetCurrentThreadId(out uint threadId)
{
return VTable.GetCurrentThreadSystemId(Self, out threadId);
}

public HResult SetCurrentThreadId(uint threadId)
public int SetCurrentThreadId(uint threadId)
{
return VTable.SetCurrentThreadSystemId(Self, threadId);
}

public HResult GetThreadTeb(uint threadId, out ulong teb)
public int GetThreadTeb(uint threadId, out ulong teb)
{
// The native code may zero out this return pointer
teb = 0;
return VTable.GetThreadTeb(Self, threadId, out teb);
}

public HResult VirtualUnwind(uint threadId, uint contextSize, byte[] context)
public int VirtualUnwind(uint threadId, uint contextSize, byte[] context)
{
fixed (byte* contextPtr = context)
{
return VTable.VirtualUnwind(Self, threadId, contextSize, contextPtr);
}
}

public HResult GetSymbolPath(out string symbolPath)
public int GetSymbolPath(out string symbolPath)
{
symbolPath = null;

Expand Down Expand Up @@ -278,7 +278,7 @@ public HResult GetSymbolPath(out string symbolPath)
return hr;
}

public HResult GetSymbolByOffset(int moduleIndex, ulong address, out string symbol, out ulong displacement)
public int GetSymbolByOffset(int moduleIndex, ulong address, out string symbol, out ulong displacement)
{
symbol = null;

Expand Down Expand Up @@ -315,7 +315,7 @@ public HResult GetSymbolByOffset(int moduleIndex, ulong address, out string symb
return hr;
}

public HResult GetOffsetBySymbol(int moduleIndex, string symbol, out ulong address)
public int GetOffsetBySymbol(int moduleIndex, string symbol, out ulong address)
{
if (symbol == null) throw new ArgumentNullException(nameof(symbol));

Expand Down Expand Up @@ -349,7 +349,7 @@ public void OutputDmlString(DEBUG_OUTPUT mask, string message)
}
}

public HResult AddModuleSymbol(string symbolFileName)
public int AddModuleSymbol(string symbolFileName)
{
if (symbolFileName == null)
{
Expand All @@ -365,32 +365,32 @@ public HResult AddModuleSymbol(string symbolFileName)
[StructLayout(LayoutKind.Sequential)]
private readonly unsafe struct IDebuggerServicesVTable
{
public readonly delegate* unmanaged[Stdcall]<IntPtr, out DebuggerServices.OperatingSystem, HResult> GetOperatingSystem;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out DEBUG_CLASS, out DEBUG_CLASS_QUALIFIER, HResult> GetDebuggeeType;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out IMAGE_FILE_MACHINE, HResult> GetExecutingProcessorType;
public readonly delegate* unmanaged[Stdcall]<IntPtr, byte*, byte*, IntPtr*, int, HResult> AddCommand;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out DebuggerServices.OperatingSystem, int> GetOperatingSystem;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out DEBUG_CLASS, out DEBUG_CLASS_QUALIFIER, int> GetDebuggeeType;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out IMAGE_FILE_MACHINE, int> GetExecutingProcessorType;
public readonly delegate* unmanaged[Stdcall]<IntPtr, byte*, byte*, IntPtr*, int, int> AddCommand;
public readonly delegate* unmanaged[Stdcall]<IntPtr, DEBUG_OUTPUT, byte*, void> OutputString;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, byte*, uint, out int, HResult> ReadVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, byte*, uint, out int, HResult> WriteVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, out uint, HResult> GetNumberModules;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, ulong, byte*, uint, out uint, byte*, uint, uint*, byte*, uint, uint*, HResult> GetModuleNames;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, out ulong, out ulong, out uint, out uint, HResult> GetModuleInfo;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, ulong, byte*, byte*, uint, uint*, HResult> GetModuleVersionInformation;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, HResult> GetNumberThreads;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, uint*, uint*, HResult> GetThreadIdsByIndex;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, uint, byte*, HResult> GetThreadContextBySystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, HResult> GetCurrentProcessSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, HResult> GetCurrentThreadSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, HResult> SetCurrentThreadSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, out ulong, HResult> GetThreadTeb;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, byte*, HResult> VirtualUnwind;
public readonly delegate* unmanaged[Stdcall]<IntPtr, byte*, uint, out uint, HResult> GetSymbolPath;
public readonly delegate* unmanaged[Stdcall]<IntPtr, int, ulong, byte*, int, out uint, out ulong, HResult> GetSymbolByOffset;
public readonly delegate* unmanaged[Stdcall]<IntPtr, int, byte*, out ulong, HResult> GetOffsetBySymbol;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, byte*, uint, out int, int> ReadVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, byte*, uint, out int, int> WriteVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, out uint, int> GetNumberModules;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, ulong, byte*, uint, out uint, byte*, uint, uint*, byte*, uint, uint*, int> GetModuleNames;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, out ulong, out ulong, out uint, out uint, int> GetModuleInfo;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, ulong, byte*, byte*, uint, uint*, int> GetModuleVersionInformation;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, int> GetNumberThreads;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, uint*, uint*, int> GetThreadIdsByIndex;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, uint, byte*, int> GetThreadContextBySystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, int> GetCurrentProcessSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, out uint, int> GetCurrentThreadSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, int> SetCurrentThreadSystemId;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, out ulong, int> GetThreadTeb;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint, uint, byte*, int> VirtualUnwind;
public readonly delegate* unmanaged[Stdcall]<IntPtr, byte*, uint, out uint, int> GetSymbolPath;
public readonly delegate* unmanaged[Stdcall]<IntPtr, int, ulong, byte*, int, out uint, out ulong, int> GetSymbolByOffset;
public readonly delegate* unmanaged[Stdcall]<IntPtr, int, byte*, out ulong, int> GetOffsetBySymbol;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint> GetOutputWidth;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint*, HResult> SupportsDml;
public readonly delegate* unmanaged[Stdcall]<IntPtr, uint*, int> SupportsDml;
public readonly delegate* unmanaged[Stdcall]<IntPtr, DEBUG_OUTPUT, byte*, void> OutputDmlString;
public readonly delegate* unmanaged[Stdcall]<IntPtr, IntPtr, byte*, HResult> AddModuleSymbol;
public readonly delegate* unmanaged[Stdcall]<IntPtr, IntPtr, byte*, int> AddModuleSymbol;
}
}
}
26 changes: 13 additions & 13 deletions src/SOS/SOS.Extensions/HostServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed unsafe class HostServices : COMCallableIUnknown, IHost
/// <param name="hostServices">The instance of the host services for the native code to use</param>
/// <returns></returns>
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate HResult InitializeCallbackDelegate(
private delegate int InitializeCallbackDelegate(
IntPtr hostServices);

internal IntPtr IHostServices { get; }
Expand Down Expand Up @@ -177,7 +177,7 @@ public void DestroyTarget(ITarget target)

#region IHostServices

private HResult GetHost(
private int GetHost(
IntPtr self,
out IntPtr host)
{
Expand All @@ -186,7 +186,7 @@ private HResult GetHost(
return HResult.S_OK;
}

private HResult RegisterDebuggerServices(
private int RegisterDebuggerServices(
IntPtr self,
IntPtr iunk)
{
Expand Down Expand Up @@ -257,7 +257,7 @@ private HResult RegisterDebuggerServices(
return HResult.S_OK;
}

private HResult CreateTarget(
private int CreateTarget(
IntPtr self)
{
Trace.TraceInformation("HostServices.CreateTarget");
Expand All @@ -279,7 +279,7 @@ private HResult CreateTarget(
return HResult.S_OK;
}

private HResult UpdateTarget(
private int UpdateTarget(
IntPtr self,
uint processId)
{
Expand Down Expand Up @@ -323,7 +323,7 @@ private void DestroyTarget(
}
}

private HResult DispatchCommand(
private int DispatchCommand(
IntPtr self,
string commandLine)
{
Expand All @@ -342,7 +342,7 @@ private HResult DispatchCommand(
return HResult.E_FAIL;
}

private HResult DisplayHelp(
private int DisplayHelp(
IntPtr self,
string command)
{
Expand Down Expand Up @@ -395,21 +395,21 @@ private void Uninitialize(
#region IHostServices delegates

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult GetHostDelegate(
private delegate int GetHostDelegate(
[In] IntPtr self,
[Out] out IntPtr host);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult RegisterDebuggerServicesDelegate(
private delegate int RegisterDebuggerServicesDelegate(
[In] IntPtr self,
[In] IntPtr iunk);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult CreateTargetDelegate(
private delegate int CreateTargetDelegate(
[In] IntPtr self);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult UpdateTargetDelegate(
private delegate int UpdateTargetDelegate(
[In] IntPtr self,
[In] uint processId);

Expand All @@ -422,12 +422,12 @@ private delegate void DestroyTargetDelegate(
[In] IntPtr self);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult DispatchCommandDelegate(
private delegate int DispatchCommandDelegate(
[In] IntPtr self,
[In, MarshalAs(UnmanagedType.LPStr)] string commandLine);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate HResult DisplayHelpDelegate(
private delegate int DisplayHelpDelegate(
[In] IntPtr self,
[In, MarshalAs(UnmanagedType.LPStr)] string command);

Expand Down
9 changes: 6 additions & 3 deletions src/SOS/SOS.Extensions/ModuleServiceFromDebuggerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,22 @@ public override string LoadSymbols()

bool IModuleSymbols.TryGetSymbolName(ulong address, out string symbol, out ulong displacement)
{
return _moduleService._debuggerServices.GetSymbolByOffset(ModuleIndex, address, out symbol, out displacement).IsOK;
HResult hr = _moduleService._debuggerServices.GetSymbolByOffset(ModuleIndex, address, out symbol, out displacement);
return hr.IsOK;
}

bool IModuleSymbols.TryGetSymbolAddress(string name, out ulong address)
{
return _moduleService._debuggerServices.GetOffsetBySymbol(ModuleIndex, name, out address).IsOK;
HResult hr = _moduleService._debuggerServices.GetOffsetBySymbol(ModuleIndex, name, out address);
return hr.IsOK;
}

#endregion

protected override bool TryGetSymbolAddressInner(string name, out ulong address)
{
return _moduleService._debuggerServices.GetOffsetBySymbol(ModuleIndex, name, out address).IsOK;
HResult hr = _moduleService._debuggerServices.GetOffsetBySymbol(ModuleIndex, name, out address);
return hr.IsOK;
}

protected override ModuleService ModuleService => _moduleService;
Expand Down
4 changes: 2 additions & 2 deletions src/SOS/SOS.Extensions/RemoteMemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public bool FreeMemory(ulong address, uint size, uint typeFlags)
[StructLayout(LayoutKind.Sequential)]
private readonly unsafe struct IRemoteMemoryServiceVTable
{
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, uint, uint, uint, out ulong, HResult> AllocVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, uint, uint, HResult> FreeVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, uint, uint, uint, out ulong, int> AllocVirtual;
public readonly delegate* unmanaged[Stdcall]<IntPtr, ulong, uint, uint, int> FreeVirtual;
}
}
}
Loading

0 comments on commit 4bacd46

Please sign in to comment.