| 1 | import gdb |
| 2 | import re |
| 3 | |
| 4 | ns_pattern = re.compile(r'nlohmann(::json_abi(?P<tags>\w*)(_v(?P<v_major>\d+)_(?P<v_minor>\d+)_(?P<v_patch>\d+))?)?::(?P<name>.+)') |
| 5 | class JsonValuePrinter: |
| 6 | "Print a json-value" |
| 7 | |
| 8 | def __init__(self, val): |
| 9 | self.val = val |
| 10 | |
| 11 | def to_string(self): |
| 12 | if self.val.type.strip_typedefs().code == gdb.TYPE_CODE_FLT: |
| 13 | return ("%.6f" % float(self.val)).rstrip("0") |
| 14 | return self.val |
| 15 | |
| 16 | def json_lookup_function(val): |
| 17 | if m := ns_pattern.fullmatch(str(val.type.strip_typedefs().name)): |
| 18 | name = m.group('name') |
| 19 | if name and name.startswith('basic_json<') and name.endswith('>'): |
| 20 | m_data = val['m_data'] |
| 21 | m_type = m_data['m_type'] |
| 22 | m = ns_pattern.fullmatch(str(m_type)) |
| 23 | t = m.group('name') |
| 24 | prefix = 'detail::value_t::' |
| 25 | if t and t.startswith(prefix): |
| 26 | try: |
| 27 | union_val = m_data['m_value'][t.replace(prefix, '', 1)] |
| 28 | if union_val.type.code == gdb.TYPE_CODE_PTR: |
| 29 | return gdb.default_visualizer(union_val.dereference()) |
| 30 | else: |
| 31 | return JsonValuePrinter(union_val) |
| 32 | except Exception: |
| 33 | return JsonValuePrinter(m_type) |
| 34 | |
| 35 | gdb.pretty_printers.append(json_lookup_function) |