|
| 1 | +import pdb |
| 2 | +import socket |
| 3 | +import threading |
| 4 | +import sys |
| 5 | +import traceback |
| 6 | +from .debug_dumpling import load_dumpling, mock_inspect |
| 7 | + |
| 8 | +DEFAULT_ADDR = "127.0.0.1" |
| 9 | +DEFAULT_PORT = 4444 |
| 10 | + |
| 11 | + |
| 12 | +class FileObjectWrapper(object): |
| 13 | + def __init__(self, fileobject, stdio): |
| 14 | + self._obj = fileobject |
| 15 | + self._io = stdio |
| 16 | + |
| 17 | + def __getattr__(self, attr): |
| 18 | + if hasattr(self._obj, attr): |
| 19 | + attr = getattr(self._obj, attr) |
| 20 | + elif hasattr(self._io, attr): |
| 21 | + attr = getattr(self._io, attr) |
| 22 | + else: |
| 23 | + raise AttributeError("Attribute %s is not found" % attr) |
| 24 | + return attr |
| 25 | + |
| 26 | + |
| 27 | +class Rpdb(pdb.Pdb): |
| 28 | + |
| 29 | + def __init__(self, addr=DEFAULT_ADDR, port=DEFAULT_PORT): |
| 30 | + """Initialize the socket and initialize pdb.""" |
| 31 | + |
| 32 | + # Backup stdin and stdout before replacing them by the socket handle |
| 33 | + self.old_stdout = sys.stdout |
| 34 | + self.old_stdin = sys.stdin |
| 35 | + self.port = port |
| 36 | + |
| 37 | + # Open a 'reusable' socket to let the webapp reload on the same port |
| 38 | + self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 | + self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) |
| 40 | + self.skt.bind((addr, port)) |
| 41 | + self.skt.listen(1) |
| 42 | + |
| 43 | + # Writes to stdout are forbidden in mod_wsgi environments |
| 44 | + try: |
| 45 | + sys.stderr.write("pdb is running on %s:%d\n" |
| 46 | + % self.skt.getsockname()) |
| 47 | + except IOError: |
| 48 | + pass |
| 49 | + |
| 50 | + (clientsocket, address) = self.skt.accept() |
| 51 | + handle = clientsocket.makefile('rw') |
| 52 | + pdb.Pdb.__init__(self, completekey='tab', |
| 53 | + stdin=FileObjectWrapper(handle, self.old_stdin), |
| 54 | + stdout=FileObjectWrapper(handle, self.old_stdin)) |
| 55 | + sys.stdout = sys.stdin = handle |
| 56 | + self.handle = handle |
| 57 | + OCCUPIED.claim(port, sys.stdout) |
| 58 | + |
| 59 | + def shutdown(self): |
| 60 | + """Revert stdin and stdout, close the socket.""" |
| 61 | + sys.stdout = self.old_stdout |
| 62 | + sys.stdin = self.old_stdin |
| 63 | + self.handle.close() |
| 64 | + OCCUPIED.unclaim(self.port) |
| 65 | + self.skt.shutdown(socket.SHUT_RDWR) |
| 66 | + self.skt.close() |
| 67 | + |
| 68 | + def do_continue(self, arg): |
| 69 | + """Clean-up and do underlying continue.""" |
| 70 | + try: |
| 71 | + return pdb.Pdb.do_continue(self, arg) |
| 72 | + finally: |
| 73 | + self.shutdown() |
| 74 | + |
| 75 | + do_c = do_cont = do_continue |
| 76 | + |
| 77 | + def do_quit(self, arg): |
| 78 | + """Clean-up and do underlying quit.""" |
| 79 | + try: |
| 80 | + return pdb.Pdb.do_quit(self, arg) |
| 81 | + finally: |
| 82 | + self.shutdown() |
| 83 | + |
| 84 | + do_q = do_exit = do_quit |
| 85 | + |
| 86 | + def do_EOF(self, arg): |
| 87 | + """Clean-up and do underlying EOF.""" |
| 88 | + try: |
| 89 | + return pdb.Pdb.do_EOF(self, arg) |
| 90 | + finally: |
| 91 | + self.shutdown() |
| 92 | + |
| 93 | + |
| 94 | +class OccupiedPorts(object): |
| 95 | + """Maintain rpdb port versus stdin/out file handles. |
| 96 | +
|
| 97 | + Provides the means to determine whether or not a collision binding to a |
| 98 | + particular port is with an already operating rpdb session. |
| 99 | +
|
| 100 | + Determination is according to whether a file handle is equal to what is |
| 101 | + registered against the specified port. |
| 102 | + """ |
| 103 | + |
| 104 | + def __init__(self): |
| 105 | + self.lock = threading.RLock() |
| 106 | + self.claims = {} |
| 107 | + |
| 108 | + def claim(self, port, handle): |
| 109 | + self.lock.acquire(True) |
| 110 | + self.claims[port] = id(handle) |
| 111 | + self.lock.release() |
| 112 | + |
| 113 | + def is_claimed(self, port, handle): |
| 114 | + self.lock.acquire(True) |
| 115 | + got = (self.claims.get(port) == id(handle)) |
| 116 | + self.lock.release() |
| 117 | + return got |
| 118 | + |
| 119 | + def unclaim(self, port): |
| 120 | + self.lock.acquire(True) |
| 121 | + del self.claims[port] |
| 122 | + self.lock.release() |
| 123 | + |
| 124 | +# {port: sys.stdout} pairs to track recursive rpdb invocation on same port. |
| 125 | +# This scheme doesn't interfere with recursive invocations on separate ports - |
| 126 | +# useful, eg, for concurrently debugging separate threads. |
| 127 | +OCCUPIED = OccupiedPorts() |
| 128 | + |
| 129 | + |
| 130 | +def r_post_mortem(dump_file, addr=DEFAULT_ADDR, port=DEFAULT_PORT): |
| 131 | + mock_inspect() |
| 132 | + dumpling = load_dumpling(dump_file) |
| 133 | + tb = dumpling["traceback"] |
| 134 | + debugger = Rpdb(addr=addr, port=port) |
| 135 | + debugger.reset() |
| 136 | + debugger.interaction(None, tb) |
0 commit comments