How to release the mouse when debugging in gdb

SDL grabs the mouse when calling SDL_SetRelativeMouseMode(SDL_TRUE) or
SDL_SetWindowGrab(win, SDL_TRUE).
When breaking into the debugger, it is not released, at least on
Linux/X11, which is kinda annoying.
SDL itself can’t do anything about this: When the debugger takes over,
your application (or SDL) can’t do anything, not even release the mouse.

However, gdb allows you to call functions in your program, so you can
alt-tab (or something) to the terminal gdb is running in and enter "call
SDL_SetRelativeMouseMode(0)"
For SDL_SetWindowGrab() it’s harder because you also need the window
handle - if you use that I’d suggest that you create a function like
DEBUG_UngrabMouse() that calls SDL_SetWindowGrab(yourWindowHandle, 0) so
you can just “call DEBUG_UngrabMouse()” from gdb.

(In theory it should also be possible to send an XF86Ungrab key event,
e.g. with “xdotool key XF86Ungrab”, but at least on my system that
doesn’t work)

Anyway, you still need to get to the gdb prompt and especially if you’re
not running gdb in a terminal but from a GUI like Eclipse CDT, QTCreator
or whatever, this is pretty hard.
Fortunately, gdb has a python scripting API that allows you to execute
python functions whenever you break into the debugger.

So the awesome KittyCat in #SDL (who isn’t signed up in this ML) wrote a
python-script which hooks into those events and releases the mouse, see
attachment.

To use it, put the python code in a file named
yourexecutablename-gdb.py, next to the executable you wanna debug.
So if your debug-executable is /path/to/your/project/MyGame, you copy
the script to /path/to/your/project/MyGame-gdb.py

Furthermore you need to allow gdb to execute it, you can do that by
adding the line
add-auto-load-safe-path /path/to/your/project
to $HOME/.gdbinit - that will allow gdb to load python scripts from that
path and all its (direct and indirect) subdirectories.

If you use SDL_SetWindowGrab(), it won’t work out of the box (because
there is no way to get the SDL_Window* handle in gdb that works for all
projects), in that case write a DEBUG_UngrabMouse() function as
described above and modify the script to call it.

Cheers,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL2-releasemouse-gdb.py
Type: text/x-python
Size: 1181 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150310/b72ae2f6/attachment.py

Nice work! :)On Tue, Mar 10, 2015 at 1:41 PM, Daniel Gibson wrote:

SDL grabs the mouse when calling SDL_SetRelativeMouseMode(SDL_TRUE) or
SDL_SetWindowGrab(win, SDL_TRUE).
When breaking into the debugger, it is not released, at least on
Linux/X11, which is kinda annoying.
SDL itself can’t do anything about this: When the debugger takes over,
your application (or SDL) can’t do anything, not even release the mouse.

However, gdb allows you to call functions in your program, so you can
alt-tab (or something) to the terminal gdb is running in and enter "call
SDL_SetRelativeMouseMode(0)"
For SDL_SetWindowGrab() it’s harder because you also need the window
handle - if you use that I’d suggest that you create a function like
DEBUG_UngrabMouse() that calls SDL_SetWindowGrab(yourWindowHandle, 0) so
you can just “call DEBUG_UngrabMouse()” from gdb.

(In theory it should also be possible to send an XF86Ungrab key event,
e.g. with “xdotool key XF86Ungrab”, but at least on my system that doesn’t
work)

Anyway, you still need to get to the gdb prompt and especially if you’re
not running gdb in a terminal but from a GUI like Eclipse CDT, QTCreator or
whatever, this is pretty hard.
Fortunately, gdb has a python scripting API that allows you to execute
python functions whenever you break into the debugger.

So the awesome KittyCat in #SDL (who isn’t signed up in this ML) wrote a
python-script which hooks into those events and releases the mouse, see
attachment.

To use it, put the python code in a file named
yourexecutablename-gdb.py, next to the executable you wanna debug.
So if your debug-executable is /path/to/your/project/MyGame, you copy the
script to /path/to/your/project/MyGame-gdb.py

Furthermore you need to allow gdb to execute it, you can do that by adding
the line
add-auto-load-safe-path /path/to/your/project
to $HOME/.gdbinit - that will allow gdb to load python scripts from that
path and all its (direct and indirect) subdirectories.

If you use SDL_SetWindowGrab(), it won’t work out of the box (because
there is no way to get the SDL_Window* handle in gdb that works for all
projects), in that case write a DEBUG_UngrabMouse() function as described
above and modify the script to call it.

Cheers,
Daniel


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Nice work! :slight_smile:

Thanks! :slight_smile:

BTW, why does my post not show up in
https://forums.libsdl.org/viewforum.php?f=1 ?
It only shows your reply.

Furthermore: http://lists.libsdl.org/pipermail/sdl-libsdl.org/ is
missing February and March (and probably half of January).

Cheers,
DanielOn 03/11/2015 01:41 AM, Sam Lantinga wrote:

On Tue, Mar 10, 2015 at 1:41 PM, Daniel Gibson <@Daniel_Gibson mailto:Daniel_Gibson> wrote:

SDL grabs the mouse when calling
SDL_SetRelativeMouseMode(SDL___TRUE) or SDL_SetWindowGrab(win,
SDL_TRUE).
When breaking into the debugger, it is not released, at least on
Linux/X11, which is kinda annoying.
SDL itself can't do anything about this: When the debugger takes
over, your application (or SDL) can't do anything, not even release
the mouse.

However, gdb allows you to call functions in your program, so you
can alt-tab (or something) to the terminal gdb is running in and
enter "call SDL_SetRelativeMouseMode(0)"
For SDL_SetWindowGrab() it's harder because you also need the window
handle - if you use that I'd suggest that you create a function like
DEBUG_UngrabMouse() that calls SDL_SetWindowGrab(__yourWindowHandle,
0) so you can just "call DEBUG_UngrabMouse()" from gdb.

(In theory it should also be possible to send an XF86Ungrab key
event, e.g. with "xdotool key XF86Ungrab", but at least on my system
that doesn't work)

Anyway, you still need to get to the gdb prompt and especially if
you're not running gdb in a terminal but from a GUI like Eclipse
CDT, QTCreator or whatever, this is pretty hard.
Fortunately, gdb has a python scripting API that allows you to
execute python functions whenever you break into the debugger.

So the awesome KittyCat in #SDL (who isn't signed up in this ML)
wrote a python-script which hooks into those events and releases the
mouse, see attachment.

To use it, put the python code in a file named
yourexecutablename-gdb.py, next to the executable you wanna debug.
So if your debug-executable is /path/to/your/project/MyGame, you
copy the script to /path/to/your/project/MyGame-__gdb.py

Furthermore you need to allow gdb to execute it, you can do that by
adding the line
   add-auto-load-safe-path /path/to/your/project
to $HOME/.gdbinit - that will allow gdb to load python scripts from
that path and all its (direct and indirect) subdirectories.

If you use SDL_SetWindowGrab(), it won't work out of the box
(because there is no way to get the SDL_Window* handle in gdb that
works for all projects), in that case write a DEBUG_UngrabMouse()
function as described above and modify the script to call it.

Cheers,
Daniel

_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Sorry for unburying this old post, but I can’t edit it and in case you find it via search (like I just did - yeah, it’s fun when past-you answers your own questions) and wonder what the mentioned python script looked like, see this post: debugging with SDL_CaptureMouse - #3 by Daniel_Gibson