def _make_send_curr_exception_trace_str(self, py_db, thread_id, exc_type, exc_desc, trace_obj): frames_list = pydevd_frame_utils.create_frames_list_from_traceback(trace_obj, None, exc_type, exc_desc) exc_type = pydevd_xml.make_valid_xml_value(str(exc_type)).replace('\t', ' ') or 'exception: type unknown' exc_desc = pydevd_xml.make_valid_xml_value(str(exc_desc)).replace('\t', ' ') or 'exception: no description' thread_suspend_str, thread_stack_str = self.make_thread_suspend_str( py_db, thread_id, frames_list, CMD_SEND_CURR_EXCEPTION_TRACE, '') return exc_type, exc_desc, thread_suspend_str, thread_stack_str
def _make_send_curr_exception_trace_str(self, thread_id, exc_type, exc_desc, trace_obj): while trace_obj.tb_next is not None: trace_obj = trace_obj.tb_next exc_type = pydevd_xml.make_valid_xml_value(str(exc_type)).replace('\t', ' ') or 'exception: type unknown' exc_desc = pydevd_xml.make_valid_xml_value(str(exc_desc)).replace('\t', ' ') or 'exception: no description' thread_suspend_str, thread_stack_str = self.make_thread_suspend_str( thread_id, trace_obj.tb_frame, CMD_SEND_CURR_EXCEPTION_TRACE, '') return exc_type, exc_desc, thread_suspend_str, thread_stack_str
def format_breakpoint_exception(self, threadid, exc_type, stacktrace): info = '<xml>' for filename, line, methodname, methodobj in stacktrace: fn = pydevd_xml.make_valid_xml_value(filename) mn = pydevd_xml.make_valid_xml_value(methodname) obj = pydevd_xml.make_valid_xml_value(methodobj) info += '<frame thread_id = "%s" file="%s" line="%s" name="%s" obj="%s" />' \ % (threadid, fn, line, mn, obj) info += "</xml>" return exc_type + '\t' + info
def send_message(event_class, time, name, thread_id, type, event, file, line, frame, lock_id=0, parent=None): dbg = GlobalDebuggerHolder.global_dbg if dbg is None: return cmdTextList = ['<xml>'] cmdTextList.append('<' + event_class) cmdTextList.append(' time="%s"' % pydevd_xml.make_valid_xml_value(str(time))) cmdTextList.append(' name="%s"' % pydevd_xml.make_valid_xml_value(name)) cmdTextList.append(' thread_id="%s"' % pydevd_xml.make_valid_xml_value(thread_id)) cmdTextList.append(' type="%s"' % pydevd_xml.make_valid_xml_value(type)) if type == "lock": cmdTextList.append(' lock_id="%s"' % pydevd_xml.make_valid_xml_value(str(lock_id))) if parent is not None: cmdTextList.append(' parent="%s"' % pydevd_xml.make_valid_xml_value(parent)) cmdTextList.append(' event="%s"' % pydevd_xml.make_valid_xml_value(event)) cmdTextList.append(' file="%s"' % pydevd_xml.make_valid_xml_value(file)) cmdTextList.append(' line="%s"' % pydevd_xml.make_valid_xml_value(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.add_command(NetCommand(145, 0, text))
def create_signature_message(signature): cmdTextList = ["<xml>"] cmdTextList.append('<call_signature file="%s" name="%s">' % (pydevd_xml.make_valid_xml_value(signature.file), pydevd_xml.make_valid_xml_value(signature.name))) for arg in signature.args: cmdTextList.append('<arg name="%s" type="%s"></arg>' % (pydevd_xml.make_valid_xml_value(arg[0]), pydevd_xml.make_valid_xml_value(arg[1]))) if signature.return_type is not None: cmdTextList.append('<return type="%s"></return>' % (pydevd_xml.make_valid_xml_value(signature.return_type))) cmdTextList.append("</call_signature></xml>") cmdText = ''.join(cmdTextList) return NetCommand(CMD_SIGNATURE_CALL_TRACE, 0, cmdText)
def format_exception_details(self, threadid, exc, *frames): name = pydevd_xml.make_valid_xml_value(str(type(exc))) if hasattr(exc, 'args') and len(exc.args) > 0: desc = str(exc.args[0]) else: desc = str(exc) desc = pydevd_xml.make_valid_xml_value(desc) info = '<xml>' info += '<thread id="{}" exc_type="{}" exc_desc="{}" >'.format( threadid, name, desc) fmt = '<frame id="{}" name="{}" file="{}" line="{}" />' for frame in frames: # (fid, func, filename, line) info += fmt.format(*frame) info += '</thread></xml>' return info
def format_exception(self, threadid, exc, frame): frameid, _, _, _ = frame name = pydevd_xml.make_valid_xml_value(type(exc).__name__) description = pydevd_xml.make_valid_xml_value(str(exc)) info = '<xml>' info += '<thread id="{}" />'.format(threadid) info += '</xml>' return '{}\t{}\t{}\t{}'.format( frameid, name or 'exception: type unknown', description or 'exception: no description', self.format_frames( threadid, CMD_SEND_CURR_EXCEPTION_TRACE, frame, ), )
def get_text_list_for_frame(frame): # partial copy-paste from make_thread_suspend_str 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 = pydevd_file_utils.get_abs_path_real_path_and_base_from_frame( curFrame)[1] myFile = pydevd_file_utils.norm_file_to_client(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_xml.frame_vars_to_xml(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId, pydevd_xml.make_valid_xml_value(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 internal_get_description(dbg, seq, thread_id, frame_id, expression): ''' Fetch the variable description stub from the debug console ''' try: frame = dbg.find_frame(thread_id, frame_id) description = pydevd_console.get_description(frame, thread_id, frame_id, expression) description = pydevd_xml.make_valid_xml_value(quote(description, '/>_= \t')) description_xml = '<xml><var name="" type="" value="%s"/></xml>' % description cmd = dbg.cmd_factory.make_get_description_message(seq, description_xml) dbg.writer.add_command(cmd) except: exc = get_exception_traceback_str() cmd = dbg.cmd_factory.make_error_message(seq, "Error in fetching description" + exc) dbg.writer.add_command(cmd)
def make_io_message(self, v, ctx): ''' @param v: the message to pass to the debug server @param ctx: 1 for stdio 2 for stderr ''' try: if len(v) > MAX_IO_MSG_SIZE: v = v[0:MAX_IO_MSG_SIZE] v += '...' v = pydevd_xml.make_valid_xml_value(quote(v, '/>_= ')) return NetCommand(str(CMD_WRITE_TO_CONSOLE), 0, '<xml><io s="%s" ctx="%s"/></xml>' % (v, ctx)) except: return self.make_error_message(0, get_exception_traceback_str())
def get_text_list_for_frame(frame): # partial copy-paste from make_thread_suspend_str 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 = pydevd_file_utils.get_abs_path_real_path_and_base_from_frame(curFrame)[1] myFile = pydevd_file_utils.norm_file_to_client(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_xml.frame_vars_to_xml(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId , pydevd_xml.make_valid_xml_value(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 make_io_message(self, msg, ctx): ''' @param msg: the message to pass to the debug server @param ctx: 1 for stdio 2 for stderr ''' try: msg = pydevd_constants.as_str(msg) if len(msg) > MAX_IO_MSG_SIZE: msg = msg[0:MAX_IO_MSG_SIZE] msg += '...' msg = pydevd_xml.make_valid_xml_value(quote(msg, '/>_= ')) return NetCommand(str(CMD_WRITE_TO_CONSOLE), 0, '<xml><io s="%s" ctx="%s"/></xml>' % (msg, ctx)) except: return self.make_error_message(0, get_exception_traceback_str())
def get_text_list_for_frame(frame): # partial copy-paste from make_thread_suspend_str 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 absolute_filename = pydevd_file_utils.get_abs_path_real_path_and_base_from_frame( curFrame)[0] my_file, _applied_mapping = pydevd_file_utils.map_file_to_client( absolute_filename) # print "file is ", my_file # my_file = 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_xml.frame_vars_to_xml(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId, pydevd_xml.make_valid_xml_value(myName))) cmdTextList.append('file="%s" line="%s">' % (quote(my_file, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back except: pydev_log.exception() return cmdTextList
def get_text_list_for_frame(frame): # partial copy-paste from make_thread_suspend_str 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 = pydevd_file_utils.get_abs_path_real_path_and_base_from_frame(curFrame)[1] myFile = pydevd_file_utils.norm_file_to_client(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_xml.frame_vars_to_xml(curFrame.f_locals) variables = '' cmdTextList.append('<frame id="%s" name="%s" ' % (myId , pydevd_xml.make_valid_xml_value(myName))) cmdTextList.append('file="%s" line="%s">' % (quote(myFile, '/>_= \t'), myLine)) cmdTextList.append(variables) cmdTextList.append("</frame>") curFrame = curFrame.f_back except : pydev_log.exception() return cmdTextList
def make_list_threads_message(self, py_db, seq): """ returns thread listing as XML """ try: threads = get_non_pydevd_threads() cmd_text = ["<xml>"] append = cmd_text.append for thread in threads: if is_thread_alive(thread): append(self._thread_to_xml(thread)) for thread_id, thread_name in list( self._additional_thread_id_to_thread_name.items()): name = pydevd_xml.make_valid_xml_value(thread_name) append('<thread name="%s" id="%s" />' % (quote(name), thread_id)) append("</xml>") return NetCommand(CMD_RETURN, seq, ''.join(cmd_text)) except: return self.make_error_message(seq, get_exception_traceback_str())
def make_custom_frame_created_message(self, frame_id, frame_description): frame_description = pydevd_xml.make_valid_xml_value(frame_description) return NetCommand(CMD_THREAD_CREATE, 0, '<xml><thread name="%s" id="%s"/></xml>' % (frame_description, frame_id))
def _thread_to_xml(self, thread): """ thread information as XML """ name = pydevd_xml.make_valid_xml_value(thread.getName()) cmdText = '<thread name="%s" id="%s" />' % (quote(name), get_thread_id(thread)) return cmdText
def get_referrer_info(searched_obj): DEBUG = 0 if DEBUG: sys.stderr.write('Getting referrers info.\n') try: try: if searched_obj is None: ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_xml.var_to_xml( searched_obj, 'Skipping getting referrers for None', additional_in_xml=' id="%s"' % (id(searched_obj), ))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret obj_id = id(searched_obj) try: if DEBUG: sys.stderr.write('Getting referrers...\n') import gc referrers = gc.get_referrers(searched_obj) except: pydev_log.exception() ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_xml.var_to_xml( searched_obj, 'Exception raised while trying to get_referrers.', additional_in_xml=' id="%s"' % (id(searched_obj), ))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret if DEBUG: sys.stderr.write('Found %s referrers.\n' % (len(referrers), )) 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') if DEBUG: sys.stderr.write('Searching Referrers of obj with id="%s"\n' % (obj_id, )) ret.append( pydevd_xml.var_to_xml( searched_obj, 'Referrers of obj with id="%s"' % (obj_id, ))) ret.append('</for>\n') curr_frame = sys._getframe() all_objects = None for r in referrers: try: if r in ignore_frames: continue # Skip the references we may add ourselves except: pass # Ok: unhashable type checked... if r is referrers: continue if r is curr_frame.f_locals: continue r_type = type(r) r_id = str(id(r)) representation = str(r_type) found_as = '' if r_type == frame_type: if DEBUG: sys.stderr.write('Found frame referrer: %r\n' % (r, )) for key, val in r.f_locals.items(): if val is searched_obj: found_as = key break elif r_type == dict: if DEBUG: sys.stderr.write('Found dict referrer: %r\n' % (r, )) # 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 if DEBUG: sys.stderr.write(' Found as %r in dict\n' % (found_as, )) 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): if DEBUG: sys.stderr.write('Found tuple referrer: %r\n' % (r, )) for i, x in enumerate(r): if x is searched_obj: found_as = '%s[%s]' % (r_type.__name__, i) if DEBUG: sys.stderr.write( ' Found as %s in tuple: \n' % (found_as, )) break if found_as: if not isinstance(found_as, str): found_as = str(found_as) found_as = ' found_as="%s"' % ( pydevd_xml.make_valid_xml_value(found_as), ) ret.append( pydevd_xml.var_to_xml(r, representation, additional_in_xml=' id="%s"%s' % (r_id, found_as))) finally: if DEBUG: sys.stderr.write('Done searching for references.\n') # 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 except: pydev_log.exception() ret = ['<xml>\n'] ret.append('<for>\n') ret.append( pydevd_xml.var_to_xml(searched_obj, 'Error getting referrers for:', additional_in_xml=' id="%s"' % (id(searched_obj), ))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret ret.append('</xml>') ret = ''.join(ret) return ret
def get_referrer_info(searched_obj): DEBUG = 0 if DEBUG: sys.stderr.write('Getting referrers info.\n') try: try: if searched_obj is None: ret = ['<xml>\n'] ret.append('<for>\n') ret.append(pydevd_xml.var_to_xml( searched_obj, 'Skipping getting referrers for None', additional_in_xml=' id="%s"' % (id(searched_obj),))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret obj_id = id(searched_obj) try: if DEBUG: sys.stderr.write('Getting referrers...\n') import gc referrers = gc.get_referrers(searched_obj) except: traceback.print_exc() ret = ['<xml>\n'] ret.append('<for>\n') ret.append(pydevd_xml.var_to_xml( searched_obj, 'Exception raised while trying to get_referrers.', additional_in_xml=' id="%s"' % (id(searched_obj),))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret if DEBUG: sys.stderr.write('Found %s referrers.\n' % (len(referrers),)) 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') if DEBUG: sys.stderr.write('Searching Referrers of obj with id="%s"\n' % (obj_id,)) ret.append(pydevd_xml.var_to_xml( searched_obj, 'Referrers of obj with id="%s"' % (obj_id,))) ret.append('</for>\n') curr_frame = sys._getframe() all_objects = None for r in referrers: try: if r in ignore_frames: continue #Skip the references we may add ourselves except: pass #Ok: unhashable type checked... if r is referrers: continue if r is curr_frame.f_locals: continue r_type = type(r) r_id = str(id(r)) representation = str(r_type) found_as = '' if r_type == frame_type: if DEBUG: sys.stderr.write('Found frame referrer: %r\n' % (r,)) for key, val in r.f_locals.items(): if val is searched_obj: found_as = key break elif r_type == dict: if DEBUG: sys.stderr.write('Found dict referrer: %r\n' % (r,)) # 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 if DEBUG: sys.stderr.write(' Found as %r in dict\n' % (found_as,)) 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): if DEBUG: sys.stderr.write('Found tuple referrer: %r\n' % (r,)) for i, x in enumerate(r): if x is searched_obj: found_as = '%s[%s]' % (r_type.__name__, i) if DEBUG: sys.stderr.write(' Found as %s in tuple: \n' % (found_as,)) break if found_as: if not isinstance(found_as, str): found_as = str(found_as) found_as = ' found_as="%s"' % (pydevd_xml.make_valid_xml_value(found_as),) ret.append(pydevd_xml.var_to_xml( r, representation, additional_in_xml=' id="%s"%s' % (r_id, found_as))) finally: if DEBUG: sys.stderr.write('Done searching for references.\n') #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 except: traceback.print_exc() ret = ['<xml>\n'] ret.append('<for>\n') ret.append(pydevd_xml.var_to_xml( searched_obj, 'Error getting referrers for:', additional_in_xml=' id="%s"' % (id(searched_obj),))) ret.append('</for>\n') ret.append('</xml>') ret = ''.join(ret) return ret ret.append('</xml>') ret = ''.join(ret) return ret