Skip to content

Commit

Permalink
Fix parsing of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 17, 2024
1 parent a4cbd35 commit 0d44bc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Ultra.Core/Parser/UltraEventPipeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void SamplerParserOnEventNativeModule(UltraNativeModuleTraceEvent evt)
{
if (evt.ModulePath is not null)
{
Console.WriteLine($"Module {evt.NativeModuleEventKind} Path: {evt.ModulePath}, LoadAddress: 0x{evt.LoadAddress:X}, Size: 0x{evt.Size:X}, Timestamp: {evt.TimestampUtc}");
Console.WriteLine($"Module {evt.NativeModuleEventKind} Path: {evt.ModulePath}, LoadAddress: 0x{evt.LoadAddress:X}, Size: 0x{evt.Size:X}, Timestamp: {evt.TimestampUtc}, Uuid: {evt.Uuid}");

if (evt.NativeModuleEventKind == UltraSamplerNativeModuleEventKind.Unloaded)
{
Expand Down
23 changes: 13 additions & 10 deletions src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,17 @@ private static bool TryGetUuidFromMacHeader(nint headerPtr, out Guid guid)
if (header->magic != MacOSLibSystem.MH_MAGIC_64) throw new InvalidOperationException("Invalid magic header");

var nbCommands = header->ncmds;
var commands = (MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
for(uint i = 0; i < nbCommands; i++)
ref var firstCommand = ref *(MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
ref var command = ref firstCommand;
for (uint i = 0; i < nbCommands; i++)
{
var command = commands[i];
if (command.cmd == MacOSLibSystem.LC_UUID)
{
var uuidCommand = (MacOSLibSystem.uuid_command*)Unsafe.AsPointer(ref command);
guid = uuidCommand->uuid;
ref var uuidCommand = ref Unsafe.As<MacOSLibSystem.load_command, MacOSLibSystem.uuid_command>(ref command);
guid = uuidCommand.uuid;
return true;
}
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
}

return false;
Expand All @@ -267,10 +268,10 @@ private static ulong GetDyldCodeSize(nint headerPtr)
if (header->magic != MacOSLibSystem.MH_MAGIC_64) throw new InvalidOperationException("Invalid magic header");

var nbCommands = header->ncmds;
var commands = (MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
for(uint i = 0; i < nbCommands; i++)
ref var firstCommand = ref *(MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
ref var command = ref firstCommand;
for (uint i = 0; i < nbCommands; i++)
{
ref var command = ref commands[i];
if (command.cmd == MacOSLibSystem.LC_SEGMENT_64)
{
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command,MacOSLibSystem.segment_command_64>(ref command);
Expand All @@ -279,13 +280,14 @@ private static ulong GetDyldCodeSize(nint headerPtr)
startAddress = segment.vmaddr;
}
}
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
}

if (startAddress == ulong.MaxValue) return 0;


command = ref firstCommand;
for (uint i = 0; i < nbCommands; i++)
{
ref var command = ref commands[i];
if (command.cmd == MacOSLibSystem.LC_SEGMENT_64)
{
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command, MacOSLibSystem.segment_command_64>(ref command);
Expand All @@ -296,6 +298,7 @@ private static ulong GetDyldCodeSize(nint headerPtr)
size = newSize;
}
}
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
}

return size;
Expand Down

0 comments on commit 0d44bc9

Please sign in to comment.