Debugging Rust in VSCode

栏目: IT技术 · 发布时间: 4年前

内容简介:Its been a while since I’ve
Debugging Rust in VSCode
Debugging Boa on Windows 10 in 2020

Its been a while since I’ve posted about debugging rust , we’ve come a long way since 2017 and I wanted to do an update on getting set up for Rust Debugging.

Installing Rust and VSCode

  • The best way to install Rust is via Rustup
  • You can grab Visual Studio Code from here

You may also need to run rustup component add rust-src if you wish to step into standard library components (mentioned below).

VSCode Extensions

  • If you’re on Windows the C/C++ Extension is needed.
  • If you’re on MacOS then CodeLLDB .
  • Set CodeLLDB to use rust-lldb It should be available on your path, placed there by rustup. Example here .

Regardless of which OS you use I recommend getting Rust Analyzer (RA). It has excellent IDE support for Rust and is being actively developed. If you’ve heard of RLS then Rust Analyzer is a replacement for that (often dubbed RLS 2.0). Unfortunately it is not in the VSCode market place just yet, so you’ll need to clone it and install. Its not too difficult, there’s instructions on their Github page.

Configure VSCode

If you don’t already have a launch.json file you can create one by opening up your menu ( Ctrl +shift + p / cmd + shift + p ) selecting “Debug: Open launch.json” and select C++ or lldb.

Below is the current configuration from Boa, you can copy and paste this and re-use for your own project.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Launch",
      "args": [],
      "program": "${workspaceFolder}/target/debug/boa",
      "cwd": "${workspaceFolder}",
      "stopOnEntry": false,
      "sourceLanguages": ["rust"],
      "sourceMap": {
        "/rustc/*": "${env:HOME}/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust"
      }
    },
    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/target/debug/boa.exe",
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "sourceFileMap": {
        "/rustc/5e1a799842ba6ed4a57e91f7ab9435947482f7d8": "${env:USERPROFILE}\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust"
      },
      "symbolSearchPath": "https://msdl.microsoft.com/download/symbols",
      "environment": []
    }
  ]
}

Using the Dev Container

Some users prefer to do their development inside the VSCode Dev Container builds. If you prefer this the Boa repository has a good example of configuration set up with debugging included, see here .

One reason for using a dev container is because the C++ debugger is a bit more aggressive at optimizing away variables whilst the rust-lldb debugger is much better, windows users can (hopefully) get the best of both worlds.

I don’t spend all my time inside the dev container because it’s a nightmare to install Rust Analyzer inside the container, this will be sorted once RA decide to be on the Marketplace.

Stepping into the Rust Std library

Debugging Rust in VSCode
Stepping into is_digit from the standard library

If you wish to step into the Rust standard library code (and some do for curiosity or to help debug a deeper problem) you can do this with sourceFileMap . On MacOS this will just work with the above launch.json (or at least that’s the idea, glob support is sketchy right now).

On windows this isn’t possible without updating the string each time you update your toolchain, I’ve added this to Tracking Issues below. This is because the long string you see after /rustc/[here] depends on the exact toolchain you have installed, so every time you update the toolchain this string will change.

You can find out the string by stepping into a native function and copying the “source location”. It will look something like this:

; id = {0x00000180}, range = [0x0000000100002760-0x00000001000027b0), name="_$LT$alloc
; Source location: /rustc/9fda7c2237db910e41d6a712e9a2139b352e558b/src/liballoc/vec.rs
100002760: 55                         pushq  %rbp
100002761: 48 89 E5                   movq   %rsp, %rbp
100002764: 48 83 EC 20                subq   $0x20, %rsp

Variable is optimized away and not available.

I’m sure you’ve seen this, it can be quite frustrating. Remember, you do get a bit more info using LLDB so consider using that.

There is an initiative to move debug builds to run on Cranelift .

One of the outcomes from this is better handling of Rust variables during debug builds and not optimizing away everything. I think this is the way forward, especially as LLDB will only get us so far

Personally, what would be amazing is if the Rust Analyzer VSCode extension could eventually hook into Cranelift for debugging in the future to make life as easy as possible. Wishful thinking? Maybe.

Tracking Issues (things to keep an eye on):


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编程珠玑(第二版)

编程珠玑(第二版)

[美] Jon Bentley / 谢君英、石朝江 / 中国电力出版社 / 2004-4 / 28.00元

《编程珠玑(第2版)》是计算机科学方面的经典名著。书的内容围绕程序设计人员面对的一系列实际问题展开。作者Jon Bentley 以其独有的洞察力和创造力,引导读者理解这些问题并学会解决方法,而这些正是程序员实际编程生涯中至关重要的。一起来看看 《编程珠玑(第二版)》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换