#include "xray_utils.h"
#include "sanitizer_common/sanitizer_allocator_internal.h"
#include "sanitizer_common/sanitizer_common.h"
#include "xray_allocator.h"
#include "xray_defs.h"
#include "xray_flags.h"
#include <cstdio>
#include <errno.h>
#include <fcntl.h>
#include <iterator>
#include <new>
#include <stdlib.h>
#include <sys/types.h>
#include <tuple>
#include <unistd.h>
#include <utility>
#if SANITIZER_FUCHSIA
#include "sanitizer_common/sanitizer_symbolizer_markup_constants.h"
#include <inttypes.h>
#include <zircon/process.h>
#include <zircon/sanitizer.h>
#include <zircon/status.h>
#include <zircon/syscalls.h>
#endif
namespace __xray {
#if SANITIZER_FUCHSIA
constexpr const char* ProfileSinkName = "llvm-xray";
LogWriter::~LogWriter() {
_zx_handle_close(Vmo);
}
void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT {
if (Begin == End)
return;
auto TotalBytes = std::distance(Begin, End);
const size_t PageSize = flags()->xray_page_size_override > 0
? flags()->xray_page_size_override
: GetPageSizeCached();
if (RoundUpTo(Offset, PageSize) != RoundUpTo(Offset + TotalBytes, PageSize)) {
zx_status_t Status = _zx_vmo_set_size(Vmo, Offset + TotalBytes);
if (Status != ZX_OK) {
Report("Failed to resize VMO: %s\n", _zx_status_get_string(Status));
return;
}
}
zx_status_t Status = _zx_vmo_write(Vmo, Begin, Offset, TotalBytes);
if (Status != ZX_OK) {
Report("Failed to write: %s\n", _zx_status_get_string(Status));
return;
}
Offset += TotalBytes;
_zx_object_set_property(Vmo, ZX_PROP_VMO_CONTENT_SIZE,
&Offset, sizeof(Offset));
}
void LogWriter::Flush() XRAY_NEVER_INSTRUMENT {
}
LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT {
zx_handle_t Vmo;
zx_status_t Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &Vmo);
if (Status != ZX_OK) {
Report("XRay: cannot create VMO: %s\n", _zx_status_get_string(Status));
return nullptr;
}
zx_info_handle_basic_t Info;
Status = _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info,
sizeof(Info), NULL, NULL);
if (Status != ZX_OK) {
Report("XRay: cannot get basic info about current process handle: %s\n",
_zx_status_get_string(Status));
return nullptr;
}
char VmoName[ZX_MAX_NAME_LEN];
internal_snprintf(VmoName, sizeof(VmoName), "%s.%zu", ProfileSinkName,
Info.koid);
_zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName));
zx_handle_t Handle;
Status =_zx_handle_duplicate(Vmo, ZX_RIGHT_SAME_RIGHTS, &Handle);
if (Status != ZX_OK) {
Report("XRay: cannot duplicate VMO handle: %s\n",
_zx_status_get_string(Status));
return nullptr;
}
__sanitizer_publish_data(ProfileSinkName, Handle);
Report("XRay: " FORMAT_DUMPFILE "\n", ProfileSinkName, VmoName);
LogWriter *LW = reinterpret_cast<LogWriter *>(InternalAlloc(sizeof(LogWriter)));
new (LW) LogWriter(Vmo);
return LW;
}
void LogWriter::Close(LogWriter *LW) {
LW->~LogWriter();
InternalFree(LW);
}
#else
LogWriter::~LogWriter() { … }
void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT { … }
void LogWriter::Flush() XRAY_NEVER_INSTRUMENT { … }
LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT { … }
void LogWriter::Close(LogWriter *LW) { … }
#endif
}