1 /*===- InstrProfilingPlatformFuchsia.c - Profile data Fuchsia platform ----===*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 \*===----------------------------------------------------------------------===*/
9 * This file implements the profiling runtime for Fuchsia and defines the
10 * shared profile runtime interface. Each module (executable or DSO) statically
11 * links in the whole profile runtime to satisfy the calls from its
12 * instrumented code. Several modules in the same program might be separately
13 * compiled and even use different versions of the instrumentation ABI and data
14 * format. All they share in common is the VMO and the offset, which live in
15 * exported globals so that exactly one definition will be shared across all
16 * modules. Each module has its own independent runtime that registers its own
17 * atexit hook to append its own data into the shared VMO which is published
18 * via the data sink hook provided by Fuchsia's dynamic linker.
21 #if defined(__Fuchsia__)
28 #include <zircon/process.h>
29 #include <zircon/sanitizer.h>
30 #include <zircon/status.h>
31 #include <zircon/syscalls.h>
33 #include "InstrProfiling.h"
34 #include "InstrProfilingInternal.h"
35 #include "InstrProfilingUtil.h"
37 /* VMO that contains the coverage data shared across all modules. This symbol
38 * has default visibility and is exported in each module (executable or DSO)
39 * that statically links in the profiling runtime.
41 zx_handle_t __llvm_profile_vmo;
42 /* Current offset within the VMO where data should be written next. This symbol
43 * has default visibility and is exported in each module (executable or DSO)
44 * that statically links in the profiling runtime.
46 uint64_t __llvm_profile_offset;
48 static const char ProfileSinkName[] = "llvm-profile";
50 static inline void lprofWrite(const char *fmt, ...) {
55 int ret = vsnprintf(s, sizeof(s), fmt, ap);
58 __sanitizer_log_write(s, ret + 1);
61 static void createVMO() {
62 /* Don't create VMO if it has been alread created. */
63 if (__llvm_profile_vmo != ZX_HANDLE_INVALID)
66 /* Get information about the current process. */
67 zx_info_handle_basic_t Info;
69 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info,
70 sizeof(Info), NULL, NULL);
71 if (Status != ZX_OK) {
72 lprofWrite("LLVM Profile: cannot get info about current process: %s\n",
73 _zx_status_get_string(Status));
77 /* Create VMO to hold the profile data. */
78 Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &__llvm_profile_vmo);
79 if (Status != ZX_OK) {
80 lprofWrite("LLVM Profile: cannot create VMO: %s\n",
81 _zx_status_get_string(Status));
85 /* Give the VMO a name including our process KOID so it's easy to spot. */
86 char VmoName[ZX_MAX_NAME_LEN];
87 snprintf(VmoName, sizeof(VmoName), "%s.%" PRIu64, ProfileSinkName, Info.koid);
88 _zx_object_set_property(__llvm_profile_vmo, ZX_PROP_NAME, VmoName,
91 /* Duplicate the handle since __sanitizer_publish_data consumes it. */
94 _zx_handle_duplicate(__llvm_profile_vmo, ZX_RIGHT_SAME_RIGHTS, &Handle);
95 if (Status != ZX_OK) {
96 lprofWrite("LLVM Profile: cannot duplicate VMO handle: %s\n",
97 _zx_status_get_string(Status));
98 _zx_handle_close(__llvm_profile_vmo);
99 __llvm_profile_vmo = ZX_HANDLE_INVALID;
103 /* Publish the VMO which contains profile data to the system. */
104 __sanitizer_publish_data(ProfileSinkName, Handle);
106 /* Use the dumpfile symbolizer markup element to write the name of VMO. */
107 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName);
110 static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
111 uint32_t NumIOVecs) {
112 /* Compute the total length of data to be written. */
114 for (uint32_t I = 0; I < NumIOVecs; I++)
115 Length += IOVecs[I].ElmSize * IOVecs[I].NumElm;
117 /* Resize the VMO to ensure there's sufficient space for the data. */
119 _zx_vmo_set_size(__llvm_profile_vmo, __llvm_profile_offset + Length);
123 /* Copy the data into VMO. */
124 for (uint32_t I = 0; I < NumIOVecs; I++) {
125 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
126 if (IOVecs[I].Data) {
127 Status = _zx_vmo_write(__llvm_profile_vmo, IOVecs[I].Data,
128 __llvm_profile_offset, Length);
131 } else if (IOVecs[I].UseZeroPadding) {
132 /* Resizing the VMO should zero fill. */
134 __llvm_profile_offset += Length;
140 static void initVMOWriter(ProfDataWriter *This) {
141 This->Write = lprofVMOWriter;
142 This->WriterCtx = NULL;
145 static int dump(void) {
146 if (lprofProfileDumped()) {
147 lprofWrite("LLVM Profile: data not published: already written.\n");
151 /* Check if there is llvm/runtime version mismatch. */
152 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
153 lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: "
154 "expected %d, but got %d\n",
155 INSTR_PROF_RAW_VERSION,
156 (int)GET_VERSION(__llvm_profile_get_version()));
160 /* Write the profile data into the mapped region. */
161 ProfDataWriter VMOWriter;
162 initVMOWriter(&VMOWriter);
163 if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0)
169 COMPILER_RT_VISIBILITY
170 int __llvm_profile_dump(void) {
172 lprofSetProfileDumped();
176 static void dumpWithoutReturn(void) { dump(); }
178 /* This method is invoked by the runtime initialization hook
179 * InstrProfilingRuntime.o if it is linked in.
181 COMPILER_RT_VISIBILITY
182 void __llvm_profile_initialize_file(void) { createVMO(); }
184 COMPILER_RT_VISIBILITY
185 int __llvm_profile_register_write_file_atexit(void) {
186 static bool HasBeenRegistered = false;
188 if (HasBeenRegistered)
191 lprofSetupValueProfiler();
193 HasBeenRegistered = true;
194 return atexit(dumpWithoutReturn);