static void _enum_threads(_ctx* (*f) (PyThreadState *)) { PyThreadState *ts; PyInterpreterState* is; for(is=PyInterpreterState_Head();is!=NULL;is = PyInterpreterState_Next(is)) { for (ts=PyInterpreterState_ThreadHead(is) ; ts != NULL; ts = ts->next) { f(ts); } } }
int rootstate_traverse(NyHeapTraverse *ta) { visitproc visit = ta->visit; NyHeapViewObject *hv = (void *)ta->hv; void *arg = ta->arg; PyThreadState *ts, *bts = PyThreadState_GET(); PyInterpreterState *is; int err; for (is = PyInterpreterState_Head(); is; is = PyInterpreterState_Next(is)) { if (hv->is_hiding_calling_interpreter && is == bts->interp) continue; VISIT(is->modules); VISIT(is->sysdict); VISIT(is->builtins); #if PY_VERSION_HEX >= 0x020303f0 VISIT(is->codec_search_path); VISIT(is->codec_search_cache); VISIT(is->codec_error_registry); #endif for (ts = is->tstate_head; ts; ts = ts->next) { if (ts == bts && hv->limitframe) { VISIT(hv->limitframe); } else if (!hv->limitframe) { VISIT(ts->frame); } VISIT(ts->c_profileobj); VISIT(ts->c_traceobj); VISIT(ts->curexc_type); VISIT(ts->curexc_value); VISIT(ts->curexc_traceback); VISIT(ts->exc_type); VISIT(ts->exc_value); VISIT(ts->exc_traceback); VISIT(ts->dict); #if PY_VERSION_HEX >= 0x020303f0 VISIT(ts->async_exc); #endif } } return 0; }
static PyInterpreterState * interp_look_up_id(PY_INT64_T requested_id) { PyInterpreterState *interp = PyInterpreterState_Head(); while (interp != NULL) { PY_INT64_T id = PyInterpreterState_GetID(interp); if (id < 0) { return NULL; } if (requested_id == id) { return interp; } interp = PyInterpreterState_Next(interp); } return NULL; }
//============================================================================= // METHOD : SPELLwsWarmStartImpl::fixState() //============================================================================= PyFrameObject* SPELLwsWarmStartImpl::fixState() { DEBUG("[WS] Fixing state =============================================="); // Synchronize so that nothing can be done while saving SPELLmonitor m(m_lock); // Get the head interpreter state PyInterpreterState* istate = PyInterpreterState_Head(); // Get the current thread state PyThreadState* oldState = PyThreadState_GET(); DEBUG("[WS] Old state: " + PSTR(oldState)); DEBUG("[WS] Interpreter head: " + PSTR(istate->tstate_head)); DEBUG("[WS] Interpreter next: " + PSTR(istate->next)); DEBUG("[WS] State recursion depth: " + ISTR(oldState->recursion_depth)); DEBUG("[WS] State next: " + PSTR(oldState->next)); // Create a fresh thread state PyThreadState* newState = PyThreadState_New(istate); istate->tstate_head = newState; newState->recursion_depth = oldState->recursion_depth; newState->tracing = oldState->tracing; newState->use_tracing = oldState->use_tracing; newState->tick_counter = oldState->tick_counter; newState->gilstate_counter = oldState->gilstate_counter; newState->dict = PyDict_Copy(oldState->dict); FrameList::iterator it; unsigned int frameCount = m_frames.size(); DEBUG("[WS] Total frames to fix " + ISTR(frameCount)); m_topFrame = NULL; for( unsigned int index = 0; index < frameCount; index++) { bool isHead = (index == (frameCount-1)); DEBUG("[WS] Fix state on frame index " + ISTR(index) + " frame=" + PYCREPR(m_frames[index])); m_topFrame = m_frames[index]; m_topFrame->fixState(newState, isHead); } DEBUG("[WS] State fixed ==============================================="); PyErr_Clear(); return m_topFrame->getFrameObject(); }
/* Return nonzero if the current one is the main interpreter */ static int psyco_is_main_interp(void) { static PyInterpreterState *main_interp = NULL; /* Cached reference */ PyInterpreterState *interp; if (main_interp) { return (main_interp == PyThreadState_Get()->interp); } /* No cached value: cache the proper value and try again. */ interp = PyInterpreterState_Head(); while (interp->next) interp = interp->next; main_interp = interp; assert (main_interp); return psyco_is_main_interp(); }
PyInterpreterState * _PyInterpreterState_LookUpID(PY_INT64_T requested_id) { if (requested_id < 0) goto error; PyInterpreterState *interp = PyInterpreterState_Head(); while (interp != NULL) { PY_INT64_T id = PyInterpreterState_GetID(interp); if (id < 0) return NULL; if (requested_id == id) return interp; interp = PyInterpreterState_Next(interp); } error: PyErr_Format(PyExc_RuntimeError, "unrecognized interpreter ID %lld", requested_id); return NULL; }
//============================================================================ // FUNCTION: SPELLutils::dumpInterpreterInfo() //============================================================================ void SPELLutils::dumpInterpreterInfo( const std::string& id ) { LOG_INFO("Begin dump"); // Cleanup previous files std::string dataDir = getSPELL_DATA() + PATH_SEPARATOR + "Runtime" + PATH_SEPARATOR; std::list<std::string> files = getFilesInDir(dataDir); std::list<std::string>::iterator it; for( it = files.begin(); it != files.end(); it++) { std::string fileName = (*it); if (fileName.find( id + "_interpreter_state") != std::string::npos ) { deleteFile( dataDir + fileName ); } if (fileName.find( id + "_thread_state") != std::string::npos ) { deleteFile( dataDir + fileName ); } if (fileName.find( id + "_frame_state") != std::string::npos ) { deleteFile( dataDir + fileName ); } } LOG_INFO("Get interpreter head"); PyInterpreterState* currIS = PyInterpreterState_Head(); // Count the interpreter state int numIS = 0; // For each interpreter state while(currIS != NULL) { LOG_INFO("Dump information for interpreter state " + ISTR(numIS)); std::string filename = dataDir + id + "_interpreter_state_" + ISTR(numIS) + ".dump"; std::ofstream dumpFile; dumpFile.open( filename.c_str() , std::ios::out ); // Main data dumpFile << "INTERPRETER STATE " << numIS << " DATA" << std::endl; dumpFile << "--------------------------------------" << std::endl; dumpFile << "Address : " << PSTR(currIS) << std::endl; dumpFile << "Next address: " << PSTR(currIS->next) << std::endl; dumpFile << "Modules : " << PYSIZE(currIS->modules) << " items." << std::endl; dumpFile << "Sysdict : " << PYSIZE(currIS->sysdict) << " items." << std::endl; dumpFile << "Builtins : " << PYSIZE(currIS->builtins) << " items." << std::endl; dumpFile << "Reloading : " << PYSIZE(currIS->modules_reloading) << " items." << std::endl; dumpFile << "Search path : " << PYSIZE(currIS->codec_search_path) << " items." << std::endl; dumpFile << "Search cache: " << PYSIZE(currIS->codec_search_cache) << " items." << std::endl; dumpFile << "Error regst.: " << PYSIZE(currIS->codec_error_registry) << " items." << std::endl; dumpFile << "DL flags : " << currIS->dlopenflags << std::endl; // Count thread states int numTS = 0; PyThreadState* currTS = currIS->tstate_head; while( currTS != NULL ) { dumpThreadStateInfo( id, currTS, numIS, numTS ); numTS++; currTS = currTS->next; } dumpFile << "Thr. states : " << numTS << std::endl; // Close the interpreter state dump, no more to add dumpFile.flush(); dumpFile.close(); // Next interpreter state currIS = currIS->next; numIS++; } LOG_INFO("Finish dump"); }
static PyObject * rootstate_getattr(PyObject *obj, PyObject *name) { char *s = PyString_AsString(name); PyInterpreterState *is; int ino; unsigned long tno; char buf[100]; if (!s) return 0; if (sscanf(s, "i%d_%50s", &ino, buf) == 2) { int countis; int numis; for (is = PyInterpreterState_Head(), numis = 0; is; is = PyInterpreterState_Next(is), numis++) ; for (is = PyInterpreterState_Head(), countis = 0; is; is = PyInterpreterState_Next(is), countis++) { int isno = numis - countis - 1; if (isno == ino) { PyObject *ret = PyMember_Get((char *)is, is_members, buf); if (!ret) PyErr_Format(PyExc_AttributeError, "interpreter state has no attribute '%s'", buf); return ret; } } PyErr_SetString(PyExc_AttributeError, "no such interpreter state number"); return 0; } if (sscanf(s, "t%lu_%50s", &tno, buf) == 2) { for (is = PyInterpreterState_Head(); is; is = PyInterpreterState_Next(is)) { PyThreadState *ts; for (ts = is->tstate_head; ts; ts = ts->next) { if (THREAD_ID(ts) == tno) { int frameno = 0; if (sscanf(buf, "f%d", &frameno) == 1) { PyFrameObject *frame; int numframes = 0; for (frame = ts->frame; frame; frame = frame->f_back) { numframes ++; } for (frame = ts->frame; frame; frame = frame->f_back) { numframes --; if (numframes == frameno) { Py_INCREF(frame); return (PyObject *)frame; } } PyErr_Format(PyExc_AttributeError, "thread state has no frame numbered %d from bottom", frameno); return 0; } else { PyObject *ret = PyMember_Get((char *)ts, ts_members, buf); if (!ret) PyErr_Format(PyExc_AttributeError, "thread state has no attribute '%s'", buf); return ret; } } } } } PyErr_Format(PyExc_AttributeError, "root state has no attribute '%.200s'", s); return 0; }
static int rootstate_relate(NyHeapRelate *r) { NyHeapViewObject *hv = (void *)r->hv; PyThreadState *ts, *bts = PyThreadState_GET(); PyInterpreterState *is; int isframe = PyFrame_Check(r->tgt); int isno; for (is = PyInterpreterState_Head(), isno = 0; is; is = PyInterpreterState_Next(is), isno++) ; for (is = PyInterpreterState_Head(), isno--; is; is = PyInterpreterState_Next(is), isno--) { char buf[100]; ISATTR(modules); ISATTR(sysdict); ISATTR(builtins); #if PY_VERSION_HEX >= 0x020303f0 ISATTR(codec_search_path); ISATTR(codec_search_cache); ISATTR(codec_error_registry); #endif for (ts = is->tstate_head; ts; ts = ts->next) { if ((ts == bts && r->tgt == hv->limitframe) || (!hv->limitframe && isframe)) { int frameno = -1; int numframes = 0; PyFrameObject *frame; for (frame = (PyFrameObject *)ts->frame; frame; frame = frame->f_back) { numframes ++; if (r->tgt == (PyObject *)frame) frameno = numframes; } if (frameno != -1) { frameno = numframes - frameno; sprintf(buf,"t%lu_f%d", THREAD_ID(ts), frameno); if (r->visit(NYHR_ATTRIBUTE, PyString_FromString(buf), r)) return 1; } } TSATTR(ts, c_profileobj); TSATTR(ts, c_traceobj); TSATTR(ts, curexc_type); TSATTR(ts, curexc_value); TSATTR(ts, curexc_traceback); TSATTR(ts, exc_type); TSATTR(ts, exc_value); TSATTR(ts, exc_traceback); TSATTR(ts, dict); #if PY_VERSION_HEX >= 0x020303f0 TSATTR(ts, async_exc); #endif } } return 0; }