Search code examples
eclipsegdbpretty-printnlohmann-json

JSON for Modern C++ Pretty Printer Not Working in Eclipse IDE


I'm attempting to set up pretty printing for JSON for Modern C++ (A.K.A. nlohmann JSON) in Eclipse. I've set up my project to load the pretty-printing Python script that's included with nlohmann JSON. When I attempt to inspect a JSON object, however, Eclipse displays the following error messages:

  1. Failed to execute MI command: -var-create - * jsonNode Error message from debugger back end: Cannot instantiate printer for default visualizer

  2. Unable to create variable object

  3. Failed to execute MI command: -var-create - * jsonNode Error message from debugger back end: Cannot instantiate printer for default visualizer

Please note that "jsonNode" is the name of the JSON object in my test code.

I followed the instructions in the pretty printer's README by adding the line source nlohmann-json.py to my .gdbinit file, and I've verified that the pretty printing script is loading using info pretty-printer in Eclipse's debug console. For the sake of being thorough, I've also attempted to print the JSON object using GDB's print command in the debug console, but it just displays the object the same, messy way it displays without the pretty printing script: $2 = {m_type = nlohmann::json_abi_v3_11_2::detail::value_t::string, m_value = {object = 0x55e4b10, array = 0x55e4b10, string = 0x55e4b10, binary = 0x55e4b10, boolean = 16, number_integer = 90065680, number_unsigned = 90065680, number_float = 4.4498358357331042e-316}} which implies this isn't a compatibility issue between the pretty-printer script and Eclipse.

So, firstly, what's causing the errors messages above, and second, how do I get Eclipse to display the contents of my JSON objects in a human-readable fashion?


Solution

  • Based on your comments, I see your GDB was built against Python 2.7.9. You also mention in your comment that you installed Python 3.11 -- unfortunately, that will not help. Whether to use Python 2.x or 3.x is a build-time choice. The differences between these too is significant enough that this can't (easily) be a runtime choice.

    I found the pretty-printer script (for JSON for Modern C++) via a google search, and spotted that it makes use of the := operator, which, as I understand it, was only added in Python 3.8.

    What this means is that, when GDB tries to run the pretty-printer, it will throw a Python error, this translates into the behaviour that you are seeing. You might be able to get more insight if you run this GDB command set python print-stack full, and then carry out the actions that are not working. This should cause GDB to print more information about the errors you are hitting, though I'm not 100% sure this will print what you need for MI commands, it's possible GDB will silently swallow the errors in this case, but it doesn't hurt to try. For standard CLI commands (i.e. not MI commands) the above setting should cause GDB to be more verbose about errors.

    One possible path forward for you is to edit the pretty-printer script to make it compatible with Python 2.7.9 (start by removing use of :=, but there could easily be other Python 3.x features being used, you would need to make the whole script Python 2.x compatible).

    Another option would be to acquire, or build, an updated GDB, this time linking against Python 3.8+.

    FYI: From GDB 13 onwards, GDB no longer supports linking against Python 2, not that this helps you much with your current problems.