]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_init.cc
Merge ^/head r317503 through r317807.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_init.cc
1 //===-- xray_init.cc --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 //
12 // XRay initialisation logic.
13 //===----------------------------------------------------------------------===//
14
15 #include <fcntl.h>
16 #include <strings.h>
17 #include <unistd.h>
18
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "xray_defs.h"
21 #include "xray_flags.h"
22 #include "xray_interface_internal.h"
23
24 extern "C" {
25 void __xray_init();
26 extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
27 extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
28 }
29
30 using namespace __xray;
31
32 // When set to 'true' this means the XRay runtime has been initialised. We use
33 // the weak symbols defined above (__start_xray_inst_map and
34 // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
35 // for runtime patching/unpatching of instrumentation points.
36 //
37 // FIXME: Support DSO instrumentation maps too. The current solution only works
38 // for statically linked executables.
39 __sanitizer::atomic_uint8_t XRayInitialized{0};
40
41 // This should always be updated before XRayInitialized is updated.
42 __sanitizer::SpinMutex XRayInstrMapMutex;
43 XRaySledMap XRayInstrMap;
44
45 // __xray_init() will do the actual loading of the current process' memory map
46 // and then proceed to look for the .xray_instr_map section/segment.
47 void __xray_init() XRAY_NEVER_INSTRUMENT {
48   initializeFlags();
49   if (__start_xray_instr_map == nullptr) {
50     Report("XRay instrumentation map missing. Not initializing XRay.\n");
51     return;
52   }
53
54   {
55     __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
56     XRayInstrMap.Sleds = __start_xray_instr_map;
57     XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
58   }
59   __sanitizer::atomic_store(&XRayInitialized, true,
60                             __sanitizer::memory_order_release);
61
62   if (flags()->patch_premain)
63     __xray_patch();
64 }
65
66 __attribute__((section(".preinit_array"),
67                used)) void (*__local_xray_preinit)(void) = __xray_init;