Debug support when using IDE? Is it possible?
Thanks for your contribution.
I am using VSCode for my Rust project. When I use ndarray, I am not able to see the data in Variable explorer and it only shows ptr. I have to use println! for seeing the data. Is it possible to add support for debug window?

I've been having to deal with this. I feel like this can be addressed following instructions in https://lldb.llvm.org/use/variable.html, but so far my initial attempts have not worked, though I haven't spent all that much time trying.
Furthermore, it could be worth looking at how rust-lldb understands vectors which is done in lldb_commands and lldb_lookup.py in the main Rust repository.
For now, you can view the values in the array using parray. If the variable is x, then this will show all the contained values:
parray `x.data.len` x.data.ptr.pointer
Note that this shows the values as stored in memory, so unless it is a 1D array, you'll need to interpret how they are arranged into the array.
Isn't this done with .natvis files? For VSCode at least. I'm not sure about a general solution that would work for all IDE.
You can find 4 natvis files in .rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\ for the common Rust types. I wanted to write one for ndarray and nalgebra for a while but it's simpler to write a println! so I haven't done it yet :)
Hi @nilgoyette
Thanks for the suggestion. I actually implemented Natvis to visualise the NDArray similar to Vec. It works for 1D and 2D array. Below is my file,
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="ndarray::ArrayBase<*,*>">
<DisplayString>{{Modified by Selva}}</DisplayString>
<Expand HideRawView="false">
<ArrayItems>
<Direction>Forward</Direction>
<Rank>2</Rank>
<Size>$i==0?(int)dim.index[0]:(int)dim.index[1]</Size>
<ValuePointer>data.ptr.pointer</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>
Sorry, I didnt update here.
Nice! I'll test this on my next debug session. Thank you! Out of curiosity, where do you place this file @selvavm ?
If there's a place we could place it so it's accessible when you debug ndarray code for users that would definitely be a pull-request worth considering. My only worry with things like this is they get added to a list of things that you need to remember to update because you won't catch them from CI failures etc :disappointed:
Nice! I'll test this on my next debug session. Thank you! Out of curiosity, where do you place this file @selvavm ?
@nilgoyette - Welcome. You can place this anywhere (I placed it in .vscode folder) and map it either in launch.json or settings.json. I prefer the settings.json.
launch.json
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}.exe",
"args": [
"example2"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "cargo build (stable-msvc)",
"visualizerFile":"${workspaceFolder}/.vscode/custom.natvis"
}
settings.json
"rust-analyzer.debug.engineSettings": {"cppvsdbg": {"visualizerFile":"${workspaceFolder}/.vscode/custom.natvis"}}
If there's a place we could place it so it's accessible when you debug ndarray code for users that would definitely be a pull-request worth considering. My only worry with things like this is they get added to a list of things that you need to remember to update because you won't catch them from CI failures etc
@xd009642 - I agree. Also I believe natvis can be used only with Visual studio code and only with certain debuggers. Hence, it might be difficult to maintain.
Not having a proper debugger support is one disadvantage I see in Rust. For example, I can't do basics like executing a trait function or println! in the debug console. In fact, I wanted to push Rust into my organisation for its superb libraries (NDArray) but it was downvoted just for this one reason. Sad thing is, Rust community is not interested in working on this.
@selvavm well there's rust-gdb and rust-lldb scripts, and projects like headcrab-rs are trying to improve things. I've also heard about aims to utilise MIRI level interpreting for a better debug experience. Generally, I find debugging code like this in rust as easy as C++, not intractable but in some areas the UX is lacking
Thank you @selvavm.
If there's no general solution to this problem, maybe we could document it in the readme or in the documentation, with a link to download the .natvis file? At least, the 42% of Rust programmers who use VSCode would now be able to find the information.
Feel free to add it to a new subdirectory in the repo somewhere. I'd make a note of the limitations - it seems to not handle the stride of the array or view at all etc. If it can be too misleading in your opinion, then we don't advocate using this.
Hey,
I wanted to add my solution to this thread, because it was quite frustrating getting this work with lldb.
What OP in this post mostlikely did, was to switch to cppvsdbg which (from my understanding) is the Debugger in Windows for C++. In VSCode you can use cppdbg, cppvsdbg and lldb.
For Linux (in my case an Arch WSL2 system) I found that lldb worked very nicely for debugging Rust, so I wanted to stay with lldb.
I could not find a way to use natvis in VSCode (possible in Visual Studio; seems to be at least) with lldb. Although I did find a project which mentioned the use of custom natvis in Visual Studio with lldb.
So with all of that out of the way... my solution was: NOTE: This is a Linux based (Arch WSL2) solution
Solution
- Make sure you have the
CodeLLDBVSCode extension. - Try running a normal debug session first. (If this works continue, otherwise fix error)
- If
ndarrayArrays looks like this
then you have the default "representation" using lldb - In your local or global
settings.jsonfor VSCode add the lines:
with"lldb.launch.initCommands": [ "command script import ${workspaceFolder}/.vscode/name_of_ndarray_format.py" ],name_of_ndarray_format.pybeing the file that I will descripe now: - Add a custom file that make the content visualization in VSCode possible.
5.1. SOURCE: https://github.com/hep-rs/boltzmann-solver/blob/master/.vscode/formatter.py - Author: @JP-Ellis
5.2. After creating the file in
${workspaceFolder}/.vscode/, you should be able to start a debug session withlldband should instead see thendarray ArrayBase typesas:
which is the desired output we want to see in a debugger.
REMARKS
NOTE: I have very limited knowledge about lldb and its innerworkings and I do not provide any warranty for my solution. This was the final solution, which worked for me and wanted to share it, so maybe more people can profit from this.