Skip to content

Commit

Permalink
Fix GDB pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Jul 29, 2022
1 parent 6adc7a8 commit 478fded
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tools/gdb_pretty_printer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions tools/gdb_pretty_printer/nlohmann-json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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:
"Print a json-value"

Expand All @@ -12,12 +14,14 @@ def to_string(self):
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:
Expand Down

0 comments on commit 478fded

Please sign in to comment.