def makeThreadSuspendMessage(self, thread_id, frame, stop_reason, message): """ <xml> <thread id="id" stop_reason="reason"> <frame id="id" name="functionName " file="file" line="line"> <var variable stuffff.... </frame> </thread> """ try: cmdTextList = ["<xml>"] if message: message = pydevd_vars.makeValidXmlValue(str(message)) cmdTextList.append('<thread id="%s" stop_reason="%s" message="%s">' % (thread_id, stop_reason, message)) curFrame = frame try: while curFrame: #print cmdText myId = str(id(curFrame)) #print "id is ", myId if curFrame.f_code is None: break #Iron Python sometimes does not have it! myName = curFrame.f_code.co_name #method name (if in method) or ? if global if myName is None: break #Iron Python sometimes does not have it! #print "name is ", myName filename, base = pydevd_file_utils.GetFilenameAndBase(curFrame) myFile = pydevd_file_utils.NormFileToClient(filename) #print "file is ", myFile #myFile = inspect.getsourcefile(curFrame) or inspect.getfile(frame) myLine = str(curFrame.f_lineno) #print "line is ", myLine #the variables are all gotten 'on-demand' #variables = pydevd_vars.frameVarsToXML(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId , pydevd_vars.makeValidXmlValue(myName))) cmdTextList.append('file="%s" line="%s">"' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back except : traceback.print_exc() cmdTextList.append("</thread></xml>") cmdText = ''.join(cmdTextList) return NetCommand(CMD_THREAD_SUSPEND, 0, cmdText) except: return self.makeErrorMessage(0, GetExceptionTracebackStr())
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 makeSendCurrExceptionTraceMessage(self, seq, thread_id, curr_frame_id, exc_type, exc_desc, trace_obj): try: while trace_obj.tb_next is not None: trace_obj = trace_obj.tb_next payload = str(curr_frame_id) + '\t' + pydevd_vars.makeValidXmlValue(str(exc_type)) + "\t" + \ pydevd_vars.makeValidXmlValue(str(exc_desc)) + "\t" + \ self.makeThreadSuspendStr(thread_id, trace_obj.tb_frame, CMD_SEND_CURR_EXCEPTION_TRACE) return NetCommand(CMD_SEND_CURR_EXCEPTION_TRACE, seq, payload) except Exception: return self.makeErrorMessage(seq, GetExceptionTracebackStr())
def create_hierarchy_call_message(call_info): cmdTextList = ["<xml><hierarchy_call>"] cmdTextList.append('<caller file="%s" name="%s" line="%s"></caller><callee file="%s" name="%s"></callee>' % (pydevd_vars.makeValidXmlValue(call_info.caller_file), pydevd_vars.makeValidXmlValue(call_info.caller_name), pydevd_vars.makeValidXmlValue(call_info.caller_line), pydevd_vars.makeValidXmlValue(call_info.callee_file), pydevd_vars.makeValidXmlValue(call_info.callee_name))) cmdTextList.append("</hierarchy_call></xml>") cmdText = ''.join(cmdTextList) print 'qwerty' return NetCommand(CMD_HIERARCHY_CALL_TRACE, 0, cmdText)
def makeSendCurrExceptionTraceMessage(self, seq, thread_id, curr_frame_id, exc_type, exc_desc, trace_obj): try: while trace_obj.tb_next is not None: trace_obj = trace_obj.tb_next exc_type = pydevd_vars.makeValidXmlValue(str(exc_type)).replace('\t', ' ') or 'exception: type unknown' exc_desc = pydevd_vars.makeValidXmlValue(str(exc_desc)).replace('\t', ' ') or 'exception: no description' payload = str(curr_frame_id) + '\t' + exc_type + "\t" + exc_desc + "\t" + \ self.makeThreadSuspendStr(thread_id, trace_obj.tb_frame, CMD_SEND_CURR_EXCEPTION_TRACE) return NetCommand(CMD_SEND_CURR_EXCEPTION_TRACE, seq, payload) except Exception: return self.makeErrorMessage(seq, GetExceptionTracebackStr())
def create_return_signature_message(signature, return_info): cmdTextList = ["<xml>"] cmdTextList.append('<return_signature file="%s" name="%s" return_type="%s">' % (pydevd_vars.makeValidXmlValue(signature.file), pydevd_vars.makeValidXmlValue(signature.name), #pydevd_vars.makeValidXmlValue('str'))) pydevd_vars.makeValidXmlValue(return_info))) cmdTextList.append("</return_signature></xml>") cmdText = ''.join(cmdTextList) #print "return command = %s" % cmdText return NetCommand(CMD_SIGNATURE_RETURN_TRACE, 0, cmdText)
def create_signature_message(signature, return_info=None): cmdTextList = ["<xml>"] cmdTextList.append('<call_signature file="%s" name="%s">' % (pydevd_vars.makeValidXmlValue(signature.file), pydevd_vars.makeValidXmlValue(signature.name))) if return_info: cmdTextList.append('<return type="%s"></return>' % pydevd_vars.makeValidXmlValue(return_info)) else: 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) # myfile = open('/home/user/txt', 'a') # myfile.writelines(cmdText + '\n=====\n') return NetCommand(CMD_SIGNATURE_CALL_TRACE, 0, cmdText)
def makeThreadSuspendMessage(self, thread_id, frame, stop_reason): """ <xml> <thread id="id" stop_reason="reason"> <frame id="id" name="functionName " file="file" line="line"> <var variable stuffff.... </frame> </thread> """ try: cmdTextList = ["<xml>"] cmdTextList.append('<thread id="%s" stop_reason="%s">' % (thread_id, stop_reason)) curFrame = frame while curFrame: #print cmdText myId = str(id(curFrame)) #print "id is ", myId if curFrame.f_code is None: break #Iron Python sometimes does not have it! myName = curFrame.f_code.co_name #method name (if in method) or ? if global if myName is None: break #Iron Python sometimes does not have it! #print "name is ", myName myFile = pydevd_file_utils.NormFileToClient(curFrame.f_code.co_filename) if file_system_encoding.lower() != "utf-8" and hasattr(myFile, "decode"): # myFile is a byte string encoded using the file system encoding # convert it to utf8 myFile = myFile.decode(file_system_encoding).encode("utf-8") #print "file is ", myFile #myFile = inspect.getsourcefile(curFrame) or inspect.getfile(frame) myLine = str(curFrame.f_lineno) #print "line is ", myLine #the variables are all gotten 'on-demand' #variables = pydevd_vars.frameVarsToXML(curFrame) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId , pydevd_vars.makeValidXmlValue(myName))) cmdTextList.append('file="%s" line="%s">"' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back cmdTextList.append("</thread></xml>") cmdText = ''.join(cmdTextList) return NetCommand(CMD_THREAD_SUSPEND, 0, cmdText) except: return self.makeErrorMessage(0, GetExceptionTracebackStr())
def makeThreadSuspendStr(self, thread_id, frame, stop_reason): """ <xml> <thread id="id" stop_reason="reason"> <frame id="id" name="functionName " file="file" line="line"> <var variable stuffff.... </frame> </thread> """ cmdTextList = ["<xml>"] cmdTextList.append('<thread id="%s" stop_reason="%s">' % (thread_id, stop_reason)) curFrame = frame while curFrame: #print cmdText myId = str(id(curFrame)) #print "id is ", myId if curFrame.f_code is None: break #Iron Python sometimes does not have it! myName = curFrame.f_code.co_name #method name (if in method) or ? if global if myName is None: break #Iron Python sometimes does not have it! #print "name is ", myName myFile = NormFileToClient(curFrame.f_code.co_filename) if file_system_encoding.lower() != "utf-8" and hasattr( myFile, "decode"): # myFile is a byte string encoded using the file system encoding # convert it to utf8 myFile = myFile.decode(file_system_encoding).encode("utf-8") #print "file is ", myFile #myFile = inspect.getsourcefile(curFrame) or inspect.getfile(frame) myLine = str(curFrame.f_lineno) #print "line is ", myLine #the variables are all gotten 'on-demand' #variables = pydevd_vars.frameVarsToXML(curFrame) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId, pydevd_vars.makeValidXmlValue(myName))) cmdTextList.append('file="%s" line="%s">"' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back cmdTextList.append("</thread></xml>") return ''.join(cmdTextList)
def get_text_list_for_frame(frame): # partial copy-paste from makeThreadSuspendStr curFrame = frame cmdTextList = [] try: while curFrame: #print cmdText myId = str(id(curFrame)) #print "id is ", myId if curFrame.f_code is None: break #Iron Python sometimes does not have it! myName = curFrame.f_code.co_name #method name (if in method) or ? if global if myName is None: break #Iron Python sometimes does not have it! #print "name is ", myName filename, base = pydevd_file_utils.GetFilenameAndBase(curFrame) myFile = pydevd_file_utils.NormFileToClient(filename) if file_system_encoding.lower() != "utf-8" and hasattr( myFile, "decode"): # myFile is a byte string encoded using the file system encoding # convert it to utf8 myFile = myFile.decode(file_system_encoding).encode("utf-8") #print "file is ", myFile #myFile = inspect.getsourcefile(curFrame) or inspect.getfile(frame) myLine = str(curFrame.f_lineno) #print "line is ", myLine #the variables are all gotten 'on-demand' #variables = pydevd_vars.frameVarsToXML(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId, pydevd_vars.makeValidXmlValue(myName))) cmdTextList.append('file="%s" line="%s">"' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back except: traceback.print_exc() return cmdTextList
def get_text_list_for_frame(frame): # partial copy-paste from makeThreadSuspendStr curFrame = frame cmdTextList = [] try: while curFrame: #print cmdText myId = str(id(curFrame)) #print "id is ", myId if curFrame.f_code is None: break #Iron Python sometimes does not have it! myName = curFrame.f_code.co_name #method name (if in method) or ? if global if myName is None: break #Iron Python sometimes does not have it! #print "name is ", myName filename, base = pydevd_file_utils.GetFilenameAndBase(curFrame) myFile = pydevd_file_utils.NormFileToClient(filename) if file_system_encoding.lower() != "utf-8" and hasattr(myFile, "decode"): # myFile is a byte string encoded using the file system encoding # convert it to utf8 myFile = myFile.decode(file_system_encoding).encode("utf-8") #print "file is ", myFile #myFile = inspect.getsourcefile(curFrame) or inspect.getfile(frame) myLine = str(curFrame.f_lineno) #print "line is ", myLine #the variables are all gotten 'on-demand' #variables = pydevd_vars.frameVarsToXML(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId , pydevd_vars.makeValidXmlValue(myName))) cmdTextList.append('file="%s" line="%s">"' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back except : traceback.print_exc() return cmdTextList
def makeIoMessage( self, v, ctx, dbg=None ): ''' @param v: the message to pass to the debug server @param ctx: 1 for stdio 2 for stderr @param dbg: If not none, add to the writer ''' try: if len( v ) > MAX_IO_MSG_SIZE: v = v[0:MAX_IO_MSG_SIZE] v += '...' v = pydevd_vars.makeValidXmlValue( quote( v, '/>_= \t' ) ) net = NetCommand( str( CMD_WRITE_TO_CONSOLE ), 0, '<xml><io s="%s" ctx="%s"/></xml>' % ( v, ctx ) ) if dbg: dbg.writer.addCommand( net ) except: return self.makeErrorMessage( 0, GetExceptionTracebackStr() )
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 makeIoMessage(self, v, ctx, dbg=None): ''' @param v: the message to pass to the debug server @param ctx: 1 for stdio 2 for stderr @param dbg: If not none, add to the writer ''' try: if len(v) > MAX_IO_MSG_SIZE: v = v[0:MAX_IO_MSG_SIZE] v += '...' v = pydevd_vars.makeValidXmlValue(urllib.quote(v, '/>_= \t')) net = NetCommand(str(CMD_WRITE_TO_CONSOLE), 0, '<xml><io s="%s" ctx="%s"/></xml>' % (v, ctx)) if dbg: dbg.writer.addCommand(net) except: return self.makeErrorMessage(0, GetExceptionTracebackStr())
def get_referrer_info(searched_obj): try: if searched_obj is None: ret = ["<xml>\n"] ret.append("<for>\n") ret.append( pydevd_vars.varToXML( searched_obj, "Skipping getting referrers for None", ' id="%s"' % (id(searched_obj),) ) ) ret.append("</for>\n") ret.append("</xml>") ret = "".join(ret) return ret obj_id = id(searched_obj) try: import gc referrers = gc.get_referrers(searched_obj) except: import traceback traceback.print_exc() ret = ["<xml>\n"] ret.append("<for>\n") ret.append( pydevd_vars.varToXML( searched_obj, "Exception raised while trying to get_referrers.", ' id="%s"' % (id(searched_obj),) ) ) ret.append("</for>\n") ret.append("</xml>") ret = "".join(ret) return ret traceback.print_exc() curr_frame = sys._getframe() frame_type = type(curr_frame) # Ignore this frame and any caller frame of this frame ignore_frames = {} # Should be a set, but it's not available on all python versions. while curr_frame is not None: if basename(curr_frame.f_code.co_filename).startswith("pydev"): ignore_frames[curr_frame] = 1 curr_frame = curr_frame.f_back ret = ["<xml>\n"] ret.append("<for>\n") ret.append(pydevd_vars.varToXML(searched_obj, "Referrers of", ' id="%s"' % (obj_id,))) ret.append("</for>\n") all_objects = None for r in referrers: try: if DictContains(ignore_frames, r): continue # Skip the references we may add ourselves except: pass # Ok: unhashable type checked... if r is referrers: continue r_type = type(r) r_id = str(id(r)) representation = str(r_type) found_as = "" if r_type == frame_type: for key, val in r.f_locals.items(): if val is searched_obj: found_as = key break elif r_type == dict: # Try to check if it's a value in the dict (and under which key it was found) for key, val in r.items(): if val is searched_obj: found_as = key break # Ok, there's one annoying thing: many times we find it in a dict from an instance, # but with this we don't directly have the class, only the dict, so, to workaround that # we iterate over all reachable objects ad check if one of those has the given dict. if all_objects is None: all_objects = gc.get_objects() for x in all_objects: try: if getattr(x, "__dict__", None) is r: r = x r_type = type(x) r_id = str(id(r)) representation = str(r_type) break except: pass # Just ignore any error here (i.e.: ReferenceError, etc.) elif r_type in (tuple, list): # Don't use enumerate() because not all Python versions have it. i = 0 for x in r: if x is searched_obj: found_as = "%s[%s]" % (r_type.__name__, i) break i += 1 if found_as: found_as = ' found_as="%s"' % (pydevd_vars.makeValidXmlValue(found_as),) ret.append(pydevd_vars.varToXML(r, representation, ' id="%s"%s' % (r_id, found_as))) finally: # If we have any exceptions, don't keep dangling references from this frame to any of our objects. all_objects = None referrers = None searched_obj = None r = None x = None key = None val = None curr_frame = None ignore_frames = None ret.append("</xml>") ret = "".join(ret) return ret
def threadToXML(self, thread): """ thread information as XML """ name = pydevd_vars.makeValidXmlValue(thread.getName()) cmdText = '<thread name="%s" id="%s" />' % (urllib.quote(name), GetThreadId(thread)) return cmdText
def get_referrer_info(searched_obj): try: if searched_obj is None: ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_vars.varToXML(searched_obj, 'Skipping getting referrers for None', ' id="%s"' % (id(searched_obj), ))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret obj_id = id(searched_obj) try: import gc referrers = gc.get_referrers(searched_obj) except: import traceback traceback.print_exc() ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_vars.varToXML( searched_obj, 'Exception raised while trying to get_referrers.', ' id="%s"' % (id(searched_obj), ))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret traceback.print_exc() curr_frame = sys._getframe() frame_type = type(curr_frame) #Ignore this frame and any caller frame of this frame ignore_frames = { } #Should be a set, but it's not available on all python versions. while curr_frame is not None: if basename(curr_frame.f_code.co_filename).startswith('pydev'): ignore_frames[curr_frame] = 1 curr_frame = curr_frame.f_back ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_vars.varToXML(searched_obj, 'Referrers of', ' id="%s"' % (obj_id, ))) ret.append('</for>\n') all_objects = None for r in referrers: try: if DictContains(ignore_frames, r): continue #Skip the references we may add ourselves except: pass #Ok: unhashable type checked... if r is referrers: continue r_type = type(r) r_id = str(id(r)) representation = str(r_type) found_as = '' if r_type == frame_type: for key, val in r.f_locals.items(): if val is searched_obj: found_as = key break elif r_type == dict: # Try to check if it's a value in the dict (and under which key it was found) for key, val in r.items(): if val is searched_obj: found_as = key break #Ok, there's one annoying thing: many times we find it in a dict from an instance, #but with this we don't directly have the class, only the dict, so, to workaround that #we iterate over all reachable objects ad check if one of those has the given dict. if all_objects is None: all_objects = gc.get_objects() for x in all_objects: if getattr(x, '__dict__', None) is r: r = x r_type = type(x) r_id = str(id(r)) representation = str(r_type) break elif r_type in (tuple, list): #Don't use enumerate() because not all Python versions have it. i = 0 for x in r: if x is searched_obj: found_as = '%s[%s]' % (r_type.__name__, i) break i += 1 if found_as: found_as = ' found_as="%s"' % ( pydevd_vars.makeValidXmlValue(found_as), ) ret.append( pydevd_vars.varToXML(r, representation, ' id="%s"%s' % (r_id, found_as))) finally: #If we have any exceptions, don't keep dangling references from this frame to any of our objects. all_objects = None referrers = None searched_obj = None r = None x = None key = None val = None curr_frame = None ignore_frames = None ret.append('</xml>') ret = ''.join(ret) return ret
def makeValid(s): return pydevd_vars.makeValidXmlValue(pydevd_vars.quote(s, '/>_= \t'))
def makeCustomFrameCreatedMessage(self, frameId, frameDescription): frameDescription = pydevd_vars.makeValidXmlValue(frameDescription) cmdText = '<xml><thread name="%s" id="%s"/></xml>' % (frameDescription, frameId) return NetCommand(CMD_THREAD_CREATE, 0, cmdText)
def threadToXML(self, thread): """ thread information as XML """ name = pydevd_vars.makeValidXmlValue(thread.getName()) cmdText = '<thread name="%s" id="%s" />' % (quote(name), GetThreadId(thread)) return cmdText