Skip to content

Commit 96dd39c

Browse files
authored
[XRay] Add __xray_default_options to specify build-time defined options (#117921)
Similar to `__asan_default_options`, users can specify default options upon building the instrumented binaries by providing their own definition of `__xray_default_options` which returns the option strings. This is useful in cases where setting the `XRAY_OPTIONS` environment variable might be difficult. Plus, it's a convenient way to populate XRay options when you always want the instrumentation to be enabled.
1 parent 2df0d29 commit 96dd39c

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

compiler-rt/lib/xray/weak_symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ___start_xray_fn_idx
22
___start_xray_instr_map
33
___stop_xray_fn_idx
44
___stop_xray_instr_map
5+
___xray_default_options

compiler-rt/lib/xray/xray_flags.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ void initializeFlags() XRAY_NEVER_INSTRUMENT {
6767
const char *XRayCompileFlags = useCompilerDefinedFlags();
6868
XRayParser.ParseString(XRayCompileFlags);
6969

70+
// Use options provided at build time of the instrumented program.
71+
const char *XRayDefaultOptions = __xray_default_options();
72+
XRayParser.ParseString(XRayDefaultOptions);
73+
7074
// Override from environment variables.
7175
XRayParser.ParseStringFromEnv("XRAY_OPTIONS");
7276

@@ -82,3 +86,7 @@ void initializeFlags() XRAY_NEVER_INSTRUMENT {
8286
}
8387

8488
} // namespace __xray
89+
90+
SANITIZER_INTERFACE_WEAK_DEF(const char *, __xray_default_options, void) {
91+
return "";
92+
}

compiler-rt/lib/xray/xray_flags.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
#include "sanitizer_common/sanitizer_flag_parser.h"
1818
#include "sanitizer_common/sanitizer_internal_defs.h"
1919

20+
extern "C" {
21+
// Users can specify their default options upon building the instrumented
22+
// binaries by provide a definition of this function.
23+
SANITIZER_INTERFACE_ATTRIBUTE
24+
const char *__xray_default_options();
25+
}
26+
2027
namespace __xray {
2128

2229
struct Flags {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: rm -fr %t && mkdir %t && cd %t
2+
// RUN: %clang_xray %s -o a.out
3+
// RUN: %run %t/a.out 2>&1 | FileCheck %s
4+
5+
// REQUIRES: built-in-llvm-tree
6+
7+
extern "C" __attribute__((xray_never_instrument)) const char *
8+
__xray_default_options() {
9+
return "patch_premain=true:verbosity=1:xray_mode=xray-basic";
10+
}
11+
12+
__attribute__((xray_always_instrument)) void always() {}
13+
14+
int main() { always(); }
15+
16+
// CHECK: =={{[0-9].*}}==XRay: Log file in '{{.*}}'

llvm/docs/XRay.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Also by default the filename of the XRay trace is ``xray-log.XXXXXX`` where the
157157
``XXXXXX`` part is randomly generated.
158158

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

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

180181

182+
In addition to environment variable, you can also provide your own definition of
183+
``const char *__xray_default_options(void)`` function, which returns the option
184+
strings. This method effectively provides default options during program build
185+
time. For example, you can create an additional source file (e.g. ``xray-opt.c``
186+
) with the following ``__xray_default_options`` definition:
187+
188+
.. code-block:: c
189+
190+
__attribute__((xray_never_instrument))
191+
const char *__xray_default_options() {
192+
return "patch_premain=true,xray_mode=xray-basic";
193+
}
194+
195+
And link it with the program you want to instrument:
196+
197+
::
198+
199+
clang -fxray-instrument prog.c xray-opt.c ...
200+
201+
The instrumented binary will use 'patch_premain=true,xray_mode=xray-basic' by
202+
default even without setting ``XRAY_OPTIONS``.
203+
204+
Note that you still can override options designated by ``__xray_default_options``
205+
using ``XRAY_OPTIONS`` during run-time.
206+
181207
If you choose to not use the default logging implementation that comes with the
182208
XRay runtime and/or control when/how the XRay instrumentation runs, you may use
183209
the XRay APIs directly for doing so. To do this, you'll need to include the

0 commit comments

Comments
 (0)