def main(): port_var = 'MYSTIKOS_PDB_PORT' port = os.environ.get(port_var) if not port: print( 'MYSTIKOS_PDB_PORT environment variable not set. Defaulting to port 5678' ) port = 5678 else: port = int(port) print('Mystikos pdb waiting for connections at port %d' % port) sys.stdout.flush() host = '127.0.0.1' client_socket = socket.create_server((host, port), family=socket.AF_INET, reuse_port=False) client_socket.listen(1) connection, address = client_socket.accept() fd = connection.makefile('rw') args = sys.argv[1:] mainpyfile = args[0] run_as_module = False if args[0] == '-m': run_as_module = True mainpyfile = args[1] args = args[1:] # Update args sys.argv[:] = args if not run_as_module: mainpyfile = os.path.realpath(mainpyfile) # Replace pdb's dir with script's dir in front of module search path. sys.path[0] = os.path.dirname(mainpyfile) else: runpy._get_module_details(mainpyfile) pdb = Pdb(completekey='tab', stdin=fd, stdout=fd) # Alias n, c, s commands to show source listing. pdb.rcLines.extend( ['alias n n;; l .', 'alias c c;; l .', 'alias s s;; l .']) try: if run_as_module: pdb._runmodule(mainpyfile) else: pdb._runscript(mainpyfile) except: pass connection.close() client_socket.close()
def pdb_main(): """This pdb_main is modified from pdb.main(). """ from pdb import Pdb, Restart, traceback mainpyfile = sys.argv[0] # Get script filename if not os.path.exists(mainpyfile): print 'Error:', mainpyfile, 'does not exist' sys.exit(1) # Replace pdb's dir with script's dir in front of module search path. sys.path[0] = os.path.dirname(mainpyfile) # 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. There is a "restart" command # which allows explicit specification of command line arguments. pdb = Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__) while True: try: pdb._runscript(mainpyfile) if pdb._user_requested_quit: break print "The program finished and will be restarted" except Restart: print "Restarting", mainpyfile, "with arguments:" print "\t" + " ".join(sys.argv[1:]) except SystemExit: # In most cases SystemExit does not warrant a post-mortem session. print "The program exited via sys.exit(). Exit status: ", print sys.exc_info()[1] except: traceback.print_exc() print "Uncaught exception. Entering post mortem debugging" print "Running 'cont' or 'step' will restart the program" t = sys.exc_info()[2] pdb.interaction(None, t) print "Post mortem debugger finished. The " + mainpyfile + \ " will be restarted"
def debug(self, *arg, **key): self.first = True Pdb._runscript(self, *arg, **key)