提交 e137cb82 编写于 作者: M Megvii Engine Team

fix(debug): format gdb script

GitOrigin-RevId: 280fa872656bd418cb4656f5498686efaf63e01a
上级 9166844f
import sys
import gdb
import re import re
import subprocess import subprocess
import sys
import gdb
_demangle_cache = {} _demangle_cache = {}
def demangle(name): def demangle(name):
if name not in _demangle_cache: if name not in _demangle_cache:
_demangle_cache[name] = subprocess.run(['c++filt', "-t", name], stdout=subprocess.PIPE).stdout _demangle_cache[name] = subprocess.run(
return _demangle_cache[name].decode('utf-8').strip() ["c++filt", "-t", name], stdout=subprocess.PIPE
).stdout
return _demangle_cache[name].decode("utf-8").strip()
def dynamic_cast(val): def dynamic_cast(val):
...@@ -52,11 +53,21 @@ def shared_ptr_deref(ptr): ...@@ -52,11 +53,21 @@ def shared_ptr_deref(ptr):
def get_type_name(type_index): def get_type_name(type_index):
return gdb.lookup_global_symbol("mgb::imperative::debug::get_type_name(std::type_index const&)").value()(type_index).string() return (
gdb.lookup_global_symbol(
"mgb::imperative::debug::get_type_name(std::type_index const&)"
)
.value()(type_index)
.string()
)
mge_apply_transform_pattern = re.compile(r"^(.*)::apply_transform\(mgb::imperative::Operator const&, mgb::imperative::Span<mgb::imperative::ValueRef>\)$") mge_apply_transform_pattern = re.compile(
mge_op_fallback_pattern = re.compile(r"^(.*)::fallback\(mgb::imperative::Span<mgb::imperative::ValueRef>\) const$") r"^(.*)::apply_transform\(mgb::imperative::Operator const&, mgb::imperative::Span<mgb::imperative::ValueRef>\)$"
)
mge_op_fallback_pattern = re.compile(
r"^(.*)::fallback\(mgb::imperative::Span<mgb::imperative::ValueRef>\) const$"
)
def is_mge_frame(frame): def is_mge_frame(frame):
...@@ -66,11 +77,15 @@ def is_mge_frame(frame): ...@@ -66,11 +77,15 @@ def is_mge_frame(frame):
matcher = mge_apply_transform_pattern.match(function.name) matcher = mge_apply_transform_pattern.match(function.name)
if matcher: if matcher:
typename = matcher.group(1) typename = matcher.group(1)
return is_subclass_of(gdb.lookup_type(typename), gdb.lookup_type("mgb::imperative::Transform")) return is_subclass_of(
gdb.lookup_type(typename), gdb.lookup_type("mgb::imperative::Transform")
)
matcher = mge_op_fallback_pattern.match(function.name) matcher = mge_op_fallback_pattern.match(function.name)
if matcher: if matcher:
typename = matcher.group(1) typename = matcher.group(1)
return is_subclass_of(gdb.lookup_type(typename), gdb.lookup_type("mgb::imperative::Operator")) return is_subclass_of(
gdb.lookup_type(typename), gdb.lookup_type("mgb::imperative::Operator")
)
return False return False
...@@ -84,18 +99,24 @@ def count_frame_level(frame): ...@@ -84,18 +99,24 @@ def count_frame_level(frame):
def print_mge_frame(frame): def print_mge_frame(frame):
function = frame.function() function = frame.function()
op = eval_on_val(dynamic_cast(frame.read_var("op")), ".to_string().c_str()").string() op = eval_on_val(
dynamic_cast(frame.read_var("op")), ".to_string().c_str()"
).string()
inputs = str(frame.read_var("inputs")) inputs = str(frame.read_var("inputs"))
matcher = mge_apply_transform_pattern.match(function.name) matcher = mge_apply_transform_pattern.match(function.name)
if matcher: if matcher:
name = matcher.group(1) name = matcher.group(1)
else: else:
name = mge_op_fallback_pattern.match(function.name).group(1) name = mge_op_fallback_pattern.match(function.name).group(1)
#TODO: span # TODO: span
sal = frame.find_sal() sal = frame.find_sal()
filename = sal.symtab.filename filename = sal.symtab.filename
line = sal.line line = sal.line
print("#{} {} apply {} on {}, file: {}:{}".format(count_frame_level(frame), name, op, inputs, filename, line)) print(
"#{} {} apply {} on {}, file: {}:{}".format(
count_frame_level(frame), name, op, inputs, filename, line
)
)
class MegengineBacktrace(gdb.Command): class MegengineBacktrace(gdb.Command):
...@@ -118,7 +139,9 @@ class MegengineBreakApply(gdb.Command): ...@@ -118,7 +139,9 @@ class MegengineBreakApply(gdb.Command):
super().__init__("mge-brk-apply", gdb.COMMAND_USER) super().__init__("mge-brk-apply", gdb.COMMAND_USER)
def invoke(self, arg, from_tty): def invoke(self, arg, from_tty):
gdb.Breakpoint("mgb::imperative::apply(mgb::imperative::Operator const&, mgb::imperative::Span<mgb::imperative::ValueRef>)") gdb.Breakpoint(
"mgb::imperative::apply(mgb::imperative::Operator const&, mgb::imperative::Span<mgb::imperative::ValueRef>)"
)
class MegengineWatch(gdb.Command): class MegengineWatch(gdb.Command):
...@@ -126,7 +149,9 @@ class MegengineWatch(gdb.Command): ...@@ -126,7 +149,9 @@ class MegengineWatch(gdb.Command):
super().__init__("mge-watch", gdb.COMMAND_USER) super().__init__("mge-watch", gdb.COMMAND_USER)
def invoke(self, arg, from_tty): def invoke(self, arg, from_tty):
watch = gdb.lookup_global_symbol("mgb::imperative::debug::watch_value(mgb::imperative::ValueRef)").value() watch = gdb.lookup_global_symbol(
"mgb::imperative::debug::watch_value(mgb::imperative::ValueRef)"
).value()
value = gdb.parse_and_eval(arg) value = gdb.parse_and_eval(arg)
watch(value) watch(value)
print("watching {}".format(str(value))) print("watching {}".format(str(value)))
...@@ -176,7 +201,9 @@ class MegengineInfo(gdb.Command): ...@@ -176,7 +201,9 @@ class MegengineInfo(gdb.Command):
def invoke(self, arg, from_tty): def invoke(self, arg, from_tty):
if arg == "opr": if arg == "opr":
registered_oprs = gdb.lookup_global_symbol("mgb::imperative::Operator::registered_types()").value()() registered_oprs = gdb.lookup_global_symbol(
"mgb::imperative::Operator::registered_types()"
).value()()
size = vector_size(registered_oprs) size = vector_size(registered_oprs)
for i in range(size): for i in range(size):
registered_opr = vector_item(registered_oprs, i) registered_opr = vector_item(registered_oprs, i)
...@@ -184,7 +211,9 @@ class MegengineInfo(gdb.Command): ...@@ -184,7 +211,9 @@ class MegengineInfo(gdb.Command):
name = get_type_name(registered_opr) name = get_type_name(registered_opr)
print("{}: {}".format(i, demangle(name))) print("{}: {}".format(i, demangle(name)))
elif arg == "trf": elif arg == "trf":
dispatch_context = gdb.lookup_global_symbol("mgb::imperative::Transform::get_context()").value()() dispatch_context = gdb.lookup_global_symbol(
"mgb::imperative::Transform::get_context()"
).value()()
transformations = dispatch_context["transformations"] transformations = dispatch_context["transformations"]
size = vector_size(transformations) size = vector_size(transformations)
for i in range(size): for i in range(size):
...@@ -207,7 +236,7 @@ if sys.version_info.major > 2: ...@@ -207,7 +236,7 @@ if sys.version_info.major > 2:
MegengineDown() MegengineDown()
MegengineInfo() MegengineInfo()
MegengineWatch() MegengineWatch()
gdb.Breakpoint("mgb::imperative::debug::notify_event(char const*)") gdb.Breakpoint("mgb::imperative::debug::notify_event(char const*)")
else: else:
print("skip import commands") print("skip import commands")
import sys import sys
import gdb import gdb
import gdb.printing import gdb.printing
import gdb.types import gdb.types
...@@ -21,38 +22,40 @@ def eval_on_val(val, eval_str): ...@@ -21,38 +22,40 @@ def eval_on_val(val, eval_str):
class SmallVectorPrinter: class SmallVectorPrinter:
def __init__(self, val): def __init__(self, val):
t = val.type.template_argument(0) t = val.type.template_argument(0)
self.begin = val['m_begin_ptr'].cast(t.pointer()) self.begin = val["m_begin_ptr"].cast(t.pointer())
self.end = val['m_end_ptr'].cast(t.pointer()) self.end = val["m_end_ptr"].cast(t.pointer())
self.size = self.end - self.begin self.size = self.end - self.begin
self.capacity = val['m_capacity_ptr'].cast(t.pointer()) - val['m_begin_ptr'].cast(t.pointer()) self.capacity = val["m_capacity_ptr"].cast(t.pointer()) - val[
"m_begin_ptr"
].cast(t.pointer())
def to_string(self): def to_string(self):
return 'SmallVector of Size {}'.format(self.size) return "SmallVector of Size {}".format(self.size)
def display_hint(self): def display_hint(self):
return 'array' return "array"
def children(self): def children(self):
for i in range(self.size): for i in range(self.size):
yield "[{}]".format(i), (self.begin+i).dereference() yield "[{}]".format(i), (self.begin + i).dereference()
class MaybePrinter: class MaybePrinter:
def __init__(self, val): def __init__(self, val):
self.val = val['m_ptr'] self.val = val["m_ptr"]
def to_string(self): def to_string(self):
if self.val: if self.val:
return 'Some {}'.format(self.val) return "Some {}".format(self.val)
else: else:
return 'None' return "None"
def display_hint(self): def display_hint(self):
return 'array' return "array"
def children(self): def children(self):
if self.val: if self.val:
yield '[0]', self.val.dereference() yield "[0]", self.val.dereference()
class ToStringPrinter: class ToStringPrinter:
...@@ -81,16 +84,16 @@ class HandlePrinter: ...@@ -81,16 +84,16 @@ class HandlePrinter:
def to_string(self): def to_string(self):
if self.val: if self.val:
return 'Handle of TensorInfo at {}'.format(self.val) return "Handle of TensorInfo at {}".format(self.val)
else: else:
return 'Empty Handle' return "Empty Handle"
def display_hint(self): def display_hint(self):
return 'array' return "array"
def children(self): def children(self):
if self.val: if self.val:
yield '[0]', self.val.dereference() yield "[0]", self.val.dereference()
def print_small_tensor(device_nd): def print_small_tensor(device_nd):
...@@ -124,17 +127,17 @@ def print_small_tensor(device_nd): ...@@ -124,17 +127,17 @@ def print_small_tensor(device_nd):
class LogicalTensorDescPrinter: class LogicalTensorDescPrinter:
def __init__(self, val): def __init__(self, val):
self.layout = val['layout'] self.layout = val["layout"]
self.comp_node = val['comp_node'] self.comp_node = val["comp_node"]
self.value = val['value'] self.value = val["value"]
def to_string(self): def to_string(self):
return 'LogicalTensorDesc' return "LogicalTensorDesc"
def children(self): def children(self):
yield 'layout', self.layout yield "layout", self.layout
yield 'comp_node', self.comp_node yield "comp_node", self.comp_node
yield 'value', print_small_tensor(self.value) yield "value", print_small_tensor(self.value)
class OpDefPrinter: class OpDefPrinter:
...@@ -145,49 +148,67 @@ class OpDefPrinter: ...@@ -145,49 +148,67 @@ class OpDefPrinter:
return self.val.dynamic_type.name return self.val.dynamic_type.name
def children(self): def children(self):
concrete_val = self.val.address.cast(self.val.dynamic_type.pointer()).dereference() concrete_val = self.val.address.cast(
self.val.dynamic_type.pointer()
).dereference()
for field in concrete_val.type.fields(): for field in concrete_val.type.fields():
if field.is_base_class or field.artificial: if field.is_base_class or field.artificial:
continue continue
if field.name == 'sm_typeinfo': if field.name == "sm_typeinfo":
continue continue
yield field.name, concrete_val[field.name] yield field.name, concrete_val[field.name]
class SpanPrinter: class SpanPrinter:
def __init__(self, val): def __init__(self, val):
self.begin = val['m_begin'] self.begin = val["m_begin"]
self.end = val['m_end'] self.end = val["m_end"]
self.size = self.end - self.begin self.size = self.end - self.begin
def to_string(self): def to_string(self):
return 'Span of Size {}'.format(self.size) return "Span of Size {}".format(self.size)
def display_hint(self): def display_hint(self):
return 'array' return "array"
def children(self): def children(self):
for i in range(self.size): for i in range(self.size):
yield "[{}]".format(i), (self.begin+i).dereference() yield "[{}]".format(i), (self.begin + i).dereference()
if sys.version_info.major > 2: if sys.version_info.major > 2:
pp = gdb.printing.RegexpCollectionPrettyPrinter("MegEngine") pp = gdb.printing.RegexpCollectionPrettyPrinter("MegEngine")
# megdnn # megdnn
pp.add_printer('megdnn::SmallVectorImpl', '^megdnn::SmallVector(Impl)?<.*>$', SmallVectorPrinter) pp.add_printer(
pp.add_printer('megdnn::TensorLayout', '^megdnn::TensorLayout$', ToStringPrinter) "megdnn::SmallVectorImpl",
pp.add_printer('megdnn::TensorShape', '^megdnn::TensorShape$', ToStringPrinter) "^megdnn::SmallVector(Impl)?<.*>$",
# megbrain SmallVectorPrinter,
pp.add_printer('mgb::CompNode', '^mgb::CompNode$', ToStringPrinter) )
pp.add_printer('mgb::Maybe', '^mgb::Maybe<.*>$', MaybePrinter) pp.add_printer("megdnn::TensorLayout", "^megdnn::TensorLayout$", ToStringPrinter)
# imperative pp.add_printer("megdnn::TensorShape", "^megdnn::TensorShape$", ToStringPrinter)
pp.add_printer('mgb::imperative::LogicalTensorDesc', '^mgb::imperative::LogicalTensorDesc$', LogicalTensorDescPrinter) # megbrain
pp.add_printer('mgb::imperative::OpDef', '^mgb::imperative::OpDef$', OpDefPrinter) pp.add_printer("mgb::CompNode", "^mgb::CompNode$", ToStringPrinter)
pp.add_printer('mgb::imperative::Subgraph', '^mgb::imperative::Subgraph$', ReprPrinter) pp.add_printer("mgb::Maybe", "^mgb::Maybe<.*>$", MaybePrinter)
pp.add_printer('mgb::imperative::EncodedSubgraph', '^mgb::imperative::EncodedSubgraph$', ReprPrinter) # imperative
# imperative dispatch pp.add_printer(
pp.add_printer('mgb::imperative::ValueRef', '^mgb::imperative::ValueRef$', ToStringPrinter) "mgb::imperative::LogicalTensorDesc",
pp.add_printer('mgb::imperative::Span', '^mgb::imperative::Span<.*>$', SpanPrinter) "^mgb::imperative::LogicalTensorDesc$",
LogicalTensorDescPrinter,
)
pp.add_printer("mgb::imperative::OpDef", "^mgb::imperative::OpDef$", OpDefPrinter)
pp.add_printer(
"mgb::imperative::Subgraph", "^mgb::imperative::Subgraph$", ReprPrinter
)
pp.add_printer(
"mgb::imperative::EncodedSubgraph",
"^mgb::imperative::EncodedSubgraph$",
ReprPrinter,
)
# imperative dispatch
pp.add_printer(
"mgb::imperative::ValueRef", "^mgb::imperative::ValueRef$", ToStringPrinter
)
pp.add_printer("mgb::imperative::Span", "^mgb::imperative::Span<.*>$", SpanPrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), pp) gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)
else: else:
print("skip import pretty printers") print("skip import pretty printers")
......
import sys
import re import re
import sys
import gdb import gdb
import gdb.types import gdb.types
...@@ -11,13 +11,13 @@ class SmallVectorImplWorker_at(gdb.xmethod.XMethodWorker): ...@@ -11,13 +11,13 @@ class SmallVectorImplWorker_at(gdb.xmethod.XMethodWorker):
self.t = t self.t = t
def get_arg_types(self): def get_arg_types(self):
return gdb.lookup_type('int') return gdb.lookup_type("int")
def get_result_type(self, *args): def get_result_type(self, *args):
return self.t return self.t
def __call__(self, obj, i): def __call__(self, obj, i):
return (obj['m_begin_ptr'].cast(self.t.pointer()) + i).dereference() return (obj["m_begin_ptr"].cast(self.t.pointer()) + i).dereference()
class SmallVectorImplWorker_size(gdb.xmethod.XMethodWorker): class SmallVectorImplWorker_size(gdb.xmethod.XMethodWorker):
...@@ -28,24 +28,25 @@ class SmallVectorImplWorker_size(gdb.xmethod.XMethodWorker): ...@@ -28,24 +28,25 @@ class SmallVectorImplWorker_size(gdb.xmethod.XMethodWorker):
return None return None
def get_result_type(self, *args): def get_result_type(self, *args):
return gdb.lookup_type('int') return gdb.lookup_type("int")
def __call__(self, obj): def __call__(self, obj):
return obj['m_end_ptr'].cast(self.t.pointer()) - obj['m_begin_ptr'].cast(self.t.pointer()) return obj["m_end_ptr"].cast(self.t.pointer()) - obj["m_begin_ptr"].cast(
self.t.pointer()
)
class SmallVectorImplMatcher(gdb.xmethod.XMethodMatcher): class SmallVectorImplMatcher(gdb.xmethod.XMethodMatcher):
def __init__(self): def __init__(self):
super().__init__('SmallVectorImplMatcher') super().__init__("SmallVectorImplMatcher")
def match(self, class_type, method_name): def match(self, class_type, method_name):
if re.match('^megdnn::SmallVector(Impl)?<.*>', if re.match("^megdnn::SmallVector(Impl)?<.*>", class_type.tag):
class_type.tag): if method_name == "at":
if method_name == 'at':
return SmallVectorImplWorker_at(class_type.template_argument(0)) return SmallVectorImplWorker_at(class_type.template_argument(0))
if method_name == 'operator[]': if method_name == "operator[]":
return SmallVectorImplWorker_at(class_type.template_argument(0)) return SmallVectorImplWorker_at(class_type.template_argument(0))
if method_name == 'size': if method_name == "size":
return SmallVectorImplWorker_size(class_type.template_argument(0)) return SmallVectorImplWorker_size(class_type.template_argument(0))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册