How Can I Debug Runtime Errors of Native Library in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nahiyan

I built a dynamic library using C++ to connect through GDNative with my Godot game.

The native library compiles without any error and the game runs properly except at times when the game crashes. The Godot debugger provides no information about the crash (no error or warning), just reporting that the debugging process stopped.

Now, it could be GDScript that caused the crash, but we were likely to get information about any runtime error if it was GDScript, right? Thus, I assume that it’s the native library that is causing the runtime issue.

So, how can I ensure that the native library isn’t having runtime issues? Yes, I can test it separately in a C++ debugger but the problem is that it’s totally dependent on the game and I can’t reproduce the scenario that causes the crash when used with Godot.

Is there any way to make Godot capture the standard output/error streams of the library and log them down? C++ usually reports issues about any runtime crash, such as “segmentation fault”.

:bust_in_silhouette: Reply From: sash-rc

You should debug your shared library (dll/so) from IDE, having Godot executable as a host application and set its working directory to your project path.

Then you’ll be able to use all C++'s debugging features (breakpoints, data inspectors. etc.) including all both Godot and your native-lib output in IDE’s captured stdout or in terminal (if enabled).

Thanks for your response. However, I’m confused with the following:

  • What do you mean by debugging from IDE? You mean like using the debugging features of an IDE such as Visual Studio or Visual Studio Code?
  • What do you mean by the Godot executable? Do you mean the executable of the Godot editor or my exported game?
  • What is a host application?

If you can kindly provide an example, it’d be quite helpful for me. I’ve been using Godot for just a couple of months at this point.

Edit: Did you mean debugging the game’s process that is spawned by Godot’s editor?

nahiyan | 2021-06-27 06:22

What do you mean by debugging from IDE? … using the debugging features of an IDE such as Visual Studio or Visual Studio Code?


What do you mean by the Godot executable? What is a host application?

I’m on Linux, and didn’t opened VS for a decade :slight_smile: but try this explanation: Debug from a DLL Project - Visual Studio (Windows) | Microsoft Learn

Any deployable application comes in 2 major forms: standalone app (exe) and shared library (dll). The later (a gdnative case) always needs a host (calling) application to be run within its address space.

Godot application is both editor and runtime host: when you launch it with defaults (by clicking icon) it brings a project chooser. When you launch it (from console with no other options) in project’s directory it will launch your game as it was exported. This is what happens when you hit “F5” in editor: it launches another Godot process and you debug it. Export template (and a final game) is the same executable with stripped down editor features.

Now back to IDE: you should set Host / Launching app / Command (terms may vary) to path/to/godot.exe and Working directory to path/to/dir/of/your/project.godot. The later my be specified in commandline. See godot --help. Then press “debug” in IDE and it will launch your game with ability to debug your code.

sash-rc | 2021-06-27 10:21

This is exactly what I needed. I was totally in the dark about the way Godot can be used from the terminal. I can’t thank you enough!

nahiyan | 2021-06-27 12:39