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