Created
December 24, 2020 13:58
json pretty printer for gdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(gdb) p -pretty on -array on -- mixed_nested | |
$1 = std::map with 5 elements = { | |
["Baptiste"] = std::map with 1 element = { | |
["first"] = "second" | |
}, | |
["Emmanuel"] = std::vector of length 3, capacity 3 = { | |
3, | |
"25", | |
0.5 | |
}, | |
["Jean"] = 0.7, | |
["Zorg"] = std::map with 8 elements = { | |
["array"] = std::vector of length 3, capacity 3 = { | |
1, | |
0, | |
2 | |
}, | |
["awesome_str"] = "bleh", | |
["bool"] = true, | |
["flex"] = 0.2, | |
["float"] = 5.22, | |
["int"] = 5, | |
["nested"] = std::map with 1 element = { | |
["bar"] = "barz" | |
}, | |
["trap "] = "you fell" | |
}, | |
["empty"] = nlohmann::detail::value_t::null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gdb | |
import re | |
class JsonValuePrinter: | |
"Print a json-value" | |
def __init__(self, val): | |
self.val = val | |
def to_string(self): | |
if self.val.type.strip_typedefs().code == gdb.TYPE_CODE_FLT: | |
return ("%.6f" % float(self.val)).rstrip("0") | |
return self.val | |
def json_lookup_function(val): | |
if re.search("^nlohmann::basic_json<.*>$", val.type.strip_typedefs().name): | |
t = str(val['m_type']) | |
if t.startswith("nlohmann::detail::value_t::"): | |
try: | |
union_val = val['m_value'][t[27:]] | |
if union_val.type.code == gdb.TYPE_CODE_PTR: | |
return gdb.default_visualizer(union_val.dereference()) | |
else: | |
return JsonValuePrinter(union_val) | |
except: | |
return JsonValuePrinter(val['m_type']) | |
gdb.pretty_printers.append(json_lookup_function) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment