Skip to content

Commit

Permalink
Add tracing macros to addressresolve (#27180)
Browse files Browse the repository at this point in the history
* Dnssd tracing

* Enable log-json-tracing for minmdns

* Centralize command line tracing args, add tracing to minmdns

* Make address resolve compile

* log errors and make shutdown actually work

* Reformat and add tracing for address lookup results

* Restyled by whitespace

* Restyled by clang-format

* Restyled by gn

* Fixed quotes on other macros too

* Restyled by clang-format

* Make advertiser support ctrl+c

* Address review comments

* Add better help on what tracing destinations exist

* Restyled by clang-format

* Fix typo in include and make tv casting app dependencies include the command line tracing support because it includes CHIPCommand.h

* Fix compile of tv-casting

* Fix TraceHandlers compile if tracing is disabled (tizen has it disabled)

* Restyle

* Make linter happy

* Make clang tidy happy

* Explain nolint in comment

---------

Co-authored-by: Andrei Litvin <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Dec 21, 2023
1 parent 3c311a4 commit 40028c7
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 54 deletions.
3 changes: 1 addition & 2 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static_library("chip-tool-utils") {
}

public_deps = [
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/app/server",
"${chip_root}/src/app/tests/suites/commands/commissioner",
"${chip_root}/src/app/tests/suites/commands/delay",
Expand All @@ -114,8 +115,6 @@ static_library("chip-tool-utils") {
"${chip_root}/src/lib",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
"${chip_root}/src/tracing",
"${chip_root}/src/tracing/log_json",
"${chip_root}/third_party/inipp",
"${chip_root}/third_party/jsoncpp",
]
Expand Down
27 changes: 3 additions & 24 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "CHIPCommand.h"

#include <TracingCommandLineArgument.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <core/CHIPBuildConfig.h>
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
Expand All @@ -26,9 +27,6 @@
#include <lib/support/ScopedBuffer.h>
#include <lib/support/TestGroupData.h>

#include <tracing/log_json/log_json_tracing.h>
#include <tracing/registry.h>

#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
#include "TraceDecoder.h"
#include "TraceHandlers.h"
Expand Down Expand Up @@ -82,15 +80,6 @@ CHIP_ERROR GetAttestationTrustStore(const char * paaTrustStorePath, const chip::
return CHIP_NO_ERROR;
}

using ::chip::Tracing::ScopedRegistration;
using ::chip::Tracing::LogJson::LogJsonBackend;

LogJsonBackend log_json_backend;

// ScopedRegistration ensures register/unregister is met, as long
// as the vector is cleared (and we do so when stopping tracing).
std::vector<std::unique_ptr<ScopedRegistration>> tracing_backends;

} // namespace

CHIP_ERROR CHIPCommand::MaybeSetUpStack()
Expand Down Expand Up @@ -258,17 +247,7 @@ void CHIPCommand::StartTracing()
{
for (const auto & destination : mTraceTo.Value())
{
if (destination == "log")
{
if (!log_json_backend.IsInList())
{
tracing_backends.push_back(std::make_unique<ScopedRegistration>(log_json_backend));
}
}
else
{
ChipLogError(AppServer, "Unknown trace destination: '%s'", destination.c_str());
}
chip::CommandLineApp::EnableTracingFor(destination.c_str());
}
}

Expand Down Expand Up @@ -298,7 +277,7 @@ void CHIPCommand::StartTracing()

void CHIPCommand::StopTracing()
{
tracing_backends.clear();
chip::CommandLineApp::StopTracing();

#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
chip::trace::DeInitTrace();
Expand Down
3 changes: 2 additions & 1 deletion examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "Command.h"

#include <TracingCommandLineArgument.h>
#include <commands/common/CredentialIssuerCommands.h>
#include <commands/example/ExampleCredentialIssuerCommands.h>
#include <credentials/GroupDataProviderImpl.h>
Expand Down Expand Up @@ -85,7 +86,7 @@ class CHIPCommand : public Command
AddArgument("trace_log", 0, 1, &mTraceLog);
AddArgument("trace_decode", 0, 1, &mTraceDecode);
#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
AddArgument("trace-to", &mTraceTo, "Trace destinations, comma-separated (e.g. log)");
AddArgument("trace-to", &mTraceTo, "Trace destinations, comma-separated (" SUPPORTED_COMMAND_LINE_TRACING_TARGETS ")");
AddArgument("ble-adapter", 0, UINT16_MAX, &mBleAdapterId);
AddArgument("storage-directory", &mStorageDirectory,
"Directory to place chip-tool's storage files in. Defaults to $TMPDIR, with fallback to /tmp");
Expand Down
17 changes: 17 additions & 0 deletions examples/common/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ config("default_config") {
include_dirs = [ "." ]
}

source_set("commandline") {
sources = [
"TracingCommandLineArgument.cpp",
"TracingCommandLineArgument.h",
]

deps = [
"${chip_root}/src/lib/support",
"${chip_root}/src/tracing",
"${chip_root}/src/tracing/log_json",
]

public_configs = [ ":default_config" ]

cflags = [ "-Wconversion" ]
}

source_set("trace_handlers") {
sources = [ "TraceHandlers.cpp" ]

Expand Down
9 changes: 8 additions & 1 deletion examples/common/tracing/TraceHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
#include <string>
#include <vector>

#include "transport/TraceMessage.h"
#include <lib/support/BytesToHex.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <transport/TraceMessage.h>

// For `s` std::string literal suffix
using namespace std::string_literals;

namespace chip {
namespace trace {

#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
namespace {

// Handles the output from the trace handlers.
Expand Down Expand Up @@ -350,5 +351,11 @@ void DeInitTrace()
gTraceOutputs.UnregisterAllStreams();
}

#else
void AddTraceStream(TraceStream *) {}
void InitTrace() {}
void DeInitTrace() {}
#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED

} // namespace trace
} // namespace chip
72 changes: 72 additions & 0 deletions examples/common/tracing/TracingCommandLineArgument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <TracingCommandLineArgument.h>

#include <lib/support/StringSplitter.h>
#include <lib/support/logging/CHIPLogging.h>
#include <tracing/log_json/log_json_tracing.h>
#include <tracing/registry.h>

#include <memory>
#include <string>
#include <vector>

namespace chip {
namespace CommandLineApp {

namespace {
using ::chip::Tracing::ScopedRegistration;
using ::chip::Tracing::LogJson::LogJsonBackend;

// currently supported backends
LogJsonBackend log_json_backend;

// ScopedRegistration ensures register/unregister is met, as long
// as the vector is cleared (and we do so when stopping tracing).
std::vector<std::unique_ptr<ScopedRegistration>> tracing_backends;

} // namespace

void EnableTracingFor(const char * cliArg)
{
chip::StringSplitter splitter(cliArg, ',');
chip::CharSpan value;

while (splitter.Next(value))
{
if (value.data_equal(CharSpan::fromCharString("log")))
{
if (!log_json_backend.IsInList())
{
tracing_backends.push_back(std::make_unique<ScopedRegistration>(log_json_backend));
}
}
else
{
ChipLogError(AppServer, "Unknown trace destination: '%s'", std::string(value.data(), value.size()).c_str());
}
}
}

void StopTracing()
{
tracing_backends.clear();
}

} // namespace CommandLineApp
} // namespace chip
41 changes: 41 additions & 0 deletions examples/common/tracing/TracingCommandLineArgument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

/// A string with supported command line tracing targets
/// to be pretty-printed in help strings if needed
#define SUPPORTED_COMMAND_LINE_TRACING_TARGETS "log"

namespace chip {
namespace CommandLineApp {

/// Enable tracing based on the given command line argument
/// like "log" or "log,perfetto" or similar
///
/// Single arguments as well as comma separated ones are accepted.
///
/// Calling this method multiple times is ok and will enable each of
/// the given tracing modules if not already enabled.
void EnableTracingFor(const char * cliArg);

/// If EnableTracingFor is called, this MUST be called as well
/// to unregister tracing backends
void StopTracing();

} // namespace CommandLineApp
} // namespace chip
3 changes: 3 additions & 0 deletions examples/minimal-mdns/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ executable("minimal-mdns-client") {

deps = [
":minimal-mdns-example-common",
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/lib",
"${chip_root}/src/lib/dnssd/minimal_mdns",
]
Expand All @@ -50,6 +51,7 @@ executable("minimal-mdns-server") {

deps = [
":minimal-mdns-example-common",
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/lib",
"${chip_root}/src/lib/dnssd/minimal_mdns",
"${chip_root}/src/lib/dnssd/minimal_mdns/responders",
Expand All @@ -65,6 +67,7 @@ executable("mdns-advertiser") {

deps = [
":minimal-mdns-example-common",
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/lib",
"${chip_root}/src/lib/dnssd",
]
Expand Down
21 changes: 21 additions & 0 deletions examples/minimal-mdns/advertiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <arpa/inet.h>
#include <strings.h>

#include <TracingCommandLineArgument.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CHIPMem.h>
Expand Down Expand Up @@ -81,6 +82,7 @@ constexpr uint16_t kOptionCommissioningRotatingId = 0x700;

constexpr uint16_t kOptionOperationalFabricId = 'f';
constexpr uint16_t kOptionOperationalNodeId = 'n';
constexpr uint16_t kOptionTraceTo = 't';

bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
{
Expand All @@ -90,6 +92,9 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
case kOptionEnableIpV4:
gOptions.enableIpV4 = true;
return true;
case kOptionTraceTo:
CommandLineApp::EnableTracingFor(aValue);
return true;
case kOptionAdvertisingMode:
if (strcmp(aValue, "operational") == 0)
{
Expand Down Expand Up @@ -187,6 +192,7 @@ OptionDef cmdLineOptionsDef[] = {

{ "fabric-id", kArgumentRequired, kOptionOperationalFabricId },
{ "node-id", kArgumentRequired, kOptionOperationalNodeId },
{ "trace-to", kArgumentRequired, kOptionTraceTo },
{},
};

Expand Down Expand Up @@ -228,12 +234,20 @@ OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS"
" --node-id <value>\n"
" -n <value>\n"
" Operational node id.\n"
" -t <dest>\n"
" --trace-to <dest>\n"
" trace to the given destination (supported: " SUPPORTED_COMMAND_LINE_TRACING_TARGETS ").\n"
"\n" };

HelpOptions helpOptions("advertiser", "Usage: advertiser [options]", "1.0");

OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr };

void StopSignalHandler(int signal)
{
DeviceLayer::PlatformMgr().StopEventLoopTask();
}

} // namespace

int main(int argc, char ** args)
Expand Down Expand Up @@ -316,8 +330,15 @@ int main(int argc, char ** args)
return 1;
}

signal(SIGTERM, StopSignalHandler);
signal(SIGINT, StopSignalHandler);

DeviceLayer::PlatformMgr().RunEventLoop();

CommandLineApp::StopTracing();
Dnssd::Resolver::Instance().Shutdown();
DeviceLayer::PlatformMgr().Shutdown();

printf("Done...\n");
return 0;
}
Loading

0 comments on commit 40028c7

Please sign in to comment.