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 Manuel Bischof committed Oct 4, 2023
1 parent 7779265 commit bc221ce
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 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(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 profile : processTracingProfiles)
{
logger->debug("tracing profiles", {{"Name", profile.first}, {"Profile", profile.second.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(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
41 changes: 34 additions & 7 deletions plugins/apitracing/test/Config_UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#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
{
Expand Down Expand Up @@ -35,14 +40,20 @@ namespace ApiTracing
TEST(ConfigTests, constructor_functionDefinitionsKeyMissing_throws)
{
YAML::Node emptyConfigRootNode;
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });

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)
{

auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));
TracingProfile expectedTracingProfile{
.name = "calc",
.traceChildren = true,
Expand All @@ -56,7 +67,11 @@ namespace ApiTracing

TEST(ConfigTests, getTracingProfile_processWithoutProfile_defaultProfile)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });
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 @@ -67,7 +82,11 @@ namespace ApiTracing

TEST(ConfigTests, getTracingProfile_unknownProcessName_nullopt)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });
auto config =
std::make_unique<Config>(pluginInterface.get(), *createMockPluginConfig("testConfiguration.yaml"));

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

Expand All @@ -76,17 +95,25 @@ namespace ApiTracing

TEST(ConfigTests, getFunctionDefinitionsPath_configFromVmiCoreConfigFile_correctAbsolutePath)
{
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });
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)
{
auto config = std::make_unique<Config>(*createMockPluginConfig("testConfiguration.yaml"));
std::unique_ptr<MockPluginInterface> pluginInterface = std::make_unique<NiceMock<MockPluginInterface>>();
ON_CALL(*pluginInterface, newNamedLogger(_))
.WillByDefault([]() { return std::make_unique<NiceMock<VmiCore::MockLogger>>(); });

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

EXPECT_NO_THROW(YAML::LoadFile(config->getFunctionDefinitionsPath()));
}
Expand Down

0 comments on commit bc221ce

Please sign in to comment.