Skip to content

Instantly share code, notes, and snippets.

@daniellivingston
Last active May 3, 2022 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniellivingston/9c93e0697bf159e429df589344ce7ed8 to your computer and use it in GitHub Desktop.
Save daniellivingston/9c93e0697bf159e429df589344ce7ed8 to your computer and use it in GitHub Desktop.
Rust `std` source mappings in VSCode
#!/usr/bin/env bash
# ============================================================================== #
# MIT License #
# ------------------------------------------------------------------------------ #
# Displays the CodeLLDB standard library mapping for debugging Rust binaries. #
# Generates a JSON string used in VSCode debug task list. #
# Requires the `jq` utility to be installed. #
# ============================================================================== #
# OUTPUT: #
# "lldb.launch.sourceMap": {
# "/rustc/<hash>/": "/home/<username>/<rust-toolchain>/lib/rustlib/src/rust/src",
# }
# ============================================================================== #
cargo_bin="$(cargo metadata --no-deps --format-version 1 | jq -r -c '.target_directory')"
crates="$(cargo metadata --no-deps --format-version 1 | jq -r -c '.packages[].targets[] | if .kind[0] == "bin" then .name else empty end')"
function srcmap_to() { realpath "$(rustc --print sysroot)/lib/rustlib/src/rust/src"; }
function srcmap_from() { strings target/debug/$1 | grep -o '^/rustc/[^/]\+/' | uniq; }
echo "\"lldb.launch.sourceMap\": {"
for target in `echo $crates`; do
target_path="$(realpath $cargo_bin/debug/$target)"
if [ -f "${target_path}" ]; then
echo " \"$(srcmap_from $target)\": \"$(srcmap_to)\","
fi
done
echo "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment