496e7c39ed2302c14ef9b83efd90fb7cb6400c89
[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
38 def setContinue(self):
39 his = self.getHistoryLen()
40 self.feed_child("cont\n");
41 return self.waitForActivation(his)
42
43 def setStepover(self):
44 his = self.getHistoryLen()
45 self.feed_child("next\n");
46 return self.waitForActivation(his)
47
48 def setStepin(self):
49 his = self.getHistoryLen()
50 self.feed_child("step\n");
51 return self.waitForActivation(his)
52
53 def setQuit(self):
54 self.feed_child("quit\n")
55 self.waitForNewline()
56 self.feed_child("y\n");
57
58 def setBreakpoint(self, file, lineno, condition=None):
59 his = self.getHistoryLen()
60 if condition==None:
61 self.feed_child("break %s:%s\n" % (file, str(lineno)))
62 else:
63 self.feed_child("break %s:%s if %s\n" % \
64 (file, str(lineno), condition))
65
66 rx = re.compile("^Breakpoint |^No|^\(gdb\) ")
67 his, response = self.waitForRx(rx, his)
68
69 print his, response
70
71 if response[0:10] == "Breakpoint":
72 return string.split(response)[1].strip()
73 if response[0:5] == "(gdb)":
74 return None
75 if response[0:14] == "No source file":
76 self.feed_child("n\n");
77 return None
78 if response[0:3] == "No ":
79 return None
80
81 return NotImplementedError()
82
83 def delBreakpoint(self, breakpoint):
84 self.feed_child("del breakpoint %s\n" % (breakpoint,))
85
86 def getBreakpoints(self):
87 starthis = self.getHistoryLen()
88 self.feed_child("info breakpoints\n")
89
90 rx = re.compile("^\(gdb\) ")
91 endhis, response = self.waitForRx(rx, starthis)
92
93
94 rxbp1 = re.compile("^\d+\s+breakpoint")
95 rxbp2 = 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 rxbp1.search(line):
106 print "Warning GdbTerminal.getBreakpoints at line", line
107 i += 1
108 continue
109
110 splits = string.split(line)
111 no = splits[0]
112 pos = splits[-1]
113 [file,lineno] = string.split(pos,":")
114 cond = None
115
116 if i+1<len(bplines) and rxbp2.search(bplines[i+1]):
117 i +=1
118 line = bplines[i]
119 pre,cond = string.split(line,"if")
120 cond = cond.strip()
121
122 bpnts += [[no, file, lineno, cond]]
123 i += 1
124
125 return bpnts
126
127
128
129 def getExpression(self, expr):
130
131 his = self.getHistoryLen()
132 self.feed_child("print " + expr + "\n")
133
134 rx = re.compile("^\(gdb\) $")
135 his, response = self.waitForRx(rx, his)
136
137 answer = self.history[his-1]
138
139 if len(string.split(answer, "=")) == 1:
140 return answer.strip()
141
142 split = string.split(answer, "=")
143 return string.join(split[1:], "=").strip()
144
145
146 def waitForActivation(self, his):
147
148 self.setActive(False)
149 rx = re.compile("^\(gdb\) $")
150 his, reponse = self.waitForRx(rx,his)
151 self.setActive(True)
152
153 if self.history[his-1][0:2]=="\x1a\x1a":
154 tuples = string.split(self.history[his-1][2:], ":")
155 return tuples[0:2]
156
157 return None
158
159
160
161 if __name__ == "__main__":
162
163
164 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
165 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
166
167 gtk.main()
168
169
170
171