1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
14 //==============================================================================
17 // host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_trigger
20 // .out/host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_trigger
24 // python pw_trace_tokenized/py/trace_tokenized.py -i trace.bin -o trace.json
25 // ./out/host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_basic
28 // In chrome navigate to chrome://tracing, and load the trace.json file.
30 #include "pw_log/log.h"
31 #include "pw_trace/example/sample_app.h"
32 #include "pw_trace/trace.h"
33 #include "pw_trace_tokenized/example/trace_to_file.h"
34 #include "pw_trace_tokenized/trace_callback.h"
35 #include "pw_trace_tokenized/trace_tokenized.h"
39 constexpr uint32_t kTriggerId = 3;
40 constexpr uint32_t kTriggerStartTraceRef =
41 PW_TRACE_REF_DATA(PW_TRACE_TYPE_ASYNC_START,
42 "Processing", // Module
44 PW_TRACE_FLAGS_DEFAULT,
46 "@pw_py_struct_fmt:B");
47 constexpr uint32_t kTriggerEndTraceRef = PW_TRACE_REF(PW_TRACE_TYPE_ASYNC_END,
48 "Processing", // Module
50 PW_TRACE_FLAGS_DEFAULT,
55 pw_trace_TraceEventReturnFlags TraceEventCallback(void* user_data,
57 pw_trace_EventType event_type,
62 PW_UNUSED(event_type);
65 if (trace_ref == kTriggerStartTraceRef && trace_id == kTriggerId) {
66 PW_LOG_INFO("Trace capture started!");
67 PW_TRACE_SET_ENABLED(true);
69 if (trace_ref == kTriggerEndTraceRef && trace_id == kTriggerId) {
70 PW_LOG_INFO("Trace capture ended!");
71 return PW_TRACE_EVENT_RETURN_FLAGS_DISABLE_AFTER_PROCESSING;
76 int main(int argc, char** argv) { // Take filename as arg
78 PW_LOG_ERROR("Expected output file name as argument.\n");
82 // Register trigger callback
83 pw::trace::Callbacks::Instance().RegisterEventCallback(
84 TraceEventCallback, pw::trace::CallbacksImpl::kCallOnEveryEvent);
86 // Ensure tracing is off at start, the trigger will turn it on.
87 PW_TRACE_SET_ENABLED(false);
89 // Dump trace data to the file passed in.
90 pw::trace::TraceToFile trace_to_file(argv[1]);
92 PW_LOG_INFO("Running trigger example...");