简体   繁体   中英

How to attach to a python process natively?

There is a C++ DLL used via C interop from Python via ctypes . At some point the C++ program realizes that it needs to be debugged. I tried to call __debugbreak() in such cases, but the Python process simply terminates.

What can I do to debug a C++ DLL used by Python?

There are a lot of details and I'm not sure what else to provide - please, ask in comments.

UPDATE: currently the target Platform is Windows. I cannot simply attach to Python process with Visual Studio because everything happens quickly. So perhaps I need to introduce a pause to the Python process... but this doesn't look like an elegant option. I would rather fix the problem that Python terminates upon a call to __debugbreak()

Following the comment of @RbMm, here is a solution that seems most convenient to me.

In C++ code write the following function and make it available to call from Python:

#include <windows.h>
#include <string>

__declspec(dllexport) void debug_break() {
    if(!IsDebuggerPresent()) {
        std::string message("The application has requested a debug for process ID=");
        message += std::to_string(GetCurrentProcessId());
        MessageBoxA(nullptr, message.c_str(), "Attach Debugger", 0);
    }
    __debugbreak();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM