Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Synchronize contexts from .NET to native #747

Merged
merged 17 commits into from
Jun 14, 2022
Merged

Conversation

vaind
Copy link
Collaborator

@vaind vaind commented May 12, 2022

closes #650

@github-actions
Copy link
Contributor

github-actions bot commented May 12, 2022

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against b12a53f

@vaind vaind changed the title feat: add ContextWriter to enable c#->native context sync feat: Synchronize contexts from .NET to native May 12, 2022
@vaind vaind force-pushed the feat/context-sync branch 2 times, most recently from c3c8e8f to 74db263 Compare May 20, 2022 18:55
@vaind
Copy link
Collaborator Author

vaind commented May 21, 2022

update: I've worked around this by moving the reusable native context-writer static functions to Sentry.Unity.NativeUtils


@bitsandfoxes I've tried implementing the scope sync for Android native crashes (which internally use sentry-native) by reusing the code in the Sentry.Unity.Native project. However, I'm getting some unity errors which I can't figure out - you'll likely know more:

changes: 8e1d28c

errors in the Unity console:

Unable to update following assemblies:Packages/io.sentry.unity.dev/Tests/Runtime/Sentry.Unity.iOS.Tests.dll (Name = Sentry.Unity.iOS.Tests, Error = 131) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp3c487b7b.tmp)

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Sentry, Version=3.17.0.0, Culture=neutral, PublicKeyToken=fba2ec45388e2af0'
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name, Mono.Cecil.ReaderParameters parameters) [0x00105] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.BaseAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) [0x00007] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.DefaultAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference name) [0x0001d] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.MetadataResolver.Resolve (Mono.Cecil.TypeReference type) [0x00038] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.ModuleDefinition.Resolve (Mono.Cecil.TypeReference type) [0x00006] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.TypeReference.Resolve () [0x00006] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.Mixin.CheckedResolve (Mono.Cecil.TypeReference self) [0x00000] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeEnum (Mono.Cecil.TypeReference enum_type) [0x00000] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeElementValue (Mono.Cecil.TypeReference type) [0x0002f] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeElement (Mono.Cecil.TypeReference type) [0x00015] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeElement (Mono.Cecil.TypeReference type) [0x00030] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeFixedArgument (Mono.Cecil.TypeReference type) [0x00015] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.SignatureReader.ReadCustomAttributeConstructorArguments (Mono.Cecil.CustomAttribute attribute, Mono.Collections.Generic.Collection`1[T] parameters) [0x0002e] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.MetadataReader.ReadCustomAttributeSignature (Mono.Cecil.CustomAttribute attribute) [0x0003c] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.CustomAttribute.<Resolve>b__35_0 (Mono.Cecil.CustomAttribute attribute, Mono.Cecil.MetadataReader reader) [0x00000] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.ModuleDefinition.Read[TItem,TRet] (TItem item, System.Func`3[T1,T2,TResult] read) [0x00029] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.CustomAttribute.Resolve () [0x00037] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at Mono.Cecil.CustomAttribute.get_ConstructorArguments () [0x00000] in <a6860a9f6366437387ebdc1f225b7fd4>:0 
  at AssemblyUpdater.Steps.MoveToNamespace.Visit (Mono.Cecil.CustomAttribute customAttribute, Unity.Cecil.Visitor.Context context) [0x000c5] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at Unity.Cecil.Visitor.Visitor.Visit (Mono.Cecil.MethodDefinition methodDefinition, Unity.Cecil.Visitor.Context context) [0x00032] in <afca345304574c67bedb060451119410>:0 
  at Unity.Cecil.Visitor.Visitor.Visit (Mono.Cecil.TypeDefinition typeDefinition, Unity.Cecil.Visitor.Context context) [0x00177] in <afca345304574c67bedb060451119410>:0 
  at Unity.Cecil.Visitor.Visitor.Visit (Mono.Cecil.ModuleDefinition moduleDefinition, Unity.Cecil.Visitor.Context context) [0x0001f] in <afca345304574c67bedb060451119410>:0 
  at Unity.Cecil.Visitor.Visitor.Visit (Mono.Cecil.AssemblyDefinition assemblyDefinition, Unity.Cecil.Visitor.Context context) [0x0001f] in <afca345304574c67bedb060451119410>:0 
  at AssemblyUpdater.Steps.AssemblyUpdaterStepBase.Visit (Mono.Cecil.AssemblyDefinition assemblyDefinition, Unity.Cecil.Visitor.Context context) [0x00000] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <919875e55a5b48959aa30f79ae9d9170>:0 
   --- End of inner exception stack trace ---
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00083] in <919875e55a5b48959aa30f79ae9d9170>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <919875e55a5b48959aa30f79ae9d9170>:0 
  at Unity.Cecil.Visitor.Visitor.Visit[T] (T node, Unity.Cecil.Visitor.Context context) [0x0001b] in <afca345304574c67bedb060451119410>:0 
  at Unity.Cecil.Visitor.Extensions.DoAccept[T] (T definition, Unity.Cecil.Visitor.Visitor visitor) [0x00013] in <afca345304574c67bedb060451119410>:0 
  at Unity.Cecil.Visitor.Extensions.Accept (Mono.Cecil.AssemblyDefinition assemblyDefinition, Unity.Cecil.Visitor.Visitor visitor) [0x00000] in <afca345304574c67bedb060451119410>:0 
  at AssemblyUpdater.Steps.AssemblyUpdaterStepBase.Apply (AssemblyUpdater.Core.AssemblyUpdaterContext context) [0x00012] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at AssemblyUpdater.Core.AssemblyUpdaterPipeline.RunUpdateRound (AssemblyUpdater.Core.AssemblyUpdaterContext context) [0x00030] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at AssemblyUpdater.Core.AssemblyUpdaterPipeline.Run (AssemblyUpdater.Core.AssemblyUpdaterContext context) [0x00007] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at AssemblyUpdater.Application.Program.UpdateAssembly (AssemblyUpdater.Application.UpdateOptions config, AssemblyUpdater.Core.AssemblyUpdaterContext context) [0x00024] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at AssemblyUpdater.Application.Program+<>c__DisplayClass0_0.<Main>b__1 (AssemblyUpdater.Application.UpdateOptions o) [0x00046] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
  at CommandLine.ParserResultExtensions.WithParsed[T] (CommandLine.ParserResult`1[T] result, System.Action`1[T] action) [0x0001e] in <5f6458f0234f43a48c09047109c24684>:0 
  at AssemblyUpdater.Application.Program.Main (System.String[] args) [0x00048] in <ea98cb70156f4362b5b9abd3c2968e12>:0 
Following assemblies were successfully updated but due to the failed ones above they were ignored (not copied to the destination folder):Packages/io.sentry.unity.dev/Runtime/Sentry.Unity.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp7f9d6b9.tmp)

Packages/io.sentry.unity.dev/Editor/Sentry.Unity.Editor.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp50fb70f2.tmp)

Packages/io.sentry.unity.dev/Editor/iOS/Sentry.Unity.Editor.iOS.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp71bbf21d.tmp)

Packages/io.sentry.unity.dev/Runtime/Sentry.Unity.Native.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp56d7472e.tmp)

Packages/io.sentry.unity.dev/Tests/Runtime/Sentry.Unity.Native.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp208714e.tmp)

Packages/io.sentry.unity.dev/Runtime/Sentry.Unity.Android.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp30fd3bff.tmp)

Packages/io.sentry.unity.dev/Runtime/Sentry.Unity.iOS.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp7c7990ce.tmp)

Packages/io.sentry.unity.dev/Tests/Runtime/Sentry.Unity.Tests.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp7a17b2bb.tmp)

Packages/io.sentry.unity.dev/Tests/Editor/Sentry.Unity.Editor.Tests.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp29a8b688.tmp)

Packages/io.sentry.unity.dev/Tests/Editor/Sentry.Unity.Editor.iOS.Tests.dll	(Result = 0) (Output: C:\Users\dlugo\AppData\Local\Temp\tmp1eb05de2.tmp)


UnityEditor.Scripting.APIUpdaterLogger:WriteErrorToConsole (string,object[])
UnityEditorInternal.APIUpdating.APIUpdaterManager:HandleAssemblyUpdaterErrors (System.Collections.Generic.IList`1<UnityEditorInternal.APIUpdating.AssemblyUpdaterUpdateTask>)
UnityEditorInternal.APIUpdating.APIUpdaterManager:UpdateAssemblies ()
UnityEditorInternal.APIUpdating.APIUpdaterManager:ProcessImportedAssemblies (string[])

  • Multiple precompiled assemblies with the same name Sentry.Unity.Native.dll included on the current platform. Only one assembly with the same name is allowed per platform. (C:/dev/sentry-unity/package-dev/Tests/Runtime/Sentry.Unity.Native.dll)
  • Multiple precompiled assemblies with the same name Sentry.Unity.Native.dll included on the current platform. Only one assembly with the same name is allowed per platform. (C:/dev/sentry-unity/package-dev/Runtime/Sentry.Unity.Native.dll)

@bitsandfoxes
Copy link
Contributor

I've seen this before and I think I know what this is. Let me take a look.

@vaind vaind force-pushed the feat/context-sync branch 3 times, most recently from 03fcf2f to 7913a02 Compare June 6, 2022 17:21
@vaind vaind marked this pull request as ready for review June 9, 2022 14:09
Copy link
Contributor

@bitsandfoxes bitsandfoxes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Maybe @bruno-garcia wants to take a look too?

package-dev/Plugins/iOS/SentryNativeBridge.m Show resolved Hide resolved
}

void SentryNativeBridgeWriteScope( // clang-format off
// // const char *AppStartTime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // const char *AppStartTime,
// const char *AppStartTime,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it looks weird but there's a hidden logic for these... the single-comment fields should actually be added, but we cannot do that because there's no way to merge existing contexts for iOS. So if you imagine commenting out the whole block, you'll end up with the fields that are missing and should be added, but depend on something currently not possible with sentry-cocoa. The double-commented lines (which would be just commented-out after un-commenting the whole block), are already present.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying!

Comment on lines +166 to +167
// // const char *DeviceModel,
// // long DeviceMemorySize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // const char *DeviceModel,
// // long DeviceMemorySize,
// const char *DeviceModel,
// long DeviceMemorySize,

void SentryNativeBridgeWriteScope( // clang-format off
// // const char *AppStartTime,
// const char *AppBuildType,
// // const char *OperatingSystemRawDescription,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // const char *OperatingSystemRawDescription,
// const char *OperatingSystemRawDescription,

const char *UnityRenderingThreadingMode
) // clang-format on
{
// Note: we're using a NSMutableDictionary because it will skip fields with nil values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does skip mean? It will create no entry if the value is nil?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Comment on lines +56 to +57
// // DeviceModel,
// // DeviceMemorySize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // DeviceModel,
// // DeviceMemorySize,
// DeviceModel,
// DeviceMemorySize,

// Additionally, there's currently no way to update existing contexts, so no more Device info for now...
[DllImport("__Internal")]
private static extern void SentryNativeBridgeWriteScope(
// // string? AppStartTime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // string? AppStartTime,
// string? AppStartTime,

private static extern void SentryNativeBridgeWriteScope(
// // string? AppStartTime,
// string? AppBuildType,
// // string? OperatingSystemRawDescription,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // string? OperatingSystemRawDescription,
// string? OperatingSystemRawDescription,

Comment on lines +96 to +97
// // string? DeviceModel,
// // long? DeviceMemorySize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // string? DeviceModel,
// // long? DeviceMemorySize,
// string? DeviceModel,
// long? DeviceMemorySize,

src/Sentry.Unity/ContextWriter.cs Outdated Show resolved Hide resolved
Co-authored-by: Stefan Jandl <stefan.jandl@sentry.io>
{
if (Attach())
{
using var gpu = new AndroidJavaObject("io.sentry.protocol.Gpu");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type isnt' found, is this going to blow up? (proguard)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, see #797

src/Sentry.Unity/ContextWriter.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
src/Sentry.Unity/SentryUnity.cs Outdated Show resolved Hide resolved
@vaind vaind mentioned this pull request Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Device/Unity context to native events
4 participants