我有一个这样的 C 结构:
typedef uint8_t matrix_size_t;
struct _matrix {
/* data */
uint8_t inited;
matrix_size_t rows;
matrix_size_t cols;
ElemType* data;
};
我想用行和列格式打印 Matrix.data,这是我的 python 代码:
import gdb
import numpy as np
import logging
class McuMatrixPrinter:
def __init__(self, variety, val):
self.variety = variety
self.inited = val["inited"]
self.rows = int(val["rows"])
self.cols = int(val["cols"])
self.data = val["data"]
self.type = self.data.type.unqualified().strip_typedefs()
self.data = self.data.cast(self.type.pointer())
logging.info(self.type)
logging.info(type(self.data))
def to_string(self):
if int(self.inited) == 0:
return "This matrix has not been inited!"
mat = np.zeros((self.rows, self.cols), dtype=np.float64)
for row in range(self.rows):
for col in range(self.cols):
offset = row * self.cols + col
item = (self.data + offset).dereference()
logging.info(type(item))
mat[row, col] = float(item)
return "Matrix::%s<%d,%d>\n%s\n" % (self.variety, self.rows, self.cols,
self.data, mat)
def PrintMatrix(val):
type = val.type
if type.code == gdb.TYPE_CODE_REF:
type = type.target()
type = type.unqualified().strip_typedefs()
typename = type.tag
if typename == None:
return None
if typename.find("_matrix") != -1:
return McuMatrixPrinter("Matrix", val)
return None
def register_matrix_printers(obj):
"Register eigen pretty-printers with objfile Obj"
logging.basicConfig(
level=logging.DEBUG,
filename='/tmp/gdbpy.log',
filemode='a',
format=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
)
if obj == None:
obj = gdb
obj.pretty_printers.append(PrintMatrix)
然而,我得到了这个例外:
(gdb) p 向量
$1 = Python 异常
(gdb) p 向量
$2 = Python 异常
有解决方案可以解决我的问题吗?