1 /*===- InstrProfilingPlatformFuchsia.c - Profile data Fuchsia platform ----===*\ 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 implements the profiling runtime for Fuchsia and defines the 11 * shared profile runtime interface. Each module (executable or DSO) statically 12 * links in the whole profile runtime to satisfy the calls from its 13 * instrumented code. Several modules in the same program might be separately 14 * compiled and even use different versions of the instrumentation ABI and data 15 * format. All they share in common is the VMO and the offset, which live in 16 * exported globals so that exactly one definition will be shared across all 17 * modules. Each module has its own independent runtime that registers its own 18 * atexit hook to append its own data into the shared VMO which is published 19 * via the data sink hook provided by Fuchsia's dynamic linker. 20 */ 21 22 #if defined(__Fuchsia__) 23 24 #include <inttypes.h> 25 #include <stdarg.h> 26 #include <stdbool.h> 27 #include <stdlib.h> 28 29 #include <zircon/process.h> 30 #include <zircon/sanitizer.h> 31 #include <zircon/syscalls.h> 32 33 #include "InstrProfiling.h" 34 #include "InstrProfilingInternal.h" 35 #include "InstrProfilingUtil.h" 36 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. 40 */ 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. 45 */ 46 uint64_t __llvm_profile_offset; 47 48 static const char ProfileSinkName[] = "llvm-profile"; 49 50 static inline void lprofWrite(const char *fmt, ...) { 51 char s[256]; 52 53 va_list ap; 54 va_start(ap, fmt); 55 int ret = vsnprintf(s, sizeof(s), fmt, ap); 56 va_end(ap); 57 58 __sanitizer_log_write(s, ret + 1); 59 } 60 61 static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, 62 uint32_t NumIOVecs) { 63 /* Allocate VMO if it hasn't been created yet. */ 64 if (__llvm_profile_vmo == ZX_HANDLE_INVALID) { 65 /* Get information about the current process. */ 66 zx_info_handle_basic_t Info; 67 zx_status_t Status = 68 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info, 69 sizeof(Info), NULL, NULL); 70 if (Status != ZX_OK) 71 return -1; 72 73 /* Create VMO to hold the profile data. */ 74 Status = _zx_vmo_create(0, 0, &__llvm_profile_vmo); 75 if (Status != ZX_OK) 76 return -1; 77 78 /* Give the VMO a name including our process KOID so it's easy to spot. */ 79 char VmoName[ZX_MAX_NAME_LEN]; 80 snprintf(VmoName, sizeof(VmoName), "%s.%" PRIu64, ProfileSinkName, 81 Info.koid); 82 _zx_object_set_property(__llvm_profile_vmo, ZX_PROP_NAME, VmoName, 83 strlen(VmoName)); 84 85 /* Duplicate the handle since __sanitizer_publish_data consumes it. */ 86 zx_handle_t Handle; 87 Status = 88 _zx_handle_duplicate(__llvm_profile_vmo, ZX_RIGHT_SAME_RIGHTS, &Handle); 89 if (Status != ZX_OK) 90 return -1; 91 92 /* Publish the VMO which contains profile data to the system. */ 93 __sanitizer_publish_data(ProfileSinkName, Handle); 94 95 /* Use the dumpfile symbolizer markup element to write the name of VMO. */ 96 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", 97 ProfileSinkName, VmoName); 98 } 99 100 /* Compute the total length of data to be written. */ 101 size_t Length = 0; 102 for (uint32_t I = 0; I < NumIOVecs; I++) 103 Length += IOVecs[I].ElmSize * IOVecs[I].NumElm; 104 105 /* Resize the VMO to ensure there's sufficient space for the data. */ 106 zx_status_t Status = 107 _zx_vmo_set_size(__llvm_profile_vmo, __llvm_profile_offset + Length); 108 if (Status != ZX_OK) 109 return -1; 110 111 /* Copy the data into VMO. */ 112 for (uint32_t I = 0; I < NumIOVecs; I++) { 113 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 114 if (IOVecs[I].Data) { 115 Status = _zx_vmo_write(__llvm_profile_vmo, IOVecs[I].Data, 116 __llvm_profile_offset, Length); 117 if (Status != ZX_OK) 118 return -1; 119 } 120 __llvm_profile_offset += Length; 121 } 122 123 return 0; 124 } 125 126 static void initVMOWriter(ProfDataWriter *This) { 127 This->Write = lprofVMOWriter; 128 This->WriterCtx = NULL; 129 } 130 131 static int dump(void) { 132 if (lprofProfileDumped()) { 133 lprofWrite("Profile data not published: already written.\n"); 134 return 0; 135 } 136 137 /* Check if there is llvm/runtime version mismatch. */ 138 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { 139 lprofWrite("Runtime and instrumentation version mismatch : " 140 "expected %d, but got %d\n", 141 INSTR_PROF_RAW_VERSION, 142 (int)GET_VERSION(__llvm_profile_get_version())); 143 return -1; 144 } 145 146 /* Write the profile data into the mapped region. */ 147 ProfDataWriter VMOWriter; 148 initVMOWriter(&VMOWriter); 149 if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0) 150 return -1; 151 152 return 0; 153 } 154 155 COMPILER_RT_VISIBILITY 156 int __llvm_profile_dump(void) { 157 int rc = dump(); 158 lprofSetProfileDumped(); 159 return rc; 160 } 161 162 static void dumpWithoutReturn(void) { dump(); } 163 164 /* This method is invoked by the runtime initialization hook 165 * InstrProfilingRuntime.o if it is linked in. 166 */ 167 COMPILER_RT_VISIBILITY 168 void __llvm_profile_initialize_file(void) {} 169 170 COMPILER_RT_VISIBILITY 171 int __llvm_profile_register_write_file_atexit(void) { 172 static bool HasBeenRegistered = false; 173 174 if (HasBeenRegistered) 175 return 0; 176 177 lprofSetupValueProfiler(); 178 179 HasBeenRegistered = true; 180 return atexit(dumpWithoutReturn); 181 } 182 183 #endif 184