def __init__(self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None): self.active = True self.out = out self._prev_handles = sys.stdin, sys.stdout self._sock, this_port = self.get_avail_port( host, port, port_search_limit) self._sock.setblocking(1) self._sock.listen(1) self.ident = '{0}:{1}'.format(self.me, this_port) self.host = host self.port = this_port self.say(BANNER.format(self=self)) self._client, address = self._sock.accept() self._client.setblocking(1) self.remote_addr = ':'.join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) self._handle = sys.stdin = sys.stdout = self._client.makefile('rwb', 0) import telnetlib as tn self._handle.write(tn.IAC + tn.WILL + tn.SGA) resp = self._handle.read(3) assert resp == tn.IAC + tn.DO + tn.SGA self._handle.write(tn.IAC + tn.WILL + tn.ECHO) resp = self._handle.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__(self, stdin=self._handle, stdout=self._handle, term_size=term_size)
def runscript(mainpyfile, args=None, pre_run="", steal_output=False): from pudb.debugger import Debugger dbg = Debugger(steal_output=steal_output) # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. The best approach would be to # have a "restart" command which would allow explicit specification of # command line arguments. import sys if args is not None: prev_sys_argv = sys.argv[:] sys.argv = [mainpyfile] + args # replace pudb's dir with script's dir in front of module search path. from os.path import dirname prev_sys_path = sys.path[:] sys.path[0] = dirname(mainpyfile) while True: if pre_run: from subprocess import call retcode = call(pre_run, close_fds=True, shell=True) if retcode: print "*** WARNING: pre-run process exited with code %d." % retcode raw_input("[Hit Enter]") status_msg = "" try: dbg._runscript(mainpyfile) except SystemExit, se: status_msg = "The debuggee exited normally with status code was %d.\n\n" % se.code except:
def __init__( self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None, reverse=False, ): """ :arg term_size: A two-tuple ``(columns, rows)``, or *None*. If *None*, try to determine the terminal size automatically. Currently, this uses a heuristic: It uses the terminal size of the debuggee as that for the debugger. The idea is that you might be running both in two tabs of the same terminal window, hence using terminals of the same size. """ self.out = out if term_size is None: try: s = struct.unpack("hh", fcntl.ioctl(1, termios.TIOCGWINSZ, "1234")) term_size = (s[1], s[0]) except Exception: term_size = (80, 24) self._prev_handles = sys.stdin, sys.stdout self._client, (address, port) = self.get_client( host=host, port=port, search_limit=port_search_limit, reverse=reverse ) self.remote_addr = ":".join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) # makefile ignores encoding if there's no buffering. raw_sock_file = self._client.makefile("rwb", 0) import codecs sock_file = codecs.StreamReaderWriter( raw_sock_file, codecs.getreader("utf-8"), codecs.getwriter("utf-8")) self._handle = sys.stdin = sys.stdout = sock_file # nc negotiation doesn't support telnet options if not reverse: import telnetlib as tn raw_sock_file.write(tn.IAC + tn.WILL + tn.SGA) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.SGA raw_sock_file.write(tn.IAC + tn.WILL + tn.ECHO) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__( self, stdin=self._handle, stdout=self._handle, term_size=term_size )
def __init__( self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None, reverse=False, ): self.active = True self.out = out self._prev_handles = sys.stdin, sys.stdout self._client, (address, port) = self.get_client(host=host, port=port, search_limit=port_search_limit, reverse=reverse) self.remote_addr = ":".join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) # makefile ignores encoding if there's no buffering. raw_sock_file = self._client.makefile("rwb", 0) import codecs if sys.version_info[0] < 3: sock_file = codecs.StreamRecoder( raw_sock_file, codecs.getencoder("utf-8"), codecs.getdecoder("utf-8"), codecs.getreader("utf-8"), codecs.getwriter("utf-8"), ) else: sock_file = codecs.StreamReaderWriter(raw_sock_file, codecs.getreader("utf-8"), codecs.getwriter("utf-8")) self._handle = sys.stdin = sys.stdout = sock_file # nc negotiation doesn't support telnet options if not reverse: import telnetlib as tn raw_sock_file.write(tn.IAC + tn.WILL + tn.SGA) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.SGA raw_sock_file.write(tn.IAC + tn.WILL + tn.ECHO) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__(self, stdin=self._handle, stdout=self._handle, term_size=term_size)
def __init__(self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None): self.active = True self.out = out self._prev_handles = sys.stdin, sys.stdout self._sock, this_port = self.get_avail_port(host, port, port_search_limit) self._sock.setblocking(1) self._sock.listen(1) self.ident = '{0}:{1}'.format(self.me, this_port) self.host = host self.port = this_port self.say(BANNER.format(self=self)) self._client, address = self._sock.accept() self._client.setblocking(1) self.remote_addr = ':'.join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) # makefile ignores encoding if there's no buffering. raw_sock_file = self._client.makefile("rwb", 0) import codecs if sys.version_info[0] < 3: sock_file = codecs.StreamRecoder(raw_sock_file, codecs.getencoder("utf-8"), codecs.getdecoder("utf-8"), codecs.getreader("utf-8"), codecs.getwriter("utf-8")) else: sock_file = codecs.StreamReaderWriter(raw_sock_file, codecs.getreader("utf-8"), codecs.getwriter("utf-8")) self._handle = sys.stdin = sys.stdout = sock_file import telnetlib as tn raw_sock_file.write(tn.IAC + tn.WILL + tn.SGA) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.SGA raw_sock_file.write(tn.IAC + tn.WILL + tn.ECHO) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__(self, stdin=self._handle, stdout=self._handle, term_size=term_size)
def __init__(self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None): self.active = True self.out = out self._prev_handles = sys.stdin, sys.stdout self._sock, this_port = self.get_avail_port( host, port, port_search_limit) self._sock.setblocking(1) self._sock.listen(1) self.ident = '{0}:{1}'.format(self.me, this_port) self.host = host self.port = this_port self.say(BANNER.format(self=self)) self._client, address = self._sock.accept() self._client.setblocking(1) self.remote_addr = ':'.join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) # makefile ignores encoding if there's no buffering. raw_sock_file = self._client.makefile("rwb", 0) import codecs if sys.version_info[0] < 3: sock_file = codecs.StreamRecoder( raw_sock_file, codecs.getencoder("utf-8"), codecs.getdecoder("utf-8"), codecs.getreader("utf-8"), codecs.getwriter("utf-8")) else: sock_file = codecs.StreamReaderWriter( raw_sock_file, codecs.getreader("utf-8"), codecs.getwriter("utf-8")) self._handle = sys.stdin = sys.stdout = sock_file import telnetlib as tn raw_sock_file.write(tn.IAC + tn.WILL + tn.SGA) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.SGA raw_sock_file.write(tn.IAC + tn.WILL + tn.ECHO) resp = raw_sock_file.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__(self, stdin=self._handle, stdout=self._handle, term_size=term_size)
def set_trace(paused=True, frame=None, term_size=None): """Set a breakpoint in a forked process on Unix system, e.g. Linux & MacOS. In- and output will be redirected to /dev/stdin & /dev/stdout. You can call pudb.forked.set_trace() directly or use it with python's built-in breakpoint(): PYTHONBREAKPOINT=pudb.forked.set_trace python … """ if frame is None: frame = sys._getframe().f_back if term_size is None: try: # Getting terminal size s = struct.unpack( "hh", fcntl.ioctl(1, termios.TIOCGWINSZ, "1234"), ) term_size = (s[1], s[0]) except Exception: term_size = (80, 24) Debugger( stdin=open("/dev/stdin"), stdout=open("/dev/stdout", "w"), term_size=term_size, ).set_trace(frame, paused=paused)
def _get_debugger(): if not CURRENT_DEBUGGER: from pudb.debugger import Debugger dbg = Debugger() CURRENT_DEBUGGER.append(dbg) return dbg else: return CURRENT_DEBUGGER[0]
def __init__(self, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, port_search_limit=100, out=sys.stdout, term_size=None): self.active = True self.out = out self._prev_handles = sys.stdin, sys.stdout self._sock, this_port = self.get_avail_port(host, port, port_search_limit) self._sock.setblocking(1) self._sock.listen(1) self.ident = '{0}:{1}'.format(self.me, this_port) self.host = host self.port = this_port self.say(BANNER.format(self=self)) self._client, address = self._sock.accept() self._client.setblocking(1) self.remote_addr = ':'.join(str(v) for v in address) self.say(SESSION_STARTED.format(self=self)) self._handle = sys.stdin = sys.stdout = self._client.makefile('rwb', 0) import telnetlib as tn self._handle.write(tn.IAC + tn.WILL + tn.SGA) resp = self._handle.read(3) assert resp == tn.IAC + tn.DO + tn.SGA self._handle.write(tn.IAC + tn.WILL + tn.ECHO) resp = self._handle.read(3) assert resp == tn.IAC + tn.DO + tn.ECHO Debugger.__init__(self, stdin=self._handle, stdout=self._handle, term_size=term_size)
def _get_debugger(**kwargs): if not CURRENT_DEBUGGER: tty_path = _tty_override() if tty_path and ("stdin" not in kwargs or "stdout" not in kwargs): tty_file, term_size = _open_tty(tty_path) kwargs.setdefault("stdin", tty_file) kwargs.setdefault("stdout", tty_file) kwargs.setdefault("term_size", term_size) from pudb.debugger import Debugger dbg = Debugger(**kwargs) CURRENT_DEBUGGER.append(dbg) return dbg else: return CURRENT_DEBUGGER[0]
def _get_debugger(**kwargs): from pudb.debugger import Debugger if not Debugger._current_debugger: tty_path = _tty_override() if tty_path and ("stdin" not in kwargs or "stdout" not in kwargs): tty_file, term_size = _open_tty(tty_path) kwargs.setdefault("stdin", tty_file) kwargs.setdefault("stdout", tty_file) kwargs.setdefault("term_size", term_size) from pudb.debugger import Debugger dbg = Debugger(**kwargs) return dbg else: return Debugger._current_debugger[0]
def _get_debugger(**kwargs): if not CURRENT_DEBUGGER: tty_path = _tty_override() user_tty = kwargs.pop('tty', None) if user_tty is not None: tty_path = user_tty if tty_path and ('stdin' not in kwargs or 'stdout' not in kwargs): tty_file, term_size = _open_tty(tty_path) kwargs.setdefault('stdin', tty_file) kwargs.setdefault('stdout', tty_file) kwargs.setdefault('term_size', term_size) from pudb.debugger import Debugger dbg = Debugger(**kwargs) CURRENT_DEBUGGER.append(dbg) return dbg else: return CURRENT_DEBUGGER[0]
def set_trace(paused=True, frame=None, term_size=None): """Set a breakpoint in a forked process on Unix system, e.g. Linux & MacOS. In- and output will be redirected to /dev/stdin & /dev/stdout. You can call pudb.forked.set_trace() directly or use it with python's built-in breakpoint(): PYTHONBREAKPOINT=pudb.forked.set_trace python … """ if frame is None: frame = sys._getframe().f_back if term_size is None: try: # Getting terminal size s = os.get_terminal_size() term_size = (s.columns, s.lines) except Exception: term_size = (80, 24) Debugger( stdin=open("/dev/stdin"), stdout=open("/dev/stdout", "w"), term_size=term_size, ).set_trace(frame, paused=paused)
def runscript(mainpyfile, args=None, pre_run="", steal_output=False): from pudb.debugger import Debugger dbg = Debugger(steal_output=steal_output) # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. The best approach would be to # have a "restart" command which would allow explicit specification of # command line arguments. import sys if args is not None: prev_sys_argv = sys.argv[:] sys.argv = [mainpyfile] + args # replace pudb's dir with script's dir in front of module search path. from os.path import dirname prev_sys_path = sys.path[:] sys.path[0] = dirname(mainpyfile) from pudb.settings import load_breakpoints for bpoint_descr in load_breakpoints(dbg): dbg.set_break(*bpoint_descr) while True: if pre_run: from subprocess import call retcode = call(pre_run, close_fds=True, shell=True) if retcode: print "*** WARNING: pre-run process exited with code %d." % retcode raw_input("[Hit Enter]") status_msg = "" try: dbg._runscript(mainpyfile) except SystemExit, se: status_msg = "The debuggee exited normally with status code %s.\n\n" % se.code except:
def set_trace(self, depth=1): """ wrap pudb.set_trace, dropping any IO capturing. """ self.disable_io_capture() dbg = Debugger() pudb.set_interrupt_handler() dbg.set_trace(sys._getframe(depth))
def post_mortem(tb, excinfo): dbg = Debugger() stack, i = dbg.get_stack(None, tb) dbg.reset() i = _find_last_non_hidden_frame(stack) dbg.interaction(stack[i][0], excinfo._excinfo)
def runscript(mainpyfile, args=None, pre_run="", steal_output=False): from pudb.debugger import Debugger dbg = Debugger(steal_output=steal_output) # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. The best approach would be to # have a "restart" command which would allow explicit specification of # command line arguments. import sys if args is not None: prev_sys_argv = sys.argv[:] sys.argv = [mainpyfile] + args # replace pudb's dir with script's dir in front of module search path. from os.path import dirname prev_sys_path = sys.path[:] sys.path[0] = dirname(mainpyfile) from pudb.settings import load_breakpoints for bpoint_descr in load_breakpoints(dbg): dbg.set_break(*bpoint_descr) while True: if pre_run: from subprocess import call retcode = call(pre_run, close_fds=True, shell=True) if retcode: print("*** WARNING: pre-run process exited with code %d." % retcode) raw_input("[Hit Enter]") status_msg = "" try: dbg._runscript(mainpyfile) except SystemExit: se = sys.exc_info()[1] status_msg = "The debuggee exited normally with status code %s.\n\n" % se.code except: dbg.post_mortem = True dbg.interaction(None, sys.exc_info()) def quit_debugger(w, size, key): dbg.ui.quit_event_loop = ["quit"] import urwid pre_run_edit = urwid.Edit("", pre_run) result = dbg.ui.call_with_ui(dbg.ui.dialog, urwid.ListBox([urwid.Text( "Your PuDB session has ended.\n\n%s" "Would you like to quit PuDB or restart your program?\n" "You may hit 'q' to quit." % status_msg), urwid.Text("\n\nIf you decide to restart, this command will be run prior to " "actually restarting:"), urwid.AttrMap(pre_run_edit, "value") ]), [ ("Restart", "restart"), ("Quit", "quit"), ], focus_buttons=True, bind_enter_esc=False, title="Finished", extra_bindings=[("q", quit_debugger)]) if result == "quit": return pre_run = pre_run_edit.get_edit_text() dbg.restart() if args is not None: sys.argv = prev_sys_argv sys.path = prev_sys_path