VerySleepy / verysleepy

Very Sleepy, a sampling CPU profiler for Windows
http://www.codersnotes.com/sleepy
GNU General Public License v2.0
1.07k stars 104 forks source link

Mechanism to detect new threads #88

Closed aaalexandrov closed 3 years ago

aaalexandrov commented 3 years ago

Use windows debugger interface to detect the debugee's threads. Fall back to polling a toolhelp snapshot in case debugger interface can't attach of fails. Fixes #4

CyberShadow commented 3 years ago

Amazing. Will look at this soon!

CyberShadow commented 3 years ago

Thanks! Do you think this should be an option? E.g., if someone starts profiling but later decides to attach a debugger, this will now not be possible AIUI. Though, I guess the old behavior is still possible if you just select all threads and profile with those threads. Maybe we only need to document it.

rmitton commented 3 years ago

If the debugger interface doesn't work out, I believe another option is to inject a custom mini DLL into the target's address space -- you can get thread creation notifications via its DllMain callback.

aaalexandrov commented 3 years ago

Thanks! Do you think this should be an option? E.g., if someone starts profiling but later decides to attach a debugger, this will now not be possible AIUI. Though, I guess the old behavior is still possible if you just select all threads and profile with those threads. Maybe we only need to document it.

Yes, my thinking was that you get the old behavior by selecting all existing threads and profiling them but if you have an idea how to make this more obvious in the UI, I can implement it

aaalexandrov commented 3 years ago

If the debugger interface doesn't work out, I believe another option is to inject a custom mini DLL into the target's address space -- you can get thread creation notifications via its DllMain callback.

It works on my machine!!1 On a more serious note, it seems to work fine, except when there's already a debugger attached to the application. And if it fails for whatever reason, it should fall back to the old behavior.

CyberShadow commented 3 years ago

Yep, I think a note in the documentation should be enough. We can update the wiki after this is merged.

LGTM, I rebased on master and added a changelog entry. Let me know if that looks OK to you and we can merge.

aaalexandrov commented 3 years ago

It looks great, thank you :)

aaalexandrov commented 3 years ago

I also looked into how to make this work through dll injection. RenderDoc does this in production code, here's the code https://github.com/baldurk/renderdoc/blob/v1.x/renderdoc/os/win32/win32_process.cpp#L252-L297

CyberShadow commented 3 years ago

We can update the wiki after this is merged.

https://github.com/VerySleepy/verysleepy/wiki/User-Interface/_compare/daaf68544578d05a78639565cfba19de45eb7ec2...f4e42831d7657691c5f03a1f3a6ed33199aef7c4