added missing main control window
[pygdb.git] / DbgTerminal.py
1 #!/usr/bin/python
2 #shuber, 2008-06-04
3
4 __author__ = "shuber"
5
6
7 import gtk
8 import pty
9 import string
10 import time
11 import threading
12 import vte
13
14 import ClientIOTerminal
15
16
17
18 class DbgTerminal (vte.Terminal):
19
20 lastrow = 0
21 history = []
22
23
24 def __init__(self, clientCmd):
25
26 vte.Terminal.__init__(self)
27
28 #Start debugger
29 self.clientCmd = clientCmd
30 self.fork_command( self.getCommand(), self.getArgv())
31
32 #Open pseudo-terminal where to-be-debugged process reads/writes to
33 self.client_ptymaster, self.client_ptyslave = pty.openpty()
34 self.setPty(self.client_ptyslave)
35
36 #Set up terminal window and initialize debugger
37 self.connect("cursor-moved", self.contents_changed)
38 self.connect("child-exited", lambda *w: gtk.main_quit())
39
40
41 def contents_changed(self, term):
42 c,r = term.get_cursor_position()
43
44 if self.lastrow <= r:
45 text = self.get_text_range(self.lastrow,0,r,-1,lambda *w:True)
46
47 #Remove the incomplete line
48 if self.getHistoryLen()>0 and (len(self.history[-1])==0 or self.history[-1]!='\n') :
49 del self.history[-1]
50
51 #Get the lines and remove empty lines
52 lines = string.split(text, "\n")
53
54 #Remove last empty line...
55 if lines[-1] == "":
56 del lines[-1]
57
58 #Add lines to history
59 self.history += [l+"\n" for l in lines[:-1]]
60 self.history += [lines[-1]]
61 self.lastrow = r
62
63
64 def waitForNewline(self):
65 r = self.lastrow
66 while not self.lastrow > r:
67 gtk.main_iteration()
68
69 def getHistoryLen(self):
70 return len(self.history)
71
72 def waitForRx(self, rx, start=None):
73
74 if start == None:
75 start = self.getHistoryLen()
76 if start < 0:
77 start = 0
78
79 while True:
80 for no in range(start, self.getHistoryLen()):
81 line = self.history[no]
82 if rx.search(line):
83 return no, line
84
85 start = self.getHistoryLen()
86 gtk.main_iteration()
87
88
89 def getCommand(self):
90 return self.getArgv()[0];
91
92 def getArgv(self):
93 raise NotImplementedError()
94
95 def setPty(self, pty):
96 raise NotImplementedError()
97
98 def setRun(self):
99 raise NotImplementedError()
100
101 def setContinue(self):
102 raise NotImplementedError()
103
104 def setStepover(self):
105 raise NotImplementedError()
106
107 def setStepin(self):
108 raise NotImplementedError()
109
110 def setQuit(self):
111 raise NotImplementedError()
112
113 def setBreakpoint(self, file, lineno):
114 raise NotImplementedError()
115
116 def getExpression(self, expr):
117 raise NotImplementedError()
118
119 def getLastLine(self):
120 if len(self.history) == 0:
121 return None
122
123 return self.history[-1]
124
125 def feed_dbg(self, text):
126 self.feed_child(text)
127
128
129
130
131
132
133 class DbgWindow (gtk.Window):
134
135 clientIOWnd = None
136
137
138 def __init__(self, terminal):
139
140 #Set up some members
141 self.terminal = terminal
142
143 #Set up GTK stuff
144 gtk.Window.__init__(self)
145 self.connect("destroy", lambda *w: gtk.main_quit())
146
147 #Set title and add terminal
148 self.set_title("Debugger I/O")
149 self.terminal.history = []
150 self.terminal.history_length = 5
151 self.add(self.terminal)
152
153 #Show the window
154 self.show_all()
155
156 def toggleClientIOWindow(self):
157 if not self.clientIOWnd:
158 self.clientIOWnd = ClientIOTerminal.ClientIOWindow(self, \
159 self.terminal.client_ptymaster)
160 else:
161 self.clientIOWnd.destroy()
162 self.clientIOWnd = None
163
164 def isClientIOWindowExisting(self):
165 return self.clientIOWnd != None
166
167
168
169
170 def launchDebugger(wnd, term):
171
172 wnd.toggleClientIOWindow()
173
174 term.setBreakpoint("main.cpp", 15)
175 term.setRun()
176 res = term.getExpression("a")
177 print "Result = ", res
178
179 term.setQuit()
180
181
182