Skip to content

Commit

Permalink
Add API for deliberately crashing an app (#1842)
Browse files Browse the repository at this point in the history
* Add Java and C code

* Commit binaries

* Add new SentrySdk.CauseCrash API

* Update samples

* Update CHANGELOG.md

* Update CI Build

* Fix sample

* Remove unobserved task (won't crash)

* Add tests

* Cleanup API approvals

* redundant using

* warning disable CS0618

* System.Obsolete in api

* System.Obsolete in api

Co-authored-by: Simon Cropp <simon.cropp@gmail.com>
  • Loading branch information
mattjohnsonpint and SimonCropp committed Aug 6, 2022
1 parent 8760b8b commit f474f07
Show file tree
Hide file tree
Showing 56 changed files with 547 additions and 159 deletions.
32 changes: 32 additions & 0 deletions .github/actions/buildnative/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build Native Dependencies
runs:
using: composite

steps:

- name: Set Environment Variables
shell: bash
run: echo "JAVA_HOME_11=$JAVA_HOME_11_X64" >> $GITHUB_ENV

- name: Install Ninja
uses: seanmiddleditch/gha-setup-ninja@master

- name: Build C Project
if: runner.os == 'Linux' || runner.os == 'macOS'
shell: bash
run: src/Sentry/Platforms/Android/C/build.sh

- name: Build C Project
if: runner.os == 'Windows'
shell: cmd
run: src\Sentry\Platforms\Android\C\build.cmd

- name: Build Java Project
if: runner.os == 'Linux' || runner.os == 'macOS'
shell: bash
run: src/Sentry/Platforms/Android/Java/build.sh

- name: Build Java Project
if: runner.os == 'Windows'
shell: cmd
run: src\Sentry\Platforms\Android\Java\build.cmd
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Setup Environment
uses: ./.github/actions/environment

- name: Build Native Dependencies
uses: ./.github/actions/buildnative

- name: Build
run: dotnet build -c Release /p:CopyLocalLockFileAssemblies=true

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Updated Sentry Android SDK to version 6.3.0 ([#1826](https://github.com/getsentry/sentry-dotnet/pull/1826))
- Add the Sentry iOS SDK ([#1829](https://github.com/getsentry/sentry-dotnet/pull/1829))
- Enable Scope Sync for iOS ([#1834](https://github.com/getsentry/sentry-dotnet/pull/1834))
- Add API for deliberately crashing an app ([#1842](https://github.com/getsentry/sentry-dotnet/pull/1842))

### Fixes

Expand Down
7 changes: 7 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Maui.Tests", "test\S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Ios", "samples\Sentry.Samples.Ios\Sentry.Samples.Ios.csproj", "{C2876321-A612-4E66-AF33-D3928FA6366F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Testing.CrashableApp", "test\Sentry.Testing.CrashableApp\Sentry.Testing.CrashableApp.csproj", "{DA3CECBB-83BE-441A-852C-077809C48307}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -349,6 +351,10 @@ Global
{C2876321-A612-4E66-AF33-D3928FA6366F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2876321-A612-4E66-AF33-D3928FA6366F}.Release|Any CPU.Build.0 = Release|Any CPU
{C2876321-A612-4E66-AF33-D3928FA6366F}.Release|Any CPU.Deploy.0 = Release|Any CPU
{DA3CECBB-83BE-441A-852C-077809C48307}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA3CECBB-83BE-441A-852C-077809C48307}.Release|Any CPU.Build.0 = Release|Any CPU
{DA3CECBB-83BE-441A-852C-077809C48307}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA3CECBB-83BE-441A-852C-077809C48307}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -404,6 +410,7 @@ Global
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6} = {AF6AF4C7-8AA2-4D59-8064-2D79560904EB}
{143076C0-8D6B-4054-9F45-06B21655F417} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
{C2876321-A612-4E66-AF33-D3928FA6366F} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{DA3CECBB-83BE-441A-852C-077809C48307} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0C652B1A-DF72-4EE5-A98B-194FE2C054F6}
Expand Down
1 change: 1 addition & 0 deletions SentryCore.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"path": "Sentry.sln",
"projects": [
"src\\Sentry\\Sentry.csproj",
"test\\Sentry.Testing.CrashableApp\\Sentry.Testing.CrashableApp.csproj",
"test\\Sentry.Testing\\Sentry.Testing.csproj",
"test\\Sentry.Tests\\Sentry.Tests.csproj"
]
Expand Down
1 change: 1 addition & 0 deletions SentryMaui.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"src\\Sentry\\Sentry.csproj",
"test\\Sentry.Extensions.Logging.Tests\\Sentry.Extensions.Logging.Tests.csproj",
"test\\Sentry.Maui.Tests\\Sentry.Maui.Tests.csproj",
"test\\Sentry.Testing.CrashableApp\\Sentry.Testing.CrashableApp.csproj",
"test\\Sentry.Testing\\Sentry.Testing.csproj",
"test\\Sentry.Tests\\Sentry.Tests.csproj"
]
Expand Down
1 change: 1 addition & 0 deletions SentryNoSamples.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test\\Sentry.Maui.Tests\\Sentry.Maui.Tests.csproj",
"test\\Sentry.NLog.Tests\\Sentry.NLog.Tests.csproj",
"test\\Sentry.Serilog.Tests\\Sentry.Serilog.Tests.csproj",
"test\\Sentry.Testing.CrashableApp\\Sentry.Testing.CrashableApp.csproj",
"test\\Sentry.Testing\\Sentry.Testing.csproj",
"test\\Sentry.Tests\\Sentry.Tests.csproj"
]
Expand Down
6 changes: 0 additions & 6 deletions samples/Sentry.Samples.Android/C/CMakeLists.txt

This file was deleted.

5 changes: 0 additions & 5 deletions samples/Sentry.Samples.Android/C/README.md

This file was deleted.

Binary file not shown.
Binary file not shown.
18 changes: 0 additions & 18 deletions samples/Sentry.Samples.Android/C/build.sh

This file was deleted.

Binary file removed samples/Sentry.Samples.Android/C/x86/libbuggy.so
Binary file not shown.
Binary file removed samples/Sentry.Samples.Android/C/x86_64/libbuggy.so
Binary file not shown.
6 changes: 0 additions & 6 deletions samples/Sentry.Samples.Android/Kotlin/Metadata.xml

This file was deleted.

13 changes: 0 additions & 13 deletions samples/Sentry.Samples.Android/Kotlin/README.md

This file was deleted.

Binary file removed samples/Sentry.Samples.Android/Kotlin/buggy.jar
Binary file not shown.
19 changes: 0 additions & 19 deletions samples/Sentry.Samples.Android/Kotlin/buggy.kt

This file was deleted.

102 changes: 50 additions & 52 deletions samples/Sentry.Samples.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
using Sentry.Samples.Android.Kotlin;
#pragma warning disable CS0618
namespace Sentry.Samples.Android;

namespace Sentry.Samples.Android
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
protected override void OnCreate(Bundle? savedInstanceState)
{
protected override void OnCreate(Bundle? savedInstanceState)
SentrySdk.Init(this, o =>
{
SentrySdk.Init(this, o =>
{
o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
o.SendDefaultPii = true; // adds the user's IP address automatically
});
o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
o.SendDefaultPii = true; // adds the user's IP address automatically
});

// Here's an example of adding custom scope information.
// This can be done at any time, and will be passed through to the Java SDK as well.
SentrySdk.ConfigureScope(scope =>
{
scope.AddBreadcrumb("Custom Breadcrumb");
scope.SetExtra("Test", "Custom Extra Data");
scope.User = new User
{
Username = "SomeUser",
Email = "test@example.com",
Other =
{
["CustomInfo"] = "Custom User Info"
}
};
});

base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

var captureException = (Button)base.FindViewById(Resource.Id.captureException)!;
captureException.Click += (s, a) =>
// Here's an example of adding custom scope information.
// This can be done at any time, and will be passed through to the Java SDK as well.
SentrySdk.ConfigureScope(scope =>
{
scope.AddBreadcrumb("Custom Breadcrumb");
scope.SetExtra("Test", "Custom Extra Data");
scope.User = new User
{
try
Username = "SomeUser",
Email = "test@example.com",
Other =
{
throw new Exception("Try, catch");
}
catch (Exception e)
{
SentrySdk.CaptureException(e);
["CustomInfo"] = "Custom User Info"
}
};
var throwUnhandledException = (Button)base.FindViewById(Resource.Id.throwUnhandledException)!;
throwUnhandledException.Click += (s, a) => throw new Exception("Unhandled");
var throwKotlinException = (Button)base.FindViewById(Resource.Id.throwKotlinException)!;
throwKotlinException.Click += (s, a) => Buggy.Throw();
var throwKotlinExceptionBackgroundThread = (Button)base.FindViewById(Resource.Id.throwKotlinExceptionBackgroundThread)!;
throwKotlinExceptionBackgroundThread.Click += (s, a) => Buggy.ThrowOnBackgroundThread();
var crashInC = (Button)base.FindViewById(Resource.Id.crashInC)!;
crashInC.Click += (s, a) => CrashInC();
}

[System.Runtime.InteropServices.DllImport("libbuggy.so", EntryPoint = "crash_in_c")]
private static extern void CrashInC();
});

base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

var captureException = (Button)base.FindViewById(Resource.Id.captureException)!;
captureException.Click += (s, a) =>
{
try
{
throw new Exception("Try, catch");
}
catch (Exception e)
{
SentrySdk.CaptureException(e);
}
};
var throwUnhandledException = (Button)base.FindViewById(Resource.Id.throwUnhandledException)!;
throwUnhandledException.Click += (s, a) => throw new Exception("Unhandled");

var throwJavaException = (Button)base.FindViewById(Resource.Id.throwJavaException)!;
throwJavaException.Click += (s, a) => SentrySdk.CauseCrash(CrashType.Java);

var throwJavaExceptionBackgroundThread = (Button)base.FindViewById(Resource.Id.throwJavaExceptionBackgroundThread)!;
throwJavaExceptionBackgroundThread.Click += (s, a) => SentrySdk.CauseCrash(CrashType.JavaBackgroundThread);

var crashInC = (Button)base.FindViewById(Resource.Id.crashInC)!;
crashInC.Click += (s, a) => SentrySdk.CauseCrash(CrashType.Native);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll_content"
Expand Down Expand Up @@ -42,15 +42,15 @@

<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/throwKotlinException"
android:text="@string/throw_kotlin_exception"
android:id="@+id/throwJavaException"
android:text="@string/throw_java_exception"
android:layout_weight="0.5"
android:layout_marginTop="10dp"/>

<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/throwKotlinExceptionBackgroundThread"
android:text="@string/throw_kotlin_exception_background_thread"
android:id="@+id/throwJavaExceptionBackgroundThread"
android:text="@string/throw_java_exception_background_thread"
android:layout_weight="0.5"
android:layout_marginTop="10dp"/>

Expand Down
4 changes: 2 additions & 2 deletions samples/Sentry.Samples.Android/Resources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<string name="app_name">Sentry .NET Android Sample</string>
<string name="capture_exception">Throw and Capture C# Exception</string>
<string name="throw_unhandled_exception">Throw C# Exception</string>
<string name="throw_kotlin_exception">Throw Kotlin Exception</string>
<string name="throw_kotlin_exception_background_thread">Throw Kotlin Exception (Background Thread)</string>
<string name="throw_java_exception">Throw Java Exception</string>
<string name="throw_java_exception_background_thread">Throw Java Exception (Background Thread)</string>
<string name="crash_in_c">Segfault in C</string>
</resources>
4 changes: 0 additions & 4 deletions samples/Sentry.Samples.Android/Sentry.Samples.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
<ApplicationId>io.sentry.dotnet.samples.android</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<!-- BG8605 and BG8606 due to kotlin.reflect missing even though no usage is explicitly done on Buggy.kt - Sample works fine. -->
<NoWarn>$(NoWarn);BG8605;BG8606</NoWarn>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
<Using Include="Android.App.Activity" Alias="Activity" />
<TransformFile Include="Kotlin\Metadata.xml" />
<AndroidLibrary Include="Kotlin\buggy.jar" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.Ios/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l

// Uncomment to try these
// throw new Exception("Test Unhandled Managed Exception");
// SentrySdk.CauseNativeCrash();
// SentrySdk.CauseCrash(CrashType.Native);

return true;
}
Expand Down
10 changes: 5 additions & 5 deletions samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>

<!--
<!--
In a real project, use the PackageReference and set the version to the latest release.
Use that instead of the ProjectReference.
-->
Expand All @@ -19,20 +19,20 @@
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
</ItemGroup>

<!--
<!--
This next section is to build an empty assembly instead of the actual sample app when not building on a Mac.
We do this here so we don't have to worry about solution configurations or filters in CI.
It is not needed in a real application.
It is not needed in a real application.
-->
<PropertyGroup Condition="!$([MSBuild]::IsOSPlatform('OSX'))">
<TargetFramework>net6.0</TargetFramework>
<OutputType>Library</OutputType>
<DefaultItemExcludes>**\*</DefaultItemExcludes>
</PropertyGroup>

<!--
<!--
Workaround for https://github.com/dotnet/maui/issues/7272 (also happens for iOS without MAUI)
This should not be needed in a real application.
This should not be needed in a real application.
-->
<Target Name="_SetPublishFolderTypeNoneOnDocFileItems" BeforeTargets="_ComputePublishLocation">
<ItemGroup>
Expand Down
Loading

0 comments on commit f474f07

Please sign in to comment.