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