def create_signature_message(signature): cmdTextList = ["<xml>"] cmdTextList.append('<call_signature file="%s" name="%s">' % (pydevd_vars.makeValidXmlValue(signature.file), pydevd_vars.makeValidXmlValue(signature.name))) for arg in signature.args: cmdTextList.append('<arg name="%s" type="%s"></arg>' % (pydevd_vars.makeValidXmlValue(arg[0]), pydevd_vars.makeValidXmlValue(arg[1]))) cmdTextList.append("</call_signature></xml>") cmdText = ''.join(cmdTextList) return NetCommand(CMD_SIGNATURE_CALL_TRACE, 0, cmdText)
def send_message(event_class, time, name, thread_id, type, event, file, line, frame, lock_id=0, parent=None): dbg = GlobalDebuggerHolder.globalDbg cmdTextList = ['<xml>'] cmdTextList.append('<' + event_class) cmdTextList.append(' time="%s"' % pydevd_vars.makeValidXmlValue(str(time))) cmdTextList.append(' name="%s"' % pydevd_vars.makeValidXmlValue(name)) cmdTextList.append(' thread_id="%s"' % pydevd_vars.makeValidXmlValue(thread_id)) cmdTextList.append(' type="%s"' % pydevd_vars.makeValidXmlValue(type)) if type == "lock": cmdTextList.append(' lock_id="%s"' % pydevd_vars.makeValidXmlValue(str(lock_id))) if parent is not None: cmdTextList.append(' parent="%s"' % pydevd_vars.makeValidXmlValue(parent)) cmdTextList.append(' event="%s"' % pydevd_vars.makeValidXmlValue(event)) cmdTextList.append(' file="%s"' % pydevd_vars.makeValidXmlValue(file)) cmdTextList.append(' line="%s"' % pydevd_vars.makeValidXmlValue(str(line))) cmdTextList.append('></' + event_class + '>') cmdTextList += get_text_list_for_frame(frame) cmdTextList.append('</xml>') text = ''.join(cmdTextList) if dbg.writer is not None: dbg.writer.addCommand(NetCommand(145, 0, text))
def run(self, file, globals=None, locals=None): if globals is None: #patch provided by: Scott Schlesier - when script is run, it does not #use globals from pydevd: #This will prevent the pydevd script from contaminating the namespace for the script to be debugged #pretend pydevd is not the main module, and #convince the file to be debugged that it was loaded as main sys.modules['pydevd'] = sys.modules['__main__'] sys.modules['pydevd'].__name__ = 'pydevd' from imp import new_module m = new_module('__main__') sys.modules['__main__'] = m m.__file__ = file globals = m.__dict__ if locals is None: locals = globals #Predefined (writable) attributes: __name__ is the module's name; #__doc__ is the module's documentation string, or None if unavailable; #__file__ is the pathname of the file from which the module was loaded, #if it was loaded from a file. The __file__ attribute is not present for #C modules that are statically linked into the interpreter; for extension modules #loaded dynamically from a shared library, it is the pathname of the shared library file. #I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in #debug and run. if m.__file__.startswith(sys.path[0]): #print >> sys.stderr, 'Deleting: ', sys.path[0] del sys.path[0] #now, the local directory has to be added to the pythonpath #sys.path.insert(0, os.getcwd()) #Changed: it's not the local directory, but the directory of the file launched #The file being run ust be in the pythonpath (even if it was not before) sys.path.insert(0, os.path.split(file)[0]) # for completness, we'll register the pydevd.reader & pydevd.writer threads net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.reader" id="-1"/></xml>') self.writer.addCommand(net) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.writer" id="-1"/></xml>') self.writer.addCommand(net) pydevd_tracing.SetTrace(self.trace_dispatch) try: #not available in jython! threading.settrace(self.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass while not self.readyToRun: time.sleep(0.1) # busy wait until we receive run command PyDBCommandThread(debugger).start() pydev_imports.execfile(file, globals, locals) #execute the script
def _locked_settrace(host, stdoutToServer, stderrToServer, port, suspend, trace_only_current_thread): if host is None: import pydev_localhost host = pydev_localhost.get_localhost() global connected global bufferStdOutToServer global bufferStdErrToServer if not connected: connected = True bufferStdOutToServer = stdoutToServer bufferStdErrToServer = stderrToServer pydevd_vm_type.SetupType() debugger = PyDB() debugger.connect(host, port) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.reader" id="-1"/></xml>') debugger.writer.addCommand(net) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.writer" id="-1"/></xml>') debugger.writer.addCommand(net) if bufferStdOutToServer: sys.stdoutBuf = pydevd_io.IOBuf() sys.stdout = pydevd_io.IORedirector( sys.stdout, sys.stdoutBuf) #@UndefinedVariable if bufferStdErrToServer: sys.stderrBuf = pydevd_io.IOBuf() sys.stderr = pydevd_io.IORedirector( sys.stderr, sys.stderrBuf) #@UndefinedVariable SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo while not debugger.readyToRun: time.sleep(0.1) # busy wait until we receive run command if suspend: debugger.setSuspend(t, CMD_SET_BREAK) #note that we do that through pydevd_tracing.SetTrace so that the tracing #is not warned to the user! pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass PyDBCommandThread(debugger).start() else: #ok, we're already in debug mode, with all set, so, let's just set the break debugger = GetGlobalDebugger() SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass if suspend: debugger.setSuspend(t, CMD_SET_BREAK)
def settrace(host='localhost', stdoutToServer=False, stderrToServer=False, port=5678, suspend=True, trace_only_current_thread=True): '''Sets the tracing function with the pydev debug function and initializes needed facilities. @param host: the user may specify another host, if the debug server is not in the same machine @param stdoutToServer: when this is true, the stdout is passed to the debug server @param stderrToServer: when this is true, the stderr is passed to the debug server so that they are printed in its console and not in this process console. @param port: specifies which port to use for communicating with the server (note that the server must be started in the same port). @note: currently it's hard-coded at 5678 in the client @param suspend: whether a breakpoint should be emulated as soon as this function is called. @param trace_only_current_thread: determines if only the current thread will be traced or all future threads will also have the tracing enabled. ''' global connected global bufferStdOutToServer global bufferStdErrToServer if not connected: connected = True bufferStdOutToServer = stdoutToServer bufferStdErrToServer = stderrToServer pydevd_vm_type.SetupType() debugger = PyDB() debugger.connect(host, port) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.reader" id="-1"/></xml>') debugger.writer.addCommand(net) net = NetCommand(str(CMD_THREAD_CREATE), 0, '<xml><thread name="pydevd.writer" id="-1"/></xml>') debugger.writer.addCommand(net) if bufferStdOutToServer: sys.stdoutBuf = pydevd_io.IOBuf() sys.stdout = pydevd_io.IORedirector( sys.stdout, sys.stdoutBuf) #@UndefinedVariable if bufferStdErrToServer: sys.stderrBuf = pydevd_io.IOBuf() sys.stderr = pydevd_io.IORedirector( sys.stderr, sys.stderrBuf) #@UndefinedVariable SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo while not debugger.readyToRun: time.sleep(0.1) # busy wait until we receive run command if suspend: debugger.setSuspend(t, CMD_SET_BREAK) #note that we do that through pydevd_tracing.SetTrace so that the tracing #is not warned to the user! pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass PyDBCommandThread(debugger).start() else: #ok, we're already in debug mode, with all set, so, let's just set the break debugger = GetGlobalDebugger() SetTraceForParents(GetFrame(), debugger.trace_dispatch) t = threadingCurrentThread() try: additionalInfo = t.additionalInfo except AttributeError: additionalInfo = PyDBAdditionalThreadInfo() t.additionalInfo = additionalInfo pydevd_tracing.SetTrace(debugger.trace_dispatch) if not trace_only_current_thread: #Trace future threads? try: #not available in jython! threading.settrace( debugger.trace_dispatch) # for all future threads except: pass try: thread.start_new_thread = pydev_start_new_thread thread.start_new = pydev_start_new_thread except: pass if suspend: debugger.setSuspend(t, CMD_SET_BREAK)