|
| 1 | +//===- TracePluginTest.cpp -------------------------------------------===// |
| 2 | +// Part of the eld Project, under the BSD License |
| 3 | +// See https://github.com/qualcomm/eld/LICENSE.txt for license information. |
| 4 | +// SPDX-License-Identifier: BSD-3-Clause |
| 5 | +//===----------------------------------------------------------------------===// |
| 6 | +#include "eld/Config/GeneralOptions.h" |
| 7 | +#include "eld/Config/LinkerConfig.h" |
| 8 | +#include "eld/Core/LinkerScript.h" |
| 9 | +#include "eld/Core/Module.h" |
| 10 | +#include "eld/Diagnostics/DiagnosticEngine.h" |
| 11 | +#include "eld/PluginAPI/LinkerWrapper.h" |
| 12 | +#include "eld/Script/Plugin.h" |
| 13 | +#include "gtest/gtest.h" |
| 14 | + |
| 15 | +using namespace eld; |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +class TracePluginTest : public ::testing::Test { |
| 20 | +protected: |
| 21 | + void SetUp() override { |
| 22 | + DiagEngine = make<DiagnosticEngine>(/*useColor=*/false); |
| 23 | + Config = make<LinkerConfig>(DiagEngine); |
| 24 | + Script = make<LinkerScript>(DiagEngine); |
| 25 | + Mod = make<Module>(*Script, *Config, /*layoutInfo=*/nullptr); |
| 26 | + } |
| 27 | + |
| 28 | + // Constructs a Plugin named `PluginName` along with a standalone |
| 29 | + // LinkerWrapper wrapping it. We construct the LinkerWrapper directly |
| 30 | + // (rather than via Plugin::getLinkerWrapper(), which requires a real |
| 31 | + // dynamically-loaded plugin library to have registered itself first) |
| 32 | + // since LinkerWrapper::isTraced() only needs the Plugin* it was built |
| 33 | + // with. |
| 34 | + eld::Plugin *makePlugin(const std::string &PluginName, |
| 35 | + plugin::LinkerWrapper **LWOut = nullptr) { |
| 36 | + auto *P = make<eld::Plugin>(plugin::PluginBase::Type::OutputSectionIterator, |
| 37 | + /*Name=*/PluginName, /*R=*/PluginName, |
| 38 | + /*O=*/"", /*Stats=*/false, |
| 39 | + /*DefaultPlugin=*/false, *Mod); |
| 40 | + auto *LW = make<plugin::LinkerWrapper>(P, *Mod); |
| 41 | + if (LWOut) |
| 42 | + *LWOut = LW; |
| 43 | + return P; |
| 44 | + } |
| 45 | + |
| 46 | + DiagnosticEngine *DiagEngine = nullptr; |
| 47 | + LinkerConfig *Config = nullptr; |
| 48 | + LinkerScript *Script = nullptr; |
| 49 | + Module *Mod = nullptr; |
| 50 | +}; |
| 51 | + |
| 52 | +TEST_F(TracePluginTest, NotTracedByDefault) { |
| 53 | + plugin::LinkerWrapper *LW = nullptr; |
| 54 | + eld::Plugin *P = makePlugin("MyPlugin", &LW); |
| 55 | + EXPECT_FALSE(P->isTraced()); |
| 56 | + EXPECT_FALSE(LW->isTraced()); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(TracePluginTest, UnscopedTraceTracesEveryPlugin) { |
| 60 | + ASSERT_TRUE((bool)Config->options().setTrace("plugin")); |
| 61 | + |
| 62 | + plugin::LinkerWrapper *LW1 = nullptr, *LW2 = nullptr; |
| 63 | + eld::Plugin *P1 = makePlugin("PluginOne", &LW1); |
| 64 | + eld::Plugin *P2 = makePlugin("PluginTwo", &LW2); |
| 65 | + EXPECT_TRUE(P1->isTraced()); |
| 66 | + EXPECT_TRUE(P2->isTraced()); |
| 67 | + EXPECT_TRUE(LW1->isTraced()); |
| 68 | + EXPECT_TRUE(LW2->isTraced()); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(TracePluginTest, ScopedTraceOnlyMatchesNamedPlugin) { |
| 72 | + ASSERT_TRUE((bool)Config->options().setTrace("plugin=MyPlugin")); |
| 73 | + |
| 74 | + plugin::LinkerWrapper *MatchingLW = nullptr, *NonMatchingLW = nullptr; |
| 75 | + eld::Plugin *Matching = makePlugin("MyPlugin", &MatchingLW); |
| 76 | + eld::Plugin *NonMatching = makePlugin("OtherPlugin", &NonMatchingLW); |
| 77 | + EXPECT_TRUE(Matching->isTraced()); |
| 78 | + EXPECT_TRUE(MatchingLW->isTraced()); |
| 79 | + EXPECT_FALSE(NonMatching->isTraced()); |
| 80 | + EXPECT_FALSE(NonMatchingLW->isTraced()); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(TracePluginTest, ScopedTraceNameIsARegex) { |
| 84 | + ASSERT_TRUE((bool)Config->options().setTrace("plugin=My.*")); |
| 85 | + |
| 86 | + eld::Plugin *Matching = makePlugin("MyPlugin"); |
| 87 | + eld::Plugin *NonMatching = makePlugin("OtherPlugin"); |
| 88 | + EXPECT_TRUE(Matching->isTraced()); |
| 89 | + EXPECT_FALSE(NonMatching->isTraced()); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_F(TracePluginTest, MultipleScopedTracesAreCumulative) { |
| 93 | + ASSERT_TRUE((bool)Config->options().setTrace("plugin=PluginOne")); |
| 94 | + ASSERT_TRUE((bool)Config->options().setTrace("plugin=PluginTwo")); |
| 95 | + |
| 96 | + eld::Plugin *P1 = makePlugin("PluginOne"); |
| 97 | + eld::Plugin *P2 = makePlugin("PluginTwo"); |
| 98 | + eld::Plugin *P3 = makePlugin("PluginThree"); |
| 99 | + EXPECT_TRUE(P1->isTraced()); |
| 100 | + EXPECT_TRUE(P2->isTraced()); |
| 101 | + EXPECT_FALSE(P3->isTraced()); |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace |
0 commit comments