Skip to content
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

Add tracing macros to addressresolve #27180

Merged
merged 25 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
20d46dd
Dnssd tracing
andreilitvin Jun 9, 2023
d6edeb6
Enable log-json-tracing for minmdns
andreilitvin Jun 9, 2023
3031816
Centralize command line tracing args, add tracing to minmdns
andreilitvin Jun 9, 2023
c04ccd5
Make address resolve compile
andreilitvin Jun 9, 2023
58de0f6
log errors and make shutdown actually work
andreilitvin Jun 9, 2023
eceda2e
Reformat and add tracing for address lookup results
andreilitvin Jun 9, 2023
b5fd594
Merge branch 'master' into dnssd_tracing
andreilitvin Jun 9, 2023
6120735
Restyled by whitespace
restyled-commits Jun 9, 2023
18f55b9
Restyled by clang-format
restyled-commits Jun 9, 2023
a459f35
Restyled by gn
restyled-commits Jun 9, 2023
b1991f3
Fixed quotes on other macros too
andreilitvin Jun 9, 2023
63e4af6
Restyled by clang-format
restyled-commits Jun 9, 2023
2c7b75f
Make advertiser support ctrl+c
andreilitvin Jun 9, 2023
137129d
Address review comments
andreilitvin Jun 9, 2023
d58a0e5
Add better help on what tracing destinations exist
andreilitvin Jun 9, 2023
e4d6caa
Restyled by clang-format
restyled-commits Jun 9, 2023
c3f7b07
Fix typo in include and make tv casting app dependencies include the …
andreilitvin Jun 9, 2023
ef36649
Fix compile of tv-casting
andy31415 Jun 12, 2023
539ac38
Fix TraceHandlers compile if tracing is disabled (tizen has it disabled)
andy31415 Jun 12, 2023
b52115a
Restyle
andy31415 Jun 12, 2023
ae1663f
Merge branch 'master' into dnssd_tracing
andy31415 Jun 12, 2023
444d37b
Make linter happy
andy31415 Jun 12, 2023
f37b833
Make clang tidy happy
andy31415 Jun 12, 2023
65a6dd1
Explain nolint in comment
andy31415 Jun 12, 2023
4a8e31d
Merge branch 'master' into dnssd_tracing
andreilitvin Jun 13, 2023
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
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