diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:42:50 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:42:50 +0300 |
| commit | 5fad10f89c485cfdc7b99011f07609f8871160d4 (patch) | |
| tree | 1860b39753b652dfe54d3cbbc80c875f40198d1f /json4cpp/tools/gdb_pretty_printer | |
| parent | 292baed7ac0cf84263263966ed32ed113cae857f (diff) | |
| parent | 9a737481aed085fd289f82dff1fa8c3c66627a7e (diff) | |
| download | Project-Tick-5fad10f89c485cfdc7b99011f07609f8871160d4.tar.gz Project-Tick-5fad10f89c485cfdc7b99011f07609f8871160d4.zip | |
Add 'json4cpp/' from commit '9a737481aed085fd289f82dff1fa8c3c66627a7e'
git-subtree-dir: json4cpp
git-subtree-mainline: 292baed7ac0cf84263263966ed32ed113cae857f
git-subtree-split: 9a737481aed085fd289f82dff1fa8c3c66627a7e
Diffstat (limited to 'json4cpp/tools/gdb_pretty_printer')
| -rw-r--r-- | json4cpp/tools/gdb_pretty_printer/README.md | 79 | ||||
| -rw-r--r-- | json4cpp/tools/gdb_pretty_printer/nlohmann-json.py | 35 |
2 files changed, 114 insertions, 0 deletions
diff --git a/json4cpp/tools/gdb_pretty_printer/README.md b/json4cpp/tools/gdb_pretty_printer/README.md new file mode 100644 index 0000000000..4a95776df4 --- /dev/null +++ b/json4cpp/tools/gdb_pretty_printer/README.md @@ -0,0 +1,79 @@ +# GDB Pretty Printer + +File [nlohmann-json.py](nlohmann-json.py) contains a pretty printer for GDB for JSON values of this library. It was +originally published as [Gist](https://gist.github.com/ssbssa/60da5339c6e6036b2afce17de06050ea#file-nlohmann-json-py) by [Hannes Domani](https://github.com/ssbssa). + +## How to use + +- Add line + + ``` + source /path/to/nlohmann-json.py + ``` + + to `~/.gdbinit`. Note you must replace `/path/to` with whatever path you stored file `nlohmann-json.py`. +- In GDB, debug as usual. When you want to pretty-print a JSON value `var`, type + + ``` + p -pretty on -array on -- var + ``` + + The result should look like + + ``` + $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 + } + ``` + +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 + +MIT License + +Copyright (C) 2020 [Hannes Domani](https://github.com/ssbssa) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/json4cpp/tools/gdb_pretty_printer/nlohmann-json.py b/json4cpp/tools/gdb_pretty_printer/nlohmann-json.py new file mode 100644 index 0000000000..2c120befd9 --- /dev/null +++ b/json4cpp/tools/gdb_pretty_printer/nlohmann-json.py @@ -0,0 +1,35 @@ +import gdb +import re + +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>.+)') +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 m := ns_pattern.fullmatch(str(val.type.strip_typedefs().name)): + name = m.group('name') + if name and name.startswith('basic_json<') and name.endswith('>'): + m_data = val['m_data'] + m_type = m_data['m_type'] + m = ns_pattern.fullmatch(str(m_type)) + t = m.group('name') + prefix = 'detail::value_t::' + if t and t.startswith(prefix): + try: + union_val = m_data['m_value'][t.replace(prefix, '', 1)] + if union_val.type.code == gdb.TYPE_CODE_PTR: + return gdb.default_visualizer(union_val.dereference()) + else: + return JsonValuePrinter(union_val) + except Exception: + return JsonValuePrinter(m_type) + +gdb.pretty_printers.append(json_lookup_function) |
