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