Skip to content

Commit

Permalink
Add wrong Kotlin inline function number to Android Troubleshooting (#…
Browse files Browse the repository at this point in the history
…7576)

* added a Kotlin section to Android Troubleshooting regarding kotlin inline function reported with wrong line numbers
* added example and link to Jetbrains issue
  • Loading branch information
stefanosiano authored Aug 4, 2023
1 parent 57a1957 commit fe203e1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/platforms/android/troubleshooting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,52 @@ For example, the following stack traces:
at io.sentry.HubAdapter.captureEvent (HubAdapter.java:29)
```

## Incorrect Line Numbers in Kotlin

Stack trace elements referring to Kotlin inline functions from files different than the call site may refer to the wrong line numbers.

This is due to a [missing feature in the JVM](https://youtrack.jetbrains.com/issue/KT-8628/Line-numbers-are-incorrect-in-exception-stack-trace). `.class` files with inline functions contain code from many source files, and when the JVM generates a stacktrace, it doesn't replace the class and line where the error occurred with the original file and line number. So, when an exception is thrown in an inline function, the line number for the stack trace of the Exception may be wrong.

Let's show an example. Here is an Activity with an inline function throwing an Exception.

```
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
erroringFunction() // Line 10
}
inline fun erroringFunction() {
throw RuntimeException() // Line 14
}
}
// Line 17
```

The stacktrace of the Exception points to the line 18 of the `MyActivity.kt` class.

```
java.lang.RuntimeException
at io.sentry.samples.android.MyActivity.onCreate(MyActivity.kt:18)
at android.app.Activity.performCreate(Activity.java:8305)
at android.app.Activity.performCreate(Activity.java:8284)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
```

## NDK

If you're using a version of `sentry-android` before 5.0, due to current limitations in how `sentry-native` works, it cannot identify native libraries loaded directly from `.apk` files. Instead, it needs to have access to the extracted libraries on the file system.
Expand Down

1 comment on commit fe203e1

@vercel
Copy link

@vercel vercel bot commented on fe203e1 Aug 4, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

sentry-docs – ./

docs.sentry.io
sentry-docs-git-master.sentry.dev
sentry-docs.sentry.dev

Please sign in to comment.