Esempio n. 1
0
    def __init__(
            self,
            serial_instance,  # type: serial.Serial
            elf_file,  # type: str
            print_filter,  # type: str
            make='make',  # type: str
            encrypted=False,  # type: bool
            toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,  # type: str
            eol='CRLF',  # type: str
            decode_coredumps=COREDUMP_DECODE_INFO,  # type: str
            decode_panic=PANIC_DECODE_DISABLE,  # type: str
            target='esp32',  # type: str
            websocket_client=None,  # type: WebSocketClient
            enable_address_decoding=True,  # type: bool
            timestamps=False,  # type: bool
            timestamp_format=''  # type: str
    ):
        super(Monitor, self).__init__()
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        # testing hook - data from serial can make exit the monitor
        socket_mode = serial_instance.port.startswith('socket://')
        self.serial = serial_instance

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)
        self.serial_reader = SerialReader(self.serial, self.event_queue)
        self.elf_file = elf_file

        # allow for possibility the "make" arg is a list of arguments (for idf.py)
        self.make = make if os.path.exists(make) else shlex.split(
            make)  # type: Any[Union[str, List[str]], str]
        self.target = target

        self._line_matcher = LineMatcher(print_filter)
        self.gdb_helper = GDBHelper(toolchain_prefix, websocket_client,
                                    self.elf_file, self.serial.port,
                                    self.serial.baudrate)
        self.logger = Logger(self.elf_file, self.console, timestamps,
                             timestamp_format, b'', enable_address_decoding,
                             toolchain_prefix)
        self.coredump = CoreDump(decode_coredumps, self.event_queue,
                                 self.logger, websocket_client, self.elf_file)
        self.serial_handler = SerialHandler(b'', socket_mode, self.logger,
                                            decode_panic, PANIC_IDLE, b'',
                                            target, False, False, self.serial,
                                            encrypted)

        # internal state
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]
Esempio n. 2
0
    def __init__(self,
                 serial_instance,
                 elf_file,
                 print_filter,
                 make='make',
                 encrypted=False,
                 toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,
                 eol='CRLF',
                 decode_coredumps=COREDUMP_DECODE_INFO,
                 decode_panic=PANIC_DECODE_DISABLE,
                 target='esp32',
                 websocket_client=None,
                 enable_address_decoding=True):
        # type: (serial.Serial, str, str, str, bool, str, str, str, str, str, WebSocketClient, bool) -> None
        super(Monitor, self).__init__()
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()
        self.enable_address_decoding = enable_address_decoding

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        socket_mode = serial_instance.port.startswith(
            'socket://'
        )  # testing hook - data from serial can make exit the monitor
        self.serial = serial_instance

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)
        self.serial_reader = SerialReader(self.serial, self.event_queue)
        self.elf_file = elf_file
        if not os.path.exists(make):
            # allow for possibility the "make" arg is a list of arguments (for idf.py)
            self.make = shlex.split(make)  # type: Union[str, List[str]]
        else:
            self.make = make
        self.encrypted = encrypted
        self.toolchain_prefix = toolchain_prefix
        self.websocket_client = websocket_client
        self.target = target

        # internal state
        self._last_line_part = b''
        self._gdb_buffer = b''
        self._pc_address_buffer = b''
        self._line_matcher = LineMatcher(print_filter)
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]
        self._force_line_print = False
        self._output_enabled = True
        self._serial_check_exit = socket_mode
        self._log_file = None  # type: Optional[BinaryIO]
        self._decode_coredumps = decode_coredumps
        self._reading_coredump = COREDUMP_IDLE
        self._coredump_buffer = b''
        self._decode_panic = decode_panic
        self._reading_panic = PANIC_IDLE
        self._panic_buffer = b''
Esempio n. 3
0
class Monitor(object):
    """
    Monitor application main class.

    This was originally derived from miniterm.Miniterm, but it turned out to be easier to write from scratch for this
    purpose.

    Main difference is that all event processing happens in the main thread, not the worker threads.
    """
    def __init__(self,
                 serial_instance,
                 elf_file,
                 print_filter,
                 make='make',
                 encrypted=False,
                 toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,
                 eol='CRLF',
                 decode_coredumps=COREDUMP_DECODE_INFO,
                 decode_panic=PANIC_DECODE_DISABLE,
                 target='esp32',
                 websocket_client=None,
                 enable_address_decoding=True):
        # type: (serial.Serial, str, str, str, bool, str, str, str, str, str, WebSocketClient, bool) -> None
        super(Monitor, self).__init__()
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()
        self.enable_address_decoding = enable_address_decoding

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        socket_mode = serial_instance.port.startswith(
            'socket://'
        )  # testing hook - data from serial can make exit the monitor
        self.serial = serial_instance

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)
        self.serial_reader = SerialReader(self.serial, self.event_queue)
        self.elf_file = elf_file
        if not os.path.exists(make):
            # allow for possibility the "make" arg is a list of arguments (for idf.py)
            self.make = shlex.split(make)  # type: Union[str, List[str]]
        else:
            self.make = make
        self.encrypted = encrypted
        self.toolchain_prefix = toolchain_prefix
        self.websocket_client = websocket_client
        self.target = target

        # internal state
        self._last_line_part = b''
        self._gdb_buffer = b''
        self._pc_address_buffer = b''
        self._line_matcher = LineMatcher(print_filter)
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]
        self._force_line_print = False
        self._output_enabled = True
        self._serial_check_exit = socket_mode
        self._log_file = None  # type: Optional[BinaryIO]
        self._decode_coredumps = decode_coredumps
        self._reading_coredump = COREDUMP_IDLE
        self._coredump_buffer = b''
        self._decode_panic = decode_panic
        self._reading_panic = PANIC_IDLE
        self._panic_buffer = b''

    def invoke_processing_last_line(self):
        # type: () -> None
        self.event_queue.put((TAG_SERIAL_FLUSH, b''), False)

    def main_loop(self):
        # type: () -> None
        self.console_reader.start()
        self.serial_reader.start()
        try:
            while self.console_reader.alive and self.serial_reader.alive:
                try:
                    item = self.cmd_queue.get_nowait()
                except queue.Empty:
                    try:
                        item = self.event_queue.get(True, 0.03)
                    except queue.Empty:
                        continue

                event_tag, data = item
                if event_tag == TAG_CMD:
                    self.handle_commands(data, self.target)
                elif event_tag == TAG_KEY:
                    try:
                        self.serial.write(codecs.encode(data))
                    except serial.SerialException:
                        pass  # this shouldn't happen, but sometimes port has closed in serial thread
                    except UnicodeEncodeError:
                        pass  # this can happen if a non-ascii character was passed, ignoring
                elif event_tag == TAG_SERIAL:
                    self.handle_serial_input(data)
                    if self._invoke_processing_last_line_timer is not None:
                        self._invoke_processing_last_line_timer.cancel()
                    self._invoke_processing_last_line_timer = threading.Timer(
                        0.1, self.invoke_processing_last_line)
                    self._invoke_processing_last_line_timer.start()
                    # If no further data is received in the next short period
                    # of time then the _invoke_processing_last_line_timer
                    # generates an event which will result in the finishing of
                    # the last line. This is fix for handling lines sent
                    # without EOL.
                elif event_tag == TAG_SERIAL_FLUSH:
                    self.handle_serial_input(data, finalize_line=True)
                else:
                    raise RuntimeError('Bad event data %r' %
                                       ((event_tag, data), ))
        except SerialStopException:
            normal_print('Stopping condition has been received\n')
        finally:
            try:
                self.console_reader.stop()
                self.serial_reader.stop()
                self.stop_logging()
                # Cancelling _invoke_processing_last_line_timer is not
                # important here because receiving empty data doesn't matter.
                self._invoke_processing_last_line_timer = None
            except Exception:
                pass
            normal_print('\n')

    def handle_serial_input(self, data, finalize_line=False):
        # type: (bytes, bool) -> None
        sp = data.split(b'\n')
        if self._last_line_part != b'':
            # add unprocessed part from previous "data" to the first line
            sp[0] = self._last_line_part + sp[0]
            self._last_line_part = b''
        if sp[-1] != b'':
            # last part is not a full line
            self._last_line_part = sp.pop()
        for line in sp:
            if line != b'':
                if self._serial_check_exit and line == self.console_parser.exit_key.encode(
                        'latin-1'):
                    raise SerialStopException()
                self.check_panic_decode_trigger(line)
                self.check_coredump_trigger_before_print(line)
                if self._force_line_print or self._line_matcher.match(
                        line.decode(errors='ignore')):
                    self._print(line + b'\n')
                    self.handle_possible_pc_address_in_line(line)
                self.check_coredump_trigger_after_print()
                self.check_gdbstub_trigger(line)
                self._force_line_print = False
        # Now we have the last part (incomplete line) in _last_line_part. By
        # default we don't touch it and just wait for the arrival of the rest
        # of the line. But after some time when we didn't received it we need
        # to make a decision.
        if self._last_line_part != b'':
            if self._force_line_print or (
                    finalize_line and self._line_matcher.match(
                        self._last_line_part.decode(errors='ignore'))):
                self._force_line_print = True
                self._print(self._last_line_part)
                self.handle_possible_pc_address_in_line(self._last_line_part)
                self.check_gdbstub_trigger(self._last_line_part)
                # It is possible that the incomplete line cuts in half the PC
                # address. A small buffer is kept and will be used the next time
                # handle_possible_pc_address_in_line is invoked to avoid this problem.
                # MATCH_PCADDR matches 10 character long addresses. Therefore, we
                # keep the last 9 characters.
                self._pc_address_buffer = self._last_line_part[-9:]
                # GDB sequence can be cut in half also. GDB sequence is 7
                # characters long, therefore, we save the last 6 characters.
                self._gdb_buffer = self._last_line_part[-6:]
                self._last_line_part = b''
        # else: keeping _last_line_part and it will be processed the next time
        # handle_serial_input is invoked

    def handle_possible_pc_address_in_line(self, line):
        # type: (bytes) -> None
        line = self._pc_address_buffer + line
        self._pc_address_buffer = b''
        if self.enable_address_decoding:
            for m in re.finditer(MATCH_PCADDR, line.decode(errors='ignore')):
                self.lookup_pc_address(m.group())

    def __enter__(self):
        # type: () -> None
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.serial_reader.stop()
        self.console_reader.stop()

    def __exit__(self, *args, **kwargs):  # type: ignore
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.console_reader.start()
        self.serial_reader.start()

    def prompt_next_action(self, reason):  # type: (str) -> None
        self.console.setup()  # set up console to trap input characters
        try:
            red_print('--- {}'.format(reason))
            red_print(self.console_parser.get_next_action_text())

            k = CTRL_T  # ignore CTRL-T here, so people can muscle-memory Ctrl-T Ctrl-F, etc.
            while k == CTRL_T:
                k = self.console.getkey()
        finally:
            self.console.cleanup()
        ret = self.console_parser.parse_next_action_key(k)
        if ret is not None:
            cmd = ret[1]
            if cmd == CMD_STOP:
                # the stop command should be handled last
                self.event_queue.put(ret)
            else:
                self.cmd_queue.put(ret)

    def run_make(self, target):  # type: (str) -> None
        with self:
            if isinstance(self.make, list):
                popen_args = self.make + [target]
            else:
                popen_args = [self.make, target]
            yellow_print('Running %s...' % ' '.join(popen_args))
            p = subprocess.Popen(popen_args, env=os.environ)
            try:
                p.wait()
            except KeyboardInterrupt:
                p.wait()
            if p.returncode != 0:
                self.prompt_next_action('Build failed')
            else:
                self.output_enable(True)

    def lookup_pc_address(self, pc_addr):  # type: (str) -> None
        cmd = [
            '%saddr2line' % self.toolchain_prefix, '-pfiaC', '-e',
            self.elf_file, pc_addr
        ]
        try:
            translation = subprocess.check_output(cmd, cwd='.')
            if b'?? ??:0' not in translation:
                self._print(translation.decode(), console_printer=yellow_print)
        except OSError as e:
            red_print('%s: %s' % (' '.join(cmd), e))

    def check_gdbstub_trigger(self, line):  # type: (bytes) -> None
        line = self._gdb_buffer + line
        self._gdb_buffer = b''
        m = re.search(b'\\$(T..)#(..)',
                      line)  # look for a gdb "reason" for a break
        if m is not None:
            try:
                chsum = sum(ord(bytes([p])) for p in m.group(1)) & 0xFF
                calc_chsum = int(m.group(2), 16)
            except ValueError:
                return  # payload wasn't valid hex digits
            if chsum == calc_chsum:
                if self.websocket_client:
                    yellow_print('Communicating through WebSocket')
                    self.websocket_client.send({
                        'event': 'gdb_stub',
                        'port': self.serial.port,
                        'prog': self.elf_file
                    })
                    yellow_print('Waiting for debug finished event')
                    self.websocket_client.wait([('event', 'debug_finished')])
                    yellow_print(
                        'Communications through WebSocket is finished')
                else:
                    self.run_gdb()
            else:
                red_print(
                    'Malformed gdb message... calculated checksum %02x received %02x'
                    % (chsum, calc_chsum))

    def check_coredump_trigger_before_print(self,
                                            line):  # type: (bytes) -> None
        if self._decode_coredumps == COREDUMP_DECODE_DISABLE:
            return

        if COREDUMP_UART_PROMPT in line:
            yellow_print('Initiating core dump!')
            self.event_queue.put((TAG_KEY, '\n'))
            return

        if COREDUMP_UART_START in line:
            yellow_print('Core dump started (further output muted)')
            self._reading_coredump = COREDUMP_READING
            self._coredump_buffer = b''
            self._output_enabled = False
            return

        if COREDUMP_UART_END in line:
            self._reading_coredump = COREDUMP_DONE
            yellow_print('\nCore dump finished!')
            self.process_coredump()
            return

        if self._reading_coredump == COREDUMP_READING:
            kb = 1024
            buffer_len_kb = len(self._coredump_buffer) // kb
            self._coredump_buffer += line.replace(b'\r', b'') + b'\n'
            new_buffer_len_kb = len(self._coredump_buffer) // kb
            if new_buffer_len_kb > buffer_len_kb:
                yellow_print('Received %3d kB...' % (new_buffer_len_kb),
                             newline='\r')

    def check_coredump_trigger_after_print(self):  # type: () -> None
        if self._decode_coredumps == COREDUMP_DECODE_DISABLE:
            return

        # Re-enable output after the last line of core dump has been consumed
        if not self._output_enabled and self._reading_coredump == COREDUMP_DONE:
            self._reading_coredump = COREDUMP_IDLE
            self._output_enabled = True
            self._coredump_buffer = b''

    def process_coredump(self):  # type: () -> None
        if self._decode_coredumps != COREDUMP_DECODE_INFO:
            raise NotImplementedError('process_coredump: %s not implemented' %
                                      self._decode_coredumps)

        coredump_script = os.path.join(os.path.dirname(__file__), '..',
                                       'components', 'espcoredump',
                                       'espcoredump.py')
        coredump_file = None
        try:
            # On Windows, the temporary file can't be read unless it is closed.
            # Set delete=False and delete the file manually later.
            with tempfile.NamedTemporaryFile(mode='wb',
                                             delete=False) as coredump_file:
                coredump_file.write(self._coredump_buffer)
                coredump_file.flush()

            if self.websocket_client:
                self._output_enabled = True
                yellow_print('Communicating through WebSocket')
                self.websocket_client.send({
                    'event': 'coredump',
                    'file': coredump_file.name,
                    'prog': self.elf_file
                })
                yellow_print('Waiting for debug finished event')
                self.websocket_client.wait([('event', 'debug_finished')])
                yellow_print('Communications through WebSocket is finished')
            else:
                cmd = [
                    sys.executable, coredump_script, 'info_corefile', '--core',
                    coredump_file.name, '--core-format', 'b64', self.elf_file
                ]
                output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
                self._output_enabled = True
                self._print(output)
                self._output_enabled = False  # Will be reenabled in check_coredump_trigger_after_print
        except subprocess.CalledProcessError as e:
            yellow_print('Failed to run espcoredump script: {}\n{}\n\n'.format(
                e, e.output))
            self._output_enabled = True
            self._print(COREDUMP_UART_START + b'\n')
            self._print(self._coredump_buffer)
            # end line will be printed in handle_serial_input
        finally:
            if coredump_file is not None:
                try:
                    os.unlink(coredump_file.name)
                except OSError as e:
                    yellow_print(
                        'Couldn\'t remote temporary core dump file ({})'.
                        format(e))

    def check_panic_decode_trigger(self, line):  # type: (bytes) -> None
        if self._decode_panic == PANIC_DECODE_DISABLE:
            return

        if self._reading_panic == PANIC_IDLE and re.search(
                PANIC_START, line.decode('ascii', errors='ignore')):
            self._reading_panic = PANIC_READING
            yellow_print('Stack dump detected')

        if self._reading_panic == PANIC_READING and PANIC_STACK_DUMP in line:
            self._output_enabled = False

        if self._reading_panic == PANIC_READING:
            self._panic_buffer += line.replace(b'\r', b'') + b'\n'

        if self._reading_panic == PANIC_READING and PANIC_END in line:
            self._reading_panic = PANIC_IDLE
            self._output_enabled = True
            self.process_panic_output(self._panic_buffer)
            self._panic_buffer = b''

    def process_panic_output(self, panic_output):  # type: (bytes) -> None
        panic_output_decode_script = os.path.join(os.path.dirname(__file__),
                                                  '..', 'tools',
                                                  'gdb_panic_server.py')
        panic_output_file = None
        try:
            # On Windows, the temporary file can't be read unless it is closed.
            # Set delete=False and delete the file manually later.
            with tempfile.NamedTemporaryFile(
                    mode='wb', delete=False) as panic_output_file:
                panic_output_file.write(panic_output)
                panic_output_file.flush()

            cmd = [
                self.toolchain_prefix + 'gdb', '--batch', '-n', self.elf_file,
                '-ex',
                "target remote | \"{python}\" \"{script}\" --target {target} \"{output_file}\""
                .format(python=sys.executable,
                        script=panic_output_decode_script,
                        target=self.target,
                        output_file=panic_output_file.name), '-ex', 'bt'
            ]

            output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
            yellow_print('\nBacktrace:\n\n')
            self._print(output)
        except subprocess.CalledProcessError as e:
            yellow_print(
                'Failed to run gdb_panic_server.py script: {}\n{}\n\n'.format(
                    e, e.output))
            self._print(panic_output)
        finally:
            if panic_output_file is not None:
                try:
                    os.unlink(panic_output_file.name)
                except OSError as e:
                    yellow_print(
                        'Couldn\'t remove temporary panic output file ({})'.
                        format(e))

    def run_gdb(self):  # type: () -> None
        with self:  # disable console control
            normal_print('')
            try:
                cmd = [
                    '%sgdb' % self.toolchain_prefix,
                    '-ex',
                    'set serial baud %d' % self.serial.baudrate,
                    '-ex',
                    'target remote %s' % self.serial.port,
                    '-ex',
                    'interrupt',  # monitor has already parsed the first 'reason' command, need a second
                    self.elf_file
                ]
                process = subprocess.Popen(cmd, cwd='.')
                process.wait()
            except OSError as e:
                red_print('%s: %s' % (' '.join(cmd), e))
            except KeyboardInterrupt:
                pass  # happens on Windows, maybe other OSes
            finally:
                try:
                    # on Linux, maybe other OSes, gdb sometimes seems to be alive even after wait() returns...
                    process.terminate()
                except Exception:
                    pass
                try:
                    # also on Linux, maybe other OSes, gdb sometimes exits uncleanly and breaks the tty mode
                    subprocess.call(['stty', 'sane'])
                except Exception:
                    pass  # don't care if there's no stty, we tried...
            self.prompt_next_action('gdb exited')

    def output_enable(self, enable):  # type: (bool) -> None
        self._output_enabled = enable

    def output_toggle(self):  # type: () -> None
        self._output_enabled = not self._output_enabled

        yellow_print(
            '\nToggle output display: {}, Type Ctrl-T Ctrl-Y to show/disable output again.'
            .format(self._output_enabled))

    def toggle_logging(self):  # type: () -> None
        if self._log_file:
            self.stop_logging()
        else:
            self.start_logging()

    def start_logging(self):  # type: () -> None
        if not self._log_file:
            name = 'log.{}.{}.txt'.format(
                os.path.splitext(os.path.basename(self.elf_file))[0],
                datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
            try:
                self._log_file = open(name, 'wb+')
                yellow_print('\nLogging is enabled into file {}'.format(name))
            except Exception as e:
                red_print('\nLog file {} cannot be created: {}'.format(
                    name, e))

    def stop_logging(self):  # type: () -> None
        if self._log_file:
            try:
                name = self._log_file.name
                self._log_file.close()
                yellow_print(
                    '\nLogging is disabled and file {} has been closed'.format(
                        name))
            except Exception as e:
                red_print('\nLog file cannot be closed: {}'.format(e))
            finally:
                self._log_file = None

    def _print(self,
               string,
               console_printer=None
               ):  # type: (Union[str, bytes], Optional[Callable]) -> None
        if console_printer is None:
            console_printer = self.console.write_bytes
        if self._output_enabled:
            console_printer(string)
        if self._log_file:
            try:
                if isinstance(string, type(u'')):
                    string = string.encode()
                self._log_file.write(string)  # type: ignore
            except Exception as e:
                red_print('\nCannot write to file: {}'.format(e))
                # don't fill-up the screen with the previous errors (probably consequent prints would fail also)
                self.stop_logging()

    def handle_commands(self, cmd, chip):  # type: (int, str) -> None
        config = get_chip_config(chip)
        reset_delay = config['reset']
        enter_boot_set = config['enter_boot_set']
        enter_boot_unset = config['enter_boot_unset']

        high = False
        low = True

        if cmd == CMD_STOP:
            self.console_reader.stop()
            self.serial_reader.stop()
        elif cmd == CMD_RESET:
            self.serial.setRTS(low)
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(reset_delay)
            self.serial.setRTS(high)
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            self.output_enable(low)
        elif cmd == CMD_MAKE:
            self.run_make('encrypted-flash' if self.encrypted else 'flash')
        elif cmd == CMD_APP_FLASH:
            self.run_make(
                'encrypted-app-flash' if self.encrypted else 'app-flash')
        elif cmd == CMD_OUTPUT_TOGGLE:
            self.output_toggle()
        elif cmd == CMD_TOGGLE_LOGGING:
            self.toggle_logging()
        elif cmd == CMD_ENTER_BOOT:
            self.serial.setDTR(high)  # IO0=HIGH
            self.serial.setRTS(low)  # EN=LOW, chip in reset
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(
                enter_boot_set
            )  # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.1
            self.serial.setDTR(low)  # IO0=LOW
            self.serial.setRTS(high)  # EN=HIGH, chip out of reset
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(
                enter_boot_unset
            )  # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.05
            self.serial.setDTR(high)  # IO0=HIGH, done
        else:
            raise RuntimeError('Bad command data %d' % cmd)  # type: ignore
Esempio n. 4
0
class Monitor(object):
    """
    Monitor application main class.

    This was originally derived from miniterm.Miniterm, but it turned out to be easier to write from scratch for this
    purpose.

    Main difference is that all event processing happens in the main thread, not the worker threads.
    """
    def __init__(
            self,
            serial_instance,  # type: serial.Serial
            elf_file,  # type: str
            print_filter,  # type: str
            make='make',  # type: str
            encrypted=False,  # type: bool
            toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,  # type: str
            eol='CRLF',  # type: str
            decode_coredumps=COREDUMP_DECODE_INFO,  # type: str
            decode_panic=PANIC_DECODE_DISABLE,  # type: str
            target='esp32',  # type: str
            websocket_client=None,  # type: WebSocketClient
            enable_address_decoding=True,  # type: bool
            timestamps=False,  # type: bool
            timestamp_format=''  # type: str
    ):
        super(Monitor, self).__init__()
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        # testing hook - data from serial can make exit the monitor
        socket_mode = serial_instance.port.startswith('socket://')
        self.serial = serial_instance

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)
        self.serial_reader = SerialReader(self.serial, self.event_queue)
        self.elf_file = elf_file

        # allow for possibility the "make" arg is a list of arguments (for idf.py)
        self.make = make if os.path.exists(make) else shlex.split(
            make)  # type: Any[Union[str, List[str]], str]
        self.target = target

        self._line_matcher = LineMatcher(print_filter)
        self.gdb_helper = GDBHelper(toolchain_prefix, websocket_client,
                                    self.elf_file, self.serial.port,
                                    self.serial.baudrate)
        self.logger = Logger(self.elf_file, self.console, timestamps,
                             timestamp_format, b'', enable_address_decoding,
                             toolchain_prefix)
        self.coredump = CoreDump(decode_coredumps, self.event_queue,
                                 self.logger, websocket_client, self.elf_file)
        self.serial_handler = SerialHandler(b'', socket_mode, self.logger,
                                            decode_panic, PANIC_IDLE, b'',
                                            target, False, False, self.serial,
                                            encrypted)

        # internal state
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]

    def invoke_processing_last_line(self):
        # type: () -> None
        self.event_queue.put((TAG_SERIAL_FLUSH, b''), False)

    def main_loop(self):
        # type: () -> None
        self.console_reader.start()
        self.serial_reader.start()
        self.gdb_helper.gdb_exit = False
        self.serial_handler.start_cmd_sent = False
        try:
            while self.console_reader.alive and self.serial_reader.alive:
                try:
                    if self.gdb_helper.gdb_exit:
                        self.gdb_helper.gdb_exit = False
                        time.sleep(GDB_EXIT_TIMEOUT)
                        try:
                            # Continue the program after exit from the GDB
                            self.serial.write(
                                codecs.encode(GDB_UART_CONTINUE_COMMAND))
                            self.serial_handler.start_cmd_sent = True
                        except serial.SerialException:
                            pass  # this shouldn't happen, but sometimes port has closed in serial thread
                        except UnicodeEncodeError:
                            pass  # this can happen if a non-ascii character was passed, ignoring

                    try:
                        item = self.cmd_queue.get_nowait()
                    except queue.Empty:
                        try:
                            item = self.event_queue.get(
                                timeout=EVENT_QUEUE_TIMEOUT)
                        except queue.Empty:
                            continue

                    event_tag, data = item
                    if event_tag == TAG_CMD:
                        self.serial_handler.handle_commands(
                            data, self.target, self.run_make,
                            self.console_reader, self.serial_reader)
                    elif event_tag == TAG_KEY:
                        try:
                            self.serial.write(codecs.encode(data))
                        except serial.SerialException:
                            pass  # this shouldn't happen, but sometimes port has closed in serial thread
                        except UnicodeEncodeError:
                            pass  # this can happen if a non-ascii character was passed, ignoring
                    elif event_tag == TAG_SERIAL:
                        self.serial_handler.handle_serial_input(
                            data, self.console_parser, self.coredump,
                            self.gdb_helper, self._line_matcher,
                            self.check_gdb_stub_and_run)
                        if self._invoke_processing_last_line_timer is not None:
                            self._invoke_processing_last_line_timer.cancel()
                        self._invoke_processing_last_line_timer = threading.Timer(
                            LAST_LINE_THREAD_INTERVAL,
                            self.invoke_processing_last_line)
                        self._invoke_processing_last_line_timer.start()
                        # If no further data is received in the next short period
                        # of time then the _invoke_processing_last_line_timer
                        # generates an event which will result in the finishing of
                        # the last line. This is fix for handling lines sent
                        # without EOL.
                    elif event_tag == TAG_SERIAL_FLUSH:
                        self.serial_handler.handle_serial_input(
                            data,
                            self.console_parser,
                            self.coredump,
                            self.gdb_helper,
                            self._line_matcher,
                            self.check_gdb_stub_and_run,
                            finalize_line=True)
                    else:
                        raise RuntimeError('Bad event data %r' %
                                           ((event_tag, data), ))
                except KeyboardInterrupt:
                    try:
                        yellow_print(
                            'To exit from IDF monitor please use \"Ctrl+]\"')
                        self.serial.write(codecs.encode(CTRL_C))
                    except serial.SerialException:
                        pass  # this shouldn't happen, but sometimes port has closed in serial thread
                    except UnicodeEncodeError:
                        pass  # this can happen if a non-ascii character was passed, ignoring
        except SerialStopException:
            normal_print('Stopping condition has been received\n')
        except KeyboardInterrupt:
            pass
        finally:
            try:
                self.console_reader.stop()
                self.serial_reader.stop()
                self.logger.stop_logging()
                # Cancelling _invoke_processing_last_line_timer is not
                # important here because receiving empty data doesn't matter.
                self._invoke_processing_last_line_timer = None
            except Exception:  # noqa
                pass
            normal_print('\n')

    def __enter__(self):
        # type: () -> None
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.serial_reader.stop()
        self.console_reader.stop()

    def __exit__(self, *args, **kwargs):  # type: ignore
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.console_reader.start()
        self.serial_reader.gdb_exit = self.gdb_helper.gdb_exit  # write gdb_exit flag
        self.serial_reader.start()

    def check_gdb_stub_and_run(self, line):  # type: (bytes) -> None
        if self.gdb_helper.check_gdb_stub_trigger(line):
            with self:  # disable console control
                self.gdb_helper.run_gdb()

    def run_make(self, target):  # type: (str) -> None
        with self:
            run_make(target, self.make, self.console, self.console_parser,
                     self.event_queue, self.cmd_queue, self.logger)
Esempio n. 5
0
class Monitor:
    """
    Monitor application base class.

    This was originally derived from miniterm.Miniterm, but it turned out to be easier to write from scratch for this
    purpose.

    Main difference is that all event processing happens in the main thread, not the worker threads.
    """
    def __init__(
            self,
            serial_instance,  # type: serial.Serial
            elf_file,  # type: str
            print_filter,  # type: str
            make='make',  # type: str
            encrypted=False,  # type: bool
            toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,  # type: str
            eol='CRLF',  # type: str
            decode_coredumps=COREDUMP_DECODE_INFO,  # type: str
            decode_panic=PANIC_DECODE_DISABLE,  # type: str
            target='esp32',  # type: str
            websocket_client=None,  # type: Optional[WebSocketClient]
            enable_address_decoding=True,  # type: bool
            timestamps=False,  # type: bool
            timestamp_format=''  # type: str
    ):
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        self.elf_file = elf_file or ''
        self.elf_exists = os.path.exists(self.elf_file)
        self.logger = Logger(self.elf_file, self.console, timestamps,
                             timestamp_format, b'', enable_address_decoding,
                             toolchain_prefix)

        self.coredump = CoreDump(decode_coredumps, self.event_queue,
                                 self.logger, websocket_client,
                                 self.elf_file) if self.elf_exists else None

        # allow for possibility the "make" arg is a list of arguments (for idf.py)
        self.make = make if os.path.exists(make) else shlex.split(
            make)  # type: Any[Union[str, List[str]], str]
        self.target = target

        # testing hook - data from serial can make exit the monitor
        if isinstance(self, SerialMonitor):
            socket_mode = serial_instance.port.startswith('socket://')
            self.serial = serial_instance
            self.serial_reader = SerialReader(self.serial, self.event_queue)

            self.gdb_helper = GDBHelper(
                toolchain_prefix, websocket_client, self.elf_file,
                self.serial.port,
                self.serial.baudrate) if self.elf_exists else None

        else:
            socket_mode = False
            self.serial = subprocess.Popen([self.elf_file],
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT)
            self.serial_reader = LinuxReader(self.serial, self.event_queue)

            self.gdb_helper = None

        cls = SerialHandler if self.elf_exists else SerialHandlerNoElf
        self.serial_handler = cls(b'', socket_mode, self.logger, decode_panic,
                                  PANIC_IDLE, b'', target, False, False,
                                  self.serial, encrypted, self.elf_file)

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)

        self._line_matcher = LineMatcher(print_filter)

        # internal state
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]

    def __enter__(self) -> None:
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.serial_reader.stop()
        self.console_reader.stop()

    def __exit__(self, exc_type, exc_val, exc_tb) -> None:  # type: ignore
        raise NotImplementedError

    def run_make(self, target: str) -> None:
        with self:
            run_make(target, self.make, self.console, self.console_parser,
                     self.event_queue, self.cmd_queue, self.logger)

    def _pre_start(self) -> None:
        self.console_reader.start()
        self.serial_reader.start()

    def main_loop(self) -> None:
        self._pre_start()

        try:
            while self.console_reader.alive and self.serial_reader.alive:
                try:
                    self._main_loop()
                except KeyboardInterrupt:
                    yellow_print(
                        'To exit from IDF monitor please use \"Ctrl+]\"')
                    self.serial_write(codecs.encode(CTRL_C))
        except SerialStopException:
            normal_print('Stopping condition has been received\n')
        except KeyboardInterrupt:
            pass
        finally:
            try:
                self.console_reader.stop()
                self.serial_reader.stop()
                self.logger.stop_logging()
                # Cancelling _invoke_processing_last_line_timer is not
                # important here because receiving empty data doesn't matter.
                self._invoke_processing_last_line_timer = None
            except Exception:  # noqa
                pass
            normal_print('\n')

    def serial_write(self, *args: str, **kwargs: str) -> None:
        raise NotImplementedError

    def check_gdb_stub_and_run(self, line: bytes) -> None:
        raise NotImplementedError

    def invoke_processing_last_line(self) -> None:
        self.event_queue.put((TAG_SERIAL_FLUSH, b''), False)

    def _main_loop(self) -> None:
        try:
            item = self.cmd_queue.get_nowait()
        except queue.Empty:
            try:
                item = self.event_queue.get(timeout=EVENT_QUEUE_TIMEOUT)
            except queue.Empty:
                return

        event_tag, data = item
        if event_tag == TAG_CMD:
            self.serial_handler.handle_commands(data, self.target,
                                                self.run_make,
                                                self.console_reader,
                                                self.serial_reader)
        elif event_tag == TAG_KEY:
            self.serial_write(codecs.encode(data))
        elif event_tag == TAG_SERIAL:
            self.serial_handler.handle_serial_input(
                data, self.console_parser, self.coredump, self.gdb_helper,
                self._line_matcher, self.check_gdb_stub_and_run)
            if self._invoke_processing_last_line_timer is not None:
                self._invoke_processing_last_line_timer.cancel()
            self._invoke_processing_last_line_timer = threading.Timer(
                LAST_LINE_THREAD_INTERVAL, self.invoke_processing_last_line)
            self._invoke_processing_last_line_timer.start()
            # If no further data is received in the next short period
            # of time then the _invoke_processing_last_line_timer
            # generates an event which will result in the finishing of
            # the last line. This is fix for handling lines sent
            # without EOL.
            # finalizing the line when coredump is in progress causes decoding issues
            # the espcoredump loader uses empty line as a sign for end-of-coredump
            # line is finalized only for non coredump data
        elif event_tag == TAG_SERIAL_FLUSH:
            self.serial_handler.handle_serial_input(
                data,
                self.console_parser,
                self.coredump,
                self.gdb_helper,
                self._line_matcher,
                self.check_gdb_stub_and_run,
                finalize_line=not self.coredump.in_progress)
        else:
            raise RuntimeError('Bad event data %r' % ((event_tag, data), ))
Esempio n. 6
0
    def __init__(
            self,
            serial_instance,  # type: serial.Serial
            elf_file,  # type: str
            print_filter,  # type: str
            make='make',  # type: str
            encrypted=False,  # type: bool
            reset=True,  # type: bool
            toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,  # type: str
            eol='CRLF',  # type: str
            decode_coredumps=COREDUMP_DECODE_INFO,  # type: str
            decode_panic=PANIC_DECODE_DISABLE,  # type: str
            target='esp32',  # type: str
            websocket_client=None,  # type: Optional[WebSocketClient]
            enable_address_decoding=True,  # type: bool
            timestamps=False,  # type: bool
            timestamp_format='',  # type: str
            force_color=False  # type: bool
    ):
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()
        # if the variable is set ANSI will be printed even if we do not print to terminal
        sys.stderr = get_converter(sys.stderr,
                                   decode_output=True,
                                   force_color=force_color)
        self.console.output = get_converter(self.console.output,
                                            force_color=force_color)
        self.console.byte_output = get_converter(self.console.byte_output,
                                                 force_color=force_color)

        self.elf_file = elf_file or ''
        self.elf_exists = os.path.exists(self.elf_file)
        self.logger = Logger(self.elf_file, self.console, timestamps,
                             timestamp_format, b'', enable_address_decoding,
                             toolchain_prefix)

        self.coredump = CoreDump(decode_coredumps, self.event_queue,
                                 self.logger, websocket_client,
                                 self.elf_file) if self.elf_exists else None

        # allow for possibility the "make" arg is a list of arguments (for idf.py)
        self.make = make if os.path.exists(make) else shlex.split(
            make)  # type: Any[Union[str, List[str]], str]
        self.target = target

        # testing hook - data from serial can make exit the monitor
        if isinstance(self, SerialMonitor):
            socket_mode = serial_instance.port.startswith('socket://')
            self.serial = serial_instance
            self.serial_reader = SerialReader(self.serial, self.event_queue,
                                              reset)

            self.gdb_helper = GDBHelper(
                toolchain_prefix, websocket_client, self.elf_file,
                self.serial.port,
                self.serial.baudrate) if self.elf_exists else None

        else:
            socket_mode = False
            self.serial = subprocess.Popen([self.elf_file],
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT)
            self.serial_reader = LinuxReader(self.serial, self.event_queue)

            self.gdb_helper = None

        cls = SerialHandler if self.elf_exists else SerialHandlerNoElf
        self.serial_handler = cls(b'', socket_mode, self.logger, decode_panic,
                                  PANIC_IDLE, b'', target, False, False,
                                  self.serial, encrypted, reset, self.elf_file)

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)

        self._line_matcher = LineMatcher(print_filter)

        # internal state
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]
Esempio n. 7
0
class Monitor(object):
    """
    Monitor application main class.

    This was originally derived from miniterm.Miniterm, but it turned out to be easier to write from scratch for this
    purpose.

    Main difference is that all event processing happens in the main thread, not the worker threads.
    """
    def __init__(
            self,
            serial_instance,  # type: serial.Serial
            elf_file,  # type: str
            print_filter,  # type: str
            make='make',  # type: str
            encrypted=False,  # type: bool
            toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX,  # type: str
            eol='CRLF',  # type: str
            decode_coredumps=COREDUMP_DECODE_INFO,  # type: str
            decode_panic=PANIC_DECODE_DISABLE,  # type: str
            target='esp32',  # type: str
            websocket_client=None,  # type: WebSocketClient
            enable_address_decoding=True,  # type: bool
            timestamps=False,  # type: bool
            timestamp_format=''  # type: str
    ):
        super(Monitor, self).__init__()
        self.event_queue = queue.Queue()  # type: queue.Queue
        self.cmd_queue = queue.Queue()  # type: queue.Queue
        self.console = miniterm.Console()
        self.enable_address_decoding = enable_address_decoding

        sys.stderr = get_converter(sys.stderr, decode_output=True)
        self.console.output = get_converter(self.console.output)
        self.console.byte_output = get_converter(self.console.byte_output)

        socket_mode = serial_instance.port.startswith(
            'socket://'
        )  # testing hook - data from serial can make exit the monitor
        self.serial = serial_instance

        self.console_parser = ConsoleParser(eol)
        self.console_reader = ConsoleReader(self.console, self.event_queue,
                                            self.cmd_queue,
                                            self.console_parser, socket_mode)
        self.serial_reader = SerialReader(self.serial, self.event_queue)
        self.elf_file = elf_file
        if not os.path.exists(make):
            # allow for possibility the "make" arg is a list of arguments (for idf.py)
            self.make = shlex.split(make)  # type: Union[str, List[str]]
        else:
            self.make = make
        self.encrypted = encrypted
        self.toolchain_prefix = toolchain_prefix
        self.websocket_client = websocket_client
        self.target = target

        # internal state
        self._last_line_part = b''
        self._pc_address_buffer = b''
        self._line_matcher = LineMatcher(print_filter)
        self._invoke_processing_last_line_timer = None  # type: Optional[threading.Timer]
        self._force_line_print = False
        self._serial_check_exit = socket_mode
        self._decode_panic = decode_panic
        self._reading_panic = PANIC_IDLE
        self._panic_buffer = b''
        self.start_cmd_sent = False
        self.gdb_helper = GDBHelper(self.toolchain_prefix,
                                    self.websocket_client, self.elf_file,
                                    self.serial.port, self.serial.baudrate)

        self.logger = Logger(self.elf_file, self.console, timestamps,
                             timestamp_format)
        self.coredump = CoreDump(decode_coredumps, self.event_queue,
                                 self.logger, self.websocket_client,
                                 self.elf_file)

    def invoke_processing_last_line(self):
        # type: () -> None
        self.event_queue.put((TAG_SERIAL_FLUSH, b''), False)

    def main_loop(self):
        # type: () -> None
        self.console_reader.start()
        self.serial_reader.start()
        self.gdb_helper.gdb_exit = False
        self.start_cmd_sent = False
        try:
            while self.console_reader.alive and self.serial_reader.alive:
                try:
                    if self.gdb_helper.gdb_exit:
                        self.gdb_helper.gdb_exit = False

                        time.sleep(0.3)
                        try:
                            # Continue the program after exit from the GDB
                            self.serial.write(codecs.encode('+$c#63'))
                            self.start_cmd_sent = True
                        except serial.SerialException:
                            pass  # this shouldn't happen, but sometimes port has closed in serial thread
                        except UnicodeEncodeError:
                            pass  # this can happen if a non-ascii character was passed, ignoring

                    try:
                        item = self.cmd_queue.get_nowait()
                    except queue.Empty:
                        try:
                            item = self.event_queue.get(True, 0.03)
                        except queue.Empty:
                            continue

                    (event_tag, data) = item

                    if event_tag == TAG_CMD:
                        self.handle_commands(data, self.target)
                    elif event_tag == TAG_KEY:
                        try:
                            self.serial.write(codecs.encode(data))
                        except serial.SerialException:
                            pass  # this shouldn't happen, but sometimes port has closed in serial thread
                        except UnicodeEncodeError:
                            pass  # this can happen if a non-ascii character was passed, ignoring
                    elif event_tag == TAG_SERIAL:
                        self.handle_serial_input(data)
                        if self._invoke_processing_last_line_timer is not None:
                            self._invoke_processing_last_line_timer.cancel()
                        self._invoke_processing_last_line_timer = threading.Timer(
                            0.1, self.invoke_processing_last_line)
                        self._invoke_processing_last_line_timer.start()
                        # If no further data is received in the next short period
                        # of time then the _invoke_processing_last_line_timer
                        # generates an event which will result in the finishing of
                        # the last line. This is fix for handling lines sent
                        # without EOL.
                    elif event_tag == TAG_SERIAL_FLUSH:
                        self.handle_serial_input(data, finalize_line=True)
                    else:
                        raise RuntimeError('Bad event data %r' %
                                           ((event_tag, data), ))
                except KeyboardInterrupt:
                    try:
                        yellow_print(
                            'To exit from IDF monitor please use \"Ctrl+]\"')
                        self.serial.write(codecs.encode('\x03'))
                    except serial.SerialException:
                        pass  # this shouldn't happen, but sometimes port has closed in serial thread
                    except UnicodeEncodeError:
                        pass  # this can happen if a non-ascii character was passed, ignoring
        except SerialStopException:
            normal_print('Stopping condition has been received\n')
        except KeyboardInterrupt:
            pass
        finally:
            try:
                self.console_reader.stop()
                self.serial_reader.stop()
                self.logger.stop_logging()
                # Cancelling _invoke_processing_last_line_timer is not
                # important here because receiving empty data doesn't matter.
                self._invoke_processing_last_line_timer = None
            except Exception:
                pass
            normal_print('\n')

    def check_gdb_stub_and_run(self, line):  # type: (bytes) -> None
        if self.gdb_helper.check_gdb_stub_trigger(line):
            with self:  # disable console control
                self.gdb_helper.run_gdb()

    def handle_serial_input(self, data, finalize_line=False):
        # type: (bytes, bool) -> None
        # Remove "+" after Continue command
        if self.start_cmd_sent is True:
            self.start_cmd_sent = False
            pos = data.find(b'+')
            if pos != -1:
                data = data[(pos + 1):]

        sp = data.split(b'\n')
        if self._last_line_part != b'':
            # add unprocessed part from previous "data" to the first line
            sp[0] = self._last_line_part + sp[0]
            self._last_line_part = b''
        if sp[-1] != b'':
            # last part is not a full line
            self._last_line_part = sp.pop()
        for line in sp:
            if line == b'':
                continue
            if self._serial_check_exit and line == self.console_parser.exit_key.encode(
                    'latin-1'):
                raise SerialStopException()
            self.check_panic_decode_trigger(line)
            with self.coredump.check(line):
                if self._force_line_print or self._line_matcher.match(
                        line.decode(errors='ignore')):
                    self.logger.print(line + b'\n')
                    self.handle_possible_pc_address_in_line(line)
            self.check_gdb_stub_and_run(line)
            self._force_line_print = False
        # Now we have the last part (incomplete line) in _last_line_part. By
        # default we don't touch it and just wait for the arrival of the rest
        # of the line. But after some time when we didn't received it we need
        # to make a decision.
        force_print_or_matched = any(
            (self._force_line_print,
             (finalize_line and self._line_matcher.match(
                 self._last_line_part.decode(errors='ignore')))))
        if self._last_line_part != b'' and force_print_or_matched:
            self._force_line_print = True
            self.logger.print(self._last_line_part)
            self.handle_possible_pc_address_in_line(self._last_line_part)
            self.check_gdb_stub_and_run(self._last_line_part)
            # It is possible that the incomplete line cuts in half the PC
            # address. A small buffer is kept and will be used the next time
            # handle_possible_pc_address_in_line is invoked to avoid this problem.
            # MATCH_PCADDR matches 10 character long addresses. Therefore, we
            # keep the last 9 characters.
            self._pc_address_buffer = self._last_line_part[-9:]
            # GDB sequence can be cut in half also. GDB sequence is 7
            # characters long, therefore, we save the last 6 characters.
            self.gdb_helper.gdb_buffer = self._last_line_part[-6:]
            self._last_line_part = b''
        # else: keeping _last_line_part and it will be processed the next time
        # handle_serial_input is invoked

    def handle_possible_pc_address_in_line(self,
                                           line):  # type: (bytes) -> None
        line = self._pc_address_buffer + line
        self._pc_address_buffer = b''
        if not self.enable_address_decoding:
            return
        for m in re.finditer(MATCH_PCADDR, line.decode(errors='ignore')):
            translation = lookup_pc_address(m.group(), self.toolchain_prefix,
                                            self.elf_file)
            if translation:
                self.logger.print(translation, console_printer=yellow_print)

    def __enter__(self):
        # type: () -> None
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.serial_reader.stop()
        self.console_reader.stop()

    def __exit__(self, *args, **kwargs):  # type: ignore
        """ Use 'with self' to temporarily disable monitoring behaviour """
        self.console_reader.start()
        self.serial_reader.gdb_exit = self.gdb_helper.gdb_exit  # write gdb_exit flag
        self.serial_reader.start()

    def run_make(self, target):  # type: (str) -> None
        with self:
            if isinstance(self.make, list):
                popen_args = self.make + [target]
            else:
                popen_args = [self.make, target]
            yellow_print('Running %s...' % ' '.join(popen_args))
            p = subprocess.Popen(popen_args, env=os.environ)
            try:
                p.wait()
            except KeyboardInterrupt:
                p.wait()
            if p.returncode != 0:
                prompt_next_action('Build failed', self.console,
                                   self.console_parser, self.event_queue,
                                   self.cmd_queue)
            else:
                self.logger.output_enabled = True

    def check_panic_decode_trigger(self, line):  # type: (bytes) -> None
        if self._decode_panic == PANIC_DECODE_DISABLE:
            return

        if self._reading_panic == PANIC_IDLE and re.search(
                PANIC_START, line.decode('ascii', errors='ignore')):
            self._reading_panic = PANIC_READING
            yellow_print('Stack dump detected')

        if self._reading_panic == PANIC_READING and PANIC_STACK_DUMP in line:
            self.logger.output_enabled = False

        if self._reading_panic == PANIC_READING:
            self._panic_buffer += line.replace(b'\r', b'') + b'\n'

        if self._reading_panic == PANIC_READING and PANIC_END in line:
            self._reading_panic = PANIC_IDLE
            self.logger.output_enabled = True
            self.gdb_helper.process_panic_output(self._panic_buffer,
                                                 self.logger, self.target)
            self._panic_buffer = b''

    def handle_commands(self, cmd, chip):  # type: (int, str) -> None
        config = get_chip_config(chip)
        reset_delay = config['reset']
        enter_boot_set = config['enter_boot_set']
        enter_boot_unset = config['enter_boot_unset']

        high = False
        low = True

        if cmd == CMD_STOP:
            self.console_reader.stop()
            self.serial_reader.stop()
        elif cmd == CMD_RESET:
            self.serial.setRTS(low)
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(reset_delay)
            self.serial.setRTS(high)
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            self.logger.output_enabled = True
        elif cmd == CMD_MAKE:
            self.run_make('encrypted-flash' if self.encrypted else 'flash')
        elif cmd == CMD_APP_FLASH:
            self.run_make(
                'encrypted-app-flash' if self.encrypted else 'app-flash')
        elif cmd == CMD_OUTPUT_TOGGLE:
            self.logger.output_toggle()
        elif cmd == CMD_TOGGLE_LOGGING:
            self.logger.toggle_logging()
        elif cmd == CMD_TOGGLE_TIMESTAMPS:
            self.logger.toggle_timestamps()
            self.logger.toggle_logging()
        elif cmd == CMD_ENTER_BOOT:
            self.serial.setDTR(high)  # IO0=HIGH
            self.serial.setRTS(low)  # EN=LOW, chip in reset
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(
                enter_boot_set
            )  # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.1
            self.serial.setDTR(low)  # IO0=LOW
            self.serial.setRTS(high)  # EN=HIGH, chip out of reset
            self.serial.setDTR(self.serial.dtr)  # usbser.sys workaround
            time.sleep(
                enter_boot_unset
            )  # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.05
            self.serial.setDTR(high)  # IO0=HIGH, done
        else:
            raise RuntimeError('Bad command data %d' % cmd)  # type: ignore