ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

Debug support when using IDE? Is it possible?

Open selvavm opened this issue 5 years ago • 10 comments

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?

image

selvavm avatar Jun 26 '20 05:06 selvavm

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.

JP-Ellis avatar Aug 11 '20 11:08 JP-Ellis

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 :)

nilgoyette avatar Dec 07 '20 15:12 nilgoyette

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&lt;*,*&gt;">
      <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.

selvavm avatar Dec 08 '20 11:12 selvavm

Nice! I'll test this on my next debug session. Thank you! Out of curiosity, where do you place this file @selvavm ?

nilgoyette avatar Dec 08 '20 13:12 nilgoyette

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:

xd009642 avatar Dec 08 '20 13:12 xd009642

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 avatar Dec 09 '20 03:12 selvavm

@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

xd009642 avatar Dec 09 '20 06:12 xd009642

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.

nilgoyette avatar Dec 09 '20 13:12 nilgoyette

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.

bluss avatar Dec 10 '20 15:12 bluss

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

  1. Make sure you have the CodeLLDB VSCode extension.
  2. Try running a normal debug session first. (If this works continue, otherwise fix error)
  3. If ndarray Arrays looks like this image then you have the default "representation" using lldb
  4. In your local or global settings.json for VSCode add the lines:
        "lldb.launch.initCommands": [
            "command script import ${workspaceFolder}/.vscode/name_of_ndarray_format.py"
        ],
    
    with name_of_ndarray_format.py being the file that I will descripe now:
  5. 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 with lldb and should instead see the ndarray ArrayBase types as: image 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.

MartinRJDagleish avatar Jan 21 '23 20:01 MartinRJDagleish