From e3bde5377fcf0697dc30322245d5726befad326f Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 16 Jun 2021 17:35:39 -0400 Subject: [PATCH] fix: il2cpp boot time on PS4 (#1062) --- CHANGELOG.md | 5 ++ .../DiagnosticLoggerExtensions.cs | 85 +++++++++++++------ src/Sentry/Internal/ProcessInfo.cs | 22 +++-- 3 files changed, 79 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64c3c8ff8..439d710ce2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +### Fixes + +- Handle error thrown while trying to get `BootTime` on PS4 with IL2CPP ([#1062](https://github.com/getsentry/sentry-dotnet/pull/1062)) + + ### Changes - Use SentryId for ISession.Id ([#1052](https://github.com/getsentry/sentry-dotnet/pull/1052)) diff --git a/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs b/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs index 22651642ea..eb5829975f 100644 --- a/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs +++ b/src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs @@ -17,7 +17,7 @@ public static void LogDebug( this IDiagnosticLogger logger, string message, TArg arg) - => logger.LogIfEnabled(SentryLevel.Debug, message, arg); + => logger.LogIfEnabled(SentryLevel.Debug, null, message, arg); /// /// Log a debug message. @@ -27,7 +27,7 @@ public static void LogDebug( string message, TArg arg, TArg2 arg2) - => logger.LogIfEnabled(SentryLevel.Debug, message, arg, arg2); + => logger.LogIfEnabled(SentryLevel.Debug, null, message, arg, arg2); /// /// Log a debug message. @@ -35,7 +35,7 @@ public static void LogDebug( public static void LogDebug( this IDiagnosticLogger logger, string message) - => logger.LogIfEnabled(SentryLevel.Debug, message); + => logger.LogIfEnabled(SentryLevel.Debug, null, message); /// /// Log a info message. @@ -43,7 +43,7 @@ public static void LogDebug( public static void LogInfo( this IDiagnosticLogger logger, string message) - => logger.LogIfEnabled(SentryLevel.Info, message); + => logger.LogIfEnabled(SentryLevel.Info, null, message); /// /// Log a info message. @@ -52,7 +52,7 @@ public static void LogInfo( this IDiagnosticLogger logger, string message, TArg arg) - => logger.LogIfEnabled(SentryLevel.Info, message, arg); + => logger.LogIfEnabled(SentryLevel.Info, null, message, arg); /// /// Log a info message. @@ -62,7 +62,7 @@ public static void LogInfo( string message, TArg arg, TArg2 arg2) - => logger.LogIfEnabled(SentryLevel.Info, message, arg, arg2); + => logger.LogIfEnabled(SentryLevel.Info, null, message, arg, arg2); /// /// Log a info message. @@ -73,7 +73,7 @@ public static void LogInfo( TArg arg, TArg2 arg2, TArg3 arg3) - => logger.LogIfEnabled(SentryLevel.Info, message, arg, arg2, arg3); + => logger.LogIfEnabled(SentryLevel.Info, null, message, arg, arg2, arg3); /// /// Log a warning message. @@ -81,7 +81,7 @@ public static void LogInfo( public static void LogWarning( this IDiagnosticLogger logger, string message) - => logger.LogIfEnabled(SentryLevel.Warning, message); + => logger.LogIfEnabled(SentryLevel.Warning, null, message); /// /// Log a warning message. @@ -90,7 +90,7 @@ public static void LogWarning( this IDiagnosticLogger logger, string message, TArg arg) - => logger.LogIfEnabled(SentryLevel.Warning, message, arg); + => logger.LogIfEnabled(SentryLevel.Warning, null, message, arg); /// /// Log a warning message. @@ -100,29 +100,29 @@ public static void LogWarning( string message, TArg arg, TArg2 arg2) - => logger.LogIfEnabled(SentryLevel.Warning, message, arg, arg2); + => logger.LogIfEnabled(SentryLevel.Warning, null, message, arg, arg2); /// - /// Log a warning message. + /// Log a error message. /// public static void LogError( this IDiagnosticLogger logger, string message, Exception? exception = null) - => logger.LogIfEnabled(SentryLevel.Error, message, exception); + => logger.LogIfEnabled(SentryLevel.Error, exception, message); /// - /// Log a warning message. + /// Log a error message. /// public static void LogError( this IDiagnosticLogger logger, string message, Exception exception, TArg arg) - => logger.LogIfEnabled(SentryLevel.Error, message, exception, arg); + => logger.LogIfEnabled(SentryLevel.Error, exception, message, arg); /// - /// Log a warning message. + /// Log a error message. /// public static void LogError( this IDiagnosticLogger logger, @@ -130,19 +130,32 @@ public static void LogError( Exception exception, TArg arg, TArg2 arg2) - => logger.LogIfEnabled(SentryLevel.Error, message, exception, arg, arg2); + => logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2); /// - /// Log a warning message. + /// Log a error message. /// - public static void LogError( + public static void LogError( this IDiagnosticLogger logger, string message, Exception exception, TArg arg, TArg2 arg2, + TArg3 arg3, + TArg4 arg4) + => logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2, arg3, arg4); + + /// + /// Log an error message. + /// + public static void LogError( + this IDiagnosticLogger logger, + Exception exception, + string message, + TArg arg, + TArg2 arg2, TArg3 arg3) - => logger.LogIfEnabled(SentryLevel.Error, message, arg, arg2, arg3, exception); + => logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2, arg3); /// /// Log a warning message. @@ -151,13 +164,13 @@ public static void LogFatal( this IDiagnosticLogger logger, string message, Exception? exception = null) - => logger.LogIfEnabled(SentryLevel.Fatal, message, exception); + => logger.LogIfEnabled(SentryLevel.Fatal, exception, message); internal static void LogIfEnabled( this IDiagnosticLogger logger, SentryLevel level, - string message, - Exception? exception = null) + Exception? exception, + string message) { if (logger.IsEnabled(level)) { @@ -168,9 +181,9 @@ internal static void LogIfEnabled( internal static void LogIfEnabled( this IDiagnosticLogger logger, SentryLevel level, + Exception? exception, string message, - TArg arg, - Exception? exception = null) + TArg arg) { if (logger.IsEnabled(level)) { @@ -181,10 +194,10 @@ internal static void LogIfEnabled( internal static void LogIfEnabled( this IDiagnosticLogger logger, SentryLevel level, + Exception? exception, string message, TArg arg, - TArg2 arg2, - Exception? exception = null) + TArg2 arg2) { if (logger.IsEnabled(level)) { @@ -195,16 +208,32 @@ internal static void LogIfEnabled( internal static void LogIfEnabled( this IDiagnosticLogger logger, SentryLevel level, + Exception? exception, string message, TArg arg, TArg2 arg2, - TArg3 arg3, - Exception? exception = null) + TArg3 arg3) { if (logger.IsEnabled(level)) { logger.Log(level, message, exception, arg, arg2, arg3); } } + + internal static void LogIfEnabled( + this IDiagnosticLogger logger, + SentryLevel level, + Exception? exception, + string message, + TArg arg, + TArg2 arg2, + TArg3 arg3, + TArg4 arg4) + { + if (logger.IsEnabled(level)) + { + logger.Log(level, message, exception, arg, arg2, arg3, arg4); + } + } } } diff --git a/src/Sentry/Internal/ProcessInfo.cs b/src/Sentry/Internal/ProcessInfo.cs index 32a2a352e5..99c58b1300 100644 --- a/src/Sentry/Internal/ProcessInfo.cs +++ b/src/Sentry/Internal/ProcessInfo.cs @@ -48,19 +48,31 @@ internal ProcessInfo( // Fast var now = DateTimeOffset.UtcNow; StartupTime = now; + long? timestamp = 0; try { - BootTime = now.AddTicks(-Stopwatch.GetTimestamp() + timestamp = Stopwatch.GetTimestamp(); + BootTime = now.AddTicks(-timestamp.Value / (Stopwatch.Frequency / TimeSpan.TicksPerSecond)); } - catch (DivideByZeroException e) + // We can live with only `StartupTime` so lets handle the lack of `BootTime` and construct this object. + catch (Exception e) { - // Seems to have failed on a single Windows Server 2012 on .NET Framework 4.8 + // DivideByZeroException: Seems to have failed on a single Windows Server 2012 on .NET Framework 4.8 // https://github.com/getsentry/sentry-dotnet/issues/954 + + // Can fail on IL2CPP with an unclear line number and this is an optional information: + // ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime. + // https://github.com/getsentry/sentry-unity/issues/233 + _options.DiagnosticLogger?.LogError( - "Failed to find BootTime: GetTimestamp {0}, Frequency {1}, TicksPerSecond: {2}", - e, Stopwatch.GetTimestamp(), Stopwatch.Frequency, TimeSpan.TicksPerSecond); + "Failed to find BootTime: Now {0}, GetTimestamp {1}, Frequency {2}, TicksPerSecond: {3}", + e, + now, + timestamp, + Stopwatch.Frequency, + TimeSpan.TicksPerSecond); } // An opt-out to the more precise approach (mainly due to IL2CPP):