Fangrui Song | 524b3c1 | 2019-03-01 06:49:51 +0000 | [diff] [blame] | 1 | //===-- XRayLists.cpp - XRay automatic-attribution ------------------------===// |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // User-provided filters for always/never XRay instrumenting certain functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Reid Kleckner | 04da3df | 2020-02-26 17:24:52 -0800 | [diff] [blame] | 12 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 13 | #include "clang/Basic/XRayLists.h" |
Reid Kleckner | e08464f | 2020-02-29 09:10:42 -0800 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
Reid Kleckner | 04da3df | 2020-02-26 17:24:52 -0800 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "llvm/Support/SpecialCaseList.h" |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | XRayFunctionFilter::XRayFunctionFilter( |
| 21 | ArrayRef<std::string> AlwaysInstrumentPaths, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 22 | ArrayRef<std::string> NeverInstrumentPaths, |
| 23 | ArrayRef<std::string> AttrListPaths, SourceManager &SM) |
Ilya Biryukov | aa981c1 | 2019-11-21 11:32:17 +0100 | [diff] [blame] | 24 | : AlwaysInstrument(llvm::SpecialCaseList::createOrDie( |
| 25 | AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())), |
| 26 | NeverInstrument(llvm::SpecialCaseList::createOrDie( |
| 27 | NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())), |
| 28 | AttrList(llvm::SpecialCaseList::createOrDie( |
| 29 | AttrListPaths, SM.getFileManager().getVirtualFileSystem())), |
| 30 | SM(SM) {} |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 31 | |
Reid Kleckner | 04da3df | 2020-02-26 17:24:52 -0800 | [diff] [blame] | 32 | XRayFunctionFilter::~XRayFunctionFilter() = default; |
| 33 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 34 | XRayFunctionFilter::ImbueAttribute |
| 35 | XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const { |
| 36 | // First apply the always instrument list, than if it isn't an "always" see |
| 37 | // whether it's treated as a "never" instrument function. |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 38 | // TODO: Remove these as they're deprecated; use the AttrList exclusively. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 39 | if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 40 | "arg1") || |
| 41 | AttrList->inSection("always", "fun", FunctionName, "arg1")) |
Dean Michael Berris | 170429e | 2017-05-24 05:46:36 +0000 | [diff] [blame] | 42 | return ImbueAttribute::ALWAYS_ARG1; |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 43 | if (AlwaysInstrument->inSection("xray_always_instrument", "fun", |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 44 | FunctionName) || |
| 45 | AttrList->inSection("always", "fun", FunctionName)) |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 46 | return ImbueAttribute::ALWAYS; |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 47 | |
| 48 | if (NeverInstrument->inSection("xray_never_instrument", "fun", |
| 49 | FunctionName) || |
| 50 | AttrList->inSection("never", "fun", FunctionName)) |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 51 | return ImbueAttribute::NEVER; |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 52 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 53 | return ImbueAttribute::NONE; |
| 54 | } |
| 55 | |
| 56 | XRayFunctionFilter::ImbueAttribute |
| 57 | XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename, |
| 58 | StringRef Category) const { |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 59 | if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 60 | Category) || |
| 61 | AttrList->inSection("always", "src", Filename, Category)) |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 62 | return ImbueAttribute::ALWAYS; |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 63 | if (NeverInstrument->inSection("xray_never_instrument", "src", Filename, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 64 | Category) || |
| 65 | AttrList->inSection("never", "src", Filename, Category)) |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 66 | return ImbueAttribute::NEVER; |
| 67 | return ImbueAttribute::NONE; |
| 68 | } |
| 69 | |
| 70 | XRayFunctionFilter::ImbueAttribute |
| 71 | XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc, |
| 72 | StringRef Category) const { |
| 73 | if (!Loc.isValid()) |
| 74 | return ImbueAttribute::NONE; |
| 75 | return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)), |
| 76 | Category); |
| 77 | } |