Overwrite breakpoints when launching pygdb from vim, updating vim also after quit
[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 setQuit(self):
40 self.feed_child("quit\n")
41 self.waitForNewline()
42 self.feed_child("y\n");
43
44 def setPty(self, pty):
45 ttyname = os.ttyname(pty)
46 len = self.getHistoryLen()
47 self.feed_child("set inferior-tty %s\n" % (ttyname,))
48 self.waitForPrompt(len)
49
50 def setBreakpoint(self, file, lineno, condition=None):
51 his = self.getHistoryLen()
52 if condition==None:
53 self.feed_child("break %s:%s\n" % (file, str(lineno)))
54 else:
55 self.feed_child("break %s:%s if %s\n" % \
56 (file, str(lineno), condition))
57
58 rx = "^Breakpoint |^No |^\(gdb\) "
59 his, response = self.waitForRx(rx, his)
60
61 answer = None
62
63 if response[0:10] == "Breakpoint":
64 answer = string.split(response)[1].strip()
65
66 #Wants an answer: "No"
67 if response[0:14] == "No source file":
68 self.feed_child("n\n");
69
70 #Wait again for (gdb)...
71 self.waitForPrompt(his)
72
73 return answer
74
75
76 def delBreakpoint(self, breakpoint):
77 his = self.getHistoryLen()
78 self.feed_child("del breakpoint %s\n" % (breakpoint,))
79 self.waitForPrompt(his)
80
81 def getBreakpoints(self):
82 starthis = self.getHistoryLen()
83 self.feed_child("info breakpoints\n")
84 endhis, response = self.waitForPrompt(starthis)
85
86 rxbp = re.compile("^\d+\s+breakpoint")
87 rxpos = re.compile("^.* at \S+:\d+$")
88 rxcond = re.compile("^\tstop only if")
89
90 bpnts = []
91 bplines = self.history[starthis+1:endhis]
92 i = 0
93
94 #Parse the resulting lines
95 while i<len(bplines):
96 line = bplines[i]
97
98 if not rxbp.search(line):
99 i += 1
100 continue
101
102 #Get number of breakpoint
103 no = string.split(line)[0]
104
105 #This line does not contain the file!
106 if not rxpos.search(line):
107 continue
108
109 pos = string.split(line)[-1]
110 [file,lineno] = string.split(pos,":")
111 cond = None
112
113 if i+1<len(bplines) and rxcond.search(bplines[i+1]):
114 i +=1
115 line = bplines[i]
116 cond = string.join(string.split(line," if ")[1:], " if ")
117 cond = cond.strip()
118
119 bpnts += [[no, file, lineno, cond]]
120 i += 1
121
122 return bpnts
123
124
125
126 def getExpression(self, expr):
127
128 his = self.getHistoryLen()
129 self.feed_child("print " + expr + "\n")
130 his, response = self.waitForPrompt(his)
131 answer = self.history[his-1]
132
133 if len(string.split(answer, "=")) == 1:
134 return answer.strip()
135
136 split = string.split(answer, "=")
137 return string.join(split[1:], "=").strip()
138
139
140 def listCodeSnippet(self):
141 starthis = self.getHistoryLen()
142 self.feed_child("list\n")
143 endhis, response = self.waitForPrompt(starthis)
144
145 text = string.join(self.history[starthis:endhis], "\n")
146 return text
147
148
149 def waitForPrompt(self, his):
150 rx = "^\(gdb\)"
151 return self.waitForRx(rx,his)
152
153
154 def testForActivity(self, his):
155 """Test whether debugger got active again"""
156
157 line = self.history[his]
158
159 if string.find(line, "\x1a\x1a") == 0:
160 tuples = string.split(line[2:], ":")
161 tuples[1] = int(tuples[1])
162 return "break", [tuples[0], int(tuples[1])]
163
164 if string.find(line, "Program exited") == 0:
165 code = string.split(line)[-1]
166
167 codeno = 0
168
169 #Parse the octal number
170 if code[0] == "O":
171 code = code[1:-1]
172 for c in code:
173 codeno = codeno*8 + int(c)
174
175 return "exited", codeno
176
177 return None
178
179
180 def testForInactivity(self, his):
181 """Test whether debugger got inactive"""
182 line = self.history[his]
183
184 if string.find(line, "Starting program:") == 0:
185 prog = string.join( string.split(line)[1:])
186 return "started", prog
187
188 if string.find(line, "Continuing.") == 0:
189 return "continued", None
190
191 if string.find(line, "\x1a\x1a") == 0:
192 rxcont = re.compile("^\(gdb\)\s+(cont|step|next|stepi|nexti)")
193
194 if rxcont.search(self.history[his-1]):
195 return "stepped", None
196
197 return None
198
199
200
201
202
203
204
205
206 if __name__ == "__main__":
207
208
209 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
210 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
211
212 gtk.main()
213
214
215
216