def _excepthook(exctype, value, tb): global _handle_exceptions if _handle_exceptions: exception_breakpoint = get_exception_breakpoint(exctype, _handle_exceptions) else: exception_breakpoint = None #Always call the original excepthook before going on to call the debugger post mortem to show it. _original_excepthook(exctype, value, tb) if not exception_breakpoint: return if tb is None: #sometimes it can be None, e.g. with GTK return frames = [] while tb: frames.append(tb.tb_frame) tb = tb.tb_next thread = threadingCurrentThread() frames_byid = dict([(id(frame),frame) for frame in frames]) frame = frames[-1] thread.additionalInfo.exception = (exctype, value, tb) thread.additionalInfo.pydev_force_stop_at_exception = (frame, frames_byid) thread.additionalInfo.message = exception_breakpoint.qname debugger = GetGlobalDebugger() pydevd_tracing.SetTrace(None) #no tracing from here pydev_log.debug('Handling post-mortem stop on exception breakpoint %s'% exception_breakpoint.qname) debugger.handle_post_mortem_stop(thread.additionalInfo, thread)
def __call__(self): from pydevd_comm import GetGlobalDebugger global_debugger = GetGlobalDebugger() if global_debugger is not None: global_debugger.SetTrace(global_debugger.trace_dispatch) return self.original_func(*self.args, **self.kwargs)
def excepthook(exctype, value, tb): global _handle_exceptions if _handle_exceptions is not None: exception_breakpoint = get_exception_breakpoint(exctype, _handle_exceptions, NOTIFY_ON_TERMINATE) else: exception_breakpoint = None if exception_breakpoint is None: return _original_excepthook(exctype, value, tb) # Always call the original excepthook before going on to call the debugger post mortem to show it. _original_excepthook(exctype, value, tb) if tb is None: # sometimes it can be None, e.g. with GTK return frames = [] traceback = tb while tb: frames.append(tb.tb_frame) tb = tb.tb_next thread = threadingCurrentThread() frames_byid = dict([(id(frame), frame) for frame in frames]) frame = frames[-1] thread.additionalInfo.exception = (exctype, value, tb) thread.additionalInfo.pydev_force_stop_at_exception = (frame, frames_byid) thread.additionalInfo.message = exception_breakpoint.qname # sys.exc_info = lambda : (exctype, value, traceback) debugger = GetGlobalDebugger() debugger.force_post_mortem_stop += 1 pydevd_tracing.SetTrace(None) # no tracing from here debugger.handle_post_mortem_stop(thread.additionalInfo, thread)
def _schedule_callback(prev, next): ''' Called when a context is stopped or a new context is made runnable. ''' try: if not prev and not next: return if next: register_tasklet_info(next) # Ok, making next runnable: set the tracing facility in it. debugger = GetGlobalDebugger() if debugger is not None and next.frame: if hasattr(next.frame, 'f_trace'): next.frame.f_trace = debugger.trace_dispatch debugger = None if prev: register_tasklet_info(prev) try: for tasklet_ref, tasklet_info in list(_weak_tasklet_registered_to_info.items()): # Make sure it's a copy! tasklet = tasklet_ref() if tasklet is None or not tasklet.alive: # Garbage-collected already! try: del _weak_tasklet_registered_to_info[tasklet_ref] except KeyError: pass if tasklet_info.frame_id is not None: removeCustomFrame(tasklet_info.frame_id) else: if tasklet.paused or tasklet.blocked or tasklet.scheduled: if tasklet.frame and tasklet.frame.f_back: f_back = tasklet.frame.f_back _filename, base = GetFilenameAndBase(f_back) is_file_to_ignore = DictContains(DONT_TRACE, base) if not is_file_to_ignore: if tasklet_info.frame_id is None: tasklet_info.frame_id = addCustomFrame(f_back, tasklet_info.tasklet_name, tasklet.thread_id) else: updateCustomFrame(tasklet_info.frame_id, f_back, tasklet.thread_id) elif tasklet.is_current: if tasklet_info.frame_id is not None: # Remove info about stackless suspended when it starts to run. removeCustomFrame(tasklet_info.frame_id) tasklet_info.frame_id = None finally: tasklet = None tasklet_info = None f_back = None except: import traceback;traceback.print_exc() if _application_set_schedule_callback is not None: return _application_set_schedule_callback(prev, next)
def _schedule_callback(prev, next): ''' Called when a context is stopped or a new context is made runnable. ''' try: if not prev and not next: return if not prev: # Ok, making next runnable: set the tracing facility in it. debugger = GetGlobalDebugger() if debugger is not None: next.frame.f_trace = debugger.trace_dispatch elif not next: pass else: # Suspending prev and resuming next #print'changing', prev, next frameId = id_to_tasklet_frame.get(id(prev)) if frameId is not None: if prev.frame is not None: replaceCustomFrame(frameId, prev.frame.f_back) else: removeCustomFrame(frameId) #Check: next.frame.f_trace: except: import traceback traceback.print_exc() if _application_set_schedule_callback is not None: return _application_set_schedule_callback(prev, next)
def run(*args, **kwargs): debugger = GetGlobalDebugger() if debugger is not None: SetTrace(debugger.trace_dispatch) debugger = None return _original_run(*args, **kwargs)
def __call__(self): from pydevd_comm import GetGlobalDebugger global_debugger = GetGlobalDebugger() if global_debugger is not None: global_debugger.SetTrace(global_debugger.trace_dispatch) if global_debugger.thread_analyser is not None: # we can detect start_new_thread only here try: from pydevd_concurrency_analyser.pydevd_concurrency_logger import log_new_thread log_new_thread(global_debugger) except: sys.stderr.write( "Failed to detect new thread for visualization") return self.original_func(*self.args, **self.kwargs)
def __delete__(self, obj): global_debugger = GetGlobalDebugger() try: if global_debugger is not None and global_debugger.disable_property_deleter_trace: pydevd_tracing.SetTrace(None) if self.fdel is None: raise AttributeError("can't delete attribute") self.fdel(obj) finally: if global_debugger is not None: pydevd_tracing.SetTrace(global_debugger.trace_dispatch)
def __set__(self, obj, value): global_debugger = GetGlobalDebugger() try: if global_debugger is not None and global_debugger.disable_property_setter_trace: pydevd_tracing.SetTrace(None) if self.fset is None: raise AttributeError("can't set attribute") self.fset(obj, value) finally: if global_debugger is not None: pydevd_tracing.SetTrace(global_debugger.trace_dispatch)
def __get__(self, obj, objtype=None): if obj is None: return self global_debugger = GetGlobalDebugger() try: if global_debugger is not None and global_debugger.disable_property_getter_trace: pydevd_tracing.SetTrace(None) if self.fget is None: raise AttributeError("unreadable attribute") return self.fget(obj) finally: if global_debugger is not None: pydevd_tracing.SetTrace(global_debugger.trace_dispatch)
def new_f(old_f, args, kwargs): debugger = GetGlobalDebugger() if debugger is not None: SetTrace(debugger.trace_dispatch) debugger = None # Remove our own traces :) self.tempval = old_f register_tasklet_info(self) # Hover old_f to see the stackless being created and *args and **kwargs to see its parameters. return old_f(*args, **kwargs)
def run(*args, **kwargs): debugger = GetGlobalDebugger() if debugger is not None: SetTrace(debugger.trace_dispatch) f_back = sys._getframe().f_back frameId = addCustomFrame(f_back, 'Main Tasklet Run') tasklet_id = id(f_back) id_to_tasklet_frame[tasklet_id] = frameId try: return _original_run(*args, **kwargs) finally: removeCustomFrame(frameId) del id_to_tasklet_frame[tasklet_id]
def excepthook(exctype, value, tb): global _handle_exceptions if _handle_exceptions is not None: exception_breakpoint = get_exception_breakpoint( exctype, _handle_exceptions, NOTIFY_ON_TERMINATE) else: exception_breakpoint = None if exception_breakpoint is None: return _original_excepthook(exctype, value, tb) #Always call the original excepthook before going on to call the debugger post mortem to show it. _original_excepthook(exctype, value, tb) if tb is None: #sometimes it can be None, e.g. with GTK return frames = [] traceback = tb while tb: frames.append(tb.tb_frame) tb = tb.tb_next thread = threadingCurrentThread() frames_byid = dict([(id(frame), frame) for frame in frames]) frame = frames[-1] thread.additionalInfo.exception = (exctype, value, tb) thread.additionalInfo.pydev_force_stop_at_exception = (frame, frames_byid) thread.additionalInfo.message = exception_breakpoint.qname #sys.exc_info = lambda : (exctype, value, traceback) debugger = GetGlobalDebugger() debugger.force_post_mortem_stop += 1 pydevd_tracing.SetTrace(None) #no tracing from here debugger.handle_post_mortem_stop(thread.additionalInfo, thread)
def new_f(old_f, args, kwargs): debugger = GetGlobalDebugger() if debugger is not None: SetTrace(debugger.trace_dispatch) frameId = addCustomFrame(caller_frame, 'Tasklet') tasklet_id = id(self) id_to_tasklet_frame[tasklet_id] = frameId try: # Note: if the debugger appears in the line below, it means that a tasklet was created # but it's still not running. # Hover old_f to see the stackless being created and *args and **kwargs to see its parameters. old_f(*args, **kwargs) finally: removeCustomFrame(frameId) del id_to_tasklet_frame[tasklet_id]
def excepthook(exctype, value, tb): if _handle_exceptions is not None: if not issubclass(exctype, _handle_exceptions): return _original_excepthook(exctype, value, tb) #Always call the original excepthook before going on to call the debugger post mortem to show it. _original_excepthook(exctype, value, tb) frames = [] while tb: frames.append(tb.tb_frame) tb = tb.tb_next thread = threadingCurrentThread() frames_byid = dict([(id(frame), frame) for frame in frames]) frame = frames[-1] thread.additionalInfo.pydev_force_stop_at_exception = (frame, frames_byid) debugger = GetGlobalDebugger() debugger.force_post_mortem_stop += 1
def ExecuteTestsInParallel(tests, jobs, split, verbosity, coverage_files, coverage_include): ''' @param tests: list(PydevTestSuite) A list with the suites to be run @param split: str Either 'module' or the number of tests that should be run in each batch @param coverage_files: list(file) A list with the files that should be used for giving coverage information (if empty, coverage information should not be gathered). @param coverage_include: str The pattern that should be included in the coverage. @return: bool Returns True if the tests were actually executed in parallel. If the tests were not executed because only 1 should be used (e.g.: 2 jobs were requested for running 1 test), False will be returned and no tests will be run. It may also return False if in debug mode (in which case, multi-processes are not accepted) ''' try: from pydevd_comm import GetGlobalDebugger if GetGlobalDebugger() is not None: return False except: pass #Ignore any error here. #This queue will receive the tests to be run. Each entry in a queue is a list with the tests to be run together When #split == 'tests', each list will have a single element, when split == 'module', each list will have all the tests #from a given module. tests_queue = [] queue_elements = [] if split == 'module': module_to_tests = {} for test in tests: lst = [] FlattenTestSuite(test, lst) for test in lst: key = (test.__pydev_pyfile__, test.__pydev_module_name__) module_to_tests.setdefault(key, []).append(test) for key, tests in module_to_tests.items(): queue_elements.append(tests) if len(queue_elements) < jobs: #Don't create jobs we will never use. jobs = len(queue_elements) elif split == 'tests': for test in tests: lst = [] FlattenTestSuite(test, lst) for test in lst: queue_elements.append([test]) if len(queue_elements) < jobs: #Don't create jobs we will never use. jobs = len(queue_elements) else: raise AssertionError('Do not know how to handle: %s' % (split,)) for test_cases in queue_elements: test_queue_elements = [] for test_case in test_cases: try: test_name = test_case.__class__.__name__+"."+test_case._testMethodName except AttributeError: #Support for jython 2.1 (__testMethodName is pseudo-private in the test case) test_name = test_case.__class__.__name__+"."+test_case._TestCase__testMethodName test_queue_elements.append(test_case.__pydev_pyfile__+'|'+test_name) tests_queue.append(test_queue_elements) if jobs < 2: return False sys.stdout.write('Running tests in parallel with: %s jobs.\n' %(jobs,)) queue = Queue.Queue() for item in tests_queue: queue.put(item, block=False) providers = [] clients = [] for i in range(jobs): test_cases_provider = CommunicationThread(queue) providers.append(test_cases_provider) test_cases_provider.start() port = test_cases_provider.port if coverage_files: clients.append(ClientThread(i, port, verbosity, coverage_files.pop(0), coverage_include)) else: clients.append(ClientThread(i, port, verbosity)) for client in clients: client.start() client_alive = True while client_alive: client_alive = False for client in clients: #Wait for all the clients to exit. if not client.finished: client_alive = True time.sleep(.2) break for provider in providers: provider.shutdown() return True
def _debugger_break(self): t = pydevd.threadingCurrentThread() debugger = GetGlobalDebugger() debugger.setSuspend(t, CMD_SET_BREAK)
def _on_set_trace_for_new_thread(): from pydevd_comm import GetGlobalDebugger global_debugger = GetGlobalDebugger() if global_debugger is not None: global_debugger.SetTrace(global_debugger.trace_dispatch)
def _locked_settrace(host, stdoutToServer, stderrToServer, port, suspend, trace_only_current_thread): if host is None: import pydev_localhost host = pydev_localhost.get_localhost() global connected global bufferStdOutToServer global bufferStdErrToServer if not connected: connected = True bufferStdOutToServer = stdoutToServer bufferStdErrToServer = stderrToServer pydevd_vm_type.SetupType() debugger = PyDB() debugger.connect(host, port) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.reader" id="-1"/></xml>') debugger.writer.addCommand(net) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.writer" id="-1"/></xml>') debugger.writer.addCommand(net) if bufferStdOutToServer: sys.stdoutBuf = pydevd_io.IOBuf() sys.stdout = pydevd_io.IORedirector( sys.stdout, sys.stdoutBuf) #@UndefinedVariable if bufferStdErrToServer: sys.stderrBuf = pydevd_io.IOBuf() sys.stderr = pydevd_io.IORedirector( sys.stderr, sys.stderrBuf) #@UndefinedVariable SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo while not debugger.readyToRun: time.sleep(0.1) # busy wait until we receive run command if suspend: debugger.setSuspend(t, CMD_SET_BREAK) #note that we do that through pydevd_tracing.SetTrace so that the tracing #is not warned to the user! pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass PyDBCommandThread(debugger).start() else: #ok, we're already in debug mode, with all set, so, let's just set the break debugger = GetGlobalDebugger() SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass if suspend: debugger.setSuspend(t, CMD_SET_BREAK)
def _schedule_callback(prev, next): ''' Called when a context is stopped or a new context is made runnable. ''' try: if not prev and not next: return current_frame = sys._getframe() if next: register_tasklet_info(next) # Ok, making next runnable: set the tracing facility in it. debugger = GetGlobalDebugger() if debugger is not None: next.trace_function = debugger.trace_dispatch frame = next.frame if frame is current_frame: frame = frame.f_back if hasattr(frame, 'f_trace'): # Note: can be None (but hasattr should cover for that too). frame.f_trace = debugger.trace_dispatch debugger = None if prev: register_tasklet_info(prev) try: for tasklet_ref, tasklet_info in list(_weak_tasklet_registered_to_info.items()): # Make sure it's a copy! tasklet = tasklet_ref() if tasklet is None or not tasklet.alive: # Garbage-collected already! try: del _weak_tasklet_registered_to_info[tasklet_ref] except KeyError: pass if tasklet_info.frame_id is not None: removeCustomFrame(tasklet_info.frame_id) else: is_running = stackless.get_thread_info(tasklet.thread_id)[1] is tasklet if tasklet is prev or (tasklet is not next and not is_running): # the tasklet won't run after this scheduler action: # - the tasklet is the previous tasklet # - it is not the next tasklet and it is not an already running tasklet frame = tasklet.frame if frame is current_frame: frame = frame.f_back if frame is not None: _filename, base = GetFilenameAndBase(frame) # print >>sys.stderr, "SchedCB: %r, %d, '%s', '%s'" % (tasklet, frame.f_lineno, _filename, base) is_file_to_ignore = DictContains(DONT_TRACE, base) if not is_file_to_ignore: tasklet_info.update_name() if tasklet_info.frame_id is None: tasklet_info.frame_id = addCustomFrame(frame, tasklet_info.tasklet_name, tasklet.thread_id) else: updateCustomFrame(tasklet_info.frame_id, frame, tasklet.thread_id, name=tasklet_info.tasklet_name) elif tasklet is next or is_running: if tasklet_info.frame_id is not None: # Remove info about stackless suspended when it starts to run. removeCustomFrame(tasklet_info.frame_id) tasklet_info.frame_id = None finally: tasklet = None tasklet_info = None frame = None except: import traceback;traceback.print_exc() if _application_set_schedule_callback is not None: return _application_set_schedule_callback(prev, next)
def __call__(self): global_debugger = GetGlobalDebugger() pydevd_tracing.SetTrace(global_debugger.trace_dispatch) self.original_func(*self.args, **self.kwargs)
def set_trace_in_qt(): import pydevd_tracing from pydevd_comm import GetGlobalDebugger debugger = GetGlobalDebugger() if debugger is not None: pydevd_tracing.SetTrace(debugger.trace_dispatch)
def settrace(host='localhost', stdoutToServer=False, stderrToServer=False, port=5678, suspend=True, trace_only_current_thread=True): '''Sets the tracing function with the pydev debug function and initializes needed facilities. @param host: the user may specify another host, if the debug server is not in the same machine @param stdoutToServer: when this is true, the stdout is passed to the debug server @param stderrToServer: when this is true, the stderr is passed to the debug server so that they are printed in its console and not in this process console. @param port: specifies which port to use for communicating with the server (note that the server must be started in the same port). @note: currently it's hard-coded at 5678 in the client @param suspend: whether a breakpoint should be emulated as soon as this function is called. @param trace_only_current_thread: determines if only the current thread will be traced or all future threads will also have the tracing enabled. ''' global connected global bufferStdOutToServer global bufferStdErrToServer if not connected: connected = True bufferStdOutToServer = stdoutToServer bufferStdErrToServer = stderrToServer pydevd_vm_type.SetupType() debugger = PyDB() debugger.connect(host, port) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.reader" id="-1"/></xml>') debugger.writer.addCommand(net) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.writer" id="-1"/></xml>') debugger.writer.addCommand(net) if bufferStdOutToServer: sys.stdoutBuf = pydevd_io.IOBuf() sys.stdout = pydevd_io.IORedirector( sys.stdout, sys.stdoutBuf) #@UndefinedVariable if bufferStdErrToServer: sys.stderrBuf = pydevd_io.IOBuf() sys.stderr = pydevd_io.IORedirector( sys.stderr, sys.stderrBuf) #@UndefinedVariable SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo while not debugger.readyToRun: time.sleep(0.1) # busy wait until we receive run command if suspend: debugger.setSuspend(t, CMD_SET_BREAK) #note that we do that through pydevd_tracing.SetTrace so that the tracing #is not warned to the user! pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass PyDBCommandThread(debugger).start() else: #ok, we're already in debug mode, with all set, so, let's just set the break debugger = GetGlobalDebugger() SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass if suspend: debugger.setSuspend(t, CMD_SET_BREAK)