X-Git-Url: https://git.sthu.org/?p=pygdb.git;a=blobdiff_plain;f=DbgTerminal.py;h=89659d035628a9fe7a450bffc9e603ff905eceb7;hp=88064dbd9df7ab5c06f0a5507fa98e099468addf;hb=1094f7f4581a9c0074294004bbd8c934593a54d5;hpb=65010f5074ddf1f3ccb7424aaec7ab78c2ede09d diff --git a/DbgTerminal.py b/DbgTerminal.py old mode 100755 new mode 100644 index 88064db..89659d0 --- a/DbgTerminal.py +++ b/DbgTerminal.py @@ -1,262 +1,180 @@ #!/usr/bin/python +#shuber, 2008-06-04 +__author__ = "shuber" -import select + +import gtk +import pango +import pty import string -import sys -import thread -import threading import time -import os -import pty -import Queue - +import threading +import vte -class DbgTerminal: +import ClientIOTerminal - #Reply cache from debugger - dbgreplyqueue = Queue.Queue() - def __init__ (self, binary, gdbReadCallback, childReadCallback): +class DbgTerminal (vte.Terminal): - #Set some members - self.gdbReadCallback = gdbReadCallback - self.childReadCallback = childReadCallback - self.binary = binary - self.stopped = False + isactive = True + lastrow = 0 + history = [] - #Connect to sub-process - self.__connect() - def __connect( self ): + def __init__(self, clientCmd): - #This function handles readings from the debugger - def gdbCb(str): - self.gdbReadCallback(str) - self.dbgreplyqueue.put(str) + vte.Terminal.__init__(self) + #Start debugger + self.clientCmd = clientCmd #Open pseudo-terminal where to-be-debugged process reads/writes to - self.ptymaster, self.ptyslave = pty.openpty() - self.childout = os.fdopen(self.ptymaster, "r", 0) - self.childin = os.fdopen(self.ptymaster, "w", 0) + self.client_ptymaster, self.client_ptyslave = pty.openpty() - #Call gdb and get in- and out-streams to/from gdb - cmd = self.getCommand(self.binary) - self.gdbin, self.gdbout = os.popen4( cmd, bufsize=0) + #Set up terminal window and initialize debugger + self.connect("cursor-moved", self.contents_changed) + self.connect("child-exited", lambda *w: gtk.main_quit()) - #Set up a reading thread to gdb output - self.gdbReadThread = self.ReadThread(self.gdbout, gdbCb) - self.gdbReadThread.start() + #font description + fontdesc = pango.FontDescription("monospace 9") + self.set_font(fontdesc) - #Set up a reading thread to childs output - self.childReadThread = self.ReadThread(self.childout, self.childReadCallback) - self.childReadThread.start() - #Set up tty gdb-childs process - self.sendSetTTY(os.ttyname(self.ptyslave)) + def initialize(self): + self.fork_command( self.getCommand(), self.getArgv()) + self.setPty(self.client_ptyslave) + self.waitForActivation() - - def getDbgReply(self): - raise NotImplementedError() - - - def iseof( self ): - """Check if terminal is closed already""" - return self.gdbReadThread.eventFin.isSet() + def contents_changed(self, term): + c,r = term.get_cursor_position() - def stop( self ): + if self.lastrow <= r: + text = self.get_text_range(self.lastrow,0,r,-1,lambda *w:True) - if not self.stopped: + #Remove the incomplete line + if self.getHistoryLen()>0 and (len(self.history[-1])==0 or self.history[-1]!='\n') : + del self.history[-1] - self.stopped = True + #Get the lines and remove empty lines + lines = string.split(text, "\n") - #Finish the reading-thread - self.gdbReadThread.fin = True - self.childReadThread.fin = True - self.gdbReadThread.eventFin.wait(1) - self.childReadThread.eventFin.wait(1) + #Remove last empty line... + if lines[-1] == "": + del lines[-1] + #Add lines to history + self.history += [l+"\n" for l in lines[:-1]] + self.history += [lines[-1]] + self.lastrow = r - def getCommand( self, binary ): - """Get the command to execute""" - raise NotImplementedError() - - def sendBreak(self, file, lineno): - raise NotImplementedError() - - def sendContinue(self): - raise NotImplementedError() - - def sendRun(self): - raise NotImplementedError() - - def sendInspectVar(self, var): - raise NotImplementedError() - - def sendInspectExpr(self, expr): - raise NotImplementedError() - - def sendSetTTY(self, ttyname): - raise NotImplementedError() - - def sendQuit(self): - raise NotImplementedError() + def waitForNewline(self): + r = self.lastrow + while not self.lastrow > r: + gtk.main_iteration() - class ReadThread (threading.Thread): - """Thread which reads from sub-process output""" + def getHistoryLen(self): + return len(self.history) - def __init__( self, stream, callback, sleep=0.1): - self.stream = stream - self.fin = False - self.callback = callback - self.sleep = sleep - self.eventFin = threading.Event() - threading.Thread.__init__(self) + def waitForRx(self, rx, start=None): - def run(self): - - try: - while True: - #Wait until data is available - rlist, wlist, xlist = select.select([self.stream], [], [], self.sleep) - - #If we should finish, finish - if self.fin: - break - - #Got new data - if len(rlist) > 0: - fd = rlist[0] - str = fd.read(1) - #Call callbacks - self.callback(str) - except: - pass - - #Set the finished event - self.eventFin.set() - - - - -class GdbTerminal (DbgTerminal): - - gdbreply = "" + if start == None: + start = self.getHistoryLen() + if start < 0: + start = 0 + while True: + for no in range(start, self.getHistoryLen()): + line = self.history[no] + if rx.search(line): + return no, line - def getCommand( self, binary ): - return "gdb --fullname %s" % (binary,) + start = self.getHistoryLen() + gtk.main_iteration() - def sendBreak(self, file, lineno): - self.gdbin.write("break %s:%d\n" % (file, lineno)) - def sendContinue(self): - self.gdbin.write("cont\n") + def getCommand(self): + return self.getArgv()[0]; - def sendRun(self): - self.gdbin.write("run\n") + def getArgv(self): + raise NotImplementedError() - def sendInspectVar(self, var): - self.sendInspectExpr(var) + def setPty(self, pty): + raise NotImplementedError() - def sendInspectExpr(self, expr): - self.gdbin.write("print %s\n" % (expr,)) + def setRun(self): + raise NotImplementedError() - def sendSetTTY(self, ttyname): - self.gdbin.write("set inferior-tty %s\n" % (ttyname,)) + def setContinue(self): + raise NotImplementedError() - def sendQuit(self): - self.gdbin.write("quit\n") - DbgTerminal.stop(self) + def setStepover(self): + raise NotImplementedError() - def getDbgReply(self, timeout=None): + def setStepin(self): + raise NotImplementedError() - while True: - splits = self.gdbreply.split("\n") + def setQuit(self): + raise NotImplementedError() - #Need more data: If there is a single (gdb) entry, then - #there are at least two splits - if len(splits) <= 1: - try: - self.gdbreply += self.dbgreplyqueue.get(True, timeout) - except Queue.Empty: - return None - #Yeah there is data! - else: - self.gdbreply = string.join(splits[1:], "(gdb)") - return string.strip(splits[0]) + def setBreakpoint(self, file, lineno, condition=False): + raise NotImplementedError() - def flushDbgReply(self): + def delBreakpoint(self, breakpoint): + raise NotImplementedError() - try: - self.gdbreply = "" - #Remove all elements from queue - while True: - self.dbgreplyqueue.get(False) - except Queue.Empty: - pass + def getExpression(self, expr): + raise NotImplementedError() -if __name__ == "__main__": + def waitForActivation(self, his): + raise NotImplementedError() - def tostdout(str): - sys.stdout.write(str) - sys.stdout.flush() + def setActive(self, isactive): + self.isactive = isactive - try: + def isActive(self): + return self.isactive - term = GdbTerminal( "./main", tostdout, tostdout) - term.sendBreak("main.cpp", 13) - term.sendBreak("main.cpp", 14) - term.sendRun() - term.childin.write("1\n"); - term.childin.write("2\n"); + + - time.sleep(0.2) - term.flushDbgReply() - term.sendInspectVar("a+b") - term.sendContinue() - term.sendInspectVar("a+b") - term.sendContinue() +class DbgWindow (gtk.Window): - time.sleep(1) + clientIOWnd = None - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) - print "reply >>>", term.getDbgReply(1) + def __init__(self, terminal): - while not term.iseof(): + #Set up some members + self.terminal = terminal - cmd = sys.stdin.readline() + #Set up GTK stuff + gtk.Window.__init__(self) + self.connect("destroy", lambda *w: gtk.main_quit()) - if term.iseof(): - break + #Set title and add terminal + self.set_title("Debugger I/O") + self.terminal.history = [] + self.terminal.history_length = 5 + self.add(self.terminal) - if cmd == "quit\n": - term.sendQuit() - else: - term.gdbin.write(cmd) + #Show the window + self.show_all() - except Exception, e: - print e - except: - pass + def toggleClientIOWindow(self): + if not self.clientIOWnd: + self.clientIOWnd = ClientIOTerminal.ClientIOWindow(self, \ + self.terminal.client_ptymaster) + else: + self.clientIOWnd.destroy() + self.clientIOWnd = None + def isClientIOWindowExisting(self): + return self.clientIOWnd != None - print "stopping..." - term.stop() -