- # Wait for OpenOCD to have made it through riscv_examine(). When using
- # OpenOCD to communicate with a simulator this may take a long time,
- # and gdb will time out when trying to connect if we attempt too early.
- start = time.time()
- messaged = False
- while True:
- log = open(Openocd.logname).read()
- if "Ready for Remote Connections" in log:
- break
- if not self.process.poll() is None:
- raise Exception(
- "OpenOCD exited before completing riscv_examine()")
- if not messaged and time.time() - start > 1:
- messaged = True
- print "Waiting for OpenOCD to examine RISCV core..."
-
- self.port = self._get_gdb_server_port()
-
- def _get_gdb_server_port(self):
- """Get port that OpenOCD's gdb server is listening on."""
- MAX_ATTEMPTS = 50
- PORT_REGEX = re.compile(r'(?P<port>\d+) \(LISTEN\)')
- for _ in range(MAX_ATTEMPTS):
- with open(os.devnull, 'w') as devnull:
- try:
- output = subprocess.check_output([
- 'lsof',
- '-a', # Take the AND of the following selectors
- '-p{}'.format(self.process.pid), # Filter on PID
- '-iTCP', # Filter only TCP sockets
- ], stderr=devnull)
- except subprocess.CalledProcessError:
- output = ""
- matches = list(PORT_REGEX.finditer(output))
- matches = [m for m in matches
- if m.group('port') not in ('6666', '4444')]
- if len(matches) > 1:
- print output
- raise Exception(
- "OpenOCD listening on multiple ports. Cannot uniquely "
- "identify gdb server port.")
- elif matches:
- [match] = matches
- return int(match.group('port'))
- time.sleep(1)
- raise Exception("Timed out waiting for gdb server to obtain port.")
+ try:
+ # Wait for OpenOCD to have made it through riscv_examine(). When
+ # using OpenOCD to communicate with a simulator this may take a
+ # long time, and gdb will time out when trying to connect if we
+ # attempt too early.
+ start = time.time()
+ messaged = False
+ while True:
+ log = open(Openocd.logname).read()
+ m = re.search(r"Listening on port (\d+) for gdb connections",
+ log)
+ if m:
+ if not self.ports:
+ self.port = int(m.group(1))
+ self.ports.append(int(m.group(1)))
+
+ if "telnet server disabled" in log:
+ break
+
+ if not process.poll() is None:
+ raise Exception(
+ "OpenOCD exited before completing riscv_examine()")
+ if not messaged and time.time() - start > 1:
+ messaged = True
+ print "Waiting for OpenOCD to start..."
+ if (time.time() - start) > self.timeout:
+ raise Exception("ERROR: Timed out waiting for OpenOCD to "
+ "listen for gdb")
+ return process
+ except Exception:
+ header("OpenOCD log")
+ sys.stdout.write(log)
+ raise