Skip to content

Commit 0d44bc9

Browse files
committed
Fix parsing of commands
1 parent a4cbd35 commit 0d44bc9

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Ultra.Core/Parser/UltraEventPipeProcessor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private void SamplerParserOnEventNativeModule(UltraNativeModuleTraceEvent evt)
9797
{
9898
if (evt.ModulePath is not null)
9999
{
100-
Console.WriteLine($"Module {evt.NativeModuleEventKind} Path: {evt.ModulePath}, LoadAddress: 0x{evt.LoadAddress:X}, Size: 0x{evt.Size:X}, Timestamp: {evt.TimestampUtc}");
100+
Console.WriteLine($"Module {evt.NativeModuleEventKind} Path: {evt.ModulePath}, LoadAddress: 0x{evt.LoadAddress:X}, Size: 0x{evt.Size:X}, Timestamp: {evt.TimestampUtc}, Uuid: {evt.Uuid}");
101101

102102
if (evt.NativeModuleEventKind == UltraSamplerNativeModuleEventKind.Unloaded)
103103
{

src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,17 @@ private static bool TryGetUuidFromMacHeader(nint headerPtr, out Guid guid)
243243
if (header->magic != MacOSLibSystem.MH_MAGIC_64) throw new InvalidOperationException("Invalid magic header");
244244

245245
var nbCommands = header->ncmds;
246-
var commands = (MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
247-
for(uint i = 0; i < nbCommands; i++)
246+
ref var firstCommand = ref *(MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
247+
ref var command = ref firstCommand;
248+
for (uint i = 0; i < nbCommands; i++)
248249
{
249-
var command = commands[i];
250250
if (command.cmd == MacOSLibSystem.LC_UUID)
251251
{
252-
var uuidCommand = (MacOSLibSystem.uuid_command*)Unsafe.AsPointer(ref command);
253-
guid = uuidCommand->uuid;
252+
ref var uuidCommand = ref Unsafe.As<MacOSLibSystem.load_command, MacOSLibSystem.uuid_command>(ref command);
253+
guid = uuidCommand.uuid;
254254
return true;
255255
}
256+
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
256257
}
257258

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

269270
var nbCommands = header->ncmds;
270-
var commands = (MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
271-
for(uint i = 0; i < nbCommands; i++)
271+
ref var firstCommand = ref *(MacOSLibSystem.load_command*)((byte*)header + sizeof(MacOSLibSystem.mach_header_64));
272+
ref var command = ref firstCommand;
273+
for (uint i = 0; i < nbCommands; i++)
272274
{
273-
ref var command = ref commands[i];
274275
if (command.cmd == MacOSLibSystem.LC_SEGMENT_64)
275276
{
276277
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command,MacOSLibSystem.segment_command_64>(ref command);
@@ -279,13 +280,14 @@ private static ulong GetDyldCodeSize(nint headerPtr)
279280
startAddress = segment.vmaddr;
280281
}
281282
}
283+
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
282284
}
283285

284286
if (startAddress == ulong.MaxValue) return 0;
285-
287+
288+
command = ref firstCommand;
286289
for (uint i = 0; i < nbCommands; i++)
287290
{
288-
ref var command = ref commands[i];
289291
if (command.cmd == MacOSLibSystem.LC_SEGMENT_64)
290292
{
291293
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command, MacOSLibSystem.segment_command_64>(ref command);
@@ -296,6 +298,7 @@ private static ulong GetDyldCodeSize(nint headerPtr)
296298
size = newSize;
297299
}
298300
}
301+
command = ref Unsafe.AddByteOffset(ref command, command.cmdsize);
299302
}
300303

301304
return size;

0 commit comments

Comments
 (0)