Fix GDB pretty printer

This commit is contained in:
Florian Albrechtskirchinger 2022-07-26 13:48:48 +02:00
parent 6adc7a8b8c
commit 478fded6f8
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
2 changed files with 11 additions and 6 deletions

View File

@ -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 ## Copyright

View File

@ -1,5 +1,7 @@
import gdb import gdb
import re
ns_pattern = re.compile(r'nlohmann::json_v(?P<v_major>\d+)_(?P<v_minor>\d+)_(?P<v_patch>\d+)(?P<tags>\w*)::(?P<name>.+)')
class JsonValuePrinter: class JsonValuePrinter:
"Print a json-value" "Print a json-value"
@ -12,12 +14,14 @@ class JsonValuePrinter:
return self.val return self.val
def json_lookup_function(val): def json_lookup_function(val):
name = val.type.strip_typedefs().name m = ns_pattern.fullmatch(val.type.strip_typedefs().name)
if name and name.startswith("nlohmann::basic_json<") and name.endswith(">"): name = m.group('name')
t = str(val['m_type']) if name and name.startswith('basic_json<') and name.endswith('>'):
if t.startswith("nlohmann::detail::value_t::"): m = ns_pattern.fullmatch(str(val['m_type']))
t = m.group('name')
if t and t.startswith('detail::value_t::'):
try: 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: if union_val.type.code == gdb.TYPE_CODE_PTR:
return gdb.default_visualizer(union_val.dereference()) return gdb.default_visualizer(union_val.dereference())
else: else: