Skip to content

Commit

Permalink
Add OS version to crash report message (#12098)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored and dylan-conway committed Jun 25, 2024
1 parent 5c8bbcc commit b8e183a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/analytics/analytics_thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,45 +260,47 @@ const platform_arch = if (Environment.isAarch64) Analytics.Architecture.arm else
pub const GenerateHeader = struct {
pub const GeneratePlatform = struct {
var osversion_name: [32]u8 = undefined;
pub fn forMac() Analytics.Platform {
fn forMac() Analytics.Platform {
@memset(&osversion_name, 0);

var platform = Analytics.Platform{ .os = Analytics.OperatingSystem.macos, .version = &[_]u8{}, .arch = platform_arch };
var len = osversion_name.len - 1;
if (std.c.sysctlbyname("kern.osrelease", &osversion_name, &len, null, 0) == -1) return platform;
// this previously used "kern.osrelease", which was the darwin xnu kernel version
// That is less useful than "kern.osproductversion", which is the macOS version
if (std.c.sysctlbyname("kern.osproductversion", &osversion_name, &len, null, 0) == -1) return platform;

platform.version = bun.sliceTo(&osversion_name, 0);
return platform;
}

pub var linux_os_name: std.c.utsname = undefined;
var platform_: ?Analytics.Platform = null;
var platform_: Analytics.Platform = undefined;
pub const Platform = Analytics.Platform;

var linux_kernel_version: Semver.Version = undefined;

pub fn forOS() Analytics.Platform {
if (platform_ != null) return platform_.?;

if (comptime Environment.isMac) {
platform_ = forMac();
return platform_.?;
} else if (comptime Environment.isPosix) {
platform_ = forLinux();

const release = bun.sliceTo(&linux_os_name.release, 0);
const sliced_string = Semver.SlicedString.init(release, release);
const result = Semver.Version.parse(sliced_string);
linux_kernel_version = result.version.min();
} else {
platform_ = Platform{
.os = Analytics.OperatingSystem.windows,
.version = &[_]u8{},
.arch = platform_arch,
};
var run_once = std.once(struct {
fn run() void {
if (comptime Environment.isMac) {
platform_ = forMac();
} else if (comptime Environment.isPosix) {
platform_ = forLinux();

const release = bun.sliceTo(&linux_os_name.release, 0);
const sliced_string = Semver.SlicedString.init(release, release);
const result = Semver.Version.parse(sliced_string);
linux_kernel_version = result.version.min();
} else if (Environment.isWindows) {
platform_ = Platform{
.os = Analytics.OperatingSystem.windows,
.version = &[_]u8{},
.arch = platform_arch,
};
}
}
}.run);

return platform_.?;
pub fn forOS() Analytics.Platform {
run_once.call();
return platform_;
}

pub fn kernelVersion() Semver.Version {
Expand All @@ -307,23 +309,23 @@ pub const GenerateHeader = struct {
}
_ = forOS();

// we only care about major, minor, patch so we don't care about the string
return linux_kernel_version;
}

pub fn forLinux() Analytics.Platform {
fn forLinux() Analytics.Platform {
linux_os_name = std.mem.zeroes(@TypeOf(linux_os_name));

_ = std.c.uname(&linux_os_name);

// Confusingly, the "release" tends to contain the kernel version much more frequently than the "version" field.
const release = bun.sliceTo(&linux_os_name.release, 0);
const version = std.mem.sliceTo(&linux_os_name.version, @as(u8, 0));

// Linux DESKTOP-P4LCIEM 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
if (std.mem.indexOf(u8, release, "microsoft") != null) {
return Analytics.Platform{ .os = Analytics.OperatingSystem.wsl, .version = version, .arch = platform_arch };
return Analytics.Platform{ .os = Analytics.OperatingSystem.wsl, .version = release, .arch = platform_arch };
}

return Analytics.Platform{ .os = Analytics.OperatingSystem.linux, .version = version, .arch = platform_arch };
return Analytics.Platform{ .os = Analytics.OperatingSystem.linux, .version = release, .arch = platform_arch };
}
};
};
15 changes: 15 additions & 0 deletions src/crash_handler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,28 @@ pub fn handleSegfaultWindows(info: *windows.EXCEPTION_POINTERS) callconv(windows
);
}

extern "C" fn gnu_get_libc_version() ?[*:0]const u8;

pub fn printMetadata(writer: anytype) !void {
if (Output.enable_ansi_colors) {
try writer.writeAll(Output.prettyFmt("<r><d>", true));
}

try writer.writeAll(metadata_version_line);
{
const platform = bun.Analytics.GenerateHeader.GeneratePlatform.forOS();
if (bun.Environment.isLinux) {
// TODO: musl
const version = gnu_get_libc_version() orelse "";
const kernel_version = bun.Analytics.GenerateHeader.GeneratePlatform.kernelVersion();
if (platform.os == .wsl) {
try writer.print("WSL Kernel v{d}.{d}.{d} | glibc v{s}\n", .{ kernel_version.major, kernel_version.minor, kernel_version.patch, bun.sliceTo(version, 0) });
} else {
try writer.print("Linux Kernel v{d}.{d}.{d} | glibc v{s}\n", .{ kernel_version.major, kernel_version.minor, kernel_version.patch, bun.sliceTo(version, 0) });
}
} else if (bun.Environment.isMac) {
try writer.print("macOS v{s}\n", .{platform.version});
}
try writer.print("Args: ", .{});
var arg_chars_left: usize = if (bun.Environment.isDebug) 4096 else 196;
for (bun.argv, 0..) |arg, i| {
Expand Down

0 comments on commit b8e183a

Please sign in to comment.