4fb4a35b8dd8c2494f76a759a6fbcbeb0cb626f9
[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, exitcb=None):
21 DbgTerminal.DbgTerminal.__init__(self, clientCmd, exitcb)
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 if response[0:10] == "Breakpoint":
70 return string.split(response)[1].strip()
71 if response[0:5] == "(gdb)":
72 return None
73 if response[0:14] == "No source file":
74 self.feed_child("n\n");
75 return None
76 if response[0:3] == "No ":
77 return None
78
79 return NotImplementedError()
80
81 def delBreakpoint(self, breakpoint):
82 self.feed_child("del breakpoint %s\n" % (breakpoint,))
83
84 def getBreakpoints(self):
85 starthis = self.getHistoryLen()
86 self.feed_child("info breakpoints\n")
87
88 rx = re.compile("^\(gdb\) ")
89 endhis, response = self.waitForRx(rx, starthis)
90
91
92 rxbp1 = re.compile("^\d+\s+breakpoint")
93 rxbp2 = re.compile("^\tstop only if")
94
95 bpnts = []
96 bplines = self.history[starthis+1:endhis]
97 i = 0
98
99 #Parse the resulting lines
100 while i<len(bplines):
101 line = bplines[i]
102
103 if not rxbp1.search(line):
104 i += 1
105 continue
106
107 splits = string.split(line)
108 no = splits[0]
109 pos = splits[-1]
110 [file,lineno] = string.split(pos,":")
111 cond = None
112
113 if i+1<len(bplines) and rxbp2.search(bplines[i+1]):
114 i +=1
115 line = bplines[i]
116 pre,cond = string.split(line,"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
131 rx = re.compile("^\(gdb\) $")
132 his, response = self.waitForRx(rx, his)
133
134 answer = self.history[his-1]
135
136 if len(string.split(answer, "=")) == 1:
137 return answer.strip()
138
139 split = string.split(answer, "=")
140 return string.join(split[1:], "=").strip()
141
142
143 def waitForActivation(self, his=None):
144
145 self.setActive(False)
146 rx = re.compile("^\(gdb\) $")
147 his, reponse = self.waitForRx(rx,his)
148 self.setActive(True)
149
150 if self.history[his-1][0:2]=="\x1a\x1a":
151 tuples = string.split(self.history[his-1][2:], ":")
152 return tuples[0:2]
153
154 return None
155
156
157
158 if __name__ == "__main__":
159
160
161 dbgterm = GdbTerminal(string.join(sys.argv[1:]))
162 dbgwnd = DbgTerminal.DbgWindow(dbgterm)
163
164 gtk.main()
165
166
167
168