Skip to content

[XRay] Add __xray_default_options to specify build-time defined options #117921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler-rt/lib/xray/weak_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ___start_xray_fn_idx
___start_xray_instr_map
___stop_xray_fn_idx
___stop_xray_instr_map
___xray_default_options
8 changes: 8 additions & 0 deletions compiler-rt/lib/xray/xray_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ void initializeFlags() XRAY_NEVER_INSTRUMENT {
const char *XRayCompileFlags = useCompilerDefinedFlags();
XRayParser.ParseString(XRayCompileFlags);

// Use options provided at build time of the instrumented program.
const char *XRayDefaultOptions = __xray_default_options();
XRayParser.ParseString(XRayDefaultOptions);

// Override from environment variables.
XRayParser.ParseStringFromEnv("XRAY_OPTIONS");

Expand All @@ -82,3 +86,7 @@ void initializeFlags() XRAY_NEVER_INSTRUMENT {
}

} // namespace __xray

SANITIZER_INTERFACE_WEAK_DEF(const char *, __xray_default_options, void) {
return "";
}
7 changes: 7 additions & 0 deletions compiler-rt/lib/xray/xray_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
#include "sanitizer_common/sanitizer_flag_parser.h"
#include "sanitizer_common/sanitizer_internal_defs.h"

extern "C" {
// Users can specify their default options upon building the instrumented
// binaries by provide a definition of this function.
SANITIZER_INTERFACE_ATTRIBUTE
const char *__xray_default_options();
}

namespace __xray {

struct Flags {
Expand Down
16 changes: 16 additions & 0 deletions compiler-rt/test/xray/TestCases/Posix/default-options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: rm -fr %t && mkdir %t && cd %t
// RUN: %clang_xray %s -o a.out
// RUN: %run %t/a.out 2>&1 | FileCheck %s

// REQUIRES: built-in-llvm-tree

extern "C" __attribute__((xray_never_instrument)) const char *
__xray_default_options() {
return "patch_premain=true:verbosity=1:xray_mode=xray-basic";
}

__attribute__((xray_always_instrument)) void always() {}

int main() { always(); }

// CHECK: =={{[0-9].*}}==XRay: Log file in '{{.*}}'
28 changes: 27 additions & 1 deletion llvm/docs/XRay.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ Also by default the filename of the XRay trace is ``xray-log.XXXXXX`` where the
``XXXXXX`` part is randomly generated.

These options can be controlled through the ``XRAY_OPTIONS`` environment
variable, where we list down the options and their defaults below.
variable during program run-time, where we list down the options and their
defaults below.

+-------------------+-----------------+---------------+------------------------+
| Option | Type | Default | Description |
Expand All @@ -178,6 +179,31 @@ variable, where we list down the options and their defaults below.
+-------------------+-----------------+---------------+------------------------+


In addition to environment variable, you can also provide your own definition of
``const char *__xray_default_options(void)`` function, which returns the option
strings. This method effectively provides default options during program build
time. For example, you can create an additional source file (e.g. ``xray-opt.c``
) with the following ``__xray_default_options`` definition:

.. code-block:: c

__attribute__((xray_never_instrument))
const char *__xray_default_options() {
return "patch_premain=true,xray_mode=xray-basic";
}

And link it with the program you want to instrument:

::

clang -fxray-instrument prog.c xray-opt.c ...

The instrumented binary will use 'patch_premain=true,xray_mode=xray-basic' by
default even without setting ``XRAY_OPTIONS``.

Note that you still can override options designated by ``__xray_default_options``
using ``XRAY_OPTIONS`` during run-time.

If you choose to not use the default logging implementation that comes with the
XRay runtime and/or control when/how the XRay instrumentation runs, you may use
the XRay APIs directly for doing so. To do this, you'll need to include the
Expand Down