I’m using VS Code with the CodeLLDB extension on macOS.
I’m using this step to step through my rust unit tests in the debugger. This is working well, but I would also like to step into rust’s std libraries and see debug symbols. Currently I can step into std, but just see assembly. How do I set things up so that I can step into std and see debug symbols?
I did run rustup component add rust-src but it said I was already up-to-date.
The std binaries distributed by the Rust organization do not contain debugging information, so this is not possible out of the box. If you really want it, you can compile std yourself with debug info enabled.
Notice that /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858 depends on the exact toolchain you’re using. So updating your toolchain will probably require modifying this string. The way I easily find the hash is by running the following command in the project directory:
The idea is that Visual Studio Code will issue the GDB commands specified in the autorun array right after GDB is initialized, before actual debugging is started. The command set substitute-path of GDB is documented in Specifying Source Directories.
FWIW future visitors might try this script. I'm meant to be learning Rust, but I don't know much about it yet so the script's in Python.
Sample output:
Add this to your .vscode/settings.json if you're using the vadimcn.vscode-lldb extension:
"lldb.launch.sourceMap": {
"/rustc/73528e339aae0f17a15ffa49a8ac608f50c6cf14/src": "/Users/joeuser/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/src"
}
Script
#!/usr/bin/env python3
import sys
if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'):
print(
'This script will try to generate a "lldb.launch.sourceMap" entry you can add '
"to your .vsode/settings.json file for use with the 'vadimcn.vscode-lldb' "
'extension. This script also requirest that the python package `requests` '
'be installed for "{}"'.format(sys.executable)
)
sys.exit(1)
import requests # noqa: E402
import json # noqa: E402
from subprocess import Popen, PIPE # noqa: E402
PATH_MAP_DOC_URL = 'https://github.com/vadimcn/vscode-lldb/blob/master/MANUAL.md#source-path-remapping'
def get_active_toolchain_root():
sout2, _ = Popen(['rustc', '--print', 'sysroot'], stdout=PIPE).communicate()
sysroot = sout2.decode('utf_8').strip()
return '{}/lib/rustlib/src/rust/src'.format(sysroot)
def get_full_commit_hash():
sout1, _ = Popen(['rustc', '--version'], stdout=PIPE).communicate()
hsh = sout1.decode().split('(')[1].split()[0]
rsp = requests.get(
url='https://api.github.com/repos/rust-lang/rust/commits/{}'.format(hsh),
headers={'Accept': 'application/vnd.github.v3+json'}
)
rsp.raise_for_status()
return rsp.json()['sha']
def get_suggested_src_mapping(fullhash, localsrcs):
compiletime_srcdir = '/rustc/{}/src'.format(fullhash)
return json.dumps({compiletime_srcdir: localsrcs}, indent=4)
if __name__ == '__main__':
srcdir = get_active_toolchain_root()
print("\nYour active toolchain's local sources: '{}'".format(srcdir))
fullhash = get_full_commit_hash()
print("Full hash of your active toolchain's git commit: {}".format(fullhash))
sourceMap = get_suggested_src_mapping(fullhash, srcdir)
print("Add this to your .vscode/settings.json if you're using the vadimcn.vscode-lldb extension:")
print('\n\t"lldb.launch.sourceMap": {}\n'.format(sourceMap.replace('\n', '\n\t')))
print('You can read more about it here: {}\n'.format(PATH_MAP_DOC_URL))