X-Git-Url: https://git.sthu.org/?a=blobdiff_plain;f=DbgTerminal.py;h=7c494458000fb40005486ea9f8b85f64849ec4ee;hb=9a8e5a643a533e00a2c8fdd6f03ea7b7a5e5d649;hp=cef0d27e464582f17837f27c3bbfd30fa604d633;hpb=f61e9c283bfbcca31062ae924778f577249a303f;p=pygdb.git diff --git a/DbgTerminal.py b/DbgTerminal.py old mode 100755 new mode 100644 index cef0d27..7c49445 --- a/DbgTerminal.py +++ b/DbgTerminal.py @@ -1,205 +1,195 @@ #!/usr/bin/python +#shuber, 2008-06-04 +__author__ = "shuber" -import select -import sys -import thread -import threading -import time -import os + +import gtk import pty +import string +import time +import threading +import vte + +import ClientIOTerminal -class DbgTerminal: - 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): + vte.Terminal.__init__(self) + + #Start debugger + self.clientCmd = clientCmd + self.fork_command( self.getCommand(), self.getArgv()) #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() + self.setPty(self.client_ptyslave) - #Open the subprocess and get in- and out-streams - 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, self.gdbReadCallback) - self.gdbReadThread.start() + def contents_changed(self, term): + c,r = term.get_cursor_position() - #Set up a reading thread to gdb output - self.childReadThread = self.ReadThread(self.childout, self.childReadCallback) - self.childReadThread.start() + if self.lastrow <= r: + text = self.get_text_range(self.lastrow,0,r,-1,lambda *w:True) - #Set up tty gdb-childs process - time.sleep(0.1) - self.sendSetTTY(os.ttyname(self.ptyslave)) - time.sleep(0.1) + #Remove the incomplete line + if self.getHistoryLen()>0 and (len(self.history[-1])==0 or self.history[-1]!='\n') : + del self.history[-1] + #Get the lines and remove empty lines + lines = string.split(text, "\n") + #Remove last empty line... + if lines[-1] == "": + del lines[-1] - def getCommand( self, binary ): - """Get the command to execute""" - raise NotImplementedError() + #Add lines to history + self.history += [l+"\n" for l in lines[:-1]] + self.history += [lines[-1]] + self.lastrow = r - def iseof( self ): - """Check if terminal is closed already""" + def waitForNewline(self): + r = self.lastrow + while not self.lastrow > r: + gtk.main_iteration() - return self.gdbReadThread.eventFin.isSet() + def getHistoryLen(self): + return len(self.history) + def waitForRx(self, rx, start=None): - def stop( self ): + if start == None: + start = self.getHistoryLen() + if start < 0: + start = 0 - if not self.stopped: + while True: + for no in range(start, self.getHistoryLen()): + line = self.history[no] + if rx.search(line): + return no, line - self.stopped = True + start = self.getHistoryLen() + gtk.main_iteration() - #Finish the reading-thread - self.gdbReadThread.fin = True - self.childReadThread.fin = True - self.gdbReadThread.eventFin.wait(1) - self.childReadThread.eventFin.wait(1) + def getCommand(self): + return self.getArgv()[0]; - def sendBreak(self, file, lineno): + def getArgv(self): raise NotImplementedError() - def sendContinue(self): - raise NotImplementedError() - - def sendRun(self): + def setPty(self, pty): raise NotImplementedError() - def sendInspectVar(self, var): + def setRun(self): raise NotImplementedError() - def sendInspectExpr(self, expr): + def setContinue(self): raise NotImplementedError() - def sendSetTTY(self, ttyname): - raise NotImplementedError() - - def sendQuit(self): + def setStepover(self): raise NotImplementedError() + def setStepin(self): + raise NotImplementedError() - class ReadThread (threading.Thread): - """Thread which reads from sub-process output""" - - 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 run(self): + def setQuit(self): + raise NotImplementedError() - try: - while True: - #Wait until data is available - rlist, wlist, xlist = select.select([self.stream], [], [], self.sleep) + def setBreakpoint(self, file, lineno, condition=False): + raise NotImplementedError() - #If we should finish, finish - if self.fin: - break + def delBreakpoint(self, breakpoint): + raise NotImplementedError() - #Got new data - if len(rlist) > 0: - fd = rlist[0] - str = fd.read(1) - #Call callbacks - self.callback(str) - except: - pass + def getExpression(self, expr): + raise NotImplementedError() - #Set the finished event - self.eventFin.set() + def waitForActivation(self, his): + raise NotImplementedError() + def setActive(self, isactive): + self.isactive = isactive + def isActive(self): + return self.isactive + def getLastLine(self): + if len(self.history) == 0: + return None -class GdbTerminal (DbgTerminal): + return self.history[-1] - def getCommand( self, binary ): - return "gdb --fullname %s" % (binary,) + def feed_dbg(self, text): + self.feed_child(text) - def sendBreak(self, file, lineno): - self.gdbin.write("break %s:%d\n" % (file, lineno)) + + - def sendContinue(self): - self.gdbin.write("cont\n") - def sendRun(self): - self.gdbin.write("run\n") - def sendInspectVar(self, var): - self.sendInspectExpr(var) +class DbgWindow (gtk.Window): - def sendInspectExpr(self, expr): - self.gdbin.write("print %s\n" % (expr,)) + clientIOWnd = None - def sendSetTTY(self, ttyname): - self.gdbin.write("set inferior-tty %s\n" % (ttyname,)) - def sendQuit(self): - self.gdbin.write("quit\n") - DbgTerminal.stop(self) + def __init__(self, terminal): + #Set up some members + self.terminal = terminal + #Set up GTK stuff + gtk.Window.__init__(self) + self.connect("destroy", lambda *w: gtk.main_quit()) -if __name__ == "__main__": + #Set title and add terminal + self.set_title("Debugger I/O") + self.terminal.history = [] + self.terminal.history_length = 5 + self.add(self.terminal) - def tostdout(str): - sys.stdout.write(str) - sys.stdout.flush() + #Show the window + self.show_all() - try: + def toggleClientIOWindow(self): + if not self.clientIOWnd: + self.clientIOWnd = ClientIOTerminal.ClientIOWindow(self, \ + self.terminal.client_ptymaster) + else: + self.clientIOWnd.destroy() + self.clientIOWnd = None - term = GdbTerminal( "./main", tostdout, tostdout) - term.sendBreak("main.cpp", 13) - term.sendRun() - term.childin.write("1\n"); - term.childin.write("2\n"); - term.sendInspectVar("a+b") - term.sendContinue() + def isClientIOWindowExisting(self): + return self.clientIOWnd != None - while not term.iseof(): - cmd = sys.stdin.readline() - if term.iseof(): - break +def launchDebugger(wnd, term): - if cmd == "quit\n": - term.sendQuit() - else: - term.gdbin.write(cmd) + wnd.toggleClientIOWindow() - except Exception, e: - print e - except: - pass + term.setBreakpoint("main.cpp", 15) + term.setRun() + res = term.getExpression("a") + print "Result = ", res + term.setQuit() - print "stopping..." - term.stop() -