Skip to content

Conversation

@jonpryor
Copy link
Contributor

@jonpryor jonpryor commented Sep 8, 2022

@jonpryor jonpryor force-pushed the jonp-debug-gref-logs branch 3 times, most recently from 6b6e99e to b13b919 Compare September 8, 2022 17:12
Comment on lines +373 to +389
`logcat-parse` combines an API around the JNI Global Reference logs with a C# REPL.
It is included with Classic Xamarin.Android installs.
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to file an issue to distribute this as a .NET global tool? On NuGet.org? Then you could do dotnet tool install --global logcat-parse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I should do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Might also take the opportunity to rename logcat-parse, since I don't think it's parsing logcat? 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can parse adb logcat output.

I'm open to alternative names. objref-parse?

In particular, `adb logcat` has a `MissingMethodException` mentioning that a constructor
with the signature `(IntPtr, JniHandleOwnership)` could not be found.

If you see a `NotSupportedException` which with
Copy link
Contributor

Choose a reason for hiding this comment

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

s/which/d


Enable JNI Global Reference log collection by setting the `debug.mono.log` system property to a value which contains `gref`:

adb shell setprop debug.mono.log gref
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest adding

adb logcat -G 16M

to increase the logcat buffer size, otherwise the logcat daemon will truncate messages

Once your app crashes, `log.txt` will contain the JNI Object Reference logs.

*However*, the output *may* be incomplete. It is not unusual for information to be missing, because *so much*
data is written to `adb logcat`.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should find out what's the maximum admissible logcat buffer size and suggest setting it here

/* 31 */ at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-31/mcw/Android.App.Activity.cs:line 2781
/* 32 */ at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
```

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd "simplify" the format here - use 1. for line numbers, the asterisks introduce visual noise IMO

csharp> var r = grefs.AllocatedPeers.Where(p => p.KeyHandle=="0x466b26f");
csharp> r;
{ PeerInfo(State='Collected' JniType='mono/java/lang/Runnable' McwType='Java.Lang.Runnable' KeyHandle=0x466b26f Handles=1) }
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we count on people having Mono installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If they're on macOS, yes. Classic Xamarin.Android SDK requires msbuild, which is part of Mono.framework.

We need a better solution for "pure .NET" environments; we likely need to turn logcat-parse into a dotnet global tool.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's what I was thining, it should be a dotnet tool most likely. Not necessarily dotnet global one, enough that it is shipped with XA and can be invoked with just logcat-parse

# run the app…

adb shell run-as @PACKAGE-NAME@ cat files/.__override__/grefs.txt > grefs.txt
adb shell run-as @PACKAGE-NAME@ cat files/.__override__/lrefs.txt > lrefs.txt
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps explain where @PACKAGE-NAME@ can be looked up? I wouldn't be surprised if many people wouldn't know what their package name actually is, it being hidden behind properties in the project file

@jonpryor jonpryor force-pushed the jonp-debug-gref-logs branch 2 times, most recently from 46d82fa to accefa5 Compare September 8, 2022 17:41

If you see a `NotSupportedException` with
[*anything else* as the inner exception](https://github.com/xamarin/xamarin-android/issues/7324),
then this is *not* due to JNI object references, and the rest of this guide will not help you.
Copy link
Contributor

Choose a reason for hiding this comment

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

A suggestion to some "next step(s)" if this guide does not help might be useful, even if it says something like "The exception message should tell you what the real error is".

@jonpryor jonpryor force-pushed the jonp-debug-gref-logs branch from accefa5 to 34052cc Compare September 16, 2022 16:26
@jonpryor jonpryor merged commit a7b5262 into dotnet:main Sep 16, 2022
grendello added a commit to grendello/xamarin-android that referenced this pull request Sep 16, 2022
* main:
  [docs] Document how to debug JNI Object Reference crashes (dotnet#7362)
  [Mono.Android] Parse cookies on redirect in AndroidClientHandler (dotnet#7373)
  Bump to xamarin/Java.Interop/main@fcc33ce2 (dotnet#7380)
  Bump to dotnet/installer@11f6b8f 7.0.100-rc.2.22459.2 (dotnet#7367)
  LEGO: Merge pull request 7385
  Bump to xamarin/monodroid@135be73 (dotnet#7384)
  Bump to xamarin/xamarin-android-tools/main@0be567a9 (dotnet#7383)
  Bump to Tessil/robin-map@784245b4 [v1.0.1] (dotnet#7382)
  Bump to mono/mono@a96bde97 (dotnet#7372)
  [ci] Fix test categories for emu smoke tests (dotnet#7374)
  Bump to xamarin/monodroid@43583d02 (dotnet#7370)
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants