# How to release the mouse when debugging in gdb

**URL:** <https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275>\
**Category:** SDL Development\
**Created:** [March 10, 2015, 8:41pm UTC](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275 "2015-03-10T20:41:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 10, 2015, 8:41pm UTC](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275/1 "2015-03-10T20:41:16Z")

</div>

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](http://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](http://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](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150310/b72ae2f6/attachment.py)

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [March 11, 2015, 12:41am UTC](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275/2 "2015-03-11T00:41:58Z")

</div>

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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 11, 2015, 12:59am UTC](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275/3 "2015-03-11T00:59:11Z")

</div>

> Nice work! 🙂

Thanks! 🙂

BTW, why does my post not show up in  
[SDL Development - Simple Directmedia Layer](https://forums.libsdl.org/viewforum.php?f=1) ?  
It only shows your reply.

Furthermore: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/](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](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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 24, 2021, 12:36am UTC](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275/4 "2021-03-24T00:36:46Z")

</div>

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](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/3)
