Path: blob/main/contrib/llvm-project/clang/lib/Driver/XRayArgs.cpp
35233 views
//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7#include "clang/Driver/XRayArgs.h"8#include "ToolChains/CommonArgs.h"9#include "clang/Driver/Driver.h"10#include "clang/Driver/DriverDiagnostic.h"11#include "clang/Driver/Options.h"12#include "clang/Driver/ToolChain.h"13#include "llvm/ADT/StringExtras.h"14#include "llvm/ADT/StringSwitch.h"15#include "llvm/Support/Path.h"16#include "llvm/Support/ScopedPrinter.h"17#include "llvm/Support/SpecialCaseList.h"18#include "llvm/Support/VirtualFileSystem.h"1920using namespace clang;21using namespace clang::driver;22using namespace llvm::opt;2324constexpr const char *XRaySupportedModes[] = {"xray-fdr", "xray-basic"};2526XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {27const Driver &D = TC.getDriver();28const llvm::Triple &Triple = TC.getTriple();29if (!Args.hasFlag(options::OPT_fxray_instrument,30options::OPT_fno_xray_instrument, false))31return;32XRayInstrument = Args.getLastArg(options::OPT_fxray_instrument);33if (Triple.isMacOSX()) {34switch (Triple.getArch()) {35case llvm::Triple::aarch64:36case llvm::Triple::x86_64:37break;38default:39D.Diag(diag::err_drv_unsupported_opt_for_target)40<< XRayInstrument->getSpelling() << Triple.str();41break;42}43} else if (Triple.isOSBinFormatELF()) {44switch (Triple.getArch()) {45case llvm::Triple::x86_64:46case llvm::Triple::arm:47case llvm::Triple::aarch64:48case llvm::Triple::hexagon:49case llvm::Triple::ppc64le:50case llvm::Triple::loongarch64:51case llvm::Triple::mips:52case llvm::Triple::mipsel:53case llvm::Triple::mips64:54case llvm::Triple::mips64el:55break;56default:57D.Diag(diag::err_drv_unsupported_opt_for_target)58<< XRayInstrument->getSpelling() << Triple.str();59}60} else {61D.Diag(diag::err_drv_unsupported_opt_for_target)62<< XRayInstrument->getSpelling() << Triple.str();63}6465// Both XRay and -fpatchable-function-entry use66// TargetOpcode::PATCHABLE_FUNCTION_ENTER.67if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ))68D.Diag(diag::err_drv_argument_not_allowed_with)69<< XRayInstrument->getSpelling() << A->getSpelling();7071if (!Args.hasFlag(options::OPT_fxray_link_deps,72options::OPT_fno_xray_link_deps, true))73XRayRT = false;7475auto Bundles =76Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);77if (Bundles.empty())78InstrumentationBundle.Mask = XRayInstrKind::All;79else80for (const auto &B : Bundles) {81llvm::SmallVector<StringRef, 2> BundleParts;82llvm::SplitString(B, BundleParts, ",");83for (const auto &P : BundleParts) {84// TODO: Automate the generation of the string case table.85auto Valid = llvm::StringSwitch<bool>(P)86.Cases("none", "all", "function", "function-entry",87"function-exit", "custom", true)88.Default(false);8990if (!Valid) {91D.Diag(clang::diag::err_drv_invalid_value)92<< "-fxray-instrumentation-bundle=" << P;93continue;94}9596auto Mask = parseXRayInstrValue(P);97if (Mask == XRayInstrKind::None) {98InstrumentationBundle.clear();99break;100}101102InstrumentationBundle.Mask |= Mask;103}104}105106// Validate the always/never attribute files. We also make sure that they107// are treated as actual dependencies.108for (const auto &Filename :109Args.getAllArgValues(options::OPT_fxray_always_instrument)) {110if (D.getVFS().exists(Filename)) {111AlwaysInstrumentFiles.push_back(Filename);112ExtraDeps.push_back(Filename);113} else114D.Diag(clang::diag::err_drv_no_such_file) << Filename;115}116117for (const auto &Filename :118Args.getAllArgValues(options::OPT_fxray_never_instrument)) {119if (D.getVFS().exists(Filename)) {120NeverInstrumentFiles.push_back(Filename);121ExtraDeps.push_back(Filename);122} else123D.Diag(clang::diag::err_drv_no_such_file) << Filename;124}125126for (const auto &Filename :127Args.getAllArgValues(options::OPT_fxray_attr_list)) {128if (D.getVFS().exists(Filename)) {129AttrListFiles.push_back(Filename);130ExtraDeps.push_back(Filename);131} else132D.Diag(clang::diag::err_drv_no_such_file) << Filename;133}134135// Get the list of modes we want to support.136auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);137if (SpecifiedModes.empty())138llvm::copy(XRaySupportedModes, std::back_inserter(Modes));139else140for (const auto &Arg : SpecifiedModes) {141// Parse CSV values for -fxray-modes=...142llvm::SmallVector<StringRef, 2> ModeParts;143llvm::SplitString(Arg, ModeParts, ",");144for (const auto &M : ModeParts)145if (M == "none")146Modes.clear();147else if (M == "all")148llvm::copy(XRaySupportedModes, std::back_inserter(Modes));149else150Modes.push_back(std::string(M));151}152153// Then we want to sort and unique the modes we've collected.154llvm::sort(Modes);155Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());156}157158void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,159ArgStringList &CmdArgs, types::ID InputType) const {160if (!XRayInstrument)161return;162const Driver &D = TC.getDriver();163XRayInstrument->render(Args, CmdArgs);164165// By default, the back-end will not emit the lowering for XRay customevent166// calls if the function is not instrumented. In the future we will change167// this default to be the reverse, but in the meantime we're going to168// introduce the new functionality behind a flag.169Args.addOptInFlag(CmdArgs, options::OPT_fxray_always_emit_customevents,170options::OPT_fno_xray_always_emit_customevents);171172Args.addOptInFlag(CmdArgs, options::OPT_fxray_always_emit_typedevents,173options::OPT_fno_xray_always_emit_typedevents);174Args.addOptInFlag(CmdArgs, options::OPT_fxray_ignore_loops,175options::OPT_fno_xray_ignore_loops);176Args.addOptOutFlag(CmdArgs, options::OPT_fxray_function_index,177options::OPT_fno_xray_function_index);178179if (const Arg *A =180Args.getLastArg(options::OPT_fxray_instruction_threshold_EQ)) {181int Value;182StringRef S = A->getValue();183if (S.getAsInteger(0, Value) || Value < 0)184D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;185else186A->render(Args, CmdArgs);187}188189int XRayFunctionGroups = 1;190int XRaySelectedFunctionGroup = 0;191if (const Arg *A = Args.getLastArg(options::OPT_fxray_function_groups)) {192StringRef S = A->getValue();193if (S.getAsInteger(0, XRayFunctionGroups) || XRayFunctionGroups < 1)194D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;195if (XRayFunctionGroups > 1)196A->render(Args, CmdArgs);197}198if (const Arg *A =199Args.getLastArg(options::OPT_fxray_selected_function_group)) {200StringRef S = A->getValue();201if (S.getAsInteger(0, XRaySelectedFunctionGroup) ||202XRaySelectedFunctionGroup < 0 ||203XRaySelectedFunctionGroup >= XRayFunctionGroups)204D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;205if (XRaySelectedFunctionGroup != 0)206A->render(Args, CmdArgs);207}208209for (const auto &Always : AlwaysInstrumentFiles) {210SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");211AlwaysInstrumentOpt += Always;212CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));213}214215for (const auto &Never : NeverInstrumentFiles) {216SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");217NeverInstrumentOpt += Never;218CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));219}220221for (const auto &AttrFile : AttrListFiles) {222SmallString<64> AttrListFileOpt("-fxray-attr-list=");223AttrListFileOpt += AttrFile;224CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));225}226227for (const auto &Dep : ExtraDeps) {228SmallString<64> ExtraDepOpt("-fdepfile-entry=");229ExtraDepOpt += Dep;230CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));231}232233for (const auto &Mode : Modes) {234SmallString<64> ModeOpt("-fxray-modes=");235ModeOpt += Mode;236CmdArgs.push_back(Args.MakeArgString(ModeOpt));237}238239SmallString<64> Bundle("-fxray-instrumentation-bundle=");240if (InstrumentationBundle.full()) {241Bundle += "all";242} else if (InstrumentationBundle.empty()) {243Bundle += "none";244} else {245if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry) &&246InstrumentationBundle.has(XRayInstrKind::FunctionExit))247Bundle += "function";248else if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry))249Bundle += "function-entry";250else if (InstrumentationBundle.has(XRayInstrKind::FunctionExit))251Bundle += "function-exit";252253if (InstrumentationBundle.has(XRayInstrKind::Custom))254Bundle += "custom";255if (InstrumentationBundle.has(XRayInstrKind::Typed))256Bundle += "typed";257}258CmdArgs.push_back(Args.MakeArgString(Bundle));259}260261262