|
| 1 | +#include "IOS/IOSSentrySubsystem.h" |
| 2 | + |
| 3 | +#include "IOS/IOSAppDelegate.h" |
| 4 | + |
| 5 | +#include "SentryDefines.h" |
| 6 | +#include "SentrySettings.h" |
| 7 | + |
| 8 | +#include "Misc/CoreDelegates.h" |
| 9 | +#include "Misc/FileHelper.h" |
| 10 | +#include "Misc/Paths.h" |
| 11 | +#include "Utils/SentryScreenshotUtils.h" |
| 12 | + |
| 13 | +static FIOSSentrySubsystem* GIOSSentrySubsystem = nullptr; |
| 14 | + |
| 15 | +struct sigaction DefaultSigIllHandler; |
| 16 | +struct sigaction DefaultSigEmtHandler; |
| 17 | +struct sigaction DefaultSigFpeHandler; |
| 18 | +struct sigaction DefaultSigBusHandler; |
| 19 | +struct sigaction DefaultSigSegvHandler; |
| 20 | +struct sigaction DefaultSigSysHandler; |
| 21 | + |
| 22 | +void SaveDefaultSignalHandlers() |
| 23 | +{ |
| 24 | + sigaction(SIGILL, NULL, &DefaultSigIllHandler); |
| 25 | + sigaction(SIGEMT, NULL, &DefaultSigEmtHandler); |
| 26 | + sigaction(SIGFPE, NULL, &DefaultSigFpeHandler); |
| 27 | + sigaction(SIGBUS, NULL, &DefaultSigBusHandler); |
| 28 | + sigaction(SIGSEGV, NULL, &DefaultSigSegvHandler); |
| 29 | + sigaction(SIGSYS, NULL, &DefaultSigSysHandler); |
| 30 | +} |
| 31 | + |
| 32 | +void RestoreDefaultSignalHandlers() |
| 33 | +{ |
| 34 | + sigaction(SIGILL, &DefaultSigIllHandler, NULL); |
| 35 | + sigaction(SIGEMT, &DefaultSigEmtHandler, NULL); |
| 36 | + sigaction(SIGFPE, &DefaultSigFpeHandler, NULL); |
| 37 | + sigaction(SIGBUS, &DefaultSigBusHandler, NULL); |
| 38 | + sigaction(SIGSEGV, &DefaultSigSegvHandler, NULL); |
| 39 | + sigaction(SIGSYS, &DefaultSigSysHandler, NULL); |
| 40 | +} |
| 41 | + |
| 42 | +static void IOSSentrySignalHandler(int Signal, siginfo_t *Info, void *Context) |
| 43 | +{ |
| 44 | + if (GIOSSentrySubsystem && GIOSSentrySubsystem->IsEnabled()) |
| 45 | + { |
| 46 | + GIOSSentrySubsystem->TryCaptureScreenshot(); |
| 47 | + } |
| 48 | + |
| 49 | + RestoreDefaultSignalHandlers(); |
| 50 | + |
| 51 | + // Re-raise signal to default handler |
| 52 | + raise(Signal); |
| 53 | +} |
| 54 | + |
| 55 | +void InstallSentrySignalHandler() |
| 56 | +{ |
| 57 | + struct sigaction Action; |
| 58 | + memset(&Action, 0, sizeof(Action)); |
| 59 | + Action.sa_sigaction = IOSSentrySignalHandler; |
| 60 | + Action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 61 | + |
| 62 | + sigaction(SIGILL, &Action, NULL); |
| 63 | + sigaction(SIGEMT, &Action, NULL); |
| 64 | + sigaction(SIGFPE, &Action, NULL); |
| 65 | + sigaction(SIGBUS, &Action, NULL); |
| 66 | + sigaction(SIGSEGV, &Action, NULL); |
| 67 | + sigaction(SIGSYS, &Action, NULL); |
| 68 | +} |
| 69 | + |
| 70 | +void FIOSSentrySubsystem::InitWithSettings(const USentrySettings* Settings, USentryBeforeSendHandler* BeforeSendHandler, USentryBeforeBreadcrumbHandler* BeforeBreadcrumbHandler, |
| 71 | + USentryTraceSampler* TraceSampler) |
| 72 | +{ |
| 73 | + GIOSSentrySubsystem = this; |
| 74 | + |
| 75 | + SaveDefaultSignalHandlers(); |
| 76 | + InstallSentrySignalHandler(); |
| 77 | + |
| 78 | + FAppleSentrySubsystem::InitWithSettings(Settings, BeforeSendHandler, BeforeBreadcrumbHandler, TraceSampler); |
| 79 | +} |
| 80 | + |
| 81 | +FString FIOSSentrySubsystem::TryCaptureScreenshot() const |
| 82 | +{ |
| 83 | + FString ScreenshotPath = GetScreenshotPath(); |
| 84 | + |
| 85 | + dispatch_sync(dispatch_get_main_queue(), ^{ |
| 86 | + UIGraphicsBeginImageContextWithOptions([IOSAppDelegate GetDelegate].RootView.bounds.size, NO, 2.0f); |
| 87 | + [[IOSAppDelegate GetDelegate].RootView drawViewHierarchyInRect:[IOSAppDelegate GetDelegate].RootView.bounds afterScreenUpdates:YES]; |
| 88 | + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); |
| 89 | + UIGraphicsEndImageContext(); |
| 90 | + |
| 91 | + NSData *ImageData = UIImagePNGRepresentation(image); |
| 92 | + |
| 93 | + TArray<uint8> ImageBytes; |
| 94 | + uint32 SavedSize = ImageData.length; |
| 95 | + ImageBytes.AddUninitialized(SavedSize); |
| 96 | + FPlatformMemory::Memcpy(ImageBytes.GetData(), [ImageData bytes], SavedSize); |
| 97 | + |
| 98 | + if (!FFileHelper::SaveArrayToFile(ImageBytes, *ScreenshotPath)) |
| 99 | + { |
| 100 | + UE_LOG(LogSentrySdk, Error, TEXT("Failed to save screenshot to: %s"), *ScreenshotPath); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + return ScreenshotPath; |
| 105 | +} |
0 commit comments