7

Let's start with description what's happening:

I'm playing with SDL2 library on Windows. I can compile programs using it, and when I run the .exe it works just fine. Problems arise when I try to debug it using GDB - when code comes to the SDL_Init or the SDL_OpenAudio functions (which possibly create new threads), GDB stops, shows "program received signal ?, unknown signal" message, and when I resume execution the program crashes.

Apparently there is a bug in GDB (https://www.mail-archive.com/[email protected]/msg149735.html) related to a thread naming, and it should be fixed in GDB version 7.11.1-1.

At first I was using GCC 5.1.0 (TDM) with GDB 7.6.1, so i decided to update to newer version. It looks that TDM doesn't provide any updates since about two years ago, so I installed MinGW-w64 (I don't remember now, but it might've been version 7.11 of GDB). Didn't help, GDB stil crashes.

Next I searched for newer version of GDB, and found 7.12 (www dot equation dot com/servlet/equation.cmd?fa=gdb). Didn't work, too, maybe fix didn't make it to this version.

Apparently this bug should be only present in x86 version of GDB, so I installed x64 version of TDM (GCC 5.1.0 and GDB 7.9.1). Program compiled fine, but GDB still catches unknown signal and program crashes.

Right now I effectively can't debug any program using SDL2. So, question is, what can I do to have it working again?


Possible solutions:

  • Use Visual Studio - I like Eclipse (and that means I started to tolerate things i don't like in it) and don't really want to learn whole new IDE, but I'll keep that as last option.
  • Compile GDB - tried that, didn't work, compiling things on Windows almost never works for me, and GDB 7.12 had this bug, too.
  • Switch to Linux - Even more radical option than moving to Visual Studio.
  • Fall back to SDL 1.2 - Things were easier back then...
  • Switch to any other library - ...and hope they'll cooperate with GDB. That doesn't really sound like a solution.
  • Switch to different compiler?
  • Disable thread naming?

Code sample:

#include <SDL2/SDL.h>

// Normally I'd use #undef main
int WinMain(int, char**)
    {
    SDL_Init(SDL_INIT_EVERYTHING);

    return 0;
    }

Compilation: g++ gdbtest.cpp -lSDL2main -lSDL2

SDL2 version: 2.0.5 (latest build for Windows, MinGW, 32bit version)


Normal run: a.exe

Results: Program starts and ends normally

Running with GDB: Console log

Results: GDB receives unknown signal, program crashes

2
  • There isn't much to go on. Can you post some minimal code to help us understand? Post more about your environment - the exact libraries you used, the command line arguments used to compile and link, etc...
    – Leonardo
    Commented May 10, 2017 at 14:24
  • Done - code is as minimal as it can get.
    – crueltear
    Commented May 11, 2017 at 8:03

2 Answers 2

6

Ok, I think I found two ways of dealing with this. Source of problem lies in how GDB handles (or at least is supposed to handle) thread naming. To name a thread, one must raise exception with specific attributes. SDL2 does this in SDL_SYS_SetupThread function located in SDL2-2.0.5/src/thread/windows/SDL_systhread.c:168:

RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf);

First option is to comment this line and recompile the library (well, compiling it is a problem in itself). Second option is to add:

SDL_SetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1");

somewhere early in code - in SDL_SYS_SetupThread function there is a call to SDL_GetHintBoolean that returns from function without naming anything if SDL_HINT_WINDOWS_... is false.

Still, finding way around this bug doesn't mean it won't come back to haunt me from some other library naming its threads.

4

Based on your new information, I could reproduce your problem successfully and managed to narrow down the cause of it (but not the root cause or the problem). But first, some consideration about how you're compiling and linking your program:

  • #include <SDL2/SDL.h>: I'd change that to #include "SDL.h" and adjust your include path with -I to point to the include folder of SDL. This way your code is more portable, as the sdl2-config command outputs flags that should be passed to the compiler and -Iis one of them.
  • // Normally I'd use #undef main: Don't do this, it is not needed. What you should do is adjust your link and compile options. SDL2's "main trickery" must work as they expect it to.
  • g++ gdbtest.cpp -lSDL2main -lSDL2: The correct linking flags for Windows usually are, as pointed out by this wiki entry: -lmingw32 -lSDL2main -lSDL2 -mwindows.
  • return 0: please don't forget to call SDL_Quit() at the end of your application.

Now, the problem you're having is that certain flags passed to SDL_Init causes gdb to fail (in the past, I remember that some of them led to hang ups, it isn't the case anymore). So, to directly solve your problem, instead of passing SDL_INIT_EVERYTHING, I passed

SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_HAPTIC | SDL_INIT_HAPTIC | SDL_INIT_EVENTS

leaving out the following:

SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER

and gdb a.exeruns normally.

As I said before, I didn't go down further to check why it makes GDB fails, but this is surely a bug worth reporting. I'd go to https://bugzilla.libsdl.org/, search for a bug simillar to it and create if not found.

1
  • Leaving those flags does indeed fix my sample program, however it crashes when I add SDL_OpenAudio or MIX_OpenAudio. This seems to happen every time a thread is created and named (I guess that SDL_INIT_TIMER starts one). Every version of GDB i tried doesn't seem to support that.
    – crueltear
    Commented May 11, 2017 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.