def getFrameNames(self, frame): """Returns the module and function name for the frame. """ if isATALInterpeterFrame(frame): source_file, ln = self.getTALPosition(frame) if source_file: return '', source_file.split('/')[-1] return DebugServer.getFrameNames(self, frame)
def start(username, password, host='127.0.0.1', port=26200, server_type='zope'): global auth_str, debug_server, connection if debug_server is not None: raise RuntimeError, 'The debug server is already running' # Create the debug server. if server_type == 'zope': from ZopeScriptDebugServer import ZopeScriptDebugServer ds = ZopeScriptDebugServer() elif server_type == 'basic': ds = DebugServer() else: raise ValueError, 'Unknown debug server type: %s' % server_type connection = DebuggerConnection(ds) connection.allowEnvChanges() # Allow changing of sys.path, etc. # Create an authentication string. auth_str = base64.encodestring('%s:%s' % (username, password)).strip() debug_server = ds server = TaskingTCPServer((host, port), DebugRequestHandler) port = int(server.socket.getsockname()[1]) # Provide a hard breakpoint hook. Use it like this: # if hasattr(sys, 'breakpoint'): sys.breakpoint() sys.breakpoint = debug_server.set_trace sys.debugger_control = debug_server sys.boa_debugger = debug_server def serve_forever(server): while 1: server.handle_request() def startDaemon(target, args=()): t = threading.Thread(target=target, args=args) t.setDaemon(1) t.start() startDaemon(serve_forever, (server, )) #startDaemon(debug_server.servicerThread) print >> sys.stderr, "Debug server listening on %s:%s" % tuple( server.socket.getsockname()[:2]) try: import atexit except ImportError: pass else: atexit.register(server.socket.close)
def main(args=None): global auth_str, debug_server, connection, serving # Create the debug server. if args is None: args = sys.argv[1:] if args and '--zope' in args: from ZopeScriptDebugServer import ZopeScriptDebugServer debug_server = ZopeScriptDebugServer() else: debug_server = DebugServer() connection = DebuggerConnection(debug_server) connection.allowEnvChanges() # Allow changing of sys.path, etc. # Create an authentication string, always 40 characters. auth_str = sha.new(str(random.random())).hexdigest() # port is 0 to allocate any port. server = TaskingTCPServer(('', 0), DebugRequestHandler) port = int(server.socket.getsockname()[1]) # Tell the client what port to connect to and the auth string to send. sys.stdout.write('%010d %s%s' % (port, auth_str, os.linesep)) sys.stdout.flush() # Provide a hard breakpoint hook. Use it like this: # if hasattr(sys, 'breakpoint'): sys.breakpoint() sys.breakpoint = debug_server.set_trace sys.debugger_control = debug_server sys.boa_debugger = debug_server def serve_forever(server): while 1: server.handle_request() def startDaemon(target, args=()): t = threading.Thread(target=target, args=args) t.setDaemon(1) t.start() startDaemon(serve_forever, (server, )) startDaemon(streamFlushThread) startDaemon(debug_server.servicerThread) # Serve until the stdin pipe closes. #print 'serving until stdin returns EOF' #sys.stdin.read() while serving: time.sleep(0.1) sys.exit(0)
def getStackInfo(self): """Returns a tuple describing the current stack. """ exc_type, exc_value, stack, frame_stack_len = ( DebugServer.getStackInfo(self)) if self.scripts_only_mode: # Filter non-script frames out of the stack. new_stack = [] new_len = 0 for idx in range(len(stack)): frame, lineno = stack[idx] if self.isAScriptFrame(frame): new_stack.append((frame, lineno)) if idx < frame_stack_len: new_len = new_len + 1 stack = new_stack frame_stack_len = new_len # Compute filenames and positions for TAL frames. # The source_file and position variables get applied to the # interpreter frame that called them. self.stack_extra = {} last_interp = None saved_source = None saved_lineno = None for idx in range(len(stack) - 1, -1, -1): frame, lineno = stack[idx] if isATALInterpeterFrame(frame): caller = frame.f_back caller_name = caller.f_code.co_name interp = frame.f_locals.get('self', None) if last_interp is interp: self.stack_extra[frame] = (saved_source, saved_lineno) if caller_name in ('do_useMacro', 'do_defineSlot'): # Using a macro or slot. # Expect to find saved_source and saved_position in # locals. saved_source = caller.f_locals.get( 'prev_source') #saved_source position = 0 #caller.f_locals.get('saved_position') if position: saved_lineno = position[0] or 0 else: saved_lineno = 0 last_interp = interp return exc_type, exc_value, stack, frame_stack_len
def getStackInfo(self): """Returns a tuple describing the current stack. """ exc_type, exc_value, stack, frame_stack_len = ( DebugServer.getStackInfo(self)) if self.scripts_only_mode: # Filter non-script frames out of the stack. new_stack = [] new_len = 0 for idx in range(len(stack)): frame, lineno = stack[idx] if self.isAScriptFrame(frame): new_stack.append((frame, lineno)) if idx < frame_stack_len: new_len = new_len + 1 stack = new_stack frame_stack_len = new_len # Compute filenames and positions for TAL frames. # The source_file and position variables get applied to the # interpreter frame that called them. self.stack_extra = {} last_interp = None saved_source = None saved_lineno = None for idx in range(len(stack) - 1, -1, -1): frame, lineno = stack[idx] if isATALInterpeterFrame(frame): caller = frame.f_back caller_name = caller.f_code.co_name interp = frame.f_locals.get('self', None) if last_interp is interp: self.stack_extra[frame] = (saved_source, saved_lineno) if caller_name in ('do_useMacro', 'do_defineSlot'): # Using a macro or slot. # Expect to find saved_source and saved_position in # locals. saved_source = caller.f_locals.get('prev_source') #saved_source position = 0#caller.f_locals.get('saved_position') if position: saved_lineno = position[0] or 0 else: saved_lineno = 0 last_interp = interp return exc_type, exc_value, stack, frame_stack_len
def isTraceable(self, frame): """Indicates whether the debugger should step into the given frame. Called often. """ if self.scripts_only_mode: code = frame.f_code if isAPythonScriptMetaType(code.co_filename): return 1 if code.co_name == 'setPosition': if frame.f_globals.get('__name__') == TALES_MODULE_NAME: # Trace calls to PageTemplate.TALES.Context.setPosition(). # Avoid stopping more than once per call. if frame.f_lineno == frame.f_code.co_firstlineno: return 1 return 0 return DebugServer.isTraceable(self, frame)
def beforeResume(self): """Frees references before jumping back into user code.""" DebugServer.beforeResume(self) self.stack_extra = None