lldb in vscode: how to show string values on hover?

132 Views Asked by At

I'm debugging a C app in VS Code on Mac OS (arm64).

I can set breakpoints and see stacktraces. However, for variables, memory addresses are shown instead of values. The variable I want to inspect is msg. Whether I add a "watch", type its name in the lldb command prompt or hover the variable in the code editor, I always have to expand the p property in the result to see the value. Assuming p stands for pointer. A minor inconvenience, but can this be improved?

desired: when hovering msg, I see it's value, like "file not found" actual: I need to click "{...}" in the hover, then expand p to see the value

The source language I am coding in is Nim, which transpiles to C.

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Debug PDX",
            "program": "${env:PLAYDATE_SDK_PATH}/bin/Playdate Simulator",
            "args": [
                "${workspaceFolder}/${workspaceFolderBasename}.pdx"
            ],
            "cwd": "${workspaceFolder}",
            "osx": {
                "MIMode": "lldb",
                "program": "${env:PLAYDATE_SDK_PATH}/bin/Playdate Simulator.app"
            },
//[..]

enter image description here

2

There are 2 best solutions below

0
Jim Ingham On BEST ANSWER

lldb's "summary formatters" are probably what you want. They allow you to present a "one line summary" for a data type, which you can see without having to disclose the type.

You can do what you want with a simple string summary like ${var.data%S}. You register that in your ~/.lldbinit using the type summary add command. There's lots more about how to use lldb summary formatters here:

https://lldb.llvm.org/use/variable.html#type-summary

0
Nino van Hooff On

This is an elaboration on the answer by @Jim Ingham which pointed me in the right direction

This can be done by adding a type summary formatter.

First, I had to get hold of an lldb console. The Debug Console does not show that by default.

Install the CodeLLDB plugin

To use this debugger, change the launch configuration to

{
            "type": "lldb",
            "request": "launch",
            "name": "Build Sim. & Debug PDX",
            "program": "${env:PLAYDATE_SDK_PATH}/bin/Playdate Simulator",
            "preLaunchTask": "Build Simulator PDX",
            "args": [
                "${workspaceFolder}/${workspaceFolderBasename}.pdx"
            ],
            "cwd": "${workspaceFolder}",
            "initCommands": ["command source ${workspaceRoot}/.lldbinit"],
            "osx": {
                "program": "${env:PLAYDATE_SDK_PATH}/bin/Playdate Simulator.app"
            },
            "linux": {
                "program": "${env:PLAYDATE_SDK_PATH}/bin/PlaydateSimulator"
            },
            "windows": {
                "program": "${env:PLAYDATE_SDK_PATH}/bin/PlaydateSimulator.exe"
            }
        },

(note that type is lldb

The initCommands is useful if you want to keep your config inside your project.

Now let's create that config in your project: Create a file called .lldbinit in your project root, and add the following:

type summary add --summary-string "${var.p.data%S}" NimStringV2

You should be able to launch the debug configuration, and the Debug Console should show something like

Console is in 'commands' mode, prefix expressions with '?'.
Executing script: initCommands
Executing commands in '<your project path>/.lldbinit'.
(lldb) type summary add --summary-string "${var.p.data%S}" NimStringV2

Now place a breakpoint after a line of code that sets a string value, and the mouse hover and Variables view of vscode should show the actual string value