blob: 011ec5f53277824630fd616c3c7e45de10bbac9d [file] [log] [blame]
Robert Oliver19146912020-07-14 10:49:48 -04001// Copyright 2020 The Pigweed Authors
2//
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
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
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
13// the License.
14//==============================================================================
15// BUID
16// ninja -C out
17// host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_trigger
18//
19// RUN
20// .out/host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_trigger
21// trace.bin
22//
23// DECODE
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
26//
27// VIEW
28// In chrome navigate to chrome://tracing, and load the trace.json file.
29
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"
36
37namespace {
38
39constexpr uint32_t kTriggerId = 3;
40constexpr uint32_t kTriggerStartTraceRef =
41 PW_TRACE_REF_DATA(PW_TRACE_TYPE_ASYNC_START,
42 "Processing", // Module
43 "Job", // Label
44 PW_TRACE_FLAGS_DEFAULT,
45 "Process",
46 "@pw_py_struct_fmt:B");
47constexpr uint32_t kTriggerEndTraceRef = PW_TRACE_REF(PW_TRACE_TYPE_ASYNC_END,
48 "Processing", // Module
49 "Job", // Label
50 PW_TRACE_FLAGS_DEFAULT,
51 "Process");
52
53} // namespace
54
Wyatt Hepler63afc002021-02-08 13:20:26 -080055pw_trace_TraceEventReturnFlags TraceEventCallback(
56 void* /* user_data */,
57 uint32_t trace_ref,
58 pw_trace_EventType /* event_type */,
59 const char* /* module */,
60 uint32_t trace_id,
61 uint8_t /* flags */) {
Robert Oliver19146912020-07-14 10:49:48 -040062 if (trace_ref == kTriggerStartTraceRef && trace_id == kTriggerId) {
63 PW_LOG_INFO("Trace capture started!");
64 PW_TRACE_SET_ENABLED(true);
65 }
66 if (trace_ref == kTriggerEndTraceRef && trace_id == kTriggerId) {
67 PW_LOG_INFO("Trace capture ended!");
68 return PW_TRACE_EVENT_RETURN_FLAGS_DISABLE_AFTER_PROCESSING;
69 }
70 return 0;
71}
72
73int main(int argc, char** argv) { // Take filename as arg
74 if (argc != 2) {
75 PW_LOG_ERROR("Expected output file name as argument.\n");
76 return -1;
77 }
78
79 // Register trigger callback
80 pw::trace::Callbacks::Instance().RegisterEventCallback(
81 TraceEventCallback, pw::trace::CallbacksImpl::kCallOnEveryEvent);
82
83 // Ensure tracing is off at start, the trigger will turn it on.
84 PW_TRACE_SET_ENABLED(false);
85
86 // Dump trace data to the file passed in.
87 pw::trace::TraceToFile trace_to_file(argv[1]);
88
89 PW_LOG_INFO("Running trigger example...");
90 RunTraceSampleApp();
91 return 0;
Wyatt Hepler63afc002021-02-08 13:20:26 -080092}