17 class GdbTerminal (DbgTerminal
.DbgTerminal
):
20 def __init__(self
, clientCmd
):
21 DbgTerminal
.DbgTerminal
.__init
__(self
, clientCmd
)
24 return ["gdb", "--fullname", string
.split(self
.clientCmd
)[0]]
27 argv
= string
.join(string
.split(self
.clientCmd
)[1:])
28 self
.feed_child("run " + argv
+ "\n")
30 def setContinue(self
):
31 self
.feed_child("cont\n");
33 def setStepover(self
):
34 self
.feed_child("next\n");
37 self
.feed_child("step\n");
40 bt
= self
.getBacktrace()
49 #Check if it is a backtrace line
50 if re
.compile("^#1\s+.*\s\S+:\d+$").search(sec
):
51 pos
= string
.split(sec
)[-1]
52 self
.feed_child("advance %s\n" % pos
)
56 self
.feed_child("quit\n")
58 self
.feed_child("y\n");
60 def setPty(self
, pty
):
61 ttyname
= os
.ttyname(pty
)
62 self
.__getAnswerFromCmd
("set inferior-tty %s\n" % (ttyname
,))
64 def setBreakpoint(self
, file, lineno
, condition
=None):
65 his
= self
.getHistoryLen()
67 self
.feed_child("break %s:%s\n" % (file, str(lineno
)))
69 self
.feed_child("break %s:%s if %s\n" % \
70 (file, str(lineno
), condition
))
72 rx
= "^Breakpoint |^No |^\(gdb\) "
73 his
, response
= self
.waitForRx(rx
, his
)
77 if response
[0:10] == "Breakpoint":
78 answer
= string
.split(response
)[1].strip()
80 #Wants an answer: "No"
81 if response
[0:14] == "No source file":
82 self
.feed_child("n\n");
84 #Wait again for (gdb)...
85 self
.waitForPrompt(his
)
90 def delBreakpoint(self
, breakpoint
):
91 self
.__getAnswerFromCmd
("del breakpoint %s\n" % (breakpoint
,))
94 def getBreakpoints(self
):
95 bplines
= self
.__getAnswerFromCmd
("info breakpoints\n")
97 rxbp
= re
.compile("^\d+\s+breakpoint")
98 rxpos
= re
.compile("at \S+:\d+$")
99 rxcond
= re
.compile("^\s+stop only if")
106 #Parse the resulting lines
107 while i
<len(bplines
):
110 while not rxbp
.search(line
):
114 #Get number of breakpoint
115 no
= string
.split(line
)[0]
117 #This line does not contain the file!
118 #Check for next line...
119 while not rxpos
.search(line
):
123 pos
= string
.split(line
)[-1]
124 [file,lineno
] = string
.split(pos
,":")
128 if i
+1<len(bplines
) and rxcond
.search(bplines
[i
+1]):
131 cond
= string
.join(string
.split(line
," if ")[1:], " if ")
134 bpnts
+= [[no
, file, lineno
, cond
]]
144 def getExpression(self
, expr
):
145 answer
= self
.__getAnswerFromCmd
("print " + expr
+ "\n")
148 if len(string
.split(answer
, "=")) == 1:
149 return answer
.strip()
151 split
= string
.split(answer
, "=")
152 return string
.join(split
[1:], "=").strip()
155 def listCodeSnippet(self
):
156 return self
.__getAnswerFromCmd
("list\n")
158 def getBacktrace(self
):
161 answ
= self
.__getAnswerFromCmd
("bt\n")
163 rxstartfull
= re
.compile("^\#\d+\s+0x[0-9a-f]+\s+in\s+\S+\s+\(")
164 rxstartshort
= re
.compile("^\#\d+\s+\S+\s+\(")
165 rxpos
= re
.compile("at \S+:\d+$")
173 while not rxstartfull
.search(line
) and not rxstartshort
.search(line
):
174 print "Warning: '", line
, "' does not match bt entry."
180 if rxstartfull
.search(line
):
188 #Search for file position
189 while not rxpos
.search(line
):
195 [file,lineno
] = pos
.split(":")
197 stack
+= [[addr
,func
,file,lineno
]]
206 def waitForPrompt(self
, his
):
208 return self
.waitForRx(rx
,his
)
210 def __getAnswerFromCmd(self
, cmd
):
211 starthis
= self
.getHistoryLen()
213 endhis
, response
= self
.waitForPrompt(starthis
)
215 return self
.history
[starthis
:endhis
]
218 def testForActivity(self
, his
):
219 """Test whether debugger got active again"""
221 line
= self
.history
[his
]
223 if string
.find(line
, "\x1a\x1a") == 0:
224 tuples
= string
.split(line
[2:], ":")
225 tuples
[1] = int(tuples
[1])
226 return "break", [tuples
[0], int(tuples
[1])]
228 if string
.find(line
, "Program exited") == 0:
229 code
= string
.split(line
)[-1]
233 #Parse the octal number
237 codeno
= codeno
*8 + int(c
)
239 return "exited", codeno
244 def testForInactivity(self
, his
):
245 """Test whether debugger got inactive"""
246 line
= self
.history
[his
]
248 if string
.find(line
, "Starting program:") == 0:
249 prog
= string
.join( string
.split(line
)[1:])
250 return "started", prog
252 if string
.find(line
, "Continuing.") == 0:
253 return "continued", None
255 if string
.find(line
, "\x1a\x1a") == 0:
256 rxcont
= re
.compile("^\(gdb\)\s+(cont|step|next|stepi|nexti|advance)")
258 if rxcont
.search(self
.history
[his
-1]):
259 return "stepped", None
260 if rxcont
.search(self
.history
[his
-2]):
261 return "stepped", None
272 if __name__
== "__main__":
275 dbgterm
= GdbTerminal(string
.join(sys
.argv
[1:]))
276 dbgwnd
= DbgTerminal
.DbgWindow(dbgterm
)