• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import gdb
2import re
3
4ns_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>.+)')
5class 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
16def 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 = ns_pattern.fullmatch(str(val['m_type']))
21          t = m.group('name')
22          if t and t.startswith('detail::value_t::'):
23              try:
24                  union_val = val['m_value'][t.removeprefix('detail::value_t::')]
25                  if union_val.type.code == gdb.TYPE_CODE_PTR:
26                      return gdb.default_visualizer(union_val.dereference())
27                  else:
28                      return JsonValuePrinter(union_val)
29              except Exception:
30                  return JsonValuePrinter(val['m_type'])
31
32gdb.pretty_printers.append(json_lookup_function)
33