2017-03-30 08:29:36 +08:00
|
|
|
//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Driver/XRayArgs.h"
|
|
|
|
#include "ToolChains/CommonArgs.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Driver/ToolChain.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2017-03-30 09:05:09 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2017-03-31 06:46:45 +08:00
|
|
|
#include "llvm/Support/SpecialCaseList.h"
|
2017-03-30 08:29:36 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr char XRayInstrumentOption[] = "-fxray-instrument";
|
|
|
|
constexpr char XRayInstructionThresholdOption[] =
|
|
|
|
"-fxray-instruction-threshold=";
|
2018-04-11 09:28:25 +08:00
|
|
|
constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
|
2017-03-30 08:29:36 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
|
|
|
|
const Driver &D = TC.getDriver();
|
|
|
|
const llvm::Triple &Triple = TC.getTriple();
|
|
|
|
if (Args.hasFlag(options::OPT_fxray_instrument,
|
|
|
|
options::OPT_fnoxray_instrument, false)) {
|
2018-02-22 14:31:40 +08:00
|
|
|
if (Triple.getOS() == llvm::Triple::Linux) {
|
2017-03-30 08:29:36 +08:00
|
|
|
switch (Triple.getArch()) {
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
case llvm::Triple::arm:
|
|
|
|
case llvm::Triple::aarch64:
|
|
|
|
case llvm::Triple::ppc64le:
|
|
|
|
case llvm::Triple::mips:
|
|
|
|
case llvm::Triple::mipsel:
|
|
|
|
case llvm::Triple::mips64:
|
|
|
|
case llvm::Triple::mips64el:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
D.Diag(diag::err_drv_clang_unsupported)
|
|
|
|
<< (std::string(XRayInstrumentOption) + " on " + Triple.str());
|
|
|
|
}
|
2018-04-04 20:47:49 +08:00
|
|
|
} else if (Triple.getOS() == llvm::Triple::FreeBSD ||
|
2018-05-11 08:58:55 +08:00
|
|
|
Triple.getOS() == llvm::Triple::OpenBSD ||
|
|
|
|
Triple.getOS() == llvm::Triple::NetBSD) {
|
2018-04-11 09:28:25 +08:00
|
|
|
if (Triple.getArch() != llvm::Triple::x86_64) {
|
|
|
|
D.Diag(diag::err_drv_clang_unsupported)
|
|
|
|
<< (std::string(XRayInstrumentOption) + " on " + Triple.str());
|
|
|
|
}
|
2018-02-22 14:31:40 +08:00
|
|
|
} else {
|
2017-03-30 08:29:36 +08:00
|
|
|
D.Diag(diag::err_drv_clang_unsupported)
|
2018-04-13 10:31:58 +08:00
|
|
|
<< (std::string(XRayInstrumentOption) + " on " + Triple.str());
|
2018-02-22 14:31:40 +08:00
|
|
|
}
|
2017-03-30 08:29:36 +08:00
|
|
|
XRayInstrument = true;
|
|
|
|
if (const Arg *A =
|
|
|
|
Args.getLastArg(options::OPT_fxray_instruction_threshold_,
|
|
|
|
options::OPT_fxray_instruction_threshold_EQ)) {
|
|
|
|
StringRef S = A->getValue();
|
|
|
|
if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
|
|
|
|
D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
|
|
|
|
}
|
|
|
|
|
2017-11-30 08:04:54 +08:00
|
|
|
// By default, the back-end will not emit the lowering for XRay customevent
|
|
|
|
// calls if the function is not instrumented. In the future we will change
|
|
|
|
// this default to be the reverse, but in the meantime we're going to
|
|
|
|
// introduce the new functionality behind a flag.
|
|
|
|
if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
|
|
|
|
options::OPT_fnoxray_always_emit_customevents, false))
|
|
|
|
XRayAlwaysEmitCustomEvents = true;
|
|
|
|
|
2018-04-18 05:32:43 +08:00
|
|
|
if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
|
|
|
|
options::OPT_fnoxray_always_emit_typedevents, false))
|
|
|
|
XRayAlwaysEmitTypedEvents = true;
|
|
|
|
|
2018-04-06 13:28:54 +08:00
|
|
|
if (!Args.hasFlag(options::OPT_fxray_link_deps,
|
|
|
|
options::OPT_fnoxray_link_deps, true))
|
|
|
|
XRayRT = false;
|
|
|
|
|
2018-04-13 10:31:58 +08:00
|
|
|
auto Bundles =
|
|
|
|
Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
|
|
|
|
if (Bundles.empty())
|
|
|
|
InstrumentationBundle.Mask = XRayInstrKind::All;
|
|
|
|
else
|
|
|
|
for (const auto &B : Bundles) {
|
|
|
|
llvm::SmallVector<StringRef, 2> BundleParts;
|
|
|
|
llvm::SplitString(B, BundleParts, ",");
|
|
|
|
for (const auto &P : BundleParts) {
|
|
|
|
// TODO: Automate the generation of the string case table.
|
|
|
|
auto Valid = llvm::StringSwitch<bool>(P)
|
|
|
|
.Cases("none", "all", "function", "custom", true)
|
|
|
|
.Default(false);
|
|
|
|
|
|
|
|
if (!Valid) {
|
|
|
|
D.Diag(clang::diag::err_drv_invalid_value)
|
|
|
|
<< "-fxray-instrumentation-bundle=" << P;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Mask = parseXRayInstrValue(P);
|
|
|
|
if (Mask == XRayInstrKind::None) {
|
|
|
|
InstrumentationBundle.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstrumentationBundle.Mask |= Mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 08:29:36 +08:00
|
|
|
// Validate the always/never attribute files. We also make sure that they
|
|
|
|
// are treated as actual dependencies.
|
|
|
|
for (const auto &Filename :
|
|
|
|
Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
|
|
|
|
if (llvm::sys::fs::exists(Filename)) {
|
|
|
|
AlwaysInstrumentFiles.push_back(Filename);
|
|
|
|
ExtraDeps.push_back(Filename);
|
|
|
|
} else
|
|
|
|
D.Diag(clang::diag::err_drv_no_such_file) << Filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Filename :
|
|
|
|
Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
|
|
|
|
if (llvm::sys::fs::exists(Filename)) {
|
|
|
|
NeverInstrumentFiles.push_back(Filename);
|
|
|
|
ExtraDeps.push_back(Filename);
|
|
|
|
} else
|
|
|
|
D.Diag(clang::diag::err_drv_no_such_file) << Filename;
|
|
|
|
}
|
2018-04-09 12:02:09 +08:00
|
|
|
|
|
|
|
for (const auto &Filename :
|
|
|
|
Args.getAllArgValues(options::OPT_fxray_attr_list)) {
|
|
|
|
if (llvm::sys::fs::exists(Filename)) {
|
|
|
|
AttrListFiles.push_back(Filename);
|
|
|
|
ExtraDeps.push_back(Filename);
|
|
|
|
} else
|
|
|
|
D.Diag(clang::diag::err_drv_no_such_file) << Filename;
|
|
|
|
}
|
2018-04-11 09:28:25 +08:00
|
|
|
|
|
|
|
// Get the list of modes we want to support.
|
|
|
|
auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
|
|
|
|
if (SpecifiedModes.empty())
|
|
|
|
llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
|
|
|
|
else
|
|
|
|
for (const auto &Arg : SpecifiedModes) {
|
|
|
|
// Parse CSV values for -fxray-modes=...
|
|
|
|
llvm::SmallVector<StringRef, 2> ModeParts;
|
|
|
|
llvm::SplitString(Arg, ModeParts, ",");
|
|
|
|
for (const auto &M : ModeParts)
|
2018-04-13 13:59:57 +08:00
|
|
|
if (M == "none")
|
|
|
|
Modes.clear();
|
|
|
|
else if (M == "all")
|
|
|
|
llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
|
|
|
|
else
|
|
|
|
Modes.push_back(M);
|
2018-04-11 09:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then we want to sort and unique the modes we've collected.
|
2018-04-23 08:49:25 +08:00
|
|
|
llvm::sort(Modes.begin(), Modes.end());
|
2018-04-11 09:28:25 +08:00
|
|
|
Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
|
2017-03-30 08:29:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
|
|
|
|
ArgStringList &CmdArgs, types::ID InputType) const {
|
|
|
|
if (!XRayInstrument)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CmdArgs.push_back(XRayInstrumentOption);
|
2017-11-30 08:04:54 +08:00
|
|
|
|
|
|
|
if (XRayAlwaysEmitCustomEvents)
|
|
|
|
CmdArgs.push_back("-fxray-always-emit-customevents");
|
|
|
|
|
2018-04-18 05:32:43 +08:00
|
|
|
if (XRayAlwaysEmitTypedEvents)
|
|
|
|
CmdArgs.push_back("-fxray-always-emit-typedevents");
|
|
|
|
|
2017-03-31 06:46:45 +08:00
|
|
|
CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
|
|
|
|
Twine(InstructionThreshold)));
|
2017-03-30 08:29:36 +08:00
|
|
|
|
|
|
|
for (const auto &Always : AlwaysInstrumentFiles) {
|
2017-11-30 08:04:54 +08:00
|
|
|
SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
|
2017-03-30 08:29:36 +08:00
|
|
|
AlwaysInstrumentOpt += Always;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Never : NeverInstrumentFiles) {
|
2017-11-30 08:04:54 +08:00
|
|
|
SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
|
2017-03-30 08:29:36 +08:00
|
|
|
NeverInstrumentOpt += Never;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
|
|
|
|
}
|
|
|
|
|
2018-04-13 10:31:58 +08:00
|
|
|
for (const auto &AttrFile : AttrListFiles) {
|
2018-04-09 12:02:09 +08:00
|
|
|
SmallString<64> AttrListFileOpt("-fxray-attr-list=");
|
|
|
|
AttrListFileOpt += AttrFile;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
|
|
|
|
}
|
|
|
|
|
2017-03-30 08:29:36 +08:00
|
|
|
for (const auto &Dep : ExtraDeps) {
|
|
|
|
SmallString<64> ExtraDepOpt("-fdepfile-entry=");
|
|
|
|
ExtraDepOpt += Dep;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
|
|
|
|
}
|
2018-04-13 13:59:57 +08:00
|
|
|
|
|
|
|
for (const auto &Mode : Modes) {
|
|
|
|
SmallString<64> ModeOpt("-fxray-modes=");
|
|
|
|
ModeOpt += Mode;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(ModeOpt));
|
|
|
|
}
|
2017-03-30 08:29:36 +08:00
|
|
|
}
|