Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[microTVM] Update Zephyr 2.5 #7786

Merged
merged 22 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add qemu debugging
  • Loading branch information
areusch committed Apr 1, 2021
commit 9244303f51fc320c0e7523e9337c6d434e05965a
4 changes: 4 additions & 0 deletions apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ while [ "$#" -gt 0 ]; do
shift
done

if [ "${TVM_QEMU_DEBUG}" != "" ]; then
ARGS=( "${ARGS[@]}" -s -S )
fi

"${ARGS[@]}"
58 changes: 54 additions & 4 deletions python/tvm/micro/contrib/zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"""Defines a compiler integration that uses an externally-supplied Zephyr project."""

import collections
import copy
import logging
import multiprocessing
import os
import pathlib
import re
import tempfile
import textwrap
Expand Down Expand Up @@ -428,12 +430,28 @@ def _get_device_args(self, cmake_entries):
f"runner {flash_runner}"
)

def _zephyr_transport(self, micro_binary):
qemu_debugger = None
if self._debug_rpc_session:
qemu_debugger = debugger.RpcDebugger(
self._debug_rpc_session,
debugger.DebuggerFactory(
QemuGdbDebugger,
(
micro_binary.abspath(micro_binary.debug_files[0]),
),
{},
),
)

return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger)

def flash(self, micro_binary):
cmake_entries = read_cmake_cache(
micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0])
)
if "qemu" in cmake_entries["BOARD"]:
return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0)
return self._zephyr_transport(micro_binary)

build_dir = os.path.dirname(
micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0])
Expand Down Expand Up @@ -532,6 +550,22 @@ def transport(self, micro_binary):
)


class QemuGdbDebugger(debugger.GdbDebugger):

def __init__(self, elf_file):
super(QemuGdbDebugger, self).__init__()
self._elf_file = elf_file

def popen_kwargs(self):
# expect self._elf file to follow the form .../zephyr/zephyr.elf
cmake_cache_path = (
pathlib.Path(self._elf_file).parent.parent / "CMakeCache.txt")
cmake_cache = read_cmake_cache(cmake_cache_path)
return {
"args": [cmake_cache["CMAKE_GDB"], "-ex", "target remote localhost:1234", "-ex", f"file {self._elf_file}"],
}


class QemuStartupFailureError(Exception):
"""Raised when the qemu pipe is not present within startup_timeout_sec."""

Expand Down Expand Up @@ -571,19 +605,20 @@ def write(self, data, timeout_sec):
class ZephyrQemuTransport(Transport):
"""The user-facing Zephyr QEMU transport class."""

def __init__(self, base_dir, startup_timeout_sec=5.0, **kwargs):
def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs):
self.base_dir = base_dir
self.startup_timeout_sec = startup_timeout_sec
self.kwargs = kwargs
self.proc = None
self.fd_transport = None
self.pipe_dir = None
self.debugger = debugger

def timeouts(self):
return TransportTimeouts(
session_start_retry_timeout_sec=2.0,
session_start_timeout_sec=self.startup_timeout_sec,
session_established_timeout_sec=5.0,
session_established_timeout_sec=5.0 if self.debugger is None else 0,
)

def open(self):
Expand All @@ -593,11 +628,23 @@ def open(self):
self.read_pipe = os.path.join(self.pipe_dir, "fifo.out")
os.mkfifo(self.write_pipe)
os.mkfifo(self.read_pipe)
if self.debugger is not None:
if 'env' in self.kwargs:
self.kwargs["env"] = copy.copy(self.kwargs["env"])
else:
self.kwargs["env"] = copy.copy(os.environ)

self.kwargs["env"]["TVM_QEMU_DEBUG"] = "1"

self.proc = subprocess.Popen(
["make", "run", f"QEMU_PIPE={self.pipe}"],
cwd=self.base_dir,
**self.kwargs,
)

if self.debugger is not None:
self.debugger.start()

# NOTE: although each pipe is unidirectional, open both as RDWR to work around a select
# limitation on linux. Without this, non-blocking I/O can't use timeouts because named
# FIFO are always considered ready to read when no one has opened them for writing.
Expand All @@ -612,6 +659,9 @@ def open(self):
self.fd_transport.open()

def close(self):
if self.debugger is not None:
self.debugger.stop()

if self.fd_transport is not None:
self.fd_transport.child_transport.write_monitor_quit()
self.proc.wait()
Expand Down Expand Up @@ -653,7 +703,7 @@ def popen_kwargs(self):
args = dict(
args=self._west_cmd
+ [
"attach",
"debug",
"--skip-rebuild",
"--build-dir",
self._build_dir,
Expand Down