File: debug.py

package info (click to toggle)
python-mitogen 0.3.25~a2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,220 kB
  • sloc: python: 21,989; sh: 183; makefile: 74; perl: 19; ansic: 18
file content (236 lines) | stat: -rw-r--r-- 6,699 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Copyright 2019, David Wilson
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# !mitogen: minify_safe

"""
Basic signal handler for dumping thread stacks.
"""

import difflib
import logging
import os
import gc
import signal
import sys
import threading
import time
import traceback

import mitogen.core
import mitogen.parent


LOG = logging.getLogger(__name__)
_last = None


def enable_evil_interrupts():
    signal.signal(signal.SIGALRM, (lambda a, b: None))
    signal.setitimer(signal.ITIMER_REAL, 0.01, 0.01)


def disable_evil_interrupts():
    signal.setitimer(signal.ITIMER_REAL, 0, 0)


def _hex(n):
    return '%08x' % n


def get_subclasses(klass):
    """
    Rather than statically import every interesting subclass, forcing it all to
    be transferred and potentially disrupting the debugged environment,
    enumerate only those loaded in memory. Also returns the original class.
    """
    stack = [klass]
    seen = set()
    while stack:
        klass = stack.pop()
        seen.add(klass)
        stack.extend(klass.__subclasses__())
    return seen


def get_routers():
    return dict(
        (_hex(id(router)), router)
        for klass in get_subclasses(mitogen.core.Router)
        for router in gc.get_referrers(klass)
        if isinstance(router, mitogen.core.Router)
    )


def get_router_info():
    return {
        'routers': dict(
            (id_, {
                'id': id_,
                'streams': len(set(router._stream_by_id.values())),
                'contexts': len(set(router._context_by_id.values())),
                'handles': len(router._handle_map),
            })
            for id_, router in get_routers().items()
        )
    }


def get_stream_info(router_id):
    router = get_routers().get(router_id)
    return {
        'streams': dict(
            (_hex(id(stream)), ({
                'name': stream.name,
                'remote_id': stream.remote_id,
                'sent_module_count': len(getattr(stream, 'sent_modules', [])),
                'routes': sorted(getattr(stream, 'routes', [])),
                'type': type(stream).__module__,
            }))
            for via_id, stream in router._stream_by_id.items()
        )
    }


def format_stacks():
    name_by_id = dict(
        (t.ident, t.name)
        for t in threading.enumerate()
    )

    l = ['', '']
    for threadId, stack in sys._current_frames().items():
        l += ["# PID %d ThreadID: (%s) %s; %r" % (
            os.getpid(),
            name_by_id.get(threadId, '<no name>'),
            threadId,
            stack,
        )]
        #stack = stack.f_back.f_back

        for filename, lineno, name, line in traceback.extract_stack(stack):
            l += [
                'File: "%s", line %d, in %s' % (
                    filename,
                    lineno,
                    name
                )
            ]
            if line:
                l += ['    ' + line.strip()]
        l += ['']

    l += ['', '']
    return '\n'.join(l)


def get_snapshot():
    global _last

    s = format_stacks()
    snap = s
    if _last:
        snap += '\n'
        diff = list(difflib.unified_diff(
            a=_last.splitlines(),
            b=s.splitlines(),
            fromfile='then',
            tofile='now'
        ))

        if diff:
            snap += '\n'.join(diff) + '\n'
        else:
            snap += '(no change since last time)\n'
    _last = s
    return snap


def _handler(*_):
    fp = open('/dev/tty', 'w', 1)
    fp.write(get_snapshot())
    fp.close()


def install_handler():
    signal.signal(signal.SIGUSR2, _handler)


def _logging_main(secs):
    while True:
        time.sleep(secs)
        LOG.info('PERIODIC THREAD DUMP\n\n%s', get_snapshot())


def dump_to_logger(secs=5):
    th = threading.Thread(
        target=_logging_main,
        kwargs={'secs': secs},
        name='mitogen.debug.dump_to_logger',
    )
    th.setDaemon(True)
    th.start()


class ContextDebugger(object):
    @classmethod
    @mitogen.core.takes_econtext
    def _configure_context(cls, econtext):
        mitogen.parent.upgrade_router(econtext)
        econtext.debugger = cls(econtext.router)

    def __init__(self, router):
        self.router = router
        self.router.add_handler(
            func=self._on_debug_msg,
            handle=mitogen.core.DEBUG,
            persist=True,
            policy=mitogen.core.has_parent_authority,
        )
        mitogen.core.listen(router, 'register', self._on_stream_register)
        LOG.debug('Context debugging configured.')

    def _on_stream_register(self, context, stream):
        LOG.debug('_on_stream_register: sending configure() to %r', stream)
        context.call_async(ContextDebugger._configure_context)

    def _on_debug_msg(self, msg):
        if msg != mitogen.core._DEAD:
            threading.Thread(
                target=self._handle_debug_msg,
                name='ContextDebuggerHandler',
                args=(msg,)
            ).start()

    def _handle_debug_msg(self, msg):
        try:
            method, args, kwargs = msg.unpickle()
            msg.reply(getattr(self, method)(*args, **kwargs))
        except Exception:
            e = sys.exc_info()[1]
            msg.reply(mitogen.core.CallError(e))