Add LICENSE
[pygdb.git] / GdbTerminal.py
old mode 100755 (executable)
new mode 100644 (file)
index 496e7c3..58c3bbe
@@ -23,38 +23,44 @@ class GdbTerminal (DbgTerminal.DbgTerminal):
        def getArgv(self):
                return ["gdb", "--fullname", string.split(self.clientCmd)[0]]
 
-       def setPty(self, pty):
-               ttyname = os.ttyname(pty)
-               self.feed_child("set inferior-tty %s\n" % (ttyname,))
-
        def setRun(self):
-
-               his = self.getHistoryLen()
                argv = string.join(string.split(self.clientCmd)[1:])
                self.feed_child("run " + argv + "\n")
-               return self.waitForActivation(his)
-
 
        def setContinue(self):
-               his = self.getHistoryLen()
                self.feed_child("cont\n");
-               return self.waitForActivation(his)
 
        def setStepover(self):
-               his = self.getHistoryLen()
                self.feed_child("next\n");
-               return self.waitForActivation(his)
 
        def setStepin(self):
-               his = self.getHistoryLen()
                self.feed_child("step\n");
-               return self.waitForActivation(his)
+
+       def setStepout(self):
+               bt = self.getBacktrace()
+
+               if len(bt) < 2:
+                       self.setContinue()
+
+               else:
+                       #Get the second line
+                       sec = bt[1]
+
+                       #Check if it is a backtrace line
+                       if re.compile("^#1\s+.*\s\S+:\d+$").search(sec):
+                               pos = string.split(sec)[-1]
+                               self.feed_child("advance %s\n" % pos)
+
 
        def setQuit(self):
                self.feed_child("quit\n")
                self.waitForNewline()
                self.feed_child("y\n");
 
+       def setPty(self, pty):
+               ttyname = os.ttyname(pty)
+               self.__getAnswerFromCmd("set inferior-tty %s\n" % (ttyname,))
+
        def setBreakpoint(self, file, lineno, condition=None):
                his = self.getHistoryLen()
                if condition==None:
@@ -63,78 +69,81 @@ class GdbTerminal (DbgTerminal.DbgTerminal):
                        self.feed_child("break %s:%s if %s\n" % \
                                        (file, str(lineno), condition))
 
-               rx = re.compile("^Breakpoint |^No|^\(gdb\) ")
+               rx = "^Breakpoint |^No |^\(gdb\) "
                his, response = self.waitForRx(rx, his)
 
-               print his, response
+               answer = None
 
                if response[0:10] == "Breakpoint":
-                       return string.split(response)[1].strip()
-               if response[0:5] == "(gdb)":
-                       return None
+                       answer = string.split(response)[1].strip()
+               
+               #Wants an answer: "No"
                if response[0:14] == "No source file":
                        self.feed_child("n\n");
-                       return None
-               if response[0:3] == "No ":
-                       return None
-                       
-               return NotImplementedError()
 
-       def delBreakpoint(self, breakpoint):
-               self.feed_child("del breakpoint %s\n" % (breakpoint,))
+               #Wait again for (gdb)...
+               self.waitForPrompt(his)
 
-       def getBreakpoints(self):
-               starthis = self.getHistoryLen()
-               self.feed_child("info breakpoints\n")
+               return answer
 
-               rx = re.compile("^\(gdb\) ")
-               endhis, response = self.waitForRx(rx, starthis)
 
+       def delBreakpoint(self, breakpoint):
+               self.__getAnswerFromCmd("del breakpoint %s\n" % (breakpoint,))
 
-               rxbp1 = re.compile("^\d+\s+breakpoint")
-               rxbp2 = re.compile("^\tstop only if")
 
-               bpnts = []
-               bplines = self.history[starthis+1:endhis]
-               i = 0
+       def getBreakpoints(self):
+               bplines = self.__getAnswerFromCmd("info breakpoints\n")
 
-               #Parse the resulting lines
-               while i<len(bplines):
-                       line = bplines[i]
+               rxbp = re.compile("^\d+\s+breakpoint")
+               rxpos = re.compile("at \S+:\d+$")
+               rxcond = re.compile("^\s+stop only if")
 
-                       if not rxbp1.search(line):
-                               print "Warning GdbTerminal.getBreakpoints at line", line
-                               i += 1
-                               continue
+               bpnts = []
 
-                       splits = string.split(line)
-                       no = splits[0]
-                       pos = splits[-1]
-                       [file,lineno] = string.split(pos,":")
-                       cond = None
+               try:
+                       i = 1
+
+                       #Parse the resulting lines
+                       while i<len(bplines):
 
-                       if i+1<len(bplines) and rxbp2.search(bplines[i+1]):
-                               i +=1
                                line = bplines[i]
-                               pre,cond = string.split(line,"if")
-                               cond = cond.strip()
+                               while not rxbp.search(line):
+                                       i += 1
+                                       line = bplines[i]
+               
+                               #Get number of breakpoint
+                               no = string.split(line)[0]
+
+                               #This line does not contain the file!
+                               #Check for next line...
+                               while not rxpos.search(line):
+                                       i += 1
+                                       line = bplines[i]
+
+                               pos = string.split(line)[-1]
+                               [file,lineno] = string.split(pos,":")
+                               cond = None
+
+                               #Look for conditions
+                               if i+1<len(bplines) and rxcond.search(bplines[i+1]):
+                                       i +=1
+                                       line = bplines[i]
+                                       cond = string.join(string.split(line," if ")[1:], " if ")
+                                       cond = cond.strip()
+
+                               bpnts += [[no, file, lineno, cond]]
+                               i += 1
 
-                       bpnts += [[no, file, lineno, cond]]
-                       i += 1
+               except IndexError:
+                       pass
 
                return bpnts
                
 
 
        def getExpression(self, expr):
-
-               his = self.getHistoryLen()
-               self.feed_child("print " + expr + "\n")
-
-               rx = re.compile("^\(gdb\) $")
-               his, response = self.waitForRx(rx, his)
-
-               answer = self.history[his-1]
+               answer = self.__getAnswerFromCmd("print " + expr + "\n")
+               answer = answer[-1]
 
                if len(string.split(answer, "=")) == 1:
                        return answer.strip()
@@ -142,22 +151,124 @@ class GdbTerminal (DbgTerminal.DbgTerminal):
                split = string.split(answer, "=")
                return string.join(split[1:], "=").strip()
 
+       
+       def listCodeSnippet(self):
+               return self.__getAnswerFromCmd("list\n")
+
+       def getBacktrace(self):
 
-       def waitForActivation(self, his):
+               stack = []
+               answ = self.__getAnswerFromCmd("bt\n")
 
-               self.setActive(False)
-               rx = re.compile("^\(gdb\) $")
-               his, reponse = self.waitForRx(rx,his)
-               self.setActive(True)
+               rxstartfull = re.compile("^\#\d+\s+0x[0-9a-f]+\s+in\s+\S+\s+\(")
+               rxstartshort = re.compile("^\#\d+\s+\S+\s+\(")
+               rxpos = re.compile("at \S+:\d+$")
 
-               if self.history[his-1][0:2]=="\x1a\x1a":
-                       tuples = string.split(self.history[his-1][2:], ":")
-                       return tuples[0:2]
+               try:
+
+                       i=0
+                       while i<len(answ):
+                               line = answ[i]
+
+                               while not rxstartfull.search(line) and not rxstartshort.search(line):
+                                       print "Warning: '", line, "' does not match bt entry."
+                                       i+=1
+                                       line = answ[i]
+
+                               parts = line.split()
+
+                               if rxstartfull.search(line):
+                                       func = parts[3]
+                                       addr = parts[1]
+                               else:
+                                       func = parts[1]
+                                       addr = None
+
+
+                               #Search for file position
+                               while not rxpos.search(line):
+                                       i+=1
+                                       line = answ[i]
+
+                               parts = line.split()
+                               pos = parts[-1]
+                               [file,lineno] = pos.split(":")
+
+                               stack += [[addr,func,file,lineno]]
+
+                               i+=1
+
+               except IndexError:
+                       pass
+
+               return stack
+
+       def waitForPrompt(self, his):
+               rx = "^\(gdb\)"
+               return self.waitForRx(rx,his)
+
+       def __getAnswerFromCmd(self, cmd):
+               starthis = self.getHistoryLen()
+               self.feed_child(cmd)
+               endhis, response = self.waitForPrompt(starthis)
+
+               return self.history[starthis:endhis]
+
+
+       def testForActivity(self, his):
+               """Test whether debugger got active again"""
+                       
+               line = self.history[his]
+
+               if string.find(line, "\x1a\x1a") == 0:
+                       tuples = string.split(line[2:], ":")
+                       tuples[1] = int(tuples[1])
+                       return "break", [tuples[0], int(tuples[1])]
+
+               if string.find(line, "Program exited") == 0:
+                       code = string.split(line)[-1]
+
+                       codeno = 0
+               
+                       #Parse the octal number
+                       if code[0] == "O":
+                               code = code[1:-1]
+                               for c in code:
+                                       codeno = codeno*8 + int(c)
+
+                       return "exited", codeno
+
+               return None
+
+
+       def testForInactivity(self, his):
+               """Test whether debugger got inactive"""                
+               line = self.history[his]
+
+               if string.find(line, "Starting program:") == 0:
+                       prog = string.join( string.split(line)[1:])
+                       return "started", prog
+
+               if string.find(line, "Continuing.") == 0:
+                       return "continued", None
+
+               if string.find(line, "\x1a\x1a") == 0:
+                       rxcont = re.compile("^\(gdb\)\s+(cont|step|next|stepi|nexti|advance)")
+
+                       if rxcont.search(self.history[his-1]):
+                               return "stepped", None
+                       if rxcont.search(self.history[his-2]):
+                               return "stepped", None
 
                return None
 
 
 
+
+
+
+
+
 if __name__ == "__main__":