| 54 | extern "C" int DoAttach(bool isDebug, const char *command, bool showDebugInfo); |
| 55 | |
| 56 | int DoAttach(bool isDebug, const char *command, bool showDebugInfo) |
| 57 | { |
| 58 | void *module = dlopen(nullptr, 0x2); |
| 59 | DEFINE_PROC(isInitFunc, Py_IsInitialized*, "Py_IsInitialized", 1); |
| 60 | DEFINE_PROC(gilEnsure, PyGILState_Ensure*, "PyGILState_Ensure", 51); |
| 61 | DEFINE_PROC(gilRelease, PyGILState_Release*, "PyGILState_Release", 51); |
| 62 | |
| 63 | |
| 64 | if(!isInitFunc()){ |
| 65 | if(showDebugInfo){ |
| 66 | printf("Py_IsInitialized returned false.\n"); |
| 67 | } |
| 68 | return 2; |
| 69 | } |
| 70 | |
| 71 | PythonVersion version = GetPythonVersion(module); |
| 72 | |
| 73 | DEFINE_PROC(interpHead, PyInterpreterState_Head*, "PyInterpreterState_Head", 51); |
| 74 | |
| 75 | auto head = interpHead(); |
| 76 | if (head == nullptr) { |
| 77 | // this interpreter is loaded but not initialized. |
| 78 | if(showDebugInfo){ |
| 79 | printf("Interpreter not initialized!\n"); |
| 80 | } |
| 81 | return 54; |
| 82 | } |
| 83 | |
| 84 | // Note: unlike windows where we have to do many things to enable threading |
| 85 | // to work to get the gil, here we'll be executing in an existing thread, |
| 86 | // so, it's mostly a matter of getting the GIL and running it and we shouldn't |
| 87 | // have any more problems. |
| 88 | |
| 89 | DEFINE_PROC(pyRun_SimpleString, PyRun_SimpleString*, "PyRun_SimpleString", 51); |
| 90 | |
| 91 | GilHolder gilLock(gilEnsure, gilRelease); // acquire and hold the GIL until done... |
| 92 | |
| 93 | pyRun_SimpleString(command); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | // This is the function which enables us to set the sys.settrace for all the threads |
no test coverage detected