diff --git a/tools/gdb_pretty_printer/README.md b/tools/gdb_pretty_printer/README.md index f5f61927b..c1a3651d4 100644 --- a/tools/gdb_pretty_printer/README.md +++ b/tools/gdb_pretty_printer/README.md @@ -50,7 +50,8 @@ File [nlohmann-json.py](nlohmann-json.py) contains a pretty printer for GDB for } ``` -Tested with GDB 9.2. See [#1952](https://github.com/nlohmann/json/issues/1952) for more information. Please post questions there. +Requires Python 3.9+. Last tested with GDB 12.1. +See [#1952](https://github.com/nlohmann/json/issues/1952) for more information. Please post questions there. ## Copyright diff --git a/tools/gdb_pretty_printer/nlohmann-json.py b/tools/gdb_pretty_printer/nlohmann-json.py index c85a383b0..774756de7 100644 --- a/tools/gdb_pretty_printer/nlohmann-json.py +++ b/tools/gdb_pretty_printer/nlohmann-json.py @@ -1,5 +1,7 @@ import gdb +import re +ns_pattern = re.compile(r'nlohmann::json_v(?P\d+)_(?P\d+)_(?P\d+)(?P\w*)::(?P.+)') class JsonValuePrinter: "Print a json-value" @@ -12,12 +14,14 @@ class JsonValuePrinter: return self.val def json_lookup_function(val): - name = val.type.strip_typedefs().name - if name and name.startswith("nlohmann::basic_json<") and name.endswith(">"): - t = str(val['m_type']) - if t.startswith("nlohmann::detail::value_t::"): + m = ns_pattern.fullmatch(val.type.strip_typedefs().name) + name = m.group('name') + if name and name.startswith('basic_json<') and name.endswith('>'): + m = ns_pattern.fullmatch(str(val['m_type'])) + t = m.group('name') + if t and t.startswith('detail::value_t::'): try: - union_val = val['m_value'][t[27:]] + union_val = val['m_value'][t.removeprefix('detail::value_t::')] if union_val.type.code == gdb.TYPE_CODE_PTR: return gdb.default_visualizer(union_val.dereference()) else: