7c282360501232a26a97acedbceeec10b286e50e
[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
34 his = self.getHistoryLen()
35 argv = string.join(string.split(self.clientCmd)[1:])
36 self.feed_child("run " + argv + "\n")
37 return self.waitForActivation(his)
38
39 def setContinue(self):
40 his = self.getHistoryLen()
41 self.feed_child("cont\n");
42 return self.waitForActivation(his)
43
44 def setStepover(self):
45 his = self.getHistoryLen()
46 self.feed_child("next\n");
47 return self.waitForActivation(his)
48
49 def setStepin(self):
50 his = self.getHistoryLen()
51 self.feed_child("step\n");
52 return self.waitForActivation(his)
53
54 def setQuit(self):
55 self.feed_child("quit\n")
56 self.waitForNewline()
57 self.feed_child("y\n");
58
59 def setBreakpoint(self, file, lineno, condition=None):
60 his = self.getHistoryLen()
61 if condition==None:
62 self.feed_child("break %s:%s\n" % (file, str(lineno)))
63 else:
64 self.feed_child("break %s:%s if %s\n" % \
65 (file, str(lineno), condition))
66
67 rx = re.compile("^Breakpoint |^No |^\(gdb\) ")
68 his, response = self.waitForRx(rx, his)
69
70 answer = None
71
72 if response[0:10] == "Breakpoint":
73 answer = string.split(response)[1].strip()
74
75 #Wants an answer: "No"
76 if response[0:14] == "No source file":
77 self.feed_child("n\n");
78
79 #Wait again for (gdb)...
80 self.waitForActivation(his)
81
82 return answer
83
84
85 def delBreakpoint(self, breakpoint):
86 self.feed_child("del breakpoint %s\n" % (breakpoint,))
87
88 def getBreakpoints(self):
89 starthis = self.getHistoryLen()
90 self.feed_child("info breakpoints\n")
91
92 rx = re.compile("^\(gdb\) ")
93 endhis, response = self.waitForRx(rx, starthis)
94
95
96 rxbp = re.compile("^\d+\s+breakpoint")
97 rxpos = re.compile("^.* at \S+:\d+$")
98 rxcond = re.compile("^\tstop only if")
99
100 bpnts = []
101 bplines = self.history[starthis+1:endhis]
102 i = 0
103
104 #Parse the resulting lines
105 while i<len(bplines):
106 line = bplines[i]
107
108 if not rxbp.search(line):
109 i += 1
110 continue
111
112 #Get number of breakpoint
113 no = string.split(line)[0]
114
115 #This line does not contain the file!
116 if not rxpos.search(line):
117 i += 1
118 line = bplines[i]
119
120 pos = string.split(line)[-1]
121 [file,lineno] = string.split(pos,":")
122 cond = None
123
124 if i+1<len(bplines) and rxcond.search(bplines[i+1]):
125 i +=1
126 line = bplines[i]
127 pre,cond = string.split(line,"if")
128 cond = cond.strip()
129
130 bpnts += [[no, file, lineno, cond]]
131 i += 1
132
133 return bpnts
134
135
136
137 def getExpression(self, expr):
138
139 his = self.getHistoryLen()
140 self.feed_child("print " + expr + "\n")
141
142 rx = re.compile("^\(gdb\)")
143 his, response = self.waitForRx(rx, his)
144
145 answer = self.history[his-1]
146
147 if len(string.split(answer, "=")) == 1:
148 return answer.strip()
149
150 split = string.split(answer, "=")
151 return string.join(split[1:], "=").strip()
152
153
154 def waitForActivation(self, starthis):
155
156 self.setActive(False)
157 rx = re.compile("^\(gdb\)")
158 endhis, response = self.waitForRx(rx,starthis)
159 self.setActive(True)
160
161 for his in reversed(range(starthis+1,endhis)):
162 if self.history[his][0:2]=="\x1a\x1a":
163 tuples = string.split(self.history[his][2:], ":")
164 return tuples[0:2]
165
166 return None
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