Skip to content

Commit

Permalink
Add debug logging for missing traces
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Mayer authored and cakeless committed Oct 11, 2023
1 parent 713814b commit 99354b1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
10 changes: 9 additions & 1 deletion plugins/apitracing/src/lib/ApiTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ namespace ApiTracing

logger->debug("ApiTracing plugin version info", {{"Version", PLUGIN_VERSION}, {"BuildNumber", BUILD_VERSION}});

auto apiTracingConfig = std::make_unique<Config>(config);
auto apiTracingConfig = std::make_unique<Config>(pluginInterface, config);

std::string commandLine;
for (const auto& piece : args)
{
commandLine += " " + piece;
}
logger->info("ApiTracing command line", {{"commandLine:", commandLine}});

if (functionDefinitionsPath.isSet())
{
apiTracingConfig->setFunctionDefinitionsPath(functionDefinitionsPath);
}

if (traceProcessName.isSet())
{
apiTracingConfig->addTracingTarget(traceProcessName);
Expand Down
11 changes: 10 additions & 1 deletion plugins/apitracing/src/lib/config/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "Config.h"
#include "../Filenames.h"
#include "TracingDefinitions.h"
#include <vmicore/plugins/IPluginConfig.h>
#include <vmicore/plugins/PluginInterface.h>

namespace ApiTracing
{
Config::Config(const VmiCore::Plugin::IPluginConfig& pluginConfig)
Config::Config(const VmiCore::Plugin::PluginInterface* pluginInterface,
const VmiCore::Plugin::IPluginConfig& pluginConfig)
: logger(pluginInterface->newNamedLogger(APITRACING_LOGGER_NAME))
{
auto configRootNode = pluginConfig.rootNode();
if (auto configFilePath = pluginConfig.configFilePath())
Expand Down Expand Up @@ -96,7 +100,12 @@ namespace ApiTracing

void Config::addTracingTarget(const std::string& name)
{
logger->debug("addTracingTarget", {{"Name", name}});
processTracingProfiles.emplace(name, profiles["default"]);
for (auto [processName, profile] : processTracingProfiles)
{
logger->debug("tracing profiles", {{"ProcessName", processName}, {"TracingProfile", profile.name}});
}
}

void Config::setFunctionDefinitionsPath(const std::filesystem::path& path)
Expand Down
6 changes: 5 additions & 1 deletion plugins/apitracing/src/lib/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <map>
#include <optional>
#include <string_view>
#include <vmicore/io/ILogger.h>
#include <vmicore/plugins/IPluginConfig.h>
#include <vmicore/plugins/PluginInterface.h>
#include <yaml-cpp/yaml.h>

namespace ApiTracing
Expand All @@ -31,7 +33,8 @@ namespace ApiTracing
class Config : public IConfig
{
public:
explicit Config(const VmiCore::Plugin::IPluginConfig& pluginConfig);
explicit Config(const VmiCore::Plugin::PluginInterface* pluginInterface,
const VmiCore::Plugin::IPluginConfig& pluginConfig);

~Config() override = default;

Expand All @@ -44,6 +47,7 @@ namespace ApiTracing
void setFunctionDefinitionsPath(const std::filesystem::path& path) override;

private:
std::unique_ptr<VmiCore::ILogger> logger;
std::filesystem::path configFileDir;
std::filesystem::path functionDefinitions;
std::map<std::string, TracingProfile, std::less<>> profiles;
Expand Down
46 changes: 33 additions & 13 deletions plugins/apitracing/test/Config_UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string_view>
#include <vmicore_test/io/mock_Logger.h>
#include <vmicore_test/plugins/mock_PluginConfig.h>
#include <vmicore_test/plugins/mock_PluginInterface.h>

using testing::_; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
using testing::NiceMock;
using testing::Return;
using VmiCore::Plugin::MockPluginConfig;
using VmiCore::Plugin::MockPluginInterface;

namespace ApiTracing
{
constexpr std::string_view defaultMainConfigFileDir = "/usr/local/var/";

class ConfigTestFixture : public testing::Test
{
protected:
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();

void SetUp() override
{
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });
}
};

std::unique_ptr<MockPluginConfig> createMockPluginConfig(const YAML::Node& configNode)
{
auto mockPluginConfig = std::make_unique<MockPluginConfig>();
Expand All @@ -32,17 +49,17 @@ namespace ApiTracing
return mockPluginConfig;
}

TEST(ConfigTests, constructor_functionDefinitionsKeyMissing_throws)
TEST_F(ConfigTestFixture, constructor_functionDefinitionsKeyMissing_throws)
{
YAML::Node emptyConfigRootNode;

EXPECT_ANY_THROW(std::make_unique<Config>(*createMockPluginConfig(emptyConfigRootNode)));
EXPECT_ANY_THROW(std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig(emptyConfigRootNode)));
}

TEST(ConfigTests, getTracingProfile_calcProcess_correctProfile)
TEST_F(ConfigTestFixture, getTracingProfile_calcProcess_correctProfile)
{

auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));
TracingProfile expectedTracingProfile{
.name = "calc",
.traceChildren = true,
Expand All @@ -54,9 +71,10 @@ namespace ApiTracing
EXPECT_EQ(tracingProfile, expectedTracingProfile);
}

TEST(ConfigTests, getTracingProfile_processWithoutProfile_defaultProfile)
TEST_F(ConfigTestFixture, getTracingProfile_processWithoutProfile_defaultProfile)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));
TracingProfile expectedTracingProfile{
.name = "default", .traceChildren = true, .modules = {{.name = "ntdll.dll", .functions = {{"function1"}}}}};

Expand All @@ -65,28 +83,30 @@ namespace ApiTracing
EXPECT_EQ(tracingProfile, expectedTracingProfile);
}

TEST(ConfigTests, getTracingProfile_unknownProcessName_nullopt)
TEST_F(ConfigTestFixture, getTracingProfile_unknownProcessName_nullopt)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));

auto tracingProfile = config->getTracingProfile("unknown.exe");

EXPECT_FALSE(tracingProfile);
}

TEST(ConfigTests, getFunctionDefinitionsPath_configFromVmiCoreConfigFile_correctAbsolutePath)
TEST_F(ConfigTestFixture, getFunctionDefinitionsPath_configFromVmiCoreConfigFile_correctAbsolutePath)
{
YAML::Node configRootNode;
configRootNode["function_definitions"] = "test.yaml";

auto config = std::make_unique<Config>(*createMockPluginConfig(configRootNode));
auto config = std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig(configRootNode));

EXPECT_EQ(config->getFunctionDefinitionsPath(), std::filesystem::path(defaultMainConfigFileDir) / "test.yaml");
}

TEST(ConfigTests, getFunctionDefinitionsPath_configWithFunctionDefinitionsKey_correctFilePath)
TEST_F(ConfigTestFixture, getFunctionDefinitionsPath_configWithFunctionDefinitionsKey_correctFilePath)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));

EXPECT_NO_THROW(YAML::LoadFile(config->getFunctionDefinitionsPath()));
}
Expand Down
5 changes: 0 additions & 5 deletions plugins/apitracing/test/FunctionHook_UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ namespace ApiTracing
std::shared_ptr<MockIntrospectionAPI> introspectionAPI = std::make_shared<NiceMock<MockIntrospectionAPI>>();
std::shared_ptr<MockExtractor> extractor = std::make_shared<NiceMock<MockExtractor>>();
std::shared_ptr<MockInterruptEvent> interruptEvent = std::make_shared<NiceMock<MockInterruptEvent>>();
std::vector<ExtractedParameterInformation> extractedParameterInformation{
{.name = "TestInt0", .data = 0},
{.name = "TestInt1",
.data = 3,
.backingParameters = {{.name = "TestInt2", .data = 4}, {.name = "TestString1", .data = "A"}}}};

void SetUp() override
{
Expand Down

0 comments on commit 99354b1

Please sign in to comment.