From d3ce492cc2c7e6a161548c2f86f10fa2c3e7e979 Mon Sep 17 00:00:00 2001 From: "fu.lin" Date: Fri, 25 Jun 2021 19:52:33 +0800 Subject: [PATCH] pycrit: fix the broken of cli the `crit show xxx.img` It will broken when the cli `crit show ipcns-shm-9.img` is executed, msg: { "magic": "IPCNS_SHM", "entries": [ { "desc": { "key": 0, "uid": 0, "gid": 0, "cuid": 0, "cgid": 0, "mode": 438, "id": 0 }, "size": 1048576, "in_pagemaps": true, "extra": Traceback (most recent call last): File "/usr/bin/crit", line 6, in cli.main() File "/usr/lib/python3/dist-packages/pycriu/cli.py", line 412, in main opts["func"](opts) File "/usr/lib/python3/dist-packages/pycriu/cli.py", line 45, in decode json.dump(img, f, indent=indent) File "/usr/lib/python3.9/json/__init__.py", line 179, in dump for chunk in iterable: File "/usr/lib/python3.9/json/encoder.py", line 431, in _iterencode yield from _iterencode_dict(o, _current_indent_level) File "/usr/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.9/json/encoder.py", line 325, in _iterencode_list yield from chunks File "/usr/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.9/json/encoder.py", line 438, in _iterencode o = _default(o) File "/usr/lib/python3.9/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable This is caused by `img['magic'][0]['extra']` which is bytes. I find other load condtions, fix them at the same time. Signed-off-by: fu.lin --- lib/py/images/images.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/py/images/images.py b/lib/py/images/images.py index 956932e03..95a10dd80 100644 --- a/lib/py/images/images.py +++ b/lib/py/images/images.py @@ -311,7 +311,7 @@ class pipes_data_extra_handler: def load(self, f, pload): size = pload.bytes data = f.read(size) - return base64.encodebytes(data).decode() + return base64.encodebytes(data).decode('utf-8') def dump(self, extra, f, pload): if (sys.version_info > (3, 0)): @@ -329,7 +329,7 @@ class sk_queues_extra_handler: def load(self, f, pload): size = pload.length data = f.read(size) - return base64.encodebytes(data).decode() + return base64.encodebytes(data).decode('utf-8') def dump(self, extra, f, _unused): if (sys.version_info > (3, 0)): @@ -350,8 +350,8 @@ class tcp_stream_extra_handler: inq = f.read(pbuff.inq_len) outq = f.read(pbuff.outq_len) - d['inq'] = base64.encodebytes(inq) - d['outq'] = base64.encodebytes(outq) + d['inq'] = base64.encodebytes(inq).decode('utf-8') + d['outq'] = base64.encodebytes(outq).decode('utf-8') return d @@ -370,7 +370,7 @@ class bpfmap_data_extra_handler: def load(self, f, pload): size = pload.keys_bytes + pload.values_bytes data = f.read(size) - return base64.encodebytes(data) + return base64.encodebytes(data).decode('utf-8') def dump(self, extra, f, pload): data = base64.decodebytes(extra) @@ -427,7 +427,7 @@ class ipc_msg_queue_handler: data = f.read(msg.msize) f.seek(rounded - msg.msize, 1) messages.append(pb2dict.pb2dict(msg)) - messages.append(base64.encodebytes(data)) + messages.append(base64.encodebytes(data).decode('utf-8')) return messages def dump(self, extra, f, pbuff): @@ -467,7 +467,7 @@ class ipc_shm_handler: data = f.read(size) rounded = round_up(size, sizeof_u32) f.seek(rounded - size, 1) - return base64.encodebytes(data) + return base64.encodebytes(data).decode('utf-8') def dump(self, extra, f, pbuff): entry = pb2dict.pb2dict(pbuff)