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.

71 Examples 7

Example 1

Project: python-manhole
Source File: manhole.py
View license
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 2

Project: MAVProxy
Source File: dumpstacks.py
View license
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 3

Project: pyqtgraph
Source File: debug.py
View license
    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 4

Project: ants
Source File: debug.py
View license
    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
View license
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: MAVProxy
Source File: dumpstacks.py
View license
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 7

Project: twitinfo
Source File: index_tweets.py
View license
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 8

Project: nuxeo-drive
Source File: commandline.py
View license
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 9

Project: checkgoogleip
Source File: checkip.py
View license
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 10

Project: python2-trepan
Source File: thred.py
View license
    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 11

Project: sarge
Source File: stack_tracer.py
View license
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 12

Project: openfda
Source File: watchdog.py
View license
  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 13

Project: twitinfo
Source File: stream_tweets.py
View license
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 14

Project: moneyguru
Source File: debug.py
View license
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 15

Project: lymph
Source File: __init__.py
View license
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 16

Project: ClusterRunner
Source File: app_info.py
View license
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 17

Project: offlineimap
Source File: stacktrace.py
View license
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 18

Project: twitinfo
Source File: index_tweets.py
View license
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 19

Project: scrapy
Source File: debug.py
View license
    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 20

Project: checkgoogleip
Source File: checkip.py
View license
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 21

Project: scion
Source File: stacktracer.py
View license
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 22

Project: python-flamegraph
Source File: flamegraph.py
View license
  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 23

Project: scion
Source File: stacktracer.py
View license
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 24

Project: music-player
Source File: utils.py
View license
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 25

Project: torngas
Source File: gunicorn.conf.py
View license
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 26

Project: gae-init-debug
Source File: sampling_profiler.py
View license
    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 27

Project: FibbingNode
Source File: utils.py
View license
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 28

Project: CloudBot
Source File: profiling.py
View license
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 29

Project: python3-trepan
Source File: thred.py
View license
    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 30

Project: twitinfo
Source File: stream_tweets.py
View license
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 31

Project: appdaemon
Source File: stacktracer.py
View license
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 32

Project: linkchecker
Source File: __init__.py
View license
    @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 33

Project: pdfmasher
Source File: debug.py
View license
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 34

Project: nodepool
Source File: nodepoold.py
View license
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 35

Project: oslo.reports
Source File: threading.py
View license
    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 36

Project: rack
Source File: threading.py
View license
    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 37

Project: nuxeo-drive
Source File: commandline.py
View license
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 38

Project: torngas
Source File: gunicorn.conf.py
View license
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 39

Project: python3-trepan
Source File: test-lib-thread.py
View license
    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 40

Project: Flexget
Source File: server.py
View license
    @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 41

Project: ganga
Source File: stacktracer.py
View license
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 42

Project: ispyd
Source File: python.py
View license
    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 43

Project: ispyd
Source File: wsgi.py
View license
    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 44

Project: socketconsole
Source File: socketconsole.py
View license
    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 45

Project: pyspv
Source File: network.py
View license
    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 46

Project: ALF
Source File: local.py
View license
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 47

Project: pykka
Source File: debug.py
View license
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 48

Project: pykka
Source File: debug.py
View license
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 49

Project: cgstudiomap
Source File: misc.py
View license
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 50

Project: rpqueue
Source File: __init__.py
View license
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))))