some code beautifying: __waitForPrompt
[pygdb.git] / GdbTerminal.py
1 #!/usr/bin/python
2 #shuber, 2008-06-04
3
4 __author__ = "shuber"
5
6
7 import gtk
8 import os
9 import re
10 import string
11 import sys
12 import time
13
14 import DbgTerminal
15
16
17 class GdbTerminal (DbgTerminal.DbgTerminal):
18
19
20 def __init__(self, clientCmd):
21 DbgTerminal.DbgTerminal.__init__(self, clientCmd)
22
23 def getArgv(self):
24 return ["gdb", "--fullname", string.split(self.clientCmd)[0]]
25
26 def setPty(self, pty):
27 ttyname = os.ttyname(pty)
28 his = self.getHistoryLen()
29 self.feed_child("set inferior-tty %s\n" % (ttyname,))
30 self.waitForActivation(his)
31
32 def setRun(self):
33 his = self.getHistoryLen()
34 argv = string.join(string.split(self.clientCmd)[1:])
35 self.feed_child("run " + argv + "\n")
36 return self.waitForActivation(his)
37
38 def setContinue(self):
39 his = self.getHistoryLen()
40 self.feed_child("cont\n");
41 return self.waitForActivation(his)
42
43 def setStepover(self):
44 his = self.getHistoryLen()
45 self.feed_child("next\n");
46 return self.waitForActivation(his)
47
48 def setStepin(self):
49 his = self.getHistoryLen()
50 self.feed_child("step\n");
51 return self.waitForActivation(his)
52
53 def setQuit(self):
54 self.feed_child("quit\n")
55 self.waitForNewline()
56 self.feed_child("y\n");
57
58 def setBreakpoint(self, file, lineno, condition=None):
59 his = self.getHistoryLen()
60 if condition==None:
61 self.feed_child("break %s:%s\n" % (file, str(lineno)))
62 else:
63 self.feed_child("break %s:%s if %s\n" % \
64 (file, str(lineno), condition))
65
66 rx = re.compile("^Breakpoint |^No |^\(gdb\) ")
67 his, response = self.waitForRx(rx, his)
68
69 answer = None
70
71 if response[0:10] == "Breakpoint":
72 answer = string.split(response)[1].strip()
73
74 #Wants an answer: "No"
75 if response[0:14] == "No source file":
76 self.feed_child("n\n");
77
78 #Wait again for (gdb)...
79 self.waitForActivation(his)
80
81 return answer
82
83
84 def delBreakpoint(self, breakpoint):
85 his = self.getHistoryLen()
86 self.feed_child("del breakpoint %s\n" % (breakpoint,))
87 self.waitForActivation(his)
88
89 def getBreakpoints(self):
90 starthis = self.getHistoryLen()
91 self.feed_child("info breakpoints\n")
92 endhis, response = self.__waitForPrompt(starthis)
93
94 rxbp = re.compile("^\d+\s+breakpoint")
95 rxpos = re.compile("^.* at \S+:\d+$")
96 rxcond = re.compile("^\tstop only if")
97
98 bpnts = []
99 bplines = self.history[starthis+1:endhis]
100 i = 0
101
102 #Parse the resulting lines
103 while i<len(bplines):
104 line = bplines[i]
105
106 if not rxbp.search(line):
107 i += 1
108 continue
109
110 #Get number of breakpoint
111 no = string.split(line)[0]
112
113 #This line does not contain the file!
114 if not rxpos.search(line):
115 i += 1
116 line = bplines[i]
117
118 pos = string.split(line)[-1]
119 [file,lineno] = string.split(pos,":")
120 cond = None
121
122 if i+1<len(bplines) and rxcond.search(bplines[i+1]):
123 i +=1
124 line = bplines[i]
125 pre,cond = string.split(line,"if")
126 cond = cond.strip()
127
128 bpnts += [[no, file, lineno, cond]]
129 i += 1
130
131 return bpnts
132
133
134
135 def getExpression(self, expr):
136
137 his = self.getHistoryLen()
138 self.feed_child("print " + expr + "\n")
139 his, response = self.__waitForPrompt(his)
140 answer = self.history[his-1]
141
142 if len(string.split(answer, "=")) == 1:
143 return answer.strip()
144
145 split = string.split(answer, "=")
146 return string.join(split[1:], "=").strip()
147
148
149 def waitForActivation(self, starthis):
150
151 self.setActive(False)
152 endhis, response = self.__waitForPrompt(starthis)
153 self.setActive(True)
154
155 for his in reversed(range(starthis+1,endhis)):
156 if self.history[his][0:2]=="\x1a\x1a":
157 tuples = string.split(self.history[his][2:], ":")
158 return tuples[0:2]
159
160 return None
161
162
163 def __waitForPrompt(self, his):
164 rx = re.compile("^\(gdb\) ")
165 return self.waitForRx(rx,his)
166
167
168
169
170 if __name__ == "__main__":
171
172
173 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
174 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
175
176 gtk.main()
177
178
179
180