fixing endless loop 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 setRun(self):
27 argv = string.join(string.split(self.clientCmd)[1:])
28 self.feed_child("run " + argv + "\n")
29
30 def setContinue(self):
31 self.feed_child("cont\n");
32
33 def setStepover(self):
34 self.feed_child("next\n");
35
36 def setStepin(self):
37 self.feed_child("step\n");
38
39 def setStepout(self):
40 bt = self.getBacktrace()
41
42 if len(bt) < 2:
43 self.setContinue()
44
45 else:
46 #Get the second line
47 sec = bt[1]
48
49 #Check if it is a backtrace line
50 if re.compile("^#1\s+.*\s\S+:\d+$").search(sec):
51 pos = string.split(sec)[-1]
52 self.feed_child("advance %s\n" % pos)
53
54
55 def setQuit(self):
56 self.feed_child("quit\n")
57 self.waitForNewline()
58 self.feed_child("y\n");
59
60 def setPty(self, pty):
61 ttyname = os.ttyname(pty)
62 self.__getAnswerFromCmd("set inferior-tty %s\n" % (ttyname,))
63
64 def setBreakpoint(self, file, lineno, condition=None):
65 his = self.getHistoryLen()
66 if condition==None:
67 self.feed_child("break %s:%s\n" % (file, str(lineno)))
68 else:
69 self.feed_child("break %s:%s if %s\n" % \
70 (file, str(lineno), condition))
71
72 rx = "^Breakpoint |^No |^\(gdb\) "
73 his, response = self.waitForRx(rx, his)
74
75 answer = None
76
77 if response[0:10] == "Breakpoint":
78 answer = string.split(response)[1].strip()
79
80 #Wants an answer: "No"
81 if response[0:14] == "No source file":
82 self.feed_child("n\n");
83
84 #Wait again for (gdb)...
85 self.waitForPrompt(his)
86
87 return answer
88
89
90 def delBreakpoint(self, breakpoint):
91 self.__getAnswerFromCmd("del breakpoint %s\n" % (breakpoint,))
92
93
94 def getBreakpoints(self):
95 bplines = self.__getAnswerFromCmd("info breakpoints\n")
96
97 rxbp = re.compile("^\d+\s+breakpoint")
98 rxpos = re.compile("^.*at \S+:\d+$")
99 rxcond = re.compile("^\s+stop only if")
100
101 bpnts = []
102 i = 1
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 #Check for next line...
117 if not rxpos.search(line):
118 i += 1
119 if not rxpos.search(line):
120 i += 1
121 continue
122
123 pos = string.split(line)[-1]
124 [file,lineno] = string.split(pos,":")
125 cond = None
126
127 #Look for conditions
128 if i+1<len(bplines) and rxcond.search(bplines[i+1]):
129 i +=1
130 line = bplines[i]
131 cond = string.join(string.split(line," if ")[1:], " if ")
132 cond = cond.strip()
133
134 bpnts += [[no, file, lineno, cond]]
135 i += 1
136
137 return bpnts
138
139
140
141 def getExpression(self, expr):
142 answer = self.__getAnswerFromCmd("print " + expr + "\n")
143 answer = answer[-1]
144
145 if len(string.split(answer, "=")) == 1:
146 return answer.strip()
147
148 split = string.split(answer, "=")
149 return string.join(split[1:], "=").strip()
150
151
152 def listCodeSnippet(self):
153 return self.__getAnswerFromCmd("list\n")
154
155 def getBacktrace(self):
156 return self.__getAnswerFromCmd("bt\n")
157
158 def waitForPrompt(self, his):
159 rx = "^\(gdb\)"
160 return self.waitForRx(rx,his)
161
162 def __getAnswerFromCmd(self, cmd):
163 starthis = self.getHistoryLen()
164 self.feed_child(cmd)
165 endhis, response = self.waitForPrompt(starthis)
166
167 return self.history[starthis:endhis]
168
169
170 def testForActivity(self, his):
171 """Test whether debugger got active again"""
172
173 line = self.history[his]
174
175 if string.find(line, "\x1a\x1a") == 0:
176 tuples = string.split(line[2:], ":")
177 tuples[1] = int(tuples[1])
178 return "break", [tuples[0], int(tuples[1])]
179
180 if string.find(line, "Program exited") == 0:
181 code = string.split(line)[-1]
182
183 codeno = 0
184
185 #Parse the octal number
186 if code[0] == "O":
187 code = code[1:-1]
188 for c in code:
189 codeno = codeno*8 + int(c)
190
191 return "exited", codeno
192
193 return None
194
195
196 def testForInactivity(self, his):
197 """Test whether debugger got inactive"""
198 line = self.history[his]
199
200 if string.find(line, "Starting program:") == 0:
201 prog = string.join( string.split(line)[1:])
202 return "started", prog
203
204 if string.find(line, "Continuing.") == 0:
205 return "continued", None
206
207 if string.find(line, "\x1a\x1a") == 0:
208 rxcont = re.compile("^\(gdb\)\s+(cont|step|next|stepi|nexti|advance)")
209
210 if rxcont.search(self.history[his-1]):
211 return "stepped", None
212 if rxcont.search(self.history[his-2]):
213 return "stepped", None
214
215 return None
216
217
218
219
220
221
222
223
224 if __name__ == "__main__":
225
226
227 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
228 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
229
230 gtk.main()
231
232
233
234