open-source-parsers / open-source-parsers/jsoncpp

GDB pretty print support

Open
#1,507 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

It's pretty hard to debug programs with a complex JSON structure such as Json::Value, thinking on that.
I did create a very simple pretty print gdb script that would help you debug and see the content under yours JSON objects.

import gdb
import json
class JsonValuePrinter:
    def __init__(self, val):
        self.val = val

    def _invoke_to_styledString_(self):
        eval_string = f"(({self.val.type.name}*){self.val.address})->toStyledString().c_str()"
        output = json.loads(gdb.parse_and_eval(eval_string).string())
        return str(output)

    def to_string(self):
        return self._invoke_to_styledString_()

def build_pretty_printer(val):
    if str(val.type) == 'Json::Value':
        return JsonValuePrinter(val)

gdb.pretty_printers.append(build_pretty_printer)

The script is pretty simple and just converts the JSON to a styled string representation with the method Json::Value::toStyledString

Would be good to access each member individually, but it's considered more complex task So if someone already have a better script or even would like to improve mine, I would really appreciate it.

Anyways, to use this script just launch gdb in your program, for example:

#include <jsoncpp/json/json.h>
#include <jsoncpp/json/value.h>

#include <iostream>

int main()
{
  Json::Value val;

  val["xd"] = 10;

  Json::Value arr;

  arr.append(val);
  arr.append(val);

  val["complex json"] = arr;

  return 0;
}

compile it

g++ main.cpp -g -O0 -o run
gdb run
source prettyPrint.py

if you try to print val for example, you would see the following output:

image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed prettyPrint.py GDB script and the Json::Value::toStyledString call shown in the issue. Compile the provided C++ example with debug symbols, source the script in GDB, and verify how printing val behaves. Done means documented or integrated GDB pretty-print support that handles the stated Json::Value example.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.