kwhat / libuiohook

A multi-platform C library to provide global keyboard and mouse hooks from userland.
Other
489 stars 123 forks source link

Severe system-wide lag when breakpoint/exception hit when debugging #197

Open GeorgeBarlow opened 1 month ago

GeorgeBarlow commented 1 month ago

Description

When using libuiohook and pausing execution with a debugger (on breakpoint or an exception that has been caught), the entire system experiences severe lag. This is mainly due to the low-level hooks continuing to handle system-wide events while the application is paused, causing the event queue to back up.

Desired Behavior

Potential Solutions

At a glance, on Windows, the Win32 API has an IsDebuggerPresent() function that could aid in better understanding the environment the process is being used/ran in. However, this function gets called when the process is initially attached to a debugger rather than when the process is halted on exception or breakpoint. The obvious consequence is that we would lose the ability to observe events when a debugger is attached if we decided to unregister the hooks at this point.

Platform

Ideally, as discussed previously with @kwhat, it could be useful to explore a solution for Windows and then expand that to the other supported operating systems. However, this may be subject to change.

kwhat commented 1 month ago

For Windows, I think you can do something like this on line 277.

while (!IsDebuggerPresent() && GetMessage(&message, (HWND) NULL, 0, 0) > 0) {

This will basically just stop the listener when the debugger connects. You won't be able to debug though the hook, but at least it wont prevent the keyboard and mouse from functioning.

Let me know if that works / open a pull-request. I don't have a Windows development environment setup right now so I can't really test.

The more I think about the above, the more I am not convinced it will work. You may need to add something like this around line 65 and 105

if (IsDebuggerPresent()) {
    hook_stop();
}