sys._current_frames.items

Here are the examples of the python api sys._current_frames.items taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

49 Examples 7

Example 1

Project: pyqtgraph Source File: debug.py
    def run(self):
        while True:
            with self.lock:
                if self._stop is True:
                    return
                    
            print("\n=============  THREAD FRAMES:  ================")
            for id, frame in sys._current_frames().items():
                if id == threading.current_thread().ident:
                    continue
                print("<< thread %d >>" % id)
                traceback.print_stack(frame)
            print("===============================================\n")
            
            time.sleep(self.interval)

Example 2

Project: python2-trepan Source File: thred.py
Function: show_it
    def showit():
        print('Current thread: %s' % current_thread_name())
        print('All threads:')
        for thread_id, f in list(sys._current_frames().items()):
            print('  %s %s' % (id2thread_name(thread_id),
                               find_debugged_frame(f)))
            pass
        print('-' * 10)
        print('Thread->id map:')
        print(map_thread_names())
        print('=' * 10)

Example 3

Project: python3-trepan Source File: test-lib-thread.py
    def id_name_checker(self):
        '''Helper for testing map_thread_names and id2thread'''
        name2id = Mthread.map_thread_names()
        for thread_id, f in list(sys._current_frames().items()):
            self.assertEqual(thread_id,
                             name2id[Mthread.id2thread_name(thread_id)])
            # FIXME: use a better test
            self.assertNotEqual(f, Mthread.find_debugged_frame(f))
            pass

Example 4

Project: scrapy Source File: debug.py
    def _thread_stacks(self):
        id2name = dict((th.ident, th.name) for th in threading.enumerate())
        dumps = ''
        for id_, frame in sys._current_frames().items():
            name = id2name.get(id_, '')
            dump = ''.join(traceback.format_stack(frame))
            dumps += "# Thread: {0}({1})\n{2}\n".format(name, id_, dump)
        return dumps

Example 5

Project: gunicorn Source File: example_config.py
def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")

    ## get traceback info
    import threading, sys, traceback
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
            threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    worker.log.debug("\n".join(code))

Example 6

Project: sarge Source File: stack_tracer.py
def _get_stack_traces():
    code = []
    threads = dict((t.ident, t.name) for t in threading.enumerate())
    for threadId, stack in sys._current_frames().items():
        if threadId == threading.current_thread().ident:
            continue
        threadName = threads.get(threadId, 'Unknown')
        code.append('\n# Thread: %s (%s)' % (threadId, threadName))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: %r, line %d, in %s' % (filename, lineno, name))
            if line:
                code.append('  %s' % (line.strip()))

    return '\n'.join(code)

Example 7

Project: linkchecker Source File: __init__.py
    @signal_handler(signal.SIGUSR1)
    def print_threadstacks(sig, frame):
        """Print stack traces of all running threads."""
        log.warn(LOG_THREAD, "*** STACKTRACE START ***")
        for threadId, stack in sys._current_frames().items():
            log.warn(LOG_THREAD, "# ThreadID: %s" % threadId)
            for filename, lineno, name, line in traceback.extract_stack(stack):
                log.warn(LOG_THREAD, 'File: "%s", line %d, in %s' % (filename, lineno, name))
                line = line.strip()
                if line:
                    log.warn(LOG_THREAD, "  %s" % line)
        log.warn(LOG_THREAD, "*** STACKTRACE END ***")

Example 8

Project: ClusterRunner Source File: app_info.py
def _get_formatted_thread_stack_traces():
    """
    Get the formatted stack trace string for each currently running thread.

    :rtype: list[str]
    """
    formatted_traces = []
    threads_by_id = {thread.ident: thread for thread in threading.enumerate()}

    # The sys_current_frames() method is intended to be used for debugging like this.
    for thread_id, stack in sys._current_frames().items():  # pylint: disable=protected-access
        thread = threads_by_id.get(thread_id)
        if thread:
            thread_type = 'daemon' if thread.isDaemon() else 'nondaemon'
            thread_stack_trace = ''.join(traceback.format_stack(stack))
            formatted_traces.append('Current trace for {} thread "{}":\n{}'
                                    .format(thread_type, thread.name, thread_stack_trace))

    return formatted_traces

Example 9

Project: CloudBot Source File: profiling.py
def get_thread_dump():
    code = []
    threads = [(get_name(thread_id), traceback.extract_stack(stack))
               for thread_id, stack in sys._current_frames().items()]
    for thread_name, stack in threads:
        code.append("# {}".format(thread_name))
        for filename, line_num, name, line in stack:
            code.append("{}:{} - {}".format(filename, line_num, name))
            if line:
                code.append("    {}".format(line.strip()))
        code.append("")  # new line
    return web.paste("\n".join(code), ext='txt')

Example 10

Project: lymph Source File: __init__.py
def get_threads_frames():
    threads = {thread.ident: thread.name for thread in threading.enumerate()}
    for ident, frame in sys._current_frames().items():
        name = threads.get(ident)
        if name:
            yield '%s:%s' % (ident, name), frame

Example 11

Project: music-player Source File: utils.py
def dumpAllThreads():
	import sys
	if not hasattr(sys, "_current_frames"):
		print "Warning: dumpAllThreads: no sys._current_frames"
		return

	import threading
	id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
	for threadId, stack in sys._current_frames().items():
		print("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId))
		better_exchook.print_traceback(stack)

Example 12

Project: python-flamegraph Source File: flamegraph.py
  def run(self):
    my_thread = threading.current_thread().ident
    while self._keeprunning:
      for thread_id, frame in sys._current_frames().items():
        if thread_id == my_thread:
          continue

        entry = create_flamegraph_entry(thread_id, frame, self._collapse_recursion)
        if self._filter is None or self._filter.search(entry):
          with self._lock:
            self._stats[entry] += 1

        self._stopevent.wait(self._interval)  # basically a sleep for x seconds unless someone asked to stop

    self._write_results()

Example 13

Project: openfda Source File: watchdog.py
Function: run
  def run(self):
    while True:
      try:
        self._queue.get(timeout=self._log_frequency)
        return
      except Queue.Empty:
        pass

      os.system('mkdir -p "%s"' % self._output_dir)
      with open(self._output_file(), 'w') as out_f:
        for threadid, frame in sys._current_frames().items():
          stack = ''.join(traceback.format_stack(frame))
          print >>out_f, 'Thread: %s' % threadid
          print >>out_f, stack
          print >>out_f, '\n\n'

Example 14

Project: FibbingNode Source File: utils.py
def dump_threads():
    """
    Shouldn't be used except for debugging purpose (e.g. find deadlocks)
    """
    import traceback

    log.error("*** STACKTRACE - START ***")
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# ThreadID: %s" % threadId)
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                                                        lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    for line in code:
        log.error(line.strip('\n'))
    log.error("*** STACKTRACE - END ***")

Example 15

Project: Flexget Source File: server.py
    @api.response(200, description='Flexget threads dump', model=dump_threads_schema)
    def get(self, session=None):
        """ Dump Server threads for debugging """
        id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
        threads = []
        for threadId, stack in sys._current_frames().items():
            dump = []
            for filename, lineno, name, line in traceback.extract_stack(stack):
                dump.append('File: "%s", line %d, in %s' % (filename, lineno, name))
                if line:
                    dump.append(line.strip())
            threads.append({
                'name': id2name.get(threadId),
                'id': threadId,
                'dump': dump
            })

        return jsonify(threads=threads)

Example 16

Project: gae-init-debug Source File: sampling_profiler.py
Function: take_sample
    def take_sample(self, sample_number, force_memory=False):
        timestamp_ms = (self.time_fxn() - self.start_time) * 1000
        # Look at stacks of all existing threads...
        # See http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/
        for thread_id, active_frame in sys._current_frames().items():
            # ...but only sample from the main request thread.
            # TODO(kamens): this profiler will need work if we ever
            # actually use multiple threads in a single request and want to
            # profile more than one of them.
            if thread_id == self.current_request_thread_id:
                # Grab a sample of this thread's current stack
                self.samples.append(ProfileSample.from_frame_and_timestamp(
                        active_frame, timestamp_ms))
        if self.memory_sample_every:
            if force_memory or sample_number % self.memory_sample_every == 0:
                self.memory_samples[timestamp_ms] = get_memory()

Example 17

Project: appdaemon Source File: stacktracer.py
def stacktraces():
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# ThreadID: %s" % threadId)
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
 
    return highlight("\n".join(code), PythonLexer(), HtmlFormatter(
      full=False,
      # style="native",
      noclasses=True,
    ))

Example 18

Project: moneyguru Source File: debug.py
Function: stacktraces
def stacktraces():
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# ThreadID: %s" % threadId)
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    
    return "\n".join(code)

Example 19

Project: python-manhole Source File: manhole.py
def dump_stacktraces():
    """
    Dumps thread ids and tracebacks to stdout.
    """
    lines = []
    for thread_id, stack in sys._current_frames().items():  # pylint: disable=W0212
        lines.append("\n######### ProcessID=%s, ThreadID=%s #########" % (
            os.getpid(), thread_id
        ))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            lines.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                lines.append("  %s" % (line.strip()))
    lines.append("#############################################\n\n")

    print('\n'.join(lines), file=sys.stderr if _MANHOLE.redirect_stderr else sys.stdout)

Example 20

Project: twitinfo Source File: index_tweets.py
def dumpstacks(signal, frame):
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name[threadId], threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    print "\n".join(code)

Example 21

Project: MAVProxy Source File: dumpstacks.py
def dumpstacks(signal, frame):
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId, ""), threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    print("\n".join(code))

Example 22

Project: checkgoogleip Source File: checkip.py
Function: dump_stacks
def dumpstacks():
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %d" % (threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    PRINT("\n".join(code))

Example 23

Project: torngas Source File: gunicorn.conf.py
def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")

    ## get traceback info
    import threading, sys, traceback

    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId, ""),
                                            threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                                                        lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    worker.log.debug("\n".join(code))

Example 24

Project: scion Source File: stacktracer.py
def stacktraces():
    # First map thread IDs to thread names
    id_name = {}
    for thread in threading.enumerate():
        id_name[thread.ident] = thread.name
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# ThreadID: %s, Name: %s" %
                    (threadId, id_name.get(threadId, "UNKNOWN")))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
 
    return highlight("\n".join(code), PythonLexer(), HtmlFormatter(
      full=False,
      # style="native",
      noclasses=True,
    ))

Example 25

Project: nuxeo-drive Source File: commandline.py
Function: dump_stacks
def dumpstacks(signal, frame):
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for thread_id, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(thread_id, ""),
                                            thread_id))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s'
                        % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    print "\n".join(code)

Example 26

Project: offlineimap Source File: stacktrace.py
Function: dump
def dump(out):
	""" Dumps current stack trace into I/O object 'out' """
	id2name = {}
	for th in threading.enumerate():
		id2name[th.ident] = th.name
	n = 0
	for i, stack in sys._current_frames().items():
		out.write ("\n# Thread #%d (id=%d), %s\n" % \
		  (n, i, id2name[i]))
		n = n + 1
		for f, lno, name, line in traceback.extract_stack (stack):
			out.write ('File: "%s", line %d, in %s' % \
			  (f, lno, name))
			if line:
				out.write (" %s" % (line.strip()))
			out.write ("\n")

Example 27

Project: oslo.reports Source File: threading.py
    def __call__(self):
        threadModels = dict(
            (thread_id, tm.ThreadModel(thread_id, stack))
            for thread_id, stack in sys._current_frames().items()
        )

        if self.traceback is not None:
            curr_thread_id = threading.current_thread().ident
            threadModels[curr_thread_id] = tm.ThreadModel(curr_thread_id,
                                                          self.traceback)

        return mwdv.ModelWithDefaultViews(threadModels,
                                          text_view=text_views.MultiView())

Example 28

Project: rack Source File: threading.py
Function: call
    def __call__(self):
        threadModels = [
            tm.ThreadModel(thread_id, stack)
            for thread_id, stack in sys._current_frames().items()
        ]

        thread_pairs = dict(zip(range(len(threadModels)), threadModels))
        return mwdv.ModelWithDefaultViews(thread_pairs,
                                          text_view=text_views.MultiView())

Example 29

Project: nodepool Source File: nodepoold.py
def stack_dump_handler(signum, frame):
    signal.signal(signal.SIGUSR2, signal.SIG_IGN)
    log_str = ""
    threads = {}
    for t in threading.enumerate():
        threads[t.ident] = t
    for thread_id, stack_frame in sys._current_frames().items():
        thread = threads.get(thread_id)
        if thread:
            thread_name = thread.name
        else:
            thread_name = 'Unknown'
        label = '%s (%s)' % (thread_name, thread_id)
        log_str += "Thread: %s\n" % label
        log_str += "".join(traceback.format_stack(stack_frame))
    log = logging.getLogger("nodepool.stack_dump")
    log.debug(log_str)
    signal.signal(signal.SIGUSR2, stack_dump_handler)

Example 30

Project: bsd-cloudinit Source File: eventlet_backdoor.py
Function: print_nativethreads
def _print_nativethreads():
    for threadId, stack in sys._current_frames().items():
        print(threadId)
        traceback.print_stack(stack)
        print()

Example 31

Project: anaconda Source File: anaconda.py
    def dumpState(self):
        from meh import ExceptionInfo
        from meh.dump import ReverseExceptionDump
        from inspect import stack as _stack
        from traceback import format_stack

        # Skip the frames for dumpState and the signal handler.
        stack = _stack()[2:]
        stack.reverse()
        exn = ReverseExceptionDump(ExceptionInfo(None, None, stack),
                                   self.mehConfig)

        # gather up info on the running threads
        threads = "\nThreads\n-------\n"

        # Every call to sys._current_frames() returns a new dict, so it is not
        # modified when threads are created or destroyed. Iterating over it is
        # thread safe.
        for thread_id, frame in sys._current_frames().items():
            threads += "\nThread %s\n" % (thread_id,)
            threads += "".join(format_stack(frame))

        # dump to a unique file
        (fd, filename) = mkstemp(prefix="anaconda-tb-", dir="/tmp")
        dump_text = exn.traceback_and_object_dump(self)
        dump_text += threads
        dump_text_bytes = dump_text.encode("utf-8")
        os.write(fd, dump_text_bytes)
        os.close(fd)

        # append to a given file
        with open("/tmp/anaconda-tb-all.log", "a+") as f:
            f.write("--- traceback: %s ---\n" % filename)
            f.write(dump_text + "\n")

Example 32

Project: socketconsole Source File: socketconsole.py
    def stacktraces(self):
        code = []
        threads = dict(
            (t.ident, t) for t in threading.enumerate())
        for thread_id, stack in sys._current_frames().items():
            # Skip the current thread; we know what it's doing
            if thread_id == self.ident:
                continue
            try:
                name = threads[thread_id].name
            except KeyError:
                name = 'Unknown (see Python issue #5632)'
            code.append("\n# Thread Name: %s, ThreadID: %s\n" %
                (name, thread_id))
            code.extend(traceback.format_list(traceback.extract_stack(stack)))
            stack_locals = list(stack.f_locals.items())[:self.locals_limit]
            code.append("\n# Locals:\n")
            for i, (var, val) in enumerate(stack_locals):
                if i == self.locals_limit:
                    break
                code.append("  %s: %r\n" % (var, val))
        return code

Example 33

Project: RIDE Source File: debugconsole.py
def _print_stacks():
    id2name = dict((th.ident, th.name) for th in threading.enumerate())
    for threadId, stack in sys._current_frames().items():
        print(id2name[threadId])
        traceback.print_stack(f=stack)

Example 34

Project: pyspv Source File: network.py
    def join(self, *args, **kwargs):
        kwargs['timeout'] = 3
        for _, p in self.peers.items():
            p.join(*args, **kwargs)
            if p.is_alive():
                import sys
                print("*** STACKTRACE - START :: peer({}) ***".format(p.peer_address))
                code = []
                for thread_id, stack in sys._current_frames().items():
                    code.append("\n# Thread ID: {}".format(thread_id))
                    for filename, lineno, name, line in traceback.extract_stack(stack):
                        code.append('\nFile: "{}", line {}, in {}'.format(filename, lineno, name))
                        if line:
                            code.append("  {}".format(line.strip()))
                
                for line in code:
                    print(line, end='')

                print("\n*** STACKTRACE - END ***")
        threading.Thread.join(self, *args, **kwargs)

Example 35

Project: canto-curses Source File: main.py
    def sigusr1(self, a = None, b = None):
        import threading

        held_locks = {}
        code = {}
        curthreads = threading.enumerate()

        for threadId, stack in sys._current_frames().items():
            name = str(threadId)
            for ct in curthreads:
                if ct.ident == threadId:
                    name = ct.name

            code[name] = ["NAME: %s" % name]
            for filename, lineno, fname, line in traceback.extract_stack(stack):
                code[name].append('FILE: "%s", line %d, in %s' % (filename, lineno, fname))
                if line:
                    code[name].append("  %s" % (line.strip()))

            held_locks[name] = ""
            for lock in alllocks:
                if lock.writer_id == threadId:
                    held_locks[name] += ("%s(w)" % lock.name)
                    continue
                for reader_id, reader_stack in lock.reader_stacks:
                    if reader_id == threadId:
                        held_locks[name] += ("%s(r)" % lock.name)

        for k in code:
            log.info('\n\nLOCKS: %s \n%s' % (held_locks[k], '\n'.join(code[k])))

        log.info("\n\nSTACKS:")
        for lock in alllocks:
            for (reader_id, reader_stack) in lock.reader_stacks:
                log.info("Lock %s (%s readers)" % (lock.name, lock.readers))
                log.info("Lock reader (thread %s):" % (reader_id,))
                log.info(''.join(reader_stack))

            for writer_stack in lock.writer_stacks:
                log.info("Lock %s (%s readers)" % (lock.name, lock.readers))
                log.info("Lock writer (thread %s):" % (lock.writer_id,))
                log.info(''.join(writer_stack))

        log.info("VARS: %s" % config.vars)
        log.info("OPTS: %s" % config.config)

Example 36

Project: canto-next Source File: canto_backend.py
    def sig_usr(self, a, b):
        import threading
        import gc

        held_locks = {}
        code = {}
        curthreads = threading.enumerate()

        for threadId, stack in sys._current_frames().items():
            name = str(threadId)
            for ct in curthreads:
                if ct.ident == threadId:
                    name = ct.name

            code[name] = ["NAME: %s" % name]
            for filename, lineno, fname, line in traceback.extract_stack(stack):
                code[name].append('FILE: "%s", line %d, in %s' % (filename, lineno, fname))
                if line:
                    code[name].append("  %s" % (line.strip()))

            held_locks[name] = ""
            for lock in alllocks:
                if lock.writer_id == threadId:
                    held_locks[name] += ("%s(w)" % lock.name)
                    continue
                for reader_id, reader_stack in lock.reader_stacks:
                    if reader_id == threadId:
                        held_locks[name] += ("%s(r)" % lock.name)

        for k in code:
            log.info('\n\nLOCKS: %s \n%s' % (held_locks[k], '\n'.join(code[k])))

        log.info("\n\nSTACKS:")
        for lock in alllocks:
            for (reader_id, reader_stack) in lock.reader_stacks:
                log.info("Lock %s (%s readers)" % (lock.name, lock.readers))
                log.info("Lock reader (thread %s):" % (reader_id,))
                log.info(''.join(reader_stack))

            for writer_stack in lock.writer_stacks:
                log.info("Lock %s (%s readers)" % (lock.name, lock.readers))
                log.info("Lock writer (thread %s):" % (lock.writer_id,))
                log.info(''.join(writer_stack))

        self.shelf.sync()
        gc.collect()

        # If we've got pympler installed, output a summary of memory usage.

        try:
            from pympler import summary, muppy
            summary.print_(summary.summarize(muppy.get_objects()))
        except:
            pass

Example 37

Project: NimLime Source File: error_handler.py
Function: handle_error
def _handle_error():
    global notified_user, error_msg, critical_error_msg
    try:
        with open(logfile_path, 'a+') as logfile:
            logfile.write(strftime("\n\n%Y-%m-%d: %H:%M:%S\n"))
            logfile.write("[Stack frames]\n")
            for thread_id, stack_frame in sys._current_frames().items():
                logfile.write("[Thread {0}]\n".format(thread_id))
                traceback.print_stack(stack_frame, file=logfile)
            logfile.write("Main ")
            traceback.print_exc(file=logfile)
        message = error_msg
    except Exception:
        print("\nError handler exception:")
        traceback.print_exc()
        message = critical_error_msg

    if not notified_user:
        sublime.error_message(message.format(logfile_path))
        notified_user = True
    sublime.status_message("NimLime has encountered an error.")

Example 38

Project: ALF Source File: local.py
Function: main
def main(proj_name, project_inst, run_folder, template_fn, iters, aggr_min, aggr_max,
         keep, timeout, write_pickle, reduce, reduce_n):
    "main loop"
    ext = os.path.splitext(os.path.basename(template_fn))[1]
    results = 0
    iterno = 0
    is_replay = (iters == 1 and aggr_min == 0 and aggr_max == 0)
    log.info("Running project %s for %d iteration(s).", proj_name, iters)
    log.info("Results will be written to %s", run_folder)
    log.info("Iteration timeout: %r", timeout)
    log.info("Ctrl+C to quit")
    log.info("%-20s %-10s %s", "Iterations", "Rate", "Failures")
    start_time = time.time()
    print_time = start_time
    done = False
    if timeout is not None:
        timeout_event = threading.Event()
        timeout_continue = threading.Event()
        class TimeoutThread(threading.Thread):
            def run(self):
                while not done:
                    if timeout_event.wait(timeout) is False:
                        # dump thread stacks and exit
                        log.error("Iteration timeout occurred!")
                        for thread_id, stack in sys._current_frames().items():
                            if thread_id == self.ident:
                                continue
                            log.error("Thread: %d", thread_id)
                            traceback.print_stack(stack)
                            log.error("")
                        _thread.interrupt_main()
                        return
                    timeout_event.clear()
                    timeout_continue.set()
        tout_tid = TimeoutThread()
        tout_tid.start()
    try:
        while not iters or iterno < iters:
            printed = False
            # create mutation fn
            if is_replay:
                mutation_fn = os.path.basename(template_fn)
            else:
                mutation_fn = "mutation_%08X%s" % (iterno, ext)
            # do an iteration
            iter_had_result = False
            cls = ""
            result = project_inst.do_iteration(mutation_fn, random.randint(aggr_max, aggr_min))

            if result is not None:
                if not isinstance(result, FuzzResult):
                    raise TypeError("Expecting FuzzResult, not %s" % type(result))
                iter_had_result = True
                cls = result.classification
                if result.classification != "NOT_AN_EXCEPTION":
                    if not os.path.isfile(mutation_fn):
                        raise Exception("result reported before mutation written to disk")
                    results += 1
                if keep or result.classification != "NOT_AN_EXCEPTION":
                    if is_replay:
                        log_fn = "%s.log.xml" % os.path.basename(template_fn)
                        pkl_fn = "%s.pkl" % os.path.basename(template_fn)
                    else:
                        log_fn = "mutation_%08X.log.xml" % (iterno)
                        pkl_fn = "mutation_%08X.pkl" % iterno
                    with open(log_fn, "w") as logf:
                        logf.write("<log>\n")
                        logf.write("<classification>%s</classification>\n" % result.classification)
                        logf.write("<backtrace>\n")
                        for lso in result.backtrace:
                            logf.write("<sym>%s</sym>\n" % lso)
                        logf.write("</backtrace>\n")
                        logf.write("<text>\n")
                        logf.write(result.text)
                        logf.write("</text>\n")
                        logf.write("</log>\n")
                    if write_pickle:
                        with open(pkl_fn, "wb") as f:
                            pickle.dump(result, f)
                    if reduce:
                        with open(template_fn, "rb") as f:
                            mutation = f.read()
                        for r in reduce:
                            mutation = _reduce(project_inst, r, reduce_n,
                                               mutation, mutation_fn, result)
                        oresult = result
                        with open(mutation_fn, "wb") as f:
                            f.write(mutation)
                        result = project_inst.run_subject(mutation_fn)
                        if not project_inst.resultmatch(oresult, result):
                            raise Exception("Result didn't match post-reduce")
            elif reduce:
                log.warning("--reduce specified, but no failure was found")
            # remove the mutation if it didn't cause failure
            if not keep and (not iter_had_result or cls == "NOT_AN_EXCEPTION" or is_replay):
                delete(mutation_fn)
            iterno += 1
            if time.time() - print_time >= 10:
                print_time = time.time()
                print_progress(start_time, iterno, results)
                printed = True
            if timeout is not None:
                timeout_event.set()
                timeout_continue.wait()
            do_deletes()
    except KeyboardInterrupt:
        log.info("User interrupt")
    finally:
        if not printed:
            print_progress(start_time, iterno, results)
        elapsed_time = time.time() - start_time
        log.info("Ran %d iterations and found %d results in %.2fs", iterno, results, elapsed_time)
        project_inst.cleanup()
        project_inst.finish()
        done = True
        if timeout is not None:
            timeout_event.set()
        do_deletes()

Example 39

Project: cgstudiomap Source File: misc.py
def dumpstacks(sig=None, frame=None):
    """ Signal handler: dump a stack trace for each existing thread."""
    code = []

    def extract_stack(stack):
        for filename, lineno, name, line in traceback.extract_stack(stack):
            yield 'File: "%s", line %d, in %s' % (filename, lineno, name)
            if line:
                yield "  %s" % (line.strip(),)

    # code from http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application#answer-2569696
    # modified for python 2.5 compatibility
    threads_info = dict([(th.ident, {'name': th.name, 'uid': getattr(th, 'uid', 'n/a')})
                        for th in threading.enumerate()])
    for threadId, stack in sys._current_frames().items():
        thread_info = threads_info.get(threadId)
        code.append("\n# Thread: %s (id:%s) (uid:%s)" %
                    (thread_info and thread_info['name'] or 'n/a',
                     threadId,
                     thread_info and thread_info['uid'] or 'n/a'))
        for line in extract_stack(stack):
            code.append(line)

    if openerp.evented:
        # code from http://stackoverflow.com/questions/12510648/in-gevent-how-can-i-dump-stack-traces-of-all-running-greenlets
        import gc
        from greenlet import greenlet
        for ob in gc.get_objects():
            if not isinstance(ob, greenlet) or not ob:
                continue
            code.append("\n# Greenlet: %r" % (ob,))
            for line in extract_stack(ob.gr_frame):
                code.append(line)

    _logger.info("\n".join(code))

Example 40

Project: ztq Source File: command_execute.py
def report(start_time):
    """ 转换器向服务器报告状态 """
    cpu_style = get_cpu_style()
    cpu_percent = get_cpu_usage() 
    mem_percent, mem_total = get_mem_usage()
    ip = CONFIG['server']['alias']

    traceback_dict = {}
    for thread_id, frame in sys._current_frames().items():
        traceback_dict[thread_id] = traceback.format_stack(frame)

    # 向服务器报告
    return dict( ip=ip,
            cpu_style=cpu_style, 
            cpu_percent=cpu_percent, 
            mem_total=mem_total, 
            mem_percent=mem_percent, 
            started=start_time, 
            timestamp=int(time.time()),
            traceback=traceback_dict,
            )

Example 41

Project: PyDev.Debugger Source File: runfiles.py
Function: run
            def run(self):
                time.sleep(10)

                thread_id_to_name = {}
                try:
                    for t in threading.enumerate():
                        thread_id_to_name[t.ident] = '%s  (daemon: %s)' % (t.name, t.daemon)
                except:
                    pass

                stack_trace = [
                    '===============================================================================',
                    'pydev pyunit runner: Threads still found running after tests finished',
                    '================================= Thread Dump =================================']

                for thread_id, stack in sys._current_frames().items():
                    stack_trace.append('\n-------------------------------------------------------------------------------')
                    stack_trace.append(" Thread %s" % thread_id_to_name.get(thread_id, thread_id))
                    stack_trace.append('')

                    if 'self' in stack.f_locals:
                        sys.stderr.write(str(stack.f_locals['self']) + '\n')

                    for filename, lineno, name, line in traceback.extract_stack(stack):
                        stack_trace.append(' File "%s", line %d, in %s' % (filename, lineno, name))
                        if line:
                            stack_trace.append("   %s" % (line.strip()))
                stack_trace.append('\n=============================== END Thread Dump ===============================')
                sys.stderr.write('\n'.join(stack_trace))

Example 42

Project: ganga Source File: stacktracer.py
def stacktraces():
    # type: () -> str
    """
    Based on http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/

    Returns:
        str: HTML code to be saved to a file
    """
    html = [
        '<!doctype html>',
        '<html lang="en">',
        '<head>',
        '<meta charset=utf-8>',
        '<title>Current Ganga Threads</title>',
        '</head>'
        '<body>'
        '<h1>Current Ganga Threads</h1>',
    ]
    for thread_id, stack in sys._current_frames().items():
        name = dict((t.ident, t.name) for t in threading.enumerate())
        title = '<h2>{0}</h2>'.format(name.get(thread_id, None))
        html.append(title)

        trace = traceback.format_stack(stack)
        html.append(highlight_trace(''.join(trace)))

    html.append('<small>' + datetime.utcnow().isoformat() + '</small>')
    html.append('</body>')
    html.append('</html>')

    return '\n'.join(html)

Example 43

Project: ispyd Source File: python.py
Function: do_threads
    def do_threads(self, line): 
        """threads
	Display stack trace dumps for all threads currently executing
	within the Python interpreter.
        
        Note that if coroutines are being used, such as systems based
        on greenlets, then only the thread stack of the currently
        executing coroutine will be displayed."""

        all = [] 
        for threadId, stack in sys._current_frames().items():
            block = []
            block.append('# ThreadID: %s' % threadId) 
            thr = threading._active.get(threadId)
            if thr:
                block.append('# Name: %s' % thr.name) 
            for filename, lineno, name, line in traceback.extract_stack(
                stack): 
                block.append('File: \'%s\', line %d, in %s' % (filename,
                        lineno, name)) 
                if line:
                    block.append('  %s' % (line.strip()))
            all.append('\n'.join(block))

        print >> self.stdout, '\n\n'.join(all)

Example 44

Project: ispyd Source File: wsgi.py
    def do_requests(self, line):
        """requests
        Display details on any web requests currently being processed by
        the WSGI application."""

        output = StringIO.StringIO()

        frames = dict(sys._current_frames().items())
        transactions = dict(WSGITransaction.transactions)
        request_ids = sorted(transactions.keys())

        for i in range(len(request_ids)):
            request_id = request_ids[i]
            text = self.format_transaction(transactions[request_id], frames)
            if text:
                output.write(text)

        text = output.getvalue()
        if text:
            print >> self.stdout, text
        else:
            print >> self.stdout, 'No active transactions.'

Example 45

Project: pykka Source File: debug.py
def log_thread_tracebacks(*args, **kwargs):
    """Logs at ``CRITICAL`` level a traceback for each running thread.

    This can be a convenient tool for debugging deadlocks.

    The function accepts any arguments so that it can easily be used as e.g. a
    signal handler, but it does not use the arguments for anything.

    To use this function as a signal handler, setup logging with a
    :attr:`logging.CRITICAL` threshold or lower and make your main thread
    register this with the :mod:`signal` module::

        import logging
        import signal

        import pykka.debug

        logging.basicConfig(level=logging.DEBUG)
        signal.signal(signal.SIGUSR1, pykka.debug.log_thread_tracebacks)

    If your application deadlocks, send the `SIGUSR1` signal to the process::

        kill -SIGUSR1 <pid of your process>

    Signal handler caveats:

    - The function *must* be registered as a signal handler by your main
      thread. If not, :func:`signal.signal` will raise a :exc:`ValueError`.

    - All signals in Python are handled by the main thread. Thus, the signal
      will only be handled, and the tracebacks logged, if your main thread is
      available to do some work. Making your main thread idle using
      :func:`time.sleep` is OK. The signal will awaken your main thread.
      Blocking your main thread on e.g. :func:`Queue.Queue.get` or
      :meth:`pykka.Future.get` will break signal handling, and thus you won't
      be able to signal your process to print the thread tracebacks.

    The morale is: setup signals using your main thread, start your actors,
    then let your main thread relax for the rest of your application's life
    cycle.

    For a complete example of how to use this, see
    ``examples/deadlock_debugging.py`` in Pykka's source code.

    .. versionadded:: 1.1
    """

    thread_names = dict((t.ident, t.name) for t in threading.enumerate())

    for ident, frame in sys._current_frames().items():
        name = thread_names.get(ident, '?')
        stack = ''.join(traceback.format_stack(frame))
        logger.critical(
            'Current state of %s (ident: %s):\n%s', name, ident, stack)

Example 46

Project: rpqueue Source File: __init__.py
def _print_stackframes_on_signal(signum, frame):
    pid = os.getpid()
    for tid, frame in sys._current_frames().items():
        log_handler.critical('PID: %s THREAD: %s\n%s' % (pid, tid, ''.join(traceback.format_stack(frame))))

Example 47

Project: offlineimap Source File: init.py
Function: dump_stacks
    def __dumpstacks(self, context=1, sighandler_deep=2):
        """ Signal handler: dump a stack trace for each existing thread."""

        currentThreadId = threading.currentThread().ident

        def unique_count(l):
            d = collections.defaultdict(lambda: 0)
            for v in l:
                d[tuple(v)] += 1
            return list((k, v) for k, v in d.items())

        stack_displays = []
        for threadId, stack in sys._current_frames().items():
            stack_display = []
            for filename, lineno, name, line in traceback.extract_stack(stack):
                stack_display.append('  File: "%s", line %d, in %s'
                                     % (filename, lineno, name))
                if line:
                    stack_display.append("    %s" % (line.strip()))
            if currentThreadId == threadId:
                stack_display = stack_display[:- (sighandler_deep * 2)]
                stack_display.append('  => Stopped to handle current signal. ')
            stack_displays.append(stack_display)
        stacks = unique_count(stack_displays)
        self.ui.debug('thread', "** Thread List:\n")
        for stack, times in stacks:
            if times == 1:
                msg = "%s Thread is at:\n%s\n"
            else:
                msg = "%s Threads are at:\n%s\n"
            self.ui.debug('thread', msg % (times, '\n'.join(stack[- (context * 2):])))

        self.ui.debug('thread', "Dumped a total of %d Threads." %
                      len(sys._current_frames().keys()))

Example 48

Project: zuul Source File: __init__.py
def stack_dump_handler(signum, frame):
    signal.signal(signal.SIGUSR2, signal.SIG_IGN)
    log_str = ""
    for thread_id, stack_frame in sys._current_frames().items():
        log_str += "Thread: %s\n" % thread_id
        log_str += "".join(traceback.format_stack(stack_frame))
    log = logging.getLogger("zuul.stack_dump")
    log.debug(log_str)
    if yappi:
        if not yappi.is_running():
            yappi.start()
        else:
            yappi.stop()
            yappi_out = six.BytesIO()
            yappi.get_func_stats().print_all(out=yappi_out)
            yappi.get_thread_stats().print_all(out=yappi_out)
            log.debug(yappi_out.getvalue())
            yappi_out.close()
            yappi.clear_stats()
    signal.signal(signal.SIGUSR2, stack_dump_handler)

Example 49

Project: support Source File: meta_service.py
Function: get_thread_stacks
def get_thread_stacks():
    return dict([(k, traceback.format_stack(v))
                 for k, v in sys._current_frames().items()])