mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-09 22:22:38 +00:00
python: add detailed logging to emulator runner
This commit is contained in:
parent
1bcf856946
commit
271da3fa39
10
core/emu.py
10
core/emu.py
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import gzip
|
import gzip
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import signal
|
import signal
|
||||||
@ -54,7 +55,6 @@ def watch_emulator(emulator):
|
|||||||
try:
|
try:
|
||||||
for _, type_names, _, _ in watch.event_gen(yield_nones=False):
|
for _, type_names, _, _ in watch.event_gen(yield_nones=False):
|
||||||
if "IN_CLOSE_WRITE" in type_names:
|
if "IN_CLOSE_WRITE" in type_names:
|
||||||
click.echo("Restarting...")
|
|
||||||
emulator.restart()
|
emulator.restart()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
emulator.stop()
|
emulator.stop()
|
||||||
@ -198,6 +198,10 @@ def cli(
|
|||||||
if quiet:
|
if quiet:
|
||||||
output = None
|
output = None
|
||||||
|
|
||||||
|
logger = logging.getLogger("trezorlib._internal.emulator")
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logger.addHandler(logging.StreamHandler())
|
||||||
|
|
||||||
emulator = CoreEmulator(
|
emulator = CoreEmulator(
|
||||||
executable,
|
executable,
|
||||||
profile_dir,
|
profile_dir,
|
||||||
@ -230,11 +234,7 @@ def cli(
|
|||||||
run_debugger(emulator)
|
run_debugger(emulator)
|
||||||
raise RuntimeError("run_debugger should not return")
|
raise RuntimeError("run_debugger should not return")
|
||||||
|
|
||||||
click.echo("Waiting for emulator to come up... ", err=True)
|
|
||||||
start = time.monotonic()
|
|
||||||
emulator.start()
|
emulator.start()
|
||||||
end = time.monotonic()
|
|
||||||
click.echo(f"Emulator ready after {end - start:.3f} seconds", err=True)
|
|
||||||
|
|
||||||
if mnemonics:
|
if mnemonics:
|
||||||
if slip0014:
|
if slip0014:
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
# You should have received a copy of the License along with this library.
|
# You should have received a copy of the License along with this library.
|
||||||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
||||||
|
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
@ -22,6 +23,10 @@ from pathlib import Path
|
|||||||
from trezorlib.debuglink import TrezorClientDebugLink
|
from trezorlib.debuglink import TrezorClientDebugLink
|
||||||
from trezorlib.transport.udp import UdpTransport
|
from trezorlib.transport.udp import UdpTransport
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
EMULATOR_WAIT_TIME = 60
|
||||||
|
|
||||||
|
|
||||||
def _rm_f(path):
|
def _rm_f(path):
|
||||||
try:
|
try:
|
||||||
@ -84,9 +89,10 @@ class Emulator:
|
|||||||
def _get_transport(self):
|
def _get_transport(self):
|
||||||
return UdpTransport("127.0.0.1:{}".format(self.port))
|
return UdpTransport("127.0.0.1:{}".format(self.port))
|
||||||
|
|
||||||
def wait_until_ready(self, timeout=60):
|
def wait_until_ready(self, timeout=EMULATOR_WAIT_TIME):
|
||||||
transport = self._get_transport()
|
transport = self._get_transport()
|
||||||
transport.open()
|
transport.open()
|
||||||
|
LOG.info("Waiting for emulator to come up...")
|
||||||
start = time.monotonic()
|
start = time.monotonic()
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@ -103,8 +109,11 @@ class Emulator:
|
|||||||
finally:
|
finally:
|
||||||
transport.close()
|
transport.close()
|
||||||
|
|
||||||
|
LOG.info("Emulator ready after {:.3f} seconds".format(time.monotonic() - start))
|
||||||
|
|
||||||
def wait(self, timeout=None):
|
def wait(self, timeout=None):
|
||||||
ret = self.process.wait(timeout=timeout)
|
ret = self.process.wait(timeout=timeout)
|
||||||
|
self.process = None
|
||||||
self.stop()
|
self.stop()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -129,6 +138,7 @@ class Emulator:
|
|||||||
if self.process:
|
if self.process:
|
||||||
if self.process.poll() is not None:
|
if self.process.poll() is not None:
|
||||||
# process has died, stop and start again
|
# process has died, stop and start again
|
||||||
|
LOG.info("Starting from a stopped process.")
|
||||||
self.stop()
|
self.stop()
|
||||||
else:
|
else:
|
||||||
# process is running, no need to start again
|
# process is running, no need to start again
|
||||||
@ -139,6 +149,9 @@ class Emulator:
|
|||||||
self.wait_until_ready()
|
self.wait_until_ready()
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
# Assuming that after the default 60-second timeout, the process is stuck
|
# Assuming that after the default 60-second timeout, the process is stuck
|
||||||
|
LOG.warning(
|
||||||
|
"Emulator did not come up after {} seconds".format(EMULATOR_WAIT_TIME)
|
||||||
|
)
|
||||||
self.process.kill()
|
self.process.kill()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@ -156,10 +169,15 @@ class Emulator:
|
|||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
if self.process:
|
if self.process:
|
||||||
|
LOG.info("Terminating emulator...")
|
||||||
|
start = time.monotonic()
|
||||||
self.process.terminate()
|
self.process.terminate()
|
||||||
try:
|
try:
|
||||||
self.process.wait(30)
|
self.process.wait(EMULATOR_WAIT_TIME)
|
||||||
|
end = time.monotonic()
|
||||||
|
LOG.info("Emulator shut down after {:.3f} seconds".format(end - start))
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
|
LOG.info("Emulator seems stuck. Sending kill signal.")
|
||||||
self.process.kill()
|
self.process.kill()
|
||||||
|
|
||||||
_rm_f(self.profile_dir / "trezor.pid")
|
_rm_f(self.profile_dir / "trezor.pid")
|
||||||
|
Loading…
Reference in New Issue
Block a user