pygdb now saves absolute breakpoint paths
[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 i += 1
108 line = bplines[i]
109
110 pos = string.split(line)[-1]
111 [file,lineno] = string.split(pos,":")
112 cond = None
113
114 if i+1<len(bplines) and rxcond.search(bplines[i+1]):
115 i +=1
116 line = bplines[i]
117 pre,cond = string.split(line,"if")
118 cond = cond.strip()
119
120 bpnts += [[no, file, lineno, cond]]
121 i += 1
122
123 return bpnts
124
125
126
127 def getExpression(self, expr):
128
129 his = self.getHistoryLen()
130 self.feed_child("print " + expr + "\n")
131 his, response = self.waitForPrompt(his)
132 answer = self.history[his-1]
133
134 if len(string.split(answer, "=")) == 1:
135 return answer.strip()
136
137 split = string.split(answer, "=")
138 return string.join(split[1:], "=").strip()
139
140
141 def listCodeSnippet(self):
142 starthis = self.getHistoryLen()
143 self.feed_child("list\n")
144 endhis, response = self.waitForPrompt(starthis)
145
146 text = string.join(self.history[starthis:endhis], "\n")
147 return text
148
149
150 def waitForPrompt(self, his):
151 rx = "^\(gdb\)"
152 return self.waitForRx(rx,his)
153
154
155 def testForActivity(self, his):
156 """Test whether debugger got active again"""
157
158 line = self.history[his]
159
160 if string.find(line, "\x1a\x1a") == 0:
161 tuples = string.split(line[2:], ":")
162 tuples[1] = int(tuples[1])
163 return "break", [tuples[0], int(tuples[1])]
164
165 if string.find(line, "Program exited") == 0:
166 code = string.split(line)[-1]
167
168 codeno = 0
169
170 #Parse the octal number
171 if code[0] == "O":
172 code = code[1:-1]
173 for c in code:
174 codeno = codeno*8 + int(c)
175
176 return "exited", codeno
177
178 return None
179
180
181 def testForInactivity(self, his):
182 """Test whether debugger got inactive"""
183 line = self.history[his]
184
185 if string.find(line, "Starting program:") == 0:
186 prog = string.join( string.split(line)[1:])
187 return "started", prog
188
189 if string.find(line, "Continuing.") == 0:
190 return "continued", None
191
192 if string.find(line, "\x1a\x1a") == 0:
193 rxcont = re.compile("^\(gdb\)\s+(cont|step|next|stepi|nexti)")
194
195 if rxcont.search(self.history[his-1]):
196 return "stepped", None
197
198 return None
199
200
201
202
203
204
205
206
207 if __name__ == "__main__":
208
209
210 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
211 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
212
213 gtk.main()
214
215
216
217