GDB Debugging FreeBasic EXE running in WINE on Linux

Forum for discussion about the documentation project.
Post Reply
coderx
Posts: 3
Joined: Nov 12, 2023 3:11

GDB Debugging FreeBasic EXE running in WINE on Linux

Post by coderx »

I've spent a bit of time trying to figure out exactly how to debug when the FB compiled EXE runs in WINE on linux. These are my findings:

Summary:
GDB can load the file and run it, it can pause at breakpoints, but I could only get assembly language code to display. No matter what I did I could not get GDB to sync up with the FreeBasic Source.

How to (for Debian variants):
1. Update/upgrade your system from a new terminal window.
sudo apt update
sudo apt upgrade

2. Install gdb host and remote and windows tools:
sudo apt install g++-mingw-w64-x86-64 gdb-mingw-w64 gdb-mingw-w64-target

3. Set up a 64 bit wine prefix. Here I create it in ~/wine64, you may use something else but remember to change the directory name throughout.
WINEARCH=win64 WINEPREFIX=~/wine64 wineboot --init

4. Configure the WINE prefix. Under the Drives tab, I set my Z: drive to point /usr/share/win32 (or /usr/share/win64), which is where the gdbserver.exe files are. Choose win32 or win64 directory based on the target application EXE (32 bit or 64 bit).
WINEPREFIX=~/wine64 winecfg

5. Install a few useful things into the Wine prefix
WINEPREFIX=~/wine64 winetricks corefonts gdiplus vcrun2019

6. Make sure your FreeBasic application was complied with the "-g" compiler flag, which adds debug information into the EXE.

7. I change over to the folder where the FreeBasic EXE and source code are located. Maybe something like (replace myappfolder with your own path):
cd ~/wine64/drive_c/myappfolder

8. Launch gdbserver.exe and listen on port 12345
WINEPREFIX=~/wine64 wine z:gdbserver.exe --multi localhost:12345

9. Open a new terminal window.

10. Create a new .gdbinit file - this is convenient for saving settings for GDB and saves a lot of typing.
nano ~/.gdbiinit

11. Put the one line below "set auto-load..." in the .gdbinit file and save it. This gives a directive to look for .gdbinit in the local directory - so you can create a .gdbinit for each of your applications easily.
set auto-load safe-path /

12. Again, change over to the folder where the FreeBaisc EXE and source code are located.
cd ~/wine64/drive_c/myappfolder

13. Now we will make a .gdbinit which has all the commands to load your application on the host and remote.
nano .gdbinit
Put the following lines in .gdbinit (replace myapp.exe etc with your own application name):
file myapp.exe
target extended-remote :12345
set sysroot 'C:\'
dir /home/myuser/wine64/drive_c/myappfolder/
set remote exec-file C:\myappfolder\myapp.exe
set language pascal
list main
list main
b main
catch signal
run

14. Save that .gdbinit file, and now start gdb from the command line. It should automatically load your application, set a breakpoint at main, and jump ahead to the breakpoint at main.
gdb

15. Continue running the application using gdb commands like "c" or "step"

16. You can introduce hard-coded breakpoints in FreeBasic using the inline assembly statement below. These will create an exception which is caught because "catch signal" was provided in .gdbinit.
asm int 3

17. List variables or functions with things like
info variables
info functions

18. Want to get fancier then use an add-on to .gdbinit like gdb-dashboard:
https://github.com/cyrus-and/gdb-dashboard

19. I could not get ddd working in this configuration.

I hope this was helpful. I had a requirement to get a legacy VB6 application off of Windows VMs and into WINE. I decided to try converting it to FreeBasic and see if it will work well that way.
Post Reply