1 //===-- xray_utils.cpp ------------------------------------------*- C++ -*-===//
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 is a part of XRay, a dynamic runtime instrumentation system.
11 //===----------------------------------------------------------------------===//
12 #include "xray_utils.h"
14 #include "sanitizer_common/sanitizer_allocator_internal.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "xray_allocator.h"
17 #include "xray_defs.h"
18 #include "xray_flags.h"
25 #include <sys/types.h>
31 #include "sanitizer_common/sanitizer_symbolizer_fuchsia.h"
34 #include <zircon/process.h>
35 #include <zircon/sanitizer.h>
36 #include <zircon/status.h>
37 #include <zircon/syscalls.h>
43 constexpr const char* ProfileSinkName
= "llvm-xray";
45 LogWriter::~LogWriter() {
46 _zx_handle_close(Vmo
);
49 void LogWriter::WriteAll(const char *Begin
, const char *End
) XRAY_NEVER_INSTRUMENT
{
52 auto TotalBytes
= std::distance(Begin
, End
);
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
));
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
));
74 // Record the data size as a property of the VMO.
75 _zx_object_set_property(Vmo
, ZX_PROP_VMO_CONTENT_SIZE
,
76 &Offset
, sizeof(Offset
));
79 void LogWriter::Flush() XRAY_NEVER_INSTRUMENT
{
80 // Nothing to do here since WriteAll writes directly into the VMO.
83 LogWriter
*LogWriter::Open() XRAY_NEVER_INSTRUMENT
{
84 // Create VMO to hold the profile data.
86 zx_status_t Status
= _zx_vmo_create(0, ZX_VMO_RESIZABLE
, &Vmo
);
87 if (Status
!= ZX_OK
) {
88 Report("XRay: cannot create VMO: %s\n", _zx_status_get_string(Status
));
92 // Get the KOID of the current process to use in the VMO name.
93 zx_info_handle_basic_t Info
;
94 Status
= _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC
, &Info
,
95 sizeof(Info
), NULL
, NULL
);
96 if (Status
!= ZX_OK
) {
97 Report("XRay: cannot get basic info about current process handle: %s\n",
98 _zx_status_get_string(Status
));
102 // Give the VMO a name including our process KOID so it's easy to spot.
103 char VmoName
[ZX_MAX_NAME_LEN
];
104 internal_snprintf(VmoName
, sizeof(VmoName
), "%s.%zu", ProfileSinkName
,
106 _zx_object_set_property(Vmo
, ZX_PROP_NAME
, VmoName
, strlen(VmoName
));
108 // Duplicate the handle since __sanitizer_publish_data consumes it and
109 // LogWriter needs to hold onto it.
111 Status
=_zx_handle_duplicate(Vmo
, ZX_RIGHT_SAME_RIGHTS
, &Handle
);
112 if (Status
!= ZX_OK
) {
113 Report("XRay: cannot duplicate VMO handle: %s\n",
114 _zx_status_get_string(Status
));
118 // Publish the VMO that receives the logging. Note the VMO's contents can
119 // grow and change after publication. The contents won't be read out until
120 // after the process exits.
121 __sanitizer_publish_data(ProfileSinkName
, Handle
);
123 // Use the dumpfile symbolizer markup element to write the name of the VMO.
124 Report("XRay: " FORMAT_DUMPFILE
"\n", ProfileSinkName
, VmoName
);
126 LogWriter
*LW
= reinterpret_cast<LogWriter
*>(InternalAlloc(sizeof(LogWriter
)));
127 new (LW
) LogWriter(Vmo
);
131 void LogWriter::Close(LogWriter
*LW
) {
135 #else // SANITIZER_FUCHSIA
136 LogWriter::~LogWriter() {
140 void LogWriter::WriteAll(const char *Begin
, const char *End
) XRAY_NEVER_INSTRUMENT
{
143 auto TotalBytes
= std::distance(Begin
, End
);
144 while (auto Written
= write(Fd
, Begin
, TotalBytes
)) {
147 continue; // Try again.
148 Report("Failed to write; errno = %d\n", errno
);
151 TotalBytes
-= Written
;
158 void LogWriter::Flush() XRAY_NEVER_INSTRUMENT
{
162 LogWriter
*LogWriter::Open() XRAY_NEVER_INSTRUMENT
{
163 // Open a temporary file once for the log.
164 char TmpFilename
[256] = {};
165 char TmpWildcardPattern
[] = "XXXXXX";
166 auto **Argv
= GetArgv();
167 const char *Progname
= !Argv
? "(unknown)" : Argv
[0];
168 const char *LastSlash
= internal_strrchr(Progname
, '/');
170 if (LastSlash
!= nullptr)
171 Progname
= LastSlash
+ 1;
173 int NeededLength
= internal_snprintf(
174 TmpFilename
, sizeof(TmpFilename
), "%s%s.%s",
175 flags()->xray_logfile_base
, Progname
, TmpWildcardPattern
);
176 if (NeededLength
> int(sizeof(TmpFilename
))) {
177 Report("XRay log file name too long (%d): %s\n", NeededLength
, TmpFilename
);
180 int Fd
= mkstemp(TmpFilename
);
182 Report("XRay: Failed opening temporary file '%s'; not logging events.\n",
187 Report("XRay: Log file in '%s'\n", TmpFilename
);
189 LogWriter
*LW
= allocate
<LogWriter
>();
190 new (LW
) LogWriter(Fd
);
194 void LogWriter::Close(LogWriter
*LW
) {
198 #endif // SANITIZER_FUCHSIA
200 } // namespace __xray