Skip to content

Commit 00b522f

Browse files
andy31415andreilitvinrestyled-commits
authored
Add tracing macros to addressresolve (#27180)
* 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 <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 945a991 commit 00b522f

21 files changed

+387
-54
lines changed

examples/chip-tool/BUILD.gn

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static_library("chip-tool-utils") {
101101
}
102102

103103
public_deps = [
104+
"${chip_root}/examples/common/tracing:commandline",
104105
"${chip_root}/src/app/server",
105106
"${chip_root}/src/app/tests/suites/commands/commissioner",
106107
"${chip_root}/src/app/tests/suites/commands/delay",
@@ -114,8 +115,6 @@ static_library("chip-tool-utils") {
114115
"${chip_root}/src/lib",
115116
"${chip_root}/src/lib/support/jsontlv",
116117
"${chip_root}/src/platform",
117-
"${chip_root}/src/tracing",
118-
"${chip_root}/src/tracing/log_json",
119118
"${chip_root}/third_party/inipp",
120119
"${chip_root}/third_party/jsoncpp",
121120
]

examples/chip-tool/commands/common/CHIPCommand.cpp

+3-24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "CHIPCommand.h"
2020

21+
#include <TracingCommandLineArgument.h>
2122
#include <controller/CHIPDeviceControllerFactory.h>
2223
#include <core/CHIPBuildConfig.h>
2324
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
@@ -26,9 +27,6 @@
2627
#include <lib/support/ScopedBuffer.h>
2728
#include <lib/support/TestGroupData.h>
2829

29-
#include <tracing/log_json/log_json_tracing.h>
30-
#include <tracing/registry.h>
31-
3230
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
3331
#include "TraceDecoder.h"
3432
#include "TraceHandlers.h"
@@ -82,15 +80,6 @@ CHIP_ERROR GetAttestationTrustStore(const char * paaTrustStorePath, const chip::
8280
return CHIP_NO_ERROR;
8381
}
8482

85-
using ::chip::Tracing::ScopedRegistration;
86-
using ::chip::Tracing::LogJson::LogJsonBackend;
87-
88-
LogJsonBackend log_json_backend;
89-
90-
// ScopedRegistration ensures register/unregister is met, as long
91-
// as the vector is cleared (and we do so when stopping tracing).
92-
std::vector<std::unique_ptr<ScopedRegistration>> tracing_backends;
93-
9483
} // namespace
9584

9685
CHIP_ERROR CHIPCommand::MaybeSetUpStack()
@@ -258,17 +247,7 @@ void CHIPCommand::StartTracing()
258247
{
259248
for (const auto & destination : mTraceTo.Value())
260249
{
261-
if (destination == "log")
262-
{
263-
if (!log_json_backend.IsInList())
264-
{
265-
tracing_backends.push_back(std::make_unique<ScopedRegistration>(log_json_backend));
266-
}
267-
}
268-
else
269-
{
270-
ChipLogError(AppServer, "Unknown trace destination: '%s'", destination.c_str());
271-
}
250+
chip::CommandLineApp::EnableTracingFor(destination.c_str());
272251
}
273252
}
274253

@@ -298,7 +277,7 @@ void CHIPCommand::StartTracing()
298277

299278
void CHIPCommand::StopTracing()
300279
{
301-
tracing_backends.clear();
280+
chip::CommandLineApp::StopTracing();
302281

303282
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
304283
chip::trace::DeInitTrace();

examples/chip-tool/commands/common/CHIPCommand.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "Command.h"
2626

27+
#include <TracingCommandLineArgument.h>
2728
#include <commands/common/CredentialIssuerCommands.h>
2829
#include <commands/example/ExampleCredentialIssuerCommands.h>
2930
#include <credentials/GroupDataProviderImpl.h>
@@ -85,7 +86,7 @@ class CHIPCommand : public Command
8586
AddArgument("trace_log", 0, 1, &mTraceLog);
8687
AddArgument("trace_decode", 0, 1, &mTraceDecode);
8788
#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
88-
AddArgument("trace-to", &mTraceTo, "Trace destinations, comma-separated (e.g. log)");
89+
AddArgument("trace-to", &mTraceTo, "Trace destinations, comma-separated (" SUPPORTED_COMMAND_LINE_TRACING_TARGETS ")");
8990
AddArgument("ble-adapter", 0, UINT16_MAX, &mBleAdapterId);
9091
AddArgument("storage-directory", &mStorageDirectory,
9192
"Directory to place chip-tool's storage files in. Defaults to $TMPDIR, with fallback to /tmp");

examples/common/tracing/BUILD.gn

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ config("default_config") {
2020
include_dirs = [ "." ]
2121
}
2222

23+
source_set("commandline") {
24+
sources = [
25+
"TracingCommandLineArgument.cpp",
26+
"TracingCommandLineArgument.h",
27+
]
28+
29+
deps = [
30+
"${chip_root}/src/lib/support",
31+
"${chip_root}/src/tracing",
32+
"${chip_root}/src/tracing/log_json",
33+
]
34+
35+
public_configs = [ ":default_config" ]
36+
37+
cflags = [ "-Wconversion" ]
38+
}
39+
2340
source_set("trace_handlers") {
2441
sources = [ "TraceHandlers.cpp" ]
2542

examples/common/tracing/TraceHandlers.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@
2222
#include <string>
2323
#include <vector>
2424

25-
#include "transport/TraceMessage.h"
2625
#include <lib/support/BytesToHex.h>
2726
#include <lib/support/CodeUtils.h>
2827
#include <lib/support/logging/CHIPLogging.h>
28+
#include <transport/TraceMessage.h>
2929

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

3333
namespace chip {
3434
namespace trace {
3535

36+
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
3637
namespace {
3738

3839
// Handles the output from the trace handlers.
@@ -350,5 +351,11 @@ void DeInitTrace()
350351
gTraceOutputs.UnregisterAllStreams();
351352
}
352353

354+
#else
355+
void AddTraceStream(TraceStream *) {}
356+
void InitTrace() {}
357+
void DeInitTrace() {}
358+
#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
359+
353360
} // namespace trace
354361
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#include <TracingCommandLineArgument.h>
19+
20+
#include <lib/support/StringSplitter.h>
21+
#include <lib/support/logging/CHIPLogging.h>
22+
#include <tracing/log_json/log_json_tracing.h>
23+
#include <tracing/registry.h>
24+
25+
#include <memory>
26+
#include <string>
27+
#include <vector>
28+
29+
namespace chip {
30+
namespace CommandLineApp {
31+
32+
namespace {
33+
using ::chip::Tracing::ScopedRegistration;
34+
using ::chip::Tracing::LogJson::LogJsonBackend;
35+
36+
// currently supported backends
37+
LogJsonBackend log_json_backend;
38+
39+
// ScopedRegistration ensures register/unregister is met, as long
40+
// as the vector is cleared (and we do so when stopping tracing).
41+
std::vector<std::unique_ptr<ScopedRegistration>> tracing_backends;
42+
43+
} // namespace
44+
45+
void EnableTracingFor(const char * cliArg)
46+
{
47+
chip::StringSplitter splitter(cliArg, ',');
48+
chip::CharSpan value;
49+
50+
while (splitter.Next(value))
51+
{
52+
if (value.data_equal(CharSpan::fromCharString("log")))
53+
{
54+
if (!log_json_backend.IsInList())
55+
{
56+
tracing_backends.push_back(std::make_unique<ScopedRegistration>(log_json_backend));
57+
}
58+
}
59+
else
60+
{
61+
ChipLogError(AppServer, "Unknown trace destination: '%s'", std::string(value.data(), value.size()).c_str());
62+
}
63+
}
64+
}
65+
66+
void StopTracing()
67+
{
68+
tracing_backends.clear();
69+
}
70+
71+
} // namespace CommandLineApp
72+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#pragma once
19+
20+
/// A string with supported command line tracing targets
21+
/// to be pretty-printed in help strings if needed
22+
#define SUPPORTED_COMMAND_LINE_TRACING_TARGETS "log"
23+
24+
namespace chip {
25+
namespace CommandLineApp {
26+
27+
/// Enable tracing based on the given command line argument
28+
/// like "log" or "log,perfetto" or similar
29+
///
30+
/// Single arguments as well as comma separated ones are accepted.
31+
///
32+
/// Calling this method multiple times is ok and will enable each of
33+
/// the given tracing modules if not already enabled.
34+
void EnableTracingFor(const char * cliArg);
35+
36+
/// If EnableTracingFor is called, this MUST be called as well
37+
/// to unregister tracing backends
38+
void StopTracing();
39+
40+
} // namespace CommandLineApp
41+
} // namespace chip

examples/minimal-mdns/BUILD.gn

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ executable("minimal-mdns-client") {
3636

3737
deps = [
3838
":minimal-mdns-example-common",
39+
"${chip_root}/examples/common/tracing:commandline",
3940
"${chip_root}/src/lib",
4041
"${chip_root}/src/lib/dnssd/minimal_mdns",
4142
]
@@ -50,6 +51,7 @@ executable("minimal-mdns-server") {
5051

5152
deps = [
5253
":minimal-mdns-example-common",
54+
"${chip_root}/examples/common/tracing:commandline",
5355
"${chip_root}/src/lib",
5456
"${chip_root}/src/lib/dnssd/minimal_mdns",
5557
"${chip_root}/src/lib/dnssd/minimal_mdns/responders",
@@ -65,6 +67,7 @@ executable("mdns-advertiser") {
6567

6668
deps = [
6769
":minimal-mdns-example-common",
70+
"${chip_root}/examples/common/tracing:commandline",
6871
"${chip_root}/src/lib",
6972
"${chip_root}/src/lib/dnssd",
7073
]

examples/minimal-mdns/advertiser.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <arpa/inet.h>
2121
#include <strings.h>
2222

23+
#include <TracingCommandLineArgument.h>
2324
#include <lib/dnssd/Advertiser.h>
2425
#include <lib/support/CHIPArgParser.hpp>
2526
#include <lib/support/CHIPMem.h>
@@ -81,6 +82,7 @@ constexpr uint16_t kOptionCommissioningRotatingId = 0x700;
8182

8283
constexpr uint16_t kOptionOperationalFabricId = 'f';
8384
constexpr uint16_t kOptionOperationalNodeId = 'n';
85+
constexpr uint16_t kOptionTraceTo = 't';
8486

8587
bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
8688
{
@@ -90,6 +92,9 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
9092
case kOptionEnableIpV4:
9193
gOptions.enableIpV4 = true;
9294
return true;
95+
case kOptionTraceTo:
96+
CommandLineApp::EnableTracingFor(aValue);
97+
return true;
9398
case kOptionAdvertisingMode:
9499
if (strcmp(aValue, "operational") == 0)
95100
{
@@ -187,6 +192,7 @@ OptionDef cmdLineOptionsDef[] = {
187192

188193
{ "fabric-id", kArgumentRequired, kOptionOperationalFabricId },
189194
{ "node-id", kArgumentRequired, kOptionOperationalNodeId },
195+
{ "trace-to", kArgumentRequired, kOptionTraceTo },
190196
{},
191197
};
192198

@@ -228,12 +234,20 @@ OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS"
228234
" --node-id <value>\n"
229235
" -n <value>\n"
230236
" Operational node id.\n"
237+
" -t <dest>\n"
238+
" --trace-to <dest>\n"
239+
" trace to the given destination (supported: " SUPPORTED_COMMAND_LINE_TRACING_TARGETS ").\n"
231240
"\n" };
232241

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

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

246+
void StopSignalHandler(int signal)
247+
{
248+
DeviceLayer::PlatformMgr().StopEventLoopTask();
249+
}
250+
237251
} // namespace
238252

239253
int main(int argc, char ** args)
@@ -316,8 +330,15 @@ int main(int argc, char ** args)
316330
return 1;
317331
}
318332

333+
signal(SIGTERM, StopSignalHandler);
334+
signal(SIGINT, StopSignalHandler);
335+
319336
DeviceLayer::PlatformMgr().RunEventLoop();
320337

338+
CommandLineApp::StopTracing();
339+
Dnssd::Resolver::Instance().Shutdown();
340+
DeviceLayer::PlatformMgr().Shutdown();
341+
321342
printf("Done...\n");
322343
return 0;
323344
}

0 commit comments

Comments
 (0)