mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-08 22:40:59 +00:00
feat(core): in debug mode, dump contents of received protobuf messages
[no changelog]
This commit is contained in:
parent
8da978981e
commit
cb882df100
@ -35,6 +35,8 @@ if False:
|
||||
Set,
|
||||
)
|
||||
|
||||
from trezor.protobuf import MessageType
|
||||
|
||||
|
||||
def unimport_begin() -> Set[str]:
|
||||
return set(sys.modules)
|
||||
@ -353,3 +355,34 @@ def empty_bytearray(preallocate: int) -> bytearray:
|
||||
b = bytearray(preallocate)
|
||||
b[:] = bytes()
|
||||
return b
|
||||
|
||||
|
||||
if __debug__:
|
||||
|
||||
def dump_protobuf_lines(msg: MessageType, line_start: str = "") -> Iterator[str]:
|
||||
msg_dict = msg.__dict__
|
||||
if not msg_dict:
|
||||
yield line_start + msg.MESSAGE_NAME + " {}"
|
||||
return
|
||||
|
||||
yield line_start + msg.MESSAGE_NAME + " {"
|
||||
for key, val in msg_dict.items():
|
||||
if type(val) == type(msg):
|
||||
sublines = dump_protobuf_lines(val, line_start=key + ": ")
|
||||
for subline in sublines:
|
||||
yield " " + subline
|
||||
elif val and isinstance(val, list) and type(val[0]) == type(msg):
|
||||
# non-empty list of protobuf messages
|
||||
yield " {}: [".format(key)
|
||||
for subval in val:
|
||||
sublines = dump_protobuf_lines(subval)
|
||||
for subline in sublines:
|
||||
yield " " + subline
|
||||
yield " ]"
|
||||
else:
|
||||
yield " {}: {!r}".format(key, val)
|
||||
|
||||
yield "}"
|
||||
|
||||
def dump_protobuf(msg: MessageType) -> str:
|
||||
return "\n".join(dump_protobuf_lines(msg))
|
||||
|
@ -101,7 +101,12 @@ def _wrap_protobuf_load(
|
||||
expected_type: type[LoadedMessageType],
|
||||
) -> LoadedMessageType:
|
||||
try:
|
||||
return protobuf.decode(buffer, expected_type, experimental_enabled)
|
||||
msg = protobuf.decode(buffer, expected_type, experimental_enabled)
|
||||
if __debug__ and utils.EMULATOR:
|
||||
log.debug(
|
||||
__name__, "received message contents:\n%s", utils.dump_protobuf(msg)
|
||||
)
|
||||
return msg
|
||||
except Exception as e:
|
||||
if __debug__:
|
||||
log.exception(__name__, e)
|
||||
|
Loading…
Reference in New Issue
Block a user