nlohmann-json.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import gdb
  2. import re
  3. 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>.+)')
  4. class JsonValuePrinter:
  5. "Print a json-value"
  6. def __init__(self, val):
  7. self.val = val
  8. def to_string(self):
  9. if self.val.type.strip_typedefs().code == gdb.TYPE_CODE_FLT:
  10. return ("%.6f" % float(self.val)).rstrip("0")
  11. return self.val
  12. def json_lookup_function(val):
  13. m = ns_pattern.fullmatch(val.type.strip_typedefs().name)
  14. name = m.group('name')
  15. if name and name.startswith('basic_json<') and name.endswith('>'):
  16. m = ns_pattern.fullmatch(str(val['m_type']))
  17. t = m.group('name')
  18. if t and t.startswith('detail::value_t::'):
  19. try:
  20. union_val = val['m_value'][t.removeprefix('detail::value_t::')]
  21. if union_val.type.code == gdb.TYPE_CODE_PTR:
  22. return gdb.default_visualizer(union_val.dereference())
  23. else:
  24. return JsonValuePrinter(union_val)
  25. except Exception:
  26. return JsonValuePrinter(val['m_type'])
  27. gdb.pretty_printers.append(json_lookup_function)