Skip to content

Define Telemetry plugin for LLDB. #126588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c773401
Define TelemetryVendor plugin.
oontvoo Feb 10, 2025
5f6a04d
use shared ptr
oontvoo Feb 11, 2025
b033513
use header guard
oontvoo Feb 11, 2025
ac87aaa
Merge branch 'llvm:main' into telemetry_plugin
oontvoo Feb 11, 2025
58e1d0f
fix cmake
oontvoo Feb 11, 2025
b767a2e
add header incl
oontvoo Feb 11, 2025
5287bac
Merge branch 'main' into telemetry_plugin
oontvoo Feb 12, 2025
bfc954b
review comment
oontvoo Feb 12, 2025
b3c50f1
addressed review commeht
oontvoo Feb 12, 2025
0a43bd9
remove getpluginname
oontvoo Feb 12, 2025
5d80f30
simplified init/terminate
oontvoo Feb 12, 2025
57f8e86
fix namespace
oontvoo Feb 12, 2025
5f14bc5
remove TelemetryVendor
oontvoo Feb 12, 2025
d19a9d4
add unittest
oontvoo Feb 12, 2025
7ac7268
fixup cmake file
oontvoo Feb 12, 2025
2e859c1
reorg dir
oontvoo Feb 12, 2025
2a3143d
fix test
oontvoo Feb 12, 2025
be5670f
rename test for using with gunit filter
oontvoo Feb 12, 2025
e7f425f
call Init directly
oontvoo Feb 12, 2025
7d4d6ba
Merge branch 'main' into telemetry_plugin
oontvoo Feb 12, 2025
17338f1
fixed test
oontvoo Feb 13, 2025
4475e98
Merge branch 'telemetry_plugin' of github.com:oontvoo/llvm-project in…
oontvoo Feb 13, 2025
c3df4c8
Update lldb/unittests/Core/TelemetryTest.cpp
oontvoo Feb 13, 2025
189af04
Update lldb/unittests/Core/TelemetryTest.cpp
oontvoo Feb 13, 2025
8b84a5e
Update lldb/unittests/Core/TelemetryTest.cpp
oontvoo Feb 13, 2025
96d02bf
Update lldb/unittests/Core/TelemetryTest.cpp
oontvoo Feb 13, 2025
7405add
added include
oontvoo Feb 13, 2025
cf180bb
add Destination test
oontvoo Feb 13, 2025
9a361d1
reformat code
oontvoo Feb 13, 2025
b05f7d4
remove inhertiance from PluginInterface
oontvoo Feb 18, 2025
ef78de7
format
oontvoo Feb 18, 2025
19e888e
Merge branch 'main' into telemetry_plugin
oontvoo Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lldb/include/lldb/Core/Telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ struct LLDBBaseTelemetryInfo : public llvm::telemetry::TelemetryInfo {
/// applicable to LLDB.
class TelemetryManager : public llvm::telemetry::Manager {
public:
llvm::Error preDispatch(llvm::telemetry::TelemetryInfo *entry) override;

virtual llvm::StringRef GetInstanceName() const = 0;
static TelemetryManager *getInstance();

protected:
TelemetryManager(std::unique_ptr<llvm::telemetry::Config> config);

llvm::Error preDispatch(llvm::telemetry::TelemetryInfo *entry) override;
static void setInstance(std::unique_ptr<TelemetryManager> manger);

private:
std::unique_ptr<llvm::telemetry::Config> m_config;
static std::unique_ptr<TelemetryManager> g_instance;
};

} // namespace telemetry
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Core/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) {
return Error::success();
}

std::unique_ptr<TelemetryManager> TelemetryManager::g_instance = nullptr;
TelemetryManager *TelemetryManager::getInstance() { return g_instance.get(); }

void TelemetryManager::setInstance(std::unique_ptr<TelemetryManager> manager) {
g_instance = std::move(manager);
}

} // namespace telemetry
} // namespace lldb_private

Expand Down
6 changes: 6 additions & 0 deletions lldb/unittests/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (LLVM_BUILD_TELEMETRY)
set(TELEMETRY_DEPS Telemetry)
endif()

add_lldb_unittest(LLDBCoreTests
CommunicationTest.cpp
DiagnosticEventTest.cpp
Expand All @@ -10,6 +14,7 @@ add_lldb_unittest(LLDBCoreTests
RichManglingContextTest.cpp
SourceLocationSpecTest.cpp
SourceManagerTest.cpp
TelemetryTest.cpp
UniqueCStringMapTest.cpp

LINK_LIBS
Expand All @@ -26,4 +31,5 @@ add_lldb_unittest(LLDBCoreTests
LLVMTestingSupport
LINK_COMPONENTS
Support
${TELEMETRY_DEPS}
)
98 changes: 98 additions & 0 deletions lldb/unittests/Core/TelemetryTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//===-- TelemetryTest.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Config/llvm-config.h"

#ifdef LLVM_BUILD_TELEMETRY

#include "lldb/Core/PluginInterface.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Telemetry.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Telemetry/Telemetry.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <memory>
#include <vector>

namespace lldb_private {

struct FakeTelemetryInfo : public llvm::telemetry::TelemetryInfo {
std::string msg;
};

class TestDestination : public llvm::telemetry::Destination {
public:
TestDestination(std::vector<const llvm::telemetry::TelemetryInfo *> *entries)
: received_entries(entries) {}

llvm::Error
receiveEntry(const llvm::telemetry::TelemetryInfo *entry) override {
received_entries->push_back(entry);
return llvm::Error::success();
}

llvm::StringLiteral name() const override { return "TestDestination"; }

private:
std::vector<const llvm::telemetry::TelemetryInfo *> *received_entries;
};

class FakePlugin : public telemetry::TelemetryManager {
public:
FakePlugin()
: telemetry::TelemetryManager(
std::make_unique<llvm::telemetry::Config>(true)) {}

// TelemetryManager interface
llvm::Error preDispatch(llvm::telemetry::TelemetryInfo *entry) override {
if (auto *fake_entry = llvm::dyn_cast<FakeTelemetryInfo>(entry))
fake_entry->msg = "In FakePlugin";

return llvm::Error::success();
}

llvm::StringRef GetInstanceName() const override {
return "FakeTelemetryPlugin";
}

static void Initialize() {
telemetry::TelemetryManager::setInstance(std::make_unique<FakePlugin>());
}

static void Terminate() { telemetry::TelemetryManager::setInstance(nullptr); }
};

} // namespace lldb_private

TEST(TelemetryTest, PluginTest) {
// This would have been called by the plugin reg in a "real" plugin
// For tests, we just call it directly.
lldb_private::FakePlugin::Initialize();

auto *ins = lldb_private::telemetry::TelemetryManager::getInstance();
ASSERT_NE(ins, nullptr);

std::vector<const ::llvm::telemetry::TelemetryInfo *> expected_entries;
ins->addDestination(
std::make_unique<lldb_private::TestDestination>(&expected_entries));

lldb_private::FakeTelemetryInfo entry;
entry.msg = "";

ASSERT_THAT_ERROR(ins->dispatch(&entry), ::llvm::Succeeded());
ASSERT_EQ(1, expected_entries.size());
EXPECT_EQ("In FakePlugin",
llvm::dyn_cast<lldb_private::FakeTelemetryInfo>(expected_entries[0])
->msg);

ASSERT_EQ("FakeTelemetryPlugin", ins->GetInstanceName());
}

#endif // LLVM_BUILD_TELEMETRY
Loading