LD_PRELOAD environment variable

545 views
Skip to first unread message

Shabnam Ab

unread,
Mar 3, 2023, 6:42:56 PM3/3/23
to bazel-discuss
Hi folks,

I need to get the full argument lists when bazel run rustc and replay the build.
I tried to capture the input arguments when exec APIs are running but it looks like even if I set the LD_PRELOAD for whole system, bazel does not pass it to the processes that it spawns. Is there any way to set a global env variable that bazel passes to the all the processes?

Could you please advise?
Thanks!


Brian Silverman

unread,
Mar 3, 2023, 8:40:29 PM3/3/23
to Shabnam Ab, bazel-discuss
--action_env=LD_PRELOAD=xyz is the closest to a global setting you'll get. Individual actions can still override that, but generally won't.

If you want the full command lines (including setting environment variables), run your build with --subcommands.

If you want those logged commands to actually be runnable, add --sandbox_debug. Be aware it will leave all the temporary files, which may increase disk usage a lot, they're cleared at the end of the next `bazel build` without --sandbox_debug.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/f5f8a224-2fec-487e-bd5b-1efff90a1ea1n%40googlegroups.com.

Shabnam Ab

unread,
Mar 3, 2023, 9:09:35 PM3/3/23
to Brian Silverman, bazel-discuss
Hey Brian,

Thank you for the information!

I have attempted the suggested options, but unfortunately they did not work. It's possible that there are mistakes in my LD_PRELOAD binary. I did not observe any difference in the log content when using sandbox_debug. Is it expected to see the actual rustc commands? Perhaps I am missing something.

I need to programmatically extract these commands, so I am looking for the best option available.

Thanks!


Brian Silverman

unread,
Mar 3, 2023, 10:26:03 PM3/3/23
to Shabnam Ab, bazel-discuss
--subcommands shows all the commands Bazel is running. You may need to `bazel clean` or make a change to one of your source files before trying it so bazel doesn't skip things which are already in the action cache.

If you want to extract this programmatically, you'll want either bazel aquery or aspects, which both avoid the need for `bazel clean`. Aspects offer integration with Bazel's caching system and more easily accessible information about the actions (providers of the rules they're from, Starlark File objects, etc), which may or may not be an advantage depending on what you're doing. aquery with one of the machine-readable formats (proto, textproto, or jsonproto) is easy to parse with an external tool, but reconstructing the information you want from the protos may be difficult.

Also, rules_rust applies some further wrappers around rustc to set up the environment correctly, so you have to look for those. I would look through the output for the labels of your rules, and see what actions they have. I happen to have a rules_rust project handy, the commands you're looking for look like this (linking a rust_binary):

SUBCOMMAND: # //rust/abc:abc [action 'Compiling Rust bin abc (13 files)', configuration: <bla bla>, execution platform: //tools/platforms:amd64]
(cd /home/brian/.cache/bazel/_bazel_brian/b648ee8a4cf3ea5f702d496242c79348/execroot/com_xyz && \
  exec env - \
    CARGO_CFG_TARGET_ARCH=aarch64 \
    CARGO_CFG_TARGET_OS=linux \
    CARGO_CRATE_NAME=abc \
    CARGO_MANIFEST_DIR='${pwd}/rust/abc' \
    CARGO_PKG_AUTHORS='' \
    CARGO_PKG_DESCRIPTION='' \
    CARGO_PKG_HOMEPAGE='' \
    CARGO_PKG_NAME=abc \
    CARGO_PKG_VERSION=0.0.0 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=0 \
    CARGO_PKG_VERSION_PATCH=0 \
    CARGO_PKG_VERSION_PRE='' \
    OUT_DIR='${pwd}/bazel-out/k8-opt--cv2/bin/rust/abc/abc_build_script.out_dir' \
    SYSROOT=bazel-out/k8-opt--cv2/bin/external/rust_linux_x86_64__aarch64-unknown-linux-gnu__stable_tools/rust_toolchain \
  bazel-out/k8-opt-exec-589A3AC8/bin/external/rules_rust/util/process_wrapper/process_wrapper --env-file bazel-out/k8-opt--cv2/bin/rust/abc/abc_build_script.env --arg-file bazel-out/k8-opt--cv2/bin/external/raze__libc__0_2_139/libc_build_script.linksearchpaths --arg-file bazel-out/k8-opt--cv2/bin/external/raze__anyhow__1_0_65/anyhow_build_script.linksearchpaths --arg-file bazel-out/k8-opt--cv2/bin/external/raze__memchr__2_5_0/memchr_build_script.linksearchpaths --arg-file bazel-out/k8-opt--cv2/bin/rust/abc/abc_build_script.linksearchpaths --arg-file bazel-out/k8-opt--cv2/bin/rust/abc/abc_build_script.flags --arg-file bazel-out/k8-opt--cv2/bin/rust/abc/abc_build_script.linkflags --subst 'pwd=${pwd}' --subst-multipwd true -- bazel-out/k8-opt--cv2/bin/external/rust_linux_x86_64__aarch64-unknown-linux-gnu__stable_tools/rust_toolchain/bin/rustc rust/abc/src/abc_main.rs '--crate-name=abc' '--crate-type=bin' '--error-format=human' '--out-dir=bazel-out/k8-opt--cv2/bin/rust/abc' '--codegen=opt-level=3' '--codegen=debuginfo=0' '--remap-path-prefix=${pwd}=' '--emit=dep-info,link' '--color=always' '--target=aarch64-unknown-linux-gnu' -L bazel-out/k8-opt--cv2/bin/external/rust_linux_x86_64__aarch64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/aarch64-unknown-linux-gnu/lib '--edition=2021' '--codegen=linker=external/llvm_toolchain/bin/cc_wrapper.sh' '--extern=cfg_if=bazel-out/k8-opt--cv2/bin/external/raze__cfg_if__1_0_0/libcfg_if-1959545905.rlib' '--extern=clap=bazel-out/k8-opt--cv2/bin/external/raze__clap__4_0_29/libclap-3778812063.rlib' '--extern=env_logger=bazel-out/k8-opt--cv2/bin/external/raze__env_logger__0_9_1/libenv_logger-1536438461.rlib' '--extern=ftrace=bazel-out/k8-opt--cv2/bin/rust/ftrace/libftrace-2099929474.rlib' '-Ldependency=bazel-out/k8-opt--cv2/bin/external/raze__zerocopy__0_6_1' '-Ldependency=bazel-out/k8-opt-exec-589A3AC8/bin/external/raze__extend__1_1_2' '-Cforce-unwind-tables=yes')

Note that my example here has various parts redacted and some dependencies removed to make it shorter.

Shabnam Ab

unread,
Mar 3, 2023, 10:44:57 PM3/3/23
to Brian Silverman, bazel-discuss
Thanks so much, I'll check it out.
Reply all
Reply to author
Forward
0 new messages