Brendonovich / swift-rs

Call Swift functions from Rust with ease!
Apache License 2.0
246 stars 28 forks source link

How to get non-inlined stack traces during crashes #61

Open tleyden opened 3 months ago

tleyden commented 3 months ago

I recently had a bug in my own code that was causing a crash, but it was difficult to debug because the stack traces were misleading due to function inlining. For example I was getting a thread crash with:

Thread 17 Crashed:
0   MyApp 0x10135eec8 screen_capture(captureFrontmostWindow:skipCaptureAllBlackImg:frontmostAppPid:) + 916
1   MyApp 0x10135eb58 screen_capture(captureFrontmostWindow:skipCaptureAllBlackImg:frontmostAppPid:) + 36
2   MyApp 0x10135eb2c screen_capture_swift + 12

but I know for a fact that the actual crash was emanating from a different swift function, func getFrontmostAppWindowID(frontmostAppPid: Int) -> Int, which is invoked by the screen_capture(..) function.

I was able to improve the situation by adding the DEBUG=true env variable, which I noticed here:

https://github.com/Brendonovich/swift-rs/blob/50a909d259ffcc743ae9232eac7d913c66f7e939/src-rs/build.rs#L222-L224

The new and improved stack trace showed the crash emanating from the expected function:

Thread 31 Crashed:
0   MyApp 0x100afc9a0 specialized getFrontmostAppWindowID(frontmostAppPid:) + 1860
1   MyApp 0x100afc9a0 specialized getFrontmostAppWindowID(frontmostAppPid:) + 1860
2   MyApp 0x100afc9c4 specialized screen_capture(captureFrontmostWindow:skipCaptureAllBlackImg:frontmostAppPid:) + 32
3   MyApp 0x100af6ebc screen_capture_swift + 12

NOTE that I also tried decorating the function in question with @inline(never), but it didn't seem to help. Maybe since it was defaulting to a release build it was ignoring those compiler hints.

I didn't see the DEBUG flag mentioned in the docs, but maybe I missed it. If it's not there, would you take a PR to update the README?

swift-rs has been working great for me, and thanks again @Brendonovich for creating this project!

Brendonovich commented 2 months ago

DEBUG is an env var set by cargo for build scripts, it should be true by default when compiling in debug mode. Were you compiling in debug or release mode? I'm not familiar with @inline(never) and the like.

tleyden commented 2 months ago

@Brendonovich I'm compiling via Tauri+yarn (which invokes cargo), and it was happening with both yarn tauri dev and yarn tauri release.

Maybe they don't set DEBUG?