codekingpro/portable-devtools
114k
1"""The debug module contains utilities and functions for better2debugging Eventlet-powered applications."""3from __future__ import print_function4 5import os6import sys7import linecache8import re9import inspect10 11__all__ = ['spew', 'unspew', 'format_hub_listeners', 'format_hub_timers',12 'hub_listener_stacks', 'hub_exceptions', 'tpool_exceptions',13 'hub_prevent_multiple_readers', 'hub_timer_stacks',14 'hub_blocking_detection']15 16_token_splitter = re.compile('\W+')17 18 19class Spew(object):20 21 def __init__(self, trace_names=None, show_values=True):22 self.trace_names = trace_names23 self.show_values = show_values24 25 def __call__(self, frame, event, arg):26 if event == 'line':27 lineno = frame.f_lineno28 if '__file__' in frame.f_globals:29 filename = frame.f_globals['__file__']30 if (filename.endswith('.pyc') or31 filename.endswith('.pyo')):32 filename = filename[:-1]33 name = frame.f_globals['__name__']34 line = linecache.getline(filename, lineno)35 else:36 name = '[unknown]'37 try:38 src = inspect.getsourcelines(frame)39 line = src[lineno]40 except IOError:41 line = 'Unknown code named [%s]. VM instruction #%d' % (42 frame.f_code.co_name, frame.f_lasti)43 if self.trace_names is None or name in self.trace_names:44 print('%s:%s: %s' % (name, lineno, line.rstrip()))45 if not self.show_values:46 return self47 details = []48 tokens = _token_splitter.split(line)49 for tok in tokens:50 if tok in frame.f_globals:51 details.append('%s=%r' % (tok, frame.f_globals[tok]))52 if tok in frame.f_locals:53 details.append('%s=%r' % (tok, frame.f_locals[tok]))54 if details:55 print("\t%s" % ' '.join(details))56 return self57 58 59def spew(trace_names=None, show_values=False):60 """Install a trace hook which writes incredibly detailed logs61 about what code is being executed to stdout.62 """63 sys.settrace(Spew(trace_names, show_values))64 65 66def unspew():67 """Remove the trace hook installed by spew.68 """69 sys.settrace(None)70 71 72def format_hub_listeners():73 """ Returns a formatted string of the current listeners on the current74 hub. This can be useful in determining what's going on in the event system,75 especially when used in conjunction with :func:`hub_listener_stacks`.76 """77 from eventlet import hubs78 hub = hubs.get_hub()79 result = ['READERS:']80 for l in hub.get_readers():81 result.append(repr(l))82 result.append('WRITERS:')83 for l in hub.get_writers():84 result.append(repr(l))85 return os.linesep.join(result)86 87 88def format_hub_timers():89 """ Returns a formatted string of the current timers on the current90 hub. This can be useful in determining what's going on in the event system,91 especially when used in conjunction with :func:`hub_timer_stacks`.92 """93 from eventlet import hubs94 hub = hubs.get_hub()95 result = ['TIMERS:']96 for l in hub.timers:97 result.append(repr(l))98 return os.linesep.join(result)99 100 101def hub_listener_stacks(state=False):102 """Toggles whether or not the hub records the stack when clients register103 listeners on file descriptors. This can be useful when trying to figure104 out what the hub is up to at any given moment. To inspect the stacks105 of the current listeners, call :func:`format_hub_listeners` at critical106 junctures in the application logic.107 """108 from eventlet import hubs109 hubs.get_hub().set_debug_listeners(state)110 111 112def hub_timer_stacks(state=False):113 """Toggles whether or not the hub records the stack when timers are set.114 To inspect the stacks of the current timers, call :func:`format_hub_timers`115 at critical junctures in the application logic.116 """117 from eventlet.hubs import timer118 timer._g_debug = state119 120 121def hub_prevent_multiple_readers(state=True):122 """Toggle prevention of multiple greenlets reading from a socket123 124 When multiple greenlets read from the same socket it is often hard125 to predict which greenlet will receive what data. To achieve126 resource sharing consider using ``eventlet.pools.Pool`` instead.127 128 But if you really know what you are doing you can change the state129 to ``False`` to stop the hub from protecting against this mistake.130 """131 from eventlet.hubs import hub132 hub.g_prevent_multiple_readers = state133 134 135def hub_exceptions(state=True):136 """Toggles whether the hub prints exceptions that are raised from its137 timers. This can be useful to see how greenthreads are terminating.138 """139 from eventlet import hubs140 hubs.get_hub().set_timer_exceptions(state)141 from eventlet import greenpool142 greenpool.DEBUG = state143 144 145def tpool_exceptions(state=False):146 """Toggles whether tpool itself prints exceptions that are raised from147 functions that are executed in it, in addition to raising them like148 it normally does."""149 from eventlet import tpool150 tpool.QUIET = not state151 152 153def hub_blocking_detection(state=False, resolution=1):154 """Toggles whether Eventlet makes an effort to detect blocking155 behavior in an application.156 157 It does this by telling the kernel to raise a SIGALARM after a158 short timeout, and clearing the timeout every time the hub159 greenlet is resumed. Therefore, any code that runs for a long160 time without yielding to the hub will get interrupted by the161 blocking detector (don't use it in production!).162 163 The *resolution* argument governs how long the SIGALARM timeout164 waits in seconds. The implementation uses :func:`signal.setitimer`165 and can be specified as a floating-point value.166 The shorter the resolution, the greater the chance of false167 positives.168 """169 from eventlet import hubs170 assert resolution > 0171 hubs.get_hub().debug_blocking = state172 hubs.get_hub().debug_blocking_resolution = resolution173 if not state:174 hubs.get_hub().block_detect_post()175 