blob: 59ba6c3082b2aa45fc945500fc7f223bcb615f0c [file] [log] [blame]
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +00001//===-- xray_utils.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//===----------------------------------------------------------------------===//
13#include "xray_utils.h"
14
Petr Hosek6a8cede2018-10-16 01:24:46 +000015#include "sanitizer_common/sanitizer_allocator_internal.h"
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000016#include "sanitizer_common/sanitizer_common.h"
Petr Hosek6a8cede2018-10-16 01:24:46 +000017#include "xray_allocator.h"
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000018#include "xray_defs.h"
19#include "xray_flags.h"
20#include <cstdio>
Krzysztof Parzyszek520e51d2017-01-25 14:20:30 +000021#include <errno.h>
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000022#include <fcntl.h>
23#include <iterator>
Dean Michael Berrisb81372b2018-06-08 07:48:03 +000024#include <stdlib.h>
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000025#include <sys/types.h>
26#include <tuple>
27#include <unistd.h>
28#include <utility>
29
Petr Hoseke7dec782018-11-22 02:00:44 +000030#if SANITIZER_FUCHSIA
31#include "sanitizer_common/sanitizer_symbolizer_fuchsia.h"
32
33#include <inttypes.h>
34#include <zircon/process.h>
35#include <zircon/sanitizer.h>
36#include <zircon/status.h>
37#include <zircon/syscalls.h>
38#endif
39
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000040namespace __xray {
41
Petr Hoseke7dec782018-11-22 02:00:44 +000042#if SANITIZER_FUCHSIA
43constexpr const char* ProfileSinkName = "llvm-xray";
44
45LogWriter::~LogWriter() {
46 _zx_handle_close(Vmo);
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +000047}
48
Petr Hoseke7dec782018-11-22 02:00:44 +000049void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT {
50 if (Begin == End)
51 return;
52 auto TotalBytes = std::distance(Begin, End);
53
54 const size_t PageSize = flags()->xray_page_size_override > 0
55 ? flags()->xray_page_size_override
56 : GetPageSizeCached();
57 if (RoundUpTo(Offset, PageSize) != RoundUpTo(Offset + TotalBytes, PageSize)) {
58 // Resize the VMO to ensure there's sufficient space for the data.
59 zx_status_t Status = _zx_vmo_set_size(Vmo, Offset + TotalBytes);
60 if (Status != ZX_OK) {
61 Report("Failed to resize VMO: %s\n", _zx_status_get_string(Status));
62 return;
63 }
64 }
65
66 // Write the data into VMO.
67 zx_status_t Status = _zx_vmo_write(Vmo, Begin, Offset, TotalBytes);
68 if (Status != ZX_OK) {
69 Report("Failed to write: %s\n", _zx_status_get_string(Status));
70 return;
71 }
72 Offset += TotalBytes;
73}
74
75void LogWriter::Flush() XRAY_NEVER_INSTRUMENT {
76 // Nothing to do here since WriteAll writes directly into the VMO.
77}
78
79LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT {
80 // Create VMO to hold the profile data.
81 zx_handle_t Vmo;
82 zx_status_t Status = _zx_vmo_create(0, 0, &Vmo);
83 if (Status != ZX_OK) {
84 Report("XRay: cannot create VMO: %s\n", _zx_status_get_string(Status));
85 return nullptr;
86 }
87
88 // Get the KOID of the current process to use in the VMO name.
89 zx_info_handle_basic_t Info;
90 Status = _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info,
91 sizeof(Info), NULL, NULL);
92 if (Status != ZX_OK) {
93 Report("XRay: cannot get basic info about current process handle: %s\n",
94 _zx_status_get_string(Status));
95 return nullptr;
96 }
97
98 // Give the VMO a name including our process KOID so it's easy to spot.
99 char VmoName[ZX_MAX_NAME_LEN];
100 internal_snprintf(VmoName, sizeof(VmoName), "%s.%zu", ProfileSinkName,
101 Info.koid);
102 _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName));
103
104 // Duplicate the handle since __sanitizer_publish_data consumes it and
105 // LogWriter needs to hold onto it.
106 zx_handle_t Handle;
107 Status =_zx_handle_duplicate(Vmo, ZX_RIGHT_SAME_RIGHTS, &Handle);
108 if (Status != ZX_OK) {
109 Report("XRay: cannot duplicate VMO handle: %s\n",
110 _zx_status_get_string(Status));
111 return nullptr;
112 }
113
114 // Publish the VMO that receives the logging. Note the VMO's contents can
115 // grow and change after publication. The contents won't be read out until
116 // after the process exits.
117 __sanitizer_publish_data(ProfileSinkName, Handle);
118
119 // Use the dumpfile symbolizer markup element to write the name of the VMO.
120 Report("XRay: " FORMAT_DUMPFILE "\n", ProfileSinkName, VmoName);
121
122 LogWriter *LW = reinterpret_cast<LogWriter *>(InternalAlloc(sizeof(LogWriter)));
123 new (LW) LogWriter(Vmo);
124 return LW;
125}
126
127void LogWriter::Close(LogWriter *LW) {
128 LW->~LogWriter();
129 InternalFree(LW);
130}
131#else // SANITIZER_FUCHSIA
Petr Hosek6a8cede2018-10-16 01:24:46 +0000132LogWriter::~LogWriter() {
133 internal_close(Fd);
134}
135
136void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT {
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000137 if (Begin == End)
138 return;
139 auto TotalBytes = std::distance(Begin, End);
140 while (auto Written = write(Fd, Begin, TotalBytes)) {
141 if (Written < 0) {
142 if (errno == EINTR)
143 continue; // Try again.
144 Report("Failed to write; errno = %d\n", errno);
145 return;
146 }
147 TotalBytes -= Written;
148 if (TotalBytes == 0)
149 break;
150 Begin += Written;
151 }
152}
153
Petr Hosek6a8cede2018-10-16 01:24:46 +0000154void LogWriter::Flush() XRAY_NEVER_INSTRUMENT {
155 fsync(Fd);
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000156}
157
Petr Hosek6a8cede2018-10-16 01:24:46 +0000158LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT {
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000159 // Open a temporary file once for the log.
Dean Michael Berrisb81372b2018-06-08 07:48:03 +0000160 char TmpFilename[256] = {};
161 char TmpWildcardPattern[] = "XXXXXX";
162 auto **Argv = GetArgv();
163 const char *Progname = !Argv ? "(unknown)" : Argv[0];
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000164 const char *LastSlash = internal_strrchr(Progname, '/');
165
166 if (LastSlash != nullptr)
167 Progname = LastSlash + 1;
168
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000169 int NeededLength = internal_snprintf(
Douglas Yung22d49482018-10-01 20:03:53 +0000170 TmpFilename, sizeof(TmpFilename), "%s%s.%s",
171 flags()->xray_logfile_base, Progname, TmpWildcardPattern);
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000172 if (NeededLength > int(sizeof(TmpFilename))) {
173 Report("XRay log file name too long (%d): %s\n", NeededLength, TmpFilename);
Petr Hosek6a8cede2018-10-16 01:24:46 +0000174 return nullptr;
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000175 }
176 int Fd = mkstemp(TmpFilename);
177 if (Fd == -1) {
178 Report("XRay: Failed opening temporary file '%s'; not logging events.\n",
179 TmpFilename);
Petr Hosek6a8cede2018-10-16 01:24:46 +0000180 return nullptr;
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000181 }
Dean Michael Berris5eaaff62018-06-05 06:12:42 +0000182 if (Verbosity())
Dean Michael Berriseec462f2017-12-13 06:37:13 +0000183 Report("XRay: Log file in '%s'\n", TmpFilename);
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000184
Petr Hosek6a8cede2018-10-16 01:24:46 +0000185 LogWriter *LW = allocate<LogWriter>();
186 new (LW) LogWriter(Fd);
187 return LW;
188}
189
190void LogWriter::Close(LogWriter *LW) {
191 LW->~LogWriter();
192 deallocate(LW);
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000193}
Petr Hoseke7dec782018-11-22 02:00:44 +0000194#endif // SANITIZER_FUCHSIA
Dean Michael Berrise7dbebf2017-01-25 03:50:46 +0000195
196} // namespace __xray