Skip to content

Commit

Permalink
fix(jtag): flush previous gdb responses before the first gdb command
Browse files Browse the repository at this point in the history
  • Loading branch information
erhankur committed Aug 10, 2024
1 parent 0694f19 commit 2d6cb43
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions pytest-embedded-jtag/pytest_embedded_jtag/gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,39 @@ def __init__(self, gdb_prog_path: Optional[str] = None, gdb_cli_args: Optional[s
gdb_prog_path = gdb_prog_path or self.GDB_PROG_PATH
gdb_cli_args = shlex.split(gdb_cli_args or self.GDB_DEFAULT_ARGS)

self._gdb_first_prompt_matched = False
self._gdb_first_write = True

super().__init__(cmd=[gdb_prog_path, *gdb_cli_args], **kwargs)

def write(self, s: AnyStr, non_blocking: bool = False, timeout: float = 30) -> Optional[str]:
super().write(s)
_buffer = ''
_t_start = time.time()

with open(self._logfile) as fr:
if self._gdb_first_write:
# Discard all queued responses before the first write
_ = fr.readlines()
self._logfile_offset = fr.tell()
self._gdb_first_write = False

super().write(s)

if non_blocking:
logging.debug('non-blocking write...')
return None

_buffer = ''
_t_start = time.time()
fr.seek(self._logfile_offset)
while True:
line = fr.readline()
_t_now = time.time()
if non_blocking:
logging.debug('non-blocking write...')
return None

if (_t_now - _t_start) >= timeout:
logging.debug(f'current buffer: {_buffer}')
raise ValueError(f'gdb no response after {timeout} seconds')

if line:
_buffer += line
if self._GDB_RESPONSE_FINISHED_RE.match(line):
if not self._gdb_first_prompt_matched:
self._gdb_first_prompt_matched = True
continue
break

_t_now = time.time()
if (_t_now - _t_start) >= timeout:
logging.debug(f'current buffer: {_buffer}')
raise ValueError(f'gdb no response after {timeout} seconds')

self._logfile_offset = fr.tell()

logging.debug(f'{self.SOURCE} <-: {_buffer}')
Expand Down

0 comments on commit 2d6cb43

Please sign in to comment.