diff --git a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs index f0eb8ed..ecc793d 100644 --- a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs +++ b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs @@ -146,7 +146,7 @@ private void AddModuleEvent(NativeModuleEventKind kind, nint loadAddress) var path = MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)info.dli_fname); evt.Path = path.ToArray(); evt.TimestampUtc = DateTime.UtcNow; - evt.Size = GetMaximumCodeAddress(loadAddress); + evt.Size = GetDyldCodeSize(loadAddress); lock (_moduleEventLock) { @@ -197,9 +197,9 @@ private static bool TryGetUuidFromMacHeader(nint headerPtr, out Guid guid) return false; } - private static ulong GetMaximumCodeAddress(nint headerPtr) + private static ulong GetDyldCodeSize(nint headerPtr) { - ulong startAddress = 0; + ulong startAddress = ulong.MaxValue; ulong size = 0; var header = (MacOSLibSystem.mach_header_64*)headerPtr; @@ -213,22 +213,30 @@ private static ulong GetMaximumCodeAddress(nint headerPtr) if (command.cmd == MacOSLibSystem.LC_SEGMENT_64) { ref var segment = ref Unsafe.As(ref command); - if (segment.vmaddr != 0) + if (segment.vmaddr < startAddress) { - if (startAddress == 0) - { - startAddress = segment.vmaddr; - } - - var newSize = (ulong)((long)segment.vmaddr + (long)segment.vmsize - (long)startAddress); - if (newSize > size) - { - size = newSize; - } + startAddress = segment.vmaddr; } } } + if (startAddress == ulong.MaxValue) return 0; + + 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(ref command); + + var newSize = (ulong)((long)segment.vmaddr + (long)segment.vmsize - (long)startAddress); + if (newSize > size) + { + size = newSize; + } + } + } + return size; }