1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

feat(core/emulator): support protobuf messages in memory dumps

[no changelog]
This commit is contained in:
Martin Milata 2021-07-19 17:05:58 +02:00
parent c1843f9f9f
commit 582e1318c4
4 changed files with 61 additions and 3 deletions

View File

@ -33,6 +33,7 @@
#include "py/objtype.h"
#include "embed/extmod/trezorobj.h"
#include "embed/rust/librust.h"
#include "embed/trezorhal/usb.h"
#include <string.h>
@ -301,6 +302,11 @@ typedef struct _mp_obj_HID_t {
usb_hid_info_t info;
} mp_obj_HID_t;
typedef struct _mp_obj_protomsg_t {
mp_obj_base_t base;
mp_map_t map;
} mp_obj_protomsg_t;
void dump_bound_method(FILE *out, const mp_obj_bound_meth_t *meth) {
print_type(out, "method", NULL, meth, false);
@ -487,6 +493,29 @@ void dump_trezor_vcp(FILE *out, const mp_obj_VCP_t *vcp) {
fprintf(out, ",\n");
}
void dump_protomsg(FILE *out, const mp_obj_protomsg_t *value) {
mp_obj_t name[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
mp_obj_type_t *type = protobuf_debug_msg_type();
type->attr((mp_obj_t)value, MP_QSTR_MESSAGE_NAME, name);
print_type(out, "protomsg", NULL, value, false);
fprintf(out, ",\n\"message_name\": ");
dump_short(out, name[0]);
dump_map_as_children(out, &value->map);
fprintf(out, "},\n");
}
void dump_protodef(FILE *out, const mp_obj_t *value) {
mp_obj_t name[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
mp_obj_type_t *type = protobuf_debug_msg_def_type();
type->attr((mp_obj_t)value, MP_QSTR_MESSAGE_NAME, name);
print_type(out, "protodef", NULL, value, false);
fprintf(out, ",\n\"message_name\": ");
dump_short(out, name[0]);
fprintf(out, "},\n");
}
void dump_value_opt(FILE *out, mp_const_obj_t value, bool eval_short) {
if (!eval_short && is_short(value)) return;
@ -603,6 +632,14 @@ void dump_value_opt(FILE *out, mp_const_obj_t value, bool eval_short) {
fprintf(out, ",\n");
}
else if (mp_obj_is_type(value, protobuf_debug_msg_type())) {
dump_protomsg(out, value);
}
else if (mp_obj_is_type(value, protobuf_debug_msg_def_type())) {
dump_protodef(out, value);
}
else {
print_type(out, "unknown", NULL, value, true);
fprintf(out, ",\n");

View File

@ -6,3 +6,8 @@ mp_obj_t protobuf_decode(mp_obj_t buf, mp_obj_t def,
mp_obj_t enable_experimental);
mp_obj_t protobuf_len(mp_obj_t obj);
mp_obj_t protobuf_encode(mp_obj_t buf, mp_obj_t obj);
#ifdef TREZOR_EMULATOR
mp_obj_t protobuf_debug_msg_type();
mp_obj_t protobuf_debug_msg_def_type();
#endif

View File

@ -262,3 +262,13 @@ unsafe extern "C" fn msg_def_obj_is_type_of(self_in: Obj, obj: Obj) -> Obj {
}
static MSG_DEF_OBJ_IS_TYPE_OF_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_2!(msg_def_obj_is_type_of);
#[no_mangle]
pub extern "C" fn protobuf_debug_msg_type() -> &'static Type {
MsgObj::obj_type()
}
#[no_mangle]
pub extern "C" fn protobuf_debug_msg_def_type() -> &'static Type {
MsgDefObj::obj_type()
}

View File

@ -56,6 +56,10 @@ def ptr_or_shortval(maybe_ptr):
return maybe_ptr["shortval"]
def is_ignored_ptr(ptr):
return (ptr == "(nil)" or ptr.startswith("0x5") or ptr.startswith("0x6"))
def deref_or_shortval(maybe_ptr):
if is_ptr(maybe_ptr) and maybe_ptr in MEMORY:
return MEMORY[maybe_ptr]
@ -154,7 +158,7 @@ allobjs.sort(key=lambda x: x.ptr)
min_ptr = min(
item.ptrval()
for item in allobjs
if item.ptr != "(nil)" and not item.ptr.startswith("0x5")
if not is_ignored_ptr(item.ptr)
)
max_ptr = max(item.ptrval() for item in allobjs if item.ptr != "(nil)")
@ -188,6 +192,8 @@ types = {
"rawbuffer": "R",
"qstrpool": "Q",
"qstrdata": "q",
"protomsg": "P",
"protodef": "p",
}
pixels_per_line = len(
@ -207,7 +213,7 @@ def pixel_index(ptrval):
for item in MEMORY.values():
if item.alloc == 0:
continue
if item.ptr.startswith("0x5"):
if is_ignored_ptr(item.ptr):
continue
ptridx = pixel_index(item.ptrval())
assert ptridx >= 0, item.item
@ -217,7 +223,7 @@ for item in MEMORY.values():
for item in MEMORY.values():
if item.alloc > 0:
continue
if item.ptr == "(nil)" or item.ptr.startswith("0x5"):
if is_ignored_ptr(item.ptr):
continue
ptridx = pixel_index(item.ptrval())
if ptridx < 0: