Skip to content

Instantly share code, notes, and snippets.

@jeremyd2019
Last active April 1, 2021 02:46
Show Gist options
  • Save jeremyd2019/3069ae1d44e86f4695087b98271af5d9 to your computer and use it in GitHub Desktop.
Save jeremyd2019/3069ae1d44e86f4695087b98271af5d9 to your computer and use it in GitHub Desktop.
Just-in-time debugging in MSYS2

MSYS2 processes

To get just-in-time debugging of MSYS2 processes, use the error_start MSYS environment variable setting:

export MSYS="error_start:$(cygpath -w /usr/bin/gdb)"
./crashy.exe

Native Windows processes

MINGW gdb can be used as a Windows JIT debugger. This is documented here under signal-event.

As Administrator:

regtool add -w '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug'
regtool set -w '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug/Debugger' \"$(cygpath -w /mingw64/bin/gdb.exe)'" -ex "attach %ld" -ex "signal-event %ld" -ex "continue"'
regtool set -w '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug/Auto' 1

regtool add -W '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug'
regtool set -W '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug/Debugger' \"$(cygpath -w /mingw32/bin/gdb.exe)'" -ex "attach %ld" -ex "signal-event %ld" -ex "continue"'
regtool set -W '/HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug/Auto' 1

Native Windows processes started from MSYS2

When a native process which was started (possibly indirectly) from an MSYS2 process (such as bash) crashes, it does not invoke the registered debugger (or Windows Error Reporting), unless the SetErrorMode SEM_NOGPFAULTERRORBOX flag was cleared in the meantime (SetErrorMode flags are inherited from a parent process by default). As of msys2-runtime 3.2.0-2, it is possible to tell MSYS2 to create processes without inheriting its error mode flags by setting an MSYS environment variable setting:

export MSYS=winjitdebug
exec bash
./crashy.exe

(note that the option needs to be set in the parent process, so bash needs to be restarted, assuming you are starting processes from bash).

Crashy test program

crashy.c:

int main()
{
        volatile char * foo = (void*)0;
        *foo = 42;
        return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment