saem/vscode-nim

Debugging: All the Local Variables are messed up

jradxl opened this issue · 7 comments

I'm sure I have configured vscode and nim correctly, and breakpoints work, but I cannot understand the Variables.
NIM 1.6.0
NIM Extension 0.6.6 (Edit: Was using nimsaem v0.1.25)
CodeLLDB 1.6.9
Ubuntu 21.10, Mate

Please advise what to do.

image

@jradxl

Are you wondering why the variables are mangled?

The Nim source code gets compiled to c, c++, or objc and the varables get mangled in a predictive way for each respective backend.
GDB and LLDB can only see those variable names.
You're seeing the code generation details. So you will notice temporary variables and such.

Or are you wondering why their values are not printed for you?

GDB and LLDB need pretty-printers installed in order to understand how to show the value of Nim and your custom data structures.

For the GDB, there is tools/nim-gdb.py in the Nim repo.

For LLDB, there is https://github.com/paulnorrie/Nim/blob/devel/tools/nimlldb.py.

Both need updated to pretty-print ORC strings and also to print objects/tuples.

Download the lldbnim.py file from above.
Here is what you need to add your CodeLLDB launch config:

"preRunCommands": [
  "command script import --allow-reload CHANGE_THIS_PATH/lldbnim.py"
]

That should give an improvement in pretty-printing strings, seqs, and enums.

More work is needed to pretty-print everything.

I'm currently working on it.

@jradxl I noticed that you're running the unmaintained VSCode Nim extension: NIM Extension 0.6.6

It has no affect on the debug symbols, but I recommend moving to the Nim extension by nimsaem (this repo).

@quantimnot , many thanks for your two posts.
It was my error in writing this issue and I am using nimsaem. I must have looked up the wrong version. I'm using v0.1.25

I was following the information on debugging as written up in this repo's readme. It does not give an example of preRunCommands being needed. It just posts a screenshot with correct variables

Ubuntu packages loaded:
gdb/impish,now 11.1-0ubuntu2 amd64 [installed,automatic]
lldb/impish,now 1:13.0-53~exp1 amd64 [installed]

I believe I've added the py file correctly, but it still does not work.
I git cloned both repositories and took py files from the tools directory into my project.
(https://github.com/paulnorrie/Nim.git) and (https://github.com/nim-lang/Nim.git)

Are you sure that Nim 1.6 is generating correct debug info for the Native debugger extensions?

Here are my task.json and launch.json, where I did try the other extension too, by WebFreak

TASK

{
    "label": "nim: build current file (for debugging)",
    "command": "nim",
    "args": [
        "compile",
        "-g",
        "--debugger:native",
        "-o:${workspaceRoot}/bin/${fileBasenameNoExtension}",
        "${relativeFile}"
    ],
    "options": {
        "cwd": "${workspaceRoot}"
    },
    "type": "shell",
}

LAUNCH

{
    "type": "lldb",
    "request": "launch",
    "name": "nim1: debug current file",
    "preRunCommands": [
        "command script import --allow-reload ./nimlldb.py"
      ],
    "preLaunchTask": "nim: build current file (for debugging)",
    "program": "${workspaceFolder}/bin/${fileBasenameNoExtension}",
    "args": [],
    "cwd": "${workspaceFolder}",
},
{
    "type":"gdb",
    "request": "launch",
    "name": "nim2: debug current file",
    "preRunCommands": [
        "command script import --allow-reload ./nim-gdb.py"
      ],
    "preLaunchTask": "nim: build current file (for debugging)",
    "args": [],
    "cwd": "${workspaceFolder}",
    "target": "${workspaceFolder}/bin/${fileBasenameNoExtension}"
}

@jradxl I think the screenshot on the readme is just as broken as yours. That screenshot has local symbols displayed for a routine. Your sample has all globals. The readme argument symbols don't get mangled. You have no arguments, only globals, and they get mangled differently. The readme doesn't mention the GDB or LLDB extensions, but I'm pretty sure @saem doesn't personally have strings, arrays, enums and the like pretty-printed without them.

Also, your GDB preRunCommands should be:

"preRunCommands": [
  "source ./nim-gdb.py"
]

I think the Nim debugging experience is poor at the moment, but I'm working to improve that. Look forward to having something soonish.

rotu commented

Did you ever figure out how to get The debugger to un-mangle names?

Hey
This is something that happens with top level variables, due to the way mim treats those.
I believe you would be able to unmangle those by putting them inside a proc.

rotu commented

@canelhasmateus Got it. Thank you!

The other things that were confusing me now make sense:

  • T1_, T2_, T3_,... variables which appear to be anonymous local expressions
  • FR_ which is some info about the current stack frame