blob: 19bf9b7d22c9e127e8300c747935f0de7865d10d [file] [log] [blame]
Dean Michael Berris835832d2017-03-30 00:29:36 +00001//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
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#include "clang/Driver/XRayArgs.h"
10#include "ToolChains/CommonArgs.h"
11#include "clang/Driver/Driver.h"
12#include "clang/Driver/DriverDiagnostic.h"
13#include "clang/Driver/Options.h"
14#include "clang/Driver/ToolChain.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/StringSwitch.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
Dean Michael Berris00ad3d52017-03-30 01:05:09 +000019#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berris504fc222017-03-30 22:46:45 +000020#include "llvm/Support/SpecialCaseList.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000021
22using namespace clang;
23using namespace clang::driver;
24using namespace llvm::opt;
25
26namespace {
27constexpr char XRayInstrumentOption[] = "-fxray-instrument";
28constexpr char XRayInstructionThresholdOption[] =
29 "-fxray-instruction-threshold=";
Dean Michael Berris835832d2017-03-30 00:29:36 +000030} // namespace
31
32XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
33 const Driver &D = TC.getDriver();
34 const llvm::Triple &Triple = TC.getTriple();
35 if (Args.hasFlag(options::OPT_fxray_instrument,
36 options::OPT_fnoxray_instrument, false)) {
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000037 if (Triple.getOS() == llvm::Triple::Linux) {
Dean Michael Berris835832d2017-03-30 00:29:36 +000038 switch (Triple.getArch()) {
39 case llvm::Triple::x86_64:
40 case llvm::Triple::arm:
41 case llvm::Triple::aarch64:
42 case llvm::Triple::ppc64le:
43 case llvm::Triple::mips:
44 case llvm::Triple::mipsel:
45 case llvm::Triple::mips64:
46 case llvm::Triple::mips64el:
47 break;
48 default:
49 D.Diag(diag::err_drv_clang_unsupported)
50 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
51 }
Dean Michael Berris9b592332018-04-04 12:47:49 +000052 } else if (Triple.getOS() == llvm::Triple::FreeBSD ||
53 Triple.getOS() == llvm::Triple::OpenBSD) {
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000054 if (Triple.getArch() != llvm::Triple::x86_64) {
55 D.Diag(diag::err_drv_clang_unsupported)
56 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
57 }
58 } else {
Dean Michael Berris835832d2017-03-30 00:29:36 +000059 D.Diag(diag::err_drv_clang_unsupported)
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000060 << (std::string(XRayInstrumentOption) + " on non-supported target OS");
61 }
Dean Michael Berris835832d2017-03-30 00:29:36 +000062 XRayInstrument = true;
63 if (const Arg *A =
64 Args.getLastArg(options::OPT_fxray_instruction_threshold_,
65 options::OPT_fxray_instruction_threshold_EQ)) {
66 StringRef S = A->getValue();
67 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
68 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
69 }
70
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +000071 // By default, the back-end will not emit the lowering for XRay customevent
72 // calls if the function is not instrumented. In the future we will change
73 // this default to be the reverse, but in the meantime we're going to
74 // introduce the new functionality behind a flag.
75 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
76 options::OPT_fnoxray_always_emit_customevents, false))
77 XRayAlwaysEmitCustomEvents = true;
78
Dean Michael Berris835832d2017-03-30 00:29:36 +000079 // Validate the always/never attribute files. We also make sure that they
80 // are treated as actual dependencies.
81 for (const auto &Filename :
82 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
83 if (llvm::sys::fs::exists(Filename)) {
84 AlwaysInstrumentFiles.push_back(Filename);
85 ExtraDeps.push_back(Filename);
86 } else
87 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
88 }
89
90 for (const auto &Filename :
91 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
92 if (llvm::sys::fs::exists(Filename)) {
93 NeverInstrumentFiles.push_back(Filename);
94 ExtraDeps.push_back(Filename);
95 } else
96 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
97 }
98 }
99}
100
101void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
102 ArgStringList &CmdArgs, types::ID InputType) const {
103 if (!XRayInstrument)
104 return;
105
106 CmdArgs.push_back(XRayInstrumentOption);
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000107
108 if (XRayAlwaysEmitCustomEvents)
109 CmdArgs.push_back("-fxray-always-emit-customevents");
110
Dean Michael Berris504fc222017-03-30 22:46:45 +0000111 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
112 Twine(InstructionThreshold)));
Dean Michael Berris835832d2017-03-30 00:29:36 +0000113
114 for (const auto &Always : AlwaysInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000115 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000116 AlwaysInstrumentOpt += Always;
117 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
118 }
119
120 for (const auto &Never : NeverInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000121 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000122 NeverInstrumentOpt += Never;
123 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
124 }
125
126 for (const auto &Dep : ExtraDeps) {
127 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
128 ExtraDepOpt += Dep;
129 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
130 }
131}